File modules/cms/action/page/PageAllAction.class.php
Last commit: Wed Apr 17 00:34:07 2024 +0200 Jan Dankert New: Inputfields for coordinates; coordinates are stored in the now 64bit-number-field of the value.
1 <?php 2 namespace cms\action\page; 3 4 use cms\action\Method; 5 use cms\action\PageAction; 6 use cms\base\Language as L; 7 use cms\generator\PageContext; 8 use cms\generator\PageGenerator; 9 use cms\generator\Producer; 10 use cms\generator\Publisher; 11 use cms\generator\PublishOrder; 12 use cms\model\BaseObject; 13 use cms\model\Content; 14 use cms\model\Element; 15 use cms\model\Folder; 16 use cms\model\Language; 17 use cms\model\Page; 18 use cms\model\PageContent; 19 use cms\model\Permission; 20 use cms\model\Project; 21 use cms\model\Value; 22 use language\Messages; 23 use util\exception\PublisherException; 24 use util\exception\SecurityException; 25 use util\exception\ValidationException; 26 use util\Session; 27 use util\Text; 28 29 class PageAllAction extends PageAction implements Method { 30 31 public function getRequiredPermission() { 32 return Permission::ACL_READ; 33 } 34 35 36 public function view() 37 { 38 $languageid = $this->request->getRequiredNumber('languageid'); 39 $language = new Language($languageid); 40 $language->load(); 41 42 $this->setTemplateVar('language_name', $language->getName()); 43 $this->setTemplateVar('languageid' , $language->languageid ); 44 45 $this->setTemplateVar('value_time',time() ); 46 $this->setTemplateVar('writable' ,$this->page->hasRight( Permission::ACL_WRITE ) ); 47 48 $elements = []; 49 50 /** @var Element $element */ 51 foreach ($this->getElements() as $element) { 52 53 $pageContent = new PageContent(); 54 $pageContent->elementId = $element->elementid; 55 $pageContent->pageId = $this->page->pageid; 56 $pageContent->languageid = $languageid; 57 $pageContent->load(); 58 59 if ( $pageContent->isPersistent() ) { 60 $value = new Value(); 61 $value->contentid = $pageContent->contentId; 62 $value->load(); 63 } 64 else { 65 // There is no content yet, so creating an empty value. 66 $value = new Value(); 67 } 68 69 $output = []; 70 $output += $element->getProperties(); 71 $output += $value->getProperties(); 72 73 $content = ''; 74 75 switch( $element->typeid) { 76 77 case Element::ELEMENT_TYPE_DATE: 78 $output['date']=date('Y-m-d',$value->date ); 79 $output['time']=date('H:i' ,$value->date ); 80 break; 81 82 case Element::ELEMENT_TYPE_LINK: 83 $project = new Project($this->page->projectid); 84 $output['rootfolderid'] = $project->getRootObjectId(); 85 86 // Ermitteln, welche Objekttypen verlinkt werden d�rfen. 87 $type = $element->subtype; 88 89 if (substr($type, 0, 5) == 'image') 90 $type = 'file'; 91 92 if (!in_array($type, array('file', 'page', 'link', 'folder'))) 93 $types = array('file', 'page', 'link'); // Fallback: Der Link kann auf Seiten,Dateien und Verknüpfungen zeigen 94 else 95 $types = array($type); // gewünschten Typ verwenden 96 97 $oid = $value->linkToObjectId; 98 $name = ''; 99 100 if ($oid) { 101 $o = new BaseObject($oid); 102 $o->load(); 103 $name = $o->filename; 104 } 105 106 $output['objects'] = array(); 107 $output['linkobjectid']= $oid; 108 $content = $oid; 109 $output['linkname']= $name; 110 111 $output['types'] = implode(',', $types); 112 113 break; 114 115 case Element::ELEMENT_TYPE_SELECT: 116 $output['items'] = $element->getSelectItems(); 117 $content = $value->text; 118 break; 119 120 case Element::ELEMENT_TYPE_INSERT: 121 // Auswahl ueber alle Elementtypen 122 $objects = array(); 123 //Änderung der möglichen Types 124 $types = array('file', 'page', 'link'); 125 $objects[0] = \cms\base\Language::lang('LIST_ENTRY_EMPTY'); // Wert "nicht ausgewählt" 126 127 $project = new Project($this->page->projectid); 128 $folder = new Folder($project->getRootObjectId()); 129 $folder->load(); 130 131 //Auch Dateien dazu 132 foreach ($project->getAllObjectIds($types) as $id) { 133 $f = new Folder($id); 134 $f->load(); 135 136 $objects[$id] = \cms\base\Language::lang($f->getType()) . ': '; 137 $objects[$id] .= implode(' » ', $f->parentObjectNames(false, true)); 138 } 139 140 foreach ($project->getAllFolders() as $id) { 141 $f = new Folder($id); 142 $f->load(); 143 144 $objects[$id] = \cms\base\Language::lang($f->getType()) . ': '; 145 $objects[$id] .= implode(' » ', $f->parentObjectNames(false, true)); 146 } 147 148 asort($objects); // Sortieren 149 150 $output['objects'] = $objects; 151 $content = $value->linkToObjectId; 152 153 break; 154 155 case Element::ELEMENT_TYPE_NUMBER: 156 $content = $value->number / pow(10, $element->decimals); 157 break; 158 159 case Element::ELEMENT_TYPE_CHECKBOX: 160 $content = $value->number; 161 break; 162 163 case Element::ELEMENT_TYPE_LONGTEXT: 164 if ($requestFormat = $this->request->getText('format')) 165 // Individual format from request. 166 $format = $requestFormat; 167 elseif ($value->format != null) 168 $format = $value->format; 169 else 170 // There is no saved value. Get the format from the template element. 171 $format = $element->format; 172 173 $output['format'] = $format; 174 $output['editor'] = Element::getAvailableFormats()[$format]; 175 176 $content = $this->linkifyOIDs($value->text); 177 break; 178 179 case Element::ELEMENT_TYPE_TEXT: 180 case Element::ELEMENT_TYPE_DATA: 181 182 $content = $value->text; 183 break; 184 185 case Element::ELEMENT_TYPE_COORD: 186 $output['lat' ] = $value->number >> 32; 187 $output['long'] = $value->number << 32 >> 32; 188 break; 189 190 default: 191 throw new \InvalidArgumentException("Unsupported element type: ".$element->typeid); 192 193 } 194 195 $output[ 'name' ] = $element->name; 196 $output[ 'value' ] = $content; 197 $this->setTemplateVar( $element->name, $content ); 198 199 $elements[ $element->elementid ] = $output; 200 } 201 202 if ($this->page->hasRight(Permission::ACL_RELEASE)) 203 $this->setTemplateVar('release', true); 204 if ($this->page->hasRight(Permission::ACL_PUBLISH)) 205 $this->setTemplateVar('publish', false); 206 207 $this->setTemplateVar('elements', $elements ); 208 } 209 210 211 212 public function post() 213 { 214 if ( !$this->page->hasRight( Permission::ACL_WRITE )) 215 throw new SecurityException(); 216 217 $languageid = $this->request->getRequiredNumber('languageid'); 218 $language = new Language($languageid); 219 $language->load(); 220 221 /** @var Element $element */ 222 foreach ($this->getElements() as $element) { 223 224 $pageContent = new PageContent(); 225 $pageContent->elementId = $element->elementid; 226 $pageContent->pageId = $this->page->pageid; 227 $pageContent->languageid = $languageid; 228 $pageContent->load(); 229 $pageContent->persist(); // Create if it does not exist yet. 230 231 $value = new Value(); 232 $value->contentid = $pageContent->contentId; 233 $oldValue = clone $value; 234 $oldValue->load(); 235 236 switch ($element->typeid) { 237 238 case Element::ELEMENT_TYPE_TEXT: 239 case Element::ELEMENT_TYPE_DATA: 240 $value->text = $this->request->getText($element->name); 241 break; 242 case Element::ELEMENT_TYPE_COORD: 243 $value->number = $this->request->getNumber($element->name.'_lat') << 32 | $this->request->getNumber($element->name.'_long'); 244 break; 245 246 case Element::ELEMENT_TYPE_LONGTEXT: 247 $value->text = $this->compactOIDs($this->request->getText($element->name)); 248 $value->format = $oldValue->format; 249 break; 250 251 case Element::ELEMENT_TYPE_DATE: 252 $value->date = strtotime($this->request->getText($element->name . '_date') . $this->request->getText($element->name . '_time')); 253 break; 254 255 case Element::ELEMENT_TYPE_SELECT: 256 $value->text = $this->request->getText($element->name); 257 break; 258 case Element::ELEMENT_TYPE_CHECKBOX: 259 $value->number = $this->request->getText($element->name)?1:0; 260 break; 261 case Element::ELEMENT_TYPE_LINK: 262 case Element::ELEMENT_TYPE_INSERT: 263 $value->linkToObjectId = $this->request->getNumber($element->name); 264 break; 265 266 case Element::ELEMENT_TYPE_NUMBER: 267 $value->number = $this->request->getText($element->name) * pow(10, $element->decimals); 268 break; 269 default: 270 throw new \LogicException('Unknown element type: ' . $element->getTypeName()); 271 } 272 273 274 // Inhalt sofort freigegeben, wenn 275 // - Recht vorhanden 276 // - Freigabe gewuenscht 277 $value->publish = $this->page->hasRight(Permission::ACL_RELEASE) && $this->request->isTrue('release'); 278 279 // Up-To-Date-Check 280 $content = new Content( $pageContent->contentId ); 281 $lastChangeTime = $content->getLastChangeSinceByAnotherUser($this->request->getNumber('value_time'), $this->getCurrentUserId()); 282 if ($lastChangeTime) 283 $this->addWarningFor($value, Messages::CONCURRENT_VALUE_CHANGE, array('last_change_time' => date(L::lang('DATE_FORMAT'), $lastChangeTime))); 284 285 // Check if anything has changed 286 if ( $oldValue->text == $value->text && 287 $oldValue->linkToObjectId == $value->linkToObjectId && 288 $oldValue->format == $value->format && 289 $oldValue->number == $value->number && 290 $oldValue->date == $value->date && 291 !(!$oldValue->publish && $value->publish) 292 ) 293 continue; // nothing has changed. 294 295 // Inhalt speichern 296 $value->persist(); 297 298 // Wenn Inhalt in allen Sprachen gleich ist, dann wird der Inhalt 299 // fuer jede Sprache einzeln gespeichert. 300 if ($element->allLanguages) { 301 $project = new Project($this->page->projectid); 302 foreach ($project->getLanguageIds() as $languageid) { 303 if ($languageid != $pageContent->languageid) { 304 $otherPageContent = clone $pageContent; 305 $otherPageContent->languageid = $languageid; 306 $otherPageContent->contentId = null; 307 $otherPageContent->load(); 308 if (!$otherPageContent->contentId) 309 $otherPageContent->persist(); // create pagecontent if it does not exist. 310 311 $otherValue = clone $value; 312 $otherValue->contentid = $otherPageContent->contentId; 313 $otherValue->persist(); 314 } 315 } 316 } 317 } 318 319 // Falls ausgewaehlt die Seite sofort veroeffentlichen 320 if ($this->page->hasRight(Permission::ACL_PUBLISH) && $this->request->isTrue('publish')) { 321 $this->publishPage( $languageid ); 322 } 323 324 $this->page->setTimestamp(); // "Letzte Aenderung" setzen 325 326 $this->addNoticeFor($this->page, Messages::SAVED); 327 } 328 329 330 /** 331 * Gets all elements of the current page. 332 * 333 * @return array 334 */ 335 protected function getElements() { 336 337 return $this->page->getTemplate()->getWritableElements(); 338 } 339 340 341 protected function linkifyOIDs( $text ) 342 { 343 $pageContext = $this->createPageContext( Producer::SCHEME_PREVIEW ); 344 345 $linkFormat = $pageContext->getLinkScheme(); 346 347 foreach( Text::parseOID($text) as $oid=>$t ) 348 { 349 $url = $linkFormat->linkToObject($this->page, (new BaseObject($oid))->load() ); 350 foreach( $t as $match) 351 $text = str_replace($match,$url,$text); 352 } 353 354 return $text; 355 } 356 357 358 359 360 protected function compactOIDs( $text ) 361 { 362 foreach( Text::parseOID($text) as $oid=>$t ) 363 { 364 foreach( $t as $match) 365 $text = str_replace($match,'?__OID__'.$oid.'__',$text); 366 } 367 368 return $text; 369 } 370 371 372 373 protected function publishPage( $languageid ) { 374 375 $project = $this->page->getProject(); 376 377 // Nothing is written to the session from this point. so we should free the session. 378 Session::close(); 379 380 $publisher = new Publisher( $project->projectid ); 381 382 foreach( $project->getModelIds() as $modelId ) { 383 384 $pageContext = new PageContext( $this->page->objectid, Producer::SCHEME_PUBLIC ); 385 $pageContext->modelId = $modelId; 386 $pageContext->languageId = $languageid; 387 388 $pageGenerator = new PageGenerator( $pageContext ); 389 390 $publisher->addOrderForPublishing( new PublishOrder( $pageGenerator->getCache()->load()->getFilename(),$pageGenerator->getPublicFilename(), $this->page->lastchangeDate ) ); 391 } 392 393 try { 394 $publisher->publish(); 395 } catch( PublisherException $e ) { 396 $this->addErrorFor( $this->page,Messages::PUBLISHED_ERROR,[],$e->getMessage() ); 397 } 398 399 $this->page->setPublishedTimestamp(); 400 401 $this->addNoticeFor( $this->page,Messages::PUBLISHED,[], 402 implode("\n",$publisher->getDestinationFilenames() ) ); 403 404 } 405 }
Downloadmodules/cms/action/page/PageAllAction.class.php
History Wed, 17 Apr 2024 00:34:07 +0200 Jan Dankert New: Inputfields for coordinates; coordinates are stored in the now 64bit-number-field of the value. Wed, 15 Feb 2023 00:22:28 +0100 Jan Dankert Fix: Changing the Longtext format was not possible. Mon, 27 Jun 2022 02:54:38 +0200 Jan Dankert Fix: Editing of dates was broken, do not know why the code was missing up to now... Mon, 27 Jun 2022 02:22:50 +0200 Jan Dankert Fix: Saving links in PageallAction and PageelementAllAction. Thu, 2 Jun 2022 01:04:54 +0200 Jan Dankert New: Element type "checkbox" Wed, 1 Jun 2022 01:05:34 +0200 Jan Dankert New: Element types for "coordinates" and "data" Wed, 1 Jun 2022 00:11:40 +0200 Jan Dankert New: DSL as a filter for number values Sun, 10 Apr 2022 12:10:24 +0200 dankert Fix: Pagecontent must be created if the value of a pageelement is stored for the first time. Fri, 11 Mar 2022 12:26:33 +0100 dankert Fix: Catch error if something happens while publishing. Wed, 9 Mar 2022 13:28:52 +0100 dankert Refactoring: Checkbox values are always sent to the server. In the actions we must test the value with 'isTrue()' Mon, 6 Dec 2021 02:22:45 +0100 dankert Fix for: Do not store value is nothing has changed. Mon, 6 Dec 2021 02:11:33 +0100 dankert New: Do not store value is nothing has changed. Sun, 5 Dec 2021 20:33:24 +0100 dankert Cleanup: Removed unusable properties from class 'Value' and 'BaseObject'. Sat, 4 Dec 2021 01:03:54 +0100 dankert Fix: Editing all values of the page is now able to save with the new pagecontent. Sat, 4 Dec 2021 00:18:39 +0100 dankert Some security enhancements. Wed, 10 Mar 2021 23:51:22 +0100 Jan Dankert Refactoring: Cleaned the Request params. Sat, 6 Mar 2021 03:42:38 +0100 Jan Dankert New: Better permission checks. Sat, 27 Feb 2021 01:07:14 +0100 Jan Dankert Fix: Request-Id may contain '_'. Sat, 27 Feb 2021 00:29:39 +0100 Jan Dankert New: Set publishing date on publishing. Fri, 26 Feb 2021 01:06:01 +0100 Jan Dankert Refactoring accessing the request parameter values. Thu, 25 Feb 2021 01:22:10 +0100 Jan Dankert New: Edit all page elements in 1 view.