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

/private/_core/plug-ins/_required/views.core.php

https://bitbucket.org/securebucket/arch-php
PHP | 197 lines | 127 code | 32 blank | 38 comment | 25 complexity | ba336e3fb26ab325fcddd640693a3964 MD5 | raw file
  1. <?php
  2. /*
  3. http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  4. @author Arch PHP - Richard Wagener <code@securebucket.com>
  5. http://archphp.org/docs#4eb6e35011aec61146000006
  6. */
  7. class views {
  8. /**
  9. * Stores sections for later includes.
  10. * Like Javascript Jquery code that should be added above </body> tag
  11. * @staticvar array $sectionIncludes holds the information
  12. * @param string $name name of section ['javascript']['css']
  13. * @param string $data the information that should be saved
  14. * @param boolean $return should this return the array.
  15. * @param type $priority priority allows you to sort it will sort from 0 to 10
  16. */
  17. static public function section($return = false, $name = '', $data = '', $priority = '0') {
  18. static $sectionIncludes = array();
  19. if ($return === true) {
  20. return $sectionIncludes;
  21. }
  22. if ($name === 'url')
  23. $sectionIncludes['url'] = $data;
  24. elseif ($name === 'navSelect')
  25. $sectionIncludes['navSelect'] = $data;
  26. else {
  27. if (!isset($sectionIncludes[$name]))
  28. $sectionIncludes[$name] = array();
  29. if (!isset($sectionIncludes[$name][$priority]))
  30. $sectionIncludes[$name][$priority] = array();
  31. $sectionIncludes[$name][$priority][] = $data;
  32. }
  33. }
  34. /**
  35. * Loads a view file. You can supply $data with a array or single.
  36. * If you have a css/js file in that view folder. It will also be
  37. * loaded.
  38. * @param <string> $path file to render e.g '_core/template.php'
  39. * @param <array|string> $data content used in the file called.
  40. * @param <boolean> $returnVar if true. Content will be returned in a array
  41. * @return <array> ['content'] has the data outputted
  42. */
  43. static public function load($path, $data = array(), $returnVar = false, $compress = false) {
  44. $return = array();
  45. // make sure it's not the core view folder.
  46. // This is loaded when going to /media/js and /media/css
  47. if (!stristr($path, '_core')) {
  48. if (!defined('INCLUDE_EMBED_JS'))
  49. define('INCLUDE_EMBED_JS'
  50. , self::MediaScanInclude(DIR_VIEWS . $path, 'js'));
  51. if (!defined('INCLUDE_EMBED_CSS'))
  52. define('INCLUDE_EMBED_CSS'
  53. , self::MediaScanInclude(DIR_VIEWS . $path, 'css'));
  54. }
  55. if (substr($path, -4) === '.css')
  56. $path .= '';
  57. elseif (substr($path, -3) === '.js')
  58. $path .= '';
  59. elseif (substr($path, -5) === '.less')
  60. $path .= '';
  61. elseif (substr($path, -5) === '.html')
  62. $path .= '';
  63. elseif (substr($path, -4) != '.php')
  64. $path .= '.php';
  65. ob_start();
  66. include(DIR_VIEWS . $path);
  67. $return['content'] = ob_get_contents();
  68. ob_end_clean();
  69. if (!$returnVar) {
  70. if($compress) echo evar::RemoveExtraSpaces ($return['content']);
  71. else echo $return['content'];
  72. } else {
  73. return $return;
  74. }
  75. }
  76. /**
  77. * Will scan a folder for css/js files, and combine them.
  78. * @param <string> $directory directory that should be scanned
  79. * @param <string> $search only return files that match filename
  80. * @return <string> return the combined media files
  81. */
  82. static function MediaScanInclude($directory, $search) {
  83. try {
  84. $build = '';
  85. $path = dirname($directory);
  86. $path .= '/' . $search;
  87. $objects = new RecursiveIteratorIterator(
  88. new RecursiveDirectoryIterator($path),
  89. RecursiveIteratorIterator::SELF_FIRST);
  90. foreach ($objects as $name => $object) {
  91. if (
  92. stristr($name, '.' . $search)
  93. && !empty($name)
  94. && !stristr($name, $search . '~')
  95. && !stristr($name, '/.')
  96. ) {
  97. ob_start();
  98. include $name;
  99. $build .= ob_get_contents();
  100. ob_end_clean();
  101. }
  102. }
  103. } catch (Exception $e) {
  104. trigger_error('Caught exception - Loading Class File: '
  105. . $e->getMessage() . "<br>");
  106. }
  107. return $build;
  108. }
  109. /**
  110. * Loads specific files, specified in the config.inc.php file.
  111. * @param <enum> $type css | js
  112. * @param <string> $data csv of files. Will be loaded in order
  113. * @return <type> returns outputted combined content
  114. */
  115. static function MediaInclude($type, $data) {
  116. try {
  117. $build = '';
  118. ob_start();
  119. if ($type === 'css') {
  120. foreach (explode(',', $data) as $cssinclude) {
  121. if (!empty($cssinclude))
  122. include DIR_VIEWS_CORE_CSS . $cssinclude;
  123. }
  124. } elseif ($type === 'js') {
  125. foreach (explode(',', $data) as $jsinclude) {
  126. if (!empty($jsinclude))
  127. include DIR_VIEWS_CORE_JS . $jsinclude;
  128. }
  129. }
  130. $build .= ob_get_contents();
  131. ob_end_clean();
  132. return $build;
  133. } catch (Exception $e) {
  134. trigger_error('Caught exception - Loading Optional Class File: '
  135. . $e->getMessage() . "<br>");
  136. }
  137. }
  138. public static function render($data = array(), $compress = false) {
  139. $temp = self::section(true);
  140. if (isset($temp['js'])) {
  141. krsort($temp['js']);
  142. $data['js'] = array();
  143. foreach ($temp['js'] as $jsarray) {
  144. $temp['jst'][] = implode(' ', $jsarray);
  145. }
  146. $data['js'] = implode('', $temp['jst']);
  147. }
  148. if (isset($temp['css'])) {
  149. krsort($temp['css']);
  150. $data['css'] = array();
  151. foreach ($temp['css'] as $cssarray) {
  152. $temp['csst'][] = implode(' ', $cssarray);
  153. }
  154. $data['css'] = implode('', $temp['csst']);
  155. }
  156. if (isset($temp['url']))
  157. $data['url'] = $temp['url'];
  158. if (isset($temp['navSelect']))
  159. $data['navSelect'] = $temp['navSelect'];
  160. views::load('_core/template', $data, false, $compress);
  161. exit;
  162. }
  163. }
  164. ?>