aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--reminder.install24
-rw-r--r--reminder.module147
2 files changed, 106 insertions, 65 deletions
diff --git a/reminder.install b/reminder.install
index 38de02f..a5bbbb0 100644
--- a/reminder.install
+++ b/reminder.install
@@ -41,7 +41,7 @@ function reminder_schema() {
'not null' => TRUE,
'default' => '0'
),
- 'interval' => array(
+ 'frequency' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
@@ -167,3 +167,25 @@ function reminder_update_7000(&$sandbox) {
$schema = reminder_schema();
db_create_table('reminder_notifications', $schema['reminder_notifications']);
}
+
+/**
+ * Change field 'interval' to 'frequency'.
+ */
+function reminder_update_7001(&$sandbox) {
+ // Make sure to not run this update twice.
+ if (db_field_exists('reminder', 'frequency')) {
+ return;
+ }
+
+ db_drop_field('reminder', 'interval');
+
+ db_add_field('reminder', 'frequency',
+ array(
+ 'description' => t('Reminder frequency.'),
+ 'type' => 'varchar',
+ 'length' => 255,
+ 'not null' => TRUE,
+ 'default' => '0'
+ )
+ );
+}
diff --git a/reminder.module b/reminder.module
index 024aa30..a1c7b68 100644
--- a/reminder.module
+++ b/reminder.module
@@ -186,7 +186,7 @@ function reminder_form(&$node) {
'#date_year_range' => '0:+3',
);
- $form['interval'] = array(
+ $form['frequency'] = array(
'#type' => 'select',
'#title' => t('Interval'),
'#options' => array(
@@ -197,8 +197,8 @@ function reminder_form(&$node) {
'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.'),
+ '#default_value' => isset($node->frequency) ? $node->frequency : 'hourly',
+ '#description' => t('Choose the frequency a reminder should be sent to you and all subscribers.'),
);
$form['reminder_options'] = array(
@@ -253,6 +253,7 @@ function reminder_validate($node, $form, &$form_state) {
* @todo
* Refactor.
* Date, remind and subscribe widgets.
+ * Save and update body/description.
*/
function reminder_node_submit($node, $form, &$form_state) {
// No preview available.
@@ -260,68 +261,78 @@ function reminder_node_submit($node, $form, &$form_state) {
return;
}
- // TODO
- dpm($form_state);
- return FALSE;
+ if (isset($form_state['values']['anonym'])) {
+ $anonym_name = $form_state['values']['anonym']['user_name'];
+ $anonym_email = $form_state['values']['anonym']['user_email'];
+ }
+ else {
+ $anonym_name = NULL;
+ $anonym_email = NULL;
+ }
+
+ $node->title = $form_state['values']['title'];
+ $node->body = $form_state['values']['body'];
- if ($form_state['values']['nid'] == NULL) {
+ if ($node->nid == NULL) {
global $user;
+
+ // Save basic data.
node_save($node);
- // Reminder_head
- // generate the reminder urls and save them
+ // 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
+ // Save reminder options.
$values = array(
- 'nid' => $node->nid,
- 'uid' => $user->uid,
- 'url' => $reminder_url,
- 'admin_url' => $admin_url,
- 'secure' => $node->reminder_options['secure'],
+ 'nid' => $node->nid,
+ 'uid' => $user->uid,
+ 'until' => $form_state['values']['until'],
+ 'frequency' => $form_state['values']['frequency'],
+ 'secure' => $form_state['values']['reminder_options']['secure'],
+ 'url' => $reminder_url,
+ 'admin_url' => $admin_url,
+ 'anonym_name' => $anonym_name,
+ 'anonym_email' => $anonym_email,
);
+
$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']));
- }
+
+ //if ($anonym_name != NULL) {
+ // $query->fields(array('anonym_name' => $anonym_name, 'anonym_email' => $anonym_email));
+ //}
+
$query->execute();
+ // Save subscribers.
foreach (reminder_parse_subscribers($node->reminder_subscriptions) as $subscriber) {
- $unsubscribe_url = reminder_generate_url('unsubscribe_url', 30);
+ $unsubscribe_url = _reminder_generate_url('unsubscribe_url', 30, 'reminder_subscriptions');
$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)
- );
+ $query = db_insert('reminder_subscriptions')->fields($values);
- // send an email message
- //if ($node->email_notification) {
+ // Send an email message.
+ //if ($form_state['values']['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'];
+ elseif (valid_email_address($form_state['values']['anonym']['user_email'])) {
+ $mail = $form_state['values']['anonym']['user_email'];
}
if ($mail != "") {
if ($user->uid > 0) {
$name = $user->name;
}
- elseif (isset($node->anonym['user_name'])) {
- $name = $node->anonym['user_name'];
+ elseif (isset($form_state['values']['anonym']['user_name'])) {
+ $name = $form_state['values']['anonym']['user_name'];
}
$params = array(
@@ -335,45 +346,52 @@ function reminder_node_submit($node, $form, &$form_state) {
}
}
else {
+ // Fetch existing options.
+ $query = db_select('reminder', 'r');
+ $query
+ ->condition('r.nid', $node->nid)
+ ->fields('r', array('uid', 'url', 'admin_url'));
+ $result = $query->execute();
+
+ foreach ($result as $record) {
+ $reminder_url = $record->url;
+ $admin_url = $record->admin_url;
+ }
- // 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'];
+ // Update basic data.
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'],
+ // Save reminder options
+ db_update('reminder')->fields(array(
+ 'until' => $form_state['values']['until'],
+ 'frequency' => $form_state['values']['frequency'],
'secure' => $form_state['values']['reminder_options']['secure'],
- ))->condition('nid', $node_id)->execute();
-
- // days and options
+ 'anonym_name' => $anonym_name,
+ 'anonym_email' => $anonym_email,
+ ))->condition('nid', $node->nid)->execute();
- // 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: Update subscribers, removing old ones.
}
- // TODO
- if ($form_state['reminder_options']['now'] != 0) {
+ // Send reminder now.
+ if (isset($form_state['reminder_options']['now']) && $form_state['reminder_options']['now'] != 0) {
reminder_send($node->nid);
}
+ 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
+ )
+ );
+
drupal_set_message(t("Saved."));
}
@@ -384,6 +402,7 @@ 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));
+ db_query("DELETE FROM {reminder_notifications} WHERE reminder_id = :reminder_id", array(':reminder_id' => $node->nid));
}
/**
@@ -420,9 +439,9 @@ function reminder_parse_subscribers($subscribers) {
* Lengh of the keychain
* @return string
*/
-function _reminder_generate_url($field, $length) {
+function _reminder_generate_url($field, $length, $table = 'reminder') {
$url = _reminder_keygen($length);
- $query = db_select('reminder', 'm')->fields('m');
+ $query = db_select($table, 'm')->fields('m');
while ($query->condition($field, $url)->execute()->rowCount() > 0) {
$url = _reminder_keygen($length);
}