Skip to content

Commit

Permalink
Dev: updated 'Rename Template' functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisGac committed Oct 18, 2017
1 parent 4ced966 commit 5aa1aa8
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 20 deletions.
39 changes: 21 additions & 18 deletions application/controllers/admin/templates.php
Expand Up @@ -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'));
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions application/models/Template.php
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions application/models/TemplateConfig.php
Expand Up @@ -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){}

Expand Down
12 changes: 12 additions & 0 deletions application/models/TemplateConfiguration.php
Expand Up @@ -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)){
Expand Down
62 changes: 62 additions & 0 deletions application/models/TemplateManifest.php
Expand Up @@ -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
*
Expand Down

0 comments on commit 5aa1aa8

Please sign in to comment.