PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/site/components/com_stories/templates/helpers/parser.php

https://github.com/bhar1red/anahita
PHP | 186 lines | 91 code | 27 blank | 68 comment | 2 complexity | cee99b5108b53122400578def7c51cb8 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * LICENSE: ##LICENSE##
  4. *
  5. * @category Anahita
  6. * @package Com_Stories
  7. * @subpackage Template_Helper
  8. * @author Arash Sanieyan <ash@anahitapolis.com>
  9. * @author Rastin Mehr <rastin@anahitapolis.com>
  10. * @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
  11. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  12. * @version SVN: $Id$
  13. * @link http://www.anahitapolis.com
  14. */
  15. /**
  16. * Story Parser Template Helper
  17. *
  18. * @category Anahita
  19. * @package Com_Stories
  20. * @subpackage Template_Helper
  21. * @author Arash Sanieyan <ash@anahitapolis.com>
  22. * @author Rastin Mehr <rastin@anahitapolis.com>
  23. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  24. * @link http://www.anahitapolis.com
  25. */
  26. class ComStoriesTemplateHelperParser extends KTemplateHelperAbstract
  27. {
  28. /**
  29. * Parse Template
  30. *
  31. * @var ComBaseTemplateDefault
  32. */
  33. protected $_template;
  34. /**
  35. * Constructor.
  36. *
  37. * @param object An optional KConfig object with configuration options
  38. */
  39. public function __construct(KConfig $config)
  40. {
  41. parent::__construct($config);
  42. $identfier = clone $this->getIdentifier();
  43. $identfier->path = array('template');
  44. $identfier->name = 'parser';
  45. register_default(array('identifier'=>$identfier, 'default'=>'ComBaseTemplateDefault'));
  46. $this->_template = $this->getService($identfier);
  47. foreach($config->filters as $filter) {
  48. $this->_template->addFilter($filter);
  49. }
  50. $this->_template->getFilter('alias')->append( KConfig::unbox($config->alias) );
  51. JFactory::getLanguage()->load('com_stories');
  52. $this->_template->addSearchPath(KConfig::unbox($config->paths), true);
  53. }
  54. /**
  55. * Initializes the options for the object
  56. *
  57. * Called from {@link __construct()} as a first step of object instantiation.
  58. *
  59. * @param object An optional KConfig object with configuration options.
  60. * @return void
  61. */
  62. protected function _initialize(KConfig $config)
  63. {
  64. $config->append(array(
  65. 'paths' => array(dirname(__FILE__).'/../stories'),
  66. 'filters' => array('alias','shorttag'),
  67. 'alias' => array(
  68. '@escape(' => 'htmlspecialchars(',
  69. '@route(' => 'JRoute::_(',
  70. '@name(' => '$this->renderHelper(\'com://site/stories.template.helper.story.actorName\',',
  71. '@possessive(' => '$this->renderHelper(\'com://site/stories.template.helper.story.possessiveNoune\',$story,',
  72. '@link(' => '$this->renderHelper(\'com://site/stories.template.helper.story.link\','
  73. )
  74. ));
  75. parent::_initialize($config);
  76. }
  77. /**
  78. * Render a story. If a $actor is passed then we are rendering the stories related to an actor. (A profile stories
  79. * as opposed to
  80. *
  81. * @param ComStoriesDomainEntityStory $story Story
  82. * @param ComActorsDomainEntityActor $actor Actor
  83. *
  84. * @return array
  85. */
  86. public function parse($story, $actor = null)
  87. {
  88. $options = array();
  89. JFactory::getLanguage()->load($story->component);
  90. static $commands;
  91. $commands = $commands ? clone $commands : new LibBaseTemplateObjectContainer();
  92. $commands->reset();
  93. $data = array(
  94. 'commands' => $commands,
  95. 'actor' => $actor,
  96. 'helper' => $this,
  97. 'story' => $story,
  98. 'subject' => $story->subject,
  99. 'target' => $story->target,
  100. 'object' => $story->object,
  101. 'comment' => $story->comment,
  102. 'type' => $story->getIdentifier()->name
  103. );
  104. $path = JPATH_ROOT.'/components/'.$story->component.'/templates/stories/'.$story->name.'.php';
  105. $output = $this->_render($story, $path, $data);
  106. $data = $this->_parseData($output);
  107. $data['commands'] = $commands;
  108. return $data;
  109. }
  110. /**
  111. * Renders a story
  112. *
  113. * @param array $paths
  114. * @param array $data
  115. */
  116. protected function _render($story, $paths, $data)
  117. {
  118. settype($paths, 'array');
  119. foreach($paths as $path)
  120. {
  121. if ( $this->_template->findFile($path) ) {
  122. return $this->_template->loadFile($path, $data)->render();
  123. }
  124. }
  125. try {
  126. return $this->_template->loadTemplate($story->name, $data)->render();
  127. }catch(Exception $e) {
  128. print '<small>file missing :'.$path.'</small>';
  129. }
  130. }
  131. /**
  132. * Parse the title,body from data
  133. *
  134. * @param string $data
  135. * @return array
  136. */
  137. protected function _parseData($data)
  138. {
  139. $output = array('title'=>'','body'=>'');
  140. $matches = array();
  141. if ( preg_match_all('#<data name="([^"]+)">(.*?)<\/data>#si', $data, $matches) )
  142. {
  143. $attributes = $matches[1];
  144. $contents = $matches[2];
  145. foreach($attributes as $i=>$attribute)
  146. $output[$attribute] = $contents[$i];
  147. }
  148. return $output;
  149. }
  150. /**
  151. * Return the parse template
  152. *
  153. * @return ComBaseTemplateDefault
  154. */
  155. public function getTemplate()
  156. {
  157. return $this->_template;
  158. }
  159. }