NewsFeaturesDownloadsDevelopmentSupportAbout Us

lifetype-1.1.6/class/action/viewarticleaction.class.php

Go to the documentation of this file.
00001 <?php
00002 
00003         include_once( PLOG_CLASS_PATH."class/action/blogaction.class.php" );
00004         include_once( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );
00005         include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );       
00006     include_once( PLOG_CLASS_PATH."class/view/errorview.class.php" );
00007 
00014         class ViewArticleAction extends BlogAction
00015     {
00016 
00017         var $_config;
00018         var $_articleId;
00019                 var $_articleName;
00020                 var $_date;
00021                 var $_maxDate;
00022                 var $_userId;
00023                 var $_userName;
00024                 var $_categoryId;
00025                 var $_categoryName;
00026 
00027                 function ViewArticleAction( $actionInfo, $request )
00028         {
00029                         $this->BlogAction( $actionInfo, $request );
00030                         
00031                         $this->registerFieldValidator( "articleId", new IntegerValidator(), true );
00032                         $this->registerFieldValidator( "articleName", new StringValidator(), true );
00033                         $this->registerFieldValidator( "postCategoryId", new IntegerValidator(), true );
00034                         $this->registerFieldValidator( "postCategoryName", new StringValidator(), true );
00035                         $this->registerFieldValidator( "userId", new IntegerValidator(), true );
00036                         $this->registerFieldValidator( "userName", new StringValidator(), true );
00037 
00038                         $this->setValidationErrorView( new ErrorView( $this->_blogInfo, "error_fetching_article" ));
00039         }
00040         
00041         // checks that the articleId is valid
00042         function validate()
00043         {
00044                         if( !parent::validate())
00045                                 return( false );
00046         
00047                 $this->_articleId = $this->_request->getValue( "articleId" );
00048                         $this->_articleName = $this->_request->getValue( "articleName" );
00049                         // find some other additional parameters and use some 'null' values
00050                         // in casuse they're empty
00051                         $this->_categoryId = $this->_request->getValue( "postCategoryId", -1 );
00052                         $this->_categoryName = $this->_request->getValue( "postCategoryName" );
00053                         $this->_userId = $this->_request->getValue( "userId", -1 );
00054                         $this->_userName = $this->_request->getValue( "userName" );
00055                         $this->_date = $this->_request->getValue( "Date", -1 );
00056                 $val = new IntegerValidator();
00057                 if( !$val->validate( $this->_date ) )
00058                 $this->_date = -1;
00059                         $this->_isCommentAdded = ($this->_request->getValue( "op" ) == "AddComment" );
00060                         
00061                         // Calculate the correct article date period
00062             $adjustedDates = $this->_getCorrectedDatePeriod( $this->_date );
00063             $this->_date = $adjustedDates["adjustedDate"];
00064             $this->_maxDate = $adjustedDates["maxDate"];
00065 //                      if( $this->_maxDate == -1 ) $this->_maxDate = 0;
00066             
00067             return true;
00068         }
00069                 
00070                 function _setErrorView()
00071                 {
00072             include_once( PLOG_CLASS_PATH."class/view/errorview.class.php" );
00073 
00074                         $this->_view = new ErrorView( $this->_blogInfo );
00075                         $this->_view->setValue( "message", "error_fetching_article" );
00076                         $this->setCommonData();         
00077                 }
00078                 
00083                 function _updateArticleReferrers($article){
00084                         $this->_updateArticleReferrersById($article->getId());
00085                 }
00090         function _updateArticleReferrersById($articleId)
00091         {
00092                 include_once( PLOG_CLASS_PATH."class/dao/referers.class.php" );         
00093                 
00094             if ( array_key_exists( 'HTTP_REFERER', $_SERVER ) )
00095             {
00096                 $referrers = new Referers();
00097                 $referrers->addReferer( $_SERVER['HTTP_REFERER'], 
00098                                         $articleId, $this->_blogInfo->getId());
00099             }
00100         }
00105                 function _updateArticleReferrersByTitle($slug)
00106                 {
00107                 include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );                                         
00108                         $articles = new Articles();
00109                         $article = $articles->getBlogArticleByTitle( $slug, $this->_blogInfo->getId());
00110                         $article ? $id = $article->getId() : $id = 0;
00111                         // if the article isn't found, we will save a referrer to
00112                     // the main page, since $id will be 0.
00113                         $this->_updateArticleReferrersById( $id );
00114                 }
00115                 
00123                 function updateNumArticleReads( $articleId )
00124                 {
00125                         $articles = new Articles();
00126                         $articles->updateArticleNumReads( $articleId );
00127                         
00128                         return( true );
00129                 }
00130 
00131         function perform()
00132         {               
00133                 include_once( PLOG_CLASS_PATH."class/view/viewarticleview.class.php" );
00134                 include_once( PLOG_CLASS_PATH."class/dao/articles.class.php" );                      
00135 
00136                 $this->_view = new ViewArticleView( $this->_blogInfo, 
00137                                                    Array( "articleId" => $this->_articleId,
00138                                                           "articleName" => $this->_articleName,
00139                                                           "categoryId" => $this->_categoryId,
00140                                                           "categoryName" => $this->_categoryName,
00141                                                           "userId" => $this->_userId,
00142                                                           "userName" => $this->_userName,
00143                                                           "date" => $this->_date ));                                                                                                                                          
00144                                                                                                                   
00145                         if( $this->_view->isCached()) {
00146                                 if( $this->_config->getValue( 'update_cached_article_reads', false )) {
00147                                         $articles = new Articles();
00148                                         if( $this->_articleId ){
00149                                                 $articles->updateArticleNumReads(       $this->_articleId );
00150                         $this->_updateArticleReferrersById( $this->_articleId );
00151                     }
00152                                         else{
00153                                                 $articles->updateArticleNumReadsByName( $this->_articleName );
00154                         $this->_updateArticleReferrersByTitle($this->_articleName );
00155                     }
00156                                 }
00157                                 
00158                                 return true;
00159                         }                       
00160                         
00161 
00162                         include_once( PLOG_CLASS_PATH."class/dao/articlecategories.class.php" );
00163                 include_once( PLOG_CLASS_PATH."class/dao/users.class.php" );
00164                 include_once( PLOG_CLASS_PATH."class/data/textfilter.class.php" );      
00165                         include_once( PLOG_CLASS_PATH.'class/data/pager/pager.class.php' );                             
00166                                                                                                                   
00167                         // ---
00168                         // if we got a category name or a user name instead of a category
00169                         // id and a user id, then we have to look up first those
00170                         // and then proceed
00171                         // ---
00172                         // users...
00173                         if( $this->_userName ) {
00174                                 $users = new Users();
00175                                 $user = $users->getUserInfoFromUsername( $this->_userName );
00176                                 if( !$user ) {
00177                                         $this->_setErrorView();
00178                                         return false;                           
00179                                 }
00180                                 // if there was a user, use his/her id
00181                                 $this->_userId = $user->getId();
00182                         }
00183                         
00184                         // ...and categories...
00185                         if( $this->_categoryName ) {
00186                                 $categories = new ArticleCategories();
00187                                 $category = $categories->getCategoryByName( $this->_categoryName, $this->_blogInfo->getId());
00188                                 if( !$category ) {
00189                                         $this->_setErrorView();
00190                                         return false;
00191                                 }
00192                                 // if there was a user, use his/her id
00193                                 $this->_categoryId = $category->getId();
00194                         }                       
00195                 
00196             // fetch the article
00197             // the article identifier can be either its internal id number or its mangled topic
00198                         $articles = new Articles();
00199             if( $this->_articleId ) { 
00200                 $article  = $articles->getBlogArticle( $this->_articleId, 
00201                                                        $this->_blogInfo->getId(), 
00202                                                        false, 
00203                                                        $this->_date, 
00204                                                        $this->_categoryId, 
00205                                                        $this->_userId,
00206                                                        POST_STATUS_PUBLISHED,
00207                                                        $this->_maxDate);
00208             } else {
00209                 $article  = $articles->getBlogArticleByTitle( $this->_articleName, 
00210                                                               $this->_blogInfo->getId(), 
00211                                                               false,
00212                                                               $this->_date, 
00213                                                               $this->_categoryId, 
00214                                                               $this->_userId,
00215                                                               POST_STATUS_PUBLISHED,
00216                                                               $this->_maxDate);
00217             }
00218 
00219             // if the article id doesn't exist, cancel the whole thing...
00220             if( !$article ) {
00221                 $this->_setErrorView();
00222                 return false;
00223             }
00224                         
00225             // check if we have to update how many times an article has been read
00226             if( $this->_config->getValue( "update_article_reads" )) {
00227                                 $this->updateNumArticleReads( $article->getId());
00228             }
00229                         
00230                         // update the referrers, if needed
00231                         $this->_updateArticleReferrers( $article );
00232                                                 
00233             // if everything's fine, we set up the article object for the view
00234             $this->_view->setArticle( $article );
00235             $this->setCommonData();
00236                         
00237             // and return everything normal
00238             return true;
00239         }
00240     }
00241 ?>