/** * Make sure we have an valid author id set, or a guest id * * Validation callback. * * @param Validation $validation Validation object * @param string $field Field name * * @uses User::lookup_by_name * @uses DB::select * @uses DB::expr * @uses Validation::error */ public function valid_author(Validation $validation, $field) { if (!empty($this->author_name) and !($account = User::lookup_by_name($this->author_name))) { $validation->error('author', 'invalid', array($this->author_name)); } else { if (isset($account)) { $this->author = $account->id; } } if (empty($this->author)) { $validation->error($field, 'not_empty', array($validation[$field])); } elseif ($this->author == 1 and empty($this->guest_name)) { $validation->error('guest_name', 'not_empty', array($validation[$field])); } elseif ($this->author == 1 and !empty($this->guest_name)) { $result = DB::select(array(DB::expr('COUNT(*)'), 'total_count'))->from('users')->where('name', 'LIKE', $this->guest_name)->or_where('nick', 'LIKE', $this->guest_name)->execute($this->_db)->get('total_count'); if ($result > 0) { $validation->error($field, 'registered_user', array($validation[$field])); } } }
/** * Validation callback * * @param string $name Validation name * @param Validation $validation Validation object * @param string $field Field name * * @uses Valid::numeric * @uses Config::get */ public function is_valid($name, Validation $validation, $field) { // Make sure we have a valid term id set if ($name == 'category') { if (isset($this->categories) and is_array($this->categories)) { foreach ($this->categories as $id => $term) { if ($term == 'last' or !Valid::numeric($term)) { $validation->error('categories', 'invalid', array($validation[$field])); } } } } elseif ($name == 'created') { if (!empty($this->author_date) and !($date = strtotime($this->author_date))) { $validation->error($field, 'invalid', array($this->author_date)); } else { if (isset($date)) { $this->created = $date; } } } elseif ($name == 'author') { if (!empty($this->author_name) and !($account = User::lookup_by_name($this->author_name))) { $validation->error($field, 'invalid', array($this->author_name)); } else { if (isset($account)) { $this->author = $account->id; } } } elseif ($name == 'pubdate') { if (!empty($this->author_pubdate) and !($date = strtotime($this->author_pubdate))) { $validation->error($field, 'invalid', array($validation[$field])); } else { if (isset($date)) { $this->pubdate = $date; } } } elseif ($name == 'image') { if (isset($_FILES['image']['name']) and !empty($_FILES['image']['name'])) { $allowed_types = Config::get('media.supported_image_formats', array('jpg', 'png', 'gif')); $data = Validation::factory($_FILES)->rule('image', 'Upload::not_empty')->rule('image', 'Upload::valid')->rule('image', 'Upload::type', array(':value', $allowed_types)); if (!$data->check()) { $validation->error($field, 'invalid', array($validation[$field])); } } } }
/** * Compose message */ public function action_compose() { $this->title = __('New Message'); // Set form destination $destination = !is_null($this->request->query('destination')) ? array('destination' => $this->request->query('destination')) : array(); // Set form action $action = Route::get('user/message')->uri(array('action' => 'compose')) . URL::query($destination); $view = View::factory('message/form')->bind('message', $message)->bind('errors', $this->_errors)->set('destination', $destination)->set('action', $action)->set('recipient', FALSE); $message = ORM::factory('message'); if ($this->valid_post('message')) { $sent = (isset($_POST['draft']) and $_POST['draft']) ? 0 : time(); $sender = Auth::instance()->get_user(); $status = $sent == 0 ? PM::STATUS_DRAFT : PM::STATUS_UNREAD; $act = $sent == 0 ? __('saved') : __('sent'); try { $message->values(array('sender' => $sender->id, 'recipient' => User::lookup_by_name($_POST['recipient']), 'subject' => $_POST['subject'], 'body' => $_POST['body'], 'status' => $status, 'format' => $_POST['format'], 'sent' => $sent))->save(); Log::info('Message :id successfully :act.', array(':id' => $message->id, ':act' => $act)); Message::success(__('Message successfully :act.', array(':act' => $act))); // Redirect to Inbox $this->request->redirect(Route::get('user/message')->uri()); } catch (ORM_Validation_Exception $e) { $this->_errors = $e->errors('models', TRUE); } } $this->response->body($view); }