Completed
Push — master ( 4bf172...5f7aee )
by Jean-Christophe
03:35
created

FormTrait::setSubmitParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
1
<?php
2
namespace Ajax\semantic\html\collections\form\traits;
3
4
use Ajax\semantic\html\collections\form\HtmlForm;
5
use Ajax\semantic\html\collections\HtmlMessage;
6
use Ajax\service\AjaxCall;
7
use Ajax\JsUtils;
8
use Ajax\semantic\html\elements\HtmlButton;
9
use Ajax\common\html\BaseHtml;
10
11
/**
12
 * trait used in Widget and HtmlForm
13
 * @author jc
14
 *
15
 */
16
trait FormTrait{
17
18
	/**
19
	 * @return HtmlForm
20
	 */
21
	abstract protected function getForm();
22
23
	protected function addCompoValidation($compo,$field){
24
		$validation=$field->getValidation();
25
		if(isset($validation)){
26
			$validation->setIdentifier($field->getDataField()->getIdentifier());
27
			$compo->addFieldValidation($validation);
28
		}
29
		return $compo;
30
	}
31
32
	protected function _runValidationParams(&$compo,JsUtils $js=NULL){
33
		$form=$this->getForm();
34
		$params=$form->getValidationParams();
35
		if(isset($params["_ajaxSubmit"])){
36
			$compilation=$this->_compileAjaxSubmit($params["_ajaxSubmit"],$js);
37
			$this->onSuccess($compilation);
38
			$form->removeValidationParam("_ajaxSubmit");
39
		}
40
		$compo->addParams($form->getValidationParams());
41
		$form->setBsComponent($compo);
42
		$form->addEventsOnRun($js);
43
	}
44
45
	protected function _compileAjaxSubmit($ajaxSubmit,JsUtils $js=null){
46
		$compilation="";
47
		if(\is_array($ajaxSubmit)){
48
			foreach ($ajaxSubmit as $ajaxSubmitItem){
49
				$compilation.=$ajaxSubmitItem->compile($js);
50
			}
51
		}elseif($ajaxSubmit instanceof AjaxCall){
52
			$compilation=$ajaxSubmit->compile($js);
53
		}
54
		$compilation=str_ireplace("\"","%quote%", $compilation);
55
		return $compilation;
56
	}
57
58
	public function setLoading() {
59
		return $this->getForm()->addToProperty("class", "loading");
60
	}
61
62
	public function setState($state) {
63
		$this->getForm()->addToProperty("class", $state);
64
		return $this;
65
	}
66
67
	public function setAttached($value=true){
68
		$form=$this->getForm();
69
		if($value)
70
			$form->addToPropertyCtrl("class", "attached", array ("attached" ));
71
		return $form;
72
	}
73
74
	public function addErrorMessage(){
75
		return $this->getForm()->addContent((new HtmlMessage(""))->setError());
76
	}
77
78
	public function jsState($state) {
79
		return $this->getForm()->jsDoJquery("addClass", $state);
80
	}
81
82
	/**
83
	 * @param string $event
84
	 * @param string|BaseHtml $identifierOrElement
85
	 * @param string $url
86
	 * @param string $responseElement
87
	 * @param array $parameters
88
	 * @return HtmlForm
89
	 */
90
	public function submitOn($event,$identifierOrElement,$url,$responseElement,$parameters=NULL){
91
		$form=$this->getForm();
92
		if($identifierOrElement  instanceof BaseHtml)
93
			$elem=$identifierOrElement;
94
		else
95
			$elem=$form->getElementById($identifierOrElement, $form->getContent());
96
		if(isset($elem)){
97
			$this->_buttonAsSubmit($elem, $event,$url,$responseElement,$parameters);
98
		}
99
		return $form;
100
	}
101
102
	public function submitOnClick($identifier,$url,$responseElement,$parameters=NULL){
103
		return $this->submitOn("click", $identifier, $url, $responseElement,$parameters);
104
	}
105
106
	public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$parameters=NULL){
107
		$bt=$this->getForm()->addButton($identifier, $value,$cssStyle);
108
		return $this->_buttonAsSubmit($bt, "click",$url,$responseElement,$parameters);
109
	}
110
111
	protected function _buttonAsSubmit(BaseHtml &$button,$event,$url,$responseElement=NULL,$parameters=NULL){
112
		$form=$this->getForm();
113
		if(isset($url) && isset($responseElement)){
114
			$button->addEvent($event, "$('#".$form->getIdentifier()."').form('validate form');",true,true);
115
			$this->setSubmitParams($url,$responseElement,$parameters);
116
		}
117
		return $button;
118
	}
119
120
	public function setSubmitParams($url,$responseElement=NULL,$parameters=NULL){
121
		$form=$this->getForm();
122
		$params=["form"=>$form->getIdentifier(),"responseElement"=>$responseElement,"url"=>$url,"stopPropagation"=>true];
123
		if(\is_array($parameters))
124
			$params=\array_merge($params,$parameters);
125
		$form->addValidationParam("_ajaxSubmit", new AjaxCall("postForm", $params));
126
		return $this;
127
	}
128
129
	public function addReset($identifier,$value,$cssStyle=NULL){
130
		$bt=$this->getForm()->addButton($identifier, $value,$cssStyle);
131
		$bt->setProperty("type", "reset");
132
		return $bt;
133
	}
134
135
	/**
136
	 * Callback on each valid field
137
	 * @param string $jsCode
138
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
139
	 */
140
	public function onValid($jsCode){
141
		$form=$this->getForm();
142
		$form->addValidationParam("onValid", "%function(){".$jsCode."}%");
143
		return $form;
144
	}
145
146
	/**
147
	 * Callback if a form is all valid
148
	 * @param string $jsCode can use event and fields parameters
149
	 * @return HtmlForm
150
	 */
151
	public function onSuccess($jsCode){
152
		$form=$this->getForm();
153
		$form->addValidationParam("onSuccess", $jsCode,"%function(event,fields){","}%");
154
		return $form;
155
	}
156
}