/[drupal]/contributions/modules/taxonomy_menu/taxonomy_menu.module
ViewVC logotype

Diff of /contributions/modules/taxonomy_menu/taxonomy_menu.module

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

revision 1.19.2.2.2.16, Fri Feb 27 21:54:06 2009 UTC revision 1.19.2.2.2.17, Sun Mar 1 04:02:18 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: taxonomy_menu.module,v 1.19.2.2.2.15 2009/02/27 03:24:03 indytechcook Exp $  // $Id: taxonomy_menu.module,v 1.19.2.2.2.16 2009/02/27 21:54:06 indytechcook Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 33  function taxonomy_menu_form_alter(&$form Line 33  function taxonomy_menu_form_alter(&$form
33        '#collapsible' => TRUE,        '#collapsible' => TRUE,
34        '#title' => t('Taxonomy menu'),        '#title' => t('Taxonomy menu'),
35        '#weight' => 10,        '#weight' => 10,
36          '#tree' => TRUE,
37      );      );
38      //this turns the vocab terms into menu items      //this turns the vocab terms into menu items
39      $form['taxonomy_menu']['vocab_enabled'] = array(      $form['taxonomy_menu']['vocab_menu'] = array(
40        '#type' => 'select',        '#type' => 'select',
41        '#title' => t('Menu'),        '#title' => t('Menu'),
42        '#default_value' => variable_get('taxonomy_menu_vocab_'. $form['vid']['#value'], FALSE),        '#default_value' => variable_get('taxonomy_menu_vocab_menu_'. $form['vid']['#value'], FALSE),
43        '#options' => $menus,        '#options' => $menus,
44        '#description' => t('With this option enabled, an entry will be created in the menu system for this vocabulary.')        '#description' => t('With this option enabled, an entry will be created in the menu system for this vocabulary.')
45      );      );
46      $form['taxonomy_menu']['menu_path'] = array(      $form['taxonomy_menu']['path'] = array(
47        '#type' => 'select',        '#type' => 'select',
48        '#title' => t('Menu Path Type'),        '#title' => t('Menu Path Type'),
49        '#default_value' => variable_get('taxonomy_menu_path_'. $form['vid']['#value'], 0),        '#default_value' => variable_get('taxonomy_menu_path_'. $form['vid']['#value'], 0),
# Line 51  function taxonomy_menu_form_alter(&$form Line 52  function taxonomy_menu_form_alter(&$form
52          The menu path will be pased through drupal_get_path_alias() function so all aliases will be applied'          The menu path will be pased through drupal_get_path_alias() function so all aliases will be applied'
53        ),        ),
54      );      );
55      //options fieldset  
56      $form['taxonomy_menu']['options'] = array(      //get taxonomy menu form options
57        '#type' => 'fieldset',      $form['taxonomy_menu']['options'] = _taxonomy_menu_create_options($form['vid']['#value']);
58        '#title' => t('Options'),  
59        '#collapsible' => TRUE,      //rebuild the menu
     );  
     //syncronise the changes.  best to leave this one.  
     $form['taxonomy_menu']['options']['vocab_sync'] = array(  
       '#type' => 'checkbox',  
       '#title' => t('Syncronise changes to this vocabulary'),  
       '#default_value' => variable_get('taxonomy_menu_sync_'. $form['vid']['#value'], TRUE),  
       '#description' => t('Every time a term is added/deleted/modified, the corresponding menu link will be altered too.'),  
     );  
     //add the number of nodes attached to the term  
     $form['taxonomy_menu']['options']['vocab_item_num'] = array(  
       '#type' => 'checkbox',  
       '#title' => t('Display Number of Nodes'),  
       '#default_value' => variable_get('taxonomy_menu_display_num_'. $form['vid']['#value'], FALSE),  
       '#description' => t('Dipslay the number of Items per taxonomy Terms.  Will not show up for vocabulary menu items.'),  
     );  
     //hide empty terms  
     $form['taxonomy_menu']['options']['hide_empty_terms'] = array(  
       '#type' => 'checkbox',  
       '#title' => t('Hide Empty Terms'),  
       '#default_value' => variable_get('taxonomy_menu_hide_empty_terms_'. $form['vid']['#value'], FALSE),  
       '#description' => t('Dipslay the number of Items per taxonomy Terms.  Will not show up for vocabulary menu items.'),  
     );  
     //menu item for the vocabulary  
     $form['taxonomy_menu']['options']['vocab_item_enabled'] = array(  
       '#type' => 'checkbox',  
       '#title' => t('Item for Vocabulary'),  
       '#default_value' => variable_get('taxonomy_menu_voc_item_'. $form['vid']['#value'], TRUE),  
       '#description' => t("Shall the vocabulary have it's own item.<br />  
         If these setting is changed then the menu is rebuilt."),  
     );  
     //auto expand the menu  
     $form['taxonomy_menu']['options']['expanded'] = array(  
       '#type' => 'checkbox',  
       '#title' => t('Auto Expand Menu Item'),  
       '#default_value' => variable_get('taxonomy_menu_expanded_'. $form['vid']['#value'], TRUE),  
       '#description' => t('Sets the expand setting to TRUE'),  
     );  
     //rebuild the menu  
60      $form['taxonomy_menu']['options']['rebuild'] = array(      $form['taxonomy_menu']['options']['rebuild'] = array(
61        '#type' => 'checkbox',        '#type' => 'checkbox',
62        '#title' => t('Select to rebuild the menu on submit.'),        '#title' => t('Select to rebuild the menu on submit.'),
# Line 119  function taxonomy_menu_form_alter(&$form Line 82  function taxonomy_menu_form_alter(&$form
82   */   */
83  function taxonomy_menu_vocab_submit($form, &$form_state) {  function taxonomy_menu_vocab_submit($form, &$form_state) {
84    $vid = $form_state['values']['vid'];    $vid = $form_state['values']['vid'];
85      $changed = FALSE;
86    
87      //set the menu name and check for changes
88      $variable_name = _taxonomy_menu_build_variable('vocab_menu', $vid);
89      if (_taxonomy_menu_check_variable($variable_name, $form_state['values']['taxonomy_menu']['vocab_menu'])) {
90        $changed_menu = TRUE;
91      }
92      variable_set($variable_name, $form_state['values']['taxonomy_menu']['vocab_menu']);
93    
94      //set the path and check for changes
95      $variable_name = _taxonomy_menu_build_variable('path', $vid);
96      if (_taxonomy_menu_check_variable($variable_name, $form_state['values']['taxonomy_menu']['path'])) {
97        $changed = TRUE;
98      }
99      variable_set($variable_name, $form_state['values']['taxonomy_menu']['path']);
100    
101      foreach ($form_state['values']['taxonomy_menu']['options'] as $key => $value) {
102        //create the variable name
103        $variable_name = _taxonomy_menu_build_variable($key, $vid);
104    
105        //check to see if the vocab enable options has changed
106        if ($key = 'voc_item') {
107          if (_taxonomy_menu_check_variable($variable_name, $value)) {
108            $change_vocab_item = TRUE;
109          }
110        }
111    
112    $old_menu = variable_get('taxonomy_menu_vocab_'. $vid, FALSE);      //if the base path has changed
113    $new_menu = $form_state['values']['vocab_enabled'];      if ($key = 'base_path') {
114          if (_taxonomy_menu_check_variable($variable_name, $value)) {
115            $changed_base_path = TRUE;
116          }
117        }
118    
119        //if $changed is alreayd set to true, then don't bother checking any others
120        if (!$changed) {
121          //check to see if the variable has changed
122          if (_taxonomy_menu_check_variable($variable_name, $value)) {
123            $changed = TRUE;
124          }
125        }
126        //save variable
127        variable_set($variable_name, $value);
128      }
129    
130    $old_vocab_enabled = variable_get('taxonomy_menu_voc_item_'. $vid, FALSE);    //if the base path has changed then rebuild the menu before you do anything else
131    $new_vocab_enabled = $form_state['values']['vocab_item_enabled'];    //this is needed so the new path can be registered before menu_link_save is run
132      if ($changed_base_path) {
133        menu_rebuild;
134      }
135    
136    // set options    //if the menu hasn't changed and the menu is disabled then do not do anything else
137    variable_set('taxonomy_menu_sync_'. $vid, $form_state['values']['vocab_sync']);    if ($form_state['values']['taxonomy_menu']['options']['rebuild'] ||
138    variable_set('taxonomy_menu_voc_item_'. $vid, $new_vocab_enabled);        $changed_menu ||
139    variable_set('taxonomy_menu_display_num_'. $vid, $form_state['values']['vocab_item_num']);        (!$changed_menu && variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE) == 0)) {
140    variable_set('taxonomy_menu_expanded_'. $vid, $form_state['values']['expanded']);      //rebuild if rebuild is selected, menu has changed or vocabulary option changed
141    variable_set('taxonomy_menu_path_'. $vid, $form_state['values']['menu_path']);      if ($form_state['values']['taxonomy_menu']['options']['rebuild'] || $changed_menu || $change_vocab_item) {
142    variable_set('taxonomy_menu_token_term_path_'. $vid, $form_state['values']['token_term_path']);        $message = _taxonomy_menu_rebuild($vid);
143    variable_set('taxonomy_menu_token_vocab_path_'. $vid, $form_state['values']['token_vocab_path']);      }
144    variable_set('taxonomy_menu_hide_empty_terms_'. $vid, $form_state['values']['hide_empty_terms']);      //if setting has changed and a menu item is enabled, then update all of the menu items
145    variable_set('taxonomy_menu_vocab_'. $vid, $new_menu);      elseif ($changed && variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE)) {
146          $message = _taxonomy_menu_update_link_items($vid);
147    // only rebuild if the menu option has changed.      }
148    if ($form_state['values']['rebuild'] || ($old_menu != $new_menu) || ($old_vocab_enabled != $new_vocab_enabled)) {  
149      _taxonomy_menu_rebuild_link_items($vid, FALSE);      //do a full menu rebuild incase we have removed the menu or moved it between menus
150        variable_set('menu_rebuild_needed', TRUE);
151        drupal_set_message($message, 'status');
152    }    }
153    else {  }
154      _taxonomy_menu_rebuild_link_items($vid, TRUE);  
155    /**
156     * rebuilds a menu
157     *
158     * @param $vid
159     * @return $message
160     *  message that is displayed
161     */
162    function _taxonomy_menu_rebuild($vid) {
163      //remove all of the menu items for this vocabulary
164      _taxonomy_menu_delete_all($vid);
165    
166      //only insert the links if a menu is set
167      if (variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE)) {
168        _taxonomy_menu_insert_link_items($vid);
169        return t('The Taxonomy Menu has been rebuilt');
170    }    }
171    
172      return t('The Taxonomy Menu has been removed');
173  }  }
174    
175  /**  /**
176     * Checks to see if the variable has changed.
177     *
178     * @param $variable
179     *  name of variable
180     * @return Boolean
181     *  TRUE if it has changed
182     */
183    function _taxonomy_menu_check_variable($variable, $new_value) {
184      if ($new_value != variable_get($variable, FALSE)) {
185        return TRUE;
186      }
187      return FALSE;
188    }
189    
190    /**
191     * Update the menu items
192     *
193     * @param $vid
194     *  vocab id
195     */
196    function _taxonomy_menu_update_link_items($vid) {
197      // rebuild vocab 'taxonomy_menu_voc_item_' . $vid
198      $menu_name = variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE);
199    
200      //get a list of the current menu links
201      $menu_links = _taxonomy_menu_get_terms($vid);
202    
203      //cycle through the
204      foreach ($menu_links as $menu_link) {
205        if ($menu_link->tid == 0) {
206          $args['vid'] = $menu_link->vid;
207        }
208        else {
209          $args['term'] = taxonomy_get_term($menu_link->tid);
210        }
211    
212        //set the mneu name
213        $args['menu_name'] = $menu_name;
214    
215        //update the menu link
216        taxonomy_menu_handler('update', $args);
217      }
218    
219      return t("The Taxonomy Menu $menu_name has been updated.");
220    }
221    
222    /**
223     * Creates new link items for the vocabulary
224     *
225     * @param $vid
226     */
227    function _taxonomy_menu_insert_link_items($vid) {
228      $menu_name = variable_get('taxonomy_menu_vocab_menu_'. $vid, FALSE);
229    
230      //check to see if we should had a vocab item
231      if (variable_get('taxonomy_menu_voc_item_'. $vid, FALSE)) {
232          $args = array(
233            'vid' => $vid,
234            'menu_name' => $menu_name,
235          );
236    
237          $mlid = taxonomy_menu_handler('insert', $args);
238        }
239      //cycle through terms for the vocab
240      foreach (taxonomy_get_tree($vid) as $term) {
241        $args = array(
242          'term' => $term,
243          'menu_name' => $menu_name,
244        );
245    
246        $mlid = taxonomy_menu_handler('insert', $args);
247      }
248    }
249    /**
250   * Implementation of hook_taxonomy().   * Implementation of hook_taxonomy().
251   *   *
252   * When a user inserts, alters or deletes taxonomy terms, we can keep   * When a user inserts, alters or deletes taxonomy terms, we can keep
# Line 160  function taxonomy_menu_taxonomy($op, $ty Line 260  function taxonomy_menu_taxonomy($op, $ty
260      if ($op == 'delete') {      if ($op == 'delete') {
261        //delete the menu items        //delete the menu items
262        _taxonomy_menu_delete_all($vid);        _taxonomy_menu_delete_all($vid);
263        $menu_name = variable_get('taxonomy_menu_vocab_'. $args['vid'], 0);        $menu_name = variable_get('taxonomy_menu_vocab_menu_'. $args['vid'], 0);
264        menu_cache_clear($menu_name);        menu_cache_clear($menu_name);
265      }      }
266    }    }
267    else {    else {
268      // only sync if taxonomy_menu is enabled for this vocab and the 'sync'      // only sync if taxonomy_menu is enabled for this vocab and the 'sync'
269      // option has been checked.      // option has been checked.
270      $menu_name = variable_get('taxonomy_menu_vocab_'. $args['vid'], 0);      $menu_name = variable_get('taxonomy_menu_vocab_menu_'. $args['vid'], 0);
271      $sync = variable_get('taxonomy_menu_sync_'. $args['vid'], 0);      $sync = variable_get('taxonomy_menu_sync_'. $args['vid'], 0);
272    
273      if ($type == 'term' && $menu_name && $sync) {      if ($type == 'term' && $menu_name && $sync) {
# Line 225  function taxonomy_menu_taxonomy($op, $ty Line 325  function taxonomy_menu_taxonomy($op, $ty
325  }  }
326    
327  /**  /**
  * Remove all menu links for a given vocab and rebuild it completely.  
  *  
  * @param $vid vocab id  
  * @param $update  
  *  if set to true, then delete all menu records then rebuild  
  *  if set to false, then just update the menu items  
  */  
 function _taxonomy_menu_rebuild_link_items($vid, $update = TRUE) {  
   // rebuild vocab 'taxonomy_menu_voc_item_' . $vid  
   $menu_name = variable_get('taxonomy_menu_vocab_'. $vid, FALSE);  
   
   //if update is true then save the existing menu items with the new menu  
   if (!$update) {  
     _taxonomy_menu_delete_all($vid);  
   }  
   
   //only if the menu name is set  
   if ($menu_name) {  
     //if supposed to add a vocab item then add it here  
     if (variable_get('taxonomy_menu_voc_item_'. $vid, FALSE)) {  
       $args = array(  
         'vid' => $vid,  
         'menu_name' => $menu_name,  
       );  
   
       //if rebuilding all then run the insert handler  
       if (!$update) {  
         $mlid = taxonomy_menu_handler('insert', $args);  
       }  
       //if update then get the mlid and update the menu items  
       else {  
         $args['mlid'] = _taxonomy_menu_get_mlid(0, $vid);  
         taxonomy_menu_handler('update', $args);  
       }  
     }  
   
     //cycle through terms for the vocab  
     foreach (taxonomy_get_tree($vid) as $term) {  
       $args = array(  
         'term' => $term,  
         'menu_name' => $menu_name,  
       );  
   
       //if rebuilding all then run the insert handler  
       if (!$update) {  
         $mlid = taxonomy_menu_handler('insert', $args);  
       }  
       //if update then get the mlid and update the menu items  
       else {  
         $args['mlid'] = _taxonomy_menu_get_mlid($term->tid, $vid);  
         taxonomy_menu_handler('update', $args);  
       }  
   
     }  
     variable_set('menu_rebuild_needed', TRUE);  
     drupal_set_message(t('The taxonomy menu has been rebuilt.'), 'status');  
   }  
   else {  
     // no menu name is specified, so user has selected = DISABLED = from  
     // the taxonomy vocab form.  
     _taxonomy_menu_delete_all($vid);  
     //full menu rebuild is needed when deleting all of the items  
     variable_set('menu_rebuild_needed', TRUE);  
     drupal_set_message(t('The taxonomy menu has been disabled.'), 'status');  
   }  
 }  
   
 /**  
328   * Implementation of hook_nodeapi().   * Implementation of hook_nodeapi().
329   *   *
330   * This hook enables the menu to be displayed in context during node views.   * This hook enables the menu to be displayed in context during node views.
# Line 309  function taxonomy_menu_nodeapi(&$node, $ Line 341  function taxonomy_menu_nodeapi(&$node, $
341      foreach ($terms as $key => $tid) {      foreach ($terms as $key => $tid) {
342        $term = taxonomy_get_term($tid);        $term = taxonomy_get_term($tid);
343        //update the menu for each term if necessary        //update the menu for each term if necessary
344        $menu_name = variable_get('taxonomy_menu_vocab_'. $term->vid, FALSE);        $menu_name = variable_get('taxonomy_menu_vocab_menu_'. $term->vid, FALSE);
345        $vocb_sync = variable_get('taxonomy_menu_sync_'. $term->vid, TRUE);        $vocb_sync = variable_get('taxonomy_menu_sync_'. $term->vid, TRUE);
346        $menu_num = variable_get('taxonomy_menu_display_num_' , $term->vid, FALSE);        $menu_num = variable_get('taxonomy_menu_display_num_' , $term->vid, FALSE);
347    
# Line 376  function taxonomy_menu_nodeapi(&$node, $ Line 408  function taxonomy_menu_nodeapi(&$node, $
408   *     'menu_name' => menu that the item is set to apply to   *     'menu_name' => menu that the item is set to apply to
409   *     'mlid' => Menu ID   *     'mlid' => Menu ID
410   */   */
411  function taxonomy_menu_handler($op, $args = array()) {  function taxonomy_menu_handler($op, $args = array(), $item = array()) {
412    
413    //get the initial $item    //get the initial $item
414    $item = _taxonomy_menu_create_item($args);    if (empty($item)) {
415        $item = _taxonomy_menu_create_item($args);
416      }
417    
418    //let other modules make edits    //let other modules make edits
419    $item = module_invoke_all('taxonomy_menu_'. $op, $item);    $item = module_invoke_all('taxonomy_menu_'. $op, $item);
# Line 515  function taxonomy_menu_path_default($vid Line 549  function taxonomy_menu_path_default($vid
549    else {    else {
550      $path = taxonomy_term_path(taxonomy_get_term($tid));      $path = taxonomy_term_path(taxonomy_get_term($tid));
551    }    }
552    
553    return drupal_get_path_alias($path);    return $path;
554  }  }
555    
556  /**  /**
# Line 675  function taxonomy_menu_taxonomy_menu_ins Line 709  function taxonomy_menu_taxonomy_menu_ins
709  function taxonomy_menu_taxonomy_menu_update($item) {  function taxonomy_menu_taxonomy_menu_update($item) {
710    return _taxonomy_menu_item($item);    return _taxonomy_menu_item($item);
711  }  }
712    
713    /**
714     * Used to create a form array of taxonomy menu options
715     * invokes hook_taxonomy_menu_options
716     *
717     * @return $form array
718     */
719    function _taxonomy_menu_create_options($vid) {
720      $options = module_invoke_all('taxonomy_menu_options');
721    
722      //cycle through field
723      foreach ($options as $field_name => $field_elements) {
724        //cycle through each value of the field
725        $variable_name = _taxonomy_menu_build_variable($field_name, $vid);
726    
727        //if the variable is set then use that, if the default key is set then use that, otherwise use false
728        $options[$field_name]['#default_value'] =
729          variable_get($variable_name,
730          !empty($options[$field_name]['default']) ? $options[$field_name]['default'] : FALSE);
731    
732        //set the type to checkbox if it is empty
733        if (empty($options[$field_name]['#type'])) {
734          $options[$field_name]['#type'] = 'checkbox';
735        }
736    
737        //set the option feildset values
738        $options['#type'] = 'fieldset';
739        $options['#title'] = t('Options');
740        $options['#collapsible'] = TRUE;
741    
742        //remove the default value from the array so we don't pass it to the form
743        unset($options[$field_name]['default']);
744      }
745    
746      return $options;
747    }
748    
749    function _taxonomy_menu_build_variable($name, $vid) {
750      $base_string = "taxonomy_menu_%s_$vid";
751    
752      return sprintf($base_string, $name);
753    }
754    
755    /**
756     * Implementation of hook_taxonomy_menu_options()
757     *
758     * @return array
759     *  Uses the value to set the variable taxonomy_menu_<value>_$vid
760     *  $options[value]
761     *   default - optional.  this is what will be used if the varialbe is not set.  if empty then FALSE is used
762     *   #title - required.
763     *   any other form element
764     */
765    function taxonomy_menu_taxonomy_menu_options() {
766    
767      $options['sync'] = array(
768        '#title' => t('Syncronise changes to this vocabulary'),
769        '#description' => t('Every time a term is added/deleted/modified, the corresponding menu link will be altered too.'),
770        'default' => TRUE,
771      );
772    
773      if (module_exists('pathauto')) {
774        $options['base_path'] = array(
775          '#title' => t('Base Path'),
776          '#type' => 'textfield',
777          '#description' => t('Base Path if using Pathauto'),
778          'default' => t('category'),
779        );
780      }
781    
782      $options['display_num'] = array(
783        '#title' => t('Display Number of Nodes'),
784        '#description' => t('Dipslay the number of Items per taxonomy Terms.  Will not show up for vocabulary menu items.'),
785        'default' => FALSE,
786      );
787    
788      $options['hide_empty_terms'] = array(
789        '#title' => t('Hide Empty Terms'),
790        '#description' => t('Hide terms with no nodes attached to them.'),
791        'default' => FALSE,
792      );
793    
794      $options['voc_item'] = array(
795        '#title' => t('Item for Vocabulary'),
796        '#description' => t("Shall the vocabulary have it's own item."),
797        'default' => TRUE,
798      );
799    
800      $options['expanded'] = array(
801        '#title' => t('Auto Expand Menu Item'),
802        '#description' => t('Sets the expand setting to TRUE'),
803        'default' => TRUE,
804      );
805    
806      return $options;
807    }

Legend:
Removed from v.1.19.2.2.2.16  
changed lines
  Added in v.1.19.2.2.2.17

  ViewVC Help
Powered by ViewVC 1.1.2