PageRenderTime 79ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/classes/framework/tablebuilder/tablebuilder.php

https://github.com/GinoPane/fantasia
PHP | 512 lines | 410 code | 81 blank | 21 comment | 34 complexity | 680a3da670c4dee949fc9f9cfc17c146 MD5 | raw file
  1. <?php
  2. class FormatType {
  3. const TEMPLATE_FORMAT = 1;
  4. const STRING_FROMAT = 2;
  5. const FUNCTION_FORMAT = 3;
  6. }
  7. class TableBuilder {
  8. protected $_action = null;
  9. protected $_params = array();
  10. protected $_data = null;
  11. protected $_dataSourceSelectSql = null;
  12. protected $_dataSourceFromSql = null;
  13. protected $_dataSourceWhereSql = null;
  14. protected $_dataSourceOrderSql = null;
  15. protected $_fields = array();
  16. protected $_template = null;
  17. protected $_templateBody = null;
  18. protected $_pagerTemplate = null;
  19. protected $_currentPage = 0;
  20. protected $_overall = null;
  21. protected $_perPage = 10;
  22. protected $_pagerLink = null;
  23. protected $_usePagination = false;
  24. protected $_useNumbering = false;
  25. protected $_pagination = null;
  26. protected $_body = null;
  27. protected $_emptyTemplate = null;
  28. protected $_emptyCell = "-";
  29. protected $_fullTable = true;
  30. public function __construct($data = null, $template = "")
  31. {
  32. $this->_data = $data;
  33. $this->_template = $template;
  34. }
  35. public function setSourceFromSql($from)
  36. {
  37. $this->_dataSourceFromSql = $from;
  38. return $this;
  39. }
  40. public function setParams($params)
  41. {
  42. $this->_params = array_merge($params);
  43. return $this;
  44. }
  45. public function getParams()
  46. {
  47. return $this->_params;
  48. }
  49. public function setSourceSelectSql($select)
  50. {
  51. $this->_dataSourceSelectSql = $select;
  52. return $this;
  53. }
  54. public function useFullTable($use = null)
  55. {
  56. if (!is_null($use)) {
  57. $this->_fullTable = (bool)$use;
  58. return $this;
  59. } else {
  60. return $this->_fullTable;
  61. }
  62. }
  63. public function setSourceWhereSql($where)
  64. {
  65. $this->_dataSourceWhereSql = $where;
  66. return $this;
  67. }
  68. public function setSourceOrderSql($order)
  69. {
  70. $this->_dataSourceOrderSql = $order;
  71. return $this;
  72. }
  73. public function setData($data)
  74. {
  75. if (is_array($data)) {
  76. $this->_data = $data;
  77. return $this;
  78. } else {
  79. return false;
  80. }
  81. }
  82. /**
  83. *
  84. * @param string $header
  85. * @param string $dataField
  86. * @param array $params is array of 'key' => 'value' pairs.
  87. *
  88. * @return TableBuilder
  89. */
  90. public function addDataField($header, $dataField, $params = null)
  91. {
  92. $this->_fields[$header] = array('title' => $header,
  93. 'field' => $dataField,
  94. 'params' => $params);
  95. return $this;
  96. }
  97. /**
  98. *
  99. * if $use is set and it is not null, function sets
  100. * numbering mode for table rows
  101. * otherwise it returns current state of numbering
  102. *
  103. * @param bool $use
  104. * @return bool | TableBuilder
  105. */
  106. public function useNumbering($use = null)
  107. {
  108. if (!is_null($use)) {
  109. $this->_useNumbering = (bool)$use;
  110. return $this;
  111. } else {
  112. return $this->_useNumbering;
  113. }
  114. }
  115. public function usePagination($state)
  116. {
  117. $this->_usePagination = (bool)$state;
  118. return $this;
  119. }
  120. public function setPager($currentPage, $perPage, $link, $overall = null)
  121. {
  122. $this->_currentPage = $currentPage;
  123. $this->_overall = $overall;
  124. $this->_perPage = $perPage;
  125. $this->_usePagination = true;
  126. return $this;
  127. }
  128. public function setCurrentPage($currentPage)
  129. {
  130. $this->_currentPage = abs((int)$currentPage);
  131. if (!$this->_currentPage )
  132. {
  133. $this->_currentPage = 1;
  134. }
  135. return $this;
  136. }
  137. public function getCurrentPage()
  138. {
  139. return $this->_currentPage;
  140. }
  141. public function setPagerLink($link)
  142. {
  143. $this->_pagerLink = $link;
  144. return $this;
  145. }
  146. public function setPerPage($perPage)
  147. {
  148. $this->_perPage = abs((int)$perPage);
  149. return $this;
  150. }
  151. public function getPerPage()
  152. {
  153. return $this->_perPage;
  154. }
  155. public function setPagerTemplate($template)
  156. {
  157. $this->_pagerTemplate = $template;
  158. return $this;
  159. }
  160. public function setTemplate($template)
  161. {
  162. if (!is_string($template))
  163. {
  164. return false;
  165. }
  166. else
  167. {
  168. $this->_template = $template;
  169. return $this;
  170. }
  171. }
  172. public function setBodyTemplate($template)
  173. {
  174. if (!is_string($template))
  175. {
  176. return false;
  177. }
  178. else
  179. {
  180. $this->_templateBody = $template;
  181. return $this;
  182. }
  183. }
  184. public function setEmptyDataTemplate($template)
  185. {
  186. if (!is_string($template))
  187. {
  188. return false;
  189. }
  190. else
  191. {
  192. $this->_emptyTemplate = $template;
  193. return $this;
  194. }
  195. }
  196. public function setEmptyCell($cell)
  197. {
  198. $this->_emptyCell = $cell;
  199. }
  200. public function getFields()
  201. {
  202. return $this->_fields;
  203. }
  204. public function getData()
  205. {
  206. return $this->_data;
  207. }
  208. public function getPager()
  209. {
  210. if ($this->_usePagination) {
  211. return $this->_pagination;
  212. } else {
  213. return false;
  214. }
  215. }
  216. public function getBody()
  217. {
  218. return $this->_body;
  219. }
  220. public function getTable($template = null)
  221. {
  222. $this->_prepareData();
  223. return $this->_parseTemplate($template);
  224. }
  225. public function emptyContent()
  226. {
  227. if ($this->_emptyTemplate && is_file($this->_emptyTemplate))
  228. {
  229. ob_start();
  230. include($this->_emptyTemplate);
  231. $empty = ob_get_contents();
  232. ob_end_clean();
  233. return $empty;
  234. }
  235. else
  236. {
  237. return false;
  238. }
  239. }
  240. public function emptyCell()
  241. {
  242. return $this->_emptyCell;
  243. }
  244. public function isEmpty()
  245. {
  246. if (!isset($this->_data) || !count($this->_data))
  247. {
  248. return true;
  249. }
  250. else
  251. {
  252. return false;
  253. }
  254. }
  255. public function getRules()
  256. {
  257. return $this->_aclRules;
  258. }
  259. public function actionAllowed($action)
  260. {
  261. if (isset($this->_aclRules[$action]))
  262. {
  263. return $this->_aclRules[$action];
  264. }
  265. else
  266. {
  267. return false;
  268. }
  269. }
  270. private function _parseTemplate($template)
  271. {
  272. $this->_body = $this->_getTableBody();
  273. $this->_pagination = $this->_getPager();
  274. if ($this->useFullTable())
  275. {
  276. $templateFile = $template ? $template : $this->_template;
  277. if($templateFile && is_file($templateFile))
  278. {
  279. ob_start();
  280. $data = $this;
  281. include($templateFile);
  282. $result = ob_get_contents();
  283. ob_end_clean();
  284. return $result;
  285. }
  286. else
  287. {
  288. return false;
  289. }
  290. }
  291. else
  292. {
  293. return array( 'table' => $this->_clearStringForJson($this->_body),
  294. 'pager' => $this->_clearStringForJson($this->_pagination));
  295. }
  296. }
  297. public function parseFieldTemplate($value, $data)
  298. {
  299. $result = $value;
  300. if (is_file($data['template']))
  301. {
  302. ob_start();
  303. include($data['template']);
  304. $result = ob_get_contents();
  305. ob_end_clean();
  306. }
  307. return $result;
  308. }
  309. private function _clearStringForJson($str)
  310. {
  311. return iconv("cp1251", "utf-8", str_replace(array("\r\n", "\r", "\n", "\t"), '', $str));
  312. }
  313. private function _getTableBody($template = null)
  314. {
  315. $templateFile = $template ? $template : $this->_templateBody;
  316. if($templateFile && is_file($templateFile))
  317. {
  318. ob_start();
  319. $data = $this;
  320. include($templateFile);
  321. $result = ob_get_contents();
  322. ob_end_clean();
  323. return $result;
  324. }
  325. else
  326. {
  327. return false;
  328. }
  329. }
  330. private function _prepareData()
  331. {
  332. $limits = $this->_getLimits();
  333. $orders = $this->_getOrders();
  334. $where = $this->_getFilters();
  335. if (!$this->_dataSourceSelectSql)
  336. {
  337. $this->_dataSourceSelectSql = "SELECT *";
  338. }
  339. $dataSql = $this->_dataSourceSelectSql . " " . $this->_dataSourceFromSql . " " . $where . " " . $orders . " " . $limits;
  340. $dbResult = DB::getInstance()->query($dataSql);
  341. if ($dbResult)
  342. {
  343. $data = $dbResult->fetchAll(PDO::FETCH_ASSOC);
  344. $this->_data = $data;
  345. }
  346. else
  347. {
  348. //var_dump(DB::getInstance()->errorInfo());
  349. }
  350. }
  351. private function _getDataFromSource()
  352. {
  353. $dataSource = new $this->_dataSource();
  354. $this->_getRules($dataSource->getSystemName());
  355. $this->_overall = $dataSource->getRowCount();
  356. //apply filters
  357. //apply orders
  358. $dataSource->addOrderBy(new OrderBy('date', OrderBy::DESC));
  359. //apply limits
  360. $this->_getLimits();
  361. if ($this->_usePagination)
  362. {
  363. $dataSource->setLimit($this->_currentPage * $this->_perPage, $this->_perPage);
  364. }
  365. return $dataSource->selectData(true, "WHERE 1", true, $this->_usePagination);
  366. }
  367. private function _getOrders()
  368. {
  369. $ordersApplied = isset($_REQUEST['order']) ? $_REQUEST['order'] : null;
  370. if (is_array($ordersApplied))
  371. {
  372. foreach($ordersApplied as $field => $order)
  373. {
  374. if (isset($this->_fields[$field]))
  375. {
  376. $this->_fields[$field]['orderDir'] = $order;
  377. }
  378. }
  379. }
  380. return $this->_dataSourceOrderSql;
  381. }
  382. private function _getFilters()
  383. {
  384. return $this->_dataSourceWhereSql;
  385. }
  386. private function _getLimits()
  387. {
  388. $limitSql = "";
  389. if (!$this->_overall)
  390. {
  391. $sql = "SELECT count(*) as overall " . $this->_dataSourceFromSql . " " . $this->_getFilters();
  392. $dbResult = DB::getInstance()->query($sql);
  393. if ($dbResult)
  394. {
  395. $data = $dbResult->fetchAll(PDO::FETCH_ASSOC);
  396. $this->_overall = $data[0]['overall'];
  397. }
  398. }
  399. if ($this->_currentPage > ceil($this->_overall / $this->_perPage))
  400. {
  401. $this->_currentPage = 1;
  402. }
  403. if ($this->_usePagination)
  404. {
  405. $limitSql = "LIMIT " . ($this->_currentPage - 1) * $this->_perPage . ", " . $this->_perPage;
  406. }
  407. return $limitSql;
  408. }
  409. private function _getPager($template = null)
  410. {
  411. $data = array();
  412. $data['link'] = $this->_pagerLink;
  413. $data['current_page'] = $this->_currentPage;
  414. $data['overall'] = $this->_overall;
  415. $data['per_page'] = $this->_perPage;
  416. $templateFile = $template ? $template : $this->_pagerTemplate;
  417. if ($templateFile && is_file($templateFile))
  418. {
  419. ob_start();
  420. include($templateFile);
  421. $pager = ob_get_contents();
  422. ob_end_clean();
  423. return $pager;
  424. }
  425. else
  426. {
  427. return false;
  428. }
  429. }
  430. }
  431. ?>