]> gitweb.fluxo.info Git - drupal/reminder.git/commitdiff
Default value for subscription field, initial code to update subscribers and minor...
authorSilvio Rhatto <rhatto@riseup.net>
Mon, 15 Oct 2012 21:22:26 +0000 (18:22 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Mon, 15 Oct 2012 21:22:26 +0000 (18:22 -0300)
reminder.module

index b3705304ac4ba30eeb801904a89d8a7265f0f712..fdba07cfd2529ab6a9be89e540b7837c2c6bc32d 100644 (file)
@@ -126,9 +126,6 @@ function reminder_node_access($node, $op, $account) {
 
 /**
  * Implementation of hook_node_form()
- *
- * @todo
- *   Default value for subscription field.
  */
 function reminder_form(&$node) {
   global $user;
@@ -176,13 +173,18 @@ function reminder_form(&$node) {
       '#maxlength' => 100,
       '#default_value' => isset($reminder['anonym_email']) ? $reminder['anonym_email'] : '',
     );
+
+  }
+
+  if (isset($node->nid)) {
+    $reminder['subscriptions'] = reminder_get_subscriptions($node->nid);
   }
 
   $form['until'] = array(
      '#type'                => 'date_popup',
      '#title'               => 'Remind until',
-     '#default_value'       => date($format_until),
-     '#date_format'         => isset($reminder['until']) ? $reminder['until'] : $format_until,
+     '#default_value'       => isset($reminder['until']) ? $reminder['until'] : date($format_until),
+     '#date_format'         => $format_until,
      '#date_label_position' => 'within',
      '#date_increment'      => 60,
      '#date_year_range'     => '0:+20',
@@ -259,8 +261,11 @@ function reminder_validate($node, $form, &$form_state) {
  * @todo
  *   Date, remind and subscribe widgets.
  *   Update subscribers, removing old ones.
+ *   Optionally send unsubscription email for removed users.
  */
 function reminder_node_submit($node, $form, &$form_state) {
+  global $user;
+
   // No preview available.
   if (!$form_state['submitted']) {
     return;
@@ -278,7 +283,6 @@ function reminder_node_submit($node, $form, &$form_state) {
   $node->title = $form_state['values']['title'];
 
   if ($node->nid == NULL) {
-    global $user;
     $action = 'create';
 
     // Save basic data.
@@ -336,7 +340,34 @@ function reminder_node_submit($node, $form, &$form_state) {
       'anonym_email' => $anonym_email,
     ))->condition('nid', $node->nid)->execute();
 
-    // TODO: Update subscribers, removing old ones.
+    // Update subscribers, removing old ones.
+    $current_subscriptions      = array();
+    $current_subscriptions_data = reminder_get_subscriptions($node->nid);
+    $new_subscriptions          = reminder_parse_subscribers($node->reminder_subscriptions);
+
+    foreach ($current_subscriptions_data as $subscriber) {
+      // We copy just the relevant data just for later use.
+      $current_subscriptions[$subscriber['id']] = $subscriber['email'];
+
+      // Unsubscribe user from reminder.
+      if (!in_array($subscriber['email'], $new_subscriptions)) {
+        db_delete('reminder_subscriptions')
+          ->condition('id', $subscriber['id'])
+          ->execute();
+      }
+    }
+
+    foreach ($new_subscriptions as $subscriber) {
+      if (!in_array($subscriber, $current_subscriptions)) {
+        $unsubscribe_url = _reminder_generate_url('unsubscribe_url', 30, 'reminder_subscriptions');
+        $values = array(
+            'email'           => $subscriber,
+            'reminder_id'     => $node->nid,
+            'unsubscribe_url' => $unsubscribe_url,
+            );
+      }
+    }
+
   }
 
   $mail = ($user->uid > 0) ? $mail = $user->mail : $form_state['values']['anonym']['user_email'];