[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/cck/includes/ -> content.rules.inc (source)

   1  <?php
   2  // $Id: content.rules.inc,v 1.1.2.7 2009/12/23 17:33:25 fago Exp $
   3  
   4  /**
   5   * @file
   6   * Provides basic rules module support.
   7   */
   8  
   9  /**
  10   * Implementation of hook_rules_action_info().
  11   */
  12  function content_rules_action_info() {
  13    $info = array();
  14    $info['content_rules_action_populate_field'] = array(
  15      'label' => t('Populate a field'),
  16      'arguments' => array(
  17        'node' => array(
  18          'type' => 'node',
  19          'label' => t('Content'),
  20        ),
  21      ),
  22      'eval input' => array('code'),
  23      'help' => t('You should make sure that the used field exists in the given content type.'),
  24      'module' => 'CCK',
  25    );
  26    return $info;
  27  }
  28  
  29  /**
  30   * Action: populate a field.
  31   */
  32  function content_rules_action_populate_field($node, $settings, $element, &$state) {
  33    // Get information about the field.
  34    $field = content_fields($settings['field_name'], $node->type);
  35    $value = _content_rules_get_field_value($settings, $state);
  36  
  37    if (!empty($field) && is_array($value)) {
  38      $node->$settings['field_name'] = $value;
  39      return array('node' => $node);
  40    }
  41  }
  42  
  43  
  44  /**
  45   * Action "populate a field" configuration form.
  46   * This is a multistep form!
  47   */
  48  function content_rules_action_populate_field_form($settings, &$form, &$form_state) {
  49    $settings += array('field_name' => '', 'code' => '', 'value' => NULL);
  50    if (empty($settings['field_name'])) {
  51      $form['settings']['field_name'] = array(
  52        '#type' => 'select',
  53        '#title' => t('Field'),
  54        '#options' => content_rules_get_field_names_by_type(),
  55        '#default_value' => $settings['field_name'],
  56        '#description' => t('Select the machine-name of the field.'),
  57        '#required' => TRUE,
  58      );
  59      // Hide some form elements in the first step.
  60      $form['negate']['#access'] = FALSE;
  61      $form['input_help']['#access'] = FALSE;
  62      $form['weight']['#access'] = FALSE;
  63  
  64      // Replace the usual submit handlers with a own handler.
  65      $form['submit']['#submit'] = array('content_rules_action_populate_field_form_step_submit');
  66      $form['submit']['#value'] = t('Continue');
  67    }
  68    else {
  69      // Show the fields form here.
  70      module_load_include('inc', 'content', 'includes/content.node_form');
  71      $field = content_fields($settings['field_name']);
  72  
  73      $form['#node'] = (object)array('type' => '', $settings['field_name'] => $settings['value']);
  74      $form['#field_info'][$field['field_name']] = $field;
  75      // We can't put it into $form['settings'] as this would break AHAH callbacks
  76      $form += (array) content_field_form($form, $form_state, $field);
  77      $form[ $settings['field_name'] ]['#weight'] = 4;
  78  
  79      unset($form['#cache']);
  80  
  81        // Advanced: PHP code.
  82      $form['advanced_options'] = array(
  83        '#type' => 'fieldset',
  84        '#title' => t('Advanced: Specify the fields value with PHP code'),
  85        '#collapsible' => TRUE,
  86        '#collapsed' => empty($settings['code']),
  87        '#weight' => 5,
  88      );
  89  
  90      $db_info = content_database_info($field);
  91      $columns = array_keys($db_info['columns']);
  92      foreach ($columns as $key => $column) {
  93        $columns[$key] = t("'@column' => value for @column", array('@column' => $column));
  94      }
  95      $sample = t("return array(\n  0 => array(@columns),\n  // You'll usually want to stop here. Provide more values\n  // if you want your 'default value' to be multi-valued:\n  1 => array(@columns),\n  2 => ...\n);", array('@columns' => implode(', ', $columns)));
  96  
  97      $form['advanced_options']['code'] = array(
  98        '#type' => 'textarea',
  99        '#title' => t('Code'),
 100        '#default_value' => $settings['code'],
 101        '#rows' => 6,
 102        '#description' => t('Advanced usage only: PHP code that returns the value to set. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the value returned by this code will override any value specified above. Expected format: <pre>!sample</pre>Using <a href="@link_devel">devel.module\'s</a> \'devel load\' tab on a content page might help you figure out the expected format.', array(
 103          '!sample' => $sample,
 104          '@link_devel' => 'http://www.drupal.org/project/devel',
 105        )),
 106      );
 107  
 108      // Add this file to be included when the form is built by rules
 109      // as it's needed by CCKs add more button.
 110      // See rules_after_build_include_files().
 111      $form['#includes'][] = './'. drupal_get_path('module', 'node') .'/node.pages.inc';
 112    }
 113  }
 114  
 115  function content_rules_action_populate_field_form_step_submit($form, &$form_state) {
 116    $form_state['element']['#settings']['field_name'] = $form_state['values']['settings']['field_name'];
 117  }
 118  
 119  /**
 120   * Validate the chosen value or php code.
 121   */
 122  function content_rules_action_populate_field_validate($form, &$form_state) {
 123    if (!isset($form_state['element']['#settings']['field_name'])) {
 124      //Just validate the last step.
 125      return;
 126    }
 127  
 128    if (isset($form_state['values']['code']) && ($php = $form_state['values']['code'])) {
 129      if (strpos($php, 'return') === FALSE) {
 130        form_set_error('code', t('You have to return the default value in the expected format.'));
 131      }
 132    }
 133    else {
 134      // Validate the field.
 135      $settings = $form_state['element']['#settings'];
 136      $field = content_fields($settings['field_name']);
 137      $field_types = _content_field_types();
 138      $function = $field_types[$field['type']]['module'] .'_field';
 139      if (function_exists($function)) {
 140        $form['#node'] = (object)array('type' => '', $settings['field_name'] => $form_state['values'][$settings['field_name']]);
 141        $items = isset($form['#node']->$field['field_name']) ? $form['#node']->$field['field_name'] : array();
 142  
 143        //Make sure AHAH 'add more' button isn't sent to the fields
 144        // for processing.
 145        unset($items[$field['field_name'] .'_add_more']);
 146  
 147        $function('validate', $form['#node'], $field, $items, $form, NULL);
 148        content_field('validate', $form['#node'], $field, $items, $form, NULL);
 149      }
 150    }
 151  }
 152  
 153  function content_rules_action_populate_field_submit(&$settings, $form, &$form_state) {
 154    // Take over field values and filter out private properties added by CCK
 155    $settings['value'] = array_filter($form_state['values'][$settings['field_name']], 'is_array');
 156  
 157    foreach ($settings['value'] as $key => $data) {
 158      foreach (array_filter(array_keys($data)) as $col) {
 159        if ($col[0] == '_') {
 160          unset($settings['value'][$key][$col]);
 161        }
 162      }
 163      if ($key && count(array_filter($settings['value'][$key])) == 0) {
 164        // For multi-valued fields don't check for any additional empty values.
 165        unset($settings['value'][$key]);
 166      }
 167    }
 168  
 169    $settings['code'] = $form_state['values']['code'];
 170  
 171    if (function_exists('rules_action_custom_php_submit')) {
 172      // Support adding variables to the php code, if php module is present.
 173      rules_action_custom_php_submit($settings, $form, $form_state);
 174    }
 175  
 176    // Add all values to the input evaluator, so that textfields / textares can
 177    // make use of it.
 178    $names = array('code');
 179  
 180    foreach ($settings['value'] as $key => $data) {
 181      foreach (array_filter($data, 'is_string') as $col => $value) {
 182        $names[] = "value|$key|$col";
 183      }
 184    }
 185    $form_state['element']['#info']['eval input'] = $names;
 186  }
 187  
 188  
 189  /**
 190   * Label callback: Improve the label of the action.
 191   */
 192  function content_rules_action_populate_field_label($settings, $argument_labels) {
 193    return t("Populate @node's field '@field'", array('@field' => $settings['field_name']) + $argument_labels);
 194  }
 195  
 196  function workflow_ng_action_populate_field_upgrade(&$element) {
 197    $element['#name'] = 'content_rules_action_populate_field';
 198    $element['#settings']['code'] = $element['#settings']['default_value_php'];
 199    $element['#settings'][$element['#settings']['field_name']] = array();
 200    unset($element['#settings']['default_value_php']);
 201  }
 202  
 203  
 204  /**
 205   * Implementation of hook_rules_condition_info().
 206   */
 207  function content_rules_condition_info() {
 208    $info = array();
 209    $info['content_rules_field_has_value'] = array(
 210      'label' => t('Field has value'),
 211      'arguments' => array(
 212        'node' => array('type' => 'node', 'label' => t('Content')),
 213      ),
 214      'eval input' => array('code'),
 215      'help' => t('You should make sure that the used field exists in the given content type. The condition returns TRUE, if the selected field has the given value.'),
 216      'module' => 'CCK',
 217    );
 218    $info['content_rules_field_changed'] = array(
 219      'label' => t('Field has changed'),
 220      'arguments' => array(
 221        'node' => array('type' => 'node', 'label' => t('Content containing changes')),
 222        'node_unchanged' => array('type' => 'node', 'label' => t('Content not containing changes')),
 223      ),
 224      'help' => t('You should make sure that the used field exists in the given content type.'),
 225      'module' => 'CCK',
 226    );
 227    return $info;
 228  }
 229  
 230  /**
 231   * Condition: Check the value of a field.
 232   */
 233  function content_rules_field_has_value($node, $settings) {
 234    // Get information about the field.
 235    $field = content_fields($settings['field_name'], $node->type);
 236    $value = _content_rules_get_field_value($settings, $state);
 237  
 238    if (empty($field) || !is_array($value)) {
 239      return FALSE;
 240    }
 241  
 242    return _content_rules_field_has_value($node->$settings['field_name'], $value);
 243  }
 244  
 245  /**
 246   * Use the same configuration form as the "populate field" action.
 247   */
 248  function content_rules_field_has_value_form($settings, &$form, &$form_state) {
 249    content_rules_action_populate_field_form($settings, $form, $form_state);
 250  }
 251  function content_rules_field_has_value_validate($form, &$form_state) {
 252    content_rules_action_populate_field_validate($form, $form_state);
 253  }
 254  function content_rules_field_has_value_submit(&$settings, $form, &$form_state) {
 255    content_rules_action_populate_field_submit($settings, $form, $form_state);
 256  }
 257  
 258  function content_rules_field_has_value_label($settings, $argument_labels) {
 259    return t("@node's field '@field' has value", array('@field' => $settings['field_name']) + $argument_labels);
 260  }
 261  
 262  /**
 263   * Condition: Check if the field has changed.
 264   */
 265  function content_rules_field_changed($node1, $node2, $settings) {
 266    // Get information about the field.
 267    $field = content_fields($settings['field_name'], $node1->type);
 268  
 269    return !empty($field) && !_content_rules_field_has_value($node1->$settings['field_name'], $node2->$settings['field_name']);
 270  }
 271  
 272  function content_rules_field_changed_form($settings, &$form, &$form_state) {
 273    $settings += array('field_name' => '');
 274    $form['settings']['field_name'] = array(
 275      '#type' => 'select',
 276      '#title' => t('Field'),
 277      '#options' => content_rules_get_field_names_by_type(),
 278      '#default_value' => $settings['field_name'],
 279      '#description' => t('Select the machine-name of the field to look at.'),
 280      '#required' => TRUE,
 281    );
 282  }
 283  
 284  function content_rules_field_changed_label($settings, $argument_labels) {
 285    return t("@node's field '@field' has been changed", array('@field' => $settings['field_name']) + $argument_labels);
 286  }
 287  
 288  
 289  /**
 290   * Returns the fields of a given field type only.
 291   * Suitable for using it with #options.
 292   */
 293  function content_rules_get_field_names_by_type($type = NULL) {
 294    $fields = array();
 295    foreach (content_fields() as $field) {
 296      if (!isset($type) || $field['type'] == $type) {
 297        $fields[$field['field_name']] = $field['field_name'];
 298      }
 299    }
 300    asort($fields);
 301    return $fields;
 302  }
 303  
 304  function _content_rules_get_field_value($settings, &$state) {
 305    if ($settings['code']) {
 306      if (function_exists('rules_input_evaluator_php_apply')) {
 307        // Support adding variables to the php code, if php module is present.
 308        $value = rules_input_evaluator_php_apply($settings['code'], $settings['vars'], $state, FALSE);
 309      }
 310      else {
 311        ob_start();
 312        $value = eval($settings['code']);
 313        ob_end_clean();
 314      }
 315    }
 316    else {
 317      $value = $settings['value'];
 318    }
 319    return $value;
 320  }
 321  
 322  /**
 323   * Checks whether both field values match in a robust way.
 324   *
 325   * It returns TRUE, only if the number of multiple values matches and
 326   * each property of the cck field's value is the same in the node.
 327   *
 328   * @param $node_value The value present in the node.
 329   * @param $value The value to check for.
 330   */
 331  function _content_rules_field_has_value($node_value, $value) {
 332    if (count($value) != count($node_value)) {
 333      return FALSE;
 334    }
 335    // Loop over multiple fields
 336    foreach ($value as $delta => $sub_value) {
 337      // Check if all properties of the value are there in the node value too
 338      if (is_array($sub_value) && is_array($node_value[$delta])) {
 339        if (count(array_diff_assoc($sub_value, $node_value[$delta])) != 0) {
 340          return FALSE;
 341        }
 342      }
 343      elseif ($sub_value !== $node_value[$delta]) {
 344        return FALSE;
 345      }
 346    }
 347    return TRUE;
 348  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7