diff options
author | Silvio Rhatto <rhatto@riseup.net> | 2012-03-23 11:28:11 -0300 |
---|---|---|
committer | Silvio Rhatto <rhatto@riseup.net> | 2012-03-23 11:28:11 -0300 |
commit | ed4695652dbc6e60e0b5d67b9eff433b707a8cca (patch) | |
tree | b132b0ac63d3c4986f68b642c90e4e3998f44bcc /reminder.module | |
download | reminder-ed4695652dbc6e60e0b5d67b9eff433b707a8cca.tar.gz reminder-ed4695652dbc6e60e0b5d67b9eff433b707a8cca.tar.bz2 |
Initial import
Diffstat (limited to 'reminder.module')
-rw-r--r-- | reminder.module | 437 |
1 files changed, 437 insertions, 0 deletions
diff --git a/reminder.module b/reminder.module new file mode 100644 index 0000000..0ac0e64 --- /dev/null +++ b/reminder.module @@ -0,0 +1,437 @@ +<?php + +/** + * @file + * + * Reminder module. + */ + +/** + * Implements hook_menu() + */ +function reminder_menu() { + $items['reminder/%'] = array( + 'title' => '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['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' => 'node_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() + * + * @todo + * Date, remind and subscribe widgets. + */ +function node_reminder_form(&$node) { + global $user; + + $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) || $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, + ); + } + 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', + '#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['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('Yes'), '1' => t('No')), + '#default_value' => isset($node->secure) ? $node->secure : 0, + ); + + return $form; +} + +/** + * Implementation of hook_node_insert() + * + * @todo + * Date, remind and subscribe widgets. + */ +function node_reminder_insert($node) { + global $user; + + // 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(); + + // 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); + } + } +} + +/** + * Implementation of hook_node_delete() + */ +function node_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); +} + +/** + * Update a reminder. + * + * @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; +} + +/** + * reminder_update_reminder_submit() + * + * @return void + * + * @todo + * Date, remind and subscribe widgets. + */ +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.")); +} + +/** + * 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')); +} |