* @license PHP License * @package WB * @subpackage content */ /** * Load classes */ WBClass::load('WBITS' , 'WBDatasource_Pager' ); /** * Wombat Incident Ticket System Ticket List Exporter * * @version 1.1.0 * @package WB * @subpackage content */ class WBITS_TicketList_Exporter extends WBStdClass { /** * Pager * @var WBDatasource_Pager */ private $pager; /** * Current Config * @var array */ protected $config; /** * Column Config * * Prepared configuration for each column * @var array */ private $cols; /** * Column Filter * * Prepared column filters * @var array */ private $colFilter = array(); /** * Current Namespace * * @var string */ private $namespace; /** * User Storage Module * @var WBUser_Storage */ protected $userStorage; /** * Constructor * */ final public function __construct() { $this->config = WBITS::getConfig(); $this->useNamespace($this->namespace); $this->init(); } /** * 2nd Constructor */ protected function init() { } /** * Set Pager * * Pager provides list of tickets * @param WBDatasource_Pager */ final public function setPager(WBDatasource_Pager $pager) { $this->pager = $pager; } final public function useNamespace($ns = '') { $ns = WBITS::selectNamespace($ns); if (empty($ns)) { return; } $this->namespace = $ns; } /** * Get Column Config * * @return array */ protected function getColumnConfig() { if (!empty($this->cols)) { return $this->cols; } $cols = $this->config['column']; if (empty($this->config['namespaces'][$this->namespace]['column'])) { $this->config['namespaces'][$this->namespace]['column'] = array(); } foreach ($this->config['namespaces'][$this->namespace]['column'] as $k => $v) { if (empty($v['export'])) { if (isset($cols[$k])) { unset($cols[$k]); } continue; } if (!isset($cols[$k])) { $cols[$k] = $v; } else { $cols[$k] = array_merge($cols[$k], $v); } } // remove unwanted columns foreach ($cols as $k => $v) { if (empty($v['export'])) { unset($cols[$k]); } } $this->cols = $cols; return $this->cols; } /** * Export Ticket List * */ final public function export() { $this->initColumnFilter(); $this->onExportStart(); $info = $this->pager->browse('__first'); for ($i = 1; $i < $info['pages']; ++$i) { $list = $this->pager->get(); foreach ($list as $l) { $this->exportListItem($l); } $this->pager->browse('__next'); } $list = $this->pager->get(); foreach ($list as $l) { $this->exportListItem($l); } $this->pager->browse('__first'); $this->onExportEnd(); } private function initColumnFilter() { $cols = $this->getColumnConfig(); foreach ($cols as $k => $c) { if (empty($c['filter'])) { continue; } if (empty($c['filter']['name'])) { continue; } /** @var WBITS_TicketList_Filter */ $this->colFilter[$k] = WBClass::create('WBITS_TicketList_Filter_' . $c['filter']['name']); $this->colFilter[$k]->configure($c['filter']); } } /** * Filter Column's Value * * @param string column's name * @param string column's value * @return string */ protected function filterColumn($col, $value) { if (empty($this->colFilter[$col])) { return $value; } return $this->colFilter[$col]->filter($value); } /** * Convert User Id 2 Name * * Load user data and format using forename, surname and user id * * @param string user id * @return string */ protected function uid2Name($uid) { if (empty($this->userStorage)) { /** @var WBUser */ $user = WBClass::create('WBUser'); $this->userStorage = $user->getStorageModule(); } if (!$this->userStorage->load($uid)) { return ''; } $data = $this->userStorage->get(); if (empty($data)) { return ''; } return sprintf('%s, %s (%d)', $data['surname'], $data['forename'], $data['id']); } /** * Event Handler to Start Export * */ protected function onExportStart() { } /** * Event Handler for Each List Item * * @param array list item data */ protected function exportListItem($item) { } /** * Event Handler at End of Export * */ protected function onExportEnd() { } }