/**
  * Carries out the specified action
  */
 function perform()
 {
     // fetch the data
     $this->_fetchCommonData();
     $this->_postId = $this->_request->getValue("postId");
     // fetch the old post
     $articles = new Articles();
     $post = $articles->getBlogArticle($this->_postId, $this->_blogInfo->getId());
     // there must be something wrong if we can't fetch the post that we are trying to update...
     if (!$post) {
         $this->_view = new AdminPostsListView($this->_blogInfo);
         $this->_view->setErrorMessage($this->_locale->tr("error_fetching_post"));
         $this->setCommonData();
         return false;
     }
     // if we got it, update some fields
     $post->setTopic(stripslashes($this->_postTopic));
     $postText = $this->_postText . POST_EXTENDED_TEXT_MODIFIER . $this->_postExtendedText;
     $post->setText(stripslashes($postText));
     $post->setCategoryIds($this->_postCategories);
     $post->setStatus($this->_postStatus);
     $post->setDateObject($this->_postTimestamp);
     $post->setCommentsEnabled($this->_commentsEnabled);
     $post->setPostSlug($this->_postSlug);
     // prepare the custom fields
     $fields = array();
     if (is_array($this->_customFields)) {
         foreach ($this->_customFields as $fieldId => $fieldValue) {
             // 3 of those parameters are not really need when creating a new object... it's enough that
             // we know the field definition id.
             $customField = new CustomFieldValue($fieldId, $fieldValue, "", -1, "", $post->getId(), $this->_blogInfo->getId(), -1);
             array_push($fields, $customField);
         }
         $post->setFields($fields);
     }
     // fire the pre event
     $this->notifyEvent(EVENT_PRE_POST_UPDATE, array("article" => &$post));
     // and finally save the post to the database
     if (!$articles->updateArticle($post)) {
         $this->_view = new AdminPostsListView($this->_blogInfo);
         $this->_view->setErrorMessage($this->_locale->tr("error_updating_post"));
         $this->setCommonData();
         return false;
     }
     // clean up the cache
     CacheControl::resetBlogCache($this->_blogInfo->getId());
     // create the definitive view
     $this->_view = new AdminPostsListView($this->_blogInfo);
     // show a message saying that the post was updated
     $message = $this->_locale->pr("post_updated_ok", $post->getTopic());
     // check if there was a previous notification
     $notifications = new ArticleNotifications();
     $artNotification = $notifications->getUserArticleNotification($this->_postId, $this->_blogInfo->getId(), $this->_userInfo->getId());
     // check if we have to add or remove a notification
     if ($this->_sendNotification) {
         if (!$artNotification) {
             // if there wasn't one and now we want it, we have to add it
             $notifications->addNotification($this->_postId, $this->_blogInfo->getId(), $this->_userInfo->getId());
             $message .= " " . $this->_locale->tr("notification_added");
         }
     } else {
         // if we don't want notifications, then we have to check if there is one since we
         // should remove it
         if ($artNotification) {
             $notifications->deleteNotification($this->_postId, $this->_blogInfo->getId(), $this->_userInfo->getId());
             $message .= " " . $this->_locale->tr("notification_removed");
         }
     }
     // if the "send xmlrpc pings" checkbox was enabled, do something about it...
     if ($post->getStatus() == POST_STATUS_PUBLISHED) {
         // get the links from the text of the post
         $postLinks = StringUtils::getLinks(stripslashes($post->getText()));
         // get the real trackback links from trackbackUrls
         $trackbackLinks = array();
         foreach (explode("\r\n", $this->_trackbackUrls) as $host) {
             trim($host);
             if ($host != "" && $host != "\r\n" && $host != "\r" && $host != "\n") {
                 array_push($trackbackLinks, $host);
             }
         }
         if ($this->_sendPings) {
             $message .= "<br/><br/>" . $this->sendXmlRpcPings();
         }
         // and now check what to do with the trackbacks
         if ($this->_sendTrackbacks) {
             // if no links, there is nothing to do
             if (count($postLinks) == 0 && count($trackbackLinks) == 0) {
                 $this->_view = new AdminPostsListView($this->_blogInfo);
                 $this->_view->setErrorMessage($this->_locale->tr("error_no_trackback_links_sent"));
             } else {
                 $this->_view = new AdminTemplatedView($this->_blogInfo, "sendtrackbacks");
                 // get the links from the text of the post
                 $this->_view->setValue("post", $post);
                 $this->_view->setValue("postLinks", $postLinks);
                 $this->_view->setValue("trackbackLinks", $trackbackLinks);
             }
         }
     }
     // show the message
     $this->_view->setSuccessMessage($message);
     // and fire the post event
     $this->notifyEvent(EVENT_POST_POST_UPDATE, array("article" => &$post));
     $this->setCommonData();
     return true;
 }
 /**
  * Carries out the specified action
  */
 function perform()
 {
     $this->_fetchCommonData();
     $this->_postId = $this->_request->getValue("postId");
     $this->_previewPost = $this->_request->getValue("previewPost");
     $this->_addPost = $this->_request->getValue("addPost");
     $this->_saveDraft = $this->_request->getValue("isDraft");
     // we know for sure that the information is correct so we can now add
     // the post to the database
     $postText = Textfilter::xhtmlize($this->_postText) . POST_EXTENDED_TEXT_MODIFIER . Textfilter::xhtmlize($this->_postExtendedText);
     $article = new Article($this->_postTopic, $postText, $this->_postCategories, $this->_userInfo->getId(), $this->_blogInfo->getId(), $this->_postStatus, 0, array(), $this->_postSlug);
     // set also the date before it's too late
     $article->setDateObject($this->_postTimestamp);
     $article->setCommentsEnabled($this->_commentsEnabled);
     // save the article to the db
     $artId = $this->_savePostData($article);
     // once we have built the object, we can add it to the database
     if ($artId) {
         $this->_view = new AdminPostsListView($this->_blogInfo);
         //$article->setId( $artId );
         $message = $this->_locale->tr("post_added_ok");
         // train the filter
         BayesianFilterCore::trainWithArticle($article);
         // add the article notification if requested to do so
         if ($this->_sendNotification) {
             $artNotifications = new ArticleNotifications();
             $artNotifications->addNotification($artId, $this->_blogInfo->getId(), $this->_userInfo->getId());
             $message .= " " . $this->_locale->tr("send_notifications_ok");
         }
         // we only have to send trackback pings if the article was published
         // otherwise there is no need to...
         $article->setId($artId);
         if ($article->getStatus() == POST_STATUS_PUBLISHED) {
             // get the output from the xmlrpc pings but only if the user decided to do so!
             if ($this->_sendPings) {
                 $pingsOutput = $this->sendXmlRpcPings();
                 $message .= "<br/><br/>" . $pingsOutput;
             }
             // and now check what to do with the trackbacks
             if ($this->_sendTrackbacks) {
                 // get the links from the text of the post
                 $postLinks = StringUtils::getLinks(stripslashes($article->getText()));
                 // get the real trackback links from trackbackUrls
                 $trackbackLinks = array();
                 foreach (explode("\r\n", $this->_trackbackUrls) as $host) {
                     trim($host);
                     if ($host != "" && $host != "\r\n" && $host != "\r" && $host != "\n") {
                         array_push($trackbackLinks, $host);
                     }
                 }
                 // if no links, there is nothing to do
                 if (count($postLinks) == 0 && count($trackbackLinks) == 0) {
                     $this->_view = new AdminPostsListView($this->_blogInfo);
                     $this->_view->setErrorMessage($this->_locale->tr("error_no_trackback_links_sent"));
                 } else {
                     $this->_view = new AdminTemplatedView($this->_blogInfo, "sendtrackbacks");
                     $this->_view->setValue("post", $article);
                     $this->_view->setValue("postLinks", $postLinks);
                     $this->_view->setValue("trackbackLinks", $trackbackLinks);
                 }
             }
             $this->_view->setSuccessMessage($message);
             $this->notifyEvent(EVENT_POST_POST_ADD, array("article" => &$article));
             // empty the cache used by this blog
             CacheControl::resetBlogCache($this->_blogInfo->getId());
         } else {
             $this->_view = new AdminPostsListView($this->_blogInfo);
             $this->_view->setSuccessMessage($this->_locale->tr("post_added_not_published"));
             $this->notifyEvent(EVENT_POST_POST_ADD, array("article" => &$article));
         }
     } else {
         $this->_view = new AdminPostsListView($this->_blogInfo);
         $this->_view->setErrorMessage($this->_locale->tr("error_adding_post"));
     }
     $this->setCommonData();
     // better to return true if everything fine
     return true;
 }