function perform()
 {
     $this->_view = new BlogView($this->_blogInfo, VIEW_TRACKBACKS_TEMPLATE, SMARTY_VIEW_CACHE_CHECK, array("articleId" => $this->_articleId, "articleName" => $this->_articleName, "categoryName" => $this->_categoryName, "categoryId" => $this->_categoryId, "userId" => $this->_userId, "userName" => $this->_userName, "date" => $this->_date));
     if ($this->_view->isCached()) {
         return true;
     }
     // ---
     // if we got a category name or a user name instead of a category
     // id and a user id, then we have to look up first those
     // and then proceed
     // ---
     // users...
     if ($this->_userName) {
         $users = new Users();
         $user = $users->getUserInfoFromUsername($this->_userName);
         if (!$user) {
             $this->_setErrorView();
             return false;
         }
         // if there was a user, use his/her id
         $this->_userId = $user->getId();
     }
     // ...and categories...
     if ($this->_categoryName) {
         $categories = new ArticleCategories();
         $category = $categories->getCategoryByName($this->_categoryName);
         if (!$category) {
             $this->_setErrorView();
             return false;
         }
         // if there was a user, use his/her id
         $this->_categoryId = $category->getId();
     }
     // fetch the article
     $articles = new Articles();
     if ($this->_articleId) {
         $article = $articles->getBlogArticle($this->_articleId, $this->_blogInfo->getId(), false, $this->_date, $this->_categoryId, $this->_userId);
     } else {
         $article = $articles->getBlogArticleByTitle($this->_articleName, $this->_blogInfo->getId(), false, $this->_date, $this->_categoryId, $this->_userId);
     }
     // if the article id doesn't exist, cancel the whole thing...
     if ($article == false) {
         $this->_view = new ErrorView($this->_blogInfo);
         $this->_view->setValue("message", "error_fetching_article");
         $this->setCommonData();
         return false;
     }
     $this->notifyEvent(EVENT_POST_LOADED, array("article" => &$article));
     $this->notifyEvent(EVENT_TRACKBACKS_LOADED, array("article" => &$article));
     // if everything's fine, we set up the article object for the view
     $this->_view->setValue("post", $article);
     $this->_view->setValue("trackbacks", $article->getTrackbacks());
     $this->setCommonData();
     // and return everything normal
     return true;
 }
 function _addCategory($data, $_debug)
 {
     if ($data["name"] == NULL) {
         $data["name"] = "Category";
     }
     if ($data["url"] == NULL) {
         $data["url"] = "";
     }
     if ($data["blog_id"] == NULL) {
         $data["blog_id"] = 1;
     }
     if ($data["mainpage"] == NULL) {
         $data["mainpage"] = 1;
     }
     $cat = new ArticleCategory($data["name"], $data["url"], $data["blog_id"], $data["mainpage"]);
     $cats = new ArticleCategories();
     if (!($cat_id = $cats->getCategoryByName(TextFilter::urlize($data["name"]), $data["blog_id"]))) {
         $cat_id = $cats->addArticleCategory($cat);
         if ($_debug) {
             print "--- --- category " . $data["name"] . " added with id:  " . $cat_id . "<br />\n\r";
         }
         $this->_stats["categories"]["write"]++;
     } else {
         if ($_debug) {
             print "--- --- category " . $data["name"] . " already exists with id:  " . $cat_id->getId() . "<br />\n\r";
         }
         $cat_id = $cat_id->getId();
     }
     // remap posts' category attribute to the correct ids.
     foreach ($this->_t_container["posts"] as $post => $val) {
         if ($val["category"] == $data["id"] || $val["category"] == NULL && $this->_container["posts"][$post]["blog_id"] == $data["blog_id"]) {
             if ($_debug) {
                 print "--- --- --- remapping post entry #" . $post . " to proper category id<br />\n\r";
             }
             $this->_container["posts"][$post]["category"] = $cat_id;
         }
     }
     return $cat_id;
 }
 function perform()
 {
     $this->_view = new ViewArticleView($this->_blogInfo, array("articleId" => $this->_articleId, "articleName" => $this->_articleName, "categoryId" => $this->_categoryId, "categoryName" => $this->_categoryName, "userId" => $this->_userId, "userName" => $this->_userName, "date" => $this->_date));
     if ($this->_view->isCached()) {
         if ($this->_config->getValue('update_cached_article_reads', false)) {
             if ($this->_articleId) {
                 $this->articles->updateArticleNumReads($this->_articleId);
                 $this->_updateArticleReferrersById($this->_articleId);
             } else {
                 $this->articles->updateArticleNumReadsByName($this->_articleName);
                 $this->_updateArticleReferrersByTitle($this->_articleName);
             }
         }
         return true;
     }
     // ---
     // if we got a category name or a user name instead of a category
     // id and a user id, then we have to look up first those
     // and then proceed
     // ---
     // users...
     if ($this->_userName) {
         $users = new Users();
         $user = $users->getUserInfoFromUsername($this->_userName);
         if (!$user) {
             $this->_setErrorView();
             return false;
         }
         // if there was a user, use his/her id
         $this->_userId = $user->getId();
     }
     // ...and categories...
     if ($this->_categoryName) {
         $categories = new ArticleCategories();
         $category = $categories->getCategoryByName($this->_categoryName, $this->_blogInfo->getId());
         if (!$category) {
             $this->_setErrorView();
             return false;
         }
         // if there was a user, use his/her id
         $this->_categoryId = $category->getId();
     }
     // fetch the article
     // the article identifier can be either its internal id number or its mangled topic
     if ($this->_articleId) {
         $article = $this->articles->getBlogArticle($this->_articleId, $this->_blogInfo->getId(), false, $this->_date, $this->_categoryId, $this->_userId, POST_STATUS_PUBLISHED, $this->_maxDate);
     } else {
         $article = $this->articles->getBlogArticleByTitle($this->_articleName, $this->_blogInfo->getId(), false, $this->_date, $this->_categoryId, $this->_userId, POST_STATUS_PUBLISHED, $this->_maxDate);
     }
     // if the article id doesn't exist, cancel the whole thing...
     if (!$article) {
         $this->_setErrorView();
         return false;
     }
     // throw the correct event
     $this->notifyEvent(EVENT_POST_LOADED, array("article" => &$article));
     // check if we have to update how many times an article has been read
     if ($this->_config->getValue("update_article_reads")) {
         $this->updateNumArticleReads($article->getId());
     }
     // update the referrers, if needed
     $this->_updateArticleReferrers($article);
     // if everything's fine, we set up the article object for the view
     $this->_view->setArticle($article);
     $this->setCommonData();
     // and return everything normal
     return true;
 }
 /**
  * Executes the action
  */
 function perform()
 {
     // first of all, we have to determine which blog we would like to see
     $blogId = $this->_blogInfo->getId();
     // fetch the settings for that blog
     $blogSettings = $this->_blogInfo->getSettings();
     // prepare the view
     $this->_view = new DefaultView($this->_blogInfo, array("categoryId" => $this->_categoryId, "blogId" => $this->_blogInfo->getId(), "categoryName" => $this->_categoryName, "date" => $this->_date, "userName" => $this->_userName, "userId" => $this->_userId));
     // check if everything's cached because if it is, then we don't have to
     // do any work... it's already been done before and we should "safely" assume
     // that there hasn't been any change so far
     if ($this->_view->isCached()) {
         return true;
     }
     // if we got a category name instead of a category id, then we
     // should first look up this category in the database and see if
     // it exists
     $categories = new ArticleCategories();
     if ($this->_categoryName) {
         $category = $categories->getCategoryByName($this->_categoryName, $this->_blogInfo->getId());
         if (!$category) {
             $this->_view = new ErrorView($this->_blogInfo);
             $this->_view->setValue('message', "error_incorrect_category_id");
             $this->setCommonData();
             return false;
         }
         // if everything went fine...
         $this->_categoryId = $category->getId();
     } else {
         // we don't do anything if the cateogry id is '0' or '-1'
         if ($this->_categoryId > 0) {
             $category = $categories->getCategory($this->_categoryId, $this->_blogInfo->getId());
             if (!$category) {
                 $this->_view = new ErrorView($this->_blogInfo);
                 $this->_view->setValue('message', "error_incorrect_category_id");
                 $this->setCommonData();
                 return false;
             }
         }
     }
     // export the category object in case it is needed
     if (isset($category)) {
         $this->_view->setValue("category", $category);
     }
     $users = new Users();
     // if we got a user user id, then we should first look up this id
     // user in the database and see if it exists
     if ($this->_userId > 0) {
         $user = $users->getUserInfoFromId($this->_userId);
         if (!$user) {
             $this->_view = new ErrorView($this->_blogInfo);
             $this->_view->setValue('message', 'error_incorrect_user_id');
             $this->setCommonData();
             return false;
         }
     } else {
         if ($this->_userName) {
             // if we got a user name instead of a user id, then we
             // should first look up this user in the database and see if
             // it exists
             $user = $users->getUserInfoFromUsername($this->_userName);
             if (!$user) {
                 $this->_view = new ErrorView($this->_blogInfo);
                 $this->_view->setValue('message', 'error_incorrect_user_username');
                 $this->setCommonData();
                 return false;
             }
             // if everything went fine...
             $this->_userId = $user->getId();
         }
     }
     // export the owner. The owner information should get from blogInfo directly
     $this->_view->setValue("owner", $this->_blogInfo->getOwnerInfo());
     $t = new Timestamp();
     $todayTimestamp = $t->getTimestamp();
     // amount of posts that we have to show, but keeping in mind that when browsing a
     // category or specific date, we should show *all* of them
     if ($this->_date > 0 || $this->_categoryId > 0) {
         $this->_postAmount = -1;
         // also, inform the template that we're showing them all!
         $this->_view->setValue('showAll', true);
     } else {
         $this->_postAmount = $blogSettings->getValue('show_posts_max');
         $this->_view->setValue('showAll', false);
     }
     //
     // :KLUDGE:
     // the more things we add here to filter, the more complicated this function
     // gets... look at this call and look at how many parameters it needs!! :(
     //
     if ($blogSettings->getValue('show_future_posts_in_calendar') && $this->_date > -1) {
         // if posts in the future are to be shown, we shouldn't set a maximum date
         $blogArticles = $this->articles->getBlogArticles($blogId, $this->_date, $this->_postAmount, $this->_categoryId, POST_STATUS_PUBLISHED, $this->_userId);
     } else {
         $blogArticles = $this->articles->getBlogArticles($blogId, $this->_date, $this->_postAmount, $this->_categoryId, POST_STATUS_PUBLISHED, $this->_userId, $todayTimestamp);
     }
     // if we couldn't fetch the articles, send an error and quit
     if (count($blogArticles) == 0) {
         $this->_view = new ErrorView($this->_blogInfo);
         $this->_view->setValue('message', 'error_fetching_articles');
     } else {
         // otherwise, continue
         // the view will take care of cutting the post if we have the "show more"
         // feature enabled or not... we could do it here but I think that belongs
         // to the view since it is presentation stuff... It could also be handled
         // by the template but then it'd make the template a little bit more
         // complicated...
         // ---
         // before finishing, let's see if there's any plugin that would like to do
         // anything with the post that we just loaded
         // ---
         $pm =& PluginManager::getPluginManager();
         $pm->setBlogInfo($this->_blogInfo);
         $pm->setUserInfo($this->_userInfo);
         $result = $pm->notifyEvent(EVENT_POSTS_LOADED, array('articles' => &$blogArticles));
         $articles = array();
         foreach ($blogArticles as $article) {
             $postText = $article->getIntroText();
             $postExtendedText = $article->getExtendedText();
             $pm->notifyEvent(EVENT_TEXT_FILTER, array("text" => &$postText));
             $pm->notifyEvent(EVENT_TEXT_FILTER, array("text" => &$postExtendedText));
             $article->setIntroText($postText);
             $article->setExtendedText($postExtendedText);
             array_push($articles, $article);
         }
         $this->_view->setValue('posts', $articles);
     }
     $this->setCommonData();
     // save the information about the session for later
     $this->saveSession();
     return true;
 }