'Taxonomy Publisher', 'description' => 'Configure which vocabularies will use Taxonomy Publisher.', 'page callback' => 'drupal_get_form', 'page arguments' => array('taxonomy_tools_publisher_admin_form'), 'access arguments' => array('administer taxonomy publisher configuration'), 'file' => 'taxonomy_tools_publisher.admin.inc', 'file path' => drupal_get_path('module', 'taxonomy_tools_publisher'), ); return $items; } /** * Implements hook_form_FORM_ID_alter(). */ function taxonomy_tools_publisher_form_taxonomy_form_term_alter(&$form, &$form_state) { $vid = $form['#vocabulary']->vid; $config = variable_get('taxonomy_tools_publisher_config', array()); if (!empty($config) && in_array($vid, $config)) { // Only for terms that are from vocabularies that use Taxonomy Publisher. if (user_access('administer term schedule')) { // Add js files that adds custom controls for jQuery datepicker. $form['#attached']['js'][] = array( 'data' => 'http://datejs.googlecode.com/files/date.js', 'type' => 'external', ); // Datepicker localization. global $language; if ($language->language != 'en') { $form['#attached']['js'][] = drupal_get_path('module', 'jquery_update') . '/replace/ui/ui/i18n/jquery.ui.datepicker-' . $language->language . '.js'; } $form['#attached']['js'][] = drupal_get_path('module', 'taxonomy_tools_publisher') . '/js/taxonomy_tools_publisher.js'; // Add additional validation function. $form['#validate'][] = 'taxonomy_tools_publisher_taxonomy_term_form_validate'; // Add buttons for clearing date values. if (isset($form['field_taxonomy_term_publish_on'])) { $form['field_taxonomy_term_publish_on']['#suffix'] = ''; } if (isset($form['field_taxonomy_term_unpublish_on'])) { $form['field_taxonomy_term_unpublish_on']['#suffix'] = ''; } // Checkbox default value when date fields are available but no values // exist in database; happens when editing terms from vocabulary for // which Taxonomy Publisher was enabled when terms already exist in it. if (empty($form_state['build_info']['args'][0]->field_taxonomy_term_status)) { $form['field_taxonomy_term_status'][LANGUAGE_NONE]['#default_value'] = 1; } } else { // User is not allowed to use taxonomy scheduler. $form['field_taxonomy_term_status']['#access'] = FALSE; $form['field_taxonomy_term_publish_on']['#access'] = FALSE; $form['field_taxonomy_term_unpublish_on']['#access'] = FALSE; } } } /** * Validates the user input of Taxonomy Publisher fields. * * @see taxonomy_tools_publisher_form_taxonomy_form_term_alter() */ function taxonomy_tools_publisher_taxonomy_term_form_validate($form, &$form_state) { if (!empty($form_state['values']['field_taxonomy_term_publish_on'][LANGUAGE_NONE][0]['value'])) { $publish_on = $form_state['values']['field_taxonomy_term_publish_on'][LANGUAGE_NONE][0]['value']; if ($publish_on < REQUEST_TIME) { // Publish on value must be in future. form_set_error('field_taxonomy_term_publish_on', t('The "Publish on:" date must be in the future.')); } } if (!empty($form_state['values']['field_taxonomy_term_unpublish_on'][LANGUAGE_NONE][0]['value'])) { $unpublish_on = $form_state['values']['field_taxonomy_term_unpublish_on'][LANGUAGE_NONE][0]['value']; if ($unpublish_on < REQUEST_TIME) { // Unpublish on value must be in future. form_set_error('field_taxonomy_term_unpublish_on', t('The "Unpublish on:" date must be in the future.')); } } if (isset($publish_on) && isset($unpublish_on) && $publish_on > $unpublish_on) { // Unpublish on value must be after publish on value. form_set_error('field_taxonomy_term_unpublish_on', t('The "Unpublish on:" date must be later than the "Publish on;" date.')); } } /** * Implements hook_taxonomy_term_presave(). */ function taxonomy_tools_publisher_taxonomy_term_presave($term) { if (in_array($term->vid, variable_get('taxonomy_tools_publisher_config', array()))) { // Clear unpublish value if term status is set to unpublished and // publish value is not set. if (isset($term->field_taxonomy_term_status) && $term->field_taxonomy_term_status[LANGUAGE_NONE][0]['value'] == 0 && isset($term->field_taxonomy_term_publish_on) && empty($term->field_taxonomy_term_publish_on[LANGUAGE_NONE]) && isset($term->field_taxonomy_term_unpublish_on) && !empty($term->field_taxonomy_term_unpublish_on[LANGUAGE_NONE])) { $term->field_taxonomy_term_unpublish_on[LANGUAGE_NONE] = array(); } // Set term status to unpublished if publish and unpublish values are set. if (isset($term->field_taxonomy_term_status) && $term->field_taxonomy_term_status[LANGUAGE_NONE][0]['value'] == 1 && isset($term->field_taxonomy_term_publish_on) && !empty($term->field_taxonomy_term_publish_on[LANGUAGE_NONE]) && isset($term->field_taxonomy_term_unpublish_on) && !empty($term->field_taxonomy_term_unpublish_on[LANGUAGE_NONE])) { $term->field_taxonomy_term_status[LANGUAGE_NONE][0]['value'] = 0; } // Show term status message. if (isset($term->field_taxonomy_term_status)) { $message = ''; if ($term->field_taxonomy_term_status[LANGUAGE_NONE][0]['value'] == 1) { $message .= t('The term @name is published.', array('@name' => $term->name)); if (isset($term->field_taxonomy_term_unpublish_on) && !empty($term->field_taxonomy_term_unpublish_on[LANGUAGE_NONE])) { $message .= ' '; $message .= t('It will be unpublished on @unpublish_on', array( '@unpublish_on' => format_date($term->field_taxonomy_term_unpublish_on[LANGUAGE_NONE][0]['value'], 'custom', TAXONOMY_PUBLISHER_MESSAGE_DATE_FORMAT)) ); } } else { $message .= t('The term @name is unpublished.', array('@name' => $term->name)); if (isset($term->field_taxonomy_term_publish_on) && !empty($term->field_taxonomy_term_publish_on[LANGUAGE_NONE])) { $message .= ' '; $message .= t('It will be published on @publish_on', array( '@publish_on' => format_date($term->field_taxonomy_term_publish_on[LANGUAGE_NONE][0]['value'], 'custom', TAXONOMY_PUBLISHER_MESSAGE_DATE_FORMAT)) ); } } drupal_set_message(check_plain($message)); } } } /** * Implements hook_cron(). */ function taxonomy_tools_publisher_cron() { // Publish terms. taxonomy_tools_publisher_publish(); // Unpublish terms. taxonomy_tools_publisher_unpublish(); // Clean navigation caches. if (module_exists('taxonomy_display')) { taxonomy_display_clean_cache(); } } /** * Publishes scheduled taxonomy terms. */ function taxonomy_tools_publisher_publish() { // Collect term ID's of terms which publish on value is before now. $config = variable_get('taxonomy_tools_publisher_config', array()); $query = db_select('field_data_field_taxonomy_term_publish_on', 'foo'); $query->addField('foo', 'entity_id', 'tid'); $query->addField('foo', 'field_taxonomy_term_publish_on_value', 'publish_on'); $query->leftJoin('taxonomy_term_data', 'bar', 'foo.entity_id = bar.tid'); $query->condition('bar.vid', $config, 'IN'); $result = $query->execute()->fetchAll(); $tids = array(); foreach ($result as $data) { if ($data->publish_on < REQUEST_TIME) { $tids[] = $data->tid; } } foreach ($tids as $tid) { // Change term status to "Published" and remove publish on value. $term = taxonomy_term_load($tid); $term->field_taxonomy_term_status[LANGUAGE_NONE][0]['value'] = 1; $term->field_taxonomy_term_publish_on[LANGUAGE_NONE] = array(); taxonomy_term_save($term); } } /** * Unpublishes scheduled taxonomy terms. */ function taxonomy_tools_publisher_unpublish() { // Collect term ID's of terms which publish on value is before now. $config = variable_get('taxonomy_tools_publisher_config', array()); $query = db_select('field_data_field_taxonomy_term_unpublish_on', 'foo'); $query->addField('foo', 'entity_id', 'tid'); $query->addField('foo', 'field_taxonomy_term_unpublish_on_value', 'unpublish_on'); $query->leftJoin('taxonomy_term_data', 'bar', 'foo.entity_id = bar.tid'); $query->condition('bar.vid', $config, 'IN'); $result = $query->execute()->fetchAll(); $tids = array(); foreach ($result as $data) { if ($data->unpublish_on < REQUEST_TIME) { $tids[] = $data->tid; } } foreach ($tids as $tid) { // Change term status to "Unpublished" and remove unpublish on value. $term = taxonomy_term_load($tid); $term->field_taxonomy_term_status[LANGUAGE_NONE][0]['value'] = 0; $term->field_taxonomy_term_unpublish_on[LANGUAGE_NONE] = array(); taxonomy_term_save($term); } } /** * Implements hook_permission(). */ function taxonomy_tools_publisher_permission() { return array( 'administer term schedule' => array( 'title' => t('Administer term schedule'), 'description' => t('User is able set scheduler times and change term status.'), ), 'bypass taxonomy publisher' => array( 'title' => t('Bypass Taxonomy Publisher settings'), 'description' => t('User is able to bypass restrictions set by Taxonomy Publisher.'), ), 'administer taxonomy publisher configuration' => array( 'title' => t('Administer Taxonomy Publisher configuration'), 'description' => t('User is able to access and use Taxonomy Publisher configuration page.'), ), ); } /** * Checks the taxonomy term and it's ancestors for availability. * * @param string $tid * A string containing taxonomy term ID. * * @return bool * TRUE if taxonomy term is published and none of it's parents is unpublished, * FALSE either if the taxonomy term itself or at least one of it's * parents is unpublished. */ function taxonomy_tools_publisher_check_branch($tid) { // Get term status value from DB. $query = db_select('field_data_field_taxonomy_term_status', 'foo'); $query->addField('foo', 'field_taxonomy_term_status_value', 'term_status'); $query->condition(db_and()->condition('foo.entity_id', $tid)->condition('entity_type', 'taxonomy_term')); $status_result = $query->execute()->fetchField(); // Term status is set to "Unpublished". if ($status_result !== FALSE && !$status_result) { $status = 0; } // Every other situation term is considered as "Published". else { $query = db_select('taxonomy_term_hierarchy', 'foo'); $query->addField('foo', 'parent'); $query->condition('foo.tid', $tid); $parent_result = $query->execute()->fetchField(); // If term has a parent it must also be checked for availability. if ($parent_result != 0) { $status = taxonomy_tools_publisher_check_branch($parent_result); } else { $status = 1; } } return $status; } /** * Implements hook_term_copy_alter(). */ function taxonomy_tools_publisher_term_copy_alter(&$term) { // Only for terms that are from a vocabulary that uses Taxonomy Publisher. if (in_array($term->vid, variable_get('taxonomy_tools_publisher_config', array()))) { // Set the status to "unpublished". $term->field_taxonomy_term_status[LANGUAGE_NONE][0]['value'] = 0; // No scheduling. $term->field_taxonomy_term_publish_on[LANGUAGE_NONE] = array(); $term->field_taxonomy_term_unpublish_on[LANGUAGE_NONE] = array(); } else { // Remove Taxonomy Publisher fields if destination vocabulary does // not use them. if (isset($term->field_taxonomy_term_status)) { unset($term->field_taxonomy_term_status); } if (isset($term->field_taxonomy_term_publish_on)) { unset($term->field_taxonomy_term_publish_on); } if (isset($term->field_taxonomy_term_unpublish_on)) { unset($term->field_taxonomy_term_unpublish_on); } } }