Esempio n. 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 The name of the field in the database (used by atkExpressionAttribute)
  *
  * @return string The searchcondition to use.
  */
 public function getSearchCondition(Query $query, $table, $value, $searchmode, $fieldname = '')
 {
     $db = $this->getDb();
     $searchcondition = '';
     // If we search through datagrid we got no from/to values
     // Therefore we will simulate them
     if (!is_array($value)) {
         // exact: ex. "d/m/yyyy", "d/m/yy", "d/m" (use current year), "m/yyyy" (from 1 to number of days in month), "yyyy" (from 1/1 to 31/12)
         // between: two values divided by "-"
         // >=: one value followed by "-" // TODO using ">" and ">="
         // <=: one value preceded by "-" // TODO using "<" and "<="
         $value = trim($value);
         if (strpos($value, '-') !== false) {
             list($from, $to) = explode('-', $value);
             $value = array('from' => trim($from), 'to' => trim($to));
         } elseif (strlen($value) == 4 && is_numeric($value)) {
             $value = array('from' => "{$value}-01-01", 'to' => "{$value}-12-31");
         } elseif (!is_numeric($value) && substr_count($value, '/') == 1 && (strlen($value) == 6 || strlen($value) == 7)) {
             $value = explode('/', $value);
             // if we always set the day to 31, the framework somewhere modifies the query for months with less than 31 days
             // eg. '2015-09-31' becomes '2015-10-01'
             $daysInMonth = self::daysInMonth($value[0], $value[1]);
             $value = $value[1] . '-' . $value[0];
             $value = array('from' => "{$value}-01", 'to' => "{$value}-{$daysInMonth}");
         } else {
             $value = array('from' => $value, 'to' => $value);
         }
         if (substr_count($value['from'], '/') == 1) {
             $value['from'] .= '/' . date('Y');
         }
         if (substr_count($value['to'], '/') == 1) {
             $value['to'] .= '/' . date('Y');
         }
     }
     $valueFrom = $this->fetchValue(array($this->fieldName() => $value['from']));
     $valueTo = $this->fetchValue(array($this->fieldName() => $value['to']));
     $fromval = $this->value2db(array($this->fieldName() => $valueFrom));
     $toval = $this->value2db(array($this->fieldName() => $valueTo));
     $fieldname = $db->func_datetochar($fieldname ? $fieldname : $table . '.' . $this->fieldName());
     if ($fromval == null && $toval == null) {
     } else {
         if ($fromval != null && $toval != null) {
             if ($fromval > $toval) {
                 // User entered dates in wrong order. Let's put them in the right order.
                 $tmp = $fromval;
                 $fromval = $toval;
                 $toval = $tmp;
             }
             $searchcondition = $query->betweenCondition($fieldname, $fromval, $toval);
         } else {
             if ($fromval != null && $toval == null) {
                 $searchcondition = $query->greaterthanequalCondition($fieldname, $fromval);
             } else {
                 if ($fromval == null && $toval != null) {
                     $searchcondition = $query->lessthanequalCondition($fieldname, $toval);
                 } else {
                     if (is_array($value['from']) or is_array($value['to'])) {
                         $searchcondition = $this->_getDateArraySearchCondition($query, $table, $value);
                     } else {
                         // plain text search condition
                         $value = $this->_autoCompleteDateString($value);
                         $searchcondition = $query->exactCondition($fieldname, $value);
                     }
                 }
             }
         }
     }
     return $searchcondition;
 }
Esempio n. 2
0
 /**
  * Get the between search condition.
  *
  * @param Query $query The query object where the search condition should be placed on
  * @param string $fieldname The name of the field in the database
  * @param array $value The processed search value
  *
  * @return string query where clause for searching
  */
 public function getBetweenCondition($query, $fieldname, $value)
 {
     if ($value['from'] != '' && $value['to'] != '') {
         if ($value['from'] > $value['to']) {
             // User entered fields in wrong order. Let's fix that.
             $tmp = $value['from'];
             $value['from'] = $value['to'];
             $value['to'] = $tmp;
         }
         return $query->betweenCondition($fieldname, $this->escapeSQL($value['from']), $this->escapeSQL($value['to']));
     } elseif ($value['from'] != '' && $value['to'] == '') {
         return $query->greaterthanequalCondition($fieldname, $value['from']);
     } elseif ($value['from'] == '' && $value['to'] != '') {
         return $query->lessthanequalCondition($fieldname, $value['to']);
     }
     return '';
 }