You are here

class draggableviews_plugin_style_draggabletable in DraggableViews 6

Same name and namespace in other branches
  1. 6.3 views/draggableviews_plugin_style_draggabletable.inc \draggableviews_plugin_style_draggabletable
  2. 6.2 draggableviews_plugin_style_draggabletable.inc \draggableviews_plugin_style_draggabletable
  3. 7 views/draggableviews_plugin_style_draggabletable.inc \draggableviews_plugin_style_draggabletable

Style plugin to render each item as a row in a draggable table; Inherits all from views_plugin_table.

Hierarchy

Expanded class hierarchy of draggableviews_plugin_style_draggabletable

1 string reference to 'draggableviews_plugin_style_draggabletable'
draggableviews_views_plugins in ./draggableviews.views.inc
@file Derives the view style plugin

File

./draggableviews_plugin_style_draggabletable.inc, line 7

View source
class draggableviews_plugin_style_draggabletable extends views_plugin_style_table {

  /**
   * Render the given style.
   */
  function options_form(&$form, &$form_state) {

    // inherit options from style_table
    parent::options_form($form, $form_state);

    // set theme handler
    // theme function is registered in *.module file
    $form['#theme'] = 'draggableviews_ui_style_plugin_draggabletable';

    // get field handler
    $handlers = $this->display->handler
      ->get_handlers('field');

    /**************************
        * DRAGGABLE VIEW OPTIONS *
        **************************
          + Set tabledrag options (multiple).
          + Set depth and weight-fields of each depth.
          + Apply tabledrag-type to content-types (Root (can't have parent), Leaf (can't have children)).
       */

    // get display object
    $displayObj = $this->view->display['default'];

    // define the saved data in the view object as the current data
    $current = $displayObj->display_options['style_options'];

    // Create an array of allowed columns from the data we know:
    // get all system-wide node types as a keyed array
    foreach (node_get_types('types') as $node_type) {

      /* get value for each */
      $node_types[$node_type->type] = $node_type->type;

      /* build new array */
    }
    $form['tabledrag_options'] = array();

    // set tabledrag options
    $form['tabledrag_depth_fields'] = array();

    // set depth fields (hierarchy weights) where weights will be saved
    $form['tabledrag_types'] = array();

    // set tabledrag types (root, leaf) for specific node-types
    // define some variables
    $leaveIndex = -1;

    // index of tabledrag data that should not be left out
    // (..to delete a specific tabledrag data row)
    $leaveIndex_type = -1;

    // index of tabledrag types that should not be left out
    // (..to delete a specific tabledrag type)
    $extra_rows = 0;

    // new tabledrag-data-rows to add at end of array
    $extra_rows_type = 0;

    // new tabledrag-type-rows to add at end of array

    //check if realtimeedit module exists
    if (module_exists('realtimeedit')) {

      // set tabledrag types (root, leaf) for specific node-types
      $form['realtimeedit_enabled_fields'] = array();
      $leaveIndex_realtimeedit = -1;

      // index of tabledrag types that should not be left out
      $extra_rows_realtimeedit = 0;

      // new tabledrag-type-rows to add at end of array
    }

    // check for input
    if (count($form_state['input']) > 0) {

      // Input is available (form has been submitted) ->
      // React on clicked buttons:
      // Button: add action
      if (isset($form_state['input']['tabledrag_action_add_button'])) {
        $extra_rows = 1;

        // 1 extra row should be built
      }

      // Buttons: check all tabledrag delete buttons
      for ($i = 0; $i < count($form_state['input']['style_options']['tabledrag_options']); $i++) {

        // Button: delete action
        if (isset($form_state['input']['tabledrag_action_del_button_' . $i])) {
          $leaveIndex = $i;

          // set tabledrag index to delete
        }
      }

      // Button: add tabledrag type
      if (isset($form_state['input']['tabledrag_type_add_button'])) {
        $extra_rows_type = 1;
      }

      // Buttons: check all delete buttons
      for ($i = 0; $i < count($form_state['input']['style_options']['tabledrag_types']); $i++) {

        // Button: delete action
        if (isset($form_state['input']['tabledrag_type_del_button_' . $i])) {
          $leaveIndex_type = $i;

          // set tabledrag type index to delete
        }
      }

      //check if realtimeedit module exists
      if (module_exists('realtimeedit')) {

        // Button: add realtimeedit enabled field
        if (isset($form_state['input']['realtimeedit_add_button'])) {
          $extra_rows_realtimeedit = 1;
        }

        // Buttons: check all delete buttons
        for ($i = 0; $i < count($form_state['input']['style_options']['realtimeedit_enabled_fields']); $i++) {

          // Button: delete action
          if (isset($form_state['input']['realtimeedit_del_button_' . $i])) {
            $leaveIndex_realtimeedit = $i;

            // set tabledrag type index to delete
          }
        }
      }

      // define the input data as the current data
      $current = $form_state['input']['style_options'];
    }

    /* Some markup to introduce DRAGGABLE VIEW form elements
     *******************************************************
     */
    $form['head_draggabletable_markup'] = array(
      '#prefix' => '<div class="">',
      '#suffix' => '</div>',
      '#value' => t('Draggable Table Settings:'),
    );
    $form['description_draggabletable_markup'] = array(
      '#prefix' => '<div class="description form-item">',
      '#suffix' => '</div>',
      '#value' => t('Draggable Table description.'),
    );
    $form['head_draggabletable_markup'] = array(
      '#prefix' => '<div class="">',
      '#suffix' => '</div>',
      '#value' => t('Add new tabledrag:'),
    );
    $form['description_draggabletable_content_type'] = array(
      '#prefix' => '<div class="description form-item">',
      '#suffix' => '</div>',
      '#value' => t('Type "root" cannot be subordinated.<br>Type "leaf" cannot have child nodes.<br>Content types with type "root" assigned will own an link to collapse/expand the subordinated nodes (if checked below).'),
    );

    /* Build tabledrag actions form elements
     *******************************************************
     */

    // loop: go through all defined tabldrag options
    for ($i = 0, $button_index = 0; $i < count($current['tabledrag_options']) + $extra_rows; $i++) {

      // increase index
      // if tabldrag option should be left out, continue loop
      // (delete tabledrag option from array)
      if ($i == $leaveIndex) {
        continue;
      }

      // if current table drag option is an extra row,
      // don't use existing data to fill form-elements (because they would not exist yet)
      $overwrite = $i < count($current['tabledrag_options']) ? true : false;
      if ($overwrite == true) {

        //get options of current table drag
        $tmpOptions = $current['tabledrag_options'][$i];
      }

      //build form elements
      $form['tabledrag_options'][] = array(
        '#weight' => 10,
        'tabledrag_action' => array(
          '#type' => 'select',
          '#options' => array(
            'match' => 'match',
            'order' => 'order',
          ),
          '#value' => $overwrite ? $tmpOptions['tabledrag_action'] : 'match',
        ),
        'tabledrag_relationship' => array(
          '#type' => 'select',
          '#options' => array(
            'parent' => 'parent',
            'sibling' => 'sibling',
          ),
          '#value' => $overwrite ? $tmpOptions['tabledrag_relationship'] : 'parent',
        ),
        /* we don't use the options group and subgroup in this version.
           Maybe we need them in the future.*/

        /*
        'tabledrag_group' => array(
          '#type' => 'select',
          '#options' => $available_fields,
          '#value' => $overwrite ? $tmpOptions['tabledrag_group'] : $available_fields[0],
        ),
        'tabledrag_subgroup' => array(
          '#type' => 'select',
          '#options' => $available_fields,
          '#value' => $overwrite ? $tmpOptions['tabledrag_subgroup'] : $available_fields[0],
        ),
        */
        'tabledrag_source' => array(
          '#type' => 'select',
          '#options' => _draggableviews_filter_fields(array(
            'number',
            'nodereference',
          ), $handlers),
          '#value' => $overwrite ? $tmpOptions['tabledrag_source'] : $available_fields[0],
        ),
        'tabledrag_action_del_button' => array(
          '#type' => 'button',
          '#name' => 'tabledrag_action_del_button_' . $button_index,
          '#value' => t('Delete'),
        ),
      );

      // increase button index
      $button_index++;
    }

    //build button to add a new tabldrag
    $form['tabledrag_action_add_button'] = array(
      '#type' => 'button',
      '#name' => 'tabledrag_action_add_button',
      '#value' => t('Add Tabledrag'),
    );

    /* Build tabledrag_depth fields
        ***************************************
          These fields will be used to store the weight of each hierarchy.
          This section will not be shown if tabledrag{action:oder,relationship:sibling} is not used
       */

    // get tabledrag depth
    $tabledrag_depth = isset($current['tabledrag_depth_fields']) ? $current['tabledrag_depth'] : 1;

    // loop: add a select for each depth
    for ($i = 0; $i < $tabledrag_depth; $i++) {

      // set current depth field
      $tmpDepthField = $current['tabledrag_depth_fields']['field_' . $i];

      //SELECT: choose field where weight of depth should be saved
      $form['tabledrag_depth_fields']['field_' . $i] = array(
        '#type' => 'select',
        '#options' => _draggableviews_filter_fields(array(
          'number',
        ), $handlers),
        '#value' => $tmpDepthField ? $tmpDepthField : $available_fields[0],
      );
    }

    // Button: set depth
    $form['tabledrag_set_depth_button'] = array(
      '#type' => 'button',
      '#name' => 'button_set_depth',
      '#value' => t('Set depth'),
    );

    // SELECT: choose depth
    $form['tabledrag_depth'] = array(
      '#type' => 'select',
      '#options' => array(
        1 => 1,
        2 => 2,
        3 => 3,
        4 => 4,
      ),
      //DUMMY OPTIONS
      '#default_value' => $tabledrag_depth,
    );

    //TABLEDRAG TYPES (ROOT,LEAF)

    /* Build tabledrag type fields
        ***************************************
          These fields will save the behaviour of a node-type (root, leaf)
       */
    for ($i = 0, $button_index = 0; $i < count($current['tabledrag_types']) + $extra_rows_type; $i++) {

      // increase index
      // if tabldrag option should be left out, continue loop
      // (delete tabledrag option from array)
      if ($i == $leaveIndex_type) {
        continue;
      }

      // if current table drag type is an extra row,
      // don't use existing data to fill form-elements (because they are not existing yet)
      $overwrite = $i < count($current['tabledrag_types']) ? true : false;

      //get options of current table drag
      $tmpOtions = $current['tabledrag_types'][$i];
      $form['tabledrag_types'][] = array(
        '#weight' => 10,
        'field_type' => array(
          '#type' => 'select',
          '#options' => $node_types,
          '#value' => $overwrite ? $tmpOtions['field_type'] : $node_types[0],
        ),
        'type' => array(
          '#type' => 'select',
          '#options' => array(
            'root' => 'root',
            'leaf' => 'leaf',
          ),
          '#value' => $overwrite ? $tmpOtions['type'] : 'root',
        ),
        'tabledrag_type_del_button' => array(
          '#type' => 'button',
          '#name' => 'tabledrag_type_del_button_' . $button_index,
          '#value' => t('Delete'),
        ),
      );

      // increase button index
      $button_index++;
    }
    $form['tabledrag_type_add_button'] = array(
      '#type' => 'button',
      '#name' => 'tabledrag_type_add_button',
      '#value' => t('Add type'),
    );

    //TABLEDRAG EXPAND/COLLAPSE OPTION

    /* Build checkbox
        ***************************************
          decide if epand links should be shown
       */
    $form['tabledrag_expand'] = array(
      '#type' => 'checkboxes',
      '#name' => 'tabledrag_expand',
      '#options' => array(
        'expand_links' => 'Show expand Links?',
        'collapsed' => 'default is collapsed',
      ),
      '#title' => t('Decide whether expand/collapse Links should be shown or not'),
      '#default_value' => isset($current['tabledrag_expand']) ? $current['tabledrag_expand'] : array(
        'expand_links',
      ),
    );

    //TABLEDRAG SHOW/HIDE INPUT FIELDS

    /* Build checkbox
        ***************************************
          decide whether input fields should be visible or not
       */
    $form['tabledrag_visible'] = array(
      '#type' => 'checkboxes',
      '#name' => 'tabledrag_visible',
      '#options' => array(
        'visible' => 'Show input fields?',
      ),
      '#title' => t('Decide whether input fields (e.g. weight, parent) should be visible or not'),
      '#default_value' => isset($current['tabledrag_visible']) ? $current['tabledrag_visible'] : array(),
    );

    /*****************************************
     * IMPLEMENTATION OF REALTIMEEDIT MODULE *
     ******************************************/

    //check if realtimeedit module exists
    if (module_exists('realtimeedit')) {

      // set tabledrag types (root, leaf) for specific node-types
      $form['realtimeedit_enabled_fields'] = array();
      $leaveIndex_type = -1;

      // index of tabledrag types that should not be left out
      // (..to delete a specific tabledrag type)
      $extra_rows_type = 0;

      // new tabledrag-type-rows to add at end of array
      // REALTIMEEDIT ENABLED FIELDS

      /* Build realtimeedit enabled fields
          ***************************************
            These fields will be realtime-editable
         */
      $max_count = count($current['realtimeedit_enabled_fields']) + $extra_rows_realtimeedit;
      $button_index = 0;

      // initalize count variable and button index
      for ($i = 0; $i < $max_count; $i++) {

        // if realtimeedit enabled field should be left out, continue loop
        // (delete enabled field from array)
        if ($i == $leaveIndex_realtimeedit) {
          continue;
        }

        // if current enabled field is an extra row,
        // don't use existing data to fill form-elements (because they are not existing yet)
        $overwrite = $i < count($current['realtimeedit_enabled_fields']) ? true : false;

        //get options of current enabled field
        $tmpOtions = $current['realtimeedit_enabled_fields'][$i];
        $form['realtimeedit_enabled_fields'][] = array(
          '#weight' => 10,
          'field' => array(
            '#type' => 'select',
            '#options' => $available_fields,
            '#value' => $overwrite ? $tmpOtions['field'] : $available_fields[0],
          ),
          'realtimeedit_del_button' => array(
            '#type' => 'button',
            '#name' => 'realtimeedit_del_button_' . $button_index,
            '#value' => t('Delete'),
          ),
        );

        // increase button index
        $button_index++;
      }
      $form['realtimeedit_add_button'] = array(
        '#type' => 'button',
        '#name' => 'realtimeedit_add_button',
        '#value' => t('Enable new field'),
      );
    }
  }

  /**
   * Render the table style.
   */
  function render() {

    // call form handler to wrap around a form.
    // -> this makes it possible to submit order changes.
    return drupal_get_form('draggableviews_view_draggabletable_form', &$this);

    /*
        $sets = $this->render_grouping($this->view->result, $this->options['grouping']);

        $output = '';
        foreach ($sets as $title => $records) {
          $output .= theme($this->theme_functions(), $this->view, $this->options, $records, $title);
        }
        return $output;*/
  }

}

Members