File modules/template_engine/components/html/component_form/FormComponent.class.php

Last commit: Wed Nov 17 23:22:19 2021 +0100	Jan Dankert	Refactoring: Using a template context for templates instead of the HTTP-Request-Data
1 <?php 2 3 namespace template_engine\components\html\component_form; 4 5 use cms\action\RequestParams; 6 use language\Messages; 7 use template_engine\components\html\Component; 8 use template_engine\element\CMSElement; 9 use template_engine\element\HtmlElement; 10 use template_engine\element\Value; 11 use template_engine\element\ValueExpression; 12 13 class FormComponent extends Component 14 { 15 16 public $method = 'POST'; 17 18 public $name = ''; 19 20 public $action = null; 21 22 public $subaction = null; 23 public $forwardTo = null; 24 25 public $id = '${_id}'; 26 27 public $languageid = null; 28 public $modelid = null; 29 30 public $label = '${message:button_ok}'; 31 32 public $apply = false; 33 public $cancel = true; 34 public $readonly = false; 35 36 /** 37 * 'view' = Loads Action in the same view 38 * 'top' = Replaces whole workbench. 39 * @var string 40 */ 41 public $target = 'view'; 42 43 public $enctype = 'application/x-www-form-urlencoded'; 44 45 public $async = false; 46 47 public $autosave = false; 48 49 public $type = ''; 50 51 public $afterSuccess; 52 53 private $token = '${_token}'; 54 55 56 public function createElement() 57 { 58 if ( ! $this->action ) 59 $this->action = $this->context->action; 60 61 if ( ! $this->subaction ) 62 $this->subaction = $this->context->method; 63 64 if ( ! $this->id ) 65 $this->id = $this->context->id; 66 67 $form = new CMSElement('form'); 68 69 if ($this->type == 'upload') 70 $this->submitFunction = ''; 71 72 $form->addAttribute('name', $this->name); 73 $form->addAttribute('target', '_self'); 74 $form->addAttribute('data-target', $this->target); 75 $form->addAttribute('action', './'); 76 if ( $this->forwardTo ) 77 $form->addAttribute('data-forward-to', $this->forwardTo); 78 $form->addAttribute('data-method', $this->subaction); 79 $form->addAttribute('data-action', $this->action); 80 $form->addAttribute('data-id', $this->id); 81 $form->addAttribute('method', $this->method); 82 $form->addAttribute('enctype', $this->enctype); 83 $form->addStyleClass('form')->addStyleClass($this->action); 84 $form->addAttribute('data-async', $this->async); 85 $form->addAttribute('data-autosave', $this->autosave); 86 87 if ($this->afterSuccess) 88 $form->addAttribute('data-after-success', $this->afterSuccess); 89 90 $form->addChild( 91 (new HtmlElement('div'))->addStyleClass('form-headline') 92 ); 93 94 // Creating the action bar 95 $actionBar = (new HtmlElement('div'))->addStyleClass('form-actionbar'); 96 97 if ($this->cancel) { 98 // Adding a cancel-button. 99 $actionBar->addChild( 100 (new CMSElement('div')) 101 ->addStyleClass('btn') 102 ->addStyleClass('btn--control') 103 ->addStyleClass('btn--secondary') 104 ->addStyleClass('act-form-cancel') 105 ->addChild( 106 (new HtmlElement('i'))->addStyleClass(['image-icon','image-icon--form-cancel']) 107 )->addChild( 108 (new HtmlElement('span'))->addStyleClass('form-btn-label')->content(Value::createExpression(ValueExpression::TYPE_MESSAGE, Messages::CANCEL)) 109 ) 110 ); 111 } 112 113 if ($this->apply && !$this->readonly) { 114 // Adding an apply-button 115 $actionBar->addChild( 116 (new CMSElement('div')) 117 ->addStyleClass('btn') 118 ->addStyleClass('btn--control') 119 ->addStyleClass('btn--primary') 120 ->addStyleClass('act-form-apply') 121 ->addChild( 122 (new HtmlElement('i'))->addStyleClass(['image-icon','image-icon--form-apply']) 123 )->addChild( 124 (new HtmlElement('span'))->addStyleClass('form-btn-label')->content(Value::createExpression(ValueExpression::TYPE_MESSAGE, Messages::APPLY)) 125 ) 126 ); 127 } 128 129 if (!$this->readonly) { 130 // Adding the save-button 131 $actionBar->addChild( 132 (new CMSElement('div')) 133 ->addStyleClass('btn') 134 ->addStyleClass('btn--control') 135 ->addStyleClass('btn--primary') 136 ->addStyleClass('act-form-save') 137 ->addChild( 138 (new HtmlElement('i'))->addStyleClass(['image-icon','image-icon--form-ok']) 139 )->addChild( 140 (new HtmlElement('span'))->addStyleClass('form-btn-label')->content($this->label) 141 ) 142 ); 143 } 144 145 $formContent = (new HtmlElement('div'))->addStyleClass('form-content')->asChildOf($form); 146 147 if ($this->languageid) 148 $formContent->addChild( 149 (new CMSElement('input')) 150 ->addAttribute('type', 'hidden') 151 ->addAttribute('name', RequestParams::PARAM_LANGUAGE_ID) 152 ->addAttribute('value', $this->languageid) 153 ); 154 155 if ($this->modelid) 156 $formContent->addChild( 157 (new CMSElement('input')) 158 ->addAttribute('type', 'hidden') 159 ->addAttribute('name', RequestParams::PARAM_MODEL_ID) 160 ->addAttribute('value', $this->modelid) 161 ); 162 163 $formContent->addChild( 164 (new CMSElement('input')) 165 ->addAttribute('type', 'hidden') 166 ->addAttribute('name', RequestParams::PARAM_TOKEN) 167 ->addAttribute('value', $this->token) 168 ); 169 170 $formContent->addChild( 171 (new CMSElement('input')) 172 ->addAttribute('type', 'hidden') 173 ->addAttribute('name', RequestParams::PARAM_ACTION) 174 ->addAttribute('value', $this->action) 175 ); 176 $formContent->addChild( 177 (new CMSElement('input')) 178 ->addAttribute('type', 'hidden') 179 ->addAttribute('name', RequestParams::PARAM_SUBACTION) 180 ->addAttribute('value', $this->subaction) 181 ); 182 $formContent->addChild( 183 (new CMSElement('input')) 184 ->addAttribute('type', 'hidden') 185 ->addAttribute('name', RequestParams::PARAM_ID) 186 ->addAttribute('value', $this->id) 187 ); 188 189 190 191 $this->adoptiveElement = $formContent; 192 193 $form->addChild( $actionBar ); 194 195 return $form; 196 } 197 }
Download modules/template_engine/components/html/component_form/FormComponent.class.php
History Wed, 17 Nov 2021 23:22:19 +0100 Jan Dankert Refactoring: Using a template context for templates instead of the HTTP-Request-Data Tue, 1 Dec 2020 00:07:39 +0100 Jan Dankert New: Visibility-Button for password fields, fix: QR-code button for mobile devices. Thu, 12 Nov 2020 01:12:45 +0100 Jan Dankert Fix: Workflow for changing the users email adress; Mail sending; Forwarding forms Wed, 14 Oct 2020 22:36:29 +0200 Jan Dankert Refactoring: Fixed the namespace in component classes, now the are able to be load by the standard autoloader. Wed, 14 Oct 2020 22:20:22 +0200 Jan Dankert Refactoring: Renamed component folders, because 'if' is no valid namespace fragment.