phpDocumentor pond
[ class tree: pond ] [ index: pond ] [ all elements ]

Source for file _chaptercache.class.php

Documentation is available at _chaptercache.class.php

  1. <?php
  2. /**
  3.  * This file implements the ChapterCache class.
  4.  *
  5.  * This file is part of Quam Plures - {@link http://quamplures.net/}
  6.  * See also {@link https://launchpad.net/quam-plures}.
  7.  *
  8.  * @copyright (c) 2009 - 2011 by the Quam Plures developers - {@link http://quamplures.net/}
  9.  * @copyright (c)2003-2009 by Francois PLANQUE - {@link http://fplanque.net/}
  10.  *
  11.  *  {@internal License choice
  12.  *  - If you have received this file as part of a package, please find the license.txt file in
  13.  *    the same folder or the closest folder above for complete license terms.
  14.  *  - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  15.  *    then you must choose one of the following licenses before using the file:
  16.  *    - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  17.  *    - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  18.  *  }}}
  19.  *
  20.  * @author fplanque: Francois PLANQUE
  21.  *
  22.  * @package pond
  23.  */
  24. if!defined('QP_MAIN_INIT') ) die'Please, do not access this page directly.' );
  25.  
  26. load_class'generic/model/_genericcategorycache.class.php' );
  27. load_class'chapters/model/_chapter.class.php' );
  28.  
  29. /**
  30.  * ChapterCache Class
  31.  *
  32.  * @package pond
  33.  */
  34. {
  35.     /**
  36.      * Lazy filled index of url titles
  37.      */
  38.     var $urlname_index = array();
  39.  
  40.  
  41.     /**
  42.      * Constructor
  43.      */
  44.     function ChapterCache()
  45.     {
  46.         global $Settings;
  47.  
  48.         if$Settings->get('chapter_ordering'== 'manual' )
  49.         {    // Manual order
  50.             $order_by 'cat_order';
  51.         }
  52.         else
  53.         {    // Alphabetic order
  54.             $order_by 'cat_name';
  55.         }
  56.  
  57.         parent::GenericCategoryCache'Chapter'false'T_categories''cat_''cat_ID''cat_name''blog_ID'$order_by );
  58.     }
  59.  
  60.  
  61.     /**
  62.      * Empty/reset the cache
  63.      */
  64.     function clear()
  65.     {
  66.         $this->urlname_index = array();
  67.         parent::clear();
  68.     }
  69.  
  70.  
  71.     /**
  72.      * Get an object from cache by ID
  73.      *
  74.      * Load the cache if necessary (all at once if allowed).
  75.      *
  76.      * @param integer ID of object to load
  77.      * @param boolean true if function should die on error
  78.      * @param boolean true if function should die on empty/null
  79.       * @param integer|NULLNULL for all subsets
  80.      * @return Chapter Reference on cached object or false.
  81.      */
  82.     function get_by_ID$req_ID$halt_on_error true$halt_on_empty true$subset_ID NULL )
  83.     {
  84.         global $DB$Debuglog;
  85.  
  86.         ifempty($req_ID) )
  87.         {
  88.             if($halt_on_empty)
  89.             {
  90.                 debug_die"Requested Chapter from $this->dbtablename without ID!);
  91.             }
  92.             $r NULL;
  93.             return $r;
  94.         }
  95.  
  96.         if!empty$this->cache$req_ID ) )
  97.         // Already in cache
  98.             $Debuglog->add"Accessing Chapter($req_ID) from cache"'dataobjects' );
  99.             return $this->cache$req_ID ];
  100.         }
  101.         elseif!$this->all_loaded )
  102.         // Not in cache, but not everything is loaded yet
  103.             if$this->load_all || is_null($subset_ID) )
  104.             // It's ok to just load everything:
  105.                 $this->load_all();
  106.             }
  107.             else
  108.             // Load just the requested object:
  109.                 $Debuglog->add"Loading <strong>$this->objtype($req_ID)</strong> into cache"'dataobjects' );
  110.                 // Note: $req_ID MUST be an unsigned integer. This is how DataObject works.
  111.                 $sql "SELECT *
  112.                           FROM T_categories
  113.                          WHERE cat_ID = $req_ID
  114.                            AND cat_blog_ID = ".$subset_ID;
  115.  
  116.                 if$row $DB->get_row$sqlOBJECT0'ChapterCache::get_by_ID()' ) )
  117.                 {
  118.                     if$this->instantiate$row ) )
  119.                     {
  120.                         $Debuglog->add'Could not add() object to cache!''dataobjects' );
  121.                     }
  122.                 }
  123.                 else
  124.                 {
  125.                     $Debuglog->add'Could not get DataObject by ID. Query: '.$sql'dataobjects' );
  126.                 }
  127.             }
  128.         }
  129.  
  130.         ifempty$this->cache$req_ID ) )
  131.         // Requested object does not exist
  132.             // $Debuglog->add( 'failure', 'dataobjects' );
  133.             if$halt_on_error )
  134.             {
  135.                 debug_die"Requested $this->objtype does not exist!);
  136.             }
  137.             $r false;
  138.             return $r;
  139.         }
  140.  
  141.         return $this->cache$req_ID ];
  142.     }
  143.  
  144.  
  145.  
  146.     /**
  147.      * Get an object from cache by urlname
  148.      *
  149.      * Load the cache if necessary (all at once if allowed).
  150.      *
  151.      * @param string ID of object to load
  152.      * @param boolean true if function should die on error
  153.      * @return reference on cached object
  154.      */
  155.     function get_by_urlname$req_urlname$halt_on_error true )
  156.     {
  157.         global $DB$Debuglog;
  158.  
  159.         if!isset$this->urlname_index[$req_urlname) )
  160.         // not yet in cache:
  161.             // Load just the requested object:
  162.             $Debuglog->add"Loading <strong>$this->objtype($req_urlname)</strong> into cache"'dataobjects' );
  163.             $sql "SELECT *
  164.                       FROM $this->dbtablename
  165.                      WHERE cat_urlname = ".$DB->quote($req_urlname);
  166.             $row $DB->get_row$sql );
  167.             ifempty$row ) )
  168.             {    // Requested object does not exist
  169.                 if$halt_on_error debug_die"Requested $this->objtype does not exist!);
  170.                 // put into index:
  171.                 $this->urlname_index[$req_urlnamefalse;
  172.  
  173.                 return $this->urlname_index[$req_urlname];
  174.             }
  175.  
  176.             $this->instantiate$row );
  177.  
  178.             // put into index:
  179.             $this->urlname_index[$req_urlname$this->cache$row->cat_ID ];
  180.         }
  181.         else
  182.         {
  183.             $Debuglog->add"Retrieving <strong>$this->objtype($req_urlname)</strong> from cache);
  184.         }
  185.  
  186.         return $this->urlname_index[$req_urlname];
  187.     }
  188.  
  189.  
  190.  
  191.     /**
  192.      * Load a keyed subset of the cache
  193.      *
  194.       * @param integer|NULLNULL for all subsets
  195.      */
  196.     function load_subset$subset_ID )
  197.     {
  198.         global $DB$Debuglog$Settings;
  199.  
  200.         if$this->all_loaded || isset$this->loaded_subsets[$subset_ID) )
  201.         // Already loaded
  202.             return false;
  203.         }
  204.  
  205.         // fp> TODO: This kills other subsets. BAD if we want to handle multiple subsets independently
  206.         $this->cleartrue );
  207.  
  208.         $Debuglog->add'ChapterCache - Loading <strong>chapters('.$subset_ID.')</strong> into cache''dataobjects' );
  209.         $sql 'SELECT *
  210.                             FROM T_categories
  211.                          WHERE cat_blog_ID = '.$subset_ID;
  212.         if$Settings->get('chapter_ordering'== 'manual' )
  213.         {    // Manual order
  214.             $sql .= ' ORDER BY cat_order';
  215.         }
  216.         else
  217.         {    // Alphabetic order
  218.             $sql .= ' ORDER BY cat_name';
  219.         }
  220.  
  221.         foreach$DB->get_results$sqlOBJECT'Loading chapters('.$subset_ID.') into cache' as $row )
  222.         {
  223.             // Instantiate a custom object
  224.             $this->instantiate$row );
  225.         }
  226.  
  227.         $this->loaded_subsets[$subset_IDtrue;
  228.  
  229.         return true;
  230.     }
  231.  
  232.  
  233.     /**
  234.      * Move a chapter and its descendants to a different collection
  235.      *
  236.      * @param integer 
  237.      * @param integer 
  238.      * @param integer 
  239.      */
  240.     function move_Chapter_subtree$chapter_ID$src_collection_ID$dest_collection_ID )
  241.     {
  242.         /**
  243.          * @var DB
  244.          */
  245.         global $DB;
  246.  
  247.         // Make sure children have been revealed for specific subset:
  248.         $this->reveal_children$src_collection_ID );
  249.  
  250.         // fp>We get the Chapter AFTER reveal_children, because something is wrong with reveal_children or get_by_ID
  251.         // I don't know what right now, but if we get Chapter earlier, we'll be stuck with an old copy of it that does NOT have the children
  252.         // TODO: find out what's freakin wrong
  253.         $Chapter $this->get_by_ID($chapter_ID);
  254.  
  255.         $chapters_to_move array();
  256.         // Get $chapters_to_move:
  257.         $this->recurse_move_subtree$Chapter$chapters_to_move );
  258.  
  259.         $DB->begin();
  260.  
  261.         // Move to root:
  262.         if$parent_Chapter $Chapter->get_parent_Chapter() )
  263.         {    // Was not already at root, cut it and move it:
  264.             $Chapter->set'parent_ID'NULL );
  265.             $Chapter->dbupdate();
  266.         }
  267.  
  268.         // Move Chapters to new Blog:
  269.         $sql 'UPDATE T_categories
  270.                              SET cat_blog_ID = '.$dest_collection_ID.'
  271.                          WHERE cat_blog_ID = '.$src_collection_ID /* extra security */ .'
  272.                               AND cat_ID IN ('.implode','$chapters_to_move ).')';
  273.         $DB->query$sql );
  274.  
  275.         $DB->commit();
  276.  
  277.         // Now the cache is badly screwed. Reseting it is fair enough, because this won't happen very often.
  278.         $this->clear();
  279.     }
  280.  
  281.  
  282.     /**
  283.      * Support function for move_Chapter_subtree
  284.      *
  285.      * @param Chapter 
  286.      * @param array 
  287.      */
  288.     function recurse_move_subtree$Chapter$list_array )
  289.     {
  290.         // Add this to the list:
  291.         $list_array[$Chapter->ID;
  292.  
  293.         foreach$Chapter->children as $child_Chapter )
  294.         {
  295.             $this->recurse_move_subtree$child_Chapter$list_array );
  296.         }
  297.     }
  298.  
  299.  
  300.     /**
  301.      * Instanciate a new object within this cache
  302.       *
  303.       * @param object|NULL
  304.       * @param integer|NULLsubset to use for new object
  305.      */
  306.     function new_obj$row NULL$subset_ID NULL )
  307.     {
  308.         // Instantiate a custom object
  309.         $Chapter new Chapter$row$subset_ID )// Copy
  310.  
  311.         return $Chapter;
  312.     }
  313. }
  314.  
  315. ?>