public function validate(&$data) { $messages = array(); $no_settlement = waRequest::post('no_settlement', 0, waRequest::TYPE_INT); if ($data['status'] != blogBlogModel::STATUS_PRIVATE && !$no_settlement) { if (isset($data['id'])) { $url_validator = new blogSlugValidator(array('id' => $data['id'])); } else { $url_validator = new blogSlugValidator(); } $url_validator->setSubject(blogSlugValidator::SUBJECT_BLOG); $name_validator = new waStringValidator(array('max_length' => 255, 'required' => true), array('required' => _w('Blog name must not be empty'))); if (!$url_validator->isValid($data['url'])) { $messages['blog_url'] = current($url_validator->getErrors()); } if (!$name_validator->isValid($data['name'])) { $messages['blog_name'] = current($name_validator->getErrors()); } } else { $blog_model = new blogBlogModel(); if (!$data['id']) { $data['url'] = $blog_model->genUniqueUrl($data['name']); } else { $url = $blog_model->select('url')->where('id = i:id', array('id' => $data['id']))->fetchField('url'); $data['url'] = $url ? $url : $blog_model->genUniqueUrl($data['name']); } } return $messages; }
/** * Validate data * * @param array &$data * @param array $options * * @return array messages or empty array */ public function validate(&$data, $options = array()) { $messages = array(); if ($data['blog_status'] != blogBlogModel::STATUS_PRIVATE) { if (!empty($data['id'])) { $url_validator = new blogSlugValidator(array('id' => $data['id'])); } else { if (!empty($options['transliterate']) && !$data['url']) { if ($data['title']) { $data['url'] = blogHelper::transliterate($data['title']); } else { $data['url'] = $this->genUniqueUrl(''); } } $url_validator = new blogSlugValidator(); } $url_validator->setSubject(blogSlugValidator::SUBJECT_POST); if (!$url_validator->isValid($data['url'])) { $messages['url'] = current($url_validator->getErrors()); if ($url_validator->isError(blogSlugValidator::ERROR_REQUIRED) && ($data['id'] || !$data['id'] && $data['status'] == blogPostModel::STATUS_DRAFT)) { $url = $this->select('url')->where('id = i:id', array('id' => $data['id']))->fetchField('url'); $data['url'] = $url ? $url : $this->genUniqueUrl($data['title']); unset($messages['url']); if (!$url_validator->isValid($data['url'])) { $messages['url'] = current($url_validator->getErrors()); } } elseif (!empty($options['make'])) { $data['url'] = $this->genUniqueUrl($data['url']); unset($messages['url']); if (!$url_validator->isValid($data['url'])) { $messages['url'] = current($url_validator->getErrors()); } } } } else { if (empty($data['id'])) { $data['url'] = $this->genUniqueUrl(empty($data['url']) ? $data['title'] : $data['url']); } else { $url = $this->select('url')->where('id = i:id', array('id' => $data['id']))->fetchField('url'); $data['url'] = $url ? $url : $this->genUniqueUrl($data['title']); } } if (isset($data['datetime']) && !is_null($data['datetime'])) { if (!empty($options['datetime'])) { $formats = (array) $options['datetime']; } elseif (isset($options['datetime'])) { $formats = array(); } elseif (strpos($data['datetime'], ':') !== false) { $formats = array('fulldatetime', 'datetime'); } else { $formats = array('date'); } if ($data['datetime'] != '') { $datetime = $data['datetime']; foreach ($formats as $format) { try { if ($datetime = waDateTime::parse($format, $data['datetime'])) { break; } } catch (Exception $ex) { $messages['datetime'] = _w('Incorrect format'); waLog::log($ex->getMessage()); } } if (preg_match('/^([\\d]{4})\\-([\\d]{1,2})\\-([\\d]{1,2})(\\s|$)/', $datetime, $matches)) { if (!checkdate($matches[2], $matches[3], $matches[1])) { $messages['datetime'] = _w('Incorrect format'); } } $data['datetime'] = $datetime; } else { if ($data['status'] != blogPostModel::STATUS_DRAFT) { $data['datetime'] = false; } } if ($data['datetime'] === false) { $messages['datetime'] = _w('Incorrect format'); } } /** * @event post_validate * @param array [string]mixed $data * @param array ['plugin']['%plugin_id%']mixed plugin data * @return array['%plugin_id%']['field']string error */ $messages['plugin'] = wa()->event('post_validate', $data); if (empty($messages['plugin'])) { unset($messages['plugin']); } return $messages; }
public function validate(&$data) { $messages = array(); $new_route_setup = waRequest::post('new_route_setup', 0, waRequest::TYPE_INT); $route_enabled = waRequest::post('route_enabled', 0, waRequest::TYPE_INT); $no_settlement = $new_route_setup && !$route_enabled; if ($data['status'] != blogBlogModel::STATUS_PRIVATE && !$no_settlement) { if (isset($data['id'])) { $url_validator = new blogSlugValidator(array('id' => $data['id'])); } else { $url_validator = new blogSlugValidator(); } $url_validator->setSubject(blogSlugValidator::SUBJECT_BLOG); $name_validator = new waStringValidator(array('max_length' => 255, 'required' => true), array('required' => _w('Blog name must not be empty'))); if (!$url_validator->isValid($data['url'])) { $messages['blog_url'] = current($url_validator->getErrors()); } if (!$name_validator->isValid($data['name'])) { $messages['blog_name'] = current($name_validator->getErrors()); } } else { $blog_model = new blogBlogModel(); if (!$data['id']) { $data['url'] = $blog_model->genUniqueUrl($data['name']); } else { $url = $blog_model->select('url')->where('id = i:id', array('id' => $data['id']))->fetchField('url'); $data['url'] = $url ? $url : $blog_model->genUniqueUrl($data['name']); } } /** * @event blog_validate * @param array[string]mixed $data * @param array['plugin']['%plugin_id%']mixed plugin data * @return array['%plugin_id%']['field']string error */ $messages['plugin'] = wa()->event('blog_validate', $data); if (empty($messages['plugin'])) { unset($messages['plugin']); } return $messages; }