Passed
Push — master ( 42337f...468e47 )
by Jean-Christophe
02:24
created

JsonDataTable::_associatePaginationBehavior()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Ajax\semantic\widgets\datatable;
4
5
use Ajax\service\JReflection;
6
use Ajax\common\html\BaseHtml;
7
use Ajax\service\AjaxCall;
8
use Ajax\JsUtils;
9
10
/**
11
 * @author jc
12
 * a DataTable refreshed with JSON
13
 * @since 2.2.2
14
 */
15
class JsonDataTable extends DataTable {
16
	protected $_modelClass="_jsonArrayModel";
17
	protected $_rowModelCallback;
18
19
	public function __construct($identifier, $model, $modelInstance=NULL) {
20
		parent::__construct($identifier, $model, $modelInstance);
21
		$this->_rowClass="_json";
22
	}
23
24
	protected function _generateContent($table){
25
		$this->_addRowModel($table);
26
		parent::_generateContent($table);
27
	}
28
29
	protected function _addRowModel($table){
30
		$row=$this->_createRow($table, $this->_modelClass);
31
		$row->setProperty("style","display:none;");
32
		$table->getBody()->_addRow($row);
33
	}
34
35
	protected function _createRow($table,$rowClass){
36
		$object=JReflection::jsonObject($this->_model);
37
		if(isset($this->_rowModelCallback)){
38
			$callback=$this->_rowModelCallback;
39
			$callback($object);
40
		}
41
		$row=$this->_generateRow($object, $table,"_jsonArrayChecked");
42
		$row->setClass($rowClass);
43
		return $row;
44
	}
45
46
	/**
47
	 * {@inheritDoc}
48
	 * @see DataTable::_associatePaginationBehavior()
49
	 */
50
	protected function _associatePaginationBehavior(JsUtils $js=NULL,$offset=null){
51
		$callback=null;
52
		$menu=$this->_pagination->getMenu();
53
		
54
		if(isset($js)){
55
			$id=$this->identifier;
56
			$callback=$js->getScript($offset).$this->getHtmlComponent()->getInnerScript();
57
			$callback.=$js->trigger("#".$id." [name='selection[]']","change",false)."$('#".$id." tbody .ui.checkbox').checkbox();".$js->execOn("change", "#".$id." [name='selection[]']", $this->_getCheckedChange($js));
58
			$callback.=$this->_generatePaginationScript($id);
59
		}
60
		if(isset($this->_urls["refresh"])){
61
			$js->jsonArrayOn("click", "#".$menu->getIdentifier()." a","#".$this->_identifier." tr.".$this->_modelClass, $this->_urls["refresh"],"post",["params"=>"{'p':$(this).attr('data-page')}","jsCallback"=>$callback]);
0 ignored issues
show
Bug introduced by
The method jsonArrayOn() does not exist on null. ( Ignorable by Annotation )

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

61
			$js->/** @scrutinizer ignore-call */ 
62
        jsonArrayOn("click", "#".$menu->getIdentifier()." a","#".$this->_identifier." tr.".$this->_modelClass, $this->_urls["refresh"],"post",["params"=>"{'p':$(this).attr('data-page')}","jsCallback"=>$callback]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
		}
63
	}
64
	
65
	protected function _generatePaginationScript($id){
66
		return ";var page=parseInt($(self).attr('data-page')) || 1;var pages_visibles=$('#pagination-{$id} .item').length-2;
67
			var lastPage=$('#pagination-{$id} ._lastPage');
68
			var middle= Math.ceil((pages_visibles-1)/ 2);
69
			var first=Math.max(1,page-middle);var max=lastPage.attr('data-max');
70
			var last=Math.min(max,first+pages_visibles-1);
71
			if(last-pages_visibles+1>=0)
72
				first=Math.min(first,last-pages_visibles+1);
73
			var number=first;
74
			$('#pagination-{$id} .item.pageNum').each(function(){
75
				$(this).attr('data-page',number);
76
				$(this).html(number);
77
				number++;
78
			});
79
			$('#pagination-{$id} .item').removeClass('active');
80
			$('#pagination-{$id} [data-page='+page+']:not(.no-active)').addClass('active');
81
			$('#pagination-{$id} ._firstPage').attr('data-page',Math.max(1,page-1));
82
			lastPage.attr('data-page',Math.min(lastPage.attr('data-max'),page+1));
83
			$('#{$id}').trigger('pageChange');$('#pagination-{$id}').show();";
84
	}
85
	protected function _compileSearchFieldBehavior(JsUtils $js=NULL){
86
		
87
	}
88
	protected function _associateSearchFieldBehavior(JsUtils $js=NULL,$offset=null){
89
		if(isset($this->_searchField) && isset($js) && isset($this->_urls["refresh"])){
90
			$callback=null;
0 ignored issues
show
Unused Code introduced by
The assignment to $callback is dead and can be removed.
Loading history...
91
			$id=$this->identifier;
92
			$callback=$js->getScript($offset).$this->getHtmlComponent()->getInnerScript();
93
			$callback.=$js->trigger("#".$id." [name='selection[]']","change",false)."$('#".$id." tbody .ui.checkbox').checkbox();".$js->execOn("change", "#".$id." [name='selection[]']", $this->_getCheckedChange($js));
94
			$callback.="$('#pagination-{$id}').hide();$('#".$this->identifier."').trigger('searchTerminate',[$(self).val()]);";
95
			$js->jsonArrayOn("change", "#".$this->_searchField->getDataField()->getIdentifier(),"#".$this->_identifier." tr.".$this->_modelClass, $this->_urls["refresh"],"post",["params"=>"{'s':$(self).val()}","jsCallback"=>$callback]);
96
		}
97
	}
98
	
99
	
100
101
	/**
102
	 * Returns a new AjaxCall object, must be compiled using $jquery object
103
	 * @param string $url
104
	 * @param string $method
105
	 * @param string $params
106
	 * @param string $jsCallback
107
	 * @return AjaxCall
108
	 */
109
	public function jsJsonArray($url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
110
		$parameters=\array_merge($parameters,["modelSelector"=>"#".$this->_identifier." tr.".$this->_modelClass,"url"=>$url,"method"=>$method,"params"=>$params,"jsCallback"=>$jsCallback]);
111
		return new AjaxCall("jsonArray", $parameters);
112
	}
113
114
	public function jsClear(){
115
		return "$('#{$this->identifier} tbody').find('._json').remove();";
116
	}
117
118
	public function clearOn(BaseHtml $element,$event, $stopPropagation=false, $preventDefault=false){
119
		return $element->addEvent($event, $this->jsClear(),$stopPropagation,$preventDefault);
120
	}
121
122
	public function clearOnClick(BaseHtml $element,$stopPropagation=false, $preventDefault=false){
123
		return $this->clearOn($element, "click",$stopPropagation,$preventDefault);
124
	}
125
126
	public function jsonArrayOn(BaseHtml $element,$event,$url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
127
		return $element->_addEvent($event, $this->jsJsonArray($url,$method,$params,$jsCallback,$parameters));
128
	}
129
130
	public function jsonArrayOnClick(BaseHtml $element,$url, $method="get", $params="{}", $jsCallback=NULL,$parameters=[]){
131
		return $this->jsonArrayOn($element, "click", $url,$method,$params,$jsCallback,$parameters);
132
	}
133
134
	/**
135
	 * Paginates the DataTable element with a Semantic HtmlPaginationMenu component
136
	 * @param number $page the active page number
137
	 * @param number $total_rowcount the total number of items
138
	 * @param number $items_per_page The number of items per page
139
	 * @param number $pages_visibles The number of visible pages in the Pagination component
140
	 * @return DataTable
141
	 */
142
	public function paginate($page,$total_rowcount,$items_per_page=10,$pages_visibles=null){
143
		return parent::paginate($page, $total_rowcount,$items_per_page,$pages_visibles);
144
	}
145
146
	public function setRowModelCallback($_rowModelCallback) {
147
		$this->_rowModelCallback=$_rowModelCallback;
148
		return $this;
149
	}
150
}
151