4 * Implementation of hook_cron().
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);
19 * Implementation of hook_cron_queue_info()
21 function vss_email_cron_queue_info() {
22 $queues['vss_email'] = array(
23 'worker callback' => 'vss_email_search_notification',
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
);
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);
44 $view->set_display('page');
45 $view->set_exposed_input($savedsearch->filters
);
46 $view->set_items_per_page(0);
51 // Get ids in the views from the past, compare to current results
52 $current_ids = vss_email_get_ids($savedsearch->sid
);
54 foreach ($view->result as
$item) {
55 $identifier = $item->nid
;
56 if (!in_array($identifier, $current_ids)) {
57 $current_ids[] = $identifier;
61 vss_email_store_ids($savedsearch->sid
, $current_ids);
63 // new items since last time?
64 if (count($new_items) > 0 && $already_seen) {
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
);
73 $user = user_load($sub->uid
);
76 '!search_title' => check_plain(vss_email_2title($sub->sid
)),
77 '!search_items' => join("\n", $titles),
79 drupal_mail('vss_email', 'views_notification', $user->mail, user_preferred_language($user), $params);
85 * Converts sid to savedsearches name
87 function vss_email_2title($sid = 0) {
88 $name = db_query("SELECT name FROM {views_savedsearches} WHERE sid = :sid", array(':sid' => $sid))->fetchField();
94 * Gets already known ids for the given views.
97 * Id of the subscription
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)
105 while ($row = $query->fetchObject()) {
106 $ids[] = $row->itemid
;
113 * Store the item ids for the given savedsearch.
116 * Id of the subscription
118 * Ids of the items in the saved view
120 function vss_email_store_ids($sid = 0, $ids = array()) {
122 $deleted = db_delete('vss_email')
123 ->condition('sid', $sid)
127 foreach ($ids as
$id) {
128 $insert = db_insert('vss_email')
138 /* implementation of hook_mail */
139 function vss_email_mail($key, &$message, $params) {
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
));
152 * Implements hook_menu().
154 function btmodule_menu() {
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
,
169 function vss_email_admin_form() {
172 $default = "Dear [username],\n\nThere are new results for your stored search '!search_title:\n\n!search_items";
174 '#type' => 'textarea',
175 '#title' => t('VSS email template'),
176 '#default_value' => t(variable_get('vss_email_savedsearch_new', $default)),
179 return system_settings_form($form);