* @license PHP License * @package WB * @subpackage content */ /** * Load classes */ // WBClass::load('WBContent'); /** * Wombat Incident Ticket System * * @version 1.1.1 * @package WB * @subpackage content */ class WBITS extends WBStdClass { /** * Table name */ const TABLE_TICKET = 'ticket'; const TABLE_TICKETSCOPE = 'ticketscope'; const TABLE_TICKETSCOPEUSER = 'ticketscopeuser'; const PAGER_TYPE_ALL = 'pager_all'; const PAGER_TYPE_USER = 'pager_user'; const PAGER_TYPE_COMPANY = 'pager_company'; /** * table * @var WBDatasource_Table_ITS */ protected $table; /** * @var WBFormProcessor_Filter */ private $filter; /** * Current configurateion */ static private $config = array(); /** * Current selected namespace, or empty for dynamic selection * @param string */ private $selectedNamespace = ''; /** * Filter Clause * @var array */ private $clause = array(); /** * Constructor * * @param array */ public function __construct($parameter = array()) { $parameter = array_merge(array( 'tmpl' => null, 'tmplDir' => null, 'req' => null, 'formConfigDir' => null ), $parameter); $this->filter = WBClass::create('WBFormProcessor_Filter'); $this->filter->setRequest($parameter['req']); $this->filter->setTemplate($parameter['tmpl'], $parameter['tmplDir']); $this->filter->setFormConfigDir($parameter['formConfigDir']); $this->table = WBClass::create('WBDatasource_Table_ITS'); $this->table->switchTranslation(false); } /** * Get ITS config * * Load from its/config.xml. Merge config with default values * Configuration parameter: * - obscurelength: length of obscure code for new tickets, recommended: min 2, max 5 * - namespace: default namespace * - namespaces: configuration of all available namespaces * * @todo strip max length * @return array */ static public function getConfig() { // load only once if (!empty(self::$config)) { return self::$config; } // load general config self::$config = array( 'obscurelength' => 3, 'namespace' => 'default', 'namespaces' => array(), 'uri' => '{OBSCUREID}' ); $config = WBClass::create('WBConfig'); if ($config->load('its/config', true)) { self::$config = array_merge(self::$config, $config->get('ticket', array())); } return self::$config; } /** * Use Namespace * * @param string */ public function useNamespace($ns = '') { $this->selectedNamespace = self::selectNamespace($ns); } /** * Select Namespace * * @param string */ public static function selectNamespace($ns) { if (empty($ns)) { return ''; } if (24 < strlen($ns)) { $ns = substr($ns, 0, 24); } self::getConfig(); if (isset(self::$config['namespaces'][$ns])) { return $ns; } return self::$config['namespace']; } /** * Set Filter Clause * * @param array $clause * @param bool $append */ public function setClause($clause, $append = false) { if ($append) { $this->clause[] = $clause; return; } $this->clause = $clause; } /** * Initialize filter options * * Render and process filter form. * * @param array $searchfields * @param WBRequest $req * @return array template variables */ public function initFilter($searchfields = array(), $req = null) { $searchfields = array_merge(array('title', 'blurb', $this->table->getIdentifier(WBITS::TABLE_TICKET)), $searchfields); if (empty($req)) { return $this->filter->buildClause($searchfields); } $query = $req->get('searchquery', ''); if (empty($query)) { return $this->filter->buildClause($searchfields); } $query = array_map('trim', explode(' ', $query)); // start ticket $parameter = array( 'table' => $this->table ); /** @var WBITS_Ticket */ $ticket = WBClass::create('WBITS_Ticket', $parameter); // find obscure id foreach ($query as &$q) { if (empty($q)) { continue; } $match = $ticket->extractObscureId($q); if (empty($match)) { continue; } $q = $match[2]; } $req->set('searchquery', implode(' ', $query)); return $this->filter->buildClause($searchfields); } /** * Get ticket pager * * @param array options * @return WBDatasource_Pager */ public function getPager($type = self::PAGER_TYPE_ALL, $options = array()) { $clause = array_merge($this->clause, $this->filter->getClause()); $options = array_merge($this->filter->getOptions(), $options); // force namespace? if (!empty($this->selectedNamespace)) { $clause[] = array( 'field' => 'namespace', 'value' => $this->selectedNamespace ); } switch ($type) { case self::PAGER_TYPE_ALL: break; default: case self::PAGER_TYPE_USER: $clause[] = array( 'field' => 'customeruid', 'value' => WBUser_Auth::getCurrent()->getId() ); break; } $id = __CLASS__ . ':' . self::TABLE_TICKET; if (!is_array($options)) { $options = array(); } $options['callback'] = WBClass::create('WBITS_DatasourceCallback', $this->getConfig()); if (!isset($options['limit'])) { $options['limit'] = 10; } return $this->table->getPager($id, self::TABLE_TICKET, null, $clause, $options); } /** * Load ticket by obscureid id * * @param string $obscureid * @return WBITS_Ticket */ public function loadByObscureId($obscureid = null) { $parameter = array( 'table' => $this->table ); /** @var WBITS_Ticket */ $ticket = WBClass::create('WBITS_Ticket', $parameter); $ticket->setClause($this->clause); if (empty($obscureid)) { return $ticket; } return $ticket->loadByObscureId($obscureid, $this->selectedNamespace); } }