/core/classes/framework/tablebuilder/tablebuilder.php
PHP | 512 lines | 410 code | 81 blank | 21 comment | 34 complexity | 680a3da670c4dee949fc9f9cfc17c146 MD5 | raw file
- <?php
- class FormatType {
- const TEMPLATE_FORMAT = 1;
- const STRING_FROMAT = 2;
- const FUNCTION_FORMAT = 3;
- }
- class TableBuilder {
- protected $_action = null;
- protected $_params = array();
- protected $_data = null;
- protected $_dataSourceSelectSql = null;
- protected $_dataSourceFromSql = null;
- protected $_dataSourceWhereSql = null;
- protected $_dataSourceOrderSql = null;
- protected $_fields = array();
-
- protected $_template = null;
- protected $_templateBody = null;
- protected $_pagerTemplate = null;
- protected $_currentPage = 0;
- protected $_overall = null;
- protected $_perPage = 10;
- protected $_pagerLink = null;
-
- protected $_usePagination = false;
- protected $_useNumbering = false;
- protected $_pagination = null;
- protected $_body = null;
- protected $_emptyTemplate = null;
- protected $_emptyCell = "-";
- protected $_fullTable = true;
- public function __construct($data = null, $template = "")
- {
- $this->_data = $data;
- $this->_template = $template;
- }
- public function setSourceFromSql($from)
- {
- $this->_dataSourceFromSql = $from;
- return $this;
- }
- public function setParams($params)
- {
- $this->_params = array_merge($params);
- return $this;
- }
- public function getParams()
- {
- return $this->_params;
- }
- public function setSourceSelectSql($select)
- {
- $this->_dataSourceSelectSql = $select;
- return $this;
- }
- public function useFullTable($use = null)
- {
- if (!is_null($use)) {
- $this->_fullTable = (bool)$use;
- return $this;
- } else {
- return $this->_fullTable;
- }
- }
- public function setSourceWhereSql($where)
- {
- $this->_dataSourceWhereSql = $where;
- return $this;
- }
- public function setSourceOrderSql($order)
- {
- $this->_dataSourceOrderSql = $order;
- return $this;
- }
- public function setData($data)
- {
- if (is_array($data)) {
- $this->_data = $data;
- return $this;
- } else {
- return false;
- }
- }
- /**
- *
- * @param string $header
- * @param string $dataField
- * @param array $params is array of 'key' => 'value' pairs.
- *
- * @return TableBuilder
- */
- public function addDataField($header, $dataField, $params = null)
- {
- $this->_fields[$header] = array('title' => $header,
- 'field' => $dataField,
- 'params' => $params);
- return $this;
- }
- /**
- *
- * if $use is set and it is not null, function sets
- * numbering mode for table rows
- * otherwise it returns current state of numbering
- *
- * @param bool $use
- * @return bool | TableBuilder
- */
- public function useNumbering($use = null)
- {
- if (!is_null($use)) {
- $this->_useNumbering = (bool)$use;
- return $this;
- } else {
- return $this->_useNumbering;
- }
- }
- public function usePagination($state)
- {
- $this->_usePagination = (bool)$state;
- return $this;
- }
- public function setPager($currentPage, $perPage, $link, $overall = null)
- {
- $this->_currentPage = $currentPage;
- $this->_overall = $overall;
- $this->_perPage = $perPage;
- $this->_usePagination = true;
- return $this;
- }
- public function setCurrentPage($currentPage)
- {
- $this->_currentPage = abs((int)$currentPage);
- if (!$this->_currentPage )
- {
- $this->_currentPage = 1;
- }
- return $this;
- }
- public function getCurrentPage()
- {
- return $this->_currentPage;
- }
- public function setPagerLink($link)
- {
- $this->_pagerLink = $link;
- return $this;
- }
- public function setPerPage($perPage)
- {
- $this->_perPage = abs((int)$perPage);
- return $this;
- }
- public function getPerPage()
- {
- return $this->_perPage;
- }
- public function setPagerTemplate($template)
- {
- $this->_pagerTemplate = $template;
- return $this;
- }
- public function setTemplate($template)
- {
- if (!is_string($template))
- {
- return false;
- }
- else
- {
- $this->_template = $template;
- return $this;
- }
- }
- public function setBodyTemplate($template)
- {
- if (!is_string($template))
- {
- return false;
- }
- else
- {
- $this->_templateBody = $template;
- return $this;
- }
- }
- public function setEmptyDataTemplate($template)
- {
- if (!is_string($template))
- {
- return false;
- }
- else
- {
- $this->_emptyTemplate = $template;
- return $this;
- }
- }
- public function setEmptyCell($cell)
- {
- $this->_emptyCell = $cell;
- }
- public function getFields()
- {
- return $this->_fields;
- }
- public function getData()
- {
- return $this->_data;
- }
- public function getPager()
- {
- if ($this->_usePagination) {
- return $this->_pagination;
- } else {
- return false;
- }
- }
- public function getBody()
- {
- return $this->_body;
- }
- public function getTable($template = null)
- {
- $this->_prepareData();
- return $this->_parseTemplate($template);
- }
- public function emptyContent()
- {
- if ($this->_emptyTemplate && is_file($this->_emptyTemplate))
- {
- ob_start();
- include($this->_emptyTemplate);
- $empty = ob_get_contents();
- ob_end_clean();
- return $empty;
- }
- else
- {
- return false;
- }
- }
- public function emptyCell()
- {
- return $this->_emptyCell;
- }
- public function isEmpty()
- {
- if (!isset($this->_data) || !count($this->_data))
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public function getRules()
- {
- return $this->_aclRules;
- }
- public function actionAllowed($action)
- {
- if (isset($this->_aclRules[$action]))
- {
- return $this->_aclRules[$action];
- }
- else
- {
- return false;
- }
- }
- private function _parseTemplate($template)
- {
- $this->_body = $this->_getTableBody();
- $this->_pagination = $this->_getPager();
- if ($this->useFullTable())
- {
- $templateFile = $template ? $template : $this->_template;
- if($templateFile && is_file($templateFile))
- {
- ob_start();
- $data = $this;
- include($templateFile);
- $result = ob_get_contents();
- ob_end_clean();
- return $result;
- }
- else
- {
- return false;
- }
- }
- else
- {
- return array( 'table' => $this->_clearStringForJson($this->_body),
- 'pager' => $this->_clearStringForJson($this->_pagination));
- }
- }
- public function parseFieldTemplate($value, $data)
- {
- $result = $value;
- if (is_file($data['template']))
- {
- ob_start();
- include($data['template']);
- $result = ob_get_contents();
- ob_end_clean();
- }
- return $result;
- }
- private function _clearStringForJson($str)
- {
- return iconv("cp1251", "utf-8", str_replace(array("\r\n", "\r", "\n", "\t"), '', $str));
- }
- private function _getTableBody($template = null)
- {
- $templateFile = $template ? $template : $this->_templateBody;
- if($templateFile && is_file($templateFile))
- {
- ob_start();
- $data = $this;
- include($templateFile);
- $result = ob_get_contents();
- ob_end_clean();
- return $result;
- }
- else
- {
- return false;
- }
- }
- private function _prepareData()
- {
- $limits = $this->_getLimits();
- $orders = $this->_getOrders();
- $where = $this->_getFilters();
- if (!$this->_dataSourceSelectSql)
- {
- $this->_dataSourceSelectSql = "SELECT *";
- }
- $dataSql = $this->_dataSourceSelectSql . " " . $this->_dataSourceFromSql . " " . $where . " " . $orders . " " . $limits;
- $dbResult = DB::getInstance()->query($dataSql);
- if ($dbResult)
- {
- $data = $dbResult->fetchAll(PDO::FETCH_ASSOC);
- $this->_data = $data;
- }
- else
- {
- //var_dump(DB::getInstance()->errorInfo());
- }
- }
- private function _getDataFromSource()
- {
- $dataSource = new $this->_dataSource();
- $this->_getRules($dataSource->getSystemName());
- $this->_overall = $dataSource->getRowCount();
- //apply filters
- //apply orders
- $dataSource->addOrderBy(new OrderBy('date', OrderBy::DESC));
- //apply limits
- $this->_getLimits();
-
- if ($this->_usePagination)
- {
- $dataSource->setLimit($this->_currentPage * $this->_perPage, $this->_perPage);
- }
- return $dataSource->selectData(true, "WHERE 1", true, $this->_usePagination);
- }
- private function _getOrders()
- {
- $ordersApplied = isset($_REQUEST['order']) ? $_REQUEST['order'] : null;
- if (is_array($ordersApplied))
- {
- foreach($ordersApplied as $field => $order)
- {
- if (isset($this->_fields[$field]))
- {
- $this->_fields[$field]['orderDir'] = $order;
- }
- }
- }
- return $this->_dataSourceOrderSql;
- }
- private function _getFilters()
- {
- return $this->_dataSourceWhereSql;
- }
- private function _getLimits()
- {
- $limitSql = "";
- if (!$this->_overall)
- {
- $sql = "SELECT count(*) as overall " . $this->_dataSourceFromSql . " " . $this->_getFilters();
- $dbResult = DB::getInstance()->query($sql);
- if ($dbResult)
- {
- $data = $dbResult->fetchAll(PDO::FETCH_ASSOC);
- $this->_overall = $data[0]['overall'];
- }
- }
- if ($this->_currentPage > ceil($this->_overall / $this->_perPage))
- {
- $this->_currentPage = 1;
- }
- if ($this->_usePagination)
- {
- $limitSql = "LIMIT " . ($this->_currentPage - 1) * $this->_perPage . ", " . $this->_perPage;
- }
- return $limitSql;
- }
- private function _getPager($template = null)
- {
- $data = array();
- $data['link'] = $this->_pagerLink;
- $data['current_page'] = $this->_currentPage;
- $data['overall'] = $this->_overall;
- $data['per_page'] = $this->_perPage;
- $templateFile = $template ? $template : $this->_pagerTemplate;
- if ($templateFile && is_file($templateFile))
- {
- ob_start();
- include($templateFile);
- $pager = ob_get_contents();
- ob_end_clean();
- return $pager;
- }
- else
- {
- return false;
- }
- }
- }
- ?>