From bad1954caba469166a15e534e82bf7739f932fe7 Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Mon, 23 Apr 2012 23:22:19 -0300 Subject: Start refactoring --- reminder.install | 38 +++++++ reminder.module | 313 ++++++++++++++++++++++++++--------------------------- reminder.pages.inc | 8 ++ 3 files changed, 201 insertions(+), 158 deletions(-) diff --git a/reminder.install b/reminder.install index d958832..38de02f 100644 --- a/reminder.install +++ b/reminder.install @@ -127,5 +127,43 @@ function reminder_schema() { 'primary key' => array('log_id'), ); + $schema['reminder_notifications'] = array( + 'description' => 'The table of sent notifications.', + 'fields' => array( + 'id' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE + ), + 'reminder_id' => array( + 'type' => 'int', + 'not null' => TRUE, + 'unsigned' => TRUE, + 'default' => 0 + ), + 'last' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '0' + ), + ), + + 'primary key' => array('id'), + ); + return $schema; } + +/** + * Adds notification table. + */ +function reminder_update_7000(&$sandbox) { + // Make sure to not run this update twice. + if (db_table_exists('reminder_notifications')) { + return; + } + + $schema = reminder_schema(); + db_create_table('reminder_notifications', $schema['reminder_notifications']); +} diff --git a/reminder.module b/reminder.module index 0f606b4..a32390e 100644 --- a/reminder.module +++ b/reminder.module @@ -40,6 +40,16 @@ function reminder_menu() { 'type' => MENU_CALLBACK, ); + $items['reminder/%/denounce'] = array( + 'title' => 'Log page', + 'description' => 'Show logs', + 'page callback' => 'reminder_denounce', + 'page arguments' => array(1), + 'access arguments' => array('show reminder'), + 'file' => 'reminder.pages.inc', + 'type' => MENU_CALLBACK, + ); + $items['user/%user/reminder'] = array( 'title' => 'My reminders', 'description' => 'List of my reminders', @@ -78,7 +88,7 @@ function reminder_node_info() { return array( 'reminder' => array( 'name' => t('Reminder'), - 'base' => 'node_reminder', + 'base' => 'reminder', 'description' => t("Create a reminder for a business lunch, a meeting or a movie night."), 'has_title' => TRUE, 'title_label' => t('Your reminder'), @@ -119,10 +129,11 @@ function reminder_node_access($node, $op, $account) { /** * Implementation of hook_node_form() */ -function node_reminder_form(&$node) { +function reminder_form(&$node) { global $user; - $type = node_type_get_type($node); + $type = node_type_get_type($node); + $format_until = 'Y-m-d H:i'; if ($type->has_title) { $form['title'] = array( @@ -164,12 +175,6 @@ function node_reminder_form(&$node) { '#maxlength' => 100, ); } - else { - // initializing data for dates and options (options are empty) - } - - $format_until = 'Y-m-d H:i'; - $format_interval = ''; $form['until'] = array( '#type' => 'date_popup', @@ -206,85 +211,175 @@ function node_reminder_form(&$node) { '#type' => 'radios', '#title' => t('Show subscribed emails'), '#description' => t("Deny subscribers to see each other."), - '#options' => array('0' => t('Yes'), '1' => t('No')), + '#options' => array('0' => t('No'), '1' => t('Yes')), '#default_value' => isset($node->secure) ? $node->secure : 0, ); + $form['reminder_options']['now'] = array( + '#type' => 'radios', + '#title' => t('Issue a reminder right now to all subscribers'), + '#description' => t("Deny subscribers to see each other."), + '#options' => array('0' => t('No'), '1' => t('Yes')), + '#default_value' => 0, + ); + $form['reminder_subscriptions'] = array( '#type' => 'textarea', '#title' => t('Subscribers'), '#description' => t('One valid email address per line.'), '#maxlength' => 100, + '#default_value' => isset($node->reminder_subscriptions) ? $node->reminder_subscriptions: NULL, ); return $form; } /** - * Implementation of hook_node_insert() + * Form validation. * * @todo - * Date, remind and subscribe widgets. + * Throttling settings to avoid SPAM: max posts per + * hour and max recipients per post. */ -function node_reminder_insert($node) { - global $user; +function reminder_validate($node, $form, &$form_state) { + if ($form['type']['#value'] == 'reminder') { + // TODO + } +} - // Reminder_head - // generate the reminder urls and save them - $reminder_url = _reminder_generate_url('url', 10); - $admin_url = _reminder_generate_url('admin_url', 25); - - // save the reminder options - $values = array( - 'nid' => $node->nid, - 'uid' => $user->uid, - 'url' => $reminder_url, - 'admin_url' => $admin_url, - 'secure' => $node->reminder_options['secure'], - ); - $query = db_insert('reminder')->fields($values); - if (isset($node->anonym)) { - $query->fields(array('anonym_name' => $node->anonym['user_name'], 'anonym_email' => $node->anonym['user_email'])); +/** + * Implementation of hook_node_submit() + * + * @todo + * Refactor. + * Date, remind and subscribe widgets. + */ +function reminder_node_submit($node, $form, &$form_state) { + // No preview available. + if (!$form_state['submitted']) { + return; } - $query->execute(); - - // setting the output texts: the url of the reminder page and the admin page - drupal_set_message(l(t("Reminder page URL: !url", array("!url" => url('reminder/' . $reminder_url, array("absolute" => TRUE)))), "reminder/" . $reminder_url)); - drupal_set_message(l(t("Admin page URL: !url", array("!url" => url('reminder/' . $admin_url, array("absolute" => TRUE)))), "reminder/" . $admin_url)); - - // send an email message - //if ($node->email_notification) { - if (TRUE) { - $mail = ""; - if ($user->uid > 0) { - $mail = $user->mail; + + // TODO + dpm($form_state); + return FALSE; + + if ($form_state['values']['nid'] == NULL) { + global $user; + node_save($node); + + // Reminder_head + // generate the reminder urls and save them + $reminder_url = _reminder_generate_url('url', 10); + $admin_url = _reminder_generate_url('admin_url', 25); + + // save the reminder options + $values = array( + 'nid' => $node->nid, + 'uid' => $user->uid, + 'url' => $reminder_url, + 'admin_url' => $admin_url, + 'secure' => $node->reminder_options['secure'], + ); + $query = db_insert('reminder')->fields($values); + if (isset($node->anonym)) { + $query->fields(array('anonym_name' => $node->anonym['user_name'], 'anonym_email' => $node->anonym['user_email'])); } - elseif (valid_email_address($node->anonym['user_email'])) { - $mail = $node->anonym['user_email']; + $query->execute(); + + foreach (reminder_parse_subscribers($node->reminder_subscriptions) as $subscriber) { + $unsubscribe_url = reminder_generate_url('unsubscribe_url', 30); + $values = array( + 'email' => $subscriber, + 'reminder_id' => $node->nid, + 'unsubscribe_url' => $unsubscribe_url, + ); } + $query = db_insert('reminder_subscriptions')->fields($values); + + // setting the output texts: the url of the reminder page and the admin page + drupal_set_message(l(t("Reminder page URL: !url", + array("!url" => url('reminder/' . $reminder_url, array("absolute" => TRUE)))), "reminder/" . $reminder_url) + ); + drupal_set_message(l(t("Admin page URL: !url", + array("!url" => url('reminder/' . $admin_url, array("absolute" => TRUE)))), "reminder/" . $admin_url) + ); - if ($mail != "") { + // send an email message + //if ($node->email_notification) { + if (TRUE) { + $mail = ""; if ($user->uid > 0) { - $name = $user->name; + $mail = $user->mail; } - elseif (isset($node->anonym['user_name'])) { - $name = $node->anonym['user_name']; + elseif (valid_email_address($node->anonym['user_email'])) { + $mail = $node->anonym['user_email']; + } + + if ($mail != "") { + if ($user->uid > 0) { + $name = $user->name; + } + elseif (isset($node->anonym['user_name'])) { + $name = $node->anonym['user_name']; + } + + $params = array( + "name" => $name, + "reminder_url" => $reminder_url, + "admin_url" => $admin_url + ); + drupal_mail('reminder', 'create_new_reminder', $mail, language_default(), $params); } - $params = array( - "name" => $name, - "reminder_url" => $reminder_url, - "admin_url" => $admin_url - ); - drupal_mail('reminder', 'create_new_reminder', $mail, language_default(), $params); } + else { + + // reminder head + $node_id = db_query("SELECT nid FROM {reminder_reminder_heads} WHERE admin_url = :admin_url", array(':admin_url' => $form_state['values']['reminder_admin_url']))->fetchField(); + $node = node_load($node_id); + + // save node options + $node->title = $form_state['values']['title']; + $node->body = $form_state['values']['body']; + node_save($node); + + // save reminder options + db_update('reminder_reminder_heads')->fields(array( + 'anonym_name' => $form_state['values']['anonym']['user_name'], + 'anonym_email' => $form_state['values']['anonym']['user_email'], + 'secure' => $form_state['values']['reminder_options']['secure'], + ))->condition('nid', $node_id)->execute(); + + // days and options + + // collect the ids of the days which are already in the db + // insert and update days and options datas + // if there is some unused id in $days_ids, that's mean we deleted them + + foreach (reminder_parse_subscribers($node->reminder_subscriptions) as $subscriber) { + $unsubscribe_url = reminder_generate_url('unsubscribe_url', 30); + $values = array( + 'email' => $subscriber, + 'reminder_id' => $node->nid, + 'unsubscribe_url' => $unsubscribe_url, + ); + } + $query = db_insert('reminder_subscriptions')->fields($values); + } + + // TODO + if ($form_state['reminder_options']['now'] != 0) { + reminder_send($node->nid); } + + drupal_set_message(t("Saved.")); } /** * Implementation of hook_node_delete() */ -function node_reminder_delete($node) { +function reminder_delete($node) { db_query("DELETE FROM {reminder} WHERE nid = :nid", array(':nid' => $node->nid)); db_query("DELETE FROM {reminder_logs} WHERE nid = :nid", array(':nid' => $node->nid)); db_query("DELETE FROM {reminder_subscriptions} WHERE reminder_id = :reminder_id", array(':reminder_id' => $node->nid)); @@ -301,116 +396,18 @@ function reminder_cron() { } /** - * Update a reminder. + * Send emails for all subscribers. * * @todo - * Date, remind and subscribe widgets. */ -function reminder_update_reminder($form, &$form_state, $node) { - $type = node_type_get_type($node); - - if ($type->has_title) { - $form['title'] = array( - '#type' => 'textfield', - '#title' => check_plain($type->title_label), - '#required' => TRUE, - '#default_value' => $node->title, - '#weight' => -5 - ); - } - - if ($type->has_body) { - $form['body'] = array( - '#type' => 'textarea', - '#title' => check_plain($type->body_label), - '#default_value' => isset($node->body) ? $node->body : '', - '#rows' => 5 - ); - } - - if (isset($node->nid) && $node->uid == 0) { - $form['anonym'] = array( - '#type' => 'fieldset', - '#title' => t('Add your name and email'), - '#tree' => TRUE, - ); - - $form['anonym']['user_name'] = array( - '#type' => 'textfield', - '#title' => t('Your name'), - '#maxlength' => 100, - '#default_value' => check_plain($node->anonym_name), - '#description' => t('This is optional.'), - ); - - $form['anonym']['user_email'] = array( - '#type' => 'textfield', - '#title' => t('Your e-mail'), - '#description' => t('So you can receive reminders'), - '#default_value' => check_plain($node->anonym_email), - '#maxlength' => 100, - ); - } - - $form['reminder_options'] = array( - '#type' => 'fieldset', - '#title' => t('Scheduler options'), - '#tree' => TRUE, - ); - - $form['reminder_options']['secure'] = array( - '#type' => 'radios', - '#title' => t('Show previous votes'), - '#description' => t("Allow new voters to see the previous votes."), - '#options' => array('0' => t('Yes'), '1' => t('No')), - '#default_value' => isset($node->secure) ? $node->secure : 0, - ); - - $form['reminder_admin_url'] = array( - '#type' => 'value', - '#value' => $node->reminder_admin_url, - ); - - $form['submit'] = array( - '#type' => 'submit', - '#value' => t('Submit'), - ); - - return $form; +function reminder_send($nid) { } /** - * reminder_update_reminder_submit() - * - * @return void - * - * @todo - * Date, remind and subscribe widgets. + * Helper function to parse subscribers. */ -function reminder_update_reminder_submit($form, &$form_state) { - // reminder head - $node_id = db_query("SELECT nid FROM {reminder_reminder_heads} WHERE admin_url = :admin_url", array(':admin_url' => $form_state['values']['reminder_admin_url']))->fetchField(); - $node = node_load($node_id); - - // save node options - $node->title = $form_state['values']['title']; - $node->body = $form_state['values']['body']; - node_save($node); - - // save reminder options - db_update('reminder_reminder_heads')->fields(array( - 'anonym_name' => $form_state['values']['anonym']['user_name'], - 'anonym_email' => $form_state['values']['anonym']['user_email'], - 'secure' => $form_state['values']['reminder_options']['secure'], - ))->condition('nid', $node_id)->execute(); - - // days and options - - // collect the ids of the days which are already in the db - // insert and update days and options datas - // if there is some unused id in $days_ids, that's mean we deleted them - - drupal_set_message(t("Saved.")); +function reminder_parse_subscribers($subscribers) { + return explode('\n', $subscribers); } /** diff --git a/reminder.pages.inc b/reminder.pages.inc index 647a1d3..755acc0 100644 --- a/reminder.pages.inc +++ b/reminder.pages.inc @@ -37,3 +37,11 @@ function reminder_logpage() { */ function reminder_mypage() { } + +/** + * Page callback. + * + * @todo + */ +function reminder_denounce() { +} -- cgit v1.2.3