Completed
Push — master ( 138cdf...e53430 )
by Jean-Christophe
03:16
created

FormTrait::setState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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