setOptions($options); } public function __get($key) { if ('_' == $key[0]) { throw new Qs_ViewController_Exception(sprintf('Cannot retrieve value for protected/private property "%s"', $key)); } if ('viewController' == $key) { return $this->_viewController; } if ('paginator' == $key) { return $this->_getPaginator(); } if (!isset($this->$key)) { return null; } return $this->$key; } public function __set($key, $value) { $this->setOption($key, $value); } public function setOptions($options) { if (isset($options['options'])) { unset($options['options']); } foreach ($options as $key => $value) { $method = 'set' . ucfirst($key); if (method_exists($this, $method)) { $this->$method($value); } else { $this->setOption($key, $value); } } return $this; } public function setOption($name, $value) { $name = (string) $name; if ('_' == $name[0]) { throw new Qs_ViewController_Exception(sprintf('Invalid attribute "%s"; must not contain a leading underscore', $name)); } if (null === $value) { unset($this->$name); } else { $this->$name = $value; } return $this; } public function setDefaultIpp($ipp) { $this->_defaultIpp = $ipp; return $this; } public function getOptions() { $options = get_object_vars($this); foreach ($options as $key => &$value) { if ('_' == substr($key, 0, 1)) { unset($options[$key]); continue; } $method = 'get' . ucfirst($key); if (method_exists($this, $method)) { $value = $this->$method(); } } return $options; } public function setSeoPaginator($flag = true) { $this->_seoPaginator = (bool) $flag; } public function setViewController($viewController) { if ((!$viewController instanceof Qs_ViewController)) { throw new Qs_ViewController_Exception('Invalid viewController passed to list object'); } $this->_viewController = $viewController; return $this; } public function addUrlVariables(array $variables) { $this->_urlVariables = array_unique(array_merge($this->_urlVariables, $variables)); return $this; } protected function _getPaginator() { if (null === $this->_paginator) { $this->_paginator = Qs_Paginator::factory($this->viewController->dataObj->getPaginatorAdapter()); } return $this->_paginator; } public function getOrderBy() { if (null === $this->orderBy) { $orderBy = isset($_GET['orderBy']) ? $_GET['orderBy'] : $this->defaultOrderBy; $orderByParams = explode(' ', $orderBy); if (count($orderByParams) >= 2) { list($field, $order) = $orderByParams; } elseif (count($orderByParams) == 1) { $field = current($orderByParams); $order = ''; } $validField = false; if (!empty($field)) { foreach ($this->_columns as $column) { if (isset($column['orderBy']) && $field == $column['orderBy']) { $validField = true; break; } } } if (!$validField && ($this->defaultOrderBy != $orderBy || !$this->defaultOrderBy)) { $field = ''; foreach ($this->_columns as $column) { if (isset($column['orderBy'])) { $field = $column['orderBy']; break; } } } if (!empty($field)) { if ('DESC' != $order) { $order = ''; } else { $order = 'DESC'; } $this->orderBy = trim("{$field} {$order}"); } else if (!empty($this->defaultOrderBy)) { $this->orderBy = $this->defaultOrderBy; } else { $this->orderBy = false; } } return $this->orderBy; } public function setPageNumber($number) { $this->_pageNumber = (int) $number; } public function getPageNumber() { if (null === $this->_pageNumber) { if ($this->_seoPaginator && count($this->viewController->getRestParams())) { $this->_pageNumber = end($this->viewController->getRestParams()); } else { $this->_pageNumber = (int) Qs_Request::getGetValue('page', 1); } } return $this->_pageNumber; } public function setIpp($ipp) { $this->_ipp = $ipp; return $this; } protected function _getIpp() { if ($this->isDisabledLimit()) { $this->_ipp = false; } if (null === $this->_ipp) { $this->_ipp = Qs_Request::getGetValue('ipp', $this->_defaultIpp); } return $this->_ipp; } public function isDisabledLimit() { return $this->_isDisabledLimit; } public function setIsDisabledLimit($flag = true) { $this->_isDisabledLimit = (bool) $flag; } public function getLimitPage() { if ($this->isDisabledLimit()) { $this->_limitPage = false; } if (null === $this->_limitPage) { $this->_limitPage = array($this->getPageNumber(), $this->_getIpp()); } return $this->_limitPage; } public function clearColumns() { $this->_columns = array(); return $this; } public function setColumns($columns) { $this->clearColumns(); $this->addColumns($columns); return $this; } public function addColumns($columns) { if ($columns instanceof Zend_Config) { $columns = $columns->toArray(); } if (!is_array($columns) || empty($columns)) { throw new Qs_ViewController_Exception('Invalid columns'); } foreach ($columns as $column) { if (!isset($column['type'])) { throw new Qs_ViewController_Exception('Undefined column type'); } if (!isset($column['name'])) { throw new Qs_ViewController_Exception('Undefined column name'); } $this->addColumn($column); } return $this; } public function addColumn($type, $name, $options = array()) { if ($options instanceof Zend_Config) { $options = $options->toArray(); } $prepareMethod = '_prepare' . ucfirst($type) . 'Column'; if (method_exists($this, $prepareMethod)) { $this->$prepareMethod($options); } else { $this->_prepareColumn($type, $name, $options); } if (!array_key_exists('params', $options)) { $options['params'] = array($name); } if (!array_key_exists('title', $options)) { $options['title'] = ucfirst(preg_replace('/([a-z])([A-Z])/', '$1 $2', $name)); } if (!isset($options['template'])) { $options['template'] = $type . '.tpl'; } if ('.' === dirname($options['template'])) { $options['template'] = $this->viewController->getTemplate('cells/' . $options['template']); } $this->_columns[$name] = $options; return $this; } public function removeColumn($name) { if (isset($this->_columns[$name])) { unset($this->_columns[$name]); } return $this; } protected function _prepareNoColumn(&$options) { if (!isset($options['attribs']['width'])) { $options['attribs']['width'] = 40; } if (!array_key_exists('title', $options)) { $options['title'] = '#'; } return $this; } protected function _getOptionsParams() { return array_keys($this->viewController->getPrimaryKey()); } protected function _prepareOptionsColumn(&$options) { if (!isset($options['actions'])) { $options['actions'] = array('edit' => array(), 'delete' => array()); } elseif (is_string($options['actions'])) { $actions = preg_split('/; ?/', $options['actions']); $options['actions'] = array_combine($actions, array_fill(0, count($actions), array())); } elseif(is_array($options['actions']) && !Qs_Array::isAssoc($options['actions'])) { $options['actions'] = array_combine($options['actions'], array_fill(0, count($options['actions']), array())); } foreach ($options['actions'] as $action => &$actionOptions) { if (!isset($actionOptions['link'])) { $actionOptions['link'] = $this->viewController->url(array('action' => $action)); } if (!isset($actionOptions['title'])) { $actionOptions['title'] = ucfirst(preg_replace('/([a-z])([A-Z])/', '$1 $2', $action)); } } if (!isset($options['params'])) { $options['params'] = $this->_getOptionsParams(); } $defaultAttribs = array( 'class' => 'grid_options', 'align' => 'center' ); if (!isset($options['attribs'])) { $options['attribs'] = array(); } $options['attribs'] = array_merge($defaultAttribs, $options['attribs']); return $this; } protected function _prepareColumn($type, $name, &$options) { switch ($type) { case 'link_enum': if (!isset($options['attribs']) || !isset($options['attribs']['align'])) { $options['attribs']['align'] = 'center'; } if (!isset($options['key'])) { $options['key'] = $this->_getOptionsParams(); } if (!isset($options['url'])) { $options['url'] = $this->viewController->url(array('action' => 'changeOption')) . '&'; } break; default: break; } return $this; } public function setSortLink($link) { $this->_sortLink = $link; } public function getSortLink() { if (null === $this->_sortLink) { $this->_sortLink = Qs_Constant::get('BASE_URL_LANGUAGE') . '/' . Qs_Constant::get('CURRENT_PAGE') . '?'; $queryParams = array(); $array = array_unique((array)$this->_urlVariables + array('page')); if (false !== ($index = array_search('orderBy', $array))) { unset($array[$index]); } foreach ($array as $name) { if (null !== ($value = Qs_Request::getGetValue($name))){ $queryParams[$name] = $value; } } if (!empty($queryParams)) { $this->_sortLink .= http_build_query($queryParams) . '&'; } } return $this->_sortLink; } public function setPaginatorUrl($url) { $this->_paginatorUrl = $url; return $this; } public function getPaginatorBaseUrl() { if (null === $this->_paginatorBaseUrl) { $this->_paginatorBaseUrl = Qs_Constant::get('BASE_URL_LANGUAGE') . '/' . Qs_Constant::get('CURRENT_PAGE_FINAL'); } return $this->_paginatorBaseUrl; } public function setPaginatorBaseUrl($url) { $this->_paginatorBaseUrl = $url; return $this; } public function getPaginatorUrl() { if (null === $this->_paginatorUrl) { $queryParams = array(); foreach ($this->_urlVariables as $name) { if ($name != 'page' && isset($_GET[$name])) { $queryParams[$name] = $_GET[$name]; } } $queryString = http_build_query($queryParams); if ($this->_seoPaginator) { $this->_paginatorUrl = $this->getPaginatorBaseUrl() . '/' . Qs_ViewController_List::$_paginatorPagePlaceholder; if (!empty($queryString)) { $this->_paginatorUrl .= '?' . $queryString; } } else { $this->_paginatorUrl = Qs_Constant::get('BASE_URL_LANGUAGE') . '/' . Qs_Constant::get('CURRENT_PAGE') . '?'; if (!empty($queryString)) { $this->_paginatorUrl .= $queryString . '&'; } $this->_paginatorUrl .= 'page=' . Qs_ViewController_List::$_paginatorPagePlaceholder; } } return $this->_paginatorUrl; } public function getPaginatorPagePlaceholder() { return Qs_ViewController_List::$_paginatorPagePlaceholder; } public function getColumn($name) { return Qs_Array::get($this->_columns, $name); } public function getColumns() { return $this->_columns; } public function setList(array $list) { $this->_list = $list; return $this; } public function getList() { if (null === $this->_list) { $options = array('order' => $this->getOrderBy()); if (false !== ($limitPage = $this->getLimitPage())) { $options['limitPage'] = $limitPage; } $this->_list = $this->viewController->dataObj->getList($options); } return $this->_list; } public function getItemsCount() { if ($this->isDisabledLimit()) { return $this->_viewController->dataObj->getListItemsCount(); } $paginator = $this->_getPaginator(); return $paginator->getTotalItemCount(); } public function toArray() { $item = $this->getOptions(); $item['sortLink'] = $this->getSortLink(); $item['columns'] = $this->getColumns(); $item['list'] = $this->getList(); if (!$this->isDisabledLimit()) { $this->paginator->setCurrentPageNumber($this->getPageNumber()) ->setItemCountPerPage($this->_getIpp()) ->setCurrentItems($item['list']); $item['paginator'] = get_object_vars($this->paginator->getPages()); $item['paginator']['url'] = $this->getPaginatorUrl(); $item['paginator']['pagePlaceholder'] = Qs_ViewController_List::$_paginatorPagePlaceholder; } return $item; } }