- <?php
- * @file
- * Example demonstrating a parent/child tabledrag form
- */
-
- * Build the parent-child example form.
- *
- * Tabledrag will take care of updating the parent_id relationship on each
- * row of our table when we drag items around, but we need to build out the
- * initial tree structure ourselves. This means ordering our items such
- * that children items come directly after their parent items, and all items
- * are sorted by weight relative to their siblings.
- *
- * To keep this from cluttering the actual tabledrag code, we have moved
- * this to a dedicated function.
- *
- * @return array
- * A form array set for theming by theme_tabledrag_example_parent_form()
- *
- * @ingroup tabledrag_example
- */
- function tabledrag_example_parent_form($form_state) {
-
-
- $form['example_items']['#tree'] = TRUE;
-
-
- $result = tabledrag_example_parent_get_data();
-
-
- foreach ($result as $item) {
-
-
-
-
-
- $form['example_items'][$item->id] = array(
-
-
- 'name' => array(
- '#markup' => $item->name,
- ),
-
-
-
-
-
- 'description' => array(
- '#type' => 'textfield',
- '#default_value' => $item->description,
- '#size' => 20,
- '#maxlength' => 255,
- ),
-
-
-
-
-
-
-
-
-
-
-
-
-
- 'id' => array(
-
-
- '#type' => 'textfield',
- '#size' => 3,
- '#default_value' => $item->id,
- '#disabled' => TRUE,
- ),
-
-
-
- 'pid' => array(
-
-
- '#type' => 'textfield',
- '#size' => 3,
- '#default_value' => $item->pid,
- ),
-
-
-
-
- 'weight' => array(
- '#type' => 'weight',
- '#title' => t('Weight'),
- '#default_value' => $item->weight,
- '#delta' => 10,
- '#title_display' => 'invisible',
- ),
-
-
-
-
-
- 'depth' => array(
- '#type' => 'hidden',
- '#value' => $item->depth,
- ),
- );
- }
-
-
-
-
-
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save Changes'));
- return $form;
- }
-
- * Theme callback for the tabledrag_example_parent_form form.
- *
- * The theme callback will format the $form data structure into a table and
- * add our tabledrag functionality. (Note that backdrop_add_tabledrag should be
- * called from the theme layer, and not from a form declaration. This helps
- * keep template files clean and readable, and prevents tabledrag.js from
- * being added twice accidentally.
- *
- * @ingroup tabledrag_example
- */
- function theme_tabledrag_example_parent_form($variables) {
- $form = $variables['form'];
-
-
- $rows = array();
-
-
- foreach (element_children($form['example_items']) as $id) {
-
-
-
-
-
-
-
-
-
-
- $form['example_items'][$id]['weight']['#attributes']['class'] = array('example-item-weight');
-
-
-
-
- $form['example_items'][$id]['id']['#attributes']['class'] = array('example-item-id');
- $form['example_items'][$id]['pid']['#attributes']['class'] = array('example-item-pid');
-
-
-
-
-
- $class = array('draggable');
-
-
-
-
-
- if ($id == '8') {
- $class[] = 'tabledrag-root';
- }
-
-
-
-
-
- if ($id == '9') {
- $class[] = 'tabledrag-leaf';
- }
-
-
-
-
- $indent = theme('indentation', array('size' => $form['example_items'][$id]['depth']['#value']));
- unset($form['example_items'][$id]['depth']);
-
-
-
-
-
- $rows[] = array(
- 'data' => array(
-
- $indent . backdrop_render($form['example_items'][$id]['name']),
-
- backdrop_render($form['example_items'][$id]['description']),
-
- backdrop_render($form['example_items'][$id]['weight']),
-
- backdrop_render($form['example_items'][$id]['id']),
-
- backdrop_render($form['example_items'][$id]['pid']),
- ),
-
-
-
-
- 'class' => $class,
- );
- }
-
-
-
-
-
-
-
- $header = array(t('Name'), t('Description'), t('Weight'), t('ID'), t('PID'));
-
-
-
-
-
- $table_id = 'example-items-table';
-
-
- $output = theme('table', array(
- 'header' => $header,
- 'rows' => $rows,
- 'attributes' => array('id' => $table_id),
- ));
-
-
- $output .= backdrop_render_children($form);
-
-
-
-
-
-
-
-
-
-
-
-
-
- backdrop_add_tabledrag($table_id, 'match', 'parent', 'example-item-pid', 'example-item-pid', 'example-item-id', FALSE);
-
-
-
-
- backdrop_add_tabledrag($table_id, 'order', 'sibling', 'example-item-weight', NULL, NULL, FALSE);
-
- return $output;
- }
-
- * Submit callback for the tabledrag_example_parent_form form.
- *
- * Updates the 'weight' column for each element in our table, taking into
- * account that item's new order after the drag and drop actions have been
- * performed.
- *
- * @ingroup tabledrag_example
- */
- function tabledrag_example_parent_form_submit($form, &$form_state) {
-
-
- foreach ($form_state['values']['example_items'] as $id => $item) {
- db_query(
- "UPDATE {tabledrag_example} SET weight = :weight, pid = :pid WHERE id = :id",
- array(':weight' => $item['weight'], ':pid' => $item['pid'], ':id' => $id)
- );
- }
- }
-
- * Retrieves the tree structure from database, and sorts by parent/child/weight.
- *
- * The sorting should result in children items immediately following their
- * parent items, with items at the same level of the hierarchy sorted by
- * weight.
- *
- * The approach used here may be considered too database-intensive.
- * Optimization of the approach is left as an exercise for the reader. :)
- *
- * @ingroup tabledrag_example
- */
- function tabledrag_example_parent_get_data() {
-
- $rootnodes = db_query('SELECT id, name, description, weight, pid
- FROM {tabledrag_example}
- WHERE (pid = 0)
- ORDER BY weight ASC');
-
- $itemtree = array();
-
-
- $depth = -1;
-
- foreach ($rootnodes as $parent) {
- tabledrag_example_get_tree($parent, $itemtree, $depth);
- }
- return $itemtree;
- }
-
- * Recursively adds to the $itemtree array, ordered by parent/child/weight.
- *
- * @ingroup tabledrag_example
- */
- function tabledrag_example_get_tree($parentitem, &$itemtree = array(), &$depth = 0) {
-
- $depth++;
-
- $parentitem->depth = $depth;
-
- $itemtree[$parentitem->id] = $parentitem;
-
- $children = db_query('SELECT id, name, description, weight, pid
- FROM {tabledrag_example}
- WHERE (pid = :pid)
- ORDER BY weight ASC',
- array(':pid' => $parentitem->id));
- foreach ($children as $child) {
-
- if (!in_array($child->id, array_keys($itemtree))) {
-
- tabledrag_example_get_tree($child, $itemtree, $depth);
- }
- }
-
-
- $depth--;
- }