/[drupal]/contributions/modules/flexifilter/flexifilter.admin.inc
ViewVC logotype

Diff of /contributions/modules/flexifilter/flexifilter.admin.inc

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

revision 1.14.2.1, Thu Apr 17 21:58:06 2008 UTC revision 1.14.2.2, Mon Jun 16 23:51:45 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: flexifilter.admin.inc,v 1.16 2008/04/13 02:35:27 cwgordon7 Exp $  // $Id: flexifilter.admin.inc,v 1.14.2.1 2008/04/17 21:58:06 cwgordon7 Exp $
3    
4  /**  /**
5   * Generates the form for the admin overview page   * Generates the form for the admin overview page
# Line 39  function flexifilter_filter_list_form_ta Line 39  function flexifilter_filter_list_form_ta
39    $filters           = flexifilter_get_filters();    $filters           = flexifilter_get_filters();
40    foreach ($filters as $id => $filter) {    foreach ($filters as $id => $filter) {
41      if ($filter['enabled'] == $enabled) {      if ($filter['enabled'] == $enabled) {
42        $row   = array($filter['label'], $filter['description']);        $row   = array(check_plain($filter['label']), check_plain($filter['description']));
43        $row[] = l(t('Edit'), 'admin/build/flexifilters/'. $id .'/edit', array(), drupal_get_destination());        $row[] = l(t('Edit'), 'admin/build/flexifilters/'. $id .'/edit', array(), drupal_get_destination());
44        $row[] = l(t('Delete'), 'admin/build/flexifilters/'. $id .'/delete', array(), drupal_get_destination());        $row[] = l(t('Delete'), 'admin/build/flexifilters/'. $id .'/delete', array(), drupal_get_destination());
45        $row[] = l(t('Preview'), 'admin/build/flexifilters/'. $id .'/preview', array(), drupal_get_destination());        $row[] = l(t('Preview'), 'admin/build/flexifilters/'. $id .'/preview', array(), drupal_get_destination());
# Line 64  function flexifilter_filter_list_form_ta Line 64  function flexifilter_filter_list_form_ta
64  }  }
65    
66  /**  /**
67   * Helper function for flexifilter_filter_edit_form; removes the automatic   * Generates the form for a given flexifilter component or condition (and its
68   * naming of submit controls.   * children, and their children...
  * Our forms are complex and have submit controls nested within fieldsets,  
  * and when pressed, we want the 'op' field to be set within the fieldset,  
  * not at the top level, so we have to undo the automatic naming of submit  
  * fields.  
  * This function is a brutal rip of code from _form_builder_handle_input_element  
69   *   *
70   * @param $form A form element to reset to the normal name.   * @param $components
71   *   *   An array of components/conditions to render.
72   * @return The updated form element   * @return
73   */   *   A FAPI form definition.
 function flexifilter_undo_submit_default_name($form) {  
   $name = array_shift($form['#parents']);  
   $form['#name'] = $name;  
   if ($form['#type'] == 'file') {  
     // To make it easier to handle $_FILES in file.inc, we place all  
     // file fields in the 'files' array. Also, we do not support  
     // nested file names.  
     $form['#name'] = 'files['. $form['#name'] .']';  
   }  
   elseif (count($form['#parents'])) {  
     $form['#name'] .= '['. implode('][', $form['#parents']) .']';  
   }  
   array_unshift($form['#parents'], $name);  
   return $form;  
 }  
   
 /**  
  * Helper function for flexifilter_filter_edit_components_data; converts a condition  
  * array from a flexifilter into a form data styled array.  
  */  
 function flexifilter_filter_edit_condition_data($data) {  
   $condition = array(  
     'class' => $data['class'],  
   );  
   foreach ($data['settings'] as $key => $value) {  
     if ($key == 'conditions') {  
       foreach ($value as $cond_key => $cond_value) {  
         $condition['condition_'. $cond_key] = flexifilter_filter_edit_condition_data($cond_value);  
       }  
     }  
     else {  
       $condition['setting_'. $key] = $value;  
     }  
   }  
   return $condition;  
 }  
   
 /**  
  * Helper function for flexifilter_filter_edit_form; converts a components  
  * array from a flexifilter into a form data styled array.  
  */  
 function flexifilter_filter_edit_components_data($components, $id_prefix = NULL) {  
   $data = array();  
   if (isset($components['id_prefix'])) {  
     $id_prefix = $components['id_prefix'];  
     $data['id_prefix'] = $id_prefix;  
     unset($components['id_prefix']);  
   }  
   if (isset($components['id_next'])) {  
     $data['id_next'] = $components['id_next'];  
     unset($components['id_next']);  
   }  
   foreach ($components as $n => $component) {  
     $us = array();  
     foreach ($component['settings'] as $key => $value) {  
       if ($key == 'components') {  
         $us['components'] = flexifilter_filter_edit_components_data($value, $id_prefix);  
       }  
       elseif ($key == 'condition') {  
         $us['condition'] = flexifilter_filter_edit_condition_data($value);  
       }  
       elseif ($key == 'step') {  
         $us[$key] = $value;  
       }  
       else {  
         $us['setting_'. $key] = $value;  
       }  
     }  
     $us['weight'] = $n;  
     $us = array_merge($us, $component);  
     unset($us['settings']);  
     $id = $us['id'];  
     unset($us['id']);  
     $data[$id_prefix . $id] = $us;  
   }  
   return $data;  
 }  
   
 /**  
  * Creates an FAPI values array for a newly created component  
  *  
  * @param $component_class The component class of the new component  
  */  
 function flexifilter_filter_edit_form_new_component_data($component_class) {  
   $components = flexifilter_get_component_list();  
   $component  = $components[$component_class];  
   $data       = array(  
     'class' => $component_class,  
     'weight' => 10000,  
     'is_new' => true,  
   );  
   if ($component['step'] == 'either') {  
     $data['step'] = 'process';  
   }  
   if ($component['is_container']) {  
     if ($component['contains_condition']) {  
       $data['condition'] = array('class' => '');  
     }  
     if ($component['contains_components']) {  
       $data['components'] = array();  
     }  
   }  
   return $data;  
 }  
   
 /**  
  * Creates an FAPI values array for a newly created condition  
  *  
  * @param $condition_class The condition class of the new condition  
  */  
 function flexifilter_filter_edit_form_new_condition_data($condition_class) {  
   $conditions = flexifilter_get_condition_list();  
   $condition  = $conditions[$condition_class];  
   $data       = array(  
     'class' => $condition_class,  
     'is_new' => TRUE,  
     'weight' => 10000,  
   );  
   return $data;  
 }  
   
 /**  
  * Helper function for flexifilter_filter_edit_form_component; creates an FAPI form table  
  * for a condition.  
74   */   */
75  function flexifilter_filter_edit_form_condition($data, $show_advanced) {  function flexifilter_component_edit_form($components) {
76    if ($data['class'] == '') {    $form = array();
     $form = array(  
       '#type' => 'fieldset',  
       '#collapsible' => TRUE,  
       '#collapsed' => (isset($data['op']) || isset($data['op_c']) || isset($data['is_new'])) ? FALSE : TRUE,  
       '#tree' => TRUE,  
       '#title' => t('No condition'),  
       '#description' => t('There is no condition set. This will probably cause the component to do nothing, as "No Condition" evaluates to false.'),  
     );  
     $form['class'] = array('#type' => 'hidden', '#value' => '');  
     return $form;  
   }  
   
   $conditions = flexifilter_get_condition_list();  
   $condition  = $conditions[$data['class']];  
   $form       = array(  
     '#type' => 'fieldset',  
     '#collapsible' => TRUE,  
     '#collapsed' => (isset($data['op']) || isset($data['is_new'])) ? FALSE : TRUE,  
     '#tree' => TRUE,  
     '#title' => $condition['label'],  
     '#description' => $condition['description'],  
   );  
   $form['class'] = array('#type' => 'hidden', '#value' => $data['class']);  
   $custom_settings = array();  
   foreach ($data as $key => $value) {  
     if (strncmp($key, 'setting_', 8) == 0) {  
       $custom_settings[substr($key, 8)] = $value;  
     }  
   }  
   $custom_elements = flexifilter_invoke_condition(array('class' => $data['class'], 'settings' => $custom_settings), 'settings');  
   if (is_array($custom_elements)) {  
     foreach ($custom_elements as $key => $element) {  
       $form['setting_'. $key] = $element;  
     }  
   }  
   if ($condition['is_container']) {  
     if ($condition['contains_conditions']) {  
       $conditions = flexifilter_get_condition_list();  
   
       $condition_max_id = 0;  
       foreach ($data as $key => $sub_condition) {  
         if (strncmp($key, 'condition_', 10) == 0) {  
           $condition_max_id = max($condition_max_id, 1 + (int)substr($key, 10));  
           if (!isset($sub_condition['op'])) {  
             $form[$key] = flexifilter_filter_edit_form_condition($sub_condition, $show_advanced);  
           }  
           else {  
             $form['#collapsed'] = FALSE;  
           }  
         }  
       }  
       if (isset($data['op_c'])) {  
         $form['condition_'. $condition_max_id] = flexifilter_filter_edit_form_condition(flexifilter_filter_edit_form_new_condition_data($data['add_condition']), $show_advanced);  
       }  
   
       $form['add_condition'] = array(  
         '#type' => 'select',  
         '#title' => t('Add condition'),  
         '#options' => flexifilter_get_grouped_labels($conditions),  
         '#description' => t('Adds a new condition wthin this one.'),  
       );  
       $form['op_c'] = array(  
         '#type' => 'submit',  
         '#value' => t('Add'),  
         '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),  
       );  
       // Add some kind of seperator, otherwise this add button and the remove button are put onto the same line  
       // which is just a little bit confusing to the user. Hence we use a horizontal rule to seperate them alot.  
       $form['end_add_cond'] = array('#type' => 'markup', '#value' => '<br/><br/><hr/><br/>');  
     }  
   }  
   $form['op'] = array(  
     '#type' => 'submit',  
     '#value' => t('Remove condition'),  
     '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),  
   );  
   
   return $form;  
 }  
   
 /**  
  * Helper function for flexifilter_filter_edit_form_components; generates an FAPI array for a  
  * single component.  
  */  
 function flexifilter_filter_edit_form_component($data, $new_weight, $is_first, $is_last, &$data_base, $show_advanced) {  
   $components = flexifilter_get_component_list();  
   $component  = $components[$data['class']];  
   $form       = array(  
     '#type' => 'fieldset',  
     '#collapsible' => TRUE,  
     '#collapsed' => (isset($data['op']) || isset($data['op_c']) || isset($data['is_new'])) ? FALSE : TRUE,  
     '#tree' => TRUE,  
     '#title' => $component['label'],  
     '#description' => $component['description'],  
   );  
   $form['class'] = array(  
     '#type' => 'hidden',  
     '#value' => $data['class'],  
   );  
   $form['weight'] = array(  
     '#type' => 'hidden',  
     '#value' => $new_weight,  
   );  
   $custom_settings = array();  
   foreach ($data as $key => $value) {  
     if (strncmp($key, 'setting_', 8) == 0) {  
       $custom_settings[substr($key, 8)] = $value;  
     }  
   }  
   $custom_elements = flexifilter_invoke_component($component, 'settings', $custom_settings);  
   if (is_array($custom_elements)) {  
     foreach ($custom_elements as $key => $element) {  
       $form['setting_'. $key] = $element;  
     }  
   }  
   if ($component['is_container']) {  
     if ($component['contains_condition']) {  
       if (isset($data['op_c'])) {  
         $data['condition'] = flexifilter_filter_edit_form_new_condition_data($data['set_condition']);  
       }  
       if (isset($data['condition']) && isset($data['condition']['op'])) {  
         $data['condition'] = array(  
           'class' => '',  
         );  
         $form['#collapsed'] = FALSE;  
       }  
       $form['condition'] = flexifilter_filter_edit_form_condition($data['condition'], $show_advanced);  
       if ($form['condition']['#collapsed'] == FALSE) {  
         $form['#collapsed'] = FALSE;  
       }  
       $conditions = flexifilter_get_condition_list($show_advanced);  
       $form['set_condition'] = array(  
         '#type' => 'select',  
         '#title' => t('Change condition'),  
         '#options' => flexifilter_get_grouped_labels($conditions),  
         '#description' => t('Changes the condition assocated with this component.'),  
       );  
       $form['op_c'] = array(  
         '#type' => 'submit',  
         '#value' => t('Change'),  
         '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),  
       );  
     }  
     if ($component['contains_components']) {  
       $form['components'] = flexifilter_filter_edit_form_components($data['components'], $data_base, $show_advanced);  
       if ($form['components']['#collapsed'] == FALSE) {  
         $form['#collapsed'] = FALSE;  
       }  
     }  
   }  
   if ($component['step'] == 'either') {  
     if ($show_advanced) {  
       $form['step'] = array(  
         '#type' => 'select',  
         '#title' => t('Step'),  
         '#options' => array(  
           'process' => t('Processing'),  
           'prepare' => t('Preparation'),  
         ),  
         '#description' => t('The filtering step to perform this action in.'),  
         '#default_value' => $data['step'],  
       );  
     }  
     else {  
       $form['step'] = array(  
         '#type' => 'hidden',  
         '#value' => $data['step'],  
       );  
     }  
   }  
   $form['move'] = array(  
     '#type' => 'select',  
     '#title' => t('Re/move this'),  
     '#options' => array(),  
     // If the element at the bottom is moved to the top, then the #value of 'top' will no longer be in the downdown  
     // and the FAPI throws a nasty validation error. Hence mark it already validated and we will do our own validation.  
     '#validated' => true,  
   );  
   if (!$is_first) {  
     $form['move']['#options']['top'] = t('Move to top');  
     $form['move']['#options']['up'] = t('Move up');  
   }  
   if (!$is_last) {  
     $form['move']['#options']['down'] = t('Move down');  
     $form['move']['#options']['bottom'] = t('Move to bottom');  
   }  
   $form['move']['#options']['remove'] = t('Remove');  
   $form['op'] = array(  
     '#type' => 'submit',  
     '#value' => t('Re/move'),  
     '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),  
   );  
   
   return $form;  
 }  
   
 /**  
  * Helper function; sorts components by weight.  
  */  
 function _flexifilter_components_sort_weight($a, $b) {  
   return ((float)$a['weight']) > ((float)$b['weight']);  
 }  
   
 /**  
  * Generates an FAPI array for a component list  
  *  
  * @param $data FAPI values for the component list. Use flexifilter_filter_edit_components_data to  
  * convert a components array into coresponding FAPI values.  
  * @param $data_base FAPI values for the top-level component list  
  */  
 function flexifilter_filter_edit_form_components($data, &$data_base, $show_advanced) {  
   $form = array(  
     '#type' => 'fieldset',  
     '#title' => t('Components'),  
     '#collapsed' => TRUE,  
     '#collapsible' => TRUE,  
     '#tree' => TRUE,  
   );  
   $components = array();  
   foreach ($data as $key => $component) {  
     if (strncmp($key, $data_base['id_prefix'], strlen($data_base['id_prefix'])) == 0) {  
       $components[$key] = $component;  
     }  
   }  
   if (isset($data['op'])) {  
     $components[$data_base['id_prefix'] . $data_base['id_next']] = flexifilter_filter_edit_form_new_component_data($data['new_list']);  
     $data_base['id_next'] = $data_base['id_next'] + 1;  
   }  
   foreach ($components as $key => $component) {  
     if (isset($component['op'])) {  
       switch ($component['move']) {  
         /*  
           Careful; this component is NOT VALIDATED by the FAPI. We only act upon certain  
           values ('top', 'up', 'down', 'bottom', 'remove'), which are always valid (even  
           if they have no effect). Other values are silently ignored.  
         */  
   
         case 'top':  
           $components[$key]['weight'] = -1000;  
           break;  
   
         case 'up':  
           $components[$key]['weight'] = $component['weight'] - 1.5;  
           break;  
   
         case 'down':  
           $components[$key]['weight'] = $component['weight'] + 1.5;  
           break;  
   
         case 'bottom':  
           $components[$key]['weight'] = 10001;  
           break;  
   
         case 'remove':  
           unset($components[$key]);  
           break;  
       }  
       $form['#collapsed'] = FALSE;  
     }  
   }  
   uasort($components, '_flexifilter_components_sort_weight');  
   $new_weight = 0;  
   $last_component_weight = count($components);  
   foreach ($components as $key => $component) {  
     $new_weight = $new_weight + 1;  
     $form[$key] = flexifilter_filter_edit_form_component($component, $new_weight, $new_weight == 1, $new_weight == $last_component_weight, $data_base, $show_advanced);  
     if ($form[$key]['#collapsed'] == FALSE) {  
       $form['#collapsed'] = FALSE;  
     }  
   }  
   $form['new_list'] = array(  
     '#type' => 'select',  
     '#options' => flexifilter_get_grouped_labels(flexifilter_get_component_list($show_advanced)),  
     '#title' => t('Add component'),  
   );  
   $form['op'] = array(  
     '#type' => 'submit',  
     '#value' => t('Add'),  
     '#process' => array('flexifilter_undo_submit_default_name', 'form_expand_ahah'),  
   );  
77    return $form;    return $form;
78    }/*
79      foreach ($components as $component) {
80        switch ($component['type']) {
81          case 'condition':
82            $form[$component] = flex
83  }  }
84    
85  /**  /**
# Line 498  function flexifilter_filter_edit_form($f Line 94  function flexifilter_filter_edit_form($f
94      $data['basic']['description'] = $flexifilter === 'new' ? '' : $flexifilter['description'];      $data['basic']['description'] = $flexifilter === 'new' ? '' : $flexifilter['description'];
95      $data['basic']['advanced'] = $flexifilter === 'new' ? 0 : $flexifilter['advanced'];      $data['basic']['advanced'] = $flexifilter === 'new' ? 0 : $flexifilter['advanced'];
96      $data['basic']['cache'] = $flexifilter === 'new' ? 1 : $flexifilter['cache'];      $data['basic']['cache'] = $flexifilter === 'new' ? 1 : $flexifilter['cache'];
97      $data['components'] = $flexifilter === 'new' ? array() : flexifilter_filter_edit_components_data($flexifilter['components']);      //$data['components'] = $flexifilter === 'new' ? array() : flexifilter_filter_edit_components_data($flexifilter['components']);
     if (!isset($data['components']['id_prefix']) || $data['components']['id_prefix'] === 'component_new_') {  
       $data['components']['id_prefix'] = 'component_'. $data['fid'] .'_';  
     }  
     if (!isset($data['components']['id_next'])) {  
       $data['components']['id_next'] = 0;  
     }  
98      $expand_root_levels = TRUE;      $expand_root_levels = TRUE;
99    }    }
100    else {    else {
# Line 559  function flexifilter_filter_edit_form($f Line 149  function flexifilter_filter_edit_form($f
149        '#default_value' => $data['basic']['cache'],        '#default_value' => $data['basic']['cache'],
150      );      );
151    }    }
   $form['components'] = flexifilter_filter_edit_form_components($data['components'], $data['components'], $data['basic']['advanced'] == 1);  
   $form['components']['id_prefix'] = array('#type' => 'hidden', '#value' => $data['components']['id_prefix']);  
   $form['components']['id_next'] = array('#type' => 'hidden', '#value' => $data['components']['id_next']);  
152    if (isset($expand_root_levels) && $expand_root_levels) {    if (isset($expand_root_levels) && $expand_root_levels) {
153      $form['basic']['#collapsed'] = FALSE;      $form['basic']['#collapsed'] = FALSE;
154      $form['components']['#collapsed'] = FALSE;      $form['components']['#collapsed'] = FALSE;

Legend:
Removed from v.1.14.2.1  
changed lines
  Added in v.1.14.2.2

  ViewVC Help
Powered by ViewVC 1.1.2