'Reminder page', 'description' => 'Access a reminder page', 'page callback' => 'reminder_page', 'page arguments' => array(1), 'access arguments' => array('show reminder'), 'file' => 'reminder.pages.inc', 'type' => MENU_CALLBACK, ); $items['reminder/%/unsubscribe'] = array( 'title' => 'Unsubscribe page', 'description' => 'Unsubscribe from a reminder', 'page callback' => 'reminder_unsubscribe', 'page arguments' => array(1), 'access arguments' => array('show reminder'), 'file' => 'reminder.pages.inc', 'type' => MENU_CALLBACK, ); $items['reminder/%/log'] = array( 'title' => 'Log page', 'description' => 'Show logs', 'page callback' => 'reminder_logpage', 'page arguments' => array(1), 'access arguments' => array('show reminder'), 'file' => 'reminder.pages.inc', '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', 'page callback' => 'reminder_mypage', 'access callback' => '_reminder_access_userreminders', 'access arguments' => array(1), 'file' => 'reminder.pages.inc', 'type' => MENU_SUGGESTED_ITEM, ); return $items; } /** * Implements hook_perm() */ function reminder_permission() { return array( 'access all reminders' => array( 'title' => t("Access all reminders"), 'restrict access' => TRUE, ), 'access own reminders' => array( 'title' => t("Access own reminders"), ), 'show reminder' => array( 'title' => t("Access reminders"), ), ); } /** * Implements hook_node_info() */ function reminder_node_info() { return array( 'reminder' => array( 'name' => t('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'), 'has_body' => TRUE, 'body_label' => t('Add some description'), ) ); } /** * Implementation of hook_form_alter() */ function reminder_form_alter(&$form, $form_state, $form_id) { // Remove preview button from reminder node form if ($form_id == 'reminder_node_form') { unset($form['actions']['preview']); } } /** * Implementation of hook_node_access() */ function reminder_node_access($node, $op, $account) { if (is_object($node) && $node->type == 'reminder') { switch ($op) { // disabling the edit page: we don't give access to any user // reason: we're using custom urls to update the nodes, and only // the owner of the reminder has the custom admin url case 'update': return FALSE; case 'delete': return user_access('delete reminder', $account) && ($account->uid == $node->uid); } } } /** * Implementation of hook_node_form() */ function reminder_form(&$node) { global $user; $type = node_type_get_type($node); $format_until = 'Y-m-d H:i'; 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) || $user->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, '#description' => t('This is optional.'), ); $form['anonym']['user_email'] = array( '#type' => 'textfield', '#title' => t('Your e-mail'), '#description' => t(''), '#maxlength' => 100, ); } $form['until'] = array( '#type' => 'date_popup', '#title' => 'Remind until', '#default_value' => date($format_until), '#date_format' => isset($node->until) ? $node->until : $format_until, '#date_label_position' => 'within', '#date_increment' => 60, '#date_year_range' => '0:+3', ); $form['interval'] = array( '#type' => 'select', '#title' => t('Interval'), '#options' => array( 'hourly' => t('Hourly'), 'daily' => t('Daily'), 'weekly' => t('Weekly'), 'monthly' => t('Monthly'), 'yearly' => t('Yearly'), 'decreasing' => t('Increasing (you get more reminders over time)'), ), '#default_value' => isset($node->interval) ? $node->interval : 'hourly', '#description' => t('Choose the interval a reminder should be sent to you and all subscribers.'), ); $form['reminder_options'] = array( '#type' => 'fieldset', '#title' => t('Reminder options'), '#tree' => TRUE, ); $form['reminder_options']['secure'] = array( '#type' => 'radios', '#title' => t('Show subscribed emails'), '#description' => t("Deny subscribers to see each other."), '#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; } /** * Form validation. * * @todo * Throttling settings to avoid SPAM: max posts per * hour and max recipients per post. */ function reminder_validate($node, $form, &$form_state) { if ($form['type']['#value'] == 'reminder') { // TODO } } /** * 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; } // 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'])); } $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) ); // send an email message //if ($node->email_notification) { if (TRUE) { $mail = ""; if ($user->uid > 0) { $mail = $user->mail; } 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); } } } 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 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)); } /** * Implements hook_cron() * * @todo * Send periodic emails. */ function reminder_cron() { variable_set('reminder_cron_last_run', REQUEST_TIME); } /** * Send emails for all subscribers. * * @todo */ function reminder_send($nid) { } /** * Helper function to parse subscribers. */ function reminder_parse_subscribers($subscribers) { return explode('\n', $subscribers); } /** * Helper function to generate a unique keychain * * @param $field * The field to be unique * @param $length * Lengh of the keychain * @return string */ function _reminder_generate_url($field, $length) { $url = _reminder_keygen($length); $query = db_select('reminder', 'm')->fields('m'); while ($query->condition($field, $url)->execute()->rowCount() > 0) { $url = _reminder_keygen($length); } return $url; } /** * Helper function to generate a keychain * * @param $length * Lengh of the keychain * @return string */ function _reminder_keygen($length) { $pattern = "1234567890abcdefghijklmnopqrstuvwxyz"; $key = $pattern{rand(0, 35)}; for ($i = 1; $i < $length; $i++) { $key .= $pattern{rand(0, 35)}; } return $key; } /** * Menu access callback. */ function _reminder_access_userreminders($account) { global $user; return user_access('access all reminders') || ($user->uid == $account->uid && user_access('access own reminders')); }