/** * Run method with main page logic * * Read in list of the latest published articles. Pagination enabled. * Populate template and display results in the page. * @access public */ public function run() { $PAGINATION_LIMIT = 10; $session = Session::getInstance(); $user = $session->getUser(); /* if ($user == null || !$user->validUser ()) { header ("Location: " . BASE_URL); return; } */ $articleDAO = ArticleDAO::getInstance(); $tagDAO = ArticleTagDAO::getInstance(); $page = isset($_GET["page"]) && is_numeric($_GET["page"]) ? intval($_GET["page"]) : 1; if ($page < 1) { $page = 1; } $count = $paginator = $paginator_page = null; $article = $articletags_array = null; $title = ""; $count = $articleDAO->countPublished(true); $paginator = new Paginator($count, $PAGINATION_LIMIT); $paginator_page = $paginator->getPage($page); $article_array = $articleDAO->allPublished(true, array("order" => "{$articleDAO->getTableName()}.postDate DESC, {$articleDAO->getTableName()}.id DESC", "limit" => $paginator_page, "joins" => true)); foreach ($article_array as $article) { $articletags_array[] = $tagDAO->allArticleTags($article, array("order" => "name")); } $this->template->render(array("title" => "Latests Articles", "main_page" => "article_list_tpl.php", "session" => $session, "article_array" => $article_array, "articletags_array" => $articletags_array, "paginator_page" => $paginator_page)); }
/** * Run method with main page logic * * Read in the specified article from the database. * Populate template and display article in the page * @access public */ public function run() { $session = Session::getInstance(); $user = $session->getUser(); /* if ($user == null || !$user->validUser ()) { header ("Location: " . BASE_URL); return; } */ $articleDAO = ArticleDAO::getInstance(); $tagDAO = ArticleTagDAO::getInstance(); $article = $articletags = null; $title = ""; if (!empty($_GET["id"]) && is_numeric($_GET["id"])) { $article_id = intval($_GET["id"]); $article = $articleDAO->load($article_id, array("joins" => true)); if ($article) { $title .= "{$article->getTitle()}"; $articletags = $tagDAO->allArticleTags($article, array("order" => "name")); } } //print_r ($articletags); $this->template->render(array("title" => "Article - " . $title, "main_page" => "view_article_tpl.php", "session" => $session, "article" => $article, "articletags" => $articletags)); }
/** * Load instances of Article entities that is tagged with the ArticleTag indicated by the tag param * * @access public * @param ArticleTag $tag * @param array $options (Optional) Read documentation on parseOptions for details * @return array */ public function allWithTag(ArticleTag $tag, $options = null) { $tagDAO = ArticleTagDAO::getInstance(); $taggedDAO = TaggedArticleDAO::getInstance(); $this->resetQueryStrings(); $this->query_reset_lock = true; $this->query_joins = " INNER JOIN {$taggedDAO->getTableName()} ON {$this->tableName}.id = {$taggedDAO->getTableName()}.articleId INNER JOIN {$tagDAO->getTableName()} ON {$taggedDAO->getTableName()}.tagId = {$tagDAO->getTableName()}.id "; $this->query_where = "WHERE {$tagDAO->getTableName()}.id = ?"; $this->query_params = array($tag->getId()); $result_array = $this->all($options); $this->query_reset_lock = false; return $result_array; }
/** * Run method with main page logic * * Populate template and display form for creating a new article entry. For POST requests, * validate form data and save information to database. Available to admins only * @access public */ public function run() { $session = Session::getInstance(); $user = $session->getUser(); if (!$user || !$user->isAdmin()) { $session->setMessage("Do not have permission to access", Session::MESSAGE_ERROR); header("Location: " . BASE_URL); return; } $form_errors = array(); $form_values = array("title" => "", "content" => "", "postDate" => "", "published" => "", "tags" => ""); $articleDAO = ArticleDAO::getInstance(); $tagDAO = ArticleTagDAO::getInstance(); if (!empty($_POST)) { $form_values["title"] = isset($_POST["title"]) ? trim($_POST["title"]) : ""; $form_values["content"] = isset($_POST["content"]) ? trim($_POST["content"]) : ""; $form_values["postDate"] = isset($_POST["postDate"]) ? trim($_POST["postDate"]) : ""; $form_values["published"] = isset($_POST["published"]) ? trim($_POST["published"]) : ""; $form_values["tags"] = isset($_POST["tags"]) ? trim($_POST["tags"]) : ""; if (empty($form_values["title"])) { $form_errors["title"] = "No title specified"; } if (empty($form_values["content"])) { $form_errors["content"] = "No content specified"; } if (empty($form_values["postDate"])) { $form_errors["postDate"] = "No post date specified"; } else { if (strtotime($_POST["postDate"]) == 0) { $form_errors["postDate"] = "An invalid post date was specified"; $form_values["postDate"] = ""; } } if ($form_values["published"] != "true" && $form_values["published"] != "false") { $form_errors["published"] = "Invalid published choice"; } if (empty($form_errors)) { $article = new Article(); $article->setTitle($form_values["title"]); $article->setContent($form_values["content"]); $article->setPostDate(strtotime($form_values["postDate"])); $article->setUpdateDate(0); $published = $form_values["published"] == "true" ? 1 : 0; $article->setPublished($published); $article->setUserId($user->id); //$article->setTags ($form_values["tags"]); $sorted_tag_array = ArticleTag::tagsFromString($form_values["tags"]); $sorted_tags = implode(" ", $sorted_tag_array); $article->setTags($sorted_tags); if ($articleDAO->insert($article)) { $tagDAO->updateTags($article); $session->setMessage("Article details saved"); header("Location: edit_article.php?id={$article->id}"); return; } else { $session->setMessage("Article details could not be saved", Session::MESSAGE_ERROR); } } } $this->template->render(array("title" => "Create Article", "extra_header" => joinPath("headers", "jscal_header_tpl.php"), "main_page" => "create_article_tpl.php", "session" => $session, "form_errors" => $form_errors, "form_values" => $form_values)); }
/** * Retrieve instance of an ArticleTagDAO or create one if it does * not exist. * * @access public * @static * @return ArticleTagDAO */ public static function getInstance() { if (!isset(self::$instance)) { self::$instance = new self(); } return self::$instance; }
/** * Run method with main page logic * * Populate template and display form for editing an article entry. For POST requests, * check user credentials, check if article exists and then update entry in database. * Available to admins only * @access public */ public function run() { $session = Session::getInstance(); $user = $session->getUser(); if (!$user || !$user->isAdmin()) { $session->setMessage("Do not have permission to access", Session::MESSAGE_ERROR); header("Location: " . BASE_URL); return; } $form_errors = array(); $form_values = array("id" => "", "title" => "", "content" => "", "postDate" => "", "updateDate" => "", "published" => "", "tags" => ""); $articleDAO = ArticleDAO::getInstance(); $tagDAO = ArticleTagDAO::getInstance(); $article = null; if (!empty($_POST)) { $form_values["id"] = isset($_POST["id"]) && is_numeric($_POST["id"]) ? intval($_POST["id"]) : ""; $form_values["title"] = isset($_POST["title"]) ? trim($_POST["title"]) : ""; $form_values["content"] = isset($_POST["content"]) ? trim($_POST["content"]) : ""; $form_values["postDate"] = isset($_POST["postDate"]) ? trim($_POST["postDate"]) : ""; $form_values["updateDate"] = isset($_POST["updateDate"]) ? trim($_POST["updateDate"]) : ""; $form_values["published"] = isset($_POST["published"]) ? trim($_POST["published"]) : ""; $form_values["tags"] = isset($_POST["tags"]) ? trim($_POST["tags"]) : ""; if (empty($form_values["id"])) { $form_errors["id"] = "No id specified"; } if (empty($form_values["title"])) { $form_errors["title"] = "No title specified"; } if (empty($form_values["content"])) { $form_errors["content"] = "No content specified"; } if (empty($form_values["postDate"])) { $form_errors["postDate"] = "No post date specified"; } else { if (strtotime($form_values["postDate"]) == 0) { $form_errors["postDate"] = "An invalid post date was specified"; $form_values["postDate"] = ""; } } if (!empty($form_values["updateDate"]) && strtotime($form_values["updateDate"]) == 0) { $form_errors["updateDate"] = "An invalid update date was specified"; $form_values["updateDate"] = ""; } if ($form_values["published"] != "true" && $form_values["published"] != "false") { $form_errors["published"] = "Invalid published choice"; } if (empty($form_errors)) { $article = $articleDAO->load($form_values["id"]); if ($article && ($user->isAdmin() || $article->userId == $user->id)) { $article->setTitle($form_values["title"]); $article->setContent($form_values["content"]); $article->setPostDate(strtotime($form_values["postDate"])); if (!empty($form_values["updateDate"])) { $article->setUpdateDate(strtotime($form_values["updateDate"])); } //$article->setUpdateDate (time ()); $published = $form_values["published"] == "true" ? 1 : 0; $article->setPublished($published); $article->setUserId($user->id); $sorted_tag_array = ArticleTag::tagsFromString($form_values["tags"]); $sorted_tags = implode(" ", $sorted_tag_array); $article->setTags($sorted_tags); //print_r ($article); if ($articleDAO->save($article)) { $tagDAO->updateTags($article); $session->setMessage("Article details saved"); header("Location: edit_article.php?id={$article->id}"); return; } else { $session->setMessage("Article details could not be saved", Session::MESSAGE_ERROR); } } else { $session->setMessage("Do not have permission to edit the article", Session::MESSAGE_ERROR); header("Location: " . BASE_URL); return; } } else { if (empty($form_errors["id"])) { $article = $articleDAO->load($form_values["id"]); } } } else { if (!empty($_GET)) { $form_values["id"] = isset($_GET["id"]) ? $_GET["id"] : ""; if (empty($form_values["id"])) { header("Location: " . BASE_URL); return; } else { $article = $articleDAO->load($form_values["id"]); // Article does not exist. Pass null to template if (!$article) { } else { if (!$user->isAdmin() && $article->userId != $user->id) { $session->setMessage("Do not have permission to edit article", Session::MESSAGE_ERROR); header("Location: " . BASE_URL); return; } else { $form_values["id"] = $article->getId(); $form_values["title"] = $article->getTitle(); $form_values["content"] = $article->getContent(); $form_values["published"] = $article->getPublished() == true ? "true" : "false"; $form_values["postDate"] = strftime("%d %B %Y", $article->getPostDate()); $form_values["updateDate"] = $article->getUpdateDate() > 0 ? strftime("%d %B %Y", $article->getUpdateDate()) : ""; $form_values["tags"] = $article->getTags(); } } } } } $this->template->render(array("title" => "Edit Article", "extra_header" => joinPath("headers", "jscal_header_tpl.php"), "main_page" => "edit_article_tpl.php", "session" => $session, "article" => $article, "form_errors" => $form_errors, "form_values" => $form_values)); }