* @license PHP License * @package WB * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * WBEvent_Handler_Dereferencer * * Update references of some WXML content * * @version 0.5.0 * @package Wombat * @subpackage base */ class WBEvent_Handler_Dereferencer extends WBEvent_Handler { /** * handler config * * - source name of source (like "blog" or "wxmlarticle" resp. "wxmlarticletranslation") * * @var array */ protected $config = array( 'source' => '', ); /** * markup scanner * @var WBMarkup_Scanner */ private $scanner; /** * markup scanner handler dereferer * @var WBMarkup_Hander_Dereferer */ private $dref; /** * @var patI18n_Importer_Wombat */ private $nlsImp; /** * update column in table * * Update exactly one column in one table for one row using primary key * Hence primary key is required, as well as column and table name. * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function process(WBEvent $e) { $body = $e->get('body'); // table editor uses a save-array $save = $e->get('save'); if (is_array($save) && isset($save['body'])) { $body = $save['body']; } $url = $this->getUrlAndPath($e); if (empty($url[0])) { return true; } $this->scanner = WBClass::create( 'WBMarkup_Scanner' ); $this->dref = WBClass::create( 'WBMarkup_Handler_Dereferer' ); $this->scanner->setHandler($this->dref); $this->scanBody($url, $body); switch ($this->config['source']) { case 'nlstranslation': case 'wxmlarticle': case 'wxmlarticletranslation': break; case 'blog': case 'generic': default: $this->scanTranslations($url, $body); break; } return true; } /** * Scan Body * * @param array $url * @param string $body */ private function scanBody($url, $body) { $this->scanner->scan($body); $this->dref->setUrl($url[0], $url[1]); $this->dref->save(); } /** * Scan Translation of Body * * Fetch translation and scan it * * @param array $url * @param string $body */ private function scanTranslations($url, $body) { $this->nlsImp = WBClass::create('patI18n_Importer'); $mid = $this->nlsImp->getId($body); $trans = $this->nlsImp->getAllTranslations($mid); foreach ($trans as $lang => $b) { // $this->scanner->scan($b); $this->scanBody(array($url[0], $url[1]. '/' . $lang), $b); } } /** * get URL and path * * Tell URL and path for current source * * @param WBEvent $e * @return array */ private function getUrlAndPath(WBEvent $e) { if (empty($this->config['source'])) { WBClass::load('WBException_Config'); throw new WBException_Config('Source must not be empty!', 1, __CLASS__); } $url = ''; $path = ''; switch ($this->config['source']) { case 'blog': $url = $this->getUrl4Blog($e); break; case 'wxmlarticle': case 'wxmlarticletranslation': $url = $e->get('url'); $path = $e->get('cs') . '/' . $e->get('lang', 'C'); break; case 'generic': $url = sprintf($this->config['url'], $e->get('id')); $path = $this->config['path']; break; case 'nlstranslation': $url = $e->get('urlid'); $path = $e->get('path'); if ('translated' == $e->get('type')) { $path .= '/' . $e->get('lang'); } break; default: WBClass::load('WBException_Config'); throw new WBException_Config('Source name is invalid!', 2, __CLASS__); break; } return array($url, $path); } /** * get blog article's URL * * Use blog-config to build proper URL * * @return string */ private function getUrl4Blog(WBEvent $e) { WBClass::load('WBString'); $blog = WBClass::create('WBBlog_Tool'); /** @var $blog WBBlog_Tool */ $blog->loadArticleByData($e->get()); $url = $blog->getArticleUrl(); return WBString::replaceSuperPlaceholders($url); } }