include.parsetpl.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. if (!function_exists('parseTplElement')) {
  3. function parseTplElement(& $_cache, $_validTypes, $type, $source, $properties = null) {
  4. global $modx;
  5. $output = false;
  6. if (!is_string($type) || !in_array($type, $_validTypes)) $type = $modx->getOption('tplType', $properties, '@CHUNK');
  7. $content = false;
  8. switch ($type) {
  9. case '@FILE':
  10. $path = $modx->getOption('tplPath', $properties, $modx->getOption('assets_path', $properties, MODX_ASSETS_PATH) . 'elements/chunks/');
  11. $key = $path . $source;
  12. if (!isset($_cache['@FILE'])) $_cache['@FILE'] = array();
  13. if (!array_key_exists($key, $_cache['@FILE'])) {
  14. if (file_exists($key)) {
  15. $content = file_get_contents($key);
  16. }
  17. $_cache['@FILE'][$key] = $content;
  18. } else {
  19. $content = $_cache['@FILE'][$key];
  20. }
  21. if (!empty($content) && $content !== '0') {
  22. $chunk = $modx->newObject('modChunk', array('name' => $key));
  23. $chunk->setCacheable(false);
  24. $output = $chunk->process($properties, $content);
  25. }
  26. break;
  27. case '@INLINE':
  28. $uniqid = uniqid();
  29. $chunk = $modx->newObject('modChunk', array('name' => "{$type}-{$uniqid}"));
  30. $chunk->setCacheable(false);
  31. $output = $chunk->process($properties, $source);
  32. break;
  33. case '@CHUNK':
  34. default:
  35. $chunk = null;
  36. if (!isset($_cache['@CHUNK'])) $_cache['@CHUNK'] = array();
  37. if (!array_key_exists($source, $_cache['@CHUNK'])) {
  38. if ($chunk = $modx->getObject('modChunk', array('name' => $source))) {
  39. $_cache['@CHUNK'][$source] = $chunk->toArray('', true);
  40. } else {
  41. $_cache['@CHUNK'][$source] = false;
  42. }
  43. } elseif (is_array($_cache['@CHUNK'][$source])) {
  44. $chunk = $modx->newObject('modChunk');
  45. $chunk->fromArray($_cache['@CHUNK'][$source], '', true, true, true);
  46. }
  47. if (is_object($chunk)) {
  48. $chunk->setCacheable(false);
  49. $output = $chunk->process($properties);
  50. }
  51. break;
  52. }
  53. return $output;
  54. }
  55. }
  56. if (!function_exists('parseTpl')) {
  57. function parseTpl($tpl, $properties = null) {
  58. static $_tplCache;
  59. $_validTypes = array(
  60. '@CHUNK'
  61. ,'@FILE'
  62. ,'@INLINE'
  63. );
  64. $output = false;
  65. if (!empty($tpl)) {
  66. $bound = array(
  67. 'type' => '@CHUNK'
  68. ,'value' => $tpl
  69. );
  70. if (strpos($tpl, '@') === 0) {
  71. $endPos = strpos($tpl, ' ');
  72. if ($endPos > 2 && $endPos < 10) {
  73. $tt = substr($tpl, 0, $endPos);
  74. if (in_array($tt, $_validTypes)) {
  75. $bound['type'] = $tt;
  76. $bound['value'] = substr($tpl, $endPos + 1);
  77. }
  78. }
  79. }
  80. if (is_array($bound) && isset($bound['type']) && isset($bound['value'])) {
  81. $output = parseTplElement($_tplCache, $_validTypes, $bound['type'], $bound['value'], $properties);
  82. }
  83. }
  84. return $output;
  85. }
  86. }
  87. if (!function_exists('getDivisors')) {
  88. function getDivisors($integer) {
  89. $divisors = array();
  90. for ($i = $integer; $i > 1; $i--) {
  91. if (($integer % $i) === 0) {
  92. $divisors[] = $i;
  93. }
  94. }
  95. return $divisors;
  96. }
  97. }