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

/campsite/src/classes/TemplateConverterHelper.php

https://github.com/joechrysler/Campsite
PHP | 665 lines | 451 code | 67 blank | 147 comment | 81 complexity | 0a2421ebe8c8af051e1f6877aa642413 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. /**
  3. * @package Campsite
  4. *
  5. * @author Holman Romero <holman.romero@gmail.com>
  6. * @copyright 2008 MDLF, Inc.
  7. * @license http://www.gnu.org/licenses/gpl.txt
  8. * @version $Revision$
  9. * @link http://www.sourcefabric.org
  10. */
  11. $_docRoot = dirname(dirname(__FILE__));
  12. require_once($_docRoot.'/classes/TemplateConverterListObject.php');
  13. require_once($_docRoot.'/classes/TemplateConverterIfBlock.php');
  14. define('CS_OBJECT', '$campsite');
  15. /**
  16. * Class TemplateConverterHelper
  17. */
  18. class TemplateConverterHelper
  19. {
  20. /**
  21. * @var array
  22. */
  23. private static $m_exceptions = array(
  24. 'article' => array(
  25. 'date' => array(
  26. 'attribute' => 'date'),
  27. 'type' => array(
  28. 'attribute' => 'type_name'),
  29. 'mon_nr' => array(
  30. 'attribute' => 'mon'),
  31. 'wday_nr' => array(
  32. 'attribute' => 'wday'),
  33. 'upload_date' => array(
  34. 'attribute' => 'creation_date'),
  35. 'uploaddate' => array(
  36. 'attribute' => 'creation_date'),
  37. 'publishdate' => array(
  38. 'attribute' => 'publish_date')
  39. ),
  40. 'articlecomment' => array(
  41. 'readeremail' => array(
  42. 'new_object' => 'comment',
  43. 'attribute' => 'reader_email'),
  44. 'readeremailobfuscated' => array(
  45. 'new_object' => 'comment',
  46. 'attribute' => 'reader_email|obfuscate_email'),
  47. 'submitdate' => array(
  48. 'new_object' => 'comment',
  49. 'attribute' => 'submit_date'),
  50. 'readeremailpreview' => array(
  51. 'new_object' => 'preview_comment_action',
  52. 'attribute' => 'reader_email'),
  53. 'readeremailpreviewobfuscated' => array(
  54. 'new_object' => 'preview_comment_action',
  55. 'attribute' => 'reader_email|obfuscate_email'),
  56. 'subjectpreview' => array(
  57. 'new_object' => 'preview_comment_action',
  58. 'attribute' => 'subject'),
  59. 'contentpreview' => array(
  60. 'new_object' => 'preview_comment_action',
  61. 'attribute' => 'content'),
  62. 'count' => array(
  63. 'new_object' => 'article',
  64. 'attribute' => 'comment_count'),
  65. 'submiterror' => array(
  66. 'new_object' => 'submit_comment_action',
  67. 'attribute' => 'error_message'),
  68. 'submiterrorno' => array(
  69. 'new_object' => 'submit_comment_action',
  70. 'attribute' => 'error_code')
  71. ),
  72. 'articleattachment' => array(
  73. 'extension' => array(
  74. 'new_object' => 'attachment',
  75. 'attribute' => 'extension'),
  76. 'description' => array(
  77. 'new_object' => 'attachment',
  78. 'attribute' => 'description'),
  79. 'filename' => array(
  80. 'new_object' => 'attachment',
  81. 'attribute' => 'file_name'),
  82. 'mimetype' => array(
  83. 'new_object' => 'attachment',
  84. 'attribute' => 'mime_type'),
  85. 'sizeb' => array(
  86. 'new_object' => 'attachment',
  87. 'attribute' => 'size_b'),
  88. 'sizekb' => array(
  89. 'new_object' => 'attachment',
  90. 'attribute' => 'size_kb'),
  91. 'sizemb' => array(
  92. 'new_object' => 'attachment',
  93. 'attribute' => 'size_mb')
  94. ),
  95. 'audioattachment' => array(
  96. 'tracknum' => array(
  97. 'new_object' => 'audioclip',
  98. 'attribute' => 'track_no'),
  99. 'disknum' => array(
  100. 'new_object' => 'audioclip',
  101. 'attribute' => 'disk_no')
  102. ),
  103. 'image' => array(
  104. 'number' => array(
  105. 'attribute' => 'article_index'),
  106. 'mon_nr' => array(
  107. 'attribute' => 'mon'),
  108. 'wday_nr' => array(
  109. 'attribute' => 'wday')
  110. ),
  111. 'issue' => array(
  112. 'date' => array(
  113. 'attribute' => 'date'),
  114. 'mon_nr' => array(
  115. 'attribute' => 'mon'),
  116. 'wday_nr' => array(
  117. 'attribute' => 'wday'),
  118. 'iscurrent' => array(
  119. 'attribute' => 'is_current')
  120. ),
  121. 'language' => array(
  122. 'englname' => array (
  123. 'attribute' => 'english_name')
  124. ),
  125. 'login' => array(
  126. 'error' => array(
  127. 'new_object' => 'login_action',
  128. 'attribute' => 'error_message')
  129. ),
  130. 'search' => array(
  131. 'error' => array(
  132. 'new_object' => 'search_articles_action',
  133. 'attribute' => 'error_message'),
  134. 'keywords' => array(
  135. 'new_object' => 'search_articles_action',
  136. 'attribute' => 'search_keywords')
  137. ),
  138. 'subscription' => array(
  139. 'expdate' => array(
  140. 'new_object' => 'user->subscription',
  141. 'attribute' => 'expiration_date'),
  142. 'unit' => array(
  143. 'new_object' => 'publication',
  144. 'attribute' => 'subscription_time_unit'),
  145. 'unitcost' => array(
  146. 'new_object' => 'publication',
  147. 'attribute' => 'subscription_unit_cost'),
  148. 'currency' => array(
  149. 'new_object' => 'publication',
  150. 'attribute' => 'subscription_currency'),
  151. 'trialtime' => array(
  152. 'new_object' => 'publication',
  153. 'attribute' => 'subscription_trial_time'),
  154. 'paidtime' => array(
  155. 'new_object' => 'publication',
  156. 'attribute' => 'subscription_paid_time'),
  157. 'error' => array(
  158. 'new_object' => 'edit_subscription_action',
  159. 'attribute' => 'error_message')
  160. ),
  161. 'user' => array(
  162. 'straddress' => array (
  163. 'attribute' => 'str_address'),
  164. 'phone2' => array(
  165. 'attribute' => 'second_phone'),
  166. 'postalcode' => array(
  167. 'attribute' => 'postal_code'),
  168. 'addok' => array(
  169. 'attribute' => 'add_ok'),
  170. 'modifyok' => array(
  171. 'attribute' => 'modify_ok'),
  172. 'adderror' => array(
  173. 'new_object' => 'edit_user_action',
  174. 'attribute' => 'error_message'),
  175. 'modifyerror' => array(
  176. 'new_object' => 'edit_user_action',
  177. 'attribute' => 'error_message')
  178. )
  179. );
  180. /**
  181. * @var array
  182. */
  183. private static $m_envObjects = array(
  184. 'language','publication','issue','section','article',
  185. 'topic','articlecomment'
  186. );
  187. /**
  188. * @var array
  189. */
  190. private static $m_printEx = array(
  191. 'articleattachment' => 'attachment',
  192. 'articlecomment' => 'comment',
  193. 'audioattachment' => 'audioclip'
  194. );
  195. /**
  196. * @var array
  197. */
  198. private static $m_urXFuncs = array(
  199. 'uri','uripath','url','urlparameters','formparameters');
  200. /**
  201. * @var array
  202. */
  203. private static $m_simpleForms = array('login','search','user');
  204. /**
  205. * @var array
  206. */
  207. private static $m_endForms = array(
  208. 'endarticlecomment','endlogin','endsearch', 'endsubscription','enduser'
  209. );
  210. /**
  211. * @var array
  212. */
  213. private static $m_operators = array(
  214. 'is' => '==',
  215. 'not' => '!=',
  216. 'greater' => '>',
  217. 'greater_equal' => '>=',
  218. 'smaller' => '<',
  219. 'smaller_equal' => '<='
  220. );
  221. /**
  222. * @var string
  223. */
  224. private static $m_withArticleType = '';
  225. /**
  226. * @var string
  227. */
  228. private static $m_withBodyField = '';
  229. /**
  230. *
  231. */
  232. public static function GetWithArticletype()
  233. {
  234. return self::$m_withArticleType;
  235. } // fn GetWithArticleType
  236. /**
  237. *
  238. */
  239. public static function GetWithBodyField()
  240. {
  241. return self::$m_withBodyField;
  242. } // fn GetWithBodyField
  243. /**
  244. * @param array $p_optArray
  245. */
  246. public static function GetNewTagContent($p_optArray, $p_tplPath = null)
  247. {
  248. if (!is_array($p_optArray) || sizeof($p_optArray) < 1) {
  249. continue;
  250. }
  251. $newTag = '';
  252. $p_optArray[0] = strtolower($p_optArray[0]);
  253. // <!** Print statement ... > to {{ $campsite->statement ... }}
  254. if ($p_optArray[0] == 'print') {
  255. return self::BuildPrintStatement($p_optArray);
  256. }
  257. // Environmental Objects
  258. // <!** Publication name ... > to {{ set_publication name= ... }}
  259. // <!** Issue off ... > to {{ unset_issue }}
  260. if (in_array($p_optArray[0], self::$m_envObjects)) {
  261. return self::BuildEnvironmentalStatement($p_optArray);
  262. }
  263. // URI and URL Functions
  264. // <!** URI> to {{ uri }}
  265. // <!** URLParameters section> to {{ urlparameters options="section" }}
  266. if (in_array($p_optArray[0], self::$m_urXFuncs)) {
  267. return self::BuildUrxStatement($p_optArray);
  268. }
  269. // User, Search and Login Forms
  270. // <!** Search do-search.tpl Go>
  271. // to
  272. // {{ search_form template="do-search.tpl" submit_button="Go" }}
  273. if (in_array($p_optArray[0], self::$m_simpleForms)) {
  274. return self::BuildSimpleFormStatement($p_optArray);
  275. }
  276. // Article Comment Form
  277. // <!** ArticleCommentForm ArtCommForm.tpl Send Preview>
  278. // to
  279. // {{ article_comment_form template="ArtCommForm.tpl"
  280. // submit_button="Send" preview_button="Preview" }}
  281. if ($p_optArray[0] == 'articlecommentform') {
  282. return self::BuildArticleCommentFormStatement($p_optArray);
  283. }
  284. // Subscription Comment Form
  285. // <!** Subscription by_section do-subsc.tpl Subscribe>
  286. // to
  287. // {{ subscription_form type="by_section" template="do-subsc.tpl"
  288. // submit_button="Subscribe" }}
  289. if ($p_optArray[0] == 'subscription') {
  290. return self::BuildSubscriptionFormStatement($p_optArray);
  291. }
  292. // End forms
  293. // <!** EndSearch> to {{ /search_form }}
  294. if (in_array($p_optArray[0], self::$m_endForms)) {
  295. return self::BuildEndFormStatement($p_optArray);
  296. }
  297. // Edit form fields
  298. // <!** Edit Login uname>
  299. // <!** Edit Search keywords>
  300. if ($p_optArray[0] == 'edit') {
  301. return self::BuildEditStatement($p_optArray);
  302. }
  303. // Select form fields
  304. // <!** Select Login RememberLogin>
  305. // to
  306. // {{ camp_select object="login" attribute="rememberlogin" }}
  307. //
  308. // <!** Select User gender male female>
  309. // to
  310. // {{ camp_select object="user" attribute="gender" male_name="M" female_name="M" }}
  311. if ($p_optArray[0] == 'select') {
  312. return self::BuildSelectStatement($p_optArray);
  313. }
  314. // HTML Encoding
  315. // <!** HTMLEncoding> to {{ enable_html_encoding }}
  316. // <!** HTMLEncoding off> to {{ disable_html_encoding }}
  317. if ($p_optArray[0] == 'htmlencoding') {
  318. return self::BuildHTMLEncodingStatement($p_optArray);
  319. }
  320. if ($p_optArray[0] == 'with' || $p_optArray[0] == 'endwith') {
  321. return self::BuildWithStatement($p_optArray);
  322. }
  323. switch ($p_optArray[0]) {
  324. // <!** Date ... > to {{ $smarty.now|camp_date_format:" ... " }}
  325. case 'date':
  326. $newTag = '$smarty.now|camp_date_format:"' . $p_optArray[1] . '"';
  327. break;
  328. // <!** include header.tpl> to {{ include file="header.tpl" }}
  329. // <!** include article.tpl> to {{ include file="news/article.tpl" }}
  330. case 'include':
  331. $filePath = (!is_null($p_tplPath)) ? $p_tplPath.'/'.$p_optArray[1]
  332. : $p_optArray[1];
  333. $newTag = 'include file="' . $filePath . '"';
  334. break;
  335. // <!** local> to {{ local }}
  336. case 'local':
  337. $newTag = 'local';
  338. break;
  339. // <!** endLocal> to {{ /local }}
  340. case 'endlocal':
  341. $newTag = '/local';
  342. break;
  343. // <!** else> to {{ /else }}
  344. case 'else':
  345. $newTag = 'else';
  346. break;
  347. }
  348. return $newTag;
  349. } // fn GetNewTagContent
  350. /**
  351. * @param array $p_optArray
  352. *
  353. * @return string $newTag
  354. */
  355. public static function BuildPrintStatement($p_optArray)
  356. {
  357. $newTag = CS_OBJECT;
  358. $object = strtolower($p_optArray[1]);
  359. if (array_key_exists($object, self::$m_exceptions)
  360. && array_key_exists(strtolower($p_optArray[2]), self::$m_exceptions[$object])) {
  361. $e = self::$m_exceptions[$object][strtolower($p_optArray[2])];
  362. $newTag .= (isset($e['new_object'])) ? '->'.$e['new_object'] : '->'.strtolower($p_optArray[1]);
  363. $newTag .= (isset($e['attribute'])) ? '->'.$e['attribute'] : '';
  364. if ($e['attribute'] == 'date' || $e['attribute'] == 'creation_date'
  365. || $e['attribute'] == 'publish_date') {
  366. $newTag.= (isset($p_optArray[3])) ? '|camp_date_format:"'.$p_optArray[3].'"' : '';
  367. }
  368. } elseif ($object == 'captcha') {
  369. $newTag = 'captcha_image_link';
  370. } else {
  371. for($i = 1; $i < sizeof($p_optArray); $i++) {
  372. if (array_key_exists(strtolower($p_optArray[$i]), self::$m_printEx)) {
  373. $p_optArray[$i] = self::$m_printEx[strtolower($p_optArray[$i])];
  374. }
  375. if ($object == 'article' && $i > 2 && $i == (sizeof($p_optArray) - 1)
  376. && strstr($p_optArray[$i], '%') !== false) {
  377. // date/time format string
  378. $newTag .= '|camp_date_format:"'.$p_optArray[$i].'"';
  379. } elseif ($p_optArray[$i] == 'image' && is_numeric($p_optArray[$i+1])) {
  380. $newTag .= '->article->' . strtolower($p_optArray[$i]) . $p_optArray[$i+1];
  381. $i += 1;
  382. } elseif ($object == 'article'
  383. && strtolower($p_optArray[$i]) == 'firstparagraph') {
  384. $newTag .= '->first_paragraph';
  385. } else {
  386. $newTag .= '->' . strtolower($p_optArray[$i]);
  387. }
  388. }
  389. }
  390. return $newTag;
  391. } // fn BuildPrintStatement
  392. /**
  393. * @param array $p_optArray
  394. *
  395. * @return string $newTag
  396. */
  397. public static function BuildEnvironmentalStatement($p_optArray)
  398. {
  399. if (strtolower($p_optArray[1]) == 'off') {
  400. if ($p_optArray[0] == 'articlecomment') {
  401. $p_optArray[0] = 'comment';
  402. }
  403. $newTag = 'unset_' . $p_optArray[0];
  404. } else {
  405. if (strtolower($p_optArray[1]) == 'current') {
  406. $newTag = 'set_current_'.$p_optArray[0];
  407. } elseif (strtolower($p_optArray[1]) == 'default') {
  408. $newTag = 'set_default_'.$p_optArray[0];
  409. } else {
  410. $newTag = 'set_' . $p_optArray[0];
  411. if ($p_optArray[0] == 'language') {
  412. $newTag.= ' name="' . strtolower($p_optArray[1]) . '"';
  413. } else {
  414. if (preg_match('/ /', $p_optArray[1])) {
  415. $p_optArray[1] = '"'.$p_optArray[1].'"';
  416. }
  417. $newTag.= (isset($p_optArray[1])) ? ' ' . $p_optArray[1] : '';
  418. $newTag.= (isset($p_optArray[2])) ? '="' . $p_optArray[2] . '"' : '';
  419. }
  420. }
  421. }
  422. return $newTag;
  423. } // fn BuildEnvironmentalStatement
  424. /**
  425. * @param array $p_optArray
  426. *
  427. * @return string $newTag
  428. */
  429. public static function BuildUrxStatement($p_optArray)
  430. {
  431. $newTag = $p_optArray[0];
  432. $ifBlockStack = TemplateConverterIfBlock::GetIfBlockStack();
  433. $ifBlockStackSize = sizeof($ifBlockStack);
  434. if ($ifBlockStackSize > 0) {
  435. $option = '';
  436. $idx = $ifBlockStackSize - 1;
  437. switch($ifBlockStack[$idx]->getIfBlock()) {
  438. case 'nextitems':
  439. $option = 'next_items'; break;
  440. case 'previous_items':
  441. $option = 'previous_items'; break;
  442. case 'nextsubtitles':
  443. $withField = self::GetWithBodyField();
  444. $option = 'next_subtitle';
  445. $option.= (strlen($withField) > 0) ? ' '.$withField : '';
  446. break;
  447. case 'prevsubtitles':
  448. $withField = self::GetWithBodyField();
  449. $option = 'previous_subtitle';
  450. $option.= (strlen($withField) > 0) ? ' '.$withField : '';
  451. break;
  452. }
  453. $newTag.= (strlen($option) > 0) ? ' options="'.$option.'"' : '';
  454. }
  455. if (sizeof($p_optArray) > 1) {
  456. $newTag.= ' options="';
  457. for ($x = 1; $x < sizeof($p_optArray); $x++) {
  458. $newTag.= ($x > 1) ? ' ' : '';
  459. $newTag.= strtolower($p_optArray[$x]);
  460. }
  461. $newTag.= '"';
  462. }
  463. return $newTag;
  464. } // fn BuildUrxStatement
  465. /**
  466. * @param array $p_optArray
  467. *
  468. * @return string $newTag
  469. */
  470. public static function BuildSimpleFormStatement($p_optArray)
  471. {
  472. $newTag = $p_optArray[0] . '_form';
  473. if ($p_optArray[0] == 'user') {
  474. $tmpArr = array();
  475. $tmpArr[0] = $p_optArray[0];
  476. for ($i = 1; $i < sizeof($p_optArray); $i++) {
  477. if (isset($p_optArray[$i+1])) {
  478. $tmpArr[$i] = $p_optArray[$i+1];
  479. }
  480. }
  481. $p_optArray = $tmpArr;
  482. }
  483. $newTag.= (isset($p_optArray[1])) ? ' template="' . $p_optArray[1] . '"' : '';
  484. $newTag.= (isset($p_optArray[2])) ? ' submit_button="' . $p_optArray[2] . '"' : '';
  485. $newTag.= (isset($p_optArray[3])) ? ' html_code="' . $p_optArray[3] . '"' : '';
  486. return $newTag;
  487. } // fn BuildSimpleFormStatement
  488. /**
  489. * @param array $p_optArray
  490. *
  491. * @return string $newTag
  492. */
  493. public static function BuildArticleCommentFormStatement($p_optArray)
  494. {
  495. $newTag = 'article_comment_form';
  496. $newTag.= (isset($p_optArray[1])) ? ' template="'.$p_optArray[1].'"' : '';
  497. $newTag.= (isset($p_optArray[2])) ? ' submit_button="'.$p_optArray[2].'"' : '';
  498. $newTag.= (isset($p_optArray[3])) ? ' preview_button="'.$p_optArray[3].'"' : '';
  499. return $newTag;
  500. } // fn BuildArticleCommentFormStatement
  501. /**
  502. * @param array $p_optArray
  503. *
  504. * @return string $newTag
  505. */
  506. public static function BuildSubscriptionFormStatement($p_optArray)
  507. {
  508. $newTag = 'subscription_form';
  509. $newTag.= (isset($p_optArray[1])) ? ' type="'.$p_optArray[1].'"' : '';
  510. $newTag.= (isset($p_optArray[2])) ? ' template="'.$p_optArray[2].'"' : '';
  511. $newTag.= (isset($p_optArray[3])) ? ' submit_button="'.$p_optArray[3].'"' : '';
  512. $newTag.= (isset($p_optArray[4])) ? ' total_field="'.$p_optArray[4].'"' : '';
  513. $newTag.= (isset($p_optArray[5])) ? ' evaluated_field="'.$p_optArray[5].'"' : '';
  514. return $newTag;
  515. } // fn BuildArticleCommentFormStatement
  516. /**
  517. * @param array $p_optArray
  518. *
  519. * @return string $newTag
  520. */
  521. public static function BuildEndFormStatement($p_optArray)
  522. {
  523. $newTag = '/' . substr($p_optArray[0], 3) . '_form';
  524. return $newTag;
  525. } // fn BuildEndFormStatement
  526. /**
  527. * @param array $p_optArray
  528. *
  529. * @return string $newTag
  530. */
  531. public static function BuildEditStatement($p_optArray)
  532. {
  533. $newTag = 'camp_edit';
  534. $newTag.= (isset($p_optArray[1])) ? ' object="'.strtolower($p_optArray[1]).'"' : '';
  535. $newTag.= (isset($p_optArray[2])) ? ' attribute="'.strtolower($p_optArray[2]).'"' : '';
  536. if (isset($p_optArray[3])) {
  537. if (is_integer($p_optArray[3])) {
  538. $newTag.= ' size="'.$p_optArray[3].'"';
  539. } elseif (strtolower($p_optArray[3]) == 'html') {
  540. $newTag.= ' html_code="';
  541. $newTag.= (isset($p_optArray[4])) ? $p_optArray[4].'"' : '"';
  542. }
  543. }
  544. return $newTag;
  545. } // fn BuildEditStatement
  546. /**
  547. * @param array $p_optArray
  548. *
  549. * @return string $newTag
  550. */
  551. public static function BuildSelectStatement($p_optArray)
  552. {
  553. $newTag = 'camp_select';
  554. $newTag.= (isset($p_optArray[1])) ? ' object="'.strtolower($p_optArray[1]).'"' : '';
  555. $newTag.= (isset($p_optArray[2])) ? ' attribute="'.strtolower($p_optArray[2]).'"' : '';
  556. if (strtolower($p_optArray[2]) == 'gender') {
  557. $newTag.= (isset($p_optArray[3])) ? ' male_name="'.strtolower($p_optArray[3]).'"' : '';
  558. $newTag.= (isset($p_optArray[4])) ? ' female_name="'.strtolower($p_optArray[4]).'"' : '';
  559. }
  560. return $newTag;
  561. } // fn BuildSelectStatement
  562. /**
  563. * @param array $p_optArray
  564. *
  565. * @return string $newTag
  566. */
  567. public static function BuildHTMLEncodingStatement($p_optArray)
  568. {
  569. if (isset($p_optArray[1]) && strtolower($p_optArray[1]) == 'off') {
  570. $newTag = 'disable_html_encoding';
  571. } else {
  572. $newTag = 'enable_html_encoding';
  573. }
  574. return $newTag;
  575. } // fn BuildHTMLEncodingStatement
  576. /**
  577. * @param array $p_optArray
  578. *
  579. * @return string $newTag
  580. */
  581. public static function BuildWithStatement($p_optArray)
  582. {
  583. if ($p_optArray[0] == 'with') {
  584. if (isset($p_optArray[1])) {
  585. self::$m_withArticleType = strtolower($p_optArray[1]);
  586. }
  587. if (isset($p_optArray[2])) {
  588. self::$m_withBodyField = strtolower($p_optArray[2]);
  589. }
  590. } elseif ($p_optArray[0] == 'endwith') {
  591. self::$m_withArticleType = '';
  592. self::$m_withBodyField = '';
  593. }
  594. $newTag = 'DISCARD_SENTENCE';
  595. return $newTag;
  596. } // fn BuildWithStatement
  597. } // class TemplateConverterHelper
  598. ?>