Example #1
0
 /**
  * Creates a searchcondition for the field,
  * was once part of searchCondition, however,
  * searchcondition() also immediately adds the search condition.
  *
  * @param Query $query The query object where the search condition should be placed on
  * @param string $table The name of the table in which this attribute
  *                           is stored
  * @param mixed $value The value the user has entered in the searchbox
  * @param string $searchmode The searchmode to use. This can be any one
  *                           of the supported modes, as returned by this
  *                           attribute's getSearchModes() method.
  * @param string $fieldname
  *
  * @return string The searchcondition to use.
  */
 public function getSearchCondition(Query $query, $table, $value, $searchmode, $fieldname = '')
 {
     // We only support 'exact' matches.
     // But you can select more than one value, which we search using the IN() statement,
     // which should work in any ansi compatible database.
     $searchcondition = '';
     if (is_array($value) && count($value) > 0 && $value[0] != '') {
         // This last condition is for when the user selected the 'search all' option, in which case, we don't add conditions at all.
         if (count($value) == 1 && $value[0] != '') {
             // exactly one value
             if ($value[0] == '__NONE__') {
                 return $query->nullCondition($table . '.' . $this->fieldName(), true);
             } else {
                 return $query->exactCondition($table . '.' . $this->fieldName(), $this->escapeSQL($value[0]));
             }
         } elseif (count($value) > 1) {
             // search for more values
             if (in_array('__NONE__', $value)) {
                 unset($value[array_search('__NONE__', $value)]);
                 return sprintf('(%s OR %s)', $query->nullCondition($table . '.' . $this->fieldName(), true), $table . '.' . $this->fieldName() . " IN ('" . implode("','", $value) . "')");
             } else {
                 return $table . '.' . $this->fieldName() . " IN ('" . implode("','", $value) . "')";
             }
         }
     }
     return $searchcondition;
 }