diff --git a/includes/newsletter.admin.inc b/includes/newsletter.admin.inc index f492d8a..0c8b1ea 100644 --- a/includes/newsletter.admin.inc +++ b/includes/newsletter.admin.inc @@ -1338,8 +1338,38 @@ function newsletter_template_edit($form, &$form_state, $template) { '#type' => 'checkbox', '#default_value' => $template->exposed, '#title' => t('Exposed'), + '#weight' => 7, + ); + $form['use_view'] = array( + '#type' => 'checkbox', + '#default_value' => isset($template->use_view)?$template->use_view:0, + '#title' => t('Use view to sent content'), + '#weight' => 8, + ); + + $views = views_get_all_views(); + $options = array(''=>t('None')); + foreach($views as $id => $view): + if($view->disabled !== 1){ + foreach($view->display as $did => $display): + $options[$id.'|'.$did] = $view->human_name.' - '.$display->display_title; + endforeach; + } + endforeach; + + $form['select_view'] = array( + '#type' => 'select', + '#options' => $options, + '#default_value' => isset($template->select_view)?$template->select_view:'', + '#title' => t('Choose the view'), '#weight' => 9, ); + $form['argument_view'] = array( + '#type' => 'textfield', + '#default_value' => isset($template->argument_view)?$template->argument_view:'', + '#title' => t('Use arguments to the view'), + '#weight' => 10, + ); $form['actions'] = array( '#type' => 'item', diff --git a/includes/newsletter.automated.inc b/includes/newsletter.automated.inc index d98eb9f..8b46704 100644 --- a/includes/newsletter.automated.inc +++ b/includes/newsletter.automated.inc @@ -93,18 +93,38 @@ class NewsletterAutomated extends NewsletterBasic { * that gets the current nodes to be sent with the current newsletter. */ protected function getQuery() { - $tids = newsletter_get_template_terms($this->template->ntid); - $tids = array_keys($tids) ? array_keys($tids) : array(0); - - $query = db_select('taxonomy_index', 'tax'); - $query->fields('tax', array('nid')); - $query->join('newsletter_list', 'list', 'list.nlid = ' . (int) $this->list->nlid); - $query->join('node', 'node', 'tax.nid = node.nid'); - $query->addField('node', 'created', 'created'); - $query->condition('tax.tid', $tids, 'IN'); - $query->where('(list.last_sent <= node.created) OR (list.last_sent IS NULL )'); - if (!$this->manualSendRate && !$this->customSendRate) { - $query->where('list.send_again = CURDATE() OR list.send_again IS NULL'); + if(isset($this->template->use_view)){ + $view = explode('|',$this->template->select_view); + if(isset($this->template->argument_view) && !empty($this->template->argument_view)){ + $view = views_get_view_result($view[0],$view[1],$this->template->argument_view); + } + else{ + $view = views_get_view_result($view[0],$view[1]); + } + $nids = array(); + foreach($view as $item): + $nids[] = $item->nid; + endforeach; + $query = db_select('node', 'node'); + $query->fields('node', array('nid')); + $query->addField('node', 'created', 'created'); + $query->condition('node.nid', $nids, 'IN'); + } + else { + $tids = newsletter_get_template_terms($this->template->ntid); + $tids = array_keys($tids) ? array_keys($tids) : array(0); + + $query = db_select('taxonomy_index', 'tax'); + $query->fields('tax', array('nid')); + $query->join('newsletter_list', 'list', 'list.nlid = ' . (int) $this->list->nlid); + $query->join('node', 'node', 'tax.nid = node.nid'); + $query->addField('node', 'created', 'created'); + $query->condition('tax.tid', $tids, 'IN'); + $query->where('(list.last_sent <= node.created) OR (list.last_sent IS NULL )'); + + if (!$this->manualSendRate && !$this->customSendRate) { + $query->where('list.send_again = CURDATE() OR list.send_again IS NULL'); + } } if ($this->customSendRate) { $query->range(0, (int) $this->list->send_rate); diff --git a/includes/newsletter.subscriber.controller.inc b/includes/newsletter.subscriber.controller.inc index 5561c80..29fc691 100644 --- a/includes/newsletter.subscriber.controller.inc +++ b/includes/newsletter.subscriber.controller.inc @@ -71,6 +71,7 @@ class NewsletterSubscriberController extends DrupalDefaultEntityController { $subscriber->created = REQUEST_TIME; $subscriber->hash = drupal_hmac_base64(REQUEST_TIME . $subscriber->email, drupal_get_hash_salt() . ip_address()); $subscriber->confirmation_timestamp = $needs_confirm ? 0 : REQUEST_TIME; + $subscriber->confirmed = 1; drupal_write_record('newsletter_subscriber', $subscriber); field_attach_insert('newsletter_subscriber', $subscriber); diff --git a/includes/newsletter.template.controller.inc b/includes/newsletter.template.controller.inc index 13a703b..fe2422d 100644 --- a/includes/newsletter.template.controller.inc +++ b/includes/newsletter.template.controller.inc @@ -20,6 +20,9 @@ class NewsletterTemplateController extends DrupalDefaultEntityController { $template->subject = NULL; $template->basic = 0; $template->exposed = 0; + $template->use_view = 0; + $template->select_view = NULL; + $template->argument_view = NULL; return $template; } @@ -38,6 +41,10 @@ class NewsletterTemplateController extends DrupalDefaultEntityController { */ public function save($template) { $template->exposed = isset($template->exposed) ? $template->exposed : 0; + if(!isset($template->use_view) || (isset($template->use_view) && $template->use_view === 0)){ + $template->select_view = ''; + $template->argument_view = ''; + } field_attach_presave('newsletter_template', $template); if (!isset($template->ntid)) { $template->created = REQUEST_TIME; diff --git a/newsletter.install b/newsletter.install index d8d48b1..3bf0164 100644 --- a/newsletter.install +++ b/newsletter.install @@ -275,6 +275,25 @@ function newsletter_schema() { 'not null' => FALSE, 'default' => 0, ), + 'use_view' => array( + 'description' => 'Use view to sent content', + 'type' => 'int', + 'length' => 1, + 'not null' => FALSE, + 'default' => 0, + ), + 'select_view' => array( + 'description' => 'Choose the view to get content', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ), + 'argument_view' => array( + 'description' => 'Put arguments in view', + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + ), ), 'unique keys' => array( 'template_subject' => array('subject'), @@ -721,3 +740,25 @@ function newsletter_update_7104(&$sandbox) { field_update_field($field); } } +function newsletter_update_7105(&$sandbox) { + db_add_field('newsletter_template', 'use_view', array( + 'description' => 'Use view to sent content', + 'type' => 'int', + 'length' => 1, + 'not null' => FALSE, + 'default' => 0, + )); + db_add_field('newsletter_template', 'select_view', array( + 'description' => 'Choose the view to get content', + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + )); + db_add_field('newsletter_template', 'argument_view', array( + 'description' => 'Put arguments in view', + 'type' => 'varchar', + 'length' => 255, + 'not null' => FALSE, + )); + return t('Update newsletter templates adding fields to choose and use a view to sent content.'); +} diff --git a/newsletter.module b/newsletter.module index 0b1447f..59e8e57 100644 --- a/newsletter.module +++ b/newsletter.module @@ -863,19 +863,20 @@ function newsletter_entity_info() { */ function newsletter_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) { if ($entity_type == 'newsletter_subscriber') { - $lang = $form['field_newsletter_list']['#language']; - - $list_ids = array_keys($form['field_newsletter_list'][$lang]['#options']); - $lists = newsletter_list_load($list_ids); - $exposed_lists = newsletter_exposed_lists(); - foreach ($lists as $list) { - if (in_array($list->nlid, $exposed_lists)) { - $form['exposed']['exposed_' . $list->nlid] = array( - '#type' => 'checkboxes', - '#options' => newsletter_get_template_terms($list->field_newsletter_template[$lang][0]['target_id']), - '#title' => t("@list's Configuration", array('@list' => $list->title)), - '#default_value' => newsletter_get_default_exposed($entity, $list), - ); + if(isset($form['field_newsletter_list'])){ + $lang = $form['field_newsletter_list']['#language']; + $list_ids = array_keys($form['field_newsletter_list'][$lang]['#options']); + $lists = newsletter_list_load($list_ids); + $exposed_lists = newsletter_exposed_lists(); + foreach ($lists as $list) { + if (in_array($list->nlid, $exposed_lists)) { + $form['exposed']['exposed_' . $list->nlid] = array( + '#type' => 'checkboxes', + '#options' => newsletter_get_template_terms($list->field_newsletter_template[$lang][0]['target_id']), + '#title' => t("@list's Configuration", array('@list' => $list->title)), + '#default_value' => newsletter_get_default_exposed($entity, $list), + ); + } } } }