openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 2f62fb6b92e75af176509e8fba28b188028dd13d
parent aad6a10a7076035161e1dfe115487a16f816d2f2
Author: Jan Dankert <devnull@localhost>
Date:   Tue,  6 Nov 2012 23:20:55 +0100

Fix: Kopieren von Templates mitsamt des Quelltextes. Issue #20

Diffstat:
action/TemplatelistAction.class.php | 30++++++++++++++++++++++++++++--
model/Template.class.php | 13++++++++++++-
2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/action/TemplatelistAction.class.php b/action/TemplatelistAction.class.php @@ -102,6 +102,7 @@ class TemplatelistAction extends Action { case 'empty': + // Neues Template anlegen. $template = new Template(); $template->add( $this->getRequestVar('name') ); $this->addNotice('template',$template->name,'ADDED','ok'); @@ -114,22 +115,46 @@ class TemplatelistAction extends Action if ( $copy_templateid == 0 ) { $this->addValidationError('templateid'); - $this->callSubAction('add'); return; } - + + // Neues Template anlegen. $template = new Template(); $template->add( $this->getRequestVar('name') ); $this->addNotice('template',$template->name,'ADDED','ok'); + // Template kopieren. $copy_template = new Template( $copy_templateid ); $copy_template->load(); + $elementMapping = array(); foreach( $copy_template->getElements() as $element ) { $element->load(); + $oldelementId = $element->elementid; $element->templateid = $template->templateid; $element->add(); $element->save(); + + $elementMapping[$oldelementId] = $element->elementid; + } + + $project = Session::getProject(); + foreach( $project->getModelIds() as $modelid ) + { + // Template laden + $copy_template->modelid = $modelid; + $copy_template->load(); + + $template->modelid = $modelid; + $src = $copy_template->src; + + // Elemente im Quelltext an die geänderten Element-Idn anpassen. + foreach( $elementMapping as $oldId=>$newId) + $src = str_replace('{{'.$oldId.'}}','{{'.$newId.'}}',$src); + + $template->src = $src; + $template->extension = $copy_template->extension; + $template->save(); } $this->addNotice('template',$copy_template->name,'COPIED','ok'); @@ -138,6 +163,7 @@ class TemplatelistAction extends Action case 'example': + // Neues Template anlegen. $template = new Template(); $model = Session::getProjectModel(); diff --git a/model/Template.class.php b/model/Template.class.php @@ -114,6 +114,9 @@ class Template ' WHERE id={templateid}' ); $sql->setInt( 'templateid',$this->templateid ); $row = $db->getRow( $sql ); + + if ( empty($row) ) + throw new ObjectNotFoundException("Template not found: ".$this->templateid); $this->name = $row['name' ]; $this->projectid = $row['projectid']; @@ -130,6 +133,11 @@ class Template $this->extension = $row['extension']; $this->src = $row['text']; } + else + { + $this->extension = null; + $this->src = null; + } } @@ -158,7 +166,8 @@ class Template $sql->setInt ( 'modelid' ,$this->modelid ); if ( intval($db->getOne($sql)) > 0 ) - { + { + // Vorlagen-Quelltext existiert für diese Varianten schon. $sql = new Sql( 'UPDATE {t_templatemodel}'. ' SET extension={extension},'. ' text={src} '. @@ -167,8 +176,10 @@ class Template } else { + // Vorlagen-Quelltext wird für diese Varianten neu angelegt. $sql = new Sql('SELECT MAX(id) FROM {t_templatemodel}'); $nextid = intval($db->getOne($sql))+1; + $sql = new Sql( 'INSERT INTO {t_templatemodel}'. ' (id,templateid,projectmodelid,extension,text) '. ' VALUES ({id},{templateid},{modelid},{extension},{src}) ');