PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/snippets/ditto/classes/template.class.inc.php

https://github.com/good-web-master/modx.evo.custom
PHP | 234 lines | 172 code | 17 blank | 45 comment | 55 complexity | 20b012737ec18a0b42e12bb75dccb5c3 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MIT, BSD-3-Clause
  1. <?php
  2. /*
  3. * Title: Template Class
  4. * Purpose:
  5. * The Template class contains all functions relating to Ditto's
  6. * handling of templates and any supporting functions they need
  7. */
  8. class template{
  9. var $language,$fields,$current;
  10. // ---------------------------------------------------
  11. // Function: template
  12. // Set the class language and fields variables
  13. // ---------------------------------------------------
  14. function template() {
  15. $this->language = $GLOBALS["ditto_lang"];
  16. $this->fields = array (
  17. "db" => array (),
  18. "tv" => array (),
  19. "custom" => array (),
  20. "item" => array (),
  21. "qe" => array (),
  22. "phx" => array (),
  23. "rss" => array (),
  24. "json" => array (),
  25. "xml" => array (),
  26. "unknown" => array()
  27. );
  28. }
  29. // ---------------------------------------------------
  30. // Function: process
  31. // Take the templates and parse them for tempalte variables,
  32. // Check to make sure they have fields, and sort the fields
  33. // ---------------------------------------------------
  34. function process($template) {
  35. if (!isset($template["base"])) {
  36. $template["base"] = $template["default"];
  37. } else {
  38. unset($template["default"]);
  39. }
  40. foreach ($template as $name=>$tpl) {
  41. if(!empty($tpl) && $tpl != "") {
  42. $templates[$name] = $this->fetch($tpl);
  43. }
  44. }
  45. $fieldList = array();
  46. foreach ($templates as $tplName=>$tpl) {
  47. $check = $this->findTemplateVars($tpl);
  48. if (is_array($check)) {
  49. $fieldList = array_merge($check, $fieldList);
  50. } else {
  51. switch ($tplName) {
  52. case "base":
  53. $displayName = "tpl";
  54. break;
  55. case "default":
  56. $displayName = "tpl";
  57. break;
  58. default:
  59. $displayName = "tpl".$tplName;
  60. break;
  61. }
  62. $templates[$tplName] = str_replace("[+tpl+]",$displayName,$this->language["bad_tpl"]);
  63. }
  64. }
  65. $fieldList = array_unique($fieldList);
  66. $fields = $this->sortFields($fieldList);
  67. $checkAgain = array ("qe", "json", "xml");
  68. foreach ($checkAgain as $type) {
  69. $fields = array_merge_recursive($fields, $this->sortFields($fields[$type]));
  70. }
  71. $this->fields = $fields;
  72. return $templates;
  73. }
  74. // ---------------------------------------------------
  75. // Function: findTemplateVars
  76. // Find al the template variables in the template
  77. // ---------------------------------------------------
  78. function findTemplateVars($tpl) {
  79. preg_match_all('~\[\+(.*?)\+\]~', $tpl, $matches);
  80. $TVs = array();
  81. foreach($matches[1] as $tv) {
  82. $match = explode(":", $tv);
  83. $TVs[strtolower($match[0])] = $match[0];
  84. }
  85. if (count($TVs) >= 1) {
  86. return array_values($TVs);
  87. } else {
  88. return false;
  89. }
  90. }
  91. // ---------------------------------------------------
  92. // Function: sortFields
  93. // Sort the array of fields provided by type
  94. // ---------------------------------------------------
  95. function sortFields ($fieldList) {
  96. global $ditto_constantFields;
  97. $dbFields = $ditto_constantFields["db"];
  98. $tvFields = $ditto_constantFields["tv"];
  99. $fields = array (
  100. "db" => array (),
  101. "tv" => array (),
  102. "custom" => array (),
  103. "item" => array (),
  104. "qe" => array (),
  105. "phx" => array (),
  106. "rss" => array (),
  107. "json" => array (),
  108. "xml" => array (),
  109. "unknown" => array()
  110. );
  111. $custom = array("author","date","url","title","ditto_iteration");
  112. foreach ($fieldList as $field) {
  113. if (substr($field, 0, 4) == "rss_") {
  114. $fields['rss'][] = substr($field,4);
  115. }else if (substr($field, 0, 4) == "xml_") {
  116. $fields['xml'][] = substr($field,4);
  117. }else if (substr($field, 0, 5) == "json_") {
  118. $fields['json'][] = substr($field,5);
  119. }else if (substr($field, 0, 4) == "item") {
  120. $fields['item'][] = substr($field, 4);
  121. }else if (substr($field, 0, 1) == "#") {
  122. $fields['qe'][] = substr($field,1);
  123. }else if (substr($field, 0, 3) == "phx") {
  124. $fields['phx'][] = $field;
  125. }else if (in_array($field, $dbFields)) {
  126. $fields['db'][] = $field;
  127. }else if(in_array($field, $tvFields)){
  128. $fields['tv'][] = $field;
  129. }else if(substr($field, 0, 2) == "tv" && in_array(substr($field,2), $tvFields)) {
  130. $fields['tv'][] = substr($field,2);
  131. // TODO: Remove TV Prefix support in Ditto
  132. }else if (in_array($field, $custom)) {
  133. $fields['custom'][] = $field;
  134. }else {
  135. $fields['unknown'][] = $field;
  136. }
  137. }
  138. return $fields;
  139. }
  140. // ---------------------------------------------------
  141. // Function: replace
  142. // Replcae placeholders with their values
  143. // ---------------------------------------------------
  144. function replace( $placeholders, $tpl ) {
  145. $keys = array();
  146. $values = array();
  147. foreach ($placeholders as $key=>$value) {
  148. $keys[] = '[+'.$key.'+]';
  149. $values[] = $value;
  150. }
  151. return str_replace($keys,$values,$tpl);
  152. }
  153. // ---------------------------------------------------
  154. // Function: determine
  155. // Determine the correct template to apply
  156. // ---------------------------------------------------
  157. function determine($templates,$x,$start,$stop,$id) {
  158. global $modx;
  159. // determine current template
  160. $currentTPL = "base";
  161. if ($x % 2 && !empty($templates["alt"])) {
  162. $currentTPL = "alt";
  163. }
  164. if ($id == $modx->documentObject['id'] && !empty($templates["current"])){
  165. $currentTPL = "current";
  166. }
  167. if ($x == 0 && !empty($templates["first"])) {
  168. $currentTPL = "first";
  169. }
  170. if ($x == ($stop -1) && !empty($templates["last"])) {
  171. $currentTPL = "last";
  172. }
  173. $this->current = $currentTPL;
  174. return $templates[$currentTPL];
  175. }
  176. // ---------------------------------------------------
  177. // Function: fetch
  178. // Get a template, based on version by Doze
  179. //
  180. // http://modxcms.com/forums/index.php/topic,5344.msg41096.html#msg41096
  181. // ---------------------------------------------------
  182. function fetch($tpl){
  183. global $modx;
  184. $template = "";
  185. if ($modx->getChunk($tpl) != "") {
  186. $template = $modx->getChunk($tpl);
  187. } else if(substr($tpl, 0, 6) == "@FILE:") {
  188. $template = $this->get_file_contents(MODX_BASE_PATH.substr($tpl, 6));
  189. } else if(substr($tpl, 0, 6) == "@CODE:") {
  190. $template = substr($tpl, 6);
  191. } else if(substr($tpl, 0, 5) == "@FILE") {
  192. $template = $this->get_file_contents(trim(substr($tpl, 5)));
  193. } else if(substr($tpl, 0, 5) == "@CODE") {
  194. $template = trim(substr($tpl, 5));
  195. } else {
  196. $template = $this->language['missing_placeholders_tpl'];
  197. }
  198. return $template;
  199. }
  200. // ---------------------------------------------------
  201. // Function: get_file_contents
  202. // Returns the contents of file name passed
  203. //
  204. // From http://www.nutt.net/2006/07/08/file_get_contents-function-for-php-4/#more-210
  205. // ---------------------------------------------------
  206. function get_file_contents($filename) {
  207. if (!function_exists('file_get_contents')) {
  208. $fhandle = fopen($filename, "r");
  209. $fcontents = fread($fhandle, filesize($filename));
  210. fclose($fhandle);
  211. } else {
  212. $fcontents = file_get_contents($filename);
  213. }
  214. return $fcontents;
  215. }
  216. }
  217. ?>