From 5aa1aa8006630ead1717344617dc91cf17f480b0 Mon Sep 17 00:00:00 2001 From: LouisGac Date: Wed, 18 Oct 2017 17:41:27 +0200 Subject: [PATCH] Dev: updated 'Rename Template' functionality --- application/controllers/admin/templates.php | 39 ++++++------ application/models/Template.php | 20 ++++++- application/models/TemplateConfig.php | 1 + application/models/TemplateConfiguration.php | 12 ++++ application/models/TemplateManifest.php | 62 ++++++++++++++++++++ 5 files changed, 114 insertions(+), 20 deletions(-) diff --git a/application/controllers/admin/templates.php b/application/controllers/admin/templates.php index a73461f0042..4ab2db12f3d 100644 --- a/application/controllers/admin/templates.php +++ b/application/controllers/admin/templates.php @@ -411,42 +411,45 @@ public function templatefiledelete() */ public function templaterename() { - if (!Permission::model()->hasGlobalPermission('templates','update')) - { + if (!Permission::model()->hasGlobalPermission('templates','update')){ die('No permission'); } + if (returnGlobal('action') == "templaterename" && returnGlobal('newname') && returnGlobal('copydir')) { $sOldName = sanitize_dirname(returnGlobal('copydir')); $sNewName = sanitize_dirname(returnGlobal('newname')); $sNewDirectoryPath = Yii::app()->getConfig('usertemplaterootdir') . "/" . $sNewName; $sOldDirectoryPath = Yii::app()->getConfig('usertemplaterootdir') . "/" . returnGlobal('copydir'); - if (isStandardTemplate(returnGlobal('newname'))) - { + + if (isStandardTemplate(returnGlobal('newname'))){ Yii::app()->user->setFlash('error',sprintf(gT("Template could not be renamed to `%s`.", "js"), $sNewName) . " " . gT("This name is reserved for standard template.", "js")); $this->getController()->redirect(array("admin/templates/sa/upload")); - } - elseif (file_exists($sNewDirectoryPath)) - { + }elseif (file_exists($sNewDirectoryPath)){ Yii::app()->user->setFlash('error',sprintf(gT("Template could not be renamed to `%s`.", "js"), $sNewName) . " " . gT("A template with that name already exists.", "js")); $this->getController()->redirect(array("admin/templates/sa/upload")); - } - elseif (rename($sOldDirectoryPath, $sNewDirectoryPath) == false) - { + }elseif (rename($sOldDirectoryPath, $sNewDirectoryPath) == false){ Yii::app()->user->setFlash('error',sprintf(gT("Template could not be renamed to `%s`.", "js"), $sNewName) . " " . gT("Maybe you don't have permission.", "js")); $this->getController()->redirect(array("admin/templates/sa/upload")); - } - else - { - Survey::model()->updateAll(array( 'template' => $sNewName ), "template = :oldname", array(':oldname'=>$sOldName)); - if ( getGlobalSetting('defaulttemplate')==$sOldName) - { - setGlobalSetting('defaulttemplate',$sNewName); + }else{ + + $oTemplate = Template::model()->findByAttributes(array('name' => $sOldName)); + + if ( is_a($oTemplate, 'Template') ){ + $oTemplate->renameTo($sNewName); + if ( getGlobalSetting('defaulttemplate')==$sOldName){ + setGlobalSetting('defaulttemplate',$sNewName); + } + + $this->getController()->redirect(array('admin/templates','sa'=>'view','editfile'=>'layout_first_page.twig','screenname'=>'welcome','templatename'=>$sNewName)); + }else{ + Yii::app()->user->setFlash('error',sprintf(gT("Template `%s` could not be found.", "js"), $sOldName)); } - $this->index("startpage.pstpl", "welcome", $sNewName); + + $this->getController()->redirect(array('admin/templateoptions')); } } } diff --git a/application/models/Template.php b/application/models/Template.php index 8452549348a..4ca063aae7e 100644 --- a/application/models/Template.php +++ b/application/models/Template.php @@ -407,8 +407,7 @@ public static function getInstance($sTemplateName=null, $iSurveyId=null, $iSurve } if (empty(self::$instance)) { - // getTemplateConfiguration($sTemplateName=null, $iSurveyId=null, $iSurveyGroupId=null, $bForceXML=false) - self::$instance = $toto = self::getTemplateConfiguration($sTemplateName, $iSurveyId, $iSurveyGroupId, $bForceXML); + self::$instance = self::getTemplateConfiguration($sTemplateName, $iSurveyId, $iSurveyGroupId, $bForceXML); self::$instance->prepareTemplateRendering($sTemplateName, $iSurveyId); } @@ -496,6 +495,23 @@ public static function getUploadTemplates() return self::$aTemplatesInUploadDir; } + + /** + * Change the template name inside DB and the manifest (called from template editor) + * NOTE: all tests (like template exist, etc) are done from template controller. + * + * @param string $sNewName The newname of the template + */ + public function renameTo($sNewName) + { + Yii::import('application.helpers.sanitize_helper', true); + $sNewName = sanitize_paranoid_string($sNewName); + Survey::model()->updateAll(array( 'template' => $sNewName ), "template = :oldname", array(':oldname'=>$this->name)); + Template::model()->updateAll(array( 'name' => $sNewName, 'folder' => $sNewName ), "name = :oldname", array(':oldname'=>$this->name)); + TemplateConfiguration::rename($this->name, $sNewName); + TemplateManifest::rename($this->name,$sNewName); + } + /** * Retrieves a list of models based on the current search/filter conditions. * diff --git a/application/models/TemplateConfig.php b/application/models/TemplateConfig.php index f6b573d22e4..d930419ad5a 100644 --- a/application/models/TemplateConfig.php +++ b/application/models/TemplateConfig.php @@ -852,6 +852,7 @@ public static function importManifest($sTemplateName, $aDatas) // Then, the lonely differences between TemplateManifest and TemplateConfiguration should be how to retreive and format the data // Note: signature are already the same + public static function rename($sOldName,$sNewName){} public function prepareTemplateRendering($sTemplateName='', $iSurveyId='', $bUseMagicInherit=true){} public function addFileReplacement($sFile, $sType){} diff --git a/application/models/TemplateConfiguration.php b/application/models/TemplateConfiguration.php index 0ccb72a31bd..ba608f358ef 100644 --- a/application/models/TemplateConfiguration.php +++ b/application/models/TemplateConfiguration.php @@ -774,6 +774,18 @@ public function getTemplatesWithNoDb() return $aTemplatesWithoutDB; } + /** + * Change the template name inside the configuration entries (called from template editor) + * NOTE: all tests (like template exist, etc) are done from template controller. + * + * @param string $sOldName The old name of the template + * @param string $sNewName The newname of the template + */ + public static function rename($sOldName,$sNewName) + { + self::model()->updateAll(array( 'template_name' => $sNewName ), "template_name = :oldname", array(':oldname'=>$sOldName)); + } + public function getAllDbTemplateFolders() { if (empty($this->allDbTemplateFolders)){ diff --git a/application/models/TemplateManifest.php b/application/models/TemplateManifest.php index f0924e0380f..e24bc93fdb1 100644 --- a/application/models/TemplateManifest.php +++ b/application/models/TemplateManifest.php @@ -293,8 +293,70 @@ public static function importManifest($sTemplateName, $aDatas=array() ) return parent::importManifest($sTemplateName, $aDatas ); } + /** + * Get the DOMDocument of the Manifest + * @param string $sConfigPath path where to find the manifest + * @return DOMDocument + */ + public static function getManifestDOM($sConfigPath) + { + // First we get the XML file + $oNewManifest = new DOMDocument(); + $oNewManifest->load($sConfigPath."/config.xml"); + return $oNewManifest; + } + /** + * Change the name inside the DOMDocument (will not save it) + * @param DOMDocument $oNewManifest The DOMDOcument of the manifest + * @param string $sName The wanted name + */ + public static function changeNameInDOM($oNewManifest, $sName) + { + $oConfig = $oNewManifest->getElementsByTagName('config')->item(0); + $oMetadatas = $oConfig->getElementsByTagName('metadatas')->item(0); + $oOldNameNode = $oMetadatas->getElementsByTagName('name')->item(0); + $oNvNameNode = $oNewManifest->createElement('name', $sName); + $oMetadatas->replaceChild($oNvNameNode, $oOldNameNode); + } + + /** + * Change the date inside the DOMDocument + * @param DOMDocument $oNewManifest The DOMDOcument of the manifest + * @param string $sDate The wanted date, if empty the current date with config time adjustment will be used + */ + public static function changeDateInDOM($oNewManifest, $sDate='') + { + $date = (empty($date))?dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust")):$date; + $oConfig = $oNewManifest->getElementsByTagName('config')->item(0); + $oMetadatas = $oConfig->getElementsByTagName('metadatas')->item(0); + $oOldDateNode = $oMetadatas->getElementsByTagName('creationDate')->item(0); + $oNvDateNode = $oNewManifest->createElement('creationDate', $sDate); + $oMetadatas->replaceChild($oNvDateNode, $oOldDateNode); + $oOldUpdateNode = $oMetadatas->getElementsByTagName('last_update')->item(0); + $oNvDateNode = $oNewManifest->createElement('last_update', $sDate); + $oMetadatas->replaceChild($oNvDateNode, $oOldUpdateNode); + } + + /** + * Change the template name inside the manifest (called from template editor) + * NOTE: all tests (like template exist, etc) are done from template controller. + * + * @param string $sOldName The old name of the template + * @param string $sNewName The newname of the template + */ + public static function rename($sOldName,$sNewName) + { + libxml_disable_entity_loader(false); + $sConfigPath = Yii::app()->getConfig('usertemplaterootdir') . "/" . $sNewName; + $oNewManifest = self::getManifestDOM($sConfigPath); + self::changeNameInDOM($oNewManifest, $sNewName); + self::changeDateInDOM($oNewManifest); + $oNewManifest->save($sConfigPath."/config.xml"); + libxml_disable_entity_loader(true); + } + /** * Update the config file of a given template so that it extends another one *