Exemplo n.º 1
0
 /**
  * Write method - add the new message
  *
  * @package     las
  * @version     1.0
  *
  * @return mixed
  */
 public function add()
 {
     $validation = new Extension\Validation();
     $validation->add('title', new Validator\StringLength(['min' => 3, 'max' => 64, 'allowEmpty' => true]));
     $validation->add('client', new Validator\InclusionIn(['domain' => array_merge([0], Arr::from_model($this->clients, null, 'id'))]));
     $validation->add('content', new Validator\PresenceOf());
     $validation->add('status', new Validator\InclusionIn(['domain' => Messages::status()]));
     $validation->setLabels(['title' => __('Title'), 'client' => __('Client'), 'content' => __('Content'), 'status' => __('Status')]);
     $messages = $validation->validate($_POST);
     // Return messages if validation not pass
     if (count($messages)) {
         return $validation->getMessages();
     } else {
         if ($this->request->getPost('client', 'int') == 0) {
             // Send message to all clients
             foreach ($this->clients as $client) {
                 $message = new Messages();
                 $message->title = $this->request->getPost('title', 'string');
                 $message->client_id = $client->id;
                 $message->content = $this->request->getPost('content', 'string');
                 $message->status = $this->request->getPost('status', 'int');
                 $message->date = date('Y-m-d H:i:s');
                 // Try to write the records
                 if ($message->create() === true) {
                     continue;
                 } else {
                     \Las\Bootstrap::log($this->getMessages());
                     return $this->getMessages();
                 }
             }
             return true;
         } else {
             $this->title = $this->request->getPost('title', 'string');
             $this->client_id = $this->request->getPost('client', 'int');
             $this->content = $this->request->getPost('content', 'string');
             $this->status = $this->request->getPost('status', 'int');
             $this->date = date('Y-m-d H:i:s');
             // Try to write the record
             if ($this->create() === true) {
                 return $this;
             } else {
                 \Las\Bootstrap::log($this->getMessages());
                 return $this->getMessages();
             }
         }
     }
 }