NewsFeaturesDownloadsDevelopmentSupportAbout Us

lifetype-1.1.6/class/action/admin/adminaddpostaction.class.php

Go to the documentation of this file.
00001 <?php
00002 
00003         include_once( PLOG_CLASS_PATH."class/action/admin/adminpostmanagementcommonaction.class.php" );
00004         include_once( PLOG_CLASS_PATH."class/view/admin/adminpostslistview.class.php" );        
00005     include_once( PLOG_CLASS_PATH."class/view/admin/admintemplatedview.class.php" );
00006     include_once( PLOG_CLASS_PATH."class/view/admin/adminnewpostview.class.php" );    
00007     include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );   
00008     include_once( PLOG_CLASS_PATH."class/data/validator/arrayvalidator.class.php" );        
00009     include_once( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
00010         
00017     class AdminAddPostAction extends AdminPostManagementCommonAction
00018         {
00023         function AdminAddPostAction( $actionInfo, $request )
00024         {
00025                 $this->AdminPostManagementCommonAction( $actionInfo, $request );
00026                 
00027                 // for data validation purposes, posts must have at least a topic, an intro text, and a category
00028                 $this->registerFieldValidator( "postText", new StringValidator());
00029                 $this->registerFieldValidator( "postTopic", new StringValidator());
00030                 $this->registerFieldValidator( "postCategories", new ArrayValidator());
00031                 $this->registerFieldValidator( "globalArticleCategoryId", new IntegerValidator(), true );
00032                 $view = new AdminNewPostView( $this->_blogInfo );
00033                 $view->setErrorMessage( $this->_locale->tr("error_adding_post"));
00034                 $this->setValidationErrorView( $view );
00035                 
00036                 // these fields do not need to be validated but should be there when we show the view once again
00037                 $this->registerField( "postExtendedText" );
00038                 $this->registerField( "postSlug" );
00039                 $this->registerField( "postStatus" );
00040                 $this->registerField( "sendNotification" );
00041                 $this->registerField( "sendTrackbacks" );
00042                 $this->registerField( "sendPings" );
00043                 $this->registerField( "postId" );
00044                 $this->registerField( "commentsEnabled" );
00045                 $this->registerField( "customField" );
00046                 $this->registerField( "postDateTime" );
00047                 $this->registerField( "trackbackUrls" );                
00048         }
00049 
00055                 function validate()
00056                 {
00057                         $validateOk = parent::validate();
00058                         if( !$validateOk )
00059                                 $this->clearAutoSaveCookie();
00060                                 
00061                         return $validateOk;
00062                 }
00063 
00069                 function _savePostData( $article )
00070                 {
00071             include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );
00072 
00073                         $status = $this->_postStatus;
00074                         
00075                         $articles = new Articles();
00076                         $article->setFields( $this->_getArticleCustomFields());                 
00077                         //print_r($article->_customFields);
00078                         
00079                         // notifiy about this event
00080                         $this->notifyEvent( EVENT_PRE_POST_ADD, Array( "article" => &$article ));                               
00081                         
00082                         // in case the post is already in the db
00083                         if( $this->_postId != "" ) {
00084                                 $article->setId( $this->_postId );
00085                                 $artId = $this->_postId;
00086                                 $postSavedOk = $articles->updateArticle( $article );
00087                                 
00088                                 if( $postSavedOk )
00089                                         $artId = $this->_postId;
00090                                 else
00091                                         $artId = false;
00092                         }
00093                         else {
00094                                 $artId = $articles->addArticle( $article );
00095                         }
00096                         
00097                         return $artId;
00098                 }
00099 
00103         function perform()
00104         {
00105             include_once( PLOG_CLASS_PATH."class/dao/article.class.php" );
00106                 include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );
00107 
00108                         $this->_fetchCommonData();
00109 
00110                         $this->_postId = $this->_request->getValue( "postId" );
00111 
00112             $this->_previewPost    = $this->_request->getValue( "previewPost" );
00113             $this->_addPost        = $this->_request->getValue( "addPost" );
00114                         $this->_saveDraft      = $this->_request->getValue( "isDraft" );
00115                 
00116             // we know for sure that the information is correct so we can now add
00117             // the post to the database
00118                         $postText = Textfilter::xhtmlize($this->_postText).POST_EXTENDED_TEXT_MODIFIER.Textfilter::xhtmlize($this->_postExtendedText);
00119                         
00120                         $article  = new Article( $this->_postTopic, 
00121                                                  $postText, 
00122                                                  $this->_postCategories,
00123                                                                          $this->_userInfo->getId(), 
00124                                                                          $this->_blogInfo->getId(), 
00125                                                                          $this->_postStatus, 
00126                                                                          0, 
00127                                                                          Array(), 
00128                                                                          $this->_postSlug );
00129                         // set also the date before it's too late
00130                         $article->setDateObject( $this->_postTimestamp );
00131                         $article->setCommentsEnabled( $this->_commentsEnabled );
00132                         $article->setGlobalCategoryId( $this->_globalArticleCategoryId );
00133                 
00134                         // save the article to the db
00135                         $artId = $this->_savePostData( $article );
00136                         
00137                 // once we have built the object, we can add it to the database
00138                 if( $artId ) {
00139                     // clear autosave cookie
00140                     $this->clearAutoSaveCookie();
00141 
00142                 $this->_view = new AdminPostsListView( $this->_blogInfo );
00143                 //$article->setId( $artId );
00144                 $message = $this->_locale->tr("post_added_ok");
00145                 
00146                 // train the filter, but only if enabled
00147                                 if( $this->_config->getValue( "bayesian_filter_enabled" ) == true ) {
00148                             include_once( PLOG_CLASS_PATH."class/bayesian/bayesianfiltercore.class.php" );
00149                         BayesianFilterCore::trainWithArticle( $article );
00150                                 }
00151                                 
00152                         // add the article notification if requested to do so
00153                 if( $this->_sendNotification ) {
00154                     require_once( PLOG_CLASS_PATH."class/dao/articlenotifications.class.php" );
00155 
00156                         $artNotifications = new ArticleNotifications();
00157                         $artNotifications->addNotification( $artId, $this->_blogInfo->getId(), $this->_userInfo->getId());
00158                     $message .= " ".$this->_locale->tr("send_notifications_ok");
00159                     }
00160 
00161                 // we only have to send trackback pings if the article was published
00162                 // otherwise there is no need to...
00163                                 $article->setId( $artId );
00164                 if( $article->getStatus() == POST_STATUS_PUBLISHED) {
00165                         // get the output from the xmlrpc pings but only if the user decided to do so!
00166 
00167                                         if( $this->_sendPings) {
00168                         $t = new Timestamp();
00169                         $today = $t->getTimestamp();
00170                         if($today > $article->getDate()){
00171                             $pingsOutput = $this->sendXmlRpcPings();
00172                             $message .= "<br/><br/>".$pingsOutput;
00173                         }
00174                                         }
00175 
00176                     // and now check what to do with the trackbacks
00177                     if( $this->_sendTrackbacks ) {
00178                         include_once( PLOG_CLASS_PATH."class/data/stringutils.class.php" );
00179 
00180                         // get the links from the text of the post
00181                                         $postLinks = StringUtils::getLinks( stripslashes($article->getText()));
00182 
00183                                 // get the real trackback links from trackbackUrls
00184                                 $trackbackLinks = Array();
00185                                 foreach(explode( "\r\n", $this->_trackbackUrls ) as $host ) {
00186                                         trim($host);
00187                                         if( $host != "" && $host != "\r\n" && $host != "\r" && $host != "\n" )
00188                                         array_push( $trackbackLinks, $host );
00189                                 }
00190 
00191                                         // if no links, there is nothing to do
00192                                         if( count($postLinks) == 0 && count($trackbackLinks) == 0 ) {
00193                                                 $this->_view = new AdminPostsListView( $this->_blogInfo );
00194                                         $this->_view->setErrorMessage( $this->_locale->tr("error_no_trackback_links_sent"));
00195                                         }
00196                                         else {
00197                                         $this->_view = new AdminTemplatedView( $this->_blogInfo, "sendtrackbacks" );
00198                                         $this->_view->setValue( "post", $article );
00199                                         $this->_view->setValue( "postLinks", $postLinks );
00200                                                         $this->_view->setValue( "trackbackLinks", $trackbackLinks );                                            
00201                                         }
00202                     }
00203                     $this->_view->setSuccessMessage( $message );
00204                                         
00205                                         $this->notifyEvent( EVENT_POST_POST_ADD, Array( "article" => &$article )); 
00206                                         
00207                                         // empty the cache used by this blog
00208                         include_once( PLOG_CLASS_PATH."class/template/cachecontrol.class.php" );
00209 
00210                                         CacheControl::resetBlogCache( $this->_blogInfo->getId());                                               
00211                 }
00212                 else {
00213                         $this->_view = new AdminPostsListView( $this->_blogInfo );
00214                     $this->_view->setSuccessMessage( $this->_locale->tr("post_added_not_published") );
00215                                         
00216                                         $this->notifyEvent( EVENT_POST_POST_ADD, Array( "article" => &$article ));
00217                 }
00218                 }
00219                 else {
00220                         $this->_view = new AdminPostsListView( $this->_blogInfo );
00221                 $this->_view->setErrorMessage( $this->_locale->tr("error_adding_post") );
00222                 }
00223 
00224             $this->setCommonData();
00225             
00226             // better to return true if everything fine
00227             return true;
00228         }
00229         
00230         function clearAutoSaveCookie()
00231         {
00232                 $rg = $this->_blogInfo->getBlogRequestGenerator();
00233                 $plogBaseUrl = $rg->getBaseUrl(false);
00234                 $cookieBaseName = "LT" . preg_replace("/[^a-zA-Z0-9]/", "", $plogBaseUrl).$this->_blogInfo->getId();
00235 
00236                 // set the auto save cookie as false
00237                 setcookie( $cookieBaseName.'postNotSaved', '0', -1, '/' );
00238                 setcookie( $cookieBaseName.'postTopic', '', -1, '/' );
00239                 setcookie( $cookieBaseName.'postText', '', -1, '/' );
00240                 setcookie( $cookieBaseName.'postExtendedText', '', -1, '/' );
00241         }
00242     }
00243 ?>