Added debug message, commented one out
[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 {
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          * Protected constructor
36          *
37          * @param       $className      Name of the class
38          * @return      void
39          */
40         protected function __construct ($className) {
41                 // Call parent constructor
42                 parent::__construct($className);
43         }
44
45         /**
46          * Setter for wrapper class name
47          *
48          * @param       $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
49          * @return      void
50          */
51         public final function setWrapperConfigEntry ($wrapperConfigEntry) {
52                 $this->wrapperConfigEntry = (string) $wrapperConfigEntry;
53         }
54
55         /**
56          * Getter for wrapper class name
57          *
58          * @return      $wrapperConfigEntry             Configuration entry which hold the wrapper class' name
59          */
60         public final function getWrapperConfigEntry () {
61                 return $this->wrapperConfigEntry;
62         }
63
64         /**
65          * Getter for criteria array
66          *
67          * @return      $criteria
68          */
69         public final function getCriteriaArray () {
70                 return $this->criteria;
71         }
72
73         /**
74          * Add criteria, this method converts dashes to underscores because dashes
75          * are not valid for criteria keys.
76          *
77          * @param       $criteriaKey    Criteria key
78          * @param       $criteriaValue  Criteria value
79          * @return      void
80          */
81         public final function addCriteria ($criteriaKey, $criteriaValue) {
82                 // Debug message
83                 if (strpos($criteriaKey, 'my-') !== false) $this->debugBackTrace('criteriaKey=' . $criteriaKey);
84                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRITERIA: criteriaKey=' . $criteriaKey . ',criteriaValue=' . $criteriaValue);
85
86                 // Add it
87                 $this->criteria[$this->convertDashesToUnderscores($criteriaKey)] = (string)$criteriaValue;
88         }
89
90         /**
91          * Add configured criteria
92          *
93          * @param       $criteriaKey    Criteria key
94          * @param       $configEntry    Configuration entry
95          * @return      void
96          */
97         public final function addConfiguredCriteria ($criteriaKey, $configEntry) {
98                 // Add the configuration entry as a criteria
99                 $value = $this->getConfigInstance()->getConfigEntry($configEntry);
100                 $this->addCriteria($criteriaKey, $value);
101         }
102
103         /**
104          * Get criteria element or null if not found
105          *
106          * @param       $criteriaKey    The requested criteria key
107          * @return      $value                  Whether the value of the critera or null
108          */
109         public function getCriteriaElemnent ($criteriaKey) {
110                 // Convert dashes to underscore
111                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
112
113                 // Debug message
114                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('CRITERIA: criteriaKey=' . $criteriaKey . ',criteria()=' . count($this->criteria));
115
116                 // Default is not found
117                 $value = NULL;
118
119                 // Is the criteria there?
120                 if (isset($this->criteria[$criteriaKey])) {
121                         // Then use it
122                         $value = $this->criteria[$criteriaKey];
123                 } // END - if
124
125                 // Return the value
126                 return $value;
127         }
128
129         /**
130          * Checks whether given array entry matches
131          *
132          * @param       $entryArray             Array with the entries to find
133          * @return      $matches                Whether the entry matches or not
134          */
135         public function ifEntryMatches (array $entryArray) {
136                 // First nothing matches and nothing is counted
137                 $matches = false;
138                 $counted = 0;
139
140                 // Walk through all entries
141                 foreach ($entryArray as $key => $entry) {
142                         // Convert dashes to underscore
143                         $key = $this->convertDashesToUnderscores($key);
144
145                         // Then walk through all search criteria
146                         foreach ($this->criteria as $criteriaKey => $criteriaValue) {
147                                 // Convert dashes to underscore
148                                 $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
149
150                                 // Is the element found and does it match?
151                                 if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
152                                         // Then count this one up
153                                         $counted++;
154                                 } // END - if
155                         } // END - foreach
156                 } // END - foreach
157
158                 // Now check if expected criteria counts match
159                 $matches = ($counted == count($this->criteria));
160
161                 // Return the result
162                 return $matches;
163         }
164
165         /**
166          * "Getter" for a cache key
167          *
168          * @param       $onlyKeys       Only use these keys for a cache key
169          * @return      $cacheKey       The key suitable for the cache system
170          */
171         public function getCacheKey ($onlyKeys = array()) {
172                 // Initialize the key
173                 $cacheKey = '';
174
175                 // Now walk through all criterias
176                 foreach ($this->criteria as $criteriaKey => $criteriaValue) {
177                         // Convert dashes to underscore
178                         $criteriaKey = $this->convertDashesToUnderscores($criteriaKey);
179
180                         // Is the value in array or is $onlyKeys empty?
181                         if ((isset($onlyKeys[$criteriaKey])) || (count($onlyKeys) == 0)) {
182                                 // Add the value URL encoded to avoid any trouble with special characters
183                                 $cacheKey .= sprintf("%s=%s;",
184                                         $criteriaKey,
185                                         urlencode($criteriaValue)
186                                 );
187                         } // END - if
188                 } // END - foreach
189
190                 // Remove last semicolon
191                 $cacheKey = substr($cacheKey, 0, -1);
192
193                 // Is the instance SearchCriteria?
194                 if ($this instanceof SearchCriteria) {
195                         // Check if 'limit' and 'skip' are in
196                         if (((isset($onlyKeys['limit'])) && (isset($onlyKeys['skip']))) || (count($onlyKeys) == 0)) {
197                                 // Add limit and skip values
198                                 $cacheKey .= sprintf(";%%limit%%=%s;%%skip%%=%s",
199                                         $this->getLimit(),
200                                         $this->getSkip()
201                                 );
202                         } // END - if
203                 } // END - if
204
205                 // Return the cache key
206                 return $cacheKey;
207         }
208
209         /**
210          * Count the criteria, e.g. useful to find out if a database query has no limitation (search criteria)
211          *
212          * @return      $count  Count of all criteria entries
213          */
214         public final function count () {
215                 // Return it
216                 return count($this->criteria);
217         }
218 }
219
220 // [EOF]
221 ?>