You are here

function draggableviews_view_draggabletable_form_submit in DraggableViews 7

Same name and namespace in other branches
  1. 6.3 draggableviews.module \draggableviews_view_draggabletable_form_submit()
  2. 6 draggableviews.inc \draggableviews_view_draggabletable_form_submit()
  3. 6.2 draggableviews.inc \draggableviews_view_draggabletable_form_submit()

Implements hook_submit().

1 string reference to 'draggableviews_view_draggabletable_form_submit'
draggableviews_view_draggabletable_form in ./draggableviews.module
Build the form

File

./draggableviews.module, line 432
Draggableviews module provides a style plugin for views. With this plugin rows become draggable and can be organized in complex structures.

Code

function draggableviews_view_draggabletable_form_submit($form, &$form_state) {

  // Check permissions.
  if (!user_access('Allow Reordering')) {
    drupal_set_message(t('You are not allowed to reorder nodes.'), 'error');
    return;
  }

  // Gather all needed information.
  $view = $form_state['build_info']['args'][0]->view;
  $results = $view->result;
  $input = $form_state['input'];
  $info = $view->draggableviews_info;
  if (!isset($info['order'])) {
    return;
  }

  // Check if structure is locked.
  if (!empty($info['locked'])) {
    drupal_set_message(t('The structure is locked.'), 'error');
    return;
  }

  // Loop through all resulting nodes.
  foreach ($results as $row) {

    // Set order values.
    if (isset($info['order']['field'])) {

      // The input array must have the same structure as the node array.
      // E.g. because of _draggableviews_get_hierarchy_depth(..).
      $info['input'][$row->{$view->base_field}]['order'][0] = $input[$info['order']['field']['field_name'] . '_' . $row->{$view->base_field}];
    }

    // Set parent values.
    if (isset($info['hierarchy'])) {
      $info['input'][$row->{$view->base_field}]['parent'] = $input[$info['hierarchy']['field']['field_name'] . '_' . $row->{$view->base_field}];
    }
  }
  _draggableviews_build_hierarchy($info);
  _draggableviews_save_hierarchy($info);
  if (isset($info['hierarchy'])) {

    // Save expanded/collapsed states.
    global $user;
    $uid = $info['expand_links']['by_uid'] ? $user->uid : 0;
    foreach ($form_state['input'] as $key => $val) {
      if (preg_match('/draggableviews_collapsed_/', $key)) {
        $parent_nid = drupal_substr($key, 25);

        // TODO Please review the conversion of this statement to the D7 database API syntax.

        /* db_query("DELETE FROM {draggableviews_collapsed}
           WHERE uid=%d AND parent_nid=%d AND view_name='%s'", $uid, $parent_nid, $view->name) */
        db_delete('draggableviews_collapsed')
          ->condition('uid', $uid)
          ->condition('parent_nid', $parent_nid)
          ->condition('view_name', $view->name)
          ->execute();

        // TODO Please review the conversion of this statement to the D7 database API syntax.

        /* db_query("INSERT INTO {draggableviews_collapsed}
           (uid, view_name, parent_nid, collapsed) VALUES (%d, '%s', %d, %d)", $uid, $view->name, $parent_nid, $val) */
        $id = db_insert('draggableviews_collapsed')
          ->fields(array(
          'uid' => $uid,
          'view_name' => $view->name,
          'parent_nid' => $parent_nid,
          'collapsed' => $val,
        ))
          ->execute();
      }
    }
  }

  // Trigger the event "A view has been sorted"
  if (module_exists('rules')) {
    rules_invoke_event('draggableviews_rules_event_sorted', $view->name);
  }

  // Redirect form.
  $get = $_GET;
  unset($get['q']);
  $form_state['redirect'] = array(
    $_GET['q'],
    array(
      'query' => $get,
    ),
  );
}