Initial commit
[sandbox/stevem/1838516.git] / vss_email.module
1 <?php
2
3 /**
4 * Implementation of hook_cron().
5 */
6 function vss_email_cron() {
7 // Detects views changes and sends email with new items.
8 // Uses cron queue for scalability
9 $queue = DrupalQueue::get('vss_email');
10 $result = db_query("SELECT sid, uid FROM {views_savedsearches};");
11 while ($sub = $result->fetchObject()) {
12 $queue->createItem($sub);
13 //vss_email_search_notification($sub);
14 }
15 }
16
17
18 /**
19 * Implementation of hook_cron_queue_info()
20 */
21 function vss_email_cron_queue_info() {
22 $queues['vss_email'] = array(
23 'worker callback' => 'vss_email_search_notification',
24 'time' => 180,
25 );
26 return $queues;
27 }
28
29
30 /* do the actual processing of the notifications */
31 function vss_email_search_notification($sub) {
32 // has uid seen sid before?
33 // (uid's first "viewing" of sid is the baseline, and changes are
34 // emailed thereafter.)
35 $variable = 'vss_email_seen_' . $sub->uid . '_' . $sub->sid;
36 $already_seen = variable_get($variable, FALSE);
37 if (!$already_seen) variable_set($variable, TRUE);
38
39 // load saved view info, apply filters, run query
40 $savedsearch = views_savedsearches_load($sub->sid);
41 $view_name = db_query("SELECT name FROM {views_view} WHERE vid = :vid", array(':vid' => $savedsearch->vid))->fetchField();
42 $view = views_get_view($view_name);
43 if (!$view) return;
44 $view->set_display('page');
45 $view->set_exposed_input($savedsearch->filters);
46 $view->set_items_per_page(0);
47 $view->build();
48 $view->pre_execute();
49 $view->execute();
50
51 // Get ids in the views from the past, compare to current results
52 $current_ids = vss_email_get_ids($savedsearch->sid);
53 $new_items = array();
54 foreach ($view->result as $item) {
55 $identifier = $item->nid;
56 if (!in_array($identifier, $current_ids)) {
57 $current_ids[] = $identifier;
58 $new_items[] = $item;
59 }
60 }
61 vss_email_store_ids($savedsearch->sid, $current_ids);
62
63 // new items since last time?
64 if (count($new_items) > 0 && $already_seen) {
65 $titles = array();
66 foreach ($new_items as $item) {
67 if (isset($item->nid)) {
68 $node = node_load($item->nid);
69 $titles[] = l($node->title, 'node/' . $node->nid);
70 }
71 }
72 // send an email
73 $user = user_load($sub->uid);
74 $params = array(
75 'user' => $user,
76 '!search_title' => check_plain(vss_email_2title($sub->sid)),
77 '!search_items' => join("\n", $titles),
78 );
79 drupal_mail('vss_email', 'views_notification', $user->mail, user_preferred_language($user), $params);
80 }
81 }
82
83
84 /**
85 * Converts sid to savedsearches name
86 */
87 function vss_email_2title($sid = 0) {
88 $name = db_query("SELECT name FROM {views_savedsearches} WHERE sid = :sid", array(':sid' => $sid))->fetchField();
89 return $name;
90 }
91
92
93 /**
94 * Gets already known ids for the given views.
95 *
96 * @param $sid
97 * Id of the subscription
98 */
99 function vss_email_get_ids($sid = 0) {
100 $query = db_select('vss_email', 'b')
101 ->fields('b', array('itemid'))
102 ->condition('b.sid', $sid)
103 ->execute();
104 $ids = array();
105 while ($row = $query->fetchObject()) {
106 $ids[] = $row->itemid;
107 }
108 return $ids;
109 }
110
111
112 /**
113 * Store the item ids for the given savedsearch.
114 *
115 * @param $sid
116 * Id of the subscription
117 * @param $ids
118 * Ids of the items in the saved view
119 */
120 function vss_email_store_ids($sid = 0, $ids = array()) {
121 // clear the decks
122 $deleted = db_delete('vss_email')
123 ->condition('sid', $sid)
124 ->execute();
125
126 // save items
127 foreach ($ids as $id) {
128 $insert = db_insert('vss_email')
129 ->fields(array(
130 'sid' => $sid,
131 'itemid' => $id,
132 ))
133 ->execute();
134 }
135 }
136
137
138 /* implementation of hook_mail */
139 function vss_email_mail($key, &$message, $params) {
140 switch ($key) {
141 case 'views_notification':
142 $text = variable_get('vss_email_savedsearch_new', '');
143 $text = token_replace($text, $params);
144 $message['subject'] = t('New search results', array('langcode' => $message['language']->language));
145 $message['body'][] = t($text, $params, array('langcode' => $message['language']->language));
146 break;
147 }
148 }
149
150
151 /**
152 * Implements hook_menu().
153 */
154 function btmodule_menu() {
155 $items = array();
156
157 $items['admin/config/vss_email'] = array(
158 'title' => 'VSS Email',
159 'page callback' => 'drupal_get_form',
160 'page arguments' => array('vss_email_admin_form'),
161 'access arguments' => array('access administration pages'),
162 'type' => MENU_NORMAL_ITEM,
163 );
164
165 return $items;
166 }
167
168
169 function vss_email_admin_form() {
170 $form = array();
171
172 $default = "Dear [username],\n\nThere are new results for your stored search '!search_title:\n\n!search_items";
173 $form[] = array(
174 '#type' => 'textarea',
175 '#title' => t('VSS email template'),
176 '#default_value' => t(variable_get('vss_email_savedsearch_new', $default)),
177 );
178
179 return system_settings_form($form);
180 }