Beispiel #1
0
 private function _applyTemplates($themeName, $remove = false)
 {
     $themePath = $this->_websiteHelper->getPath() . $this->_themesConfig['path'] . $themeName . DIRECTORY_SEPARATOR;
     $themeFiles = glob($themePath . '{,mobile/}*.html', GLOB_BRACE);
     if ($themeFiles !== false) {
         $themeFiles = array_map(function ($file) use($themePath) {
             return str_replace($themePath, '', $file);
         }, $themeFiles);
     }
     $themeConfig = false;
     $errors = array();
     //check we are not missing any required template
     foreach ($this->_protectedTemplates as $template) {
         if (!in_array($template . '.html', $themeFiles)) {
             array_push($errors, $this->_translator->translate('Theme missing template: ') . $template);
         }
     }
     if (!empty($errors)) {
         $this->_error(join('<br />', $errors), self::REST_STATUS_BAD_REQUEST);
     }
     //trying to get theme.ini file with templates presets
     try {
         $themeConfig = parse_ini_string(Tools_Filesystem_Tools::getFile($themePath . '/' . Tools_Template_Tools::THEME_CONFIGURATION_FILE));
     } catch (Exception $e) {
         $themeConfig = false;
     }
     $mapper = Application_Model_Mappers_TemplateMapper::getInstance();
     $mapper->clearTemplates();
     // this will remove all templates except system required. @see $_protectedTemplates
     $templateTypeTable = new Application_Model_DbTable_TemplateType();
     foreach ($themeFiles as $templateFile) {
         $templateName = preg_replace(array('~' . DIRECTORY_SEPARATOR . '~', '~\\.html$~'), array('_', ''), $templateFile);
         $template = $mapper->find($templateName);
         if (!$template instanceof Application_Model_Models_Template) {
             $template = new Application_Model_Models_Template();
             $template->setName($templateName);
         }
         // checking if we have template type in theme.ini or page meet mobile template naming convention
         if (is_array($themeConfig) && !empty($themeConfig) && array_key_exists($templateName, $themeConfig)) {
             $templateType = $themeConfig[$templateName];
         } elseif (preg_match('~^mobile' . DIRECTORY_SEPARATOR . '~', $templateFile)) {
             $templateType = Application_Model_Models_Template::TYPE_MOBILE;
         }
         if (isset($templateType)) {
             // checking if we have this type in db or adding it
             $checkTypeExists = $templateTypeTable->find($templateType);
             if (!$checkTypeExists->count()) {
                 $checkTypeExists = $templateTypeTable->createRow(array('id' => $templateType, 'title' => ucfirst(preg_replace('/^type/ui', '', $templateType)) . ' Template'));
                 $checkTypeExists->save();
             }
             unset($checkTypeExists);
             $template->setType($templateType);
         }
         // getting template content
         try {
             $template->setContent(Tools_Filesystem_Tools::getFile($themePath . DIRECTORY_SEPARATOR . $templateFile));
         } catch (Exceptions_SeotoasterException $e) {
             array_push($errors, 'Can\'t read template file: ' . $templateName);
         }
         // saving template to db
         $mapper->save($template);
         unset($template, $templateName);
     }
     unset($templateTypeTable);
     //updating config table
     Application_Model_Mappers_ConfigMapper::getInstance()->save(array('currentTheme' => $themeName));
     if (!empty($errors)) {
         $this->_error(join('<br />', $errors), self::REST_STATUS_BAD_REQUEST);
     }
     return true;
 }
 public function fetchAllTypes()
 {
     $dbTable = new Application_Model_DbTable_TemplateType();
     return $dbTable->getAdapter()->fetchPairs($dbTable->select()->order('title ASC'));
 }