summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve2012-11-12 16:29:13 (GMT)
committer Steve2012-11-12 16:29:13 (GMT)
commit0a4f30ac849ddac0b963373d676d8defe71d173a (patch)
treeea903452fe64a242fb896a0e693035808bdf09cd
Initial commit
-rw-r--r--vss_email.info10
-rw-r--r--vss_email.install43
-rw-r--r--vss_email.module180
3 files changed, 233 insertions, 0 deletions
diff --git a/vss_email.info b/vss_email.info
new file mode 100644
index 0000000..860e39c
--- /dev/null
+++ b/vss_email.info
@@ -0,0 +1,10 @@
+name = Views Saved Search Email
+description = Sends email notifications for Views Saved Search results.
+core = 7.x
+
+package = Views
+
+dependencies[] = views_savedsearches
+dependencies[] = token
+
+
diff --git a/vss_email.install b/vss_email.install
new file mode 100644
index 0000000..da38855
--- /dev/null
+++ b/vss_email.install
@@ -0,0 +1,43 @@
+<?php
+
+/**
+ * @file
+ * Install file for VSS Email
+ */
+
+/**
+ * Implementation of hook_schema().
+ */
+function vss_email_schema() {
+ $schema['vss_email'] = array(
+ 'description' => 'Stores items for each saved search',
+ 'fields' => array(
+ 'sid' => array(
+ 'description' => 'The subscription id.',
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE),
+ 'itemid' => array(
+ 'description' => 'Identifier of the item (can be whatever item .',
+ 'type' => 'int',
+ 'unsigned' => TRUE,
+ 'not null' => TRUE),
+ ),
+ 'primary key' => array('sid', 'itemid'),
+ );
+ return $schema;
+}
+
+/**
+ * Implementation of hook_install().
+ */
+function vss_email_install() {
+ drupal_install_schema('vss_email');
+}
+
+/**
+ * Implementation of hook_uninstall().
+ */
+function vss_email_uninstall() {
+ drupal_uninstall_schema('vss_email');
+}
diff --git a/vss_email.module b/vss_email.module
new file mode 100644
index 0000000..651bb2e
--- /dev/null
+++ b/vss_email.module
@@ -0,0 +1,180 @@
+<?php
+
+/**
+ * Implementation of hook_cron().
+ */
+function vss_email_cron() {
+ // Detects views changes and sends email with new items.
+ // Uses cron queue for scalability
+ $queue = DrupalQueue::get('vss_email');
+ $result = db_query("SELECT sid, uid FROM {views_savedsearches};");
+ while ($sub = $result->fetchObject()) {
+ $queue->createItem($sub);
+ //vss_email_search_notification($sub);
+ }
+}
+
+
+/**
+ * Implementation of hook_cron_queue_info()
+ */
+function vss_email_cron_queue_info() {
+ $queues['vss_email'] = array(
+ 'worker callback' => 'vss_email_search_notification',
+ 'time' => 180,
+ );
+ return $queues;
+}
+
+
+/* do the actual processing of the notifications */
+function vss_email_search_notification($sub) {
+ // has uid seen sid before?
+ // (uid's first "viewing" of sid is the baseline, and changes are
+ // emailed thereafter.)
+ $variable = 'vss_email_seen_' . $sub->uid . '_' . $sub->sid;
+ $already_seen = variable_get($variable, FALSE);
+ if (!$already_seen) variable_set($variable, TRUE);
+
+ // load saved view info, apply filters, run query
+ $savedsearch = views_savedsearches_load($sub->sid);
+ $view_name = db_query("SELECT name FROM {views_view} WHERE vid = :vid", array(':vid' => $savedsearch->vid))->fetchField();
+ $view = views_get_view($view_name);
+ if (!$view) return;
+ $view->set_display('page');
+ $view->set_exposed_input($savedsearch->filters);
+ $view->set_items_per_page(0);
+ $view->build();
+ $view->pre_execute();
+ $view->execute();
+
+ // Get ids in the views from the past, compare to current results
+ $current_ids = vss_email_get_ids($savedsearch->sid);
+ $new_items = array();
+ foreach ($view->result as $item) {
+ $identifier = $item->nid;
+ if (!in_array($identifier, $current_ids)) {
+ $current_ids[] = $identifier;
+ $new_items[] = $item;
+ }
+ }
+ vss_email_store_ids($savedsearch->sid, $current_ids);
+
+ // new items since last time?
+ if (count($new_items) > 0 && $already_seen) {
+ $titles = array();
+ foreach ($new_items as $item) {
+ if (isset($item->nid)) {
+ $node = node_load($item->nid);
+ $titles[] = l($node->title, 'node/' . $node->nid);
+ }
+ }
+ // send an email
+ $user = user_load($sub->uid);
+ $params = array(
+ 'user' => $user,
+ '!search_title' => check_plain(vss_email_2title($sub->sid)),
+ '!search_items' => join("\n", $titles),
+ );
+ drupal_mail('vss_email', 'views_notification', $user->mail, user_preferred_language($user), $params);
+ }
+}
+
+
+/**
+ * Converts sid to savedsearches name
+ */
+function vss_email_2title($sid = 0) {
+ $name = db_query("SELECT name FROM {views_savedsearches} WHERE sid = :sid", array(':sid' => $sid))->fetchField();
+ return $name;
+}
+
+
+/**
+ * Gets already known ids for the given views.
+ *
+ * @param $sid
+ * Id of the subscription
+ */
+function vss_email_get_ids($sid = 0) {
+ $query = db_select('vss_email', 'b')
+ ->fields('b', array('itemid'))
+ ->condition('b.sid', $sid)
+ ->execute();
+ $ids = array();
+ while ($row = $query->fetchObject()) {
+ $ids[] = $row->itemid;
+ }
+ return $ids;
+}
+
+
+/**
+ * Store the item ids for the given savedsearch.
+ *
+ * @param $sid
+ * Id of the subscription
+ * @param $ids
+ * Ids of the items in the saved view
+ */
+function vss_email_store_ids($sid = 0, $ids = array()) {
+ // clear the decks
+ $deleted = db_delete('vss_email')
+ ->condition('sid', $sid)
+ ->execute();
+
+ // save items
+ foreach ($ids as $id) {
+ $insert = db_insert('vss_email')
+ ->fields(array(
+ 'sid' => $sid,
+ 'itemid' => $id,
+ ))
+ ->execute();
+ }
+}
+
+
+/* implementation of hook_mail */
+function vss_email_mail($key, &$message, $params) {
+ switch ($key) {
+ case 'views_notification':
+ $text = variable_get('vss_email_savedsearch_new', '');
+ $text = token_replace($text, $params);
+ $message['subject'] = t('New search results', array('langcode' => $message['language']->language));
+ $message['body'][] = t($text, $params, array('langcode' => $message['language']->language));
+ break;
+ }
+}
+
+
+/**
+ * Implements hook_menu().
+ */
+function btmodule_menu() {
+ $items = array();
+
+ $items['admin/config/vss_email'] = array(
+ 'title' => 'VSS Email',
+ 'page callback' => 'drupal_get_form',
+ 'page arguments' => array('vss_email_admin_form'),
+ 'access arguments' => array('access administration pages'),
+ 'type' => MENU_NORMAL_ITEM,
+ );
+
+ return $items;
+}
+
+
+function vss_email_admin_form() {
+ $form = array();
+
+ $default = "Dear [username],\n\nThere are new results for your stored search '!search_title:\n\n!search_items";
+ $form[] = array(
+ '#type' => 'textarea',
+ '#title' => t('VSS email template'),
+ '#default_value' => t(variable_get('vss_email_savedsearch_new', $default)),
+ );
+
+ return system_settings_form($form);
+}