CMS  Version 3.9
subscription_manager.inc
Go to the documentation of this file.
1 <?php
7 Fakoli::using("forum", "email");
8 
10 {
12  {
13 
14  }
15 
17  {
18  global $config;
19 
20  // Don't query if this is a new topic.
21  if($message->topic_id)
22  {
23  $constraint = "WHERE forum_id = {$message->forum_id} AND subscription_type = ". subscription_instant_notification ." AND (topic_id = 0 OR topic_id = {$message->topic_id})";
25  }
26 
27  if(count($subscribers) > 0)
28  {
29  $mgr = new UserManager();
30  $emailField = $mgr->getEmailField();
32  $subject = $config["sitename"] . " Forum Instant Notification";
33  trace("instant_notification:: email content $emailContent", 3);
34 
35  foreach($subscribers as $subscriber)
36  {
37  $l_user = $subscriber->User();
38  if($l_user)
39  {
40  $emailTo = $l_user->$emailField;
41  $emailHandler = new EmailHandler($emailTo, $subject, $emailContent);
42  $emailHandler->send();
43  }
44  }
45  }
46  }
47 
48 
50  {
51  global $config;
52 
53  $firstPost = $message->Topic()->FirstPost();
54  $url = "http://" . $config["http_host"] . "/forum_topic?forum_id={$firstPost->forum_id}&topic_id={$firstPost->topic_id}";
55 
56  $content = "Forum Topic: \"{$firstPost->title}\" posted by {$firstPost->Author()->getFullName()}\n";
57  $content .= "http://" . $config["http_host"] . "/forum_topic?forum_id={$firstPost->forum_id}&topic_id={$firstPost->topic_id}\n\n";
58  $content .= "Reply: \"{$message->title}\" posted by {$message->Author()->getFullName()}\n\n";
59  $content .= strip_tags($message->message);
61 
62  return $content;
63  }
64 
66  {
67  global $config;
68 
69  $messageFooter = "\n\nTo unsubscribe to these messages or change your subscription preferences, go to ";
70  $messageFooter .= "http://" . $config["http_host"] . "/forum_subscription_preferences\n\n";
71 
72  return $messageFooter;
73  }
74 
75 
76  /*
77  * Subscribe to entire forum, leave topic_id blank.
78  * If user is subscribed to any topics within this forum,
79  * delete the subscriptions.
80  */
81 
82  static function updateSubscription($subscription, $subscription_type)
83  {
84  // update subscription type for either a forum or topic subscription
86  $subscription_type != $subscription->subscription_type)
87  {
88  trace("update_subscription:: subscription change", 3);
89  $subscription->filter = new InclusionFilter("subscription_type");
90  $subscription->subscription_type = $subscription_type;
91  $subscription->save();
92  }
94  {
95  trace("update_subscription:: unsubscribe", 3);
96  $subscription->delete();
97  }
98  }
99 
100  static function renderSubscriptionType($subscription)
101  {
102  $options = SubscriptionManager::formatSubscriptionOptions($subscription->Forum(), $subscription);
103  $current = $subscription->subscription_type;
104  $forum_subscription_id = $subscription->forum_subscription_id;
105 
106  unset($options[0]);
107  foreach($options as $value => $name)
108  {
109  $selected = ($current == $value) ? " checked" : "";
110  $html .= "<input style='border: none' type='radio' id='subscription[{$forum_subscription_id}]' name='subscription[{$forum_subscription_id}]' value='$value' $selected>$name</option><br>\n";
111  }
112 
113  return $html;
114  }
115 
116  static function formatSubscriptionOptions($forum, $forumSubscription, $topicSubscriptionCount = 0)
117  {
119 
121  {
122  unset($options[0]);
123  }
124 
125  if(!$forumSubscription && !$topicSubscriptionCount)
126  {
128  }
129 
130  if($topicSubscriptionCount > 0)
131  {
132  $options[subscription_daily_digest] = "Subscribe to <b>" . $options[subscription_daily_digest] . "</b> for {$forum->title}";
133  $options[subscription_instant_notification] = "Subscribe to <b>" . $options[subscription_instant_notification] . "</b> for {$forum->title}";
134  $options[subscription_unsubscribe] .= " from topic(s) in {$forum->title}";
135  }
136 
137  return $options;
138  }
139 
140 }
141 
142 
143 class ForumSubscriptionHelper
144 {
145  var $forum;
146  var $topic;
147  var $title;
149  var $forumSubscription;
150  var $topicSubscription;
151  var $subscription;
153  var $topics_page = false;
154  var $subscriptionOptions = array();
155 
156  var $icons = array(
157  "forum" => "/fakoli/images/icon_subscribed_to_topic.png",
158  "topic" => "/fakoli/images/icon_subscribed_to_topic.png");
159 
160  var $buttons = array(
161  "forum" => "/fakoli/images/button_subscribe_forum.png",
162  "topic" => "/fakoli/images/button_subscribe_topic.png");
163 
164  var $subscribeTypes = array("new_topic", "new_forum", "update_topic", "update_forum", "topic_to_forum");
165  var $subscripeType = "";
166 
167  function __construct($forum, $topic = null)
168  {
169  $this->forum = $forum;
170  $this->topic = $topic;
171 
172  $this->forumSubscription = ForumSubscription::findUserSubscription($this->forum->forum_id, 0);
173 
174  if(!$this->topic)
175  {
176  $this->topicSubscriptions = ForumSubscription::getUserTopicSubscriptions($forum->forum_id);
177  $this->subscription = $this->forumSubscription;
178  if(!$this->forumSubscription && count($this->topicSubscriptions))
179  {
180  $this->subscribeType = "topic_to_forum";
181  }
182  else
183  {
184  $this->subscribeType = (!$this->forumSubscription) ? "new_forum" : "update_forum";
185  }
186  }
187  else
188  {
189  $this->topics_page = true;
190  $this->subscription = ForumSubscription::findUserSubscription($this->forum->forum_id, $this->topic->topic_id);
191  $this->topicSubscription = $this->subscription;
192  if($this->forumSubscription)
193  {
194  $this->subscribeType = "update_forum";
195  }
196  else
197  {
198  $this->subscribeType = ($this->topicSubscription) ? "update_topic" : "new_topic";
199  }
200  }
201 
202  $this->title = $this->getTitle();
203  }
204 
205  function getTitle()
206  {
207  if($this->topic)
208  {
209  $title = $this->topic->getTitle();
210  }
211  else
212  {
213  $title = preg_replace("/forum$/i", "", $this->forum->title) . " Forum";
214  }
215 
216  return $title;
217  }
218 
219 
224  function drawLink()
225  {
226  global $user;
227  if(!$user) return;
228 
230 
231  $count = count($this->topicSubscriptions);
232 
233  if($this->subscribeType == "new_topic" || $this->subscribeType == "new_forum")
234  {
235  $this->drawButtonLink();
236  }
237  else if($this->subscribeType == "topic_to_forum")
238  {
239  $text = "You are Currently Subscribed to $count ";
240  $text .= ($count > 1) ? "Topics" : "Topic";
241  $text .= " in this Forum";
242  $topOffset = ($count > 0) ? $count * 10 : 0;
243  $this->drawIconLink($text, $topOffset);
244  }
245  else if($this->subscribeType == "update_forum" || $this->subscribeType == "update_topic")
246  {
247  $text = "You are Currently Subscribed to";
248  if(!$this->topics_page)
249  {
250  $text .= " this Forum";
251  }
252  else if($this->topics_page && $this->forumSubscription)
253  {
254  $text .= " all Topics in this Forum";
255  }
256  else
257  {
258  $text .= " this Topic";
259  }
260  $this->drawIconLink($text);
261  }
262  }
263 
264  function drawIconLink($text, $topOffset = 0)
265  {
266  $icon = ($this->topics_page) ? $this->icons["topic"] : $this->icons["forum"];
267  $alt = ($this->topics_page) ? "subscribe_topic" : "subscribe_forum";
268 
269  $img = "<img src='$icon' alt='{$alt}' style='border: none'>";
270  echo "<p style=\"text-align: right\">$img&nbsp;&nbsp;<a id='subscription_link' href=\"#\" onclick=\"forumSubscriptionMgr.showSubscriptionDialog(); return false\">$text</a></p>\n";
271  }
272 
273  function drawButtonLink($button, $topOffset = 0)
274  {
275  $button = ($this->topics_page) ? $this->buttons["topic"] : $this->buttons["forum"];
276 
277  $img = "<img src='$button' style='border: none'>";
278  echo "<div style=\"display: inline-block; float: right\"><a id='subscription_link' href=\"#\" onclick=\"forumSubscriptionMgr.showSubscriptionDialog(); return false\">$img</a></div>\n";
279  }
280 
281  function drawHeaderText()
282  {
283  if(!$this->topics_page)
284  {
285  $text = $this->formatForumSubscriptionHeader();
286  }
287  else
288  {
289  $text = $this->formatTopicSubscriptionHeader();
290  }
291 
292  echo $text;
293  }
294 
295  function formatForumSubscriptionHeader()
296  {
297  $subscription = $this->subscription;
298  $count = count($this->topicSubscriptions);
299  $subscribeForumText = "If you prefer, you can subscribe to the forum as a whole.";
300 
301  if(!$subscription AND $count == 0)
302  {
303  $text = "<p>You can subscribe to the forum's daily digest or subscribe to instant notification to receive an email whenever a new topic or reply is posted.</p>\n";
304  }
305  elseif(!$subscription AND $count > 0 AND $count < 20)
306  {
307  $text = "<p>You are currently subscribed to the following topic(s) in this forum:</p>";
308  $text .= "<ul>\n" . formatItems($this->topicSubscriptions, "<li>{ForumSubscriptionHelper::formatTopicSubscription}</li>", "\n") . "</ul>\n";
309  $text .= "<p>{$subscribeForumText}</p>";
310  }
311  elseif(!$subscription AND $count > 0)
312  {
313  $text = "<p>You are currently subscribed to several topics in this forum:</p>";
314  $text .= "<p>{$subscribeForumText}</p>";
315  }
316  else if($subscription)
317  {
318  $subscriptionType = ForumSubscription::$subscriptionOptions[$subscription->subscription_type];
319  $text = "<p>You are currently subscribed to $subscriptionType for this forum. You can change your subscription type or unsubscribe.</p>";
320  }
321 
322  return $text;
323  }
324 
325  static function formatTopicSubscription($subscription)
326  {
327  return $subscription->ForumTopic()->FirstPost()->title;
328  }
329 
330  function formatTopicSubscriptionHeader()
331  {
332  $subscription = $this->topicSubscription;
333 
334  if(!$subscription)
335  {
336  return "<p>You can subscribe to the topics's daily digest or subscribe to instant notification to receive an email whenever a reply is posted.</p>\n";
337  }
338  else
339  {
340  $subscriptionType = ForumSubscription::$subscriptionOptions[$subscription->subscription_type];
341  return "<p>You are currently subscribed to $subscriptionType for this topic. You can change your subscription type or unsubscribe.</p>";
342  }
343  }
344 
371  function formatSubscriptionOptions()
372  {
374 
375  $subscription = $this->subscription;
376  $count = count($this->topicSubscriptions);
377 
378  $title = "<i>". $this->title . "</i>";
379 
380  $options[subscription_daily_digest] = "Subscribe to <b>" . $options[subscription_daily_digest] . "</b> for {$title}";
381  $options[subscription_instant_notification] = "Subscribe to <b>" . $options[subscription_instant_notification] . "</b> for {$title}";
382 
383  $unsubscribeText = "Unsubscribe from";
384  if($subscription)
385  {
386  $options[subscription_unsubscribe] = $unsubscribeText . " {$title}";
387  }
388  else if(!$subscription && !$this->topics_page && $count)
389  {
390  $options[subscription_unsubscribe] = $unsubscribeText . " from all topics in {$title}";
391  }
392 
393  return $options;
394  }
395 
401  function saveForumSubscription($form)
402  {
403  $subscription = $form->data;
404  $type = $subscription->subscription_type;
405 
406  if($subscription->forum_subscription_id && $type != subscription_unsubscribe)
407  {
408  $subscription->filter = new InclusionFilter("subscription_type");
409  $subscription->save();
410  }
411  else if($type != subscription_unsubscribe)
412  {
413  trace("**************forum saveForumSubscription type is not unsubscribe - save new subscription");
414  if(!$subscription->topics_page)
415  {
416  $this->forumUnsubscribe();
417  }
418  $subscription->save();
419  }
420  else if($type == subscription_unsubscribe)
421  {
422  $this->forumUnsubscribe();
423  }
424  else
425  {
426  trace("update_subscription:: nothing to do", 3);
427  }
428 
429  return true;
430  }
431 
436  function forumUnsubscribe()
437  {
438  trace("SubscriptionManager:: forumUnsubscribe", 3);
439  global $user;
440 
441  if(!$user) return;
442 
444  $pk = $user->getPrimaryKey();
445  $user_id = $user->$pk;
446  $forumSubscription->delete("WHERE forum_id={$this->forum->forum_id} AND $user_id={$user_id}");
447  }
448 
449 
450 
451  function writeScript()
452  {
453  $forum_subscription_id = ($this->subscription) ? $this->subscription->forum_subscription_id : 0;
454  $topic_id = $this->topic->topic_id ? $this->topic->topic_id : 0;
455 
456  ob_start();
457 
458  ?>
459 
460  <script type="text/javascript" src="/components/forum/js/forum_subscription_manager.js"></script>
461  <script type="text/javascript">
462 
463  var forumSubscriptionMgr;
464 
465  window.addEvent('domready', function()
466  {
467  forumSubscriptionMgr = new ForumSubscriptionManager(
468  <?php echo $this->forum->forum_id ?>, <?php echo $topic_id ?>, <?php echo $forum_subscription_id ?>, '<?php echo $this->title ?>');
469  });
470 
471  </script>
472 
473  <?php
474  $script .= ob_get_contents();
475  ob_end_clean();
476 
477  return $script;
478  }
479 }
480 
494 class ForumTopicSubscriptionHelper extends ForumSubscriptionHelper
495 {
496  function __construct($forum, $topic)
497  {
498  $this->forum = $forum;
499  $this->topic = $topic;
500 
501  $this->forumSubscription = ForumSubscription::findUserSubscription($this->forum->forum_id, 0);
502 
503  $this->subscription = ForumSubscription::findUserSubscription($this->forum->forum_id, $this->topic->topic_id);
504 
505  if($this->forumSubscription)
506  {
507  $this->subscribeType = "update_forum";
508  }
509  else
510  {
511  $this->subscribeType = ($this->subscription) ? "update_topic" : "new_topic";
512  }
513 
514  $this->title = $this->topic->getTitle();
515 
516  }
517 
518  function drawLink()
519  {
520  global $user;
521  if(!$user) return;
522 
523  if($this->subscribeType == "new_topic")
524  {
525  $this->drawButtonLink();
526  }
527  else if($this->subscribeType == "update_topic")
528  {
529  $text = "You are Currently Subscribed to";
530  $text .= ($this->forumSubscription) ? " all Topics in this Forum" : " this Topic";
531  $this->drawIconLink($text);
532  }
533  }
534 
535  function drawHeaderText()
536  {
537  echo $this->formatHeader();
538  }
539 
540  function formatHeader()
541  {
542  if(!$this->subscription)
543  {
544  return "<p>You can subscribe to the topics's daily digest or subscribe to instant notification to receive an email whenever a reply is posted.</p>\n";
545  }
546  else
547  {
548  $subscriptionType = ForumSubscription::$subscriptionOptions[$this->subscription->subscription_type];
549  return "<p>You are currently subscribed to $subscriptionType for this topic. You can change your subscription type or unsubscribe.</p>";
550  }
551  }
552 
553 
554 }?>
$constraint
$user_id
$form
$name
Definition: upload.inc:54
$icon
Definition: upload.inc:92
if(! $token) $subscriber
$bookmark title
static using()
Import the datamodels, views and manifest for the specified component(s).
Definition: core.inc:116
static getUserTopicSubscriptions($forum_id)
static findUserSubscription($forum_id, $topic_id=0)
Provides the interface to the user model for the application.
global $user
const subscription_unsubscribe
static updateSubscription($subscription, $subscription_type)
static formatSubscriptionOptions($forum, $forumSubscription, $topicSubscriptionCount=0)
static formatTopicInstantNotification($message)
const subscription_instant_notification
static renderSubscriptionType($subscription)
static sendInstantNotifications($message)
const subscription_daily_digest
global $config
Definition: import.inc:4
$message
Definition: mail_to.inc:49
if(! $blog->published||! $blog->enable_rss_feed||!checkRole($blog->allow_read)) $url
Definition: rss.inc:58
$messageFooter
if(array_key_exists("HTTP_IF_MODIFIED_SINCE", $_SERVER)) $content
Definition: styles.css.inc:24
$forumSubscription
$forum_subscription_id
$topicSubscriptions
$button
Definition: show.inc:6
$topic
Definition: topic_form.inc:42
$topic_id
Definition: topic_form.inc:40