Пример #1
0
 /**
  * Remove template
  * @param Application_Model_Models_Template $template
  * @return mixed
  */
 public function delete(Application_Model_Models_Template $template)
 {
     return $this->getDbTable()->delete(array('name = ?' => $template->getName()));
 }
Пример #2
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;
 }
Пример #3
0
 /**
  * Method returns template editing screen
  * and saves edited template
  */
 public function templateAction()
 {
     $templateForm = new Application_Form_Template();
     $templateName = $this->getRequest()->getParam('id');
     $mapper = Application_Model_Mappers_TemplateMapper::getInstance();
     $currentTheme = $this->_helper->config->getConfig('currentTheme');
     if (!$this->getRequest()->isPost()) {
         $templateForm->getElement('pageId')->setValue($this->getRequest()->getParam('pid'));
         if ($templateName) {
             $template = $mapper->find($templateName);
             if ($template instanceof Application_Model_Models_Template) {
                 $templateForm->getElement('content')->setValue($template->getContent());
                 $templateForm->getElement('name')->setValue($template->getName());
                 $templateForm->getElement('id')->setValue($template->getName());
                 $templateForm->getElement('templateType')->setValue($template->getType());
                 $this->view->pagesUsingTemplate = Tools_Page_Tools::getPagesCountByTemplate($templateName);
             }
             //get template preview image
             try {
                 $templatePreviewDir = $this->_websiteConfig['path'] . $this->_themeConfig['path'] . $currentTheme . DIRECTORY_SEPARATOR . $this->_themeConfig['templatePreview'];
                 $images = Tools_Filesystem_Tools::findFilesByExtension($templatePreviewDir, '(jpg|gif|png)', false, true, false);
                 if (isset($images[$template->getName()])) {
                     $this->view->templatePreview = $this->_themeConfig['path'] . $currentTheme . '/' . $this->_themeConfig['templatePreview'] . $images[$template->getName()];
                 }
             } catch (Exceptions_SeotoasterException $se) {
                 $this->view->templatePreview = 'system/images/no_preview.png';
             }
         }
     } else {
         if ($templateForm->isValid($this->getRequest()->getPost())) {
             $templateData = $templateForm->getValues();
             $originalName = $templateData['id'];
             if ($templateData['templateType'] === Application_Model_Models_Template::TYPE_MOBILE || preg_match('~^mobile_~', $templateData['name'])) {
                 $isMobileTemplate = true;
                 $templateData['name'] = 'mobile_' . preg_replace('~^mobile_~', '', $templateData['name']);
                 $templateData['templateType'] = Application_Model_Models_Template::TYPE_MOBILE;
             } else {
                 $isMobileTemplate = false;
             }
             //check if we received 'id' in request and try to find existing template with this id
             /**
              * @var $template Application_Model_Models_Template
              */
             if (!empty($originalName) && null !== ($template = $mapper->find($originalName))) {
                 $status = 'update';
                 // avoid renaming of system protected templates
                 if (!in_array($template->getName(), $this->_protectedTemplates)) {
                     $template->setOldName($originalName);
                     $template->setName($templateData['name']);
                 } else {
                     // TODO throw error if trying to rename protected template
                 }
                 $template->setContent($templateData['content']);
             } else {
                 $status = 'new';
                 //if ID missing and name is not exists and name is not system protected - creating new template
                 if (in_array($templateData['name'], $this->_protectedTemplates) || null !== $mapper->find($templateData['name'])) {
                     $this->_helper->response->response($this->_translator->translate('Template with such name already exists'), true);
                 }
                 $template = new Application_Model_Models_Template($templateData);
             }
             $template->setType($templateData['templateType']);
             // saving/updating template in db
             $result = $mapper->save($template);
             if ($result) {
                 $this->_helper->cache->clean(false, false, array(preg_replace('/[^\\w\\d_]/', '', $template->getName())));
             }
             // saving to file in theme folder
             $currentThemePath = realpath($this->_websiteConfig['path'] . $this->_themeConfig['path'] . $currentTheme);
             $filepath = $currentThemePath . DIRECTORY_SEPARATOR;
             if ($isMobileTemplate) {
                 if (!is_dir($filepath . 'mobile')) {
                     Tools_Filesystem_Tools::mkDir($filepath . 'mobile');
                 }
                 $filepath .= preg_replace('~^mobile_~', 'mobile' . DIRECTORY_SEPARATOR, $template->getName());
             } else {
                 $filepath .= $templateData['name'];
             }
             $filepath .= '.html';
             try {
                 if ($filepath) {
                     Tools_Filesystem_Tools::saveFile($filepath, $templateData['content']);
                 }
                 if ($status === 'update' && $template->getOldName() !== $template->getName()) {
                     $oldFilename = $currentThemePath . DIRECTORY_SEPARATOR;
                     if ($isMobileTemplate) {
                         $oldFilename .= preg_replace('~^mobile_~', 'mobile' . DIRECTORY_SEPARATOR, $template->getOldName());
                     } else {
                         $oldFilename .= $template->getOldName();
                     }
                     $oldFilename .= '.html';
                     if (is_file($oldFilename)) {
                         if (false === Tools_Filesystem_Tools::deleteFile($oldFilename)) {
                         }
                     }
                     unset($oldFilename);
                 }
             } catch (Exceptions_SeotoasterException $e) {
                 Tools_System_Tools::debugMode() && error_log($e->getMessage());
             }
             $this->_helper->cache->clean(Helpers_Action_Cache::KEY_PLUGINTABS, Helpers_Action_Cache::PREFIX_PLUGINTABS);
             $this->_helper->response->response($status, false);
         } else {
             $errorMessages = array();
             $validationErrors = $templateForm->getErrors();
             $messages = array('name' => array('isEmpty' => 'Template name field can\'t be empty.', 'notAlnum' => 'Template name contains characters which are non alphabetic and no digits', 'stringLengthTooLong' => 'Template name field is too long.', 'stringLengthTooShort' => 'Template name field is too short.'), 'content' => array('isEmpty' => 'Content can\'t be empty.'));
             foreach ($validationErrors as $element => $errors) {
                 if (empty($errors)) {
                     continue;
                 }
                 foreach ($messages[$element] as $n => $message) {
                     if (in_array($n, $errors)) {
                         array_push($errorMessages, $message);
                     }
                 }
             }
             $this->_helper->response->response($errorMessages, true);
         }
     }
     $this->view->helpSection = 'addtemplate';
     $this->view->templateForm = $templateForm;
 }