예제 #1
0
 /**
  * Default validation rules.
  *
  * @param Validator $validator The validator to customize.
  * @return Validator
  */
 public function validationDefault(Validator $validator)
 {
     return $validator->notEmpty('username', __d('wasabi_core', 'Please enter a username.'))->notEmpty('email', __d('wasabi_core', 'Please enter an email address.'))->add('email', ['email' => ['rule' => 'email', 'message' => __d('wasabi_core', 'Please enter a valid email address.')]])->notEmpty('group_id', __d('wasabi_core', 'Please select a group this user belongs to.'))->notEmpty('password', __d('wasabi_core', 'Please enter a password.'), 'create')->add('password', ['length' => ['rule' => ['minLength', 6], 'message' => __d('wasabi_core', 'Ensure your password consists of at least 6 characters.')]])->notEmpty('password_confirmation', __d('wasabi_core', 'Please repeat your Password.'), function ($context) {
         if ($context['newRecord'] === true) {
             return true;
         }
         if (isset($context['data']['password']) && !empty($context['data']['password'])) {
             return true;
         }
         return false;
     })->add('password_confirmation', 'equalsPassword', ['rule' => function ($passwordConfirmation, $provider) {
         if ($passwordConfirmation !== $provider['data']['password']) {
             return __d('wasabi_core', 'The Password Confirmation does not match the Password field.');
         }
         return true;
     }])->add('language_id', 'isValid', ['rule' => function ($languageId) {
         $languageIds = Hash::map(Configure::read('languages.backend'), '{n}', function ($language) {
             return $language->id;
         });
         if (!in_array($languageId, $languageIds)) {
             return __d('wasabi_core', 'Invalid language selected.');
         }
         return true;
     }])->add('timezone', 'isValid', ['rule' => function ($timezone) {
         if (!in_array($timezone, DateTimeZone::listIdentifiers())) {
             return __d('wasabi_core', 'Invalid timezone selected.');
         }
         return true;
     }]);
 }
예제 #2
0
 /**
  * Test map()
  *
  * @return void
  */
 public function testMap()
 {
     $data = static::articleData();
     $result = Hash::map($data, '{n}.Article.id', [$this, 'mapCallback']);
     $expected = [2, 4, 6, 8, 10];
     $this->assertEquals($expected, $result);
 }
예제 #3
0
 /**
  * Profile action
  * GET | PUT
  *
  * @return void
  */
 public function profile()
 {
     $user = $this->Users->get($this->Auth->user('id'));
     if ($this->request->is('put') && !empty($this->request->data)) {
         /** @var User $user */
         $user = $this->Users->patchEntity($user, $this->request->data);
         if ($this->Users->save($user)) {
             $this->request->session()->write('Auth.User.language_id', $user->language_id);
             $this->Flash->success(__d('wasabi_core', 'Your profile has been updated.'));
             $this->redirect(['action' => 'profile']);
             return;
         } else {
             $this->Flash->error($this->formErrorMessage);
         }
     }
     $this->set(['user' => $user, 'languages' => Hash::map(Configure::read('languages.backend'), '{n}', function ($language) {
         return ['value' => $language->id, 'text' => $language->name];
     })]);
 }
예제 #4
0
 /**
  * Transforma uma string em array com base no $separator.
  * @param  string $string A string a ser transformada em array.
  * @param  string $separator O separador para definir o que cortará a string e definir cada elemento do array.
  * @return  Array O array.
  */
 public function strToArray($string, $separator = ',')
 {
     $theArray = explode($separator, $string);
     $theArray = Hash::map($theArray, '{n}', 'trim');
     return $theArray;
 }
예제 #5
0
 /**
  * reorderPages action
  * AJAX POST
  */
 public function reorderPages()
 {
     if (!$this->request->isAll(['ajax', 'post'])) {
         throw new MethodNotAllowedException();
     }
     if (empty($this->request->data)) {
         throw new BadRequestException();
     }
     $this->request->data = Hash::map($this->request->data, '{n}', function ($page) {
         if ($page['parent_id'] === 'null') {
             $page['parent_id'] = null;
         }
         return $page;
     });
     $pages = $this->Pages->patchEntities($this->Pages->find('all'), $this->request->data, ['associated' => false]);
     /** @var Connection $connection */
     $connection = $this->Pages->connection();
     $connection->begin();
     foreach ($pages as $page) {
         $this->Pages->behaviors()->unload('Tree');
         if (!$this->Pages->save($page)) {
             $connection->rollback();
             break;
         }
     }
     if ($connection->inTransaction()) {
         $connection->commit();
         $status = 'success';
         $flashMessage = __d('wasabi_cms', 'The page positions have been updated.');
     } else {
         $status = 'error';
         $flashMessage = $this->dbErrorMessage;
     }
     $this->set(['status' => $status, 'flashMessage' => $flashMessage, '_serialize' => ['status', 'flashMessage']]);
     $this->RequestHandler->renderAs($this, 'json');
 }
예제 #6
0
 /**
  * reorderItems action
  * AJAX POST
  */
 public function reorderItems()
 {
     if (!$this->request->isAll(['ajax', 'post'])) {
         throw new MethodNotAllowedException();
     }
     if (empty($this->request->data)) {
         throw new BadRequestException();
     }
     $this->request->data = Hash::map($this->request->data, '{n}', function ($item) {
         if ($item['parent_id'] === 'null') {
             $item['parent_id'] = null;
         }
         return $item;
     });
     // save the new language positions
     $menuItems = $this->MenuItems->patchEntities($this->MenuItems->find('threaded'), $this->request->data);
     $this->MenuItems->connection()->begin();
     foreach ($menuItems as $menuItem) {
         $this->MenuItems->behaviors()->unload('Tree');
         if (!$this->MenuItems->save($menuItem)) {
             $this->MenuItems->connection()->rollback();
             break;
         }
     }
     if ($this->MenuItems->connection()->inTransaction()) {
         $this->MenuItems->connection()->commit();
         $status = 'success';
         $flashMessage = __d('wasabi_core', 'The menu item positions have been updated.');
     } else {
         $status = 'error';
         $flashMessage = $this->dbErrorMessage;
     }
     $this->set(['status' => $status, 'flashMessage' => $flashMessage, '_serialize' => ['status', 'flashMessage']]);
 }