* @package WB * @subpackage ITS */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_ITS_Comment * * Add event comment for changes * * @version 1.2.0 * @package Wombat * @subpackage ITS */ class WBEvent_Handler_ITS_Comment extends WBEvent_Handler { /** * Mode: Template * * In template mode, load template directly and create HTML comment by adding global vars * and parsing the template. After that, the database holds the finalized comment including markup. * @see WBEvent_Handler_ITS_Comment::MODE_XML */ const MODE_TEMPLATE = "template"; /** * Mode: XML * * In XML mode, the comment will be created as XML tag: This tag contains all the * event's data as well as the name of the template to render actual output. HTML output will be created * by WBMarkup_Converter_Wb_ItsComment whenever needed. This approach allows to display translated comment * messages, or even mend the layout afterwards. * @see WBEvent_Handler_ITS_Comment::MODE_XML */ const MODE_XML = "xml"; /** * handler config * * Parameters: * - "reason" named reason to write comment for * - "user" user id or "auto" to load from event data 'uid' * - "namespace" comment namespace * - "approve" approve comment on the fly * - "mode" either template or xml * - "tmpl" named template to load * * @var array */ protected $config = array( 'tmpl' => 'created', 'user' => '1', 'defaultuser' => '1', 'namespace' => 'ticket', 'approve' => 1, 'mode' => self::MODE_XML ); /** * Comment * @var WBDatasource_Comment */ private $cmt; /** * Template engine * @var patTemplate */ private $tmpl; /** * Write comment * * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function process(WBEvent $e) { // load user id from event's data if ('auto' == $this->config['user']) { $this->config['user'] = $e->get('uid'); if (empty($this->config['user'])) { $this->config['user'] = $this->config['defaultuser']; } } $parameter = array( 'user' => $this->config['user'] ); $this->cmt = WBClass::create('WBDatasource_Comment', $parameter); $this->cmt->useEventTrigger(false); $this->cmt->setNamespace($this->config['namespace']); $this->cmt->setXId($e->get('id')); $approve = intval($this->config['approve']); if (1 > $approve) { $approve = false; } else { $approve = true; } switch ($this->config['mode']) { case self::MODE_XML: $comment = $this->mkXMLComment($e->get()); break; default: case self::MODE_TEMPLATE: $comment = $this->mkTemplateComment($e->get()); break; } $this->cmt->add($comment, $approve); return true; } /** * Create comment as XML tag * * Serialize date using php-serialize and encode use base64. This make it possible to * store anything in the database where comment's body is stored usually. * * @see serialize() * @see base64_encode() * @param array $data * @return string */ private function mkXMLComment($data) { return sprintf('' , $data['obscureid'] , $this->config['tmpl'] , base64_encode(serialize($data)) ); } /** * Create comment with template engine * * Load templates form Wxml/converter/wb/itscomment/, use tmpl config parameter. Add all scalar * event attributes and parse template. * * @param array $data * @return string */ private function mkTemplateComment($data) { $this->tmpl = WBClass::create('patTemplate'); $this->tmpl->readTemplatesFromInput('Wxml/converter/wb/itscomment/' . $this->config['tmpl'] . '.tmpl'); $tmplData = array(); foreach ($data as $key => $value) { if (is_scalar($value)) { $tmplData[$key] = $value; continue; } if ('diff' == $key) { foreach ($value as $k => $v) { if (is_array($v)) { $tmplData['diff_' . $k . '_count'] = count($value); if ($this->tmpl->exists('diff_' . $k . '_list_entry')) { $this->tmpl->addRows('diff_' . $k . '_list_entry', $v); } continue; } $tmplData['diff_' . $k] = $v; } continue; } $tmplData[$key . '_count'] = count($value); if ($this->tmpl->exists($key . '_list_entry')) { $this->tmpl->addVar($key . '_list_entry', 'value', $value); } } $this->tmpl->addGlobalVars($tmplData); return trim($this->tmpl->getParsedTemplate('snippet')); } }