File modules/cms/action/projectlist/ProjectlistAddAction.class.php
Last commit: Sat Jan 28 20:02:52 2023 +0100 Jan Dankert Fix: Sample text is markdown.
1 <?php 2 namespace cms\action\projectlist; 3 use cms\action\Action; 4 use cms\action\Method; 5 use cms\action\ProjectlistAction; 6 use cms\base\Configuration; 7 use cms\model\Element; 8 use cms\model\Folder; 9 use cms\model\Language; 10 use cms\model\Model; 11 use cms\model\Page; 12 use cms\model\PageContent; 13 use cms\model\Project; 14 use cms\model\Template; 15 use cms\model\Text; 16 use cms\model\Value; 17 use configuration\Config; 18 use language\Messages; 19 use util\exception\SecurityException; 20 21 class ProjectlistAddAction extends ProjectlistAction implements Method { 22 23 public function view() { 24 25 } 26 27 28 /** 29 * Add a new project. 30 */ 31 public function post() { 32 33 $name = $this->request->getNotEmptyText('name'); 34 35 $project = new Project(); 36 $project->name = $name; 37 $project->persist(); 38 39 $this->addNoticeFor( $project,Messages::ADDED ); 40 41 switch( $this->request->getText('type') ) { 42 case '': 43 case 'empty': 44 break; 45 46 case 'example': 47 $this->createSamplesInProject( $project ); 48 break; 49 50 default: 51 // Unknown type value, no action. 52 } 53 } 54 55 56 public function checkAccess() 57 { 58 if ( ! $this->userIsAdmin() ) 59 throw new SecurityException(); 60 } 61 62 63 /** 64 * Create example data in the new project. 65 * 66 * @param $project Project 67 */ 68 protected function createSamplesInProject($project ) { 69 70 $exampleConfig = Configuration::subset(['project','examples']); 71 72 $rootFolderId = $project->getRootObjectId(); 73 74 // Modell anlegen 75 $model = new Model(); 76 $model->projectid = $project->getId(); 77 $model->name = 'html'; 78 $model->persist();; 79 80 // Sprache anlegen 81 $language = new Language(); 82 $language->projectid = $project->getId(); 83 $language->isoCode = 'en'; 84 $language->name = 'english'; 85 $language->persist(); 86 87 88 // Template anlegen 89 $template = new Template(); 90 $template->projectid = $project->getId(); 91 $template->name = 'Standard'; 92 $template->persist(); 93 94 $element = new Element(); 95 $element->templateid = $template->templateid; 96 $element->typeid = Element::ELEMENT_TYPE_LONGTEXT; 97 $element->writable = true; 98 $element->format = Element::ELEMENT_FORMAT_MARKDOWN; 99 $element->label = 'Text'; 100 $element->name = 'text'; 101 $element->persist(); 102 103 // Template anlegen 104 $templateModel = $template->loadTemplateModelFor( $model->modelid ); 105 $templateModel->extension = 'html'; 106 $templateModel->src = $exampleConfig->get('html',<<<HTML 107 <html> 108 <head> 109 <link rel="stylesheet" href="{{style}}" /> 110 <script src="{{script}}" defer="defer"></script> 111 </head> 112 <body> 113 <h1>My first page</h1> 114 <hr> 115 <p>{{text}}</p> 116 </body> 117 </html> 118 HTML 119 ); 120 $templateModel->public = true; 121 $templateModel->persist(); 122 123 $scriptFolder = new Folder(); 124 $scriptFolder->parentid = $rootFolderId; 125 $scriptFolder->projectid = $project->getId(); 126 $scriptFolder->filename = 'script'; 127 $scriptFolder->persist(); 128 129 $script = new Text(); 130 $script->validFromDate = time(); 131 $script->settings = <<<SETTINGS 132 # Sample configuration for this script 133 # filter: 134 SETTINGS; 135 $script->parentid = $scriptFolder->getId(); 136 $script->projectid = $project->getId(); 137 $script->filename = 'script.js'; 138 $script->value = $exampleConfig->get('script',<<<SCRIPT 139 // Sample Javascript 140 SCRIPT 141 ); 142 $script->persist(); 143 $script->public = true; 144 $script->saveValue(); 145 146 $scriptElement = new Element(); 147 $scriptElement->templateid = $template->templateid; 148 $scriptElement->typeid = Element::ELEMENT_TYPE_LINK; 149 $scriptElement->writable = false; 150 $scriptElement->defaultObjectId = $script->getId(); 151 $scriptElement->label = 'Script'; 152 $scriptElement->name = 'script'; 153 $scriptElement->persist(); 154 155 $style = new Text(); 156 $style->parentid = $scriptFolder->getId(); 157 $style->projectid = $project->getId();; 158 $style->filename = 'style.css'; 159 $style->value = $exampleConfig->get('style',<<<STYLE 160 /* Theme CSS */ 161 background-color: white; 162 STYLE 163 ); 164 $style->persist(); 165 $style->public = true; 166 $style->saveValue(); 167 168 $styleElement = new Element(); 169 $styleElement->templateid = $template->templateid; 170 $styleElement->typeid = Element::ELEMENT_TYPE_LINK; 171 $styleElement->writable = false; 172 $styleElement->label = 'Stylesheet'; 173 $styleElement->name = 'style'; 174 $styleElement->defaultObjectId = $style->getId(); 175 $styleElement->persist(); 176 177 // Beispiel-Seite anlegen 178 $page = new Page(); 179 $page->parentid = $rootFolderId; 180 $page->projectid = $project->getId(); 181 $page->templateid = $template->templateid; 182 $page->filename = 'start'; 183 $page->getDefaultName()->name = 'Sample page'; 184 $page->persist(); 185 186 $pageContent = new PageContent(); 187 $pageContent->pageId = $page->pageid; 188 $pageContent->elementId = $element->elementid; 189 $pageContent->languageid = $language->languageid; 190 $pageContent->persist(); 191 192 $value = new Value(); 193 $value->contentid = $pageContent->contentId; 194 $value->text = $exampleConfig->get('text',<<<TEXT 195 # Congratulations 196 197 ## Congratulations, this is your first page. 198 199 This is your first page in OpenRat CMS. You may edit the page and change this text. 200 TEXT 201 ); 202 $value->format = Element::ELEMENT_FORMAT_MARKDOWN; 203 $value->publish = true; 204 $value->persist(); 205 206 } 207 }
Downloadmodules/cms/action/projectlist/ProjectlistAddAction.class.php
History Sat, 28 Jan 2023 20:02:52 +0100 Jan Dankert Fix: Sample text is markdown. Mon, 25 Apr 2022 03:26:21 +0200 Jan Dankert New: New Project may be created with sample data. Fri, 3 Dec 2021 22:30:29 +0100 dankert Fix: Check rights for listing projects. Sun, 14 Mar 2021 22:29:56 +0100 Jan Dankert Refactoring: Clearer access check. Sat, 6 Mar 2021 03:42:38 +0100 Jan Dankert New: Better permission checks. Sat, 27 Feb 2021 00:02:03 +0100 Jan Dankert Fix: Adding projects with project name. Fri, 26 Feb 2021 01:06:01 +0100 Jan Dankert Refactoring accessing the request parameter values. Wed, 18 Nov 2020 01:46:36 +0100 Jan Dankert Refactoring of model classes: New method persist() and some other cleanups. Tue, 17 Nov 2020 23:51:00 +0100 Jan Dankert Refactoring: Every Actionmethod has now its own class.