[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/action/admin/ -> adminaddtemplateaction.class.php (source)

   1  <?php
   2  
   3      lt_include( PLOG_CLASS_PATH.'class/action/admin/siteadminaction.class.php' );
   4      lt_include( PLOG_CLASS_PATH.'class/view/admin/adminsitetemplateslistview.class.php' );
   5      lt_include( PLOG_CLASS_PATH.'class/view/admin/admintemplatedview.class.php' );
   6      lt_include( PLOG_CLASS_PATH.'class/file/unpacker/unpacker.class.php' );
   7      lt_include( PLOG_CLASS_PATH.'class/data/validator/stringvalidator.class.php' );
   8      lt_include( PLOG_CLASS_PATH.'class/template/templatesandbox.class.php' );
   9      lt_include( PLOG_CLASS_PATH.'class/file/fileuploads.class.php' );
  10      lt_include( PLOG_CLASS_PATH.'class/template/templatesets/templatesetstorage.class.php' );
  11      lt_include( PLOG_CLASS_PATH.'class/template/templatesets/templatefinder.class.php' );
  12      lt_include( PLOG_CLASS_PATH.'class/data/validator/uploadvalidator.class.php' );
  13  
  14      /**
  15       * \ingroup Action
  16       * @private
  17       *
  18       * Shows a form to add a new template file
  19       */
  20      class AdminAddTemplateAction extends AdminAction
  21      {
  22  
  23          function AdminAddTemplateAction( $actionInfo, $request )
  24          {
  25              $this->AdminAction( $actionInfo, $request );
  26  
  27              // decide what to do based on which submit button was pressed
  28              if( $this->_request->getValue( "addTemplateUpload" ) != "" )
  29                  $this->_op = "addTemplateUpload";
  30              else
  31                  $this->_op = "scanTemplates";
  32  
  33              $this->requireAdminPermission( "add_template" );
  34          }
  35  
  36          /**
  37           * @private
  38           */
  39          function _checkTemplateSandboxResult( $result )
  40          {
  41              switch( $result ) {
  42                  case ERROR_TEMPLATE_NOT_INSIDE_FOLDER:
  43                      $errorMessage = $this->_locale->tr('error_template_not_inside_folder');
  44                      break;
  45                  case ERROR_MISSING_BASE_FILES:
  46                      $errorMessage =  $this->_locale->tr('error_missing_base_files');
  47                      break;
  48                  case TEMPLATE_SANDBOX_ERROR_UNPACKING:
  49                      $errorMessage =  $this->_locale->tr('error_unpacking');
  50                      break;
  51                  case TEMPLATE_SANDBOX_ERROR_FORBIDDEN_EXTENSIONS:
  52                      $errorMessage =  $this->_locale->tr('error_forbidden_extensions');
  53                      break;
  54                  case TEMPLATE_SANDBOX_ERROR_CREATING_WORKING_FOLDER:
  55                      $errorMessage = $this->_locale->tr('error_creating_working_folder');
  56                      break;
  57                  default:
  58                      $errorMessage = $this->_locale->pr('error_checking_template', $result);
  59                      break;
  60              }
  61  
  62              return $errorMessage;
  63          }
  64  
  65          /**
  66           * installs an uploaded template
  67           */
  68          function _performUploadTemplate()
  69          {
  70              // handle the uploaded file
  71              $files    = HttpVars::getFiles();
  72              $uploads  = new FileUploads( $files );
  73  
  74              if( count($files) == 0 || $files["templateFile"]["name"] == "") {
  75                  $this->_view = new AdminTemplatedView( $this->_blogInfo, "newglobaltemplate" );
  76                  $this->_view->setValue( "templateFolder", TemplateSetStorage::getBaseTemplateFolder());
  77                  $this->_view->setErrorMessage( $this->_locale->tr("error_must_upload_file"));
  78                  $this->setCommonData();
  79                  return false;
  80              }
  81  
  82              $config =& Config::getConfig();
  83  
  84              $tmpFolder = $config->getValue( 'temp_folder' );
  85  
  86              // move it to the temporary folder
  87              $result = $uploads->process( $tmpFolder );
  88  
  89              // and from there, unpack it
  90              $upload   = new FileUpload( $files['templateFile'] );
  91              $templateName = TemplateSandbox::toTemplateSetName( $upload->getFileName() );            
  92  
  93              // Check the template set exist or not
  94              if( TemplateSets::isTemplate( $templateName ) ) {
  95                  $this->_view = new AdminSiteTemplatesListView( $this->_blogInfo );
  96                  $this->_view->setErrorMessage( $this->_locale->pr("error_template_exist", $templateName));
  97                  $this->setCommonData();
  98  
  99                  return false;
 100              }
 101  
 102              $templateSandbox = new TemplateSandbox();
 103              $valid = $templateSandbox->checkTemplateSet( $upload->getFileName(), $tmpFolder.'/');
 104  
 105              if( $valid < 0 ) {
 106                  $this->_view = new AdminSiteTemplatesListView( $this->_blogInfo );
 107                  $this->_view->setErrorMessage( $this->_checkTemplateSandboxResult( $valid ));
 108                  $this->setCommonData();
 109                  return false;
 110              }
 111  
 112              // the template was ok, so then we can proceed and move it to the main
 113              // template folder, add it to our array of templates
 114  
 115              //
 116              // :KLUDGE:
 117              //
 118              // maybe we should simply move the files rather than unpacking the whole
 119              // thing again, but this indeed makes things easier! ;)
 120              $unpacker = new Unpacker();
 121              $templateFolder = $config->getValue( 'template_folder' );
 122              $fileToUnpack = $tmpFolder.'/'.$upload->getFileName();
 123              if( !$unpacker->unpack( $fileToUnpack, $templateFolder )) {
 124                  $this->_view = new AdminSiteTemplatesListView( $this->_blogInfo );
 125                  $tf = new Textfilter();
 126                  $this->_view->setErrorMessage( $this->_locale->pr('error_installing_template', $tf->filterAllHtml($upload->getFileName())));
 127                  $this->setCommonData();
 128                  return false;
 129              }
 130  
 131              // if the template set was installed ok in the template folder, we can record
 132              // it as a valid set
 133              $ts = new TemplateSetStorage();
 134              $ts->addTemplate( $templateName );
 135  
 136              $this->_view = new AdminSiteTemplatesListView( $this->_blogInfo );
 137              $this->_view->setSuccessMessage( $this->_locale->pr('template_installed_ok', $templateName));
 138              $this->setCommonData();
 139  
 140              return true;
 141          }
 142  
 143  
 144          //
 145          // adds a template manually
 146          //
 147          function _addTemplateCode( $templateName )
 148          {
 149  
 150              $ts = new TemplateSetStorage();
 151  
 152              // make sure that the template is valid
 153              $templateSandbox = new TemplateSandbox();
 154              $valid = $templateSandbox->checkTemplateFolder( $templateName, $ts->getBaseTemplateFolder());
 155              if( $valid < 0 ) {
 156                  $this->_errorMessage .= $this->_locale->pr( 'error_installing_template', $templateName ).': '.$this->_checkTemplateSandboxResult( $valid ).'<br/>';
 157                  $result = false;
 158              }
 159              else {
 160                  // otherwise, we can add it without problems
 161                  $ts->addTemplate( $templateName );
 162                  $this->_successMessage .= $this->_locale->pr( 'template_installed_ok', $templateName).'<br/>';
 163                  $result = true;
 164              }
 165  
 166              $this->setCommonData();
 167              return $result;
 168          }
 169  
 170          /**
 171           * scans the templates folder looking for new files
 172           *
 173           * @private
 174           */
 175  		function _performScanTemplateFolder()
 176          {
 177              // set up the view
 178              $this->_view = new AdminSiteTemplatesListView( $this->_blogInfo );
 179  
 180              // and tell the template finder to find any new template file...
 181              $tf = new TemplateFinder();
 182              $newTemplates = $tf->find();
 183  
 184              $this->_errorMessage = "";
 185              $this->_successMessage = "";
 186  
 187              if( count($newTemplates) == 0 ) {
 188                  // no new templates found
 189                  $this->_errorMessage = $this->_locale->tr( 'error_no_new_templates_found' );
 190              }
 191              else {
 192                  // now add each one of the new ones
 193                  foreach( $newTemplates as $newTemplate ) {
 194                      $this->_addTemplateCode( $newTemplate );
 195                  }
 196              }
 197  
 198              // set the success and error messages, if any
 199              if( $this->_errorMessage != '' ) $this->_view->setErrorMessage( $this->_errorMessage );
 200              if( $this->_successMessage != '' ) $this->_view->setSuccessMessage( $this->_successMessage );
 201  
 202              return true;
 203          }
 204  
 205          /**
 206           * perform the action
 207           */
 208          function perform()
 209          {
 210              if( $this->_op == 'addTemplateUpload' )
 211                  $result = $this->_performUploadTemplate();
 212              elseif( $this->_op == 'scanTemplates' )
 213                  $result = $this->_performScanTemplateFolder();
 214              else {
 215                  throw( new Exception( 'You shouldn\'t be seeing this!!! :)' ));
 216                  die();
 217              }
 218  
 219              $this->setCommonData();
 220  
 221              return $result;
 222          }
 223      }
 224  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics