<?php
class XenZine_ViewPublic_Article_View extends XenForo_ViewPublic_Base
{
public function renderHtml()
{
$formatter = Dark_ParseHTML_BbCode_Formatter_Ritsu::create('Dark_ParseHTML_BbCode_Formatter_Ritsu', array('view' => $this));
$bbCodeParser = new Dark_ParseHTML_BbCode_Parser($formatter);
$bbCodeOptions = array(
'states' => array(
'viewAttachments' => true
)
);
$article = $this->_params['article']; // referance unalterd copy
$this->_params['article']['message'] = $this->_params['article']['body'];
$this->_params['article']['body'] = Dark_ParseHTML_ViewPublic_Helper_Message::getBbCodeWrapper($this->_params['article'], $bbCodeParser, $bbCodeOptions);
$this->_params['article']['message'] = $this->_params['article']['resource_box'];
$this->_params['article']['resource_box'] = XenForo_ViewPublic_Helper_Message::getBbCodeWrapper($this->_params['article'], $bbCodeParser, $bbCodeOptions);
// we also want to render all of the different types of custom fields
$customFieldModel = $this->_getCustomFieldModel();
$customFieldsFromCache = $customFieldModel->getCustomFieldsFromCache();
$customValues = $this->_params['customFieldsBottom'];
$this->customFeildsBbCodeWrapper($this->_params['customFieldsBottom'], $customValues, $customFieldsFromCache, $article, $customFieldModel, $bbCodeParser, $bbCodeOptions);
$customValues = $this->_params['customFieldsSide'];
$this->customFeildsBbCodeWrapper($this->_params['customFieldsSide'], $customValues, $customFieldsFromCache, $article, $customFieldModel, $bbCodeParser, $bbCodeOptions);
$customValues = $this->_params['customFieldsTop'];
$this->customFeildsBbCodeWrapper($this->_params['customFieldsTop'], $customValues, $customFieldsFromCache, $article, $customFieldModel, $bbCodeParser, $bbCodeOptions);
$tabs = $this->_params['customTabs'];
if($tabs)
{
foreach($tabs as $tabKey=>$tab)
{
$customValues = $this->_params['customTabs'][$tabKey]['customFields'];
$this->customFeildsBbCodeWrapper($this->_params['customTabs'][$tabKey]['customFields'], $customValues, $customFieldsFromCache, $article, $customFieldModel, $bbCodeParser, $bbCodeOptions);
}
}
}
public function customFeildsBbCodeWrapper(&$paramKey, $customValues, $customFieldsFromCache, $article, $customFieldModel, $bbCodeParser, $bbCodeOptions)
{
if($customValues)
{
foreach($customValues as $customValueKey => $customValue)
{
if($customValue)
{
$field_id = $customValue['field_id'];
$field = $customFieldModel->getCustomFieldById($field_id, $customFieldsFromCache);
$fieldMap = $customFieldModel->getCustomFieldMapByType($field['field_type']);
if($fieldMap == "text" || $fieldMap == "single")
{
$field_value = $customValue['field_value'];
$nArticle = $article; // use a referance, so we can keep using it, it gets moddifed on passing through getBbCodeWrapper
$nArticle['message'] = $field_value;
$field_value = XenForo_ViewPublic_Helper_Message::getBbCodeWrapper($nArticle, $bbCodeParser, $bbCodeOptions);
$paramKey[$customValueKey]['field_value'] = $field_value;
$this->removeCustomAttachments($nArticle['message'], $this->_params['article']);
}
if($fieldMap == "multiple" )
{
foreach($customValue['field_value'] as $vKey => $v)
{
$nArticle = $article; // use a referance, so we can keep using it, it gets moddifed on passing through getBbCodeWrapper
$nArticle['message'] = $v;
$v = XenForo_ViewPublic_Helper_Message::getBbCodeWrapper($nArticle, $bbCodeParser, $bbCodeOptions);
$paramKey[$customValueKey]['field_value'][$vKey] = $v;
$this->removeCustomAttachments($nArticle['message'], $this->_params['article']);
}
}
if($fieldMap == "tablerows" )
{
foreach($customValue['field_value']['rows'] as $rowKey => $row)
{
foreach($row['value'] as $cellKey => $cell)
{
$nArticle = $article; // use a referance, so we can keep using it, it gets moddifed on passing through getBbCodeWrapper
$nArticle['message'] = $cell;
$cell = XenForo_ViewPublic_Helper_Message::getBbCodeWrapper($nArticle, $bbCodeParser, $bbCodeOptions);
$paramKey[$customValueKey]['field_value']['rows'][$rowKey]['value'][$cellKey] = $cell;
$this->removeCustomAttachments($nArticle['message'], $this->_params['article']);
}
}
}
}
}
}
}
public function removeCustomAttachments($text, &$message) // remove attachments from body message that are found in cusotm Attachments
{
if (stripos($text, '[/attach]') !== false)
{
if (preg_match_all('#\[attach(=[^\]]*)?\](?P<id>\d+)(\D.*)?\[/attach\]#iU', $text, $matches))
{
foreach ($matches['id'] AS $attachId)
{
unset($message['attachments'][$attachId]);
}
}
}
}
protected function _getCustomFieldModel()
{
return XenForo_Model::create('XenZine_Model_CustomField');
}
}