Removed die()
[core.git] / inc / classes / main / criteria / search / class_SearchCriteria.php
index 91baad35ae52214a98457aa51ef79fd815098df2..ded04793312a5f9c37b4f40e8d06c10f0b88d5bd 100644 (file)
@@ -6,7 +6,7 @@
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -27,7 +27,7 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
        /**
         * Criteria to handle
         */
-       private $searchCriteria = array();
+       private $criteria = array();
 
        /**
         * Limitation for the search
@@ -54,7 +54,7 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
         *
         * @return      $criteriaInstance       An instance of this criteria
         */
-       public final static function createSearchCriteria () {
+       public static final function createSearchCriteria () {
                // Get a new instance
                $criteriaInstance = new SearchCriteria();
 
@@ -63,38 +63,26 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
        }
 
        /**
-        * Add criteria
-        *
-        * @param       $criteriaKey    Criteria key
-        * @param       $criteriaValue  Criteria value
-        * @return      void
-        */
-       public final function addCriteria ($criteriaKey, $criteriaValue) {
-               $this->searchCriteria[(string)$criteriaKey] = (string)$criteriaValue;
-       }
-
-       /**
-        * Add configured criteria
+        * Setter for limit
         *
-        * @param       $criteriaKey    Criteria key
-        * @param       $configEntry    Configuration entry
+        * @param       $limit  Search limit
         * @return      void
+        * @todo        Find a nice casting here. (int) allows until and including 32766.
         */
-       public final function addConfiguredCriteria ($criteriaKey, $configEntry) {
-               // Add the configuration entry as a criteria
-               $value = $this->getConfigInstance()->readConfig($configEntry);
-               $this->addCriteria($criteriaKey, $value);
+       public final function setLimit ($limit) {
+               $this->limit = $limit;
        }
 
        /**
-        * Setter for limit
+        * "Setter" for limit from a configuration entry
         *
-        * @param       $limit  Search limit
+        * @param       $configEntry    The configuration entry which hold a number as limit
         * @return      void
-        * @todo        Find a nice casting here. (int) allows until and including 32766.
         */
-       public final function setLimit ($limit) {
-               $this->limit = $limit;
+       public final function setConfiguredLimit ($configEntry) {
+               // Get the limit from config entry and set it
+               $limit = $this->getConfigInstance()->getConfigEntry($configEntry);
+               $this->setLimit($limit);
        }
 
        /**
@@ -127,81 +115,89 @@ class SearchCriteria extends BaseCriteria implements LocalSearchCriteria {
        }
 
        /**
-        * "Getter" for a cache key
+        * Checks whether the given key/value pair is matching with 'default' and one of 'choice' and
+        * never with in 'exclude'.
         *
-        * @return      $cacheKey       The key suitable for the cache system
+        * @param       $key                    Key element to check
+        * @param       $value                  Value to check
+        * @param       $separator              Separator for "exploding" $value (default: ',')
+        * @return      $isMatching             Whether the key/value is matching or excluded
         */
-       public function getCacheKey () {
-               // Initialize the key
-               $cacheKey = "";
-
-               // Now walk through all criterias
-               foreach ($this->searchCriteria as $criteriaKey => $criteriaValue) {
-                       // Add the value URL encoded to avoid any trouble with special characters
-                       $cacheKey .= sprintf("%s=%s;",
-                               $criteriaKey,
-                               urlencode($criteriaValue)
-                       );
-               }
+       public function isCriteriaMatching ($key, $value, $separator = ',') {
+               // $value cannot be array
+               assert(!is_array($value));
 
-               // Add limit and skip values
-               $cacheKey .= sprintf("%%limit%%=%s;%%skip%%=%s",
-                       $this->limit,
-                       $this->skip
-               );
+               // "Explode" value
+               $valueArray = explode($separator, $value);
 
-               // Return the cache key
-               return $cacheKey;
-       }
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value=' . $value . ' - ENTERED!');
 
-       /**
-        * Get criteria element or null if not found
-        *
-        * @param       $criteria       The criteria we want to have
-        * @return      $value          Wether the value of the critera or null
-        */
-       public function getCriteriaElemnent ($criteria) {
-               // Default is not found
-               $value = null;
-
-               // Is the criteria there?
-               if (isset($this->searchCriteria[$criteria])) {
-                       // Then use it
-                       $value = $this->searchCriteria[$criteria];
-               }
+               // Get 'default' search value
+               $searchDefault = $this->getCriteriaElemnent($key);
 
-               // Return the value
-               return $value;
-       }
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault);
 
-       /**
-        * Checks wether given array entry matches
-        *
-        * @param       $entryArray             Array with the entries to find
-        * @return      $matches                Wether the entry matches or not
-        */
-       public function ifEntryMatches (array $entryArray) {
-               // First nothing matches and nothing is counted
-               $matches = false;
-               $counted = 0;
-
-               // Walk through all entries
-               foreach ($entryArray as $key => $entry) {
-                       // Then walk through all search criteria
-                       foreach ($this->searchCriteria as $criteriaKey => $criteriaValue) {
-                               // Is the element found and does it match?
-                               if (($key == $criteriaKey) && ($criteriaValue == $entry)) {
-                                       // Then count this one up
-                                       $counted++;
-                               } // END - if
+               // 'default' check
+               $isMatching = ((!is_null($searchDefault)) && ($searchDefault == $value));
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaElement(' . $key . ')[' . gettype($searchDefault) . ']=' . $searchDefault . ',isMatching=' . intval($isMatching));
+
+               // Get 'choice' search value (can be NULL or $separator-separated string)
+               $searchChoice = $this->getCriteriaChoiceElemnent($key);
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[' . gettype($searchChoice) . ']=' . print_r($searchChoice, TRUE));
+
+               // 'choice' check
+               if ((is_array($searchChoice)) && (count($valueArray) == 1)) {
+                       // $value is a single-search value, so use in_array()
+                       $isMatching = (((is_null($searchDefault)) || ($isMatching === TRUE)) && ((is_null($searchChoice)) || ((is_array($searchChoice)) && (in_array($value, $searchChoice)))));
+
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - SINGLE-MATCH');
+               } elseif ((is_array($searchChoice)) && (count($valueArray) > 1)) {
+                       // $value is choice-search value, so check all entries
+                       $isMatching = ((is_null($searchDefault)) || ($isMatching === TRUE));
+                       $idx = 0;
+                       foreach ($valueArray as $idx => $match) {
+                               // Debug message
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: match=' . $match . ',count(searchChoice)=' . count($searchChoice));
+
+                               // Is it found? (one is okay)
+                               $isMatching = (($isMatching === TRUE) && (in_array($match, $searchChoice)));
+
+                               // Debug message
+                               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: match=' . $match . ',isMatching=' . intval($isMatching));
                        } // END - foreach
-               } // END - foreach
 
-               // Now check if expected criteria counts match
-               $matches = ($counted == count($this->searchCriteria));
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',valueArray()=' . count($valueArray) . ',idx=' . $idx . ',isMatching=' . intval($isMatching) . ' - CHOICE-MATCH');
+               } else {
+                       // Choice-match is NULL?
+                       $isMatching = (($isMatching === TRUE) || (is_null($searchChoice)));
+
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - NULL-MATCH');
+               }
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaChoiceElement(' . $key . ')[]=' . gettype($searchChoice) . ',isMatching=' . intval($isMatching));
+
+               // Get 'exclude' search value
+               $searchExclude = $this->getCriteriaExcludeElemnent($key);
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: getCriteriaExcludeElement(' . $key . ')[' . gettype($searchExclude) . ']=' . $searchExclude);
+
+               // 'exclude' check
+               $isMatching = (((is_null($searchChoice)) || ($isMatching === TRUE)) && ((is_null($searchExclude)) || ($searchExclude != $value)));
 
-               // Return the result
-               return $matches;
+               // Return result
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('SEARCH-CRITERIA[' . __LINE__ . ']: key=' . $key . ',value=' . $value . ',isMatching=' . intval($isMatching) . ' - EXIT!');
+               return $isMatching;
        }
 }