Beispiel #1
0
 /**
  * Format a datetime string to UTC/GMT according to given format.
  *
  * @param string $datetime String representation of a datetime object.
  * @param string $format   Format of the returned date.
  *
  * @return string
  */
 public static function formatGmt($datetime, $format = 'Y-m-d H:i:s')
 {
     /* The given datetime string is using the currently set timezone. */
     $datetime = new \DateTime($datetime);
     /* Format datetime string to UTC/GMT. */
     return gmdate($format, $datetime->format('U'));
 }
Beispiel #2
0
 /**
  * Login verification gate.
  *
  * @param Request $request Current Router request.
  *
  * @return void
  */
 protected function checkLogged(Request $request)
 {
     if (1 === Core\Session()->get('cms_user_logged')) {
         $this->user = unserialize(rawurldecode(Core\Session()->get('cms_user_info')));
         Core\Registry()->set('current_cms_user', $this->user);
         Core\Helpers\DateTime::setEnvironmentTimezone($this->user->timezone);
     } else {
         $request->redirectTo(array('controller' => 'authentication', 'action' => 'login', 'redirect' => $request->meta('REQUEST_URI')));
     }
 }
 /**
  * Calculates timezone offset which needs to be removed.
  *
  * @param Base\Model $resource Currently processed resource.
  *
  * @static
  * @access private
  *
  * @return void
  */
 private static function removeTimezoneEffect(Base\Model $resource)
 {
     foreach ($resource::timezoneAwareFields() as $datetime_field) {
         if ($resource->{$datetime_field}) {
             $resource->{$datetime_field} = Helpers\DateTime::formatGmt($resource->{$datetime_field});
         }
     }
 }
Beispiel #4
0
 /**
  * Assigns filtering of the results.
  *
  * @param DB\Query $query  Current query object instance of BaseModel or its children.
  * @param array    $params Query params to format filtering criteria.
  *
  * @access private
  * @static
  * @uses   Core\DB()
  * @uses   Core\DbCache()
  * @uses   Core\Helpers\SQL
  *
  * @return Core\Modules\DB\Query
  */
 private static function assignFilter(DB\Query $query, array $params)
 {
     if (isset($params['filtering']) && !empty($params['filtering']) && is_array($params['filtering'])) {
         $model_fields = $query->getObject()->getSchema();
         if ($query->getObject()->hasAndBelongsToMany) {
             $model_fields = array_merge($model_fields, $query->getObject()->hasAndBelongsToMany);
         }
         foreach ($params['filtering'] as $field => $value) {
             if ($value && isset($model_fields[$field])) {
                 if (is_array($value)) {
                     if (isset($value['start'], $value['end']) && !empty($value['start']) && !empty($value['end'])) {
                         if (in_array($model_fields[$field]['type'], array('date', 'datetime'), true)) {
                             $decorator = 'Core\\Modules\\DB\\Decorators\\Interfaces\\TimezoneAwareness';
                             if (is_subclass_of($query->getObject(), $decorator)) {
                                 $value['start'] = Core\Helpers\DateTime::formatGmt($value['start'] . date(' H:i:s'), 'Y-m-d');
                                 $value['end'] = Core\Helpers\DateTime::formatGmt($value['end'] . date(' H:i:s'), 'Y-m-d');
                             }
                             $query = $query->where("(DATE({$field}) BETWEEN " . Core\DB()->escapeString($value['start']) . ' AND ' . Core\DB()->escapeString($value['end']) . ')');
                         } else {
                             $query = $query->where("{$field} BETWEEN " . Core\DB()->escapeString($value['start']) . ' AND ' . Core\DB()->escapeString($value['end']));
                         }
                     } else {
                         if (isset($query->getObject()->hasAndBelongsToMany[$field]) && $query->getObject()->hasAndBelongsToMany[$field]) {
                             $related = $query->getObject()->hasAndBelongsToMany[$field];
                             $obj = $query->getObject();
                             $primaryKey = $obj->primaryKeyField();
                             $prefix = Core\Config()->DB['tables_prefix'];
                             foreach ($value as $v) {
                                 $query = $query->join("{$related['table']} as {$related['table']}{$v}", "{$prefix}{$obj::$tableName}.{$primaryKey} = " . "{$related['table']}{$v}.{$related['key']}" . ' AND ' . "{$related['table']}{$v}.{$related['relative_key']} = {$v}");
                             }
                         }
                     }
                 } elseif (strlen($value)) {
                     if ($model_fields[$field]['type'] === 'string') {
                         $value_to_match = trim(Core\DB()->escapeString($value), "'");
                         $query = $query->where("{$field} LIKE \"%{$value_to_match}%\"");
                     } else {
                         $query = $query->where($field . ' = ' . Core\DB()->escapeString($value));
                     }
                 }
             }
         }
     }
     return $query;
 }