Base for a static organization website

HtmlToolbarHelper.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @since DebugKit 0.1
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. App::uses('ToolbarHelper', 'DebugKit.View/Helper');
  15. App::uses('Security', 'Utility');
  16. /**
  17. * Html Toolbar Helper
  18. *
  19. * Injects the toolbar elements into HTML layouts.
  20. * Contains helper methods for
  21. *
  22. * @since DebugKit 0.1
  23. */
  24. class HtmlToolbarHelper extends ToolbarHelper {
  25. /**
  26. * helpers property
  27. *
  28. * @var array
  29. */
  30. public $helpers = array('Html', 'Form');
  31. /**
  32. * settings property
  33. *
  34. * @var array
  35. */
  36. public $settings = array('format' => 'html', 'forceEnable' => false);
  37. /**
  38. * Recursively goes through an array and makes neat HTML out of it.
  39. *
  40. * @param mixed $values Array to make pretty.
  41. * @param integer $openDepth Depth to add open class
  42. * @param integer $currentDepth current depth.
  43. * @param boolean $doubleEncode
  44. * @return string
  45. */
  46. public function makeNeatArray($values, $openDepth = 0, $currentDepth = 0, $doubleEncode = false) {
  47. static $printedObjects = null;
  48. if ($currentDepth === 0) {
  49. $printedObjects = new SplObjectStorage();
  50. }
  51. $className = "neat-array depth-$currentDepth";
  52. if ($openDepth > $currentDepth) {
  53. $className .= ' expanded';
  54. }
  55. $nextDepth = $currentDepth + 1;
  56. $out = "<ul class=\"$className\">";
  57. if (!is_array($values)) {
  58. if (is_bool($values)) {
  59. $values = array($values);
  60. }
  61. if ($values === null) {
  62. $values = array(null);
  63. }
  64. }
  65. if (empty($values)) {
  66. $values[] = '(empty)';
  67. }
  68. foreach ($values as $key => $value) {
  69. $out .= '<li><strong>' . h($key, $doubleEncode) . '</strong>';
  70. if (is_array($value) && count($value) > 0) {
  71. $out .= '(array)';
  72. } elseif (is_object($value)) {
  73. $out .= '(object)';
  74. }
  75. if ($value === null) {
  76. $value = '(null)';
  77. }
  78. if ($value === false) {
  79. $value = '(false)';
  80. }
  81. if ($value === true) {
  82. $value = '(true)';
  83. }
  84. if (empty($value) && $value != 0) {
  85. $value = '(empty)';
  86. }
  87. if ($value instanceof Closure) {
  88. $value = 'function';
  89. }
  90. $isObject = is_object($value);
  91. if ($isObject && $printedObjects->contains($value)) {
  92. $isObject = false;
  93. $value = ' - recursion';
  94. }
  95. if ($isObject) {
  96. $printedObjects->attach($value);
  97. }
  98. if (
  99. (
  100. $value instanceof ArrayAccess ||
  101. $value instanceof Iterator ||
  102. is_array($value) ||
  103. $isObject
  104. ) && !empty($value)
  105. ) {
  106. $out .= $this->makeNeatArray($value, $openDepth, $nextDepth, $doubleEncode);
  107. } else {
  108. $out .= h($value, $doubleEncode);
  109. }
  110. $out .= '</li>';
  111. }
  112. $out .= '</ul>';
  113. return $out;
  114. }
  115. /**
  116. * Create an HTML message
  117. *
  118. * @param string $label label content
  119. * @param string $message message content
  120. * @return string
  121. */
  122. public function message($label, $message) {
  123. return sprintf('<p><strong>%s</strong> %s</p>', $label, $message);
  124. }
  125. /**
  126. * Start a panel.
  127. * Make a link and anchor.
  128. *
  129. * @param $title
  130. * @param $anchor
  131. * @return string
  132. */
  133. public function panelStart($title, $anchor) {
  134. $link = $this->Html->link($title, '#' . $anchor);
  135. return $link;
  136. }
  137. /**
  138. * Create a table.
  139. *
  140. * @param array $rows Rows to make.
  141. * @param array $headers Optional header row.
  142. * @return string
  143. */
  144. public function table($rows, $headers = array()) {
  145. $out = '<table class="debug-table">';
  146. if (!empty($headers)) {
  147. $out .= $this->Html->tableHeaders($headers);
  148. }
  149. $out .= $this->Html->tableCells($rows, array('class' => 'odd'), array('class' => 'even'), false, false);
  150. $out .= '</table>';
  151. return $out;
  152. }
  153. /**
  154. * Send method
  155. *
  156. * @return void
  157. */
  158. public function send() {
  159. if (!$this->settings['forceEnable'] && Configure::read('debug') == 0) {
  160. return;
  161. }
  162. $view = $this->_View;
  163. $head = '';
  164. if (isset($view->viewVars['debugToolbarCss']) && !empty($view->viewVars['debugToolbarCss'])) {
  165. $head .= $this->Html->css($view->viewVars['debugToolbarCss']);
  166. }
  167. $js = sprintf('window.DEBUGKIT_JQUERY_URL = "%s";', $this->webroot('/debug_kit/js/jquery.js'));
  168. $head .= $this->Html->scriptBlock($js);
  169. if (isset($view->viewVars['debugToolbarJavascript'])) {
  170. foreach ($view->viewVars['debugToolbarJavascript'] as $script) {
  171. if ($script) {
  172. $head .= $this->Html->script($script);
  173. }
  174. }
  175. }
  176. $search = '</head>';
  177. $pos = strpos($view->output, $search);
  178. if ($pos !== false) {
  179. $view->output = substr_replace($view->output, $head . "\n</head>", $pos, strlen($search));
  180. }
  181. $toolbar = $view->element('debug_toolbar', array('disableTimer' => true), array('plugin' => 'DebugKit'));
  182. $search = '</body>';
  183. $pos = strrpos($view->output, $search);
  184. if ($pos !== false) {
  185. $view->output = substr_replace($view->output, $toolbar . "\n</body>", $pos, strlen($search));
  186. }
  187. }
  188. /**
  189. * Generates a SQL explain link for a given query
  190. *
  191. * @param string $sql SQL query string you want an explain link for.
  192. * @param $connection
  193. * @return string Rendered Html link or '' if the query is not a select/describe
  194. */
  195. public function explainLink($sql, $connection) {
  196. if (!preg_match('/^[\s()]*SELECT/i', $sql)) {
  197. return '';
  198. }
  199. $sql = str_replace(array("\n", "\t"), ' ', $sql);
  200. $hash = Security::hash($sql . $connection, 'sha1', true);
  201. $url = array(
  202. 'plugin' => 'debug_kit',
  203. 'controller' => 'toolbar_access',
  204. 'action' => 'sql_explain'
  205. );
  206. foreach (Router::prefixes() as $prefix) {
  207. $url[$prefix] = false;
  208. }
  209. $this->explainLinkUid = (isset($this->explainLinkUid) ? $this->explainLinkUid + 1 : 0);
  210. $uid = $this->explainLinkUid . '_' . rand(0, 10000);
  211. $form = $this->Form->create('log', array('url' => $url, 'id' => "logForm{$uid}"));
  212. $form .= $this->Form->hidden('log.ds', array('id' => "logDs{$uid}", 'value' => $connection));
  213. $form .= $this->Form->hidden('log.sql', array('id' => "logSql{$uid}", 'value' => $sql));
  214. $form .= $this->Form->hidden('log.hash', array('id' => "logHash{$uid}", 'value' => $hash));
  215. $form .= $this->Form->submit(__d('debug_kit', 'Explain'), array(
  216. 'div' => false,
  217. 'class' => 'sql-explain-link'
  218. ));
  219. $form .= $this->Form->end();
  220. return $form;
  221. }
  222. }