Source for file tags.php

Documentation is available at tags.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Libraries
  4.  * @subpackage  Table
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Abstract class defining methods that can be
  14.  * implemented by an Observer class of a JTable class (which is an Observable).
  15.  * Attaches $this Observer to the $table in the constructor.
  16.  * The classes extending this class should not be instanciated directly, as they
  17.  * are automatically instanciated by the JObserverMapper
  18.  *
  19.  * @package     Joomla.Libraries
  20.  * @subpackage  Table
  21.  * @link        http://docs.joomla.org/JTableObserver
  22.  * @since       3.1.2
  23.  */
  24. {
  25.     /**
  26.      * Helper object for managing tags
  27.      *
  28.      * @var    JHelperTags 
  29.      * @since  3.1.2
  30.      */
  31.     protected $tagsHelper;
  32.  
  33.     /**
  34.      * The pattern for this table's TypeAlias
  35.      *
  36.      * @var    string 
  37.      * @since  3.1.2
  38.      */
  39.     protected $typeAliasPattern = null;
  40.  
  41.     /**
  42.      * Override for postStoreProcess param newTags, Set by setNewTags, used by onAfterStore and onBeforeStore
  43.      *
  44.      * @var    array 
  45.      * @since  3.1.2
  46.      */
  47.     protected $newTags = false;
  48.  
  49.     /**
  50.      * Override for postStoreProcess param replaceTags. Set by setNewTags, used by onAfterStore
  51.      *
  52.      * @var    boolean 
  53.      * @since  3.1.2
  54.      */
  55.     protected $replaceTags = true;
  56.  
  57.     /**
  58.      * Not public, so marking private and deprecated, but needed internally in parseTypeAlias for
  59.      * PHP < 5.4.0 as it's not passing context $this to closure function.
  60.      *
  61.      * @var         JTableObserverTags 
  62.      * @since       3.1.2
  63.      * @deprecated  Never use this
  64.      * @private
  65.      */
  66.     public static $_myTableForPregreplaceOnly;
  67.  
  68.     /**
  69.      * Creates the associated observer instance and attaches it to the $observableObject
  70.      * Creates the associated tags helper class instance
  71.      * $typeAlias can be of the form "{variableName}.type", automatically replacing {variableName} with table-instance variables variableName
  72.      *
  73.      * @param   JObservableInterface  $observableObject  The subject object to be observed
  74.      * @param   array                 $params            ( 'typeAlias' => $typeAlias )
  75.      *
  76.      * @return  JTableObserverTags 
  77.      *
  78.      * @since   3.1.2
  79.      */
  80.     public static function createObserver(JObservableInterface $observableObject$params array())
  81.     {
  82.         $typeAlias $params['typeAlias'];
  83.  
  84.         $observer new self($observableObject);
  85.  
  86.         $observer->tagsHelper new JHelperTags;
  87.         $observer->typeAliasPattern $typeAlias;
  88.  
  89.         return $observer;
  90.     }
  91.  
  92.     /**
  93.      * Pre-processor for $table->store($updateNulls)
  94.      *
  95.      * @param   boolean  $updateNulls  The result of the load
  96.      * @param   string   $tableKey     The key of the table
  97.      *
  98.      * @return  void 
  99.      *
  100.      * @since   3.1.2
  101.      */
  102.     public function onBeforeStore($updateNulls$tableKey)
  103.     {
  104.         $this->parseTypeAlias();
  105.         if (empty($this->table->tagsHelper->tags))
  106.         {
  107.             $this->tagsHelper->preStoreProcess($this->table);
  108.         }
  109.         else
  110.         {
  111.             $this->tagsHelper->preStoreProcess($this->table(array) $this->table->tagsHelper->tags);
  112.         }
  113.     }
  114.  
  115.     /**
  116.      * Post-processor for $table->store($updateNulls)
  117.      * You can change optional params newTags and replaceTags of tagsHelper with method setNewTagsToAdd
  118.      *
  119.      * @param   boolean  &$result  The result of the load
  120.      *
  121.      * @return  void 
  122.      *
  123.      * @since   3.1.2
  124.      */
  125.     public function onAfterStore(&$result)
  126.     {
  127.         if ($result)
  128.         {
  129.             if (empty($this->table->tagsHelper->tags))
  130.             {
  131.                 $result $this->tagsHelper->postStoreProcess($this->table);
  132.             }
  133.             else
  134.             {
  135.                 $result $this->tagsHelper->postStoreProcess($this->table$this->table->tagsHelper->tags);
  136.             }
  137.             // Restore default values for the optional params:
  138.             $this->newTags = array();
  139.             $this->replaceTags = true;
  140.         }
  141.     }
  142.  
  143.     /**
  144.      * Pre-processor for $table->delete($pk)
  145.      *
  146.      * @param   mixed  $pk  An optional primary key value to delete.  If not set the instance property value is used.
  147.      *
  148.      * @return  void 
  149.      *
  150.      * @since   3.1.2
  151.      * @throws  UnexpectedValueException
  152.      */
  153.     public function onBeforeDelete($pk)
  154.     {
  155.         $this->parseTypeAlias();
  156.         $this->tagsHelper->deleteTagData($this->table$pk);
  157.     }
  158.  
  159.     /**
  160.      * Sets the new tags to be added or to replace existing tags
  161.      *
  162.      * @param   array    $newTags      New tags to be added to or replace current tags for an item
  163.      * @param   boolean  $replaceTags  Replace tags (true) or add them (false)
  164.      *
  165.      * @return  boolean 
  166.      *
  167.      * @since   3.1.2
  168.      */
  169.     public function setNewTags($newTags$replaceTags)
  170.     {
  171.         $this->parseTypeAlias();
  172.  
  173.         return $this->tagsHelper->postStoreProcess($this->table$newTags$replaceTags);
  174.     }
  175.  
  176.     /**
  177.      * Internal method
  178.      * Parses a TypeAlias of the form "{variableName}.type", replacing {variableName} with table-instance variables variableName
  179.      * Storing result into $this->tagsHelper->typeAlias
  180.      *
  181.      * @return  void 
  182.      *
  183.      * @since   3.1.2
  184.      */
  185.     protected function parseTypeAlias()
  186.     {
  187.         // Needed for PHP < 5.4.0 as it's not passing context $this to closure function
  188.         static::$_myTableForPregreplaceOnly $this->table;
  189.  
  190.         $this->tagsHelper->typeAlias preg_replace_callback('/{([^}]+)}/',
  191.             function($matches)
  192.             {
  193.                 return JTableObserverTags::$_myTableForPregreplaceOnly->{$matches[1]};
  194.             },
  195.             $this->typeAliasPattern
  196.         );
  197.     }
  198. }

Documentation generated on Tue, 19 Nov 2013 15:15:17 +0100 by phpDocumentor 1.4.3