NewsFeaturesDownloadsDevelopmentSupportAbout Us

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

Go to the documentation of this file.
00001 <?php
00002 
00003     include_once( PLOG_CLASS_PATH."class/action/admin/blogowneradminaction.class.php" );
00004     include_once( PLOG_CLASS_PATH."class/action/admin/adminaddtemplateaction.class.php" );
00005     include_once( PLOG_CLASS_PATH."class/file/unpacker/unpacker.class.php" );
00006         include_once( PLOG_CLASS_PATH."class/view/admin/admintemplatedview.class.php" );
00007         include_once( PLOG_CLASS_PATH."class/view/admin/adminblogtemplatesetslistview.class.php" );
00008         include_once( PLOG_CLASS_PATH."class/template/templatesets/templatesetstorage.class.php" );
00009     include_once( PLOG_CLASS_PATH."class/template/templatesandbox.class.php" );
00010     include_once( PLOG_CLASS_PATH."class/file/fileuploads.class.php" );
00011 
00018     class AdminAddBlogTemplateAction extends BlogOwnerAdminAction
00019         {
00020 
00021                 var $_op;
00022 
00023         function AdminAddBlogTemplateAction( $actionInfo, $request )
00024         {
00025             $this->BlogOwnerAdminAction( $actionInfo, $request );
00026 
00027             // decide what to do based on which submit button was pressed
00028             if( $this->_request->getValue( "addBlogTemplate" ) != "" )
00029                 $this->_op = "addBlogTemplate";
00030             else
00031                 $this->_op = "scanBlogTemplates";
00032         }
00033 
00034         function validate()
00035         {
00036             //
00037             // first of all, let's make sure that users are allowed to
00038             // add new templates
00039             //
00040             $config =& Config::getConfig();
00041             if( !$config->getValue( "users_can_add_templates" )) {
00042                 $this->_view = new AdminBlogTemplateSetsListView( $this->_blogInfo );
00043                 $this->_view->setErrorMessage( $this->_locale->tr("error_add_template_disabled"));
00044                 $this->setCommonData();
00045 
00046                 return false;
00047             }
00048 
00049                         return parent::validate();
00050                 }
00051 
00052         function _performUploadTemplate()
00053         {
00054             // get the temporary folder
00055             $config =& Config::getConfig();
00056             $tmpFolder = $config->getValue( "temp_folder" );
00057 
00058             // move it to the temporary folder
00059             $files    = HttpVars::getFiles();
00060 
00061             if( count($files) == 0 || $files["templateFile"]["name"] == "") {
00062                                 $this->_view = new AdminTemplatedView( $this->_blogInfo, "newblogtemplate" );
00063                                 $this->_view->setValue( "templateFolder", TemplateSetStorage::getBlogBaseTemplateFolder( $this->_blogInfo->getId()));
00064                 $this->_view->setErrorMessage( $this->_locale->tr("error_must_upload_file"));
00065                 $this->setCommonData();
00066                 return false;
00067                         }
00068 
00069             $uploads  = new FileUploads( $files );
00070 
00071             $result = $uploads->process( $tmpFolder );
00072             if( $result < 0 ) {
00073                 $this->_view = new AdminBlogTemplateSetsListView( $this->_blogInfo );
00074                 $this->_view->setErrorMessage( $this->_locale->tr("error_uploads_disabled"));
00075                 $this->setCommonData();
00076 
00077                 return false;
00078             }
00079 
00080             $upload   = new FileUpload( $files["templateFile"] );
00081 
00082             // and make it go through the template sandbox to check if
00083             // we're dealing with a 'healthy' file
00084             $templateSandbox = new TemplateSandbox();
00085             $valid = $templateSandbox->checkTemplateSet( $upload->getFileName(), $tmpFolder."/");
00086 
00087             if( $valid < 0 ) {
00088                 $this->_view = new AdminBlogTemplateSetsListView( $this->_blogInfo );
00089                 $this->_view->setErrorMessage( AdminAddTemplateAction::_checkTemplateSandboxResult( $valid ));
00090                 $this->setCommonData();
00091 
00092                 return false;
00093             }
00094 
00095             //
00096             // :KLUDGE:
00097             //
00098             // maybe we should simply move the files rather than unpacking the whole
00099             // thing again, but this indeed makes things easier! ;)
00100             //
00101 
00102             // since it is a local template, the path has to be $template_folder/blog_x/$templateName
00103             $ts = new TemplateSetStorage();
00104             $blogTemplateFolder = $ts->createBlogTemplateFolder( $this->_blogInfo->getId());
00105 
00106             // it should be there now... we can continue
00107             $destFolder = $blogTemplateFolder."/";
00108 
00109             $unpacker = new Unpacker();
00110             if( !$unpacker->unpack( $tmpFolder."/".$upload->getFileName(), $destFolder )) {
00111                 $this->_view = new AdminBlogTemplateSetsListView( $this->_blogInfo );
00112                 $this->_view->setErrorMessage( $this->_locale->tr("error_installing_template"));
00113                 $this->setCommonData();
00114 
00115                 // remove the file before returning!
00116                 File::delete( $tmpFolder."/".$upload->getFileName());
00117 
00118                 return false;
00119             }
00120 
00121             // if the template set was installed ok in the template folder, we can record
00122             // it as a valid set
00123                         $templateName = TemplateSandbox::toTemplateSetName( $upload->getFileName() );
00124             $ts->addTemplate( $templateName, $this->_blogInfo->getId());
00125 
00126             // remove the file
00127             File::delete( $tmpFolder."/".$upload->getFileName());
00128 
00129                         $this->_view = new AdminBlogTemplateSetsListView( $this->_blogInfo );
00130             $this->_view->setSuccessMessage( $this->_locale->pr("template_installed_ok", $templateName));
00131             $this->setCommonData();
00132 
00133             return true;
00134         }
00135 
00136 
00140                 function _addTemplateCode( $templateName )
00141         {
00142             $config =& Config::getConfig();
00143             $templateFolder = $config->getValue( "template_folder" );
00144 
00145             $ts = new TemplateSetStorage();
00146 
00147             $this->_view = new AdminBlogTemplateSetsListView( $this->_blogInfo );
00148 
00149             // make sure that the template is valid
00150             $templateSandbox = new TemplateSandbox();
00151             $valid = $templateSandbox->checkTemplateFolder( $templateName, $ts->getBlogBaseTemplateFolder( $this->_blogInfo->getId()));
00152                         if( $valid < 0 ) {
00153                                 $this->_errorMessage = $this->_locale->pr( 'error_installing_template', $templateName ).': '.AdminAddTemplateAction::_checkTemplateSandboxResult( $valid ).'<br/>';
00154                                 $result = false;
00155                         }
00156             else {
00157                 // otherwise, we can add it without problems
00158                 $ts->addTemplate( $templateName, $this->_blogInfo->getId());
00159                     $this->_successMessage =  $this->_locale->pr( "template_installed_ok", $templateName );
00160                     $result = true;
00161             }
00162 
00163                         $this->setCommonData();
00164             return $result;
00165         }
00166                 
00172                 function _performScanTemplateFolder()
00173                 {
00174                         $this->_errorMessage = "";
00175                         $this->_successMessage = "";            
00176                 
00177                         // set up the view
00178             $this->_view = new AdminBlogTemplateSetsListView( $this->_blogInfo );               
00179                 
00180                         // and tell the template finder to find any new template file...
00181                         $tf = new TemplateFinder( TemplateSetStorage::getBlogBaseTemplateFolder( $this->_blogInfo->getId()));
00182                         $newTemplates = $tf->find( TemplateSets::getBlogTemplates( $this->_blogInfo->getId()));
00183                         
00184                         $this->_errorMessage = "";
00185                         $this->_successMessage = "";
00186                         
00187                         if( count($newTemplates) == 0 ) {
00188                                 // no new templates found
00189                                 $this->_errorMessage = $this->_locale->tr( 'error_no_new_templates_found' );
00190                         }
00191                         else {
00192                                 // now add each one of the new ones
00193                                 foreach( $newTemplates as $newTemplate ) {
00194                                         $this->_addTemplateCode( $newTemplate );
00195                                 }
00196                         }
00197                         
00198                         // set the success and error messages, if any
00199                         if( $this->_errorMessage != '' ) $this->_view->setErrorMessage( $this->_errorMessage );
00200                         if( $this->_successMessage != '' ) $this->_view->setSuccessMessage( $this->_successMessage );
00201                         
00202                         $this->setCommonData();
00203                         
00204                         return true;
00205                 }               
00206 
00207         function perform()
00208         {
00209             if( $this->_op == "addBlogTemplate" ) {
00210                 $result = $this->_performUploadTemplate();
00211                         }
00212             elseif( $this->_op == "scanBlogTemplates" ) {
00213                 $result = $this->_performScanTemplateFolder();
00214                         }
00215             else {
00216                 throw( new Exception( "You shouldn't be seeing this!!! :)" ));
00217                 die();
00218             }
00219 
00220             // We should update the session too, or we will get data dirty
00221             include_once( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
00222             $blogs = new Blogs();
00223             $blogInfo = $blogs->getBlogInfo( $this->_blogInfo->getId() );
00224             $this->_session->setValue( "blogInfo", $blogInfo );
00225             $this->saveSession();
00226             
00227             return $result;
00228         }
00229     }
00230 ?>