Completed
Push — master ( ca057c...baf19c )
by Jean-Christophe
03:05
created

DataTableFieldAsTrait::getFieldButton()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
42
				$object->setProperty("data-ajax",$this->_instanceViewer->getIdentifier());
43
				if($object->propertyContains("class","visibleover")){
44
					$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...
45
					$object->setProperty("style","visibility:hidden;");
46
				}
47
			}
48
			return $object;
49
		};
50
		return $result;
51
	}
52
53
	/**
54
	 * @param string $caption
55
	 * @return HtmlButton
56
	 */
57
	private function getFieldButton($caption,$visibleHover=true){
58
		$bt= new HtmlButton("",$caption);
59
		if($visibleHover)
60
			$this->_visibleOver($bt);
0 ignored issues
show
Bug introduced by
It seems like _visibleOver() 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...
61
			return $bt;
62
	}
63
64
	/**
65
	 * Inserts a new Button for each row
66
	 * @param string $caption
67
	 * @param callable $callback
68
	 * @param boolean $visibleHover
69
	 * @return \Ajax\semantic\widgets\datatable\DataTable
70
	 */
71
	public function addFieldButton($caption,$visibleHover=true,$callback=null){
72
		$this->addField($this->getCallable("getFieldButton",[$caption,$visibleHover],$callback));
73
		return $this;
74
	}
75
76
	/**
77
	 * Inserts a new Button for each row at col $index
78
	 * @param int $index
79
	 * @param string $caption
80
	 * @param callable $callback
81
	 * @return \Ajax\semantic\widgets\datatable\DataTable
82
	 */
83
	public function insertFieldButton($index,$caption,$visibleHover=true,$callback=null){
84
		$this->insertField($index, $this->getFieldButtonCallable($caption,$visibleHover,$callback));
85
		return $this;
86
	}
87
88
	/**
89
	 * Inserts a new Button for each row in col at $index
90
	 * @param int $index
91
	 * @param string $caption
92
	 * @param callable $callback
93
	 * @return \Ajax\semantic\widgets\datatable\DataTable
94
	 */
95
	public function insertInFieldButton($index,$caption,$visibleHover=true,$callback=null){
96
		$this->insertInField($index, $this->getFieldButtonCallable($caption,$visibleHover,$callback));
97
		return $this;
98
	}
99
100
	private function addDefaultButton($icon,$class=null,$visibleHover=true,$callback=null){
101
		$this->addField($this->getCallable("getDefaultButton",[$icon,$class,$visibleHover],$callback));
102
		return $this;
103
	}
104
105
	private function insertDefaultButtonIn($index,$icon,$class=null,$visibleHover=true,$callback=null){
106
		$this->insertInField($index,$this->getCallable("getDefaultButton",[$icon,$class,$visibleHover],$callback));
107
		return $this;
108
	}
109
110
	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...
111
		$bt=$this->getFieldButton("",$visibleHover);
112
		$bt->asIcon($icon);
113
		if(isset($class))
114
			$bt->addClass($class);
115
			return $bt;
116
	}
117
118
	public function addDeleteButton($visibleHover=true,$generateBehavior=true,$callback=null){
119
		$this->_hasDelete=$generateBehavior;
120
		return $this->addDefaultButton("remove","delete red basic",$visibleHover,$callback);
121
	}
122
123
	public function addEditButton($visibleHover=true,$generateBehavior=true,$callback=null){
124
		$this->_hasEdit=$generateBehavior;
125
		return $this->addDefaultButton("edit","edit basic",$visibleHover,$callback);
126
	}
127
128
	public function addEditDeleteButtons($visibleHover=true,$generateBehavior=true,$callbackEdit=null,$callbackDelete=null){
129
		$this->addEditButton($visibleHover,$generateBehavior,$callbackEdit);
130
		$index=$this->_instanceViewer->visiblePropertiesCount()-1;
131
		$this->insertDeleteButtonIn($index,$visibleHover,$generateBehavior,$callbackDelete);
132
		return $this;
133
	}
134
135
	public function insertDeleteButtonIn($index,$visibleHover=true,$generateBehavior=true,$callback=null){
136
		$this->_hasDelete=$generateBehavior;
137
		return $this->insertDefaultButtonIn($index,"remove","delete red basic",$visibleHover,$callback);
138
	}
139
140
	public function insertEditButtonIn($index,$visibleHover=true,$generateBehavior=true,$callback=null){
141
		$this->_hasEdit=$generateBehavior;
142
		return $this->insertDefaultButtonIn($index,"edit","edit basic",$visibleHover,$callback);
143
	}
144
}