/[drupal]/contributions/modules/index/includes/index.form.inc
ViewVC logotype

Diff of /contributions/modules/index/includes/index.form.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1.2.15 by xano, Sun Nov 8 23:18:45 2009 UTC revision 1.1.2.16 by xano, Tue Nov 17 16:08:36 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: index.form.inc,v 1.1.2.14 2009/11/08 20:11:49 xano Exp $  // $Id: index.form.inc,v 1.1.2.15 2009/11/08 23:18:45 xano Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 12  Line 12 
12   * @return array   * @return array
13   *   A Drupal form.   *   A Drupal form.
14   */   */
15  function index_form_add($form_state, $iid = NULL) {  function index_form_add(array $form_state, $iid = NULL) {
16    drupal_add_css(drupal_get_path('module', 'index') . '/css/index.admin.css', 'module', 'all', FALSE);    drupal_add_css(drupal_get_path('module', 'index') . '/css/index.admin.css', 'module', 'all', FALSE);
17    drupal_add_js(drupal_get_path('module', 'index') . '/js/index.admin.js');    drupal_add_js(drupal_get_path('module', 'index') . '/js/index.admin.js');
18    
# Line 109  function index_form_add($form_state, $ii Line 109  function index_form_add($form_state, $ii
109    
110    // Layers widget.    // Layers widget.
111    $form['layers'] = index_form_add_layers($index);    $form['layers'] = index_form_add_layers($index);
   $form['layers']['update'] = array(  
     '#type' => 'submit',  
     '#value' => t('Update hierarchy'),  
     '#submit' => array('index_form_add_update'),  
   );  
112    
113    $form['buttons']['save'] = array(    $form['buttons']['save'] = array(
114      '#type' => 'submit',      '#type' => 'submit',
115      '#value' => t('Save')      '#value' => t('Save'),
116        '#submit' => array('index_form_add_save'),
117    );    );
118    
119    return $form;    return $form;
# Line 136  function index_form_add_validate(array $ Line 132  function index_form_add_validate(array $
132    if ($values['page_enabled'] && !$values['page_path']) {    if ($values['page_enabled'] && !$values['page_path']) {
133      form_set_error('page_path', t('Please enter a path for the index page.'));      form_set_error('page_path', t('Please enter a path for the index page.'));
134    }    }
   // Assure there are no empty levels in the hierarchy.  
   $set = FALSE;  
   for ($i = $values['layer_count'] - 1; $i >= 0; $i--) {  
     if ($values["layer_type_$i"] !== '0') {  
       $set = TRUE;  
     }  
     elseif ($set) {  
       form_set_error("layer_type_$i", t('Level !level in your hierarchy may not be empty if level !level_next is set.', array('!level' => $i + 1, '!level_next' => $i + 2)));  
     }  
   }  
135  }  }
136    
137  /**  /**
138   * Submit handler for index_form_add().   * Submit handler for index_form_add().
139     *
140     * Save user submitted data.
141   */   */
142  function index_form_add_submit(array $form, array &$form_state) {  function index_form_add_save(array $form, array &$form_state) {
143    $values = $form_state['values'];    $values = $form_state['values'];
144    index_load_include('classes');    index_load_include('classes');
145    $index = new index($values['iid']);    $index = new index($values['iid']);
# Line 168  function index_form_add_submit(array $fo Line 156  function index_form_add_submit(array $fo
156  }  }
157    
158  /**  /**
159   * Update index_form_add() to process user submitted data.   * Submit handler for index_form_add().
160     *
161     * Rebuild index_form_add() to process user submitted data.
162   */   */
163  function index_form_add_update(array $form, array &$form_state) {  function index_form_add_update(array $form, array &$form_state) {
164    $form_state['rebuild'] = TRUE;    $form_state['rebuild'] = TRUE;
# Line 179  function index_form_add_update(array $fo Line 169  function index_form_add_update(array $fo
169   */   */
170  function theme_index_form_add(array $form) {  function theme_index_form_add(array $form) {
171    $rows = array();    $rows = array();
172    for ($i = 0; $i < $form['layers']['layer_count']['#value']; $i++) {    foreach ($form['layers']['lineages']['#value'] as $lineage => $depth) {
173      $layer = &$form['layers']['selectors'][$i];      $required = $form['layers']["layer_type_$lineage"]['#required'] ? '<span class="form-required" title="' . t('This field is required.') . '">*</span>' : NULL;
     $required = $layer["layer_type_$i"]['#required'] ? '<span class="form-required" title="' . t('This field is required.') . '">*</span>' : NULL;  
174      $rows[] = array(      $rows[] = array(
175        theme('indentation', $i - 1) . theme('index_form_add_layer_depth', $i) . drupal_render($layer["layer_type_$i"]),        theme('indentation', $depth - 1) . theme('index_form_add_layer_depth', $depth) . (isset($form['layers']["layer_type_item_$lineage"]) ? drupal_render($form['layers']["layer_type_item_$lineage"]) : drupal_render($form['layers']["layer_type_$lineage"])),
176        drupal_render($layer["layer_count_$i"]),        drupal_render($form['layers']["layer_count_$lineage"]),
177          drupal_render($form['layers']["layer_child_add_$lineage"]),
178          drupal_render($form['layers']["layer_remove_$lineage"]),
179      );      );
180    }    }
181    $rows[] = array(    $rows[] = array(
182      array(      array(
183        'data' => drupal_render($form['layers']['update']),        'data' => drupal_render($form['layers']['layer_child_add_root']),
184        'colspan' => 3,        'colspan' => 4,
185      ),      ),
186    );    );
187    $header = array(    $header = array(
188      t('Data type'),      t('Data type'),
189      t('Count only'),      t('Count only'),
190        array(
191          'data' => t('Operations'),
192          'colspan' => 2,
193        ),
194    );    );
195    $layers = theme('table', $header, $rows, array('id' => 'index-form-add-layers'), t('Hierarchy'));    $layers = theme('table', $header, $rows, array('id' => 'index-form-add-layers'), t('Hierarchy'));
196    
# Line 203  function theme_index_form_add(array $for Line 198  function theme_index_form_add(array $for
198  }  }
199    
200  /**  /**
201   * Theme the layer depth.   * Theme the layer depth icon.
202     *
203     * @param $depth integer
204     *   The depth of the layer to theme the depth icon for.
205   */   */
206  function theme_index_form_add_layer_depth($depth) {  function theme_index_form_add_layer_depth($depth) {
207    $root = !$depth ? 'root' : NULL;    $root = !$depth ? 'root' : NULL;
208    
209    return '<label for="edit-layer-type-' . $depth . '" class="index-form-add-layer-depth ' . $root . '">' . t('Layer !depth', array('!depth' => $depth)) . '</label>';    return '<div class="index-form-add-layer-depth ' . $root . '"></div>';
210  }  }
211    
212  /**  /**
# Line 221  function theme_index_form_add_layer_dept Line 219  function theme_index_form_add_layer_dept
219   */   */
220  function index_form_add_populate_index(index $index, array $form_state) {  function index_form_add_populate_index(index $index, array $form_state) {
221    $values = $form_state['values'];    $values = $form_state['values'];
222    
223      // Loop through all values and add them if the index accepts them.
224    foreach ($values as $property => $value) {    foreach ($values as $property => $value) {
225      if (isset($index->$property)) {      if (isset($index->$property)) {
226        $index->$property = $values[$property];        $index->$property = $values[$property];
227      }      }
228    }    }
229    $index->layers = array();  
230    for ($i = 0; $i < $values['layer_count']; $i++) {    // Add child layers and remove existing ones based on user input.
231      if ($values["layer_type_$i"] !== '0') {    $clicked_button = end($form_state['clicked_button']['#array_parents']);
232        $index->layers[$i] = array(    $clicked_parts = explode('_', $clicked_button);
233          'type' => $values["layer_type_$i"],    $clicked_lineage = (string) array_pop($clicked_parts);
234          'count' => isset($values["layer_count_$i"]) ? $values["layer_count_$i"] : 0,    $clicked_action = implode('_', $clicked_parts);
235        );    $lineages = array_keys($values['lineages']);
236      $layers = array();
237      // Add a new child layer.
238      if ($clicked_action == 'layer_child_add') {
239        // Compute the new layer's lineage if the new layer is in the root.
240        if ($clicked_lineage == 'root') {
241          $new_lineage = 0;
242          foreach (array_keys($values['lineages']) as $lineage_j) {
243            if (strpos($clicked_lineage, '.') === FALSE) {
244              $new_lineage++;
245            }
246          }
247          $lineages[] = $new_lineage;
248        }
249        // Compute the new layer's lineage if the layer has a parent.
250        else {
251          $count_children = 0;
252          foreach (array_keys($values['lineages']) as $lineage_j) {
253            if (preg_match("#$clicked_lineage-\d+?#", $lineage_j)) {
254              $count_children++;
255            }
256          }
257          $lineages[] = $clicked_lineage . '-' . $count_children;
258      }      }
259    }    }
260      foreach ($lineages as $lineage) {
261        $lineage = (string) $lineage;
262        // Don't save this layer and its children into the index if it has been
263        // removed.
264        if ($clicked_action == 'layer_remove' && ($lineage === $clicked_lineage || strpos($lineage, $clicked_lineage . '-') === 0)) {
265          continue;
266        }
267        // Add the current layer.
268        $layers[$lineage] = array(
269          'lineage' => $lineage,
270          'entity_type' => isset($values["layer_type_$lineage"]) ? $values["layer_type_$lineage"] : NULL,
271          'count' => isset($values["layer_count_$lineage"]) ? $values["layer_count_$lineage"] : 0,
272        );
273      }
274      $index->setLayers($layers);
275  }  }
276  /**  /**
277   * Create the layer building widget, by default a table.   * Create the layer building widget, by default a table.
278   */   */
279  function index_form_add_layers(index $index) {  function index_form_add_layers(index $index) {
280      // TODO: Allow root layers to be added.
281      // Prepare the entity types options.
282    $entity_types_info = index_entity_types_get();    $entity_types_info = index_entity_types_get();
283    $default_option = array('<' . t('none') . '>');    foreach ($entity_types_info as $entity_type => $entity_type_info) {
284    $parent_type = 0;      $entity_types_options[$entity_type] = $entity_type_info['#title'];
285    for ($i = 0; $i <= count($index->layers); $i++) {    }
286      $current_options = array();  
287      if ($parent_type) {    $form['layer_child_add_root'] = array(
288        $current_entity_types_info = array_intersect_key($entity_types_info, $entity_types_info[$parent_type]['#relations']);      '#type' => 'submit',
289        foreach ($current_entity_types_info as $entity_type => $entity_type_info) {      '#value' => t('Add item'),
290          $current_options[$entity_type] = $entity_type_info['#title'];      '#submit' => array('index_form_add_update'),
291        }      '#attributes' => array(
292      }        'class' => 'index-form-add-child-add',
293      else {        'title' => t('Add item'),
294        foreach ($entity_types_info as $entity_type => $entity_type_info) {      ),
295          $current_options[$entity_type] = $entity_type_info['#title'];    );
296        }    $form['lineages'] = array(
297      }      '#type' => 'value',
298        '#value' => array(),
299      // Save this layer's type for the next one.    );
300      if (isset($index->layers[$i])) {    _index_form_add_layers($form, $entity_types_info, $entity_types_options, $index->layers);
301        $parent_type = $index->layers[$i]['type'];  
302        $count = $index->layers[$i]['count'];    return $form;
303    }
304    
305    function _index_form_add_layers(array &$form, array $entity_types_info, array $entity_types_options, array $layers, $parent_entity_type = NULL, $depth = 0) {
306      foreach ($layers as $layer) {
307        $layer_entity_types_options = $parent_entity_type ? array_intersect_key($entity_types_options, $entity_types_info[$parent_entity_type]['#relations']) : $entity_types_options;
308        // Parent layers cannot be modified and are represented by a value element
309        // for processing and an item element for the user to see.
310        if (count($layer['children'])) {
311          $form['layer_type_' . $layer['lineage']] = array(
312            '#type' => 'value',
313            '#value' => $layer['entity_type'],
314          );
315          $form['layer_type_item_' . $layer['lineage']] = array(
316            '#type' => 'item',
317            '#value' => $entity_types_info[$layer['entity_type']]['#title'],
318          );
319      }      }
320        // Layers that have no children can be modified.
321      else {      else {
322        $parent_type = $count = 0;        $form['layer_type_' . $layer['lineage']] = array(
323            '#type' => 'select',
324            '#options' => $layer_entity_types_options,
325            // Unconfigured elements get the first of the available values as their
326            // default value.
327            '#default_value' => !is_null($layer['entity_type']) ? $layer['entity_type'] : array_shift(array_keys($layer_entity_types_options)),
328          );
329          $form['layer_count_' . $layer['lineage']] = array(
330            '#type' => 'checkbox',
331            '#default_value' => $layer['count'],
332          );
333      }      }
334    
335      $layers['selectors'][] = array(      $form['layer_child_add_' . $layer['lineage']] = array(
336        "layer_type_$i" => array(        '#type' => 'submit',
337          '#type' => 'select',        '#value' => t('Add item (!layer)', array('!layer' => $layer['lineage'])),
338          '#options' => array_merge($default_option, $current_options),        '#submit' => array('index_form_add_update'),
339          '#default_value' => $parent_type,        '#attributes' => array(
340          '#required' => $i < count($index->layers),          'class' => 'index-form-add-child-add',
341            'title' => t('Add item'),
342          ),
343        );
344        $form['layer_remove_' . $layer['lineage']] = array(
345          '#type' => 'submit',
346          '#value' => t('Remove (!layer)', array('!layer' => $layer['lineage'])),
347          '#submit' => array('index_form_add_update'),
348          '#attributes' => array(
349            'class' => 'index-form-add-layer-remove',
350            'title' => t('Remove this item and its children'),
351        ),        ),
       "layer_count_$i" => $i == count($index->layers) ? array(  
         '#type' => 'checkbox',  
         '#default_value' => $count,  
       ) : NULL,  
352      );      );
   }  
   $layers['layer_count'] = array(  
     '#type' => 'value',  
     '#value' => $i,  
   );  
353    
354    return $layers;      $form['lineages']['#value'][$layer['lineage']] = $depth;
355        _index_form_add_layers($form, $entity_types_info, $entity_types_options, $layer['children'], $layer['entity_type'], $depth + 1);
356      }
357  }  }
358    
359  /**  /**
# Line 313  function index_preview() { Line 382  function index_preview() {
382   * @return array   * @return array
383   *   A Drupal form.   *   A Drupal form.
384   */   */
385  function index_form_delete(&$form_state, $iid) {  function index_form_delete(array &$form_state, $iid) {
386    index_load_include('classes');    index_load_include('classes');
387    $index = new index((int)$iid);    $index = new index((int)$iid);
388    $form['#redirect'] = 'admin/build/index';    $form['#redirect'] = 'admin/build/index';
# Line 334  function index_form_delete(&$form_state, Line 403  function index_form_delete(&$form_state,
403  /**  /**
404   * Submit handler for index_form_delete().   * Submit handler for index_form_delete().
405   */   */
406  function index_form_delete_submit($form, &$form_state) {  function index_form_delete_submit(array $form, array &$form_state) {
407    $index = new index($form_state['values']['iid']);    $index = new index($form_state['values']['iid']);
408    $index->delete();    $index->delete();
409    drupal_set_message(t('%title has been deleted.', array('%title' => $index->title)));    drupal_set_message(t('%title has been deleted.', array('%title' => $index->title)));

Legend:
Removed from v.1.1.2.15  
changed lines
  Added in v.1.1.2.16

  ViewVC Help
Powered by ViewVC 1.1.3