NewsFeaturesDownloadsDevelopmentSupportAbout Us

lifetype/branches/lifetype-1.0.6/class/action/defaultaction.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/view/defaultview.class.php" );
00005     include_once( PLOG_CLASS_PATH."class/net/http/session/sessioninfo.class.php" );
00006     include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
00007     include_once( PLOG_CLASS_PATH."class/view/errorview.class.php" );
00008         include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" ); 
00009         include_once( PLOG_CLASS_PATH."class/data/validator/stringvalidator.class.php" );
00010         include_once( PLOG_CLASS_PATH."class/data/validator/integervalidator.class.php" );      
00011 
00018         class DefaultAction extends BlogAction 
00019         {
00020 
00021                 var $_config;
00022         var $_date;
00023         var $_categoryId;
00024                 var $_categoryName;
00025                 var $_userId;
00026                 var $_userName;
00027                 var $_postAmount;
00028 
00029                 function DefaultAction( $actionInfo, $request )
00030         {
00031                         $this->BlogAction( $actionInfo, $request );
00032                         
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_articles" ));
00039         }
00040 
00041         function validate()
00042         {
00043                         if( !parent::validate())
00044                                 return false;
00045         
00046             // value of the Date parameter from the request
00047             $this->_date = $this->_request->getValue( "Date", -1 );
00048                 $val = new IntegerValidator();
00049                 if( !$val->validate( $this->_date ) ) {
00050                 $this->_date = -1;
00051             }
00052 
00053                         $this->_categoryName = $this->_request->getValue( 'postCategoryName' );
00054             $this->_categoryId = $this->_request->getValue( 'postCategoryId' );
00055             if( $this->_categoryId == '' )
00056                 if( $this->_date == -1 )
00057                         $this->_categoryId = 0;
00058                 else
00059                         $this->_categoryId = -1;
00060                                         
00061                         $this->_userId = $this->_request->getValue( 'userId', -1 );
00062                         $this->_userName = $this->_request->getValue( 'userName' );
00063 
00064             return true;
00065         }
00066 
00070         function perform()
00071         {
00072                 // first of all, we have to determine which blog we would like to see
00073                         $blogId = $this->_blogInfo->getId();
00074 
00075             // fetch the settings for that blog
00076             $blogSettings = $this->_blogInfo->getSettings();
00077 
00078             // prepare the view
00079                 $this->_view = new DefaultView( $this->_blogInfo,
00080                                                         Array( "categoryId" => $this->_categoryId,
00081                                                                "blogId" => $this->_blogInfo->getId(),
00082                                                                "categoryName" => $this->_categoryName,
00083                                                            "date" => $this->_date,
00084                                                            "userName" => $this->_userName,
00085                                                            "userId" => $this->_userId ));
00086                                                                                                                   
00087                         // check if everything's cached because if it is, then we don't have to
00088                         // do any work... it's already been done before and we should "safely" assume
00089                         // that there hasn't been any change so far
00090                         if( $this->_view->isCached()) {
00091                                 return true;
00092                         }
00093 
00094             // if we got a category name instead of a category id, then we
00095             // should first look up this category in the database and see if
00096             // it exists
00097             $categories = new ArticleCategories();
00098             if( $this->_categoryName ) {
00099                 $category = $categories->getCategoryByName( $this->_categoryName, $this->_blogInfo->getId());
00100                 if( !$category ) {
00101                     $this->_view = new ErrorView( $this->_blogInfo );
00102                     $this->_view->setValue( 'message', "error_incorrect_category_id" );
00103                     $this->setCommonData();
00104                     return false;
00105                 }
00106                 
00107                 // if everything went fine...
00108                 $this->_categoryId = $category->getId();
00109             }
00110                         else {
00111                                 // we don't do anything if the cateogry id is '0' or '-1'
00112                                 if( $this->_categoryId > 0 ) {
00113                                         $category = $categories->getCategory( $this->_categoryId, $this->_blogInfo->getId());
00114                                         if( !$category ) {
00115                                                 $this->_view = new ErrorView( $this->_blogInfo );
00116                                                 $this->_view->setValue( 'message', "error_incorrect_category_id" );
00117                                                 $this->setCommonData();
00118                                                 return false;
00119                                         }
00120                                 }
00121                         }
00122                         
00123                         // export the category object in case it is needed
00124             if( isset($category) )
00125                 $this->_view->setValue( "category", $category );                        
00126                         
00127 
00128             $users = new Users();
00129 
00130             // if we got a user user id, then we should first look up this id
00131             // user in the database and see if it exists
00132             if( $this->_userId > 0) {
00133                 $user = $users->getUserInfoFromId( $this->_userId );
00134                 if( !$user ) {
00135                     $this->_view = new ErrorView( $this->_blogInfo );
00136                     $this->_view->setValue( 'message', 'error_incorrect_user_id' );
00137                     $this->setCommonData();
00138                     return false;
00139                 }
00140             } 
00141             else if( $this->_userName ) {
00142                     // if we got a user name instead of a user id, then we
00143                     // should first look up this user in the database and see if
00144                     // it exists
00145                 $user = $users->getUserInfoFromUsername( $this->_userName );
00146                 if( !$user ) {
00147                     $this->_view = new ErrorView( $this->_blogInfo );
00148                     $this->_view->setValue( 'message', 'error_incorrect_user_username' );
00149                     $this->setCommonData();
00150                     return false;
00151                 }
00152                 
00153                 // if everything went fine...
00154                 $this->_userId = $user->getId();
00155             }
00156 
00157             // export the owner. The owner information should get from blogInfo directly
00158             $this->_view->setValue( "owner", $this->_blogInfo->getOwnerInfo() );
00159                         
00160             $t = new Timestamp();
00161             $todayTimestamp = $t->getTimestamp();
00162                         
00163             // amount of posts that we have to show, but keeping in mind that when browsing a
00164             // category or specific date, we should show *all* of them
00165             if( $this->_date > 0 || $this->_categoryId > 0 ) {
00166                 $this->_postAmount = -1;
00167                 // also, inform the template that we're showing them all!
00168                 $this->_view->setValue( 'showAll', true );
00169             }
00170             else {
00171                 $this->_postAmount = $blogSettings->getValue( 'show_posts_max' );
00172                 $this->_view->setValue( 'showAll', false );
00173             }
00174                         
00175             //
00176             // :KLUDGE:
00177             // the more things we add here to filter, the more complicated this function
00178             // gets... look at this call and look at how many parameters it needs!! :(
00179             //
00180                         
00181                         if( ($blogSettings->getValue( 'show_future_posts_in_calendar')) && ( $this->_date > -1 )) {
00182                                 // if posts in the future are to be shown, we shouldn't set a maximum date
00183                                 $blogArticles = $this->articles->getBlogArticles( $blogId, $this->_date,
00184                                                                 $this->_postAmount, $this->_categoryId,
00185                                                                 POST_STATUS_PUBLISHED, $this->_userId );                        
00186                         }
00187                         else {
00188                                 $blogArticles = $this->articles->getBlogArticles( $blogId, $this->_date,
00189                                                                 $this->_postAmount, $this->_categoryId,
00190                                                                 POST_STATUS_PUBLISHED, $this->_userId, $todayTimestamp );
00191                         }
00192 
00193             // if we couldn't fetch the articles, send an error and quit
00194             if( count($blogArticles) == 0 ) {
00195                 $this->_view = new ErrorView( $this->_blogInfo );
00196                 $this->_view->setValue( 'message', 'error_fetching_articles' );
00197             }
00198             else {
00199                     // otherwise, continue
00200                 // the view will take care of cutting the post if we have the "show more"
00201                 // feature enabled or not... we could do it here but I think that belongs
00202                 // to the view since it is presentation stuff... It could also be handled
00203                 // by the template but then it'd make the template a little bit more
00204                 // complicated...
00205                                 
00206                                 // ---
00207                                 // before finishing, let's see if there's any plugin that would like to do 
00208                                 // anything with the post that we just loaded
00209                                 // ---
00210                                 $pm =& PluginManager::getPluginManager();
00211                                 $pm->setBlogInfo( $this->_blogInfo );
00212                                 $pm->setUserInfo( $this->_userInfo );
00213                                 $result = $pm->notifyEvent( EVENT_POSTS_LOADED, Array( 'articles' => &$blogArticles ));
00214                                 $articles = Array();
00215                                 foreach( $blogArticles as $article ) {
00216                                         $postText = $article->getIntroText();
00217                                         $postExtendedText = $article->getExtendedText();
00218                                         $pm->notifyEvent( EVENT_TEXT_FILTER, Array( "text" => &$postText ));
00219                                         $pm->notifyEvent( EVENT_TEXT_FILTER, Array( "text" => &$postExtendedText ));
00220                                         $article->setIntroText( $postText );
00221                                         $article->setExtendedText( $postExtendedText );
00222                                         array_push( $articles, $article );
00223                                 }
00224                                 
00225                 $this->_view->setValue( 'posts', $articles );
00226             }
00227 
00228             $this->setCommonData();
00229             // save the information about the session for later
00230             $this->saveSession();
00231 
00232             return true;
00233         }
00234     }
00235 ?>