diff --git a/hosting_expiry_date.info b/hosting_expiry_date.info index b158582..123e77e 100644 --- a/hosting_expiry_date.info +++ b/hosting_expiry_date.info @@ -1,4 +1,4 @@ -core = "6.x" +core = "7.x" description = "Allows for sites to be disabled after a set time." name = "Expiry Date" diff --git a/hosting_expiry_date.install b/hosting_expiry_date.install index fcd3a27..5ca7a5f 100644 --- a/hosting_expiry_date.install +++ b/hosting_expiry_date.install @@ -6,20 +6,6 @@ */ /** - * Implementation of hook_install(). - */ -function hosting_expiry_date_install() { - drupal_install_schema('hosting_expiry_date'); -} - -/** - * Implementation of hook_uninstall(). - */ -function hosting_expiry_date_uninstall() { - drupal_uninstall_schema('hosting_expiry_date'); -} - -/** * Implementation of hook_schema(). */ function hosting_expiry_date_schema() { diff --git a/hosting_expiry_date.module b/hosting_expiry_date.module index 59c067e..eea2398 100644 --- a/hosting_expiry_date.module +++ b/hosting_expiry_date.module @@ -1,12 +1,15 @@ array( + 'title' => t('set site expiry dates'), + 'description' => t('TODO Add a description for \'set site expiry dates\''), + ), + ); } /** @@ -14,7 +17,7 @@ function hosting_expiry_date_perm() { */ function hosting_expiry_date_menu() { $items = array(); - $items['node/%hosting_expiry_date_node/expiry'] = array( + $items['node/%hosting_expiry_date_site/expiry'] = array( 'title' => t('Expiry dates'), 'description' => t('Site expiry date(s)'), 'page callback' => 'drupal_get_form', @@ -29,31 +32,102 @@ function hosting_expiry_date_menu() { /** * Wildcard loader function to limit the expiry dates tab to just site nodes */ -function hosting_expiry_date_node_load($arg) { +function hosting_expiry_date_site_load($arg) { if (!is_numeric($arg)) { return FALSE; } if ($node = node_load($arg)) { - if (in_array($node->type, array('site'))) { - return $arg; + if ($node->type == 'site') { + return $node; } } return FALSE; } /** + * Implements hook_node_load(). + */ +function hosting_expiry_date_node_load($nodes, $types) { + if (in_array('site', $types)) { + foreach($nodes as $nid => $node) { + $nids[] = $nid; + } + $dates = hosting_expiry_date_get_dates($nids); + foreach ($dates as $key => $info) { + $nodes[$info->site_nid]->expiry_date[$info->action]['date'] = $info->expiry_date; + } + return $nodes; + } +} + +/** + * Implements hook_cron(). + */ +function hosting_expiry_date_cron(){ + // Get all site node IDs that have expire dates. + $dataset = db_query('SELECT site_nid FROM {hosting_expiry_date_dates}')->fetchCol(); + $queue = DrupalQueue::get("hosting_expiry_date_queue"); + foreach ($dataset as $data) { + $queue->createItem($data); + } +} + +/** + * Implements hook_cron_queue_info(). + */ +function hosting_expiry_date_cron_queue_info() { + $queues['hosting_expiry_date_queue'] = array( + 'worker callback' => 'hosting_expiry_date_queue_worker', + ); + + return $queues; +} + +/** + * Worker function for hosting_expiry_date_queue. + */ +function hosting_expiry_date_queue_worker($site_nid){ + $node = node_load($site_nid); + + $time = time(); + $dates = $node->expiry; + if (isset($dates['disable']['date'])) { + $disable_date = strtotime($dates['disable']['date']); + if ($disable_date <= $time) { + hosting_add_task($node->nid, 'disable'); + } + } + + if (isset($dates['delete']['date'])) { + $delete_date = strtotime($dates['delete']['date']); + if ($disable_date <= $time) { + hosting_add_task($node->nid, 'delete'); + } + } +} + +/** + * Helper function to load dates for an array of nids. + */ +function hosting_expiry_date_get_dates($nids){ + $dates = db_query('SELECT * FROM {hosting_expiry_date_dates} WHERE site_nid IN (:nids)', array(':nids' => $nids))->fetchAll(); + return $dates; +} + +/** * Form where we collect expiry dates for sites * - * @param $nid - * Node ID of the site + * @param $node + * Node object of the site */ -function hosting_expiry_date_site_form($form_state, $nid) { - drupal_set_title(t('Expiry Dates for') . ' ' . node_load($nid)->title); - $form = array(); +function hosting_expiry_date_site_form($form, &$form_state, $node) { + drupal_set_title(t('Expiry Dates for') . ' ' . $node->title); + $nid = $node->nid; + $form_state['node'] = $node; $form['intro'] = array( '#value' => t('Set expiry dates for this site. The site sill be disabled or deleted on these dates.'), ); - + $form['Disable'] = array( '#type' => 'fieldset', '#title' => t('Disable site'), @@ -62,13 +136,13 @@ function hosting_expiry_date_site_form($form_state, $nid) { $form['Disable']['disable_active'] = array( '#type' => 'checkbox', '#title' => t('Automatically disable this site?'), - '#default_value' => (bool) hosting_expiry_date_lookup($nid, 'disable'), + '#default_value' => isset($node->expiry['disable']['date']) ? 1 : 0, ); $form['Disable']['disable_date'] = array( '#type' => 'date', '#title' => t('Disable date'), - '#default_value' => hosting_expiry_date_default($nid, 'disable', '30'), + '#default_value' => hosting_expiry_date_default($node, 'disable', '30'), ); $form['Delete'] = array( @@ -79,13 +153,13 @@ function hosting_expiry_date_site_form($form_state, $nid) { $form['Delete']['delete_active'] = array( '#type' => 'checkbox', '#title' => t('Automatically delete this site?'), - '#default_value' => (bool) hosting_expiry_date_lookup($nid, 'delete'), + '#default_value' => isset($node->expiry['delete']['date']) ? 1 : 0, ); $form['Delete']['delete_date'] = array( '#type' => 'date', '#title' => t('Delete date'), - '#default_value' => hosting_expiry_date_default($nid, 'delete', '30'), + '#default_value' => hosting_expiry_date_default($node, 'delete', '30'), ); $form['submit'] = array( @@ -117,23 +191,39 @@ function hosting_expiry_date_site_form_validate($form, &$form_state) { * Implementation of hook_form_submit(). */ function hosting_expiry_date_site_form_submit($form, &$form_state) { - $nid = $form['#parameters'][2]; - - db_query('DELETE FROM {hosting_expiry_date_dates} WHERE site_nid = %d', $nid); - + $node = $form_state['node']; + + db_delete('hosting_expiry_date_dates') + ->condition('nid', $$node->nid) + ->execute(); + if ($form_state['values']['disable_active'] == '1') { $disable_date = date_create_from_format('Y-m-d', $form_state['values']['disable_date']['year'] . '-' . $form_state['values']['disable_date']['month'] . '-' . $form_state['values']['disable_date']['day']); - db_query('INSERT INTO {hosting_expiry_date_dates} (site_nid, expiry_date, action) VALUES (%d, "%s", "disable")', $nid, $disable_date->format('Y-m-d H:i:s')); - } + db_insert('hosting_expiry_date_dates') // Table name no longer needs {} + ->fields(array( + 'site_nid' => $node->nid, + 'expiry_date' => $disable_date->format('Y-m-d H:i:s'), + 'action' => 'disable', + )) + ->execute(); + } if ($form_state['values']['delete_active'] == '1') { $delete_date = date_create_from_format('Y-m-d', $form_state['values']['delete_date']['year'] . '-' . $form_state['values']['delete_date']['month'] . '-' . $form_state['values']['delete_date']['day']); - db_query('INSERT INTO {hosting_expiry_date_dates} (site_nid, expiry_date, action) VALUES (%d, "%s", "delete")', $nid, $delete_date->format('Y-m-d H:i:s')); - } + db_insert('hosting_expiry_date_dates') // Table name no longer needs {} + ->fields(array( + 'site_nid' => $node->nid, + 'expiry_date' => $delete_date->format('Y-m-d H:i:s'), + 'action' => 'delete', + )) + ->execute(); + } } /** * Get the expiry date for a given node * + * todo: leave this as a helper function or remove as its not used? + * * @param $nid * Node ID of the site * @param $action @@ -142,9 +232,9 @@ function hosting_expiry_date_site_form_submit($form, &$form_state) { * @return * If set, the expiry date set for $action. Otherwise, FALSE. */ -function hosting_expiry_date_lookup($nid, $action) { - $date = db_result(db_query('SELECT expiry_date FROM {hosting_expiry_date_dates} WHERE site_nid = %d AND action = "%s"',$nid, $action)); - if (!empty($date)) { +function hosting_expiry_date_lookup($node, $action) { + $date = db_query('SELECT expiry_date FROM {hosting_expiry_date_dates} WHERE site_nid = :nid AND action = ":action"', array(':nid' => $node->nid, ':action' => $action))->fetchField(); + if ($date) { return $date; } else { @@ -155,19 +245,19 @@ function hosting_expiry_date_lookup($nid, $action) { /** * Format an expiry date for display in a form * - * @param $nid - * Node ID of the site + * @param $node + * Node object of the site * @param $action * 'disable' or 'delete' * @param $offset - * The number of day in the future to set the default date + * The number of days in the future to set the default date * * @return * If set, the expiry date set for $action, as an array. Otherwise, today's * date + $offset, as an array. */ -function hosting_expiry_date_default($nid, $action, $offset = '0') { - $date = date_create_from_format('Y-m-d H:i:s', hosting_expiry_date_lookup($nid, $action)); +function hosting_expiry_date_default($node, $action, $offset = '0') { + $date = date_create_from_format('Y-m-d H:i:s', $node->expiry[$action]['date']); if (empty($date)) { $today = new DateTime(); $date = date_modify($today, '+' . $offset . ' days');