CMS  Version 3.9
send_daily_digest.inc
Go to the documentation of this file.
1 <?php
2 /*
3  * Description: For each users who subscribes to one or more
4  * forums, send an email with a summary of messages added to the
5  * topics to which they subscribe.
6  *
7  * File is run dailey through windows scheduler.
8  *
9  * Steps:
10  *
11  * 1) Create an array of daily summaries for each forum topic.
12  *
13  * 2) Get the set of forum subscripions grouped by user. For each
14  * user, build the digest for each of their forum topic subscriptions.
15  *
16  * 3) Save the email message into an array. Send all at the end.
17  *
18  * @author: Janice Gallant for Sonjara, Inc.
19  *
20  * 10/26/2010
21  */
22 
23 
24 Fakoli::using("forum", "email", "user");
25 
26 /*
27  * Get all the new posts from today for each topic
28  */
29 $today = now();
30 $yesterday = date('Y/m/d H:i:s', mktime(date("H"), date("i"), date("s"), date("m"), date("d")-1, date("y")));
31 
32 $messages = groupedQuery(ForumMessage, "WHERE last_modified BETWEEN '$yesterday' AND '$today' ORDER BY last_modified", "topic_id");
33 
34 if(count($messages) == 0)
35 {
36  trace("Send Daily Digest, no new messages", 3);
37  return;
38 }
39 
40 $topics = indexedQuery(ForumTopic, "", "topic_id");
41 
42 global $config;
43 
44 /*
45  * Create the digest for each topic in the forum
46  */
47 if(count($messages) > 0)
48 {
49  foreach($messages as $topic_id => $topicMessages)
50  {
51  $firstPost = $topics[$topic_id]->FirstPost();
52  $author = $firstPost->Author()->getFullName();
53  $url = "http://" . $config["http_host"] . "/forum_topic?forum_id={$firstPost->forum_id}&topic_id={$firstPost->topic_id}";
54  $forum_id = $firstPost->forum_id;
55  $digest[$forum_id][$topic_id] .= "Forum Topic: \"{$firstPost->title}\" posted by {$author}\n";
56  $digest[$forum_id][$topic_id] .= $url . "\n\n";
57  foreach($topicMessages as $message)
58  {
59  $author = $message->Author()->getFullName();
60  $digest[$forum_id][$topic_id] .= "Reply: \"{$message->title}\" posted by {$author}<br><br>";
61  }
62  }
63 }
64 
65 /*
66  * Loop through the forum subscribers first by each forum,
67  * so we can output the forum title at the start of the
68  * topic group in the email message for each user.
69  *
70  * Then, reindex the subscriptions to that forum by user_id.
71  * Loop through each user subscription and add the digest
72  * for the topics to which they subscribe.
73  *
74  * Store the digest messages in an array, indexed by the
75  * user_id.
76  */
77 
78 
79 $forumKeys = implode(",", array_keys(reindexList($messages, "forum_id")));
80 $forums = indexedQuery(Forum, "WHERE forum_id IN ($forumKeys)", "forum_id");
81 $forumSubscribers = groupedQuery(ForumSubscription, "WHERE forum_id IN ($forumKeys) AND subscription_type = ".subscription_daily_digest, "forum_id");
82 
83 $forumTopics = reindexList($topics, "forum_id");
84 
85 if(count($forumSubscribers) > 0)
86 {
88  {
90  $userSubscriptions = reindexList($forumSubscriptions, "user_id");
91 
92  foreach($userSubscriptions as $l_user_id => $subscriptions)
93  {
94  $digestMessages[$l_user_id] .= $forum->title . " Forum\n\n";
95  // Whole forum subscriber
96  if(!is_array($subscriptions))
97  {
99  foreach($forumTopics as $fTopic)
100  {
101  $digestMessages[$l_user_id] .= $digest[$forum_id][$fTopic->topic_id];
102  }
103  }
104  else // individual topic subscriber
105  {
106  foreach($subscriptions as $subscription)
107  {
108  $digestMessages[$l_user_id] .= $digest[$forum_id][$subscription->topic_id];
109  }
110  }
111  }
112  }
113 }
114 
115 
116 
117 /*
118  * Finally, for each digestMessage in the array, send an
119  * email to the user. Add a footer link to unsubscribe.
120  */
121 //echo "<pre>"; print_r($digestMessages); echo "</pre>";
122 
124 $userClass = $mgr->getUserClass();
125 $emailField = $mgr->getEmailField();
127 $pk = $userClassObj->getPrimaryKey();
128 $subscribers = indexedQuery($userClass, "WHERE $pk IN (SELECT user_id FROM forum_subscription)", $pk);
129 $subject = $config["sitename"] . " Daily Forum Digest";
131 
132 if(count($digestMessages) > 0)
133 {
134  foreach($digestMessages as $l_user_id => $subscriberMessage)
135  {
136  $l_user = $subscribers[$l_user_id];
137  $l_userEmail = $l_user->$emailField;
138  $body = $subscriberMessage . $messageFooter;
139  $email = new EmailHandler($l_userEmail, $subject, $body);
140  $email->send();
141  }
142 }
143 
144 ?>
$forum_id
Definition: forum_form.inc:39
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
Definition: forum.inc:42
Provides the interface to the user model for the application.
const subscription_daily_digest
$message
Definition: mail_to.inc:49
if(! $blog->published||! $blog->enable_rss_feed||!checkRole($blog->allow_read)) $url
Definition: rss.inc:58
$forumSubscribers
if(count($forumSubscribers) > 0) $mgr
if(count($messages) > 0) $forumKeys
if(count($messages)==0) $topics
global $config
$forumTopics
$messageFooter
$forumSubscriptions
$topic_id
Definition: topic_form.inc:40