Skip to content

Commit

Permalink
Dev: dryer code
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisGac committed Oct 18, 2017
1 parent 5aa1aa8 commit 160a7e9
Showing 1 changed file with 67 additions and 44 deletions.
111 changes: 67 additions & 44 deletions application/models/TemplateManifest.php
Expand Up @@ -289,7 +289,6 @@ public static function importManifest($sTemplateName, $aDatas=array() )
$aDatas['files_folder'] = (string) $oTemplate->config->engine->filesdirectory;
$aDatas['aOptions'] = (!empty($oTemplate->config->options[0]) && count($oTemplate->config->options[0]) == 0 )?array():$oTemplate->config->options[0]; // If template provide empty options, it must be cleaned to avoid crashes


return parent::importManifest($sTemplateName, $aDatas );
}

Expand Down Expand Up @@ -358,30 +357,12 @@ public static function rename($sOldName,$sNewName)
}

/**
* Update the config file of a given template so that it extends another one
* Delete files and engine node inside the DOM
*
* It will:
* 1. Delete files and engine nodes
* 2. Update the name of the template
* 3. Change the creation/modification date to the current date
* 4. Change the autor name to the current logged in user
* 5. Change the author email to the admin email
*
* Used in template editor
* Both templates and configuration files must exist before using this function
*
* It's used when extending a template from template editor
* @param string $sToExtends the name of the template to extend
* @param string $sNewName the name of the new template
* @param DOMDocument $oNewManifest The DOMDOcument of the manifest
*/
static public function extendsConfig($sToExtends, $sNewName)
public static function deleteFilesAndEngineInDom($oNewManifest)
{
$sConfigPath = Yii::app()->getConfig('usertemplaterootdir') . "/" . $sNewName;

// First we get the XML file
libxml_disable_entity_loader(false);
$oNewManifest = new DOMDocument();
$oNewManifest->load($sConfigPath."/config.xml");
$oConfig = $oNewManifest->getElementsByTagName('config')->item(0);

// Then we delete the nodes that should be inherit
Expand All @@ -392,45 +373,87 @@ static public function extendsConfig($sToExtends, $sNewName)
foreach($aNodesToDelete as $node){
$oConfig->removeChild($node);
}
}

// We replace the name by the new name
$oMetadatas = $oConfig->getElementsByTagName('metadatas')->item(0);

$oOldNameNode = $oMetadatas->getElementsByTagName('name')->item(0);
$oNvNameNode = $oNewManifest->createElement('name', $sNewName);
$oMetadatas->replaceChild($oNvNameNode, $oOldNameNode);

// We change the date
$today = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig("timeadjust"));
$oOldDateNode = $oMetadatas->getElementsByTagName('creationDate')->item(0);
$oNvDateNode = $oNewManifest->createElement('creationDate', $today);
$oMetadatas->replaceChild($oNvDateNode, $oOldDateNode);

$oOldUpdateNode = $oMetadatas->getElementsByTagName('last_update')->item(0);
$oNvDateNode = $oNewManifest->createElement('last_update', $today);
$oMetadatas->replaceChild($oNvDateNode, $oOldUpdateNode);

// We change the author name
/**
* Change author inside the DOM
*
* @param DOMDocument $oNewManifest The DOMDOcument of the manifest
*/
public static function changeAuthorInDom($oNewManifest)
{
$oConfig = $oNewManifest->getElementsByTagName('config')->item(0);
$oMetadatas = $oConfig->getElementsByTagName('metadatas')->item(0);
$oOldAuthorNode = $oMetadatas->getElementsByTagName('author')->item(0);
$oNvAuthorNode = $oNewManifest->createElement('author', Yii::app()->user->name);
$oMetadatas->replaceChild($oNvAuthorNode, $oOldAuthorNode);
}

// We change the author email
/**
* Change author email inside the DOM
*
* @param DOMDocument $oNewManifest The DOMDOcument of the manifest
*/
public static function changeEmailInDom($oNewManifest)
{
$oConfig = $oNewManifest->getElementsByTagName('config')->item(0);
$oMetadatas = $oConfig->getElementsByTagName('metadatas')->item(0);
$oOldMailNode = $oMetadatas->getElementsByTagName('authorEmail')->item(0);
$oNvMailNode = $oNewManifest->createElement('authorEmail', htmlspecialchars(getGlobalSetting('siteadminemail')));
$oMetadatas->replaceChild($oNvMailNode, $oOldMailNode);
}

// TODO: provide more datas in the post variable such as description, url, copyright, etc

// We add the extend parameter
/**
* Change the extends node inside the DOM
* If it doesn't exist, it will create it
* @param DOMDocument $oNewManifest The DOMDOcument of the manifest
* @param string $sToExtends Name of the template to extends
*/
public static function changeExtendsInDom($oNewManifest, $sToExtends)
{
$oExtendsNode = $oNewManifest->createElement('extends', $sToExtends);
$oConfig = $oNewManifest->getElementsByTagName('config')->item(0);
$oMetadatas = $oConfig->getElementsByTagName('metadatas')->item(0);

// We test if mother template already extends another template
if(!empty($oMetadatas->getElementsByTagName('extends')->item(0))){
$oMetadatas->replaceChild($oExtendsNode, $oMetadatas->getElementsByTagName('extends')->item(0));
}else{
$oMetadatas->appendChild($oExtendsNode);
}
}

/**
* Update the config file of a given template so that it extends another one
*
* It will:
* 1. Delete files and engine nodes
* 2. Update the name of the template
* 3. Change the creation/modification date to the current date
* 4. Change the autor name to the current logged in user
* 5. Change the author email to the admin email
*
* Used in template editor
* Both templates and configuration files must exist before using this function
*
* It's used when extending a template from template editor
* @param string $sToExtends the name of the template to extend
* @param string $sNewName the name of the new template
*/
public static function extendsConfig($sToExtends, $sNewName)
{
$sConfigPath = Yii::app()->getConfig('usertemplaterootdir') . "/" . $sNewName;

// First we get the XML file
libxml_disable_entity_loader(false);
$oNewManifest = self::getManifestDOM($sConfigPath);

self::deleteFilesAndEngineInDom($oNewManifest);
self::changeNameInDOM($oNewManifest, $sNewName);
self::changeDateInDOM($oNewManifest);
self::changeAuthorInDom($oNewManifest);
self::changeEmailInDom($oNewManifest);
self::changeExtendsInDom($oNewManifest, $sToExtends);

$oNewManifest->save($sConfigPath."/config.xml");

Expand Down

0 comments on commit 160a7e9

Please sign in to comment.