Esempio n. 1
0
 /**
  * Build filters
  */
 protected function collection_filters()
 {
     /*
       'value' =>
        array (size=2)
          'from' => string '18.04.2014 03:06' (length=16)
          'to' => string '18.04.2014 03:06' (length=16)
      'operator' => string 'BETWEEN' (length=7)
      'connector' => string 'AND' (length=3)
      'raw' => null
     */
     //
     // Filters: Conditions
     //
     if (!empty($this->filters)) {
         foreach ($this->filters as $k => $v) {
             $k = $this->_sql_key($k);
             $sql = false;
             if ($k && array_key_exists($v['connector'], static::$connectors) && array_key_exists($v['operator'], static::$operators)) {
                 if ($v['raw']) {
                     // @todo RAW
                     $sql = strings::str_replace(array(':connector', ':key', ':operator', ':value'), array($v['connector'], $k, $v['operator'], $this->_normalize_value($k, $v['value'])), $v['raw']);
                 } else {
                     if (!isset(static::$operators[$v['operator']])) {
                         throw new collection_filter_exception(__METHOD__ . ' no operator ' . $v['operator']);
                     }
                     if (static::$operators[$v['operator']] instanceof Closure) {
                         $v['key'] = $k;
                         $sql = call_user_func(static::$operators[$v['operator']], $v);
                     } else {
                         if (!empty($v['value'])) {
                             $sql = $k . ' ' . $v['operator'] . " " . $this->_normalize_value($k, $v['value']);
                         }
                     }
                 }
                 if ($sql) {
                     $this->collection->append_where($sql, $v['connector']);
                 }
             }
         }
     }
     //
     // Sorting
     //
     if (!empty($this->orders)) {
         $orders = array();
         foreach ($this->orders as $k => $v) {
             $v = strtoupper($v);
             if ('ASC' !== $v && 'DESC' !== $v) {
                 throw new Collection_Filter_Exception('Bad order: ' . $k);
             }
             $orders[] = $this->_sql_key($k) . ' ' . $v;
         }
         $this->collection->set_order(trim(join(', ', $orders)));
     }
 }