You are here

function views_gantt_update_node in Views Gantt 7.2

Same name and namespace in other branches
  1. 7 views_gantt.module \views_gantt_update_node()

Updates node if there are changes in it's fields.

Parameters

object $node: Node object

array $data: Array of option names (from style plugin options) => values to update

array $style_options: Array of all style plugin option names

1 call to views_gantt_update_node()
views_gantt_update in ./views_gantt.module
Callback for task/project update when we change it in Gantt chart.

File

./views_gantt.module, line 216
Module file for Views Gantt

Code

function views_gantt_update_node($node, $data, $style_options) {
  $changed = FALSE;
  $lang = LANGUAGE_NONE;

  // For now we support only simple fields that
  // storing their values in array with 'value' key,
  // and datetime fields (form Date module).
  // Maybe we don't need to add another field types,
  // because we update only dates and duration.
  foreach ($data as $option_name => $option_value) {
    if (isset($style_options[$option_name])) {
      $field_name = $style_options[$option_name];
      if (isset($node->{$field_name})) {
        if (is_array($node->{$field_name})) {

          // Get field info.
          $field_info = field_info_field($field_name);

          // If field is translatable, we check if it has
          // an index equals to node language.
          $is_translatable = field_is_translatable('node', $field_info);
          if ($is_translatable && isset($node->{$field_name}[$node->language])) {
            $lang = $node->language;
          }
          $index = key($field_info['columns']);
          if ($option_name == 'end_date_field' && isset($field_info['columns']['value2'])) {
            $index = 'value2';
          }

          // If field type = datetime, modify new value before storing.
          if (isset($field_info['columns'][$index]) && $field_info['columns'][$index]['type'] == 'datetime') {
            views_gantt_normalize_date_field($option_value, 'Y-m-d H:i:s');
          }
          if ($option_value) {
            $node->{$field_name}[$lang][0][$index] = $option_value;
          }
          else {
            $node->{$field_name} = array();
          }
        }
        else {
          $node->{$field_name} = $option_value;
        }
        $changed = TRUE;
      }
    }
  }

  // If we have some changes, we need to save node.
  // Also if node saved, we remove cached gantt data from session.
  if ($changed) {
    node_save($node);
    unset($_SESSION['views_gantt']);
  }
}