Completed
Push — master ( 04d259...772d83 )
by Jean-Christophe
03:54
created

FormTrait::setAttached()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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