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

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

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

revision 1.1 by xano, Tue Nov 17 17:46:41 2009 UTC revision 1.1.2.1 by xano, Tue Nov 17 17:46:41 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id: index.form.inc,v 1.1.2.16 2009/11/17 16:08:36 xano Exp $
3    
4    /**
5     * @file
6     *   The Index administration section.
7     */
8    
9    /**
10     * List all existing indexes.
11     */
12    function index_overview() {
13      $header = array(
14        t('Title'),
15        array(
16          'data' => t('Operations'),
17          'colspan' => 2,
18        ),
19      );
20      $rows = array();
21      $result = db_query("SELECT iid, title FROM {index_index} ORDER BY title");
22      while ($row = db_fetch_object($result)) {
23        $rows[] = array(
24          $row->title,
25          l(t('edit'), 'admin/build/index/edit/' . $row->iid),
26          l(t('delete'), 'admin/build/index/delete/' . $row->iid),
27        );
28      }
29      if (!count($rows)) {
30        $rows[] = array(array(
31          'data' => t('You have no indexes. <a href="!index_form_add">Add a new one</a>.', array('!index_form_add' => url('admin/build/index/add'))),
32          'colspan' => 3,
33        ));
34      }
35    
36      return theme('table', $header, $rows);
37    }
38    
39    /**
40     * Form: the index add form.
41     *
42     * @return array
43     *   A Drupal form.
44     */
45    function index_form_add(array $form_state, $iid = NULL) {
46      drupal_add_css(drupal_get_path('module', 'index') . '/css/index.admin.css', 'module', 'all', FALSE);
47      drupal_add_js(drupal_get_path('module', 'index') . '/js/index.admin.js');
48    
49      index_load_include('classes');
50      index_load_include('view');
51    
52      $index = new index((int) $iid);
53      if (isset($form_state['values'])) {
54        index_form_add_populate_index($index, $form_state);
55      }
56    
57      $form = array(
58        '#redirect' => 'admin/build/index',
59        '#suffix' => '<div id="index-form-add-preview-page"><h2>' . t('Page preview') . '</h2><div class="content">' . $index->preview('page') . '</div></div><div id="index-form-add-preview-block"><h2>' . t('Block preview') . '</h2><div class="content">' . $index->preview('page') . '</div></div>',
60      );
61    
62      // Internal settings.
63      $form['iid'] = array(
64        '#type' => 'value',
65        '#value' => $iid,
66      );
67    
68      // Index identification.
69      $form['identification'] = array(
70        '#prefix' => '<div id="index-form-add-identification">',
71        '#suffix' => '</div>',
72      );
73      $form['identification']['title'] = array(
74        '#type' => 'textfield',
75        '#title' => t('Title'),
76        '#default_value' => $index->title,
77        '#maxlength' => 255,
78        '#required' => TRUE,
79      );
80      $form['identification']['description'] = array(
81        '#type' => 'textarea',
82        '#title' => t('Description'),
83        '#default_value' => $index->description,
84      );
85    
86      // "Create as" settings.
87      $modes = index_modes_get();
88      $page_modes = $block_modes = array();
89      foreach ($modes as $callback => $mode) {
90        if ($mode['#page']) {
91          $page_modes[$callback] = $mode['#title'];
92        }
93        if ($mode['#block']) {
94          $block_modes[$callback] = $mode['#title'];
95        }
96      }
97      $page_path = check_plain($index->page_path);
98      $page_path_description = $page_path ? t('Currently located at !location.', array('!location' => l('/' . $page_path, $page_path))) : '';
99      $form['create'] = array(
100        '#type' => 'fieldset',
101        '#title' => t('Create as'),
102          '#prefix' => '<div id="index-form-add-create">',
103          '#suffix' => '</div>',
104      );
105      $form['create']['page_enabled'] = array(
106        '#type' => 'checkbox',
107        '#title' => t('Page'),
108        '#default_value' => $index->page_enabled,
109      );
110      $form['create']['page_mode'] = array(
111        '#type' => 'select',
112        '#title' => t('Display mode'),
113        '#options' => $page_modes,
114        '#default_value' => $index->page_mode,
115        '#prefix' => '<div id="index-page-settings" class="index-settings">',
116      );
117      $form['create']['page_path'] = array(
118        '#type' => 'textfield',
119        '#title' => t('Path'),
120        '#description' => $page_path_description,
121        '#default_value' => $index->page_path,
122        '#maxlength' => '255',
123        '#element_validate' => array('index_path_validate'),
124        '#suffix' => '</div>',
125      );
126      $form['create']['block_enabled'] = array(
127        '#type' => 'checkbox',
128        '#title' => t('Block'),
129        '#default_value' => $index->block_enabled,
130      );
131      $form['create']['block_mode'] = array(
132        '#type' => 'select',
133        '#title' => t('Display mode'),
134        '#options' => $block_modes,
135        '#default_value' => $index->block_mode,
136        '#prefix' => '<div id="index-block-settings" class="index-settings">',
137        '#suffix' => '</div>',
138      );
139    
140      // Layers widget.
141      $form['layers'] = index_form_add_layers($index);
142    
143      $form['buttons']['save'] = array(
144        '#type' => 'submit',
145        '#value' => t('Save'),
146        '#submit' => array('index_form_add_save'),
147      );
148    
149      return $form;
150    }
151    
152    /**
153     * Validate handler for index_form_add().
154     */
155    function index_form_add_validate(array $form, array &$form_state) {
156      $values = $form_state['values'];
157      // Assure that this index is being created as at least a page or a block.
158      if (!$values['page_enabled'] && !$values['block_enabled']) {
159        form_set_error('page_enabled', t('Please create this index as a page and/or a block.'));
160        form_set_error('block_enabled');
161      }
162      if ($values['page_enabled'] && !$values['page_path']) {
163        form_set_error('page_path', t('Please enter a path for the index page.'));
164      }
165    }
166    
167    /**
168     * Submit handler for index_form_add().
169     *
170     * Save user submitted data.
171     */
172    function index_form_add_save(array $form, array &$form_state) {
173      $values = $form_state['values'];
174      index_load_include('classes');
175      $index = new index($values['iid']);
176      index_form_add_populate_index($index, $form_state);
177    
178      if ($values['iid']) {
179        $index->update();
180        drupal_set_message(t('%title has been updated.', array('%title' => $index->title)));
181      }
182      else {
183        $index->insert();
184        drupal_set_message(t('%title has been created.', array('%title' => $index->title)));
185      }
186    }
187    
188    /**
189     * Submit handler for index_form_add().
190     *
191     * Rebuild index_form_add() to process user submitted data.
192     */
193    function index_form_add_update(array $form, array &$form_state) {
194      $form_state['rebuild'] = TRUE;
195    }
196    
197    /**
198     * Theme index_form_add().
199     */
200    function theme_index_form_add(array $form) {
201      $rows = array();
202      foreach ($form['layers']['lineages']['#value'] as $lineage => $depth) {
203        $required = $form['layers']["layer_type_$lineage"]['#required'] ? '<span class="form-required" title="' . t('This field is required.') . '">*</span>' : NULL;
204        $rows[] = array(
205          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"])),
206          drupal_render($form['layers']["layer_count_$lineage"]),
207          drupal_render($form['layers']["layer_child_add_$lineage"]),
208          drupal_render($form['layers']["layer_remove_$lineage"]),
209        );
210      }
211      $rows[] = array(
212        array(
213          'data' => drupal_render($form['layers']['layer_child_add_root']),
214          'colspan' => 4,
215        ),
216      );
217      $header = array(
218        t('Data type'),
219        t('Count only'),
220        array(
221          'data' => t('Operations'),
222          'colspan' => 2,
223        ),
224      );
225      $layers = theme('table', $header, $rows, array('id' => 'index-form-add-layers'), t('Hierarchy'));
226    
227      return drupal_render($form['identification']) . drupal_render($form['create']) . $layers . drupal_render($form);
228    }
229    
230    /**
231     * Theme the layer depth icon.
232     *
233     * @param $depth integer
234     *   The depth of the layer to theme the depth icon for.
235     */
236    function theme_index_form_add_layer_depth($depth) {
237      $root = !$depth ? 'root' : NULL;
238    
239      return '<div class="index-form-add-layer-depth ' . $root . '"></div>';
240    }
241    
242    /**
243     * Populate an index with form submitted data.
244     *
245     * @param $index index
246     *   The index object to populate.
247     * @param $form_state array
248     *   The $form_state of the form from which use get the data.
249     */
250    function index_form_add_populate_index(index $index, array $form_state) {
251      $values = $form_state['values'];
252    
253      // Loop through all values and add them if the index accepts them.
254      foreach ($values as $property => $value) {
255        if (isset($index->$property)) {
256          $index->$property = $values[$property];
257        }
258      }
259    
260      // Add child layers and remove existing ones based on user input.
261      $clicked_button = end($form_state['clicked_button']['#array_parents']);
262      $clicked_parts = explode('_', $clicked_button);
263      $clicked_lineage = (string) array_pop($clicked_parts);
264      $clicked_action = implode('_', $clicked_parts);
265      $lineages = array_keys($values['lineages']);
266      $layers = array();
267      // Add a new child layer.
268      if ($clicked_action == 'layer_child_add') {
269        // Compute the new layer's lineage if the new layer is in the root.
270        if ($clicked_lineage == 'root') {
271          $new_lineage = 0;
272          foreach (array_keys($values['lineages']) as $lineage_j) {
273            if (strpos($clicked_lineage, '.') === FALSE) {
274              $new_lineage++;
275            }
276          }
277          $lineages[] = $new_lineage;
278        }
279        // Compute the new layer's lineage if the layer has a parent.
280        else {
281          $count_children = 0;
282          foreach (array_keys($values['lineages']) as $lineage_j) {
283            if (preg_match("#$clicked_lineage-\d+?#", $lineage_j)) {
284              $count_children++;
285            }
286          }
287          $lineages[] = $clicked_lineage . '-' . $count_children;
288        }
289      }
290      foreach ($lineages as $lineage) {
291        $lineage = (string) $lineage;
292        // Don't save this layer and its children into the index if it has been
293        // removed.
294        if ($clicked_action == 'layer_remove' && ($lineage === $clicked_lineage || strpos($lineage, $clicked_lineage . '-') === 0)) {
295          continue;
296        }
297        // Add the current layer.
298        $layers[$lineage] = array(
299          'lineage' => $lineage,
300          'entity_type' => isset($values["layer_type_$lineage"]) ? $values["layer_type_$lineage"] : NULL,
301          'count' => isset($values["layer_count_$lineage"]) ? $values["layer_count_$lineage"] : 0,
302        );
303      }
304      $index->setLayers($layers);
305    }
306    /**
307     * Create the layer building widget, by default a table.
308     */
309    function index_form_add_layers(index $index) {
310      // Prepare the entity types options.
311      $entity_types_info = index_entity_types_get();
312      foreach ($entity_types_info as $entity_type => $entity_type_info) {
313        $entity_types_options[$entity_type] = $entity_type_info['#title'];
314      }
315    
316      $form['layer_child_add_root'] = array(
317        '#type' => 'submit',
318        '#value' => t('Add item'),
319        '#submit' => array('index_form_add_update'),
320        '#attributes' => array(
321          'class' => 'index-form-add-layer-child-add',
322          'title' => t('Add item'),
323        ),
324      );
325      $form['lineages'] = array(
326        '#type' => 'value',
327        '#value' => array(),
328      );
329      _index_form_add_layers($form, $entity_types_info, $entity_types_options, $index->layers);
330    
331      return $form;
332    }
333    
334    function _index_form_add_layers(array &$form, array $entity_types_info, array $entity_types_options, array $layers, $parent_entity_type = NULL, $depth = 0) {
335      foreach ($layers as $layer) {
336        $layer_entity_types_options = $parent_entity_type ? array_intersect_key($entity_types_options, $entity_types_info[$parent_entity_type]['#relations']) : $entity_types_options;
337        // Parent layers cannot be modified and are represented by a value element
338        // for processing and an item element for the user to see.
339        if (count($layer['children'])) {
340          $form['layer_type_' . $layer['lineage']] = array(
341            '#type' => 'value',
342            '#value' => $layer['entity_type'],
343          );
344          $form['layer_type_item_' . $layer['lineage']] = array(
345            '#type' => 'item',
346            '#value' => $entity_types_info[$layer['entity_type']]['#title'],
347          );
348        }
349        // Layers that have no children can be modified.
350        else {
351          $form['layer_type_' . $layer['lineage']] = array(
352            '#type' => 'select',
353            '#options' => $layer_entity_types_options,
354            // Unconfigured elements get the first of the available values as their
355            // default value.
356            '#default_value' => !is_null($layer['entity_type']) ? $layer['entity_type'] : array_shift(array_keys($layer_entity_types_options)),
357          );
358          $form['layer_count_' . $layer['lineage']] = array(
359            '#type' => 'checkbox',
360            '#default_value' => $layer['count'],
361          );
362        }
363    
364        $form['layer_child_add_' . $layer['lineage']] = array(
365          '#type' => 'submit',
366          '#value' => t('Add item (!layer)', array('!layer' => $layer['lineage'])),
367          '#submit' => array('index_form_add_update'),
368          '#attributes' => array(
369            'class' => 'index-form-add-layer-child-add',
370            'title' => t('Add item'),
371          ),
372        );
373        $form['layer_remove_' . $layer['lineage']] = array(
374          '#type' => 'submit',
375          '#value' => t('Remove (!layer)', array('!layer' => $layer['lineage'])),
376          '#submit' => array('index_form_add_update'),
377          '#attributes' => array(
378            'class' => 'index-form-add-layer-remove',
379            'title' => t('Remove this item and its children'),
380          ),
381        );
382    
383        $form['lineages']['#value'][$layer['lineage']] = $depth;
384        _index_form_add_layers($form, $entity_types_info, $entity_types_options, $layer['children'], $layer['entity_type'], $depth + 1);
385      }
386    }
387    
388    /**
389     * Write a rendered index to the screen for the AJAX preview.
390     */
391    function index_preview() {
392      index_load_include('classes');
393    
394      $index = new index();
395      $index->title = $_GET['title'];
396      $index->description = $_GET['description'];
397      $index->page_enabled = isset($_GET['page_enabled']) ? $_GET['page_enabled'] : FALSE;
398      $index->page_mode = $_GET['page_mode'];
399      $index->block_enabled = isset($_GET['block_enabled']) ? $_GET['block_enabled'] : FALSE;
400      $index->block_mode = $_GET['block_mode'];
401    
402      drupal_json(array(
403          'page' => $index->page_enabled ? $index->preview('page') : NULL,
404          'block' => $index->block_enabled ? $index->preview('block') : NULL,
405      ));
406    }
407    
408    /**
409     * Form: the index delete form.
410     *
411     * @return array
412     *   A Drupal form.
413     */
414    function index_form_delete(array &$form_state, $iid) {
415      index_load_include('classes');
416      $index = new index((int)$iid);
417      $form['#redirect'] = 'admin/build/index';
418      $form['iid'] = array(
419        '#type' => 'value',
420        '#value' => (int)$iid,
421      );
422    
423      return confirm_form($form,
424        t('Are you sure you want to delete %title?', array('%title' => $index->title)),
425        isset($_GET['destination']) ? $_GET['destination'] : 'admin/build/index',
426        t('This action cannot be undone.'),
427        t('Delete'),
428        t('Cancel')
429      );
430    }
431    
432    /**
433     * Submit handler for index_form_delete().
434     */
435    function index_form_delete_submit(array $form, array &$form_state) {
436      $index = new index($form_state['values']['iid']);
437      $index->delete();
438      drupal_set_message(t('%title has been deleted.', array('%title' => $index->title)));
439    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.3