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

Last commit: Sun Dec 8 20:56:47 2024 +0100	Jan Dankert	New: Users are now able to store bookmarks.
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->readonly && !$this->autosave ) { 98 // Show action buttons 99 100 if ($this->cancel) { 101 // Adding a cancel-button. 102 $actionBar->addChild( 103 (new CMSElement('div')) 104 ->addStyleClass('btn') 105 ->addStyleClass('btn--control') 106 ->addStyleClass('btn--secondary') 107 ->addStyleClass('act-form-cancel') 108 ->addChild( 109 (new HtmlElement('i'))->addStyleClass(['image-icon','image-icon--form-cancel']) 110 )->addChild( 111 (new HtmlElement('span'))->addStyleClass('form-btn-label')->content(Value::createExpression(ValueExpression::TYPE_MESSAGE, Messages::CANCEL)) 112 ) 113 ); 114 } 115 116 if ($this->apply) { 117 // Adding an apply-button 118 $actionBar->addChild( 119 (new CMSElement('div')) 120 ->addStyleClass('btn') 121 ->addStyleClass('btn--control') 122 ->addStyleClass('btn--primary') 123 ->addStyleClass('act-form-apply') 124 ->addChild( 125 (new HtmlElement('i'))->addStyleClass(['image-icon','image-icon--form-apply']) 126 )->addChild( 127 (new HtmlElement('span'))->addStyleClass('form-btn-label')->content(Value::createExpression(ValueExpression::TYPE_MESSAGE, Messages::APPLY)) 128 ) 129 ); 130 } 131 132 // Adding the save-button 133 $actionBar->addChild( 134 (new CMSElement('div')) 135 ->addStyleClass('btn') 136 ->addStyleClass('btn--control') 137 ->addStyleClass('btn--primary') 138 ->addStyleClass('act-form-save') 139 ->addChild( 140 (new HtmlElement('i'))->addStyleClass(['image-icon','image-icon--form-ok']) 141 )->addChild( 142 (new HtmlElement('span'))->addStyleClass('form-btn-label')->content($this->label) 143 ) 144 ); 145 } 146 147 $formContent = (new HtmlElement('div'))->addStyleClass('form-content')->asChildOf($form); 148 149 if ($this->languageid) 150 $formContent->addChild( 151 (new CMSElement('input')) 152 ->addAttribute('type', 'hidden') 153 ->addAttribute('name', RequestParams::PARAM_LANGUAGE_ID) 154 ->addAttribute('value', $this->languageid) 155 ); 156 157 if ($this->modelid) 158 $formContent->addChild( 159 (new CMSElement('input')) 160 ->addAttribute('type', 'hidden') 161 ->addAttribute('name', RequestParams::PARAM_MODEL_ID) 162 ->addAttribute('value', $this->modelid) 163 ); 164 165 $formContent->addChild( 166 (new CMSElement('input')) 167 ->addAttribute('type', 'hidden') 168 ->addAttribute('name', RequestParams::PARAM_TOKEN) 169 ->addAttribute('value', $this->token) 170 ); 171 172 $formContent->addChild( 173 (new CMSElement('input')) 174 ->addAttribute('type', 'hidden') 175 ->addAttribute('name', RequestParams::PARAM_ACTION) 176 ->addAttribute('value', $this->action) 177 ); 178 $formContent->addChild( 179 (new CMSElement('input')) 180 ->addAttribute('type', 'hidden') 181 ->addAttribute('name', RequestParams::PARAM_SUBACTION) 182 ->addAttribute('value', $this->subaction) 183 ); 184 $formContent->addChild( 185 (new CMSElement('input')) 186 ->addAttribute('type', 'hidden') 187 ->addAttribute('name', RequestParams::PARAM_ID) 188 ->addAttribute('value', $this->id) 189 ); 190 191 192 193 $this->adoptiveElement = $formContent; 194 195 $form->addChild( $actionBar ); 196 197 return $form; 198 } 199 }
Download modules/template_engine/components/html/component_form/FormComponent.class.php
History Sun, 8 Dec 2024 20:56:47 +0100 Jan Dankert New: Users are now able to store bookmarks. 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.