PageRenderTime 51ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Chumper/Datatable/Table.php

https://github.com/felipepastor/Datatable
PHP | 379 lines | 230 code | 48 blank | 101 comment | 28 complexity | d2a10d637fb7b770439f67253c6ef757 MD5 | raw file
  1. <?php namespace Chumper\Datatable;
  2. use Exception;
  3. use Illuminate\Support\Facades\Request;
  4. use Illuminate\Support\Facades\View;
  5. use Illuminate\Support\Facades\Config;
  6. /**
  7. * Class Table
  8. * @package Chumper\Datatable
  9. */
  10. class Table {
  11. /**
  12. * @var array
  13. */
  14. private $config = array();
  15. /**
  16. * @var array
  17. */
  18. private $columns = array();
  19. /**
  20. * @var array
  21. */
  22. private $options = array();
  23. /**
  24. * @var array
  25. */
  26. private $callbacks = array();
  27. /**
  28. * Values to be sent to custom templates
  29. *
  30. * @var array
  31. */
  32. private $customValues = array();
  33. /**
  34. * @var array
  35. */
  36. private $data = array();
  37. /**
  38. * @var boolean Determines if the template should echo the javascript
  39. */
  40. private $noScript = false;
  41. /**
  42. * @var String The name of the id the table will have later
  43. */
  44. protected $idName;
  45. /**
  46. * @var String The name of the class the table will have later
  47. */
  48. protected $className;
  49. /**
  50. * @var String The view used to render the table
  51. */
  52. protected $table_view;
  53. /**
  54. * @var String The view used to render the javascript
  55. */
  56. protected $script_view;
  57. /**
  58. * @var boolean indicates if the mapping was already added to the options
  59. */
  60. private $createdMapping = true;
  61. /**
  62. * @var array name of mapped columns
  63. */
  64. private $aliasColumns = array();
  65. function __construct()
  66. {
  67. $this->config = Config::get('datatable::table');
  68. $this->setId( $this->config['id'] );
  69. $this->setClass( $this->config['class'] );
  70. $this->setOptions( $this->config['options'] );
  71. $this->setCallbacks( $this->config['callbacks'] );
  72. $this->noScript = $this->config['noScript'];
  73. $this->table_view = $this->config['table_view'];
  74. $this->script_view = $this->config['script_view'];
  75. }
  76. /**
  77. * @return $this
  78. */
  79. public function addColumn()
  80. {
  81. foreach (func_get_args() as $title)
  82. {
  83. if(is_array($title))
  84. {
  85. foreach ($title as $mapping => $arrayTitle)
  86. {
  87. $this->columns[] = $arrayTitle;
  88. $this->aliasColumns[] = $mapping;
  89. if(is_string($mapping))
  90. {
  91. $this->createdMapping = false;
  92. }
  93. }
  94. }
  95. else
  96. {
  97. $this->columns[] = $title;
  98. $this->aliasColumns[] = count($this->aliasColumns)+1;
  99. }
  100. }
  101. return $this;
  102. }
  103. /**
  104. * @return int
  105. */
  106. public function countColumns()
  107. {
  108. return count($this->columns);
  109. }
  110. /**
  111. * @return $this
  112. */
  113. public function removeOption($key)
  114. {
  115. if(isset($this->options[$key])) unset($this->options[$key]);
  116. return $this;
  117. }
  118. /**
  119. * @return $this
  120. * @throws \Exception
  121. */
  122. public function setOptions()
  123. {
  124. if(func_num_args() == 2)
  125. {
  126. $this->options[func_get_arg(0)] =func_get_arg(1);
  127. }
  128. else if(func_num_args() == 1 && is_array(func_get_arg(0)))
  129. {
  130. foreach (func_get_arg(0) as $key => $option)
  131. {
  132. $this->options[$key] = $option;
  133. }
  134. }
  135. else
  136. throw new Exception('Invalid number of options provided for the method "setOptions"');
  137. return $this;
  138. }
  139. /**
  140. * @return $this
  141. * @throws \Exception
  142. */
  143. public function setCallbacks()
  144. {
  145. if(func_num_args() == 2)
  146. {
  147. $this->callbacks[func_get_arg(0)] = func_get_arg(1);
  148. }
  149. else if(func_num_args() == 1 && is_array(func_get_arg(0)))
  150. {
  151. foreach (func_get_arg(0) as $key => $value)
  152. {
  153. $this->callbacks[$key] = $value;
  154. }
  155. }
  156. else
  157. throw new Exception('Invalid number of callbacks provided for the method "setCallbacks"');
  158. return $this;
  159. }
  160. /**
  161. * @return $this
  162. * @throws \Exception
  163. */
  164. public function setCustomValues()
  165. {
  166. if(func_num_args() == 2)
  167. {
  168. $this->customValues[func_get_arg(0)] = func_get_arg(1);
  169. }
  170. else if(func_num_args() == 1 && is_array(func_get_arg(0)))
  171. {
  172. foreach (func_get_arg(0) as $key => $value)
  173. {
  174. $this->customValues[$key] = $value;
  175. }
  176. }
  177. else
  178. throw new Exception('Invalid number of custom values provided for the method "setCustomValues"');
  179. return $this;
  180. }
  181. /**
  182. * @param array $data
  183. * @return $this
  184. */
  185. public function setData(array $data)
  186. {
  187. $this->data = $data;
  188. return $this;
  189. }
  190. /**
  191. * @param $url
  192. * @return $this
  193. */
  194. public function setUrl($url)
  195. {
  196. $this->options['sAjaxSource'] = $url;
  197. $this->options['bServerSide'] = true;
  198. return $this;
  199. }
  200. /**
  201. * @return array
  202. */
  203. public function getOptions()
  204. {
  205. return $this->options;
  206. }
  207. /**
  208. * @return array
  209. */
  210. public function getCallbacks()
  211. {
  212. return $this->callbacks;
  213. }
  214. /**
  215. * @return array
  216. */
  217. public function getCustomValues()
  218. {
  219. return $this->customValues;
  220. }
  221. /**
  222. * @return array
  223. */
  224. public function getData()
  225. {
  226. return $this->data;
  227. }
  228. /**
  229. * @param null $view
  230. * @return mixed
  231. */
  232. public function render($view = null)
  233. {
  234. if( ! is_null($view))
  235. $this->table_view = $view;
  236. if(!isset($this->options['sAjaxSource']))
  237. {
  238. $this->setUrl(Request::url());
  239. }
  240. // create mapping for frontend
  241. if(!$this->createdMapping)
  242. {
  243. $this->createMapping();
  244. }
  245. return View::make($this->table_view,array(
  246. 'options' => $this->options,
  247. 'callbacks' => $this->callbacks,
  248. 'values' => $this->customValues,
  249. 'data' => $this->data,
  250. 'columns' => array_combine($this->aliasColumns,$this->columns),
  251. 'noScript' => $this->noScript,
  252. 'id' => $this->idName,
  253. 'class' => $this->className,
  254. ));
  255. }
  256. /**
  257. * Instructs the table not to echo the javascript
  258. *
  259. * @return $this
  260. */
  261. public function noScript()
  262. {
  263. $this->noScript = true;
  264. return $this;
  265. }
  266. public function script($view = null)
  267. {
  268. if( ! is_null($view))
  269. $this->script_view = $view;
  270. // create mapping for frontend
  271. if(!$this->createdMapping)
  272. {
  273. $this->createMapping();
  274. }
  275. return View::make($this->script_view,array(
  276. 'options' => $this->options,
  277. 'callbacks' => $this->callbacks,
  278. 'id' => $this->idName,
  279. ));
  280. }
  281. public function getId()
  282. {
  283. return $this->idName;
  284. }
  285. public function setId($id = '')
  286. {
  287. $this->idName = empty($id)? str_random(8) : $id;
  288. return $this;
  289. }
  290. public function getClass()
  291. {
  292. return $this->className;
  293. }
  294. public function setClass($class)
  295. {
  296. $this->className = $class;
  297. return $this;
  298. }
  299. public function setAliasMapping($value)
  300. {
  301. $this->createdMapping = !$value;
  302. return $this;
  303. }
  304. //--------------------PRIVATE FUNCTIONS
  305. private function createMapping()
  306. {
  307. // set options for better handling
  308. // merge with existing options
  309. if(!array_key_exists('aoColumns', $this->options))
  310. {
  311. $this->options['aoColumns'] = array();
  312. }
  313. $matching = array();
  314. $i = 0;
  315. foreach($this->aliasColumns as $name)
  316. {
  317. if(array_key_exists($i,$this->options['aoColumns']))
  318. {
  319. $this->options['aoColumns'][$i] = array_merge_recursive($this->options['aoColumns'][$i],array('mData' => $name));
  320. }
  321. else
  322. {
  323. $this->options['aoColumns'][$i] = array('mData' => $name);
  324. }
  325. $i++;
  326. }
  327. $this->createdMapping = true;
  328. //dd($matching);
  329. return $matching;
  330. }
  331. }