Completed
Push — master ( baf19c...6831db )
by Jean-Christophe
03:30
created

DataTableFieldAsTrait::getCallable()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
nc 1
cc 5
eloc 13
nop 3
1
<?php
2
namespace Ajax\semantic\widgets\datatable;
3
use Ajax\semantic\html\elements\HtmlButton;
4
use Ajax\semantic\widgets\base\InstanceViewer;
5
use Ajax\semantic\html\base\HtmlSemDoubleElement;
6
7
/**
8
 * @author jc
9
 * @property boolean $_hasDelete
10
 * @property boolean $_hasEdit
11
 * @property boolean $_visibleHover;
12
 * @property InstanceViewer $_instanceViewer
13
 */
14
trait DataTableFieldAsTrait{
15
	abstract public function addField($field);
16
	abstract public function insertField($index,$field);
17
	abstract public function insertInField($index,$field);
18
	/**
19
	 * @param string $caption
20
	 * @param callable $callback
21
	 * @param boolean $visibleHover
22
	 * @return callable
23
	 */
24
	private function getFieldButtonCallable($caption,$visibleHover=true,$callback=null){
25
		return $this->getCallable("getFieldButton",[$caption,$visibleHover],$callback);
26
	}
27
28
	/**
29
	 * @param callable $thisCallback
30
	 * @param array $parameters
31
	 * @param callable $callback
32
	 * @return callable
33
	 */
34
	private function getCallable($thisCallback,$parameters,$callback=null){
35
		$result=function($instance) use($thisCallback,$parameters,$callback){
36
			$object=call_user_func_array(array($this,$thisCallback), $parameters);
37
			if(isset($callback)){
38
				if(\is_callable($callback)){
39
					$callback($object,$instance);
40
				}
41
			}
42
			if($object instanceof HtmlSemDoubleElement){
43
				$object->setProperty("data-ajax",$this->_instanceViewer->getIdentifier());
44
				if($object->propertyContains("class","visibleover")){
45
					$this->_visibleHover=true;
0 ignored issues
show
Bug introduced by
The property _visibleHover does not seem to exist. Did you mean _visibleHover;?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
46
					$object->setProperty("style","visibility:hidden;");
47
				}
48
			}
49
			return $object;
50
		};
51
		return $result;
52
	}
53
54
	/**
55
	 * @param string $caption
56
	 * @return HtmlButton
57
	 */
58
	private function getFieldButton($caption,$visibleHover=true){
59
		$bt= new HtmlButton("",$caption);
60
		if($visibleHover)
61
			$this->_visibleOver($bt);
62
			return $bt;
63
	}
64
65
	/**
66
	 * Creates a submit button at $index position
67
	 * @param int $index
68
	 * @param string $cssStyle
69
	 * @param string $url
70
	 * @param string $responseElement
71
	 * @param array $attributes associative array (<b>ajax</b> key is for ajax post)
72
	 * @return \Ajax\semantic\widgets\datatable\DataTable
73
	 */
74
	public function fieldAsSubmit($index,$cssStyle=NULL,$url=NULL,$responseElement=NULL,$attributes=NULL){
75
		return $this->_fieldAs(function($id,$name,$value,$caption) use ($url,$responseElement,$cssStyle,$index,$attributes){
0 ignored issues
show
Unused Code introduced by
The parameter $caption is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Bug introduced by
It seems like _fieldAs() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
76
			$button=new HtmlButton($id,$value,$cssStyle);
77
			$button->postOnClick($url,"$(event.target).closest('tr').find(':input').serialize()",$responseElement,$attributes["ajax"]);
78
			if(!isset($attributes["visibleHover"]) || $attributes["visibleHover"])
79
				$this->_visibleOver($button);
80
				return $button;
81
		}, $index,$attributes);
82
	}
83
84
	protected function _visibleOver($element){
85
		$this->_visibleHover=true;
0 ignored issues
show
Bug introduced by
The property _visibleHover does not seem to exist. Did you mean _visibleHover;?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
86
		return $element->addToProperty("class", "visibleover")->setProperty("style","visibility:hidden;");
87
	}
88
89
	/**
90
	 * Inserts a new Button for each row
91
	 * @param string $caption
92
	 * @param callable $callback
93
	 * @param boolean $visibleHover
94
	 * @return \Ajax\semantic\widgets\datatable\DataTable
95
	 */
96
	public function addFieldButton($caption,$visibleHover=true,$callback=null){
97
		$this->addField($this->getCallable("getFieldButton",[$caption,$visibleHover],$callback));
98
		return $this;
99
	}
100
101
	/**
102
	 * Inserts a new Button for each row at col $index
103
	 * @param int $index
104
	 * @param string $caption
105
	 * @param callable $callback
106
	 * @return \Ajax\semantic\widgets\datatable\DataTable
107
	 */
108
	public function insertFieldButton($index,$caption,$visibleHover=true,$callback=null){
109
		$this->insertField($index, $this->getFieldButtonCallable($caption,$visibleHover,$callback));
110
		return $this;
111
	}
112
113
	/**
114
	 * Inserts a new Button for each row in col at $index
115
	 * @param int $index
116
	 * @param string $caption
117
	 * @param callable $callback
118
	 * @return \Ajax\semantic\widgets\datatable\DataTable
119
	 */
120
	public function insertInFieldButton($index,$caption,$visibleHover=true,$callback=null){
121
		$this->insertInField($index, $this->getFieldButtonCallable($caption,$visibleHover,$callback));
122
		return $this;
123
	}
124
125
	private function addDefaultButton($icon,$class=null,$visibleHover=true,$callback=null){
126
		$this->addField($this->getCallable("getDefaultButton",[$icon,$class,$visibleHover],$callback));
127
		return $this;
128
	}
129
130
	private function insertDefaultButtonIn($index,$icon,$class=null,$visibleHover=true,$callback=null){
131
		$this->insertInField($index,$this->getCallable("getDefaultButton",[$icon,$class,$visibleHover],$callback));
132
		return $this;
133
	}
134
135
	private function getDefaultButton($icon,$class=null,$visibleHover=true){
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
136
		$bt=$this->getFieldButton("",$visibleHover);
137
		$bt->asIcon($icon);
138
		if(isset($class))
139
			$bt->addClass($class);
140
			return $bt;
141
	}
142
143
	public function addDeleteButton($visibleHover=true,$generateBehavior=true,$callback=null){
144
		$this->_hasDelete=$generateBehavior;
145
		return $this->addDefaultButton("remove","delete red basic",$visibleHover,$callback);
146
	}
147
148
	public function addEditButton($visibleHover=true,$generateBehavior=true,$callback=null){
149
		$this->_hasEdit=$generateBehavior;
150
		return $this->addDefaultButton("edit","edit basic",$visibleHover,$callback);
151
	}
152
153
	public function addEditDeleteButtons($visibleHover=true,$generateBehavior=true,$callbackEdit=null,$callbackDelete=null){
154
		$this->addEditButton($visibleHover,$generateBehavior,$callbackEdit);
155
		$index=$this->_instanceViewer->visiblePropertiesCount()-1;
156
		$this->insertDeleteButtonIn($index,$visibleHover,$generateBehavior,$callbackDelete);
157
		return $this;
158
	}
159
160
	public function insertDeleteButtonIn($index,$visibleHover=true,$generateBehavior=true,$callback=null){
161
		$this->_hasDelete=$generateBehavior;
162
		return $this->insertDefaultButtonIn($index,"remove","delete red basic",$visibleHover,$callback);
163
	}
164
165
	public function insertEditButtonIn($index,$visibleHover=true,$generateBehavior=true,$callback=null){
166
		$this->_hasEdit=$generateBehavior;
167
		return $this->insertDefaultButtonIn($index,"edit","edit basic",$visibleHover,$callback);
168
	}
169
}