* @license PHP License * @package WB * @subpackage content */ /** * Load classes */ WBClass::load('WBContent' , 'WBITS'); /** * Content component: ITS_Ticket * * @version 1.0.0 * @package WB * @subpackage content */ class WBContent_ITS_Ticket extends WBContent { /** * Parameter list * @var array */ protected $config = array( 'captcha' => 'auto', 'action' => 'add', 'requiredgroup' => '', 'obscureid' => '', 'titlebubbleformat' => 'Support Ticket: %s', 'titlebubblenotfound' => 'Support Ticket' ); /** * Backend * @var WBITS */ private $its; /** * Current ticket * @var WBITS_Ticket */ private $ticket; /** * 2nd constructor * * Called after configuration was done */ protected function init() { $parameter = array( 'tmpl' => $this->tmpl, 'tmplDir' => $this->config['tmplDir'], 'req' => $this->req, 'formConfigDir' => $this->getFormConfigDir() ); $this->its = WBClass::create('WBITS', $parameter); } /** * run * * run component * * @return array parameter list */ public function run() { unset($this->config['__path']); $this->tmpl->addGlobalVars($this->config, 'CONFIG_'); if (!$this->isUserInGroup($this->config['requiredgroup'])) { $this->loadTemplates('anon'); return $this->config; } switch ($this->config['action']) { case 'list': $this->listTicket(); break; case 'add': $this->processForm('edit'); break; case 'upload': $this->processForm('upload'); break; default: $this->displayTickets(); break; } return $this->config; } /** * List my tickets * * List tickets where this use account set as customer */ private function listTicket() { $pager = $this->its->getPager(WBITS::PAGER_TYPE_USER); $this->tmpl->addGlobalVars($pager->browse($this->config['goto']), 'pager_'); $this->loadTemplates('list'); $this->tmpl->addRows('list_entry', $pager->get()); } /** * Add Uploaded File to Ticket * * @param patForms $form * @param array $values */ protected function onUploadValid($form, $values) { $this->ticket = WBClass::create('WBITS_Ticket'); $this->ticket->loadByObscureId($this->config['obscureid']); $data = $this->ticket->getData(); $data['ticketmedia'][] = $values['file']; $save = array( 'ticketmedia' => $data['ticketmedia'] ); // save comment with file if (isset($values['filenote'])) { $save['comment'] = $values['filenote']; } $this->ticket->save($save); return true; } /** * Save ticket data * * Add new ticket * * @param patForms $form * @param array $values * @return bool always true */ protected function onEditValid($form, $values) { if ($this->user->isAuthenticated()) { $values['customeruid'] = $this->user->getId(); } // remove captcha, if set if (isset($values['captcha'])) { unset($values['captcha']); } // generate title from blurb if (!isset($values['title'])) { $blurb = $values['blurb']; $blurb = str_replace('

', "

\n", $blurb); $blurb = strip_tags($blurb); $blurb = str_replace(' ', ' ', $blurb); $blurb = trim($blurb); $blurb = array_map('trim', explode("\n", $blurb)); foreach ($blurb as $title) { $title = trim($title); if (5 > strlen($title)) { continue; } $values['title'] = $title; break; } } $this->ticket = WBClass::create('WBITS_Ticket'); $this->ticket->save($values); return true; } /** * List tickets * * Get pager and fill template with list */ private function displayTickets() { $this->loadTemplates('display'); $this->ticket = $this->its->loadByObscureId($this->config['obscureid']); $oid = $this->ticket->getObscureId(); if (empty($oid)) { $this->bubbles['title'] = $this->config['titlebubblenotfound']; return; } $data = $this->ticket->getData(); $this->bubbles['title'] = sprintf($this->config['titlebubbleformat'], $data['title']); $tmplData = array(); foreach ($data as $key => $value) { if (is_scalar($value)) { $tmplData[$key] = $value; continue; } foreach($value as $k => $v) { if (is_scalar($v)) { $tmplData[$key . '_' . $k] = $v; } } } $this->tmpl->addGlobalVars($tmplData); $this->tmpl->addGlobalVar('obscureid', $oid); $this->tmpl->addGlobalVar('id', $this->ticket->getId()); } /** * Fetch list of form elements * * Use different file regardless of actual form name. * * @param string name of form * @return array $elements */ protected function getFormElementList($name) { $list = parent::getFormElementList($name); if (!in_array($name, array('edit', 'upload'))) { return $list; } // add captcha element in case user is not logged in right now if ($this->config['captcha'] == 'auto' && !$this->user->isAuthenticated()) { $list['captcha'] = array( 'type' => 'String', 'attributes' => array( 'title' => patI18n::dgettext('wombat', 'CAPTCHA'), 'label' => patI18n::dgettext('wombat', 'CAPTCHA'), 'description' => patI18n::dgettext('wombat', 'Please type in what the picture shows.'), 'class' => 'field captcha', 'minlength' => 3, 'maxlength' => 255, ), 'rule' => array('Captcha') ); } if ('edit' != $name) { return $list; } // copy default values from user data if ($this->user->isAuthenticated()) { $userData = $this->user->getData(); // name if (isset($list['customername'])) { $list['customername']['attributes']['default'] = $userData['forename'] . ' ' . $userData['surname']; } // e-mail if (isset($list['customeremailurlid'])) { $dict = WBClass::create('WBDictionary_URL'); $dict->addWord($userData['email']); $list['customeremailurlid']['attributes']['default'] = $dict->getId(); } } return $list; } /** * receive output * * fetch output of this content component * * @return string */ public function getString() { return $this->tmpl->getParsedTemplate('snippet'); } /** * location of form config * * Return sub directory where form element definitions are located * * @return string folder */ protected function getFormConfigDir() { return strtolower($this->config['tmplDir']); } }