openrat-cms

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

commit 491f0e4939268d426956ebd7021df1c400ced019
parent c6d34dae7129e43409d936e0c491acef7efd864b
Author: Jan Dankert <develop@jandankert.de>
Date:   Mon, 25 Apr 2022 03:26:21 +0200

New: New Project may be created with sample data.

Diffstat:
Mmodules/cms/action/RequestParams.class.php | 18++++++++++++++++++
Mmodules/cms/action/projectlist/ProjectlistAddAction.class.php | 171++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mmodules/cms/model/File.class.php | 7++++---
Mmodules/cms/model/Folder.class.php | 2+-
Mmodules/cms/model/Project.class.php | 56+-------------------------------------------------------
Mmodules/cms/ui/themes/default/html/views/projectlist/add.php | 39+++++++++++++++++++++++++++++++++++----
Mmodules/cms/ui/themes/default/html/views/projectlist/add.tpl.src.xml | 15+++++++++++----
7 files changed, 240 insertions(+), 68 deletions(-)

diff --git a/modules/cms/action/RequestParams.class.php b/modules/cms/action/RequestParams.class.php @@ -253,6 +253,24 @@ class RequestParams } + + /** + * Gets the value of the request parameter. + * + * @param $nameOfRequestParameter + * @return String + * @throws ValidationException + */ + public function getNotEmptyText( $nameOfRequestParameter ) { + + if ( $value = $this->getRequiredText( $nameOfRequestParameter ) ) + return $value; + else + return new ValidationException( $nameOfRequestParameter ); + } + + + /** * Checks if the request contains the parameter. * diff --git a/modules/cms/action/projectlist/ProjectlistAddAction.class.php b/modules/cms/action/projectlist/ProjectlistAddAction.class.php @@ -3,7 +3,18 @@ namespace cms\action\projectlist; use cms\action\Action; use cms\action\Method; use cms\action\ProjectlistAction; +use cms\base\Configuration; +use cms\model\Element; +use cms\model\Folder; +use cms\model\Language; +use cms\model\Model; +use cms\model\Page; +use cms\model\PageContent; use cms\model\Project; +use cms\model\Template; +use cms\model\Text; +use cms\model\Value; +use configuration\Config; use language\Messages; use util\exception\SecurityException; @@ -19,13 +30,26 @@ class ProjectlistAddAction extends ProjectlistAction implements Method { */ public function post() { - $name = $this->request->getRequiredText('name'); + $name = $this->request->getNotEmptyText('name'); $project = new Project(); $project->name = $name; $project->persist(); $this->addNoticeFor( $project,Messages::ADDED ); + + switch( $this->request->getText('type') ) { + case '': + case 'empty': + break; + + case 'example': + $this->createSamplesInProject( $project ); + break; + + default: + // Unknown type value, no action. + } } @@ -34,4 +58,149 @@ class ProjectlistAddAction extends ProjectlistAction implements Method { if ( ! $this->userIsAdmin() ) throw new SecurityException(); } + + + /** + * Create example data in the new project. + * + * @param $project Project + */ + protected function createSamplesInProject($project ) { + + $exampleConfig = Configuration::subset(['project','examples']); + + $rootFolderId = $project->getRootObjectId(); + + // Modell anlegen + $model = new Model(); + $model->projectid = $project->getId(); + $model->name = 'html'; + $model->persist();; + + // Sprache anlegen + $language = new Language(); + $language->projectid = $project->getId(); + $language->isoCode = 'en'; + $language->name = 'english'; + $language->persist(); + + + // Template anlegen + $template = new Template(); + $template->projectid = $project->getId(); + $template->name = 'Standard'; + $template->persist(); + + $element = new Element(); + $element->templateid = $template->templateid; + $element->typeid = Element::ELEMENT_TYPE_LONGTEXT; + $element->writable = true; + $element->format = Element::ELEMENT_FORMAT_MARKDOWN; + $element->label = 'Text'; + $element->name = 'text'; + $element->persist(); + + // Template anlegen + $templateModel = $template->loadTemplateModelFor( $model->modelid ); + $templateModel->extension = 'html'; + $templateModel->src = $exampleConfig->get('html',<<<HTML +<html> + <head> + <link rel="stylesheet" href="{{style}}" /> + <script src="{{script}}" defer="defer"></script> + </head> + <body> + <h1>My first page</h1> + <hr> + <p>{{text}}</p> + </body> +</html> +HTML + ); + $templateModel->public = true; + $templateModel->persist(); + + $scriptFolder = new Folder(); + $scriptFolder->parentid = $rootFolderId; + $scriptFolder->projectid = $project->getId(); + $scriptFolder->filename = 'script'; + $scriptFolder->persist(); + + $script = new Text(); + $script->validFromDate = time(); + $script->settings = <<<SETTINGS +# Sample configuration for this script +# filter: +SETTINGS; + $script->parentid = $scriptFolder->getId(); + $script->projectid = $project->getId(); + $script->filename = 'script.js'; + $script->value = $exampleConfig->get('script',<<<SCRIPT +// Sample Javascript +SCRIPT + ); + $script->persist(); + $script->public = true; + $script->saveValue(); + + $scriptElement = new Element(); + $scriptElement->templateid = $template->templateid; + $scriptElement->typeid = Element::ELEMENT_TYPE_LINK; + $scriptElement->writable = false; + $scriptElement->defaultObjectId = $script->getId(); + $scriptElement->label = 'Script'; + $scriptElement->name = 'script'; + $scriptElement->persist(); + + $style = new Text(); + $style->parentid = $scriptFolder->getId(); + $style->projectid = $project->getId();; + $style->filename = 'style.css'; + $style->value = $exampleConfig->get('style',<<<STYLE +/* Theme CSS */ +background-color: white; +STYLE + ); + $style->persist(); + $style->public = true; + $style->saveValue(); + + $styleElement = new Element(); + $styleElement->templateid = $template->templateid; + $styleElement->typeid = Element::ELEMENT_TYPE_LINK; + $styleElement->writable = false; + $styleElement->label = 'Stylesheet'; + $styleElement->name = 'style'; + $styleElement->defaultObjectId = $style->getId(); + $styleElement->persist(); + + // Beispiel-Seite anlegen + $page = new Page(); + $page->parentid = $rootFolderId; + $page->projectid = $project->getId(); + $page->templateid = $template->templateid; + $page->filename = 'start'; + $page->getDefaultName()->name = 'Sample page'; + $page->persist(); + + $pageContent = new PageContent(); + $pageContent->pageId = $page->pageid; + $pageContent->elementId = $element->elementid; + $pageContent->languageid = $language->languageid; + $pageContent->persist(); + + $value = new Value(); + $value->contentid = $pageContent->contentId; + $value->text = $exampleConfig->get('text',<<<TEXT +# Congratulations + +## Congratulations, this is your first page. + +This is your first page in OpenRat CMS. You may edit the page and change this text. +TEXT + ); + $value->publish = true; + $value->persist(); + + } } diff --git a/modules/cms/model/File.class.php b/modules/cms/model/File.class.php @@ -447,9 +447,6 @@ class File extends BaseObject */ function delete() { - $content = new Content( $this->contentid ); - $content->delete(); - // Delete file $sql = DB::sql( <<<SQL DELETE FROM {{file}} @@ -459,6 +456,10 @@ SQL $sql->setInt( 'objectid',$this->objectid ); $sql->execute(); + // Content must be deleted after the file (because the file is referencing the content) + $content = new Content( $this->contentid ); + $content->delete(); + parent::delete(); } diff --git a/modules/cms/model/Folder.class.php b/modules/cms/model/Folder.class.php @@ -28,7 +28,7 @@ class Folder extends BaseObject } - public function add() + protected function add() { parent::add(); diff --git a/modules/cms/model/Project.class.php b/modules/cms/model/Project.class.php @@ -341,65 +341,11 @@ SQL $sql->execute(); - // Modell anlegen - $model = new Model(); - $model->projectid = $this->projectid; - $model->name = 'html'; - $model->persist();; - - // Sprache anlegen - $language = new Language(); - $language->projectid = $this->projectid; - $language->isoCode = 'en'; - $language->name = 'english'; - $language->persist(); - - // Haupt-Ordner anlegen + // Every projects needs a root folder. The root folder has no parent. $folder = new Folder(); $folder->projectid = $this->projectid; $folder->filename = $this->name; $folder->persist(); - - // Template anlegen - $template = new Template(); - $template->projectid = $this->projectid; - $template->name = '#1'; - $template->persist(); - - $element = new Element(); - $element->templateid = $template->templateid; - $element->typeid = Element::ELEMENT_TYPE_TEXT; - $element->writable = true; - $element->format = Element::ELEMENT_FORMAT_MARKDOWN; - $element->label = 'Text'; - $element->name = 'text'; - $element->persist(); - - // Template anlegen - $templateModel = $template->loadTemplateModelFor( $model->modelid ); - $templateModel->extension = 'html'; - $templateModel->src = '<html><body><h1>Sample page</h1><hr><p>Text: {{text}}</p></body></html>'; - $templateModel->persist(); - - // Beispiel-Seite anlegen - $page = new Page(); - $page->parentid = $folder->objectid; - $page->projectid = $this->projectid; - $page->templateid = $template->templateid; - $page->filename = 'start'; - $page->getDefaultName()->name = 'Sample page'; - $page->persist(); - - $pageContent = new PageContent(); - $pageContent->pageId = $page->pageid; - $pageContent->elementId = $element->elementid; - $pageContent->languageid = $language->languageid; - $pageContent->persist(); - - $value = new Value(); - $value->contentid = $pageContent->contentId; - $value->text = 'sample text'; - $value->persist(); } diff --git a/modules/cms/ui/themes/default/html/views/projectlist/add.php b/modules/cms/ui/themes/default/html/views/projectlist/add.php @@ -6,10 +6,41 @@ <input type="<?php echo O::escapeHtml('hidden') ?>" name="<?php echo O::escapeHtml('action') ?>" value="<?php echo O::escapeHtml('projectlist') ?>" /><?php echo O::escapeHtml('') ?> <input type="<?php echo O::escapeHtml('hidden') ?>" name="<?php echo O::escapeHtml('subaction') ?>" value="<?php echo O::escapeHtml('add') ?>" /><?php echo O::escapeHtml('') ?> <input type="<?php echo O::escapeHtml('hidden') ?>" name="<?php echo O::escapeHtml('id') ?>" value="<?php echo O::escapeHtml(''.@$_id.'') ?>" /><?php echo O::escapeHtml('') ?> - <section class="<?php echo O::escapeHtml('or-fieldset') ?>"><?php echo O::escapeHtml('') ?> - <h3 class="<?php echo O::escapeHtml('or-fieldset-label') ?>"><?php echo O::escapeHtml(''.@O::lang('name').'') ?></h3> - <div class="<?php echo O::escapeHtml('or-fieldset-value') ?>"><?php echo O::escapeHtml('') ?> - <input name="<?php echo O::escapeHtml('name') ?>" type="<?php echo O::escapeHtml('text') ?>" maxlength="<?php echo O::escapeHtml('128') ?>" value="<?php echo O::escapeHtml(''.@$name.'') ?>" class="<?php echo O::escapeHtml('or-focus or-input') ?>" /><?php echo O::escapeHtml('') ?> + <section class="<?php echo O::escapeHtml('or-group or-collapsible or-collapsible--is-open or-collapsible--is-visible or-collapsible--show') ?>"><?php echo O::escapeHtml('') ?> + <h2 class="<?php echo O::escapeHtml('or-collapsible-title or-group-title or-collapsible-act-switch') ?>"><?php echo O::escapeHtml('') ?> + <i class="<?php echo O::escapeHtml('or-image-icon or-image-icon--node-closed or-collapsible--on-closed') ?>"><?php echo O::escapeHtml('') ?></i> + <i class="<?php echo O::escapeHtml('or-image-icon or-image-icon--node-open or-collapsible--on-open') ?>"><?php echo O::escapeHtml('') ?></i> + <span><?php echo O::escapeHtml(''.@O::lang('ADD').'') ?></span> + </h2> + <div class="<?php echo O::escapeHtml('or-collapsible-value or-group-value') ?>"><?php echo O::escapeHtml('') ?> + <section class="<?php echo O::escapeHtml('or-fieldset') ?>"><?php echo O::escapeHtml('') ?> + <h3 class="<?php echo O::escapeHtml('or-fieldset-label') ?>"><?php echo O::escapeHtml(''.@O::lang('name').'') ?></h3> + <div class="<?php echo O::escapeHtml('or-fieldset-value') ?>"><?php echo O::escapeHtml('') ?> + <input name="<?php echo O::escapeHtml('name') ?>" type="<?php echo O::escapeHtml('text') ?>" maxlength="<?php echo O::escapeHtml('128') ?>" value="<?php echo O::escapeHtml(''.@$name.'') ?>" class="<?php echo O::escapeHtml('or-focus or-input') ?>" /><?php echo O::escapeHtml('') ?> + </div> + </section> + </div> + </section> + <section class="<?php echo O::escapeHtml('or-group or-collapsible or-collapsible--is-open or-collapsible--is-visible or-collapsible--show') ?>"><?php echo O::escapeHtml('') ?> + <h2 class="<?php echo O::escapeHtml('or-collapsible-title or-group-title or-collapsible-act-switch') ?>"><?php echo O::escapeHtml('') ?> + <i class="<?php echo O::escapeHtml('or-image-icon or-image-icon--node-closed or-collapsible--on-closed') ?>"><?php echo O::escapeHtml('') ?></i> + <i class="<?php echo O::escapeHtml('or-image-icon or-image-icon--node-open or-collapsible--on-open') ?>"><?php echo O::escapeHtml('') ?></i> + <span><?php echo O::escapeHtml(''.@O::lang('OPTIONS').'') ?></span> + </h2> + <div class="<?php echo O::escapeHtml('or-collapsible-value or-group-value') ?>"><?php echo O::escapeHtml('') ?> + <section class="<?php echo O::escapeHtml('or-fieldset') ?>"><?php echo O::escapeHtml('') ?> + <h3 class="<?php echo O::escapeHtml('or-fieldset-label') ?>"><?php echo O::escapeHtml('') ?></h3> + <div class="<?php echo O::escapeHtml('or-fieldset-value') ?>"><?php echo O::escapeHtml('') ?> + <label><?php echo O::escapeHtml('') ?> + <input type="<?php echo O::escapeHtml('radio') ?>" name="<?php echo O::escapeHtml('type') ?>" value="<?php echo O::escapeHtml('empty') ?>" <?php if(@$type=='empty'){ ?>checked="<?php echo O::escapeHtml('checked') ?>"<?php } ?> class="<?php echo O::escapeHtml('or-form-radio') ?>" /><?php echo O::escapeHtml('') ?> + <span class="<?php echo O::escapeHtml('or-form-label') ?>"><?php echo O::escapeHtml(''.@O::lang('EMPTY').'') ?></span> + </label> + <label><?php echo O::escapeHtml('') ?> + <input type="<?php echo O::escapeHtml('radio') ?>" name="<?php echo O::escapeHtml('type') ?>" value="<?php echo O::escapeHtml('example') ?>" <?php if(@$type=='example'){ ?>checked="<?php echo O::escapeHtml('checked') ?>"<?php } ?> class="<?php echo O::escapeHtml('or-form-radio') ?>" /><?php echo O::escapeHtml('') ?> + <span class="<?php echo O::escapeHtml('or-form-label') ?>"><?php echo O::escapeHtml(''.@O::lang('EXAMPLE').'') ?></span> + </label> + </div> + </section> </div> </section> </div> diff --git a/modules/cms/ui/themes/default/html/views/projectlist/add.tpl.src.xml b/modules/cms/ui/themes/default/html/views/projectlist/add.tpl.src.xml @@ -1,10 +1,17 @@ <output xmlns="http://www.openrat.de/template" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openrat.de/template ../../../../../../../template_engine/components/template.xsd"> <form> - <fieldset class="line" label="${message:name}"> - <input name="name" class="focus" maxlength="128"/> - </fieldset> - + <group title="${message:ADD}"> + <fieldset label="${message:name}"> + <input name="name" class="focus" maxlength="128"/> + </fieldset> + </group> + <group title="${message:OPTIONS}"> + <fieldset> + <radio name="type" value="empty" label="${message:EMPTY}" /> + <radio name="type" checked="true" value="example" label="${message:EXAMPLE}"/> + </fieldset> + </group> <!-- Project copy/export is not available <group title="${message:options}">