Added methods for 'choice' and 'exclude' criteria
[core.git] / inc / classes / main / criteria / class_BaseCriteria.php
1 <?php
2 /**
3  * A general crtieria class
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.org
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24 class BaseCriteria extends BaseFrameworkSystem implements Criteria {
25         /**
26          * Wrapper class name stored in config entry
27          */
28         private $wrapperConfigEntry = '';
29
30         /**
31          * Criteria to handle
32          */
33         private $criteria = array();
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44         }
45
46         /**
47          * Setter for wrapper class name
48          *
49          * @param       $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
50          * @return      void
51          */
52         public final function setWrapperConfigEntry ($wrapperConfigEntry) {
53                 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
54         }
55
56         /**
57          * Getter for wrapper class name
58          *
59          * @return      $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
60          */
61         public final function getWrapperConfigEntry () {
62                 return $this->wrapperConfigEntry;
63         }
64
65         /**
66          * Getter for criteria array
67          *
68          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
69          * @return      $criteria
70          */
71         public final function getCriteriaArray ($criteriaType = 'default') {
72                 return $this->criteria[$criteriaType];
73         }
74
75         /**
76          * Getter for criteria array 'choice' type
77          *
78          * @return      $criteria
79          */
80         public final function getCriteriaChoiceArray () {
81                 return $this->getCriteriaArray('choice');
82         }
83
84         /**
85          * Getter for criteria array 'exclude' type
86          *
87          * @return      $criteria
88          */
89         public final function getCriteriaExcludeArray () {
90                 return $this->getCriteriaArray('exclude');
91         }
92
93         /**
94          * Add criteria, this method converts dashes to underscores because dashes
95          * are not valid for criteria keys.
96          *
97          * @param       $criteriaKey    Criteria key
98          * @param       $criteriaValue  Criteria value
99          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
100          * @return      void
101          */
102         public final function addCriteria ($criteriaKey, $criteriaValue, $criteriaType = 'default') {
103                 // Debug message
104                 if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
105                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput(strtoupper($criteriaType) . '-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
106
107                 // Convert dashes to underscore
108                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
109
110                 // Is it already there?
111                 if (isset($this->criteria[$criteriaType][$criteriaKey])) {
112                         // Append it
113                         $this->criteria[$criteriaType][$criteriaKey] .= ',' . $criteriaValue;
114                 } else {
115                         // Add it
116                         $this->criteria[$criteriaType][$criteriaKey] = (string) $criteriaValue;
117                 }
118         }
119
120         /**
121          * Add "choice" criteria, this method converts dashes to underscores because
122          * dashes are not valid for criteria keys.
123          *
124          * @param       $criteriaKey    Criteria key
125          * @param       $criteriaValue  Criteria value
126          * @return      void
127          */
128         public final function addChoiceCriteria ($criteriaKey, $criteriaValue) {
129                 // Debug message
130                 if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
131                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CHOICE-CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
132
133                 // Add it
134                 $this->criteria['choice'][$this->convertDashesToUnderscores($criteriaKey)][] = (string) $criteriaValue;
135         }
136
137         /**
138          * Add "exclude" criteria, this method converts dashes to underscores because
139          * dashes are not valid for criteria keys.
140          *
141          * @param       $criteriaKey    Criteria key
142          * @param       $criteriaValue  Criteria value
143          * @return      void
144          */
145         public final function addExcludeCriteria ($criteriaKey, $criteriaValue) {
146                 // Add it with generic method
147                 $this->addCriteria($criteriaKey, $criteriaValue, 'exclude');
148         }
149
150         /**
151          * Add configured criteria
152          *
153          * @param       $criteriaKey    Criteria key
154          * @param       $configEntry    Configuration entry
155          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
156          * @return      void
157          */
158         public final function addConfiguredCriteria ($criteriaKey, $configEntry, $criteriaType = 'default') {
159                 // Add the configuration entry as a criteria
160                 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
161                 $this->addCriteria($criteriaKey, $value, $criteriaType);
162         }
163
164         /**
165          * Get criteria element or null if not found
166          *
167          * @param       $criteriaKey    The requested criteria key
168          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
169          * @return      $value                  Whether the value of the critera or null
170          */
171         public function getCriteriaElemnent ($criteriaKey, $criteriaType = 'default') {
172                 // Convert dashes to underscore
173                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
174
175                 // Debug message
176                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria[$criteriaType]));
177
178                 // Default is not found
179                 $value = NULL;
180
181                 // Is the criteria there?
182                 if (isset($this->criteria[$criteriaType][$criteriaKey])) {
183                         // Then use it
184                         $value = $this->criteria[$criteriaType][$criteriaKey];
185                 } // END - if
186
187                 // Return the value
188                 return $value;
189         }
190
191         /**
192          * Checks whether given array entry matches
193          *
194          * @param       $entryArray             Array with the entries to find
195          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
196          * @return      $matches                Whether the entry matches or not
197          */
198         public function ifEntryMatches (array $entryArray, $criteriaType = 'default') {
199                 // First nothing matches and nothing is counted
200                 $matches = false;
201                 $counted = 0;
202
203                 // Walk through all entries
204                 foreach ($entryArray as $key => $entry) {
205                         // Convert dashes to underscore
206                         $key = $this->convertDashesToUnderscores($key);
207
208                         // Then walk through all search criteria
209                         foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
210                                 // Convert dashes to underscore
211                                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
212
213                                 // Is the element found and does it match?
214                                 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
215                                         // Then count this one up
216                                         $counted++;
217                                 } // END - if
218                         } // END - foreach
219                 } // END - foreach
220
221                 // Now check if expected criteria counts match
222                 $matches = ($counted == count($this->criteria[$criteriaType]));
223
224                 // Return the result
225                 return $matches;
226         }
227
228         /**
229          * Checks whether given array 'choice' entry matches
230          *
231          * @param       $entryArray             Array with the entries to find
232          * @return      $matches                Whether the entry matches or not
233          */
234         public function ifChoiceMatches (array $entryArray) {
235                 // Call inner method
236                 return $this->ifEntryMatches($entryArray, 'choice');
237         }
238
239         /**
240          * Checks whether given array 'exclude' entry matches
241          *
242          * @param       $entryArray             Array with the entries to find
243          * @return      $matches                Whether the entry matches or not
244          */
245         public function ifExcludeMatches (array $entryArray) {
246                 // Call inner method
247                 return $this->ifEntryMatches($entryArray, 'exclude');
248         }
249
250         /**
251          * "Getter" for a cache key
252          *
253          * @param       $onlyKeys       Only use these keys for a cache key
254          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
255          * @return      $cacheKey       The key suitable for the cache system
256          */
257         public function getCacheKey ($onlyKeys = array(), $criteriaType = 'default') {
258                 // Initialize the key
259                 $cacheKey = '';
260
261                 // Now walk through all criterias
262                 foreach ($this->criteria[$criteriaType] as $criteriaKey => $criteriaValue) {
263                         // Convert dashes to underscore
264                         $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
265
266                         // Is the value in array or is $onlyKeys empty?
267                         if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
268                                 // Add the value URL encoded to avoid any trouble with special characters
269                                 $cacheKey .= sprintf("%s=%s;",
270                                         $criteriaKey,
271                                         urlencode($criteriaValue)
272                                 );
273                         } // END - if
274                 } // END - foreach
275
276                 // Remove last semicolon
277                 $cacheKey = substr($cacheKey, 0, -1);
278
279                 // Is the instance SearchCriteria?
280                 if ($this instanceof SearchCriteria) {
281                         // Check if 'limit' and 'skip' are in
282                         if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
283                                 // Add limit and skip values
284                                 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
285                                         $this->getLimit(),
286                                         $this->getSkip()
287                                 );
288                         } // END - if
289                 } // END - if
290
291                 // Return the cache key
292                 return $cacheKey;
293         }
294
295         /**
296          * "Getter" for a cache key ('choice' type)
297          *
298          * @param       $onlyKeys       Only use these keys for a cache key
299          * @return      $cacheKey       The key suitable for the cache system
300          */
301         public function getCacheKeyChoice ($onlyKeys = array()) {
302                 // Call inner method
303                 return $this->getCacheKey($onlyKeys, 'choice');
304         }
305
306         /**
307          * "Getter" for a cache key ('exclude' type)
308          *
309          * @param       $onlyKeys       Only use these keys for a cache key
310          * @return      $cacheKey       The key suitable for the cache system
311          */
312         public function getCacheKeyExclude ($onlyKeys = array()) {
313                 // Call inner method
314                 return $this->getCacheKey($onlyKeys, 'exclude');
315         }
316
317         /**
318          * "Getter" for a cache key ('choice' type)
319          *
320          * @param       $onlyKeys       Only use these keys for a cache key
321          * @return      $cacheKey       The key suitable for the cache system
322          */
323         public function getCacheKeyChoice ($onlyKeys = array()) {
324                 // Call inner method
325                 return $this->getCacheKey($onlyKeys, 'choice');
326         }
327
328         /**
329          * Count the criteria, e.g. useful to find out if a database query has no
330          * limitation (search criteria).
331          *
332          * @param       $criteriaType   Type of this criteria, can be one of 'default' (default), 'choice' or 'exclude'
333          * @return      $count  Count of all criteria entries
334          */
335         public final function count ($criteriaType = 'default') {
336                 // Return it
337                 return count($this->criteria[$criteriaType]);
338         }
339
340         /**
341          * Count 'choice' criteria, e.g. useful to find out if a database query
342          * has no limitation (search criteria).
343          *
344          * @return      $count  Count of all criteria entries
345          */
346         public final function countChoice () {
347                 return $this->count('choice');
348         }
349
350         /**
351          * Count 'exclude' criteria, e.g. useful to find out if a database query
352          * has no limitation (search criteria).
353          *
354          * @return      $count  Count of all criteria entries
355          */
356         public final function countExclude () {
357                 return $this->count('exclude');
358         }
359 }
360
361 // [EOF]
362 ?>