/[drupal]/contributions/modules/category/category_display/category_display.module
ViewVC logotype

Diff of /contributions/modules/category/category_display/category_display.module

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

revision 1.39, Sun May 31 09:15:03 2009 UTC revision 1.40, Wed Aug 5 04:52:54 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: category_display.module,v 1.38 2009/02/07 09:01:16 jaza Exp $  // $Id: category_display.module,v 1.39 2009/05/31 09:15:03 jaza Exp $
3    
4  /**  /**
5   * @file   * @file
# Line 69  function category_display_nodeapi(&$node Line 69  function category_display_nodeapi(&$node
69    
70      case 'prepare':      case 'prepare':
71        $behavior = variable_get('category_behavior_'. $node->type, 0);        $behavior = variable_get('category_behavior_'. $node->type, 0);
72        if (!empty($behavior)) {        if (!empty($behavior) && $behavior == 'container') {
         $links_for_cats = $_POST['category']['menu']['links_for_cats'];  
         $toc_for_cats = $_POST['category']['display']['toc_for_cats'];  
         $show_listing = $_POST['category']['display']['show_listing'];  
73    
74          // Prepare defaults for the add/edit form.          // Prepare defaults for the add/edit form.
75          if (empty($node->category_menu)) {          if (empty($node->category_menu)) {
76            $node->category_menu = array();            $node->category_menu = array();
77          }          }
78            if (empty($node->category_display)) {
79          if (isset($_POST['category']['menu']['links_for_cats'])) {            $node->category_display = _category_display_defaults();
           $node->category_menu['links_for_cats'] = $links_for_cats;  
         }  
         if (isset($_POST['category']['display']['toc_for_cats'])) {  
           $node->category_display['toc_for_cats'] = $toc_for_cats;  
         }  
         if (isset($_POST['category']['display']['show_listing'])) {  
           $node->category_display['show_listing'] = $show_listing;  
80          }          }
81        }        }
82        break;        break;
# Line 94  function category_display_nodeapi(&$node Line 84  function category_display_nodeapi(&$node
84      case 'insert':      case 'insert':
85      case 'update':      case 'update':
86        $behavior = variable_get('category_behavior_'. $node->type, 0);        $behavior = variable_get('category_behavior_'. $node->type, 0);
       _category_display_flatten_values($node);  
   
87        if (!empty($behavior) && $behavior == 'container') {        if (!empty($behavior) && $behavior == 'container') {
88          category_display_save_container($node);          category_display_save_container($node);
89        }        }
# Line 125  function category_display_form_alter(&$f Line 113  function category_display_form_alter(&$f
113    
114      if (!empty($behavior) && $behavior == 'container') {      if (!empty($behavior) && $behavior == 'container') {
115        $form['category']['display'] = _category_display_form_elements($node);        $form['category']['display'] = _category_display_form_elements($node);
116          $form['#submit'][] = 'category_display_node_form_submit';
117      }      }
118    }    }
119  }  }
# Line 278  function _category_display_form_elements Line 267  function _category_display_form_elements
267  }  }
268    
269  /**  /**
270     * Additional form submit handler for node forms.
271     *
272     * Flattens the $form_state['values']['category']['display'] array
273     * on both submits and previews.
274     */
275    function category_display_node_form_submit($form, &$form_state) {
276      $fieldsets = array(
277        'display' => TRUE,
278      );
279      if (!empty($form_state['values']['category']) && is_array($form_state['values']['category'])) {
280        foreach ($form_state['values']['category'] as $key => $value) {
281          if (isset($fieldsets[$key]) && is_array($value)) {
282            $fieldset = 'category_'. $key;
283            if (empty($form_state['values'][$fieldset])) {
284              $form_state['values'][$fieldset] = array();
285            }
286            $form_state['values'][$fieldset] = $value + $form_state['values'][$fieldset];
287    
288            unset($form_state['values']['category'][$key]);
289          }
290        }
291      }
292    }
293    
294    /**
295   * Implementation of hook_category_legacy().   * Implementation of hook_category_legacy().
296   */   */
297  function category_display_category_legacy($op, &$vocabularies, $edit = NULL) {  function category_display_category_legacy($op, &$vocabularies, $edit = NULL) {
# Line 307  function category_display_category_legac Line 321  function category_display_category_legac
321   *   The display settings for the specified node.   *   The display settings for the specified node.
322   */   */
323  function category_display_get_container($nid) {  function category_display_get_container($nid) {
324    $result = db_query("SELECT * FROM {category_display} WHERE cid = %d", $nid);    $cache_key = 'cdisp_get_cont';
325      $container = category_cache_op('get', $nid, $cache_key);
326      if (!isset($container)) {
327    
328    if ($container = db_fetch_object($result)) {      $result = db_query("SELECT * FROM {category_display} WHERE cid = %d", $nid);
329      return $container;      $container = db_fetch_object($result);
330        if (empty($container)) {
331          $container = (object) _category_display_defaults();
332        }
333    
334        category_cache_op('set', $nid, $cache_key, $container);
335    }    }
336      return $container;
337  }  }
338    
339  /**  /**
# Line 334  function category_display_save_container Line 356  function category_display_save_container
356      $status = SAVED_UPDATED;      $status = SAVED_UPDATED;
357    }    }
358    
359      category_cache_op('flush', $node->nid);
360    return $status;    return $status;
361  }  }
362    
# Line 346  function category_display_save_container Line 369  function category_display_save_container
369   */   */
370  function category_display_del_container($nid) {  function category_display_del_container($nid) {
371    db_query('DELETE FROM {category_display} WHERE cid = %d', $nid);    db_query('DELETE FROM {category_display} WHERE cid = %d', $nid);
372      category_cache_op('flush', $nid);
373    }
374    
375    /**
376     * Helper function to provide default settings for new containers.
377     */
378    function _category_display_defaults() {
379      return array(
380        'navlinks'      => 0,
381        'toc_for_cats'  => 0,
382        'toc_depth'     => 0,
383        'toc_nodecount' => 0,
384        'nodelinks'     => 1,
385        'show_listing'  => 1,
386        'emptymsg'      => 1,
387      );
388  }  }
389    
390  /**  /**
# Line 477  function category_display_next($book_lin Line 516  function category_display_next($book_lin
516   * Format the menu links for the child pages of the current page.   * Format the menu links for the child pages of the current page.
517   */   */
518  function category_display_children($book_link, $container_info) {  function category_display_children($book_link, $container_info) {
519    $book_link_orig = $book_link;    $cache_key = 'cdisp_children'. $container_info->cid .':'. $book_link['mlid'];
520      $output = category_cache_op('get', 0, $cache_key);
521      if (!isset($output)) {
522    
523    // If the TOC has more than one level, then we need to find a menu item with      $book_link_orig = $book_link;
524    // a specific depth. For example, if we want the TOC to be five levels deep,  
525    // then we need to find a menu item at the fifth level of the hierarchy (or      // If the TOC has more than one level, then we need to find a menu item with
526    // as close to it as possible). This menu item then gets passed to      // a specific depth. For example, if we want the TOC to be five levels deep,
527    // category_menu_tree_all_data(), so that it retrieves a menu hierarchy      // then we need to find a menu item at the fifth level of the hierarchy (or
528    // with the depth we need.      // as close to it as possible). This menu item then gets passed to
529    if ($container_info->toc_depth != 0 && isset($book_link)) {      // category_menu_tree_all_data(), so that it retrieves a menu hierarchy
530      if ($container_info->toc_depth == -1 || $container_info->toc_depth > MENU_MAX_DEPTH) {      // with the depth we need.
531        $container_info->toc_depth = MENU_MAX_DEPTH;      if ($container_info->toc_depth != 0 && isset($book_link)) {
532      }        if ($container_info->toc_depth == -1 || $container_info->toc_depth > MENU_MAX_DEPTH) {
533      $container_info->toc_depth += $book_link['depth'];          $container_info->toc_depth = MENU_MAX_DEPTH;
534      if ($book_link_new = _category_display_children_recurse($book_link, $container_info)) {        }
535        $book_link = $book_link_new;        $container_info->toc_depth += $book_link['depth'];
536          if ($book_link_new = _category_display_children_recurse($book_link, $container_info)) {
537            $book_link = $book_link_new;
538          }
539      }      }
   }  
540    
541    $book_link = $book_link_orig;      $book_link = $book_link_orig;
542    
543    $tree = array();      $tree = array();
544    
545    if (isset($book_link)) {      if (isset($book_link)) {
546      $tree = category_menu_tree_all_data($book_link_orig['menu_name'], $book_link, $container_info->toc_depth);        $tree = category_menu_tree_all_data($book_link_orig['menu_name'], $book_link, $container_info->toc_depth);
547    }      }
548    // When no menu link is provided to this function, we're generating a TOC      // When no menu link is provided to this function, we're generating a TOC
549    // for a hidden container. This is typically useful for things such as a site      // for a hidden container. This is typically useful for things such as a site
550    // map. All of the below code is for the special case of hidden containers      // map. All of the below code is for the special case of hidden containers
551    // that have no menu link.      // that have no menu link.
552    else {      else {
553      $children = category_get_children($container_info->cid);        $children = category_get_children($container_info->cid);
554      $containers_map = array();        $containers_map = array();
555      if (!empty($children)) {        if (!empty($children)) {
556        foreach (array_keys($children) as $child_cid) {          foreach (array_keys($children) as $child_cid) {
557          $child_cnid = !empty($children[$child_cid]->cnid) ? $children[$child_cid]->cnid : $child_cid;            $child_cnid = !empty($children[$child_cid]->cnid) ? $children[$child_cid]->cnid : $child_cid;
558          $child_menu = category_menu_get_container($child_cnid);            $child_menu = category_menu_get_container($child_cnid);
559          $child_display = category_display_get_container($child_cnid);            $child_display = category_display_get_container($child_cnid);
560          $child_menu_map = _category_display_children_get_menu_map($children[$child_cid], $child_cnid);            $child_menu_map = _category_display_children_get_menu_map($children[$child_cid], $child_cnid);
561    
562          if ($container_info->toc_depth != 0) {            if ($container_info->toc_depth != 0) {
563            if ($container_info->toc_depth == -1 || $container_info->toc_depth > MENU_MAX_DEPTH) {              if ($container_info->toc_depth == -1 || $container_info->toc_depth > MENU_MAX_DEPTH) {
564              $container_info->toc_depth = MENU_MAX_DEPTH;                $container_info->toc_depth = MENU_MAX_DEPTH;
565                }
566                $container_info->toc_depth += $child_menu_map['depth'];
567                $container_info->toc_depth--;
568                if ($child_menu_map_new = _category_display_children_recurse($child_menu_map, $container_info, 1, TRUE)) {
569                  $child_menu_map = $child_menu_map_new;
570                }
571            }            }
572            $container_info->toc_depth += $child_menu_map['depth'];            else {
573            $container_info->toc_depth--;              $container_info->toc_depth = $child_menu_map['depth'];
574            if ($child_menu_map_new = _category_display_children_recurse($child_menu_map, $container_info, 1, TRUE)) {              $container_info->toc_depth--;
             $child_menu_map = $child_menu_map_new;  
575            }            }
         }  
         else {  
           $container_info->toc_depth = $child_menu_map['depth'];  
           $container_info->toc_depth--;  
         }  
576    
577          if ($child_menu->links_for_cats && !isset($containers_map[$child_cnid])) {            if ($child_menu->links_for_cats && !isset($containers_map[$child_cnid])) {
578            $tree_new = category_menu_tree_all_data('category-menu-toc-'. $child_cnid, $child_menu_map, $container_info->toc_depth);              $tree_new = category_menu_tree_all_data('category-menu-toc-'. $child_cnid, $child_menu_map, $container_info->toc_depth);
579            _category_display_children_prune_tagged_content($tree_new, $container_info);              _category_display_children_prune_tagged_content($tree_new, $container_info);
580            $tree += $tree_new;              $tree += $tree_new;
581            $containers_map[$child_cnid] = TRUE;              $containers_map[$child_cnid] = TRUE;
582              }
583          }          }
       }  
584    
585        if (!$container_info->toc_depth) {          if (!$container_info->toc_depth) {
586          foreach (array_keys($tree) as $key) {            foreach (array_keys($tree) as $key) {
587            unset($tree[$key]['below']);              unset($tree[$key]['below']);
588              }
589          }          }
590        }        }
591      }      }
   }  
592    
593    // We've retrieved a menu hierarchy with the desired depth. Now we set the      // We've retrieved a menu hierarchy with the desired depth. Now we set the
594    // menu link back to what it was originally, and we reduce the hierarchy so      // menu link back to what it was originally, and we reduce the hierarchy so
595    // that it doesn't include the parents of the current page, or any menu links      // that it doesn't include the parents of the current page, or any menu links
596    // for assigned content.      // for assigned content.
597    if (isset($book_link)) {      if (isset($book_link)) {
598      $tree = _category_display_children_reduce($tree, $book_link, $container_info);        $tree = _category_display_children_reduce($tree, $book_link, $container_info);
599    }      }
600    
601        $output = $tree ? ($container_info->toc_nodecount ? category_display_menu_tree_output($tree) : menu_tree_output($tree)) : '';
602    
603    return $tree ? ($container_info->toc_nodecount ? category_display_menu_tree_output($tree) : menu_tree_output($tree)) : '';    category_cache_op('set', 0, $cache_key, $output);
604      }
605      return $output;
606  }  }
607    
608  /**  /**
# Line 656  function _category_display_children_prun Line 703  function _category_display_children_prun
703          preg_match('/^node\/(\d+)$/', $tree[$key]['link']['link_path'], $matches);          preg_match('/^node\/(\d+)$/', $tree[$key]['link']['link_path'], $matches);
704          if (!empty($matches[1]) && $type = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $matches[1]))) {          if (!empty($matches[1]) && $type = db_result(db_query("SELECT type FROM {node} WHERE nid = %d", $matches[1]))) {
705            $behavior = variable_get('category_behavior_'. $type, 0);            $behavior = variable_get('category_behavior_'. $type, 0);
706            if (empty($behavior) && db_result(db_query("SELECT n.nid FROM {node} n INNER JOIN {category_node} cn ON n.nid = cn.nid WHERE n.type = '%s'", $type))) {            if (empty($behavior)) {
707              $include_link = FALSE;              $include_link = FALSE;
708            }            }
709          }          }
# Line 836  function theme_category_display_menu_ite Line 883  function theme_category_display_menu_ite
883    
884    return $output;    return $output;
885  }  }
   
 /**  
  * Helper function to flatten the $node->category['display'] array.  
  */  
 function _category_display_flatten_values(&$node) {  
   $fieldsets = array(  
     'display' => TRUE,  
   );  
   if (!empty($node->category) && is_array($node->category)) {  
     foreach ($node->category as $key => $value) {  
       if (isset($fieldsets[$key]) && is_array($value)) {  
         $fieldset = 'category_'. $key;  
         if (empty($node->$fieldset)) {  
           $node->$fieldset = array();  
         }  
         $node->$fieldset += $value;  
   
         unset($node->category[$key]);  
       }  
     }  
   }  
 }  

Legend:
Removed from v.1.39  
changed lines
  Added in v.1.40

  ViewVC Help
Powered by ViewVC 1.1.2