diff --git a/scheduler.admin.inc b/scheduler.admin.inc index 1f9be3f..dc1b4cc 100644 --- a/scheduler.admin.inc +++ b/scheduler.admin.inc @@ -65,7 +65,16 @@ function scheduler_admin() { ), ), ); - + $form['scheduler_publisher_user'] = array( + '#type' => 'radios', + '#title' => t('Specific Publisher User'), + '#default_value' => variable_get('scheduler_publisher_user', 'cron_user'), + '#description' => t('By default publisher of the scheduled content is the Cron User. By selecting \'specific user\', an autocomplete textfield will be added to the node edit scheduling options.'), + '#options' => array( + 'cron_user' => t('Publish as cron job user.'), + 'specific_user' => t('Specific User.'), + ), + ); // Options for setting date-only with default time. $form['scheduler_date_only_fieldset'] = array( '#type' => 'fieldset', @@ -164,6 +173,9 @@ function scheduler_admin_validate($form, &$form_state) { * Form submission handler for scheduler_admin(). */ function scheduler_admin_submit($form, &$form_state) { + // Set variable for scheduler_publisher_user based on the selected. + variable_set('scheduler_publisher_user', $form_state['values']['scheduler_publisher_user']); + // For the minute increment, change a blank value to 1. Date popup does not // support blank values. if (empty($form_state['values']['scheduler_date_popup_minute_increment'])) { diff --git a/scheduler.cron.inc b/scheduler.cron.inc index 7b8543a..955708f 100644 --- a/scheduler.cron.inc +++ b/scheduler.cron.inc @@ -72,6 +72,16 @@ function _scheduler_publish() { $publish_on = $n->publish_on; $n->publish_on = NULL; + // Before we do any saving we need to change the current user to the publisher user (if set). + $specific_user_enabled = variable_get('scheduler_publisher_user', FALSE) == 'specific_user' ? 1 : 0; + if ($specific_user_enabled && isset($n->publisher_uid)) { + global $user; + $original_user = $user; + $old_state = drupal_save_session(); + drupal_save_session(FALSE); + $user = user_load($n->publisher_uid); + } + // Invoke scheduler API to allow modules to alter the node before it is // saved. // @todo For D8, remove this from here. @@ -91,6 +101,12 @@ function _scheduler_publish() { // Invoke scheduler API for modules to react after the node is published. _scheduler_scheduler_api($n, $action); + // Set the current user back to original (if changed). + if ($specific_user_enabled && $n->scheduler['publisher_uid']) { + $user = $original_user; + drupal_save_session($old_state); + } + $result = TRUE; } @@ -165,6 +181,16 @@ function _scheduler_unpublish() { $unpublish_on = $n->unpublish_on; $n->unpublish_on = NULL; + // Before we do any saving we need to change the current user to the publisher user (if set). + $specific_user_enabled = variable_get('scheduler_publisher_user', FALSE) == 'specific_user' ? 1 : 0; + if ($specific_user_enabled && isset($n->publisher_uid)) { + global $user; + $original_user = $user; + $old_state = drupal_save_session(); + drupal_save_session(FALSE); + $user = user_load($n->publisher_uid); + } + // Invoke scheduler API to allow modules to alter the node before it is // saved. // @todo For D8, remove this from here. @@ -184,6 +210,12 @@ function _scheduler_unpublish() { // Invoke scheduler API for modules to react after the node is unpublished. _scheduler_scheduler_api($n, 'unpublish'); + // Set the current user back to original (if changed). + if ($specific_user_enabled && $n->scheduler['publisher_uid']) { + $user = $original_user; + drupal_save_session($old_state); + } + $result = TRUE; } diff --git a/scheduler.edit.inc b/scheduler.edit.inc index 5772075..8562db5 100644 --- a/scheduler.edit.inc +++ b/scheduler.edit.inc @@ -16,6 +16,7 @@ function _scheduler_form_alter(&$form, $form_state) { $publishing_enabled = variable_get('scheduler_publish_enable_' . $form['type']['#value'], 0) == 1; $unpublishing_enabled = variable_get('scheduler_unpublish_enable_' . $form['type']['#value'], 0) == 1; + $specific_user_enabled = variable_get('scheduler_publisher_user', FALSE) == 'specific_user' ? 1 : 0; $date_format = variable_get('scheduler_date_format', SCHEDULER_DATE_FORMAT); $date_only_format = variable_get('scheduler_date_only_format', SCHEDULER_DATE_ONLY_FORMAT); $time_only_format = variable_get('scheduler_time_only_format', SCHEDULER_TIME_ONLY_FORMAT); @@ -34,9 +35,14 @@ function _scheduler_form_alter(&$form, $form_state) { elseif (isset($node->nid) && $node->nid > 0) { // Load the values from the database if we are viewing an existing node. $query = db_select('scheduler', 's'); - $query->fields('s', array('publish_on', 'unpublish_on')); + $query->fields('s', array('publish_on', 'unpublish_on', 'uid')); $query->condition('s.nid', $node->nid, '='); $defaults = $query->execute()->fetchObject(); + + // Have to doublecheck if there is a uid and it is not set to -1. + if(isset($defaults->uid) && $defaults->uid >= 0) { + $defaults->publisher_user = user_load($defaults->uid); + } } else { // Initialise standard values. @@ -47,6 +53,12 @@ function _scheduler_form_alter(&$form, $form_state) { $defaults->unpublish_on = isset($node->unpublish_on) ? $node->unpublish_on : NULL; } + // If publisher_user is not set, set to current user. + if (!isset($defaults->publisher_user->name)) { + $defaults = new StdClass; + global $user; + $defaults->publisher_user = $user; + } // If there is a text value then convert it to a Unix timestamp. if (isset($defaults->publish_on) && $defaults->publish_on && !is_numeric($defaults->publish_on)) { $defaults->publish_on = _scheduler_strtotime($defaults->publish_on); @@ -173,6 +185,16 @@ function _scheduler_form_alter(&$form, $form_state) { unset($form['scheduler_settings']['unpublish_on']['#maxlength']); } } + + if ($specific_user_enabled) { + $form['scheduler_settings']['publisher_user'] = array( + '#type' => 'textfield', + '#title' => t('Choose the username that will publish/unpublish this content'), + '#size' => 30, + '#autocomplete_path' => 'user/autocomplete', + '#default_value' => $defaults->publisher_user->name, + ); + } } /** diff --git a/scheduler.install b/scheduler.install index a9f4812..59c75af 100644 --- a/scheduler.install +++ b/scheduler.install @@ -33,6 +33,13 @@ function scheduler_schema() { 'not null' => TRUE, 'default' => 0, ), + 'uid' => array( + 'description' => 'The specified {users}.uid of the user who published/unpublished the content.', + 'type' => 'int', + 'unsigned' => FALSE, + 'not null' => TRUE, + 'default' => -1, + ), ), 'indexes' => array( 'scheduler_publish_on' => array('publish_on'), @@ -44,6 +51,16 @@ function scheduler_schema() { } /** + * Implements hook_install(). + * Set variable to scheduler_publisher_user. + */ +function scheduler_install() { + if (!variable_get('scheduler_publisher_user')) { + variable_set('scheduler_publisher_user', 'cron_user'); + } +} + +/** * Implements hook_uninstall(). */ function scheduler_uninstall() { @@ -60,6 +77,7 @@ function scheduler_uninstall() { 'scheduler_lightweight_access_key', 'scheduler_lightweight_log', 'scheduler_time_only_format', + 'scheduler_publisher_user', ); $types = node_type_get_types(); @@ -187,3 +205,26 @@ function scheduler_update_7103() { . ', ' . format_plural($rows_deleted, '1 row deleted', '@count rows deleted') . ' ' . t('in role_permission table'); } + +/** + * Add uid column to 'scheduler' table. Adds 'scheduler_publish_user' variable. + */ +function scheduler_update_7104() { + if (!db_field_exists('scheduler', 'uid')) { + // Default value is -1 because there is a user 0 (Anonymous). + $field = array( + 'type' => 'int', + 'unsigned' => FALSE, + 'not null' => TRUE, + 'default' => -1, + 'description' => 'The specified {users}.uid of the user who published/unpublished the content.', + ); + db_add_field('scheduler', 'uid', $field); + db_update('scheduler') + ->fields(array('uid' => -1)) + ->execute(); + } + if (!variable_get('scheduler_publisher_user')) { + variable_set('scheduler_publisher_user', 'cron_user'); + } +} diff --git a/scheduler.module b/scheduler.module index d0833be..6903895 100644 --- a/scheduler.module +++ b/scheduler.module @@ -446,6 +446,9 @@ function scheduler_node_load($nodes, $types) { $nid = $record->nid; $nodes[$nid]->publish_on = $record->publish_on; $nodes[$nid]->unpublish_on = $record->unpublish_on; + if($record->uid >= 0) { + $nodes[$nid]->publisher_uid = $record->uid; + } $row = array(); // @todo This seems unneeded and is confusing. It is not certain that this // node is either published or unpublished, probably it isn't or the @@ -597,10 +600,24 @@ function scheduler_node_presave($node) { function scheduler_node_insert($node) { // Only insert into database if we need to (un)publish this node at some date. if (!empty($node->publish_on) || !empty($node->unpublish_on)) { + $specific_user_enabled = variable_get('scheduler_publisher_user') == 'specific_user'; + if($specific_user_enabled) { + if (isset($node->publisher_user) && !is_numeric($node->publisher_user)) { + // insert is coming from node edit form with a textfield called 'publisher_user'. + $uid = user_load_by_name($node->publisher_user)->uid; + } + else { + $uid = $node->publisher_uid; + } + } + else { + $uid = -1; + } db_insert('scheduler')->fields(array( 'nid' => $node->nid, 'publish_on' => $node->publish_on, 'unpublish_on' => $node->unpublish_on, + 'uid' => $uid, ))->execute(); // Invoke the events to indicate that a new node has been scheduled. @@ -623,9 +640,27 @@ function scheduler_node_update($node) { // otherwise the user probably cleared out the (un)publish dates so we should // remove the record. if (!empty($node->publish_on) || !empty($node->unpublish_on)) { + $specific_user_enabled = variable_get('scheduler_publisher_user') == 'specific_user'; + if ($specific_user_enabled) { + if (isset($node->publisher_user) && !is_numeric($node->publisher_user)) { + // insert is coming from node edit form with a textfield called 'publisher_user'. + $uid = user_load_by_name($node->publisher_user)->uid; + } + elseif (isset($node->publisher_uid)) { + $uid = $node->publisher_uid; + } + else { + // Rare case, where, user enables the 'specific user' configuration setting but there is no user set for the scheduled node. + $uid = -1; + } + } + else { + $uid = -1; + } db_merge('scheduler')->key(array('nid' => $node->nid))->fields(array( 'publish_on' => $node->publish_on, 'unpublish_on' => $node->unpublish_on, + 'uid' => $uid, ))->execute(); // Invoke the events to indicate that an existing node has been scheduled.