--- subscriptions/contribs/subscriptions_mail_from_solr/subscriptions_mail_from_solr.info	Thu Jul 12 09:40:37 2012
+++ subscriptions/contribs/subscriptions_mail_from_solr/subscriptions_mail_from_solr.info	Tue Jul 10 14:56:16 2012
@@ -0,0 +1,7 @@
+name = Subscriptions mail from Apache Solr
+description = Add subscriptions to the queue from a search via Apache Solr.
+package = "Subscriptions"
+dependencies[] = subscriptions_mail
+files[] = subscriptions_mail_from_solr.class.php
+core = 7.x
+version = "7.x-1.0-beta1"
--- subscriptions/contribs/subscriptions_mail_from_solr/subscriptions_mail_from_solr.class.php	Thu Jul 12 09:40:13 2012
+++ subscriptions/contribs/subscriptions_mail_from_solr/subscriptions_mail_from_solr.class.php	Wed Jul 11 16:26:15 2012
@@ -0,0 +1,227 @@
+<?php
+
+/**
+ * @file
+ * Subscriptions mail from Apache Solr
+ * Add subscriptions to the queue from a search via Apache Solr.
+ * Expects subscriptions_queue entries of the following format.
+ * INSERT INTO `subscriptions_queue` (`sqid`, `uid`, `name`, `language`,
+ *  `module`, `field`, `value`, `author_uid`, `send_interval`, `digest`,
+ *  `load_args`, `load_function`, `is_new`, `last_sent`, `suspended`)
+ * VALUES(229, 181336, 'jsmith', '',
+ *  'node', 'type', 'job', -1, 1, -1,
+ *  '293510', 'subscriptions_content_load_node', 0, 1341467750, 0);
+ * 
+ * A test insert for the from_solr table.
+ * INSERT INTO `subscriptions_mail_from_solr` (`uid`, `name`, `language`,
+ *  `module`, `field`, `value`, `author_uid`, `send_interval`, `digest`,
+ *  `fq`,
+ *  `load_function`, `is_new`, `last_sent`, `suspended`)
+ * VALUES (181335, 'dot', '',
+ *  '', '', '', -1, 1, -1,
+ *  'sm_vid_Location:Sydney',
+ *  '', 1, 1341896459, 0);
+
+ */
+
+/**
+ * Your basic processing class().
+ * Automatically loaded by Drupal when called from the module.
+ * Called only when needed druring the cron run.
+ * 
+ */
+class subscriptions_mail_from_solr {
+  protected $module_name = 'subscriptions_mail_from_solr';
+  protected $display_name = 'Subscriptions mail from Apache Solr';
+  protected $field_separator = '|';
+  protected $pair_separator = ':';
+  /*
+   * The module name is passed so other modules can reuse the class.
+   */
+  function __construct($module_name = null) {
+    $this->module_name_set($module_name);
+  }
+  function module_name() {
+    return $this->module_name;
+  }
+  function module_name_set($name = null) {
+    if (!empty($name)) {
+      $this->module_name = $name;
+      $this->display_name(ucfirst(str_replace('_', ' ', $name)));
+    }
+  }
+  function display_name() {
+    return $this->display_name;
+  }
+  function display_name_set($name = null) {
+    if (!empty($name)) {
+      $this->display_name = $name;
+    }
+  }
+  function fq($pairs) {
+    return implode($this->field_separator, $pairs);
+  }
+  function pair($name, $value) {
+    if (is_array($value)) {
+      $value = '"' . implode('" OR "', $value) . '"';
+    }
+    return $name . $this->pair_separator . $value;
+  }
+  /*
+   * Provide a standard simplified log function for this module.
+   */
+  function log($message, $variables = array(), $severity = WATCHDOG_NOTICE) {
+    watchdog($this->display_name(), $message, $variables, $severity);
+  }
+  function error($message, $variables = array()) {
+    $this->log($message, $variables, WATCHDOG_ERROR);
+  }
+
+  /**
+   */
+  function schema() {
+    module_load_include('install', $this->module_name());
+    $schema = subscriptions_mail_from_solr_schema();
+    return $schema[$this->module_name()];
+  }
+
+  /**
+   * Implements hook subscriptions_mail_preload_queue.
+   * This selection query should match the Subscriptions mail selection query.
+   * $fq is the field query parameter defined for Apache Solr.
+   */
+  function insert($columns) {
+    if (!is_array($columns)) {
+      $this->error('Insert($columns): $columns is not an array. $columns: @value', array('@value' => serialize($columns)));
+      return 0;
+    }
+    if (!isset($columns['uid'])) {
+      global $user;
+      $columns['uid'] = $user->uid;
+    }
+    module_load_include('install', $this->module_name());
+    $schema = $this->schema();
+    $fields = $schema['fields'];
+    $error = FALSE;
+    foreach ($columns as $name => $value) {
+      if (!isset($fields[$name])) {
+        $this->error('Insert: Column @name is not a valid column.', array('@name' => $name));
+        $error = TRUE;
+      }
+      if (isset($fields[$name]['type'])) {
+        $type = $fields[$name]['type'];
+        if ($type == 'serial') {
+          $this->error('Insert: Column @name is automatically assigned by the database as should not be in the insert.', array('@name' => $name));
+          $error = TRUE;
+        }
+        elseif ($type == 'int') {
+          $int_value = (integer) $value;
+          if ($int_value != $value) {
+            $this->error('Insert: Column @name is does not have an integer value: @value', array('@name' => $name, '@value' => serialize($value)));
+            $error = TRUE;
+          }
+        }
+        elseif ($type == 'text') {
+          if (!is_string($value)) {
+            $this->error('Insert: Column @name is not a string: @value', array('@name' => $name, '@value' => serialize($value)));
+            $error = TRUE;
+          }
+        }
+        elseif ($type == 'varchar') {
+          if (!is_string($value)) {
+            $this->error('Insert: Column @name is not a string: @value', array('@name' => $name, '@value' => serialize($value)));
+            $error = TRUE;
+          }
+          elseif (isset($fields[$name]['length']) and strlen($value) > $fields[$name]['length']) {
+            $this->error('Insert: Column @name is too long: @value', array('@name' => $name, '@value' => serialize($value)));
+            $error = TRUE;
+          }
+        }
+      }
+    }
+    if (!isset($columns['fq'])) {
+      $this->error('Insert: Column fq is missing. Columns: @value', array('@value' => serialize($value)));
+      $error = TRUE;
+    }
+    if ($error) {
+      return 0;
+    }
+    $id = db_insert($this->module_name())->fields($columns)->execute();
+    $this->log('Inserted @id with fq: @fq', array('@id' => $id, '@fq' => $columns['fq']));
+  }
+
+  /**
+   * Insert function to add a row to the database table after some basic
+   * checking of columns. Nothing spectacular here.
+   */
+  function preload_queue($time) {
+    global $user, $language;
+    $query = 'SELECT * FROM {' . $this->module_name() . '} WHERE suspended = 0 AND last_sent + send_interval < :time';
+    $results = db_query($query, array(':time' => $time))->fetchAll();
+    foreach($results as $row) {
+      $saved_user = $user;
+      $saved_language = $language;
+      drupal_save_session(FALSE);
+      $user = user_load($row->uid);
+      $fq = explode($this->field_separator(), $row->fq);
+      $new_queue_entries = $this->from($fq);
+      $user = $saved_user;
+      $language = $saved_language;
+      drupal_save_session(TRUE);
+      db_update($this->module_name())
+        ->condition('smfsid', $row->smfsid)
+        ->fields(array('last_sent' => $time))
+        ->execute();
+      if (!empty($new_queue_entries)) {
+        // Time to create queue entries.
+        //watchdog('Subscriptions mail use view', '$new_queue_entries_in_a_table: @value', array('@value' => serialize($new_queue_entries_in_a_table)));
+        $queue_fields_default = (array) $row;
+        unset($queue_fields_default['smfsid']);
+        unset($queue_fields_default['fq']);
+        if ($queue_fields_default['load_function'] == '') {
+          $queue_fields_default['load_function'] = 'subscriptions_content_load_node';
+        }
+        $queue_fields_default['name'] = $user->name;
+        $queue_fields_default['language'] = $user->language;
+        foreach ($new_queue_entries as $nid) {
+          $queue_fields = $queue_fields_default;
+          if ($queue_fields['module'] == '' and $queue_fields['field'] == '' and $queue_fields['value'] == '') {
+            $queue_fields['module'] = 'node';
+            $queue_fields['field'] = 'nid';
+            $queue_fields['value'] = $nid;
+          }
+          $queue_fields['load_args'] = $nid;
+          $sqid = db_insert('subscriptions_queue')
+            ->fields($queue_fields)
+            ->execute();
+          $this->log('Queued user @uid, node: @nid, queue id: @sqid', array(
+            '@uid' => $queue_fields['uid'],
+            '@nid' => $nid,
+            '@sqid' => $sqid,
+          ));
+        }
+      }
+    }
+    return true;
+  }
+
+  /**
+   * Get results from an Apache Solr search.
+   * $fq is the field query parameter defined for Apache Solr.
+   * We expect the fq parameter and do not use anything else.
+   * This function is not tested with anything else.
+   */
+  function from($fq) {
+    $node_ids = array();
+    if(!empty($fq)) {
+      if(is_string($fq)) {
+        $fq = array($fq);
+      }
+      $results = apachesolr_search_run(null, array('fq' => $fq));
+      foreach ($results as $result) {
+        $node_ids[] = $result['fields']['entity_id'];
+      }
+    }
+    return $node_ids;
+  }
+}
--- subscriptions/contribs/subscriptions_mail_from_solr/subscriptions_mail_from_solr.install	Thu Jul 12 09:40:43 2012
+++ subscriptions/contribs/subscriptions_mail_from_solr/subscriptions_mail_from_solr.install	Wed Jul 11 15:24:01 2012
@@ -0,0 +1,43 @@
+<?php
+
+/**
+ * @file
+ * Subscriptions mail from Apache Solr
+ * Add subscriptions to the queue from a search via Apache Solr.
+ */
+
+/**
+ * Implements hook_schema().
+ *
+ * @return array
+ */
+function subscriptions_mail_from_solr_schema() {
+
+  $schema = array();
+
+  $schema['subscriptions_mail_from_solr'] = array(
+    'description'     => 'Add subscriptions to the queue from Apache Solr with the parameters named here.',
+    'fields'          => array(
+      'smfsid'        => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+      'uid'           => array('type' => 'int', 'not null' => FALSE),
+      'module'        => array('type' => 'varchar', 'length' => '64', 'not null' => FALSE),
+      'field'         => array('type' => 'varchar', 'length' => '32', 'not null' => FALSE),
+      'value'         => array('type' => 'varchar', 'length' => '237', 'not null' => FALSE),
+      'author_uid'    => array('type' => 'int', 'not null' => FALSE),
+      'send_interval' => array('type' => 'int', 'not null' => FALSE),
+      'digest'        => array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE),
+      'fq'       => array(
+        'type' => 'text',
+        'not null' => TRUE,
+        'description' => 'Apache Solr field query.',
+      ),
+      'load_function' => array('type' => 'varchar', 'length' => '60', 'not null' => TRUE, 'default' => ''),
+      'is_new'        => array('type' => 'int', 'size' => 'tiny', 'not null' => FALSE),
+      'last_sent'     => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'suspended'     => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0)),
+    'primary key'     => array('smfsid'),
+    'indexes'         => array('uid' => array('uid')),
+  );
+
+  return $schema;
+}
--- subscriptions/contribs/subscriptions_mail_from_solr/subscriptions_mail_from_solr.module	Thu Jul 12 09:40:30 2012
+++ subscriptions/contribs/subscriptions_mail_from_solr/subscriptions_mail_from_solr.module	Thu Jul 12 09:38:19 2012
@@ -0,0 +1,49 @@
+<?php
+
+/**
+ * @file
+ * Subscriptions mail from Apache Solr
+ * Add subscriptions to the queue from a search via Apache Solr.
+ */
+
+/**
+ * Module internal name, the machine name.
+ */
+function subscriptions_mail_from_solr_module_name() {
+  return 'subscriptions_mail_from_solr';
+}
+
+/**
+ * Get the processing object.
+ */
+function subscriptions_mail_from_solr() {
+  static $object = NULL;
+  if ($object === NULL) {
+    $class = subscriptions_mail_from_solr_module_name();
+    $object = new $class(subscriptions_mail_from_solr_module_name());
+  }
+  return $object;
+}
+
+/**
+ * Insert function to add a row to the database table after some basic checking.
+ */
+function subscriptions_mail_from_solr_insert($columns) {
+  return subscriptions_mail_from_solr()->insert($columns);
+}
+
+/**
+ * Implements hook subscriptions_mail_preload_queue.
+ */
+function subscriptions_mail_from_solr_subscriptions_menu_preload_queue($time) {
+  return subscriptions_mail_from_solr()->preload_queue($time);
+}
+
+/**
+ * Implements hook_views_api().
+ */
+function subscriptions_mail_from_solr_views_api() {
+  return array(
+    'api' => 3,
+  );
+}
--- subscriptions/contribs/subscriptions_mail_from_solr/subscriptions_mail_from_solr.views.inc	Thu Jul 12 09:40:24 2012
+++ subscriptions/contribs/subscriptions_mail_from_solr/subscriptions_mail_from_solr.views.inc	Wed Jul 11 16:39:34 2012
@@ -0,0 +1,53 @@
+<?php
+/**
+ * @file
+ * Subscriptions mail from Apache Solr
+ * Add subscriptions to the queue from a search via Apache Solr.
+ */
+
+/**
+ * Implementation of hook_views_data()
+ */
+function subscriptions_mail_from_solr_views_data() {
+  drupal_load('module', 'subscriptions_mail_from_solr');
+  $object = subscriptions_mail_from_solr();
+  $table = $object->module_name();
+  $schema = $object->schema();
+  $fields = $schema['fields'];
+  $data = array();
+  $data[$table]['table']['group'] = t($object->display_name());
+  $data[$table]['table']['base'] = array(
+    'field' => 'uid',
+    'title' => t($object->display_name()),
+    'help' => t($object->display_name()),
+    'weight' => -10,
+  );
+  
+  foreach ($fields as $machine_name => $field) {
+    ;
+    $data[$table][$machine_name] = array(
+      'title' => t($machine_name),
+      'help' => t(''),
+      'field' => array('click sortable' => TRUE),
+      'sort' => array('handler' => 'views_handler_sort'),
+  );
+    if ($field['type'] == 'int' or $field['type'] == 'serial') {
+      $data[$table][$machine_name]['filter'] = array('handler' => 'views_handler_filter_numeric');
+    }
+    else {
+      $data[$table][$machine_name]['filter'] = array('handler' => 'views_handler_filter_string');
+      $data[$table][$machine_name]['argument'] = array('handler' => 'views_handler_argument_string');
+    }
+    if (isset($field['description'])) {
+      $data[$table][$machine_name]['help'] = $field['description'];
+    }
+  }
+  
+  /* The following join lets you add user information to your views. */
+  $data['users']['table']['join'][$table] = array(
+    'field' => 'uid',
+    'left_field' => 'uid',
+  );
+  
+  return $data;
+}
