File modules/cms/action/template/TemplateDiffAction.class.php

Last commit: Tue Dec 7 00:26:56 2021 +0100	dankert	New: TemplateDiffAction for comparing template values.
1 <?php 2 namespace cms\action\template; 3 use cms\action\Method; 4 use cms\action\TemplateAction; 5 use cms\generator\PublishEdit; 6 use cms\generator\PublishPreview; 7 use cms\model\Value; 8 use util\exception\ValidationException; 9 use util\Text; 10 11 class TemplateDiffAction extends TemplateAction implements Method { 12 13 public function view() { 14 $value1id = $this->request->getNumber('compareid'); 15 $value2id = $this->request->getNumber('withid' ); 16 17 // Wenn Value1-Id = Value2-Id 18 if ( $value1id == $value2id ) 19 throw new ValidationException('withid' ); 20 21 // Value 2 must be greater than value 1. 22 if ( $value1id > $value2id ) 23 list($value1id,$value2id) = array( $value2id,$value1id ); 24 25 $value1 = new Value(); 26 $value2 = new Value(); 27 28 $value1->loadWithId( $value1id ); 29 $value2->loadWithId( $value2id ); 30 31 // both values must be part of the same content. 32 if ( $value1->contentid != $value2->contentid ) 33 throw new ValidationException('compareid'); 34 35 // Security-Check: 36 // Content must be a part of a template 37 $this->ensureValueIdIsInAnyTemplate( $value1->valueid ); 38 39 $this->setTemplateVar('date_left' ,$value1->lastchangeTimeStamp); 40 $this->setTemplateVar('date_right',$value2->lastchangeTimeStamp); 41 42 // Split whole text into lines. 43 $text1 = explode("\n",$value1->text); 44 $text2 = explode("\n",$value2->text); 45 46 // Make the diff 47 $diffResult = Text::diff($text1,$text2); 48 49 $outputResult = array_map( function( $left,$right) { 50 return [ 51 'left' => $left, 52 'right'=> $right 53 ]; 54 },$diffResult[0],$diffResult[1] ); 55 56 $this->setTemplateVar('diff',$outputResult ); 57 } 58 59 60 public function post() { 61 } 62 }
Download modules/cms/action/template/TemplateDiffAction.class.php
History Tue, 7 Dec 2021 00:26:56 +0100 dankert New: TemplateDiffAction for comparing template values.