NewsNewsFeaturesDownloadsDevelopmentSupportForumDocumentsAbout Us

adminaddblogtemplateaction.class.php

查看本檔案說明文件.
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             $fileParts = explode( ".", $upload->getFileName());
00124             $templateName = $fileParts[0];
00125             $ts->addTemplate( $templateName, $this->_blogInfo->getId());
00126 
00127             // remove the file
00128             File::delete( $tmpFolder."/".$upload->getFileName());
00129 
00130             $this->_view = new AdminBlogTemplateSetsListView( $this->_blogInfo );
00131             $this->_view->setSuccessMessage( $this->_locale->pr("template_installed_ok", $templateName));
00132             $this->setCommonData();
00133 
00134             return true;
00135         }
00136 
00137 
00141         function _addTemplateCode( $templateName )
00142         {
00143             $config =& Config::getConfig();
00144             $templateFolder = $config->getValue( "template_folder" );
00145 
00146             $ts = new TemplateSetStorage();
00147 
00148             $this->_view = new AdminBlogTemplateSetsListView( $this->_blogInfo );
00149 
00150             // make sure that the template is valid
00151             $templateSandbox = new TemplateSandbox();
00152             $valid = $templateSandbox->checkTemplateFolder( $templateName, $ts->getBlogBaseTemplateFolder( $this->_blogInfo->getId()));
00153             if( $valid < 0 ) {
00154                 $this->_errorMessage = $this->_locale->pr( 'error_installing_template', $templateName ).': '.AdminAddTemplateAction::_checkTemplateSandboxResult( $valid ).'<br/>';
00155                 $result = false;
00156             }
00157             else {
00158                 // otherwise, we can add it without problems
00159                 $ts->addTemplate( $templateName, $this->_blogInfo->getId());
00160                 $this->_successMessage =  $this->_locale->pr( "template_installed_ok", $templateName );
00161                 $result = true;
00162             }
00163 
00164             $this->setCommonData();
00165             return $result;
00166         }
00167         
00173         function _performScanTemplateFolder()
00174         {
00175             $this->_errorMessage = "";
00176             $this->_successMessage = "";        
00177         
00178             // set up the view
00179             $this->_view = new AdminBlogTemplateSetsListView( $this->_blogInfo );       
00180         
00181             // and tell the template finder to find any new template file...
00182             $tf = new TemplateFinder( TemplateSetStorage::getBlogBaseTemplateFolder( $this->_blogInfo->getId()));
00183             $newTemplates = $tf->find( TemplateSets::getBlogTemplates( $this->_blogInfo->getId()));
00184             
00185             $this->_errorMessage = "";
00186             $this->_successMessage = "";
00187             
00188             if( count($newTemplates) == 0 ) {
00189                 // no new templates found
00190                 $this->_errorMessage = $this->_locale->tr( 'error_no_new_templates_found' );
00191             }
00192             else {
00193                 // now add each one of the new ones
00194                 foreach( $newTemplates as $newTemplate ) {
00195                     $this->_addTemplateCode( $newTemplate );
00196                 }
00197             }
00198             
00199             // set the success and error messages, if any
00200             if( $this->_errorMessage != '' ) $this->_view->setErrorMessage( $this->_errorMessage );
00201             if( $this->_successMessage != '' ) $this->_view->setSuccessMessage( $this->_successMessage );
00202             
00203             $this->setCommonData();
00204             
00205             return true;
00206         }       
00207 
00208         function perform()
00209         {
00210             if( $this->_op == "addBlogTemplate" ) {
00211                 $result = $this->_performUploadTemplate();
00212             }
00213             elseif( $this->_op == "scanBlogTemplates" ) {
00214                 $result = $this->_performScanTemplateFolder();
00215             }
00216             else {
00217                 throw( new Exception( "You shouldn't be seeing this!!! :)" ));
00218                 die();
00219             }
00220 
00221             return $result;
00222         }
00223     }
00224 ?>