/**
  * fix data matche collumn data defined
  * @param $data
  * @param $config
  * @return bool|float|int|null|string|Expression
  */
 public function fixData($data, $config)
 {
     if ($data instanceof Expression) {
         return $data;
     }
     $type = $config['type'];
     if (null !== $data) {
         switch ($type) {
             case 'integer':
                 return (int) $data;
             case 'float':
             case 'number':
             case 'decimal':
                 return (double) $data;
             case 'double':
                 return (double) $data;
             case 'time':
             case 'timestamp':
             case 'date':
             case 'datetime':
                 if ($data instanceof \DateTime) {
                     $data = new DateTime($data->format('Y-m-d H:i:s'));
                 }
                 if ($data instanceof DateTime) {
                     return $data;
                 }
                 return new DateTime($data);
             case 'blob':
             case 'string':
                 return (string) $data;
             case 'boolean':
             case 'bool':
                 return (bool) $data;
             case 'array':
                 if (is_scalar($data)) {
                     $data = json_decode($data);
                 }
                 return $data;
         }
     } else {
         if (!isset(self::$_validate[$config['name']]) || !isset(self::$_validate[$config['name']]['require'])) {
             if ('bool' == $type) {
                 return 0;
             }
             if ('array' == $type) {
                 return array();
             }
         } else {
             switch ($type) {
                 case 'integer':
                 case 'int':
                 case 'float':
                 case 'decimal':
                 case 'double':
                 case 'number':
                     return 0;
                 case 'timestamp':
                     return new DateTime('0000-00-00 00:00:00');
                 case 'time':
                     return new DateTime('0000-00-00 00:00:00');
                 case 'date':
                     return new DateTime('0000-00-00');
                 case 'datetime':
                     return new DateTime('0000-00-00 00:00:00');
                 case 'blob':
                 case 'string':
                     return '';
                 case 'boolean':
                 case 'bool':
                     return 0;
                 case 'array':
                     return array();
             }
         }
     }
     return null;
 }
Exemplo n.º 2
0
 public function executeProfiles()
 {
     $user = $this->getSessionUser();
     $this->setView('User/profiles');
     $error = [];
     $message = null;
     //JS Urls
     $this->document()->addJsVar('change_pass_url', $this->createUrl('user/change_pass'));
     if ($this->request()->isPostRequest()) {
         $data = $this->post('user', 'ARRAY', []);
         //for save
         unset($data['username']);
         unset($data['password']);
         unset($data['secret']);
         unset($data['register_time']);
         unset($data['modified_time']);
         $data['birthday'] = DateTime::createFromFormat('d/m/Y', $data['birthday']);
         $user->hydrate($data);
         if ($user->isColumnModified('email') && !\Users::checkAvailableEmail($user->getEmail())) {
             //he change his's email, need check it exists
             $error['email'] = t('Email was taken!');
         }
         if (empty($error)) {
             if ($user->save()) {
                 $message = t('Update your profiles success!');
             } else {
                 if (!$user->isValid()) {
                     foreach ($user->getValidationFailures() as $failure) {
                         $error[$failure->getColumn()] = $failure->getMessage();
                     }
                 } else {
                     //unknow error
                     $error['common'] = t('Error occur, plz try again. Thanks');
                 }
             }
         }
     }
     $this->view()->assign(['user' => $user, 'error' => $error, 'message' => $message]);
     return $this->renderComponent();
 }