Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
tags.php
Aller à la documentation de ce fichier.
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  */
25 {
26  /**
27  * Helper object for managing tags
28  *
29  * @var JHelperTags
30  * @since 3.1.2
31  */
32  protected $tagsHelper;
33 
34  /**
35  * The pattern for this table's TypeAlias
36  *
37  * @var string
38  * @since 3.1.2
39  */
40  protected $typeAliasPattern = null;
41 
42  /**
43  * Override for postStoreProcess param newTags, Set by setNewTags, used by onAfterStore and onBeforeStore
44  *
45  * @var array
46  * @since 3.1.2
47  */
48  protected $newTags = false;
49 
50  /**
51  * Override for postStoreProcess param replaceTags. Set by setNewTags, used by onAfterStore
52  *
53  * @var boolean
54  * @since 3.1.2
55  */
56  protected $replaceTags = true;
57 
58  /**
59  * Not public, so marking private and deprecated, but needed internally in parseTypeAlias for
60  * PHP < 5.4.0 as it's not passing context $this to closure function.
61  *
62  * @var JTableObserverTags
63  * @since 3.1.2
64  * @deprecated Never use this
65  * @private
66  */
68 
69  /**
70  * Creates the associated observer instance and attaches it to the $observableObject
71  * Creates the associated tags helper class instance
72  * $typeAlias can be of the form "{variableName}.type", automatically replacing {variableName} with table-instance variables variableName
73  *
74  * @param JObservableInterface $observableObject The subject object to be observed
75  * @param array $params ( 'typeAlias' => $typeAlias )
76  *
77  * @return JTableObserverTags
78  *
79  * @since 3.1.2
80  */
81  public static function createObserver(JObservableInterface $observableObject, $params = array())
82  {
83  $typeAlias = $params['typeAlias'];
84 
85  $observer = new self($observableObject);
86 
87  $observer->tagsHelper = new JHelperTags;
88  $observer->typeAliasPattern = $typeAlias;
89 
90  return $observer;
91  }
92 
93  /**
94  * Pre-processor for $table->store($updateNulls)
95  *
96  * @param boolean $updateNulls The result of the load
97  * @param string $tableKey The key of the table
98  *
99  * @return void
100  *
101  * @since 3.1.2
102  */
103  public function onBeforeStore($updateNulls, $tableKey)
104  {
105  $this->parseTypeAlias();
106  if (empty($this->table->tagsHelper->tags))
107  {
108  $this->tagsHelper->preStoreProcess($this->table);
109  }
110  else
111  {
112  $this->tagsHelper->preStoreProcess($this->table, (array) $this->table->tagsHelper->tags);
113  }
114  }
115 
116  /**
117  * Post-processor for $table->store($updateNulls)
118  * You can change optional params newTags and replaceTags of tagsHelper with method setNewTagsToAdd
119  *
120  * @param boolean &$result The result of the load
121  *
122  * @return void
123  *
124  * @since 3.1.2
125  */
126  public function onAfterStore(&$result)
127  {
128  if ($result)
129  {
130  if (empty($this->table->tagsHelper->tags))
131  {
132  $result = $this->tagsHelper->postStoreProcess($this->table);
133  }
134  else
135  {
136  $result = $this->tagsHelper->postStoreProcess($this->table, $this->table->tagsHelper->tags);
137  }
138  // Restore default values for the optional params:
139  $this->newTags = array();
140  $this->replaceTags = true;
141  }
142  }
143 
144  /**
145  * Pre-processor for $table->delete($pk)
146  *
147  * @param mixed $pk An optional primary key value to delete. If not set the instance property value is used.
148  *
149  * @return void
150  *
151  * @since 3.1.2
152  * @throws UnexpectedValueException
153  */
154  public function onBeforeDelete($pk)
155  {
156  $this->parseTypeAlias();
157  $this->tagsHelper->deleteTagData($this->table, $pk);
158  }
159 
160  /**
161  * Sets the new tags to be added or to replace existing tags
162  *
163  * @param array $newTags New tags to be added to or replace current tags for an item
164  * @param boolean $replaceTags Replace tags (true) or add them (false)
165  *
166  * @return boolean
167  *
168  * @since 3.1.2
169  */
170  public function setNewTags($newTags, $replaceTags)
171  {
172  $this->parseTypeAlias();
173 
174  return $this->tagsHelper->postStoreProcess($this->table, $newTags, $replaceTags);
175  }
176 
177  /**
178  * Internal method
179  * Parses a TypeAlias of the form "{variableName}.type", replacing {variableName} with table-instance variables variableName
180  * Storing result into $this->tagsHelper->typeAlias
181  *
182  * @return void
183  *
184  * @since 3.1.2
185  */
186  protected function parseTypeAlias()
187  {
188  // Needed for PHP < 5.4.0 as it's not passing context $this to closure function
189  static::$_myTableForPregreplaceOnly = $this->table;
190 
191  $this->tagsHelper->typeAlias = preg_replace_callback('/{([^}]+)}/',
192  function($matches)
193  {
195  },
196  $this->typeAliasPattern
197  );
198  }
199 }