include.parsetpl.php 3.9 KB

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