You are here

function _webform_render_matrix in Webform Matrix Component 7.2

Same name and namespace in other branches
  1. 6 components/matrix.inc \_webform_render_matrix()
  2. 7.4 components/matrix.inc \_webform_render_matrix()
  3. 7 components/matrix.inc \_webform_render_matrix()
  4. 7.3 components/matrix.inc \_webform_render_matrix()

Implements _webform_render_component().

File

components/matrix.inc, line 249
Webform module matrix component.

Code

function _webform_render_matrix($component, $value = NULL, $filter = TRUE) {
  module_load_include('inc', 'webform', '/components/date');
  $nid = $component['nid'];
  $cid = $component['cid'];
  $formkey = $component['form_key'];
  $pid = $component['pid'];
  $element = array();
  $element['#weight'] = $component['weight'];
  $element['#webform_component'] = $component;
  $element['#attached']['css'][] = drupal_get_path('module', 'webform_matrix_component') . '/webform_matrix_component.css';
  if ($component['extra']['title_display'] != 0) {
    $element['value'] = array(
      '#markup' => "<label>" . _webform_filter_xss($component['name']) . "</label>",
    );
  }
  $datepicker = FALSE;
  if (isset($component['extra']['matrix_col'])) {
    $editvalue = unserialize($value[0]);
    $matrix_col = $component['extra']['matrix_col'];
    $matrix_row = $component['extra']['matrix_row'];
    for ($prow = 1; $prow <= $matrix_row; $prow++) {
      if (isset($component['extra']['element'])) {
        $element[$prow]['#prefix'] = "<div class='matrix-render-row-div matrix-render-col-{$matrix_col}'>";
        $element[$prow]['#suffix'] = "</div>";
        foreach ($component['extra']['element'] as $elekey => $elevalue) {
          list($j, $i) = explode('-', $elekey);
          $width = 100 / $matrix_col;
          $width = $width - 0.001;
          $prefix = "<div style='width:{$width}% !important;' class='matrix-row-column-element'>";
          $suffix = "</div>";
          $eletype = $elevalue['type'];
          $title = isset($elevalue['title']) ? $elevalue['title'] : $elekey;
          $title_display = $prow == 1 ? 'before' : "invisible";

          // Only first row is manadatory.
          $required = FALSE;
          if ($prow == 1) {

            // If component is mandatory.
            $required = $component['mandatory'] ? TRUE : FALSE;
            if (!$required) {

              // If particular element is manadatory.
              $required = $elevalue['mandatory'] ? TRUE : FALSE;
            }
          }
          switch ($eletype) {
            case 'textfield':
              $element[$prow][$i] = array(
                '#type' => 'textfield',
                '#title' => $title,
                '#prefix' => $prefix,
                '#suffix' => $suffix,
                '#maxlength' => 50,
                '#default_value' => $editvalue[$prow][$i],
                '#required' => $required,
                '#title_display' => $title_display,
              );
              break;
            case 'select':
              $options = $elevalue['option'];
              $option_array = array();
              $option_array = _webform_matrix_component_select_option_from_text($options);
              $element[$prow][$i] = array(
                '#type' => 'select',
                '#title' => $title,
                '#options' => $option_array,
                '#prefix' => $prefix,
                '#suffix' => $suffix,
                '#default_value' => $editvalue[$prow][$i],
                '#required' => $required,
                '#title_display' => $title_display,
                '#empty_option' => 'Select',
                '#empty_value' => '_none',
              );
              break;
            case 'date':
              $datepicker = isset($elevalue['datepicker']) ? $elevalue['datepicker'] : FALSE;
              $element[$prow][$i] = array(
                '#type' => 'date',
                '#title' => $title,
                '#title_display' => $title_display,
                '#required' => $required,
                '#start_date' => !empty($elevalue['startdate']) ? $elevalue['startdate'] : '-2 years',
                '#end_date' => !empty($elevalue['enddate']) ? $elevalue['enddate'] : '+2 years',
                '#year_textfield' => 0,
                '#default_value' => !empty($editvalue[$prow][$i]) ? $editvalue[$prow][$i] : $elevalue['default_date'],
                '#timezone' => 'user',
                '#process' => array(
                  'webform_expand_date',
                ),
                '#theme' => 'webform_date_matrix',
                '#theme_wrappers' => array(
                  'webform_element',
                ),
                '#element_validate' => array(
                  'webform_validate_date',
                ),
                '#datepicker' => $datepicker,
                '#prefix' => $prefix,
                '#suffix' => $suffix,
              );
              break;
            case 'label':
              $label_name = $elevalue['label_name'];
              $element[$prow][$i] = array(
                '#type' => 'item',
                '#title' => $title,
                '#markup' => $label_name,
                '#attributes' => array(
                  'class' => array(
                    'form-item',
                  ),
                ),
                '#prefix' => $prefix,
                '#suffix' => $suffix,
                '#default_value' => $editvalue[$prow][$i],
                '#title_display' => $title_display,
              );
              break;
          }
          drupal_alter('webform_render_matrix', $component, $elevalue, $element[$prow][$i]);
        }
      }
    }
  }
  if ($datepicker) {
    $element['#attached']['library'] = array(
      array(
        'system',
        'ui.datepicker',
      ),
    );
  }
  $element['#theme'][] = 'webform_matrix';
  $element_validate_array = array();
  foreach (module_implements('webform_matrix_validate') as $module) {
    $element_validate_array[] = $module . '_webform_matrix_validate';
  }
  if (!empty($element_validate_array)) {
    $element['#element_validate'] = $element_validate_array;
  }
  return $element;
}