Exemplo n.º 1
0
 private function saveData(Text $text, Request $request, Document $document, DocumentRepository $documentRepo)
 {
     if (!$request->hasRequestValue("intro") || !$request->hasRequestValue("title")) {
         return;
     }
     if ($document->isForWidgetArea()) {
         $text->addError($text->t("main.document") . ' ' . $text->t("errors.not_editable"));
         return;
     }
     $document->setIntro($request->getRequestString("intro", ''));
     $document->setTitle($request->getRequestString("title", ''));
     $valid = true;
     if (!Validate::requestToken($request)) {
         $valid = false;
     }
     if (!Validate::stringLength($document->getIntro(), Document::INTRO_MIN_LENGTH, Document::INTRO_MAX_LENGTH)) {
         $text->addError($text->t("documents.intro") . ' ' . Validate::getLastError($text));
         $valid = false;
     }
     if (!Validate::stringLength($document->getTitle(), Document::TITLE_MIN_LENGTH, Document::TITLE_MAX_LENGTH)) {
         $text->addError($text->t("documents.title") . ' ' . Validate::getLastError($text));
         $valid = false;
     }
     if (!$valid) {
         return;
     }
     $isNew = $document->getId() == 0;
     $documentRepo->saveDocument($document);
     if ($isNew) {
         $text->addMessage($text->t("main.document") . ' ' . $text->t("editor.is_created"));
     } else {
         $text->addMessage($text->t("main.document") . ' ' . $text->t("editor.is_edited"));
     }
 }
Exemplo n.º 2
0
 private function updateCategory(CategoryRepository $categoryRepo, Request $request, Text $text)
 {
     $this->category->setName($request->getRequestString("category_name", ""));
     $this->category->setDescriptionHtml($request->getRequestString("category_description", ""));
     $valid = true;
     if (!Validate::stringLength($this->category->getName(), CategoryRepository::NAME_MIN_LENGTH, CategoryRepository::NAME_MAX_LENGTH)) {
         $text->addError($text->t("categories.name") . ' ' . Validate::getLastError($text));
         $valid = false;
     }
     if (!Validate::stringLength($this->category->getDescriptionHtml(), CategoryRepository::DESCRIPTION_MIN_LENGTH, CategoryRepository::DESCRIPTION_MAX_LENGTH)) {
         $text->addError($text->t("categories.description") . ' ' . Validate::getLastError($text));
         $valid = false;
     }
     if ($valid) {
         $newCategory = $this->category->getId() === 0;
         $categoryRepo->saveCategory($this->category);
         // Add a confirmation
         $confirmation = $text->t("main.category") . " " . $text->t("editor.is_edited");
         if ($newCategory) {
             $confirmation = $text->t("main.category") . " " . $text->t("editor.is_created");
         }
         $viewCategory = Link::of($text->getUrlPage("category", $this->category->getId()), $text->t("categories.view_category"));
         $viewCategories = Link::of($text->getUrlpage("category_list"), $text->t("categories.view_all_categories"));
         $text->addMessage($confirmation, $viewCategory, $viewCategories);
     }
 }
Exemplo n.º 3
0
 private function validateInput(User $user, $password1, $password2, UserRepository $userRepo, Text $text)
 {
     $valid = true;
     if (!Validate::username($user->getUsername())) {
         $valid = false;
         $text->addError($text->t("users.the_username") . " " . Validate::getLastError($text));
     }
     if (!Validate::displayName($user->getDisplayName())) {
         $valid = false;
         $text->addError($text->t("users.the_display_name") . " " . Validate::getLastError($text));
     }
     if (!Validate::password($password1, $password2)) {
         $valid = false;
         $text->addError($text->t("users.the_password") . " " . Validate::getLastError($text));
     }
     if (!Validate::email($user->getEmail())) {
         $valid = false;
         $text->addError($text->t("users.the_email") . " " . Validate::getLastError($text));
     }
     if ($userRepo->isUsernameInUse($user->getUsername())) {
         // User with that name already exists
         $valid = false;
         $text->addError($text->tReplaced("errors.already_in_use_on_this_site", $text->t("users.the_username")));
     }
     if (!empty($user->getEmail()) && $userRepo->isEmailInUse($user->getEmail())) {
         // User with that email already exists
         $valid = false;
         $text->addError($text->tReplaced("errors.already_in_use_on_this_site", $text->t("users.the_email")));
     }
     return $valid;
 }
Exemplo n.º 4
0
 private function trySaveMenu(MenuRepository $menuRepo, Text $text)
 {
     if (!Validate::stringLength($this->menu->getName(), 1, MenuRepository::NAME_MAX_LENGTH)) {
         $text->addError($text->t("links.menu") . " " . Validate::getLastError($text));
         return;
     }
     $menuRepo->saveMenu($this->menu);
     $text->addMessage($text->t("links.menu") . " " . $text->t("editor.is_changed"), Link::of($text->getUrlPage("edit_menu", $this->menu->getId()), $text->t("links.menu.go_back")));
 }
Exemplo n.º 5
0
 private function trySwitchTheme(ThemeManager $themeManager, Text $text, Request $request)
 {
     $themeDirectory = $request->getRequestString("theme", "");
     if (!$themeManager->themeExists($themeDirectory)) {
         $text->addError($text->t("themes.does_not_exist"));
         return false;
     }
     $themeManager->setActiveTheme($themeDirectory);
     $text->addMessage($text->t("themes.successfully_switched"));
     return true;
 }
Exemplo n.º 6
0
 /**
  * Validates a comment for saving to the database.
  * @param Comment $comment The comment.
  * @param Text $text Errors go here.
  * @return boolean True if the comment is valid, false otherwise.
  */
 public function validateComment(Comment $comment, Text $text)
 {
     $valid = true;
     if (!Validate::stringLength($comment->getBodyRaw(), Comment::BODY_MIN_LENGTH, Comment::BODY_MAX_LENGTH)) {
         $text->addError($text->t("comments.comment") . " " . Validate::getLastError($text));
         $valid = false;
     }
     if ($comment->isByVisitor()) {
         if (!Validate::email($comment->getUserEmail())) {
             $text->addError($text->t("users.email") . " " . Validate::getLastError($text));
             $valid = false;
         }
         if (!Validate::displayName($comment->getUserDisplayName())) {
             $text->addError($text->t("users.name") . " " . Validate::getLastError($text));
             $valid = false;
         }
     }
     return $valid;
 }
Exemplo n.º 7
0
 private function handleRequest(Text $text, Request $request, LinkRepository $linkRepo)
 {
     $valid = true;
     $linkText = $request->getRequestString("link_text", "");
     $this->link->setText($linkText);
     if (!Validate::nameOfLink($linkText)) {
         $text->addError($this->t("links.text") . " " . Validate::getLastError($text));
         $valid = false;
     }
     $url = $request->getRequestString("link_url", "");
     if (Validate::url($url)) {
         $this->link->setUrl(new Uri($url));
     } else {
         $text->addError($text->t("links.url") . " " . Validate::getLastError($text));
         $valid = false;
     }
     if ($valid) {
         $linkRepo->saveLink($this->link);
         $text->addMessage($text->t("main.link") . ' ' . $text->t("editor.is_edited"), Link::of($text->getUrlPage("edit_menu", $this->link->getMenuId()), $text->t("links.menu.go_back")));
     }
 }
Exemplo n.º 8
0
 private function validateInput(User $user, $password, Authentication $auth, UserRepository $userRepo, Text $text)
 {
     $valid = true;
     if (!Validate::username($user->getUsername())) {
         $valid = false;
         $text->addError($text->t("users.the_username") . " " . Validate::getLastError($text));
     }
     if (!Validate::displayName($user->getDisplayName())) {
         $valid = false;
         $text->addError($text->t("users.the_display_name") . " " . Validate::getLastError($text));
     }
     if (!Validate::password($password, $password)) {
         $valid = false;
         $text->addError($text->t("users.the_password") . " " . Validate::getLastError($text));
     }
     if (!Validate::email($user->getEmail())) {
         $valid = false;
         $text->addError($text->t("users.the_email") . " " . Validate::getLastError($text));
     }
     if ($userRepo->isUsernameInUse($user->getUsername())) {
         // User with that name already exists
         $valid = false;
         $text->addError($text->tReplaced("errors.already_in_use_on_this_site", $text->t("users.the_username")));
     }
     if (!empty($user->getEmail()) && $userRepo->isEmailInUse($user->getEmail())) {
         // User with that email already exists
         $valid = false;
         $text->addError($text->tReplaced("errors.already_in_use_on_this_site", $text->t("users.the_email")));
     }
     if (!$auth->isValidRankForAccounts($user->getRank())) {
         // Invlaid rank
         $valid = false;
         $text->addError($text->t("users.the_rank") . " " . $text->t("errors.is_invalid"));
     }
     return $valid;
 }
Exemplo n.º 9
0
 public function processInput(Text $text, Request $request, CategoryRepository $oCategories)
 {
     $article = $this->articleObject;
     $noErrors = true;
     // Title
     if ($request->hasRequestValue("article_title")) {
         $title = trim($request->getRequestString('article_title'));
         if (strLen($title) > Article::MAX_TITLE_LENGTH) {
             $text->addError($text->t("articles.title") . " " . $text->tReplaced("errors.is_too_long_num", Article::MAX_TITLE_LENGTH));
             $noErrors = false;
         }
         if (strLen($title) < Article::MIN_TITLE_LENGTH) {
             $text->addError($text->tReplacedKey("errors.please_enter_this", "articles.title", true));
             $noErrors = false;
         }
         $article->setTitle($title);
     }
     // Intro
     if ($request->hasRequestValue("article_intro")) {
         $intro = trim($request->getRequestString("article_intro"));
         if (strLen($intro) < Article::MIN_INTRO_LENGTH) {
             $text->addError($text->tReplacedKey("errors.please_enter_this", "articles.intro", true));
             $noErrors = false;
         }
         if (strLen($intro) > Article::MAX_INTRO_LENGTH) {
             $text->addError($text->t("articles.intro") . " " . $text->tReplaced("errors.is_too_long_num", Article::MAX_INTRO_LENGTH));
             $noErrors = false;
         }
         $article->setIntro($intro);
     }
     // Body
     if ($request->hasRequestValue("article_body")) {
         $body = trim($request->getRequestString("article_body"));
         if (strLen($body) < Article::MIN_BODY_LENGTH) {
             $text->addError($text->tReplacedKey("errors.please_enter_this", "articles.body", true));
             $noErrors = false;
         }
         if (strLen($body) > Article::MAX_BODY_LENGTH) {
             $text->addError($text->t("articles.body") . " " . $text->tReplaced("errors.is_too_long_num", Article::MAX_BODY_LENGTH));
             $noErrors = false;
         }
         $article->setBody($body);
     }
     // Category
     if ($request->hasRequestValue("article_category")) {
         $categoryId = (int) $request->getRequestString('article_category', 0);
         if ($categoryId == 0) {
             // Silent failure when category id is set to 0, as it is a default value
             $noErrors = false;
         } elseif (!$this->categoryExists($oCategories, $categoryId)) {
             $text->addError($text->t("main.category") . " " . $website->t("errors.not_found"));
             $noErrors = false;
         }
         $article->categoryId = $categoryId;
     }
     // Featured image
     if ($request->hasRequestValue("article_featured_image")) {
         $featuredImage = trim($request->getRequestString("article_featured_image"));
         if (strLen($featuredImage) > Article::MAX_FEATURED_IMAGE_URL_LENGTH) {
             $text->addError($text->t("articles.featured_image") . " " . $text->tReplaced("ërrors.is_too_long_num", Article::MAX_FEATURED_IMAGE_URL_LENGTH));
             $noErrors = false;
         }
         $article->featuredImage = $featuredImage;
     }
     // Pinned, hidden, comments
     if ($request->hasRequestValue("submit")) {
         $article->pinned = $request->hasRequestValue("article_pinned");
         $article->setHidden($request->hasRequestValue("article_hidden"));
         $article->showComments = $request->hasRequestValue("article_comments");
     }
     // Event date
     $eventDate = "";
     $eventTime = "";
     if ($request->hasRequestValue("article_eventdate")) {
         $eventDate = trim($request->getRequestString("article_eventdate"));
     }
     if ($request->hasRequestValue("article_eventtime") && $eventDate) {
         $eventTime = trim($request->getRequestString("article_eventtime"));
     }
     if (empty($eventDate) && $request->hasRequestValue("article_eventdate")) {
         // Field was made empty, so delete date on article
         $article->onCalendar = null;
     }
     if (!empty($eventDate)) {
         if (strtotime($eventDate) === false) {
             $text->addError($text->t("articles.event_date") . " " . $text->t("errors.not_correct"));
             $noErrors = false;
         } else {
             // Add date to article
             $article->onCalendar = new DateTime($eventDate . " " . $eventTime);
         }
     }
     return $noErrors;
 }