openrat-cms

OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs | README

Form.class.php (5080B)


      1 <?php
      2 
      3 namespace template_engine\components;
      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 
     24 	public $id = '${_id}';
     25 
     26 	public $languageid = null;
     27 	public $modelid = null;
     28 
     29 	public $label = '${message:button_ok}';
     30 
     31 	public $apply = false;
     32 	public $cancel = true;
     33 	public $readonly = false;
     34 
     35 	/**
     36 	 * 'view' = Loads Action in the same view
     37 	 * 'top' = Replaces whole workbench.
     38 	 * @var string
     39 	 */
     40 	public $target = 'view';
     41 
     42 	public $enctype = 'application/x-www-form-urlencoded';
     43 
     44 	public $async = false;
     45 
     46 	public $autosave = false;
     47 
     48 	public $type = '';
     49 
     50 	public $afterSuccess;
     51 
     52 	private $token = '${_token}';
     53 
     54 
     55 	public function createElement()
     56 	{
     57 		if   ( ! $this->action )
     58 			$this->action = $this->request->action;
     59 
     60 		if   ( ! $this->subaction )
     61 			$this->subaction = $this->request->method;
     62 
     63 		if   ( ! $this->id )
     64 			$this->id = $this->request->id;
     65 
     66 		$form = new CMSElement('form');
     67 
     68 		if ($this->type == 'upload')
     69 			$this->submitFunction = '';
     70 
     71 		$form->addAttribute('name', $this->name);
     72 		$form->addAttribute('target', '_self');
     73 		$form->addAttribute('data-target', $this->target);
     74 		$form->addAttribute('action', './');
     75 		$form->addAttribute('data-method', $this->subaction);
     76 		$form->addAttribute('data-action', $this->action);
     77 		$form->addAttribute('data-id', $this->id);
     78 		$form->addAttribute('method', $this->method);
     79 		$form->addAttribute('enctype', $this->enctype);
     80 		$form->addStyleClass('form')->addStyleClass($this->action);
     81 		$form->addAttribute('data-async', $this->async);
     82 		$form->addAttribute('data-autosave', $this->autosave);
     83 
     84 		if ($this->afterSuccess)
     85 			$form->addAttribute('data-after-success', $this->afterSuccess);
     86 
     87 		$form->addChild(
     88 			(new HtmlElement('div'))->addStyleClass('form-headline')
     89 		);
     90 
     91 		// Creating the action bar
     92 		$actionBar = (new HtmlElement('div'))->addStyleClass('form-actionbar');
     93 
     94 		if ($this->cancel) {
     95 			// Adding a cancel-button.
     96 			$actionBar->addChild(
     97 				(new CMSElement('div'))
     98 					->addStyleClass('btn')
     99 					->addStyleClass('btn--secondary')
    100 					->addStyleClass('act-form-cancel')
    101 					->addChild(
    102 						(new HtmlElement('i'))->addStyleClass(['image-icon','image-icon--form-cancel'])
    103 					)->addChild(
    104 						(new HtmlElement('span'))->addStyleClass('form-btn-label')->content(Value::createExpression(ValueExpression::TYPE_MESSAGE, Messages::CANCEL))
    105 					)
    106 			);
    107 		}
    108 
    109 		if ($this->apply && !$this->readonly) {
    110 			// Adding an apply-button
    111 			$actionBar->addChild(
    112 				(new CMSElement('div'))
    113 					->addStyleClass('btn')
    114 					->addStyleClass('btn--primary')
    115 					->addStyleClass('act-form-apply')
    116 					->addChild(
    117 						(new HtmlElement('i'))->addStyleClass(['image-icon','image-icon--form-apply'])
    118 					)->addChild(
    119 						(new HtmlElement('span'))->addStyleClass('form-btn-label')->content(Value::createExpression(ValueExpression::TYPE_MESSAGE, Messages::APPLY))
    120 					)
    121 			);
    122 		}
    123 
    124 		if (!$this->readonly) {
    125 			// Adding the save-button
    126 			$actionBar->addChild(
    127 				(new CMSElement('div'))
    128 					->addStyleClass('btn')
    129 					->addStyleClass('btn--primary')
    130 					->addStyleClass('act-form-save')
    131 					->addChild(
    132 						(new HtmlElement('i'))->addStyleClass(['image-icon','image-icon--form-ok'])
    133 					)->addChild(
    134 						(new HtmlElement('span'))->addStyleClass('form-btn-label')->content($this->label)
    135 					)
    136 			);
    137 		}
    138 
    139 		$formContent = (new HtmlElement('div'))->addStyleClass('form-content')->asChildOf($form);
    140 
    141 		if ($this->languageid)
    142 			$formContent->addChild(
    143 				(new CMSElement('input'))
    144 					->addAttribute('type', 'hidden')
    145 					->addAttribute('name', RequestParams::PARAM_LANGUAGE_ID)
    146 					->addAttribute('value', $this->languageid)
    147 			);
    148 
    149 		if ($this->modelid)
    150 			$formContent->addChild(
    151 				(new CMSElement('input'))
    152 					->addAttribute('type', 'hidden')
    153 					->addAttribute('name', RequestParams::PARAM_MODEL_ID)
    154 					->addAttribute('value', $this->modelid)
    155 			);
    156 
    157 		$formContent->addChild(
    158 			(new CMSElement('input'))
    159 				->addAttribute('type', 'hidden')
    160 				->addAttribute('name', RequestParams::PARAM_TOKEN)
    161 				->addAttribute('value', $this->token)
    162 		);
    163 
    164 		$formContent->addChild(
    165 			(new CMSElement('input'))
    166 				->addAttribute('type', 'hidden')
    167 				->addAttribute('name', RequestParams::PARAM_ACTION)
    168 				->addAttribute('value', $this->action)
    169 		);
    170 		$formContent->addChild(
    171 			(new CMSElement('input'))
    172 				->addAttribute('type', 'hidden')
    173 				->addAttribute('name', RequestParams::PARAM_SUBACTION)
    174 				->addAttribute('value', $this->subaction)
    175 		);
    176 		$formContent->addChild(
    177 			(new CMSElement('input'))
    178 				->addAttribute('type', 'hidden')
    179 				->addAttribute('name', RequestParams::PARAM_ID)
    180 				->addAttribute('value', $this->id)
    181 		);
    182 
    183 
    184 
    185 		$this->adoptiveElement = $formContent;
    186 
    187 		$form->addChild( $actionBar );
    188 
    189 		return $form;
    190 	}
    191 }