Passed
Push — master ( f2c7af...346e2d )
by Jean-Christophe
02:19
created

FormTrait::setState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\common\html\BaseHtml;
9
use Ajax\semantic\components\Form;
10
use Ajax\semantic\html\collections\form\HtmlFormField;
11
use Ajax\semantic\components\validation\FieldValidation;
12
13
/**
14
 * trait used in Widget and HtmlForm
15
 * @author jc
16
 *
17
 */
18
trait FormTrait{
19
20
	/**
21
	 * @return HtmlForm
22
	 */
23
	abstract protected function getForm();
24
	
25
26
	protected function addCompoValidation(Form $compo,HtmlFormField $field){
27
		$validation=$field->getValidation();
28
		if(isset($validation)){
29
			$validation->setIdentifier($field->getDataField()->getIdentifier());
30
			$compo->addFieldValidation($validation);
31
		}
32
	}
33
	
34
	protected function addExtraCompoValidation(Form $compo,FieldValidation $validation){
35
		$compo->addFieldValidation($validation);
36
	}
37
38
	protected function _runValidationParams(Form &$compo,JsUtils $js=NULL){
39
		$form=$this->getForm();
40
		$params=$form->getValidationParams();
41
		if(isset($params["_ajaxSubmit"])){
42
			$compilation=$this->_compileAjaxSubmit($params["_ajaxSubmit"],$js);
43
			$this->onSuccess($compilation);
0 ignored issues
show
Bug introduced by
It seems like $compilation can also be of type array; however, parameter $jsCode of Ajax\semantic\html\colle...\FormTrait::onSuccess() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
			$this->onSuccess(/** @scrutinizer ignore-type */ $compilation);
Loading history...
44
			$form->removeValidationParam("_ajaxSubmit");
45
		}
46
		$compo->addParams($form->getValidationParams());
47
		$form->setBsComponent($compo);
48
		$form->addEventsOnRun($js);
49
	}
50
51
	protected function _compileAjaxSubmit($ajaxSubmit,JsUtils $js=null){
52
		$compilation="";
53
		if(\is_array($ajaxSubmit)){
54
			foreach ($ajaxSubmit as $ajaxSubmitItem){
55
				$compilation.=$ajaxSubmitItem->compile($js);
56
			}
57
		}elseif($ajaxSubmit instanceof AjaxCall){
58
			$compilation=$ajaxSubmit->compile($js);
59
		}
60
		$compilation=str_ireplace("\"","%quote%", $compilation);
61
		return $compilation;
62
	}
63
64
	public function setLoading() {
65
		return $this->getForm()->addToProperty("class", "loading");
66
	}
67
68
	public function setState($state) {
69
		$this->getForm()->addToProperty("class", $state);
70
		return $this;
71
	}
72
73
	public function setAttached($value=true){
74
		$form=$this->getForm();
75
		if($value)
76
			$form->addToPropertyCtrl("class", "attached", array ("attached" ));
77
		return $form;
78
	}
79
80
	public function addErrorMessage(){
81
		return $this->getForm()->addContent((new HtmlMessage(""))->setError());
82
	}
83
84
	public function jsState($state) {
85
		return $this->getForm()->jsDoJquery("addClass", $state);
86
	}
87
88
	/**
89
	 * @param string $event
90
	 * @param string|BaseHtml $identifierOrElement
91
	 * @param string $url
92
	 * @param string $responseElement
93
	 * @param array $parameters
94
	 * @return HtmlForm
95
	 */
96
	public function submitOn($event,$identifierOrElement,$url,$responseElement,$parameters=NULL){
97
		$form=$this->getForm();
98
		if($identifierOrElement  instanceof BaseHtml)
99
			$elem=$identifierOrElement;
100
		else
101
			$elem=$form->getElementById($identifierOrElement, $form->getContent());
102
		if(isset($elem)){
103
			$this->_buttonAsSubmit($elem, $event,$url,$responseElement,$parameters);
104
		}
105
		return $form;
106
	}
107
108
	public function submitOnClick($identifier,$url,$responseElement,$parameters=NULL){
109
		return $this->submitOn("click", $identifier, $url, $responseElement,$parameters);
110
	}
111
112
	public function addSubmit($identifier,$value,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$parameters=NULL){
113
		$bt=$this->getForm()->addButton($identifier, $value,$cssStyle);
114
		return $this->_buttonAsSubmit($bt, "click",$url,$responseElement,$parameters);
115
	}
116
117
	protected function _buttonAsSubmit(BaseHtml &$button,$event,$url,$responseElement=NULL,$parameters=NULL){
118
		$form=$this->getForm();
119
		if(isset($url) && isset($responseElement)){
120
			$button->addEvent($event, "$('#".$form->getIdentifier()."').form('validate form');",true,true);
121
			$this->setSubmitParams($url,$responseElement,$parameters);
122
		}
123
		return $button;
124
	}
125
126
	public function setSubmitParams($url,$responseElement=NULL,$parameters=NULL){
127
		$form=$this->getForm();
128
		$params=["form"=>$form->getIdentifier(),"responseElement"=>$responseElement,"url"=>$url,"stopPropagation"=>true];
129
		if(\is_array($parameters)){
130
			$params=\array_merge($params,$parameters);
131
		}
132
		$form->addValidationParam("_ajaxSubmit", new AjaxCall("postForm", $params));
133
		return $this;
134
	}
135
136
	public function addReset($identifier,$value,$cssStyle=NULL){
137
		$bt=$this->getForm()->addButton($identifier, $value,$cssStyle);
138
		$bt->setProperty("type", "reset");
139
		return $bt;
140
	}
141
142
	/**
143
	 * Callback on each valid field
144
	 * @param string $jsCode
145
	 * @return \Ajax\semantic\html\collections\form\HtmlForm
146
	 */
147
	public function onValid($jsCode){
148
		$form=$this->getForm();
149
		$form->addValidationParam("onValid", "%function(){".$jsCode."}%");
150
		return $form;
151
	}
152
153
	/**
154
	 * Callback if a form is all valid
155
	 * @param string $jsCode can use event and fields parameters
156
	 * @return HtmlForm
157
	 */
158
	public function onSuccess($jsCode){
159
		$form=$this->getForm();
160
		$form->addValidationParam("onSuccess", $jsCode,"%function(event,fields){","}%");
161
		return $form;
162
	}
163
	
164
	public function addExtraFieldRules($fieldname,$rules){
165
		$form=$this->getForm();
166
		$fv=$form->getExtraFieldValidation($fieldname);
167
		foreach ($rules as $rule){
168
			$fv->addRule($rule);
169
		}
170
	}
171
	
172
	public function addExtraFieldRule($fieldname,$type,$prompt=NULL,$value=NULL){
173
		$form=$this->getForm();
174
		$fv=$form->getExtraFieldValidation($fieldname);
175
		$fv->addRule($type,$prompt,$value);
176
	}
177
	
178
	public function setOptional($fieldname,$optional=true){
179
		$form=$this->getForm();
180
		$fv=$form->getExtraFieldValidation($fieldname);
181
		$fv->setOptional($optional);
182
	}
183
184
	public function setActionTarget(string $action, string $target){
185
		$form=$this->getForm();
186
		$form->setProperty('data-target',$target);
187
		$form->setProperty('action',$action);
188
	}
189
}
190