PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Croogo/View/Helper/CroogoHelper.php

https://github.com/kareypowell/croogo
PHP | 444 lines | 321 code | 40 blank | 83 comment | 77 complexity | 258e62de6c65cb3ea7f3731e44413288 MD5 | raw file
  1. <?php
  2. App::uses('AppHelper', 'View/Helper');
  3. App::uses('CroogoStatus', 'Croogo.Lib');
  4. /**
  5. * Croogo Helper
  6. *
  7. * @category Helper
  8. * @package Croogo.Croogo.View.Helper
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class CroogoHelper extends AppHelper {
  15. public $helpers = array(
  16. 'Form' => array('className' => 'Croogo.CroogoForm'),
  17. 'Html' => array('className' => 'Croogo.CroogoHtml'),
  18. 'Croogo.Layout',
  19. 'Menus.Menus',
  20. );
  21. /**
  22. * Provides backward compatibility for deprecated methods
  23. */
  24. public function __call($method, $params) {
  25. if ($method == 'settingsInput') {
  26. if (!$this->_View->Helpers->loaded('SettingsForm')) {
  27. $this->_View->Helpers->load('Settings.SettingsForm');
  28. }
  29. $callable = array($this->_View->SettingsForm, 'input');
  30. return call_user_func_array($callable, $params);
  31. }
  32. }
  33. /**
  34. * Default Constructor
  35. *
  36. * @param View $View The View this helper is being attached to.
  37. * @param array $settings Configuration settings for the helper.
  38. */
  39. public function __construct(View $View, $settings = array()) {
  40. $this->helpers[] = Configure::read('Site.acl_plugin') . '.' . Configure::read('Site.acl_plugin');
  41. parent::__construct($View, $settings);
  42. $this->_CroogoStatus = new CroogoStatus();
  43. }
  44. /**
  45. * Before Render callback
  46. */
  47. public function beforeRender($viewFile) {
  48. if (isset($this->request->params['admin'])) {
  49. Croogo::dispatchEvent('Croogo.setupAdminData', $this->_View);
  50. }
  51. }
  52. public function statuses() {
  53. return $this->_CroogoStatus->statuses();
  54. }
  55. /**
  56. * Convenience method to Html::script() for admin views
  57. *
  58. * This method does nothing if request is ajax or not in admin prefix.
  59. *
  60. * @see HtmlHelper::script()
  61. * @param $url string|array Javascript files to include
  62. * @param array|boolean Options or Html attributes
  63. * @return mixed String of <script /> tags or null
  64. */
  65. public function adminScript($url, $options = array()) {
  66. $options = Hash::merge(array('inline' => false), $options);
  67. if ($this->request->is('ajax') || empty($this->request->params['admin'])) {
  68. return;
  69. }
  70. return $this->Html->script($url, $options);
  71. }
  72. /** Generate Admin menus added by CroogoNav::add()
  73. *
  74. * @param array $menus
  75. * @param array $options
  76. * @return string menu html tags
  77. */
  78. public function adminMenus($menus, $options = array(), $depth = 0) {
  79. $options = Hash::merge(array(
  80. 'type' => 'sidebar',
  81. 'children' => true,
  82. 'htmlAttributes' => array(
  83. 'class' => 'nav nav-stacked',
  84. ),
  85. ), $options);
  86. $aclPlugin = Configure::read('Site.acl_plugin');
  87. $userId = AuthComponent::user('id');
  88. if (empty($userId)) {
  89. return '';
  90. }
  91. $sidebar = $options['type'] === 'sidebar';
  92. $htmlAttributes = $options['htmlAttributes'];
  93. $out = null;
  94. $sorted = Hash::sort($menus, '{s}.weight', 'ASC');
  95. if (empty($this->Role)) {
  96. $this->Role = ClassRegistry::init('Users.Role');
  97. $this->Role->Behaviors->attach('Croogo.Aliasable');
  98. }
  99. $currentRole = $this->Role->byId($this->Layout->getRoleId());
  100. foreach ($sorted as $menu) {
  101. if (isset($menu['separator'])) {
  102. $liOptions['class'] = 'divider';
  103. $out .= $this->Html->tag('li', null, $liOptions);
  104. continue;
  105. }
  106. if ($currentRole != 'admin' && !$this->{$aclPlugin}->linkIsAllowedByUserId($userId, $menu['url'])) {
  107. continue;
  108. }
  109. if (empty($menu['htmlAttributes']['class'])) {
  110. $menuClass = Inflector::slug(strtolower('menu-' . $menu['title']), '-');
  111. $menu['htmlAttributes'] = Hash::merge(array(
  112. 'class' => $menuClass
  113. ), $menu['htmlAttributes']);
  114. }
  115. $title = '';
  116. if ($menu['icon'] === false) {
  117. } elseif (empty($menu['icon'])) {
  118. $menu['htmlAttributes'] += array('icon' => 'white');
  119. } else {
  120. $menu['htmlAttributes'] += array('icon' => $menu['icon']);
  121. }
  122. if ($sidebar) {
  123. $title .= '<span>' . $menu['title'] . '</span>';
  124. } else {
  125. $title .= $menu['title'];
  126. }
  127. $children = '';
  128. if (!empty($menu['children'])) {
  129. $childClass = '';
  130. if ($sidebar) {
  131. $childClass = 'nav nav-stacked sub-nav ';
  132. $childClass .= ' submenu-' . Inflector::slug(strtolower($menu['title']), '-');
  133. if ($depth > 0) {
  134. $childClass .= ' dropdown-menu';
  135. }
  136. } else {
  137. if ($depth == 0) {
  138. $childClass = 'dropdown-menu';
  139. }
  140. }
  141. $children = $this->adminMenus($menu['children'], array(
  142. 'type' => $options['type'],
  143. 'children' => true,
  144. 'htmlAttributes' => array('class' => $childClass),
  145. ), $depth + 1);
  146. $menu['htmlAttributes']['class'] .= ' hasChild dropdown-close';
  147. }
  148. $menu['htmlAttributes']['class'] .= ' sidebar-item';
  149. $menuUrl = $this->url($menu['url']);
  150. if ($menuUrl == env('REQUEST_URI')) {
  151. if (isset($menu['htmlAttributes']['class'])) {
  152. $menu['htmlAttributes']['class'] .= ' current';
  153. } else {
  154. $menu['htmlAttributes']['class'] = 'current';
  155. }
  156. }
  157. if (!$sidebar && !empty($children)) {
  158. if ($depth == 0) {
  159. $title .= ' <b class="caret"></b>';
  160. }
  161. $menu['htmlAttributes']['class'] = 'dropdown-toggle';
  162. $menu['htmlAttributes']['data-toggle'] = 'dropdown';
  163. }
  164. if (isset($menu['before'])) {
  165. $title = $menu['before'] . $title;
  166. }
  167. if (isset($menu['after'])) {
  168. $title = $title . $menu['after'];
  169. }
  170. $link = $this->Html->link($title, $menu['url'], $menu['htmlAttributes']);
  171. $liOptions = array();
  172. if ($sidebar && !empty($children) && $depth > 0) {
  173. $liOptions['class'] = ' dropdown-submenu';
  174. }
  175. if (!$sidebar && !empty($children)) {
  176. if ($depth > 0) {
  177. $liOptions['class'] = ' dropdown-submenu';
  178. } else {
  179. $liOptions['class'] = ' dropdown';
  180. }
  181. }
  182. $out .= $this->Html->tag('li', $link . $children, $liOptions);
  183. }
  184. if (!$sidebar && $depth > 0) {
  185. $htmlAttributes['class'] = 'dropdown-menu';
  186. }
  187. return $this->Html->tag('ul', $out, $htmlAttributes);
  188. }
  189. /**
  190. * Show links under Actions column
  191. *
  192. * @param integer $id
  193. * @param array $options
  194. * @return string
  195. */
  196. public function adminRowActions($id, $options = array()) {
  197. $output = '';
  198. $rowActions = Configure::read('Admin.rowActions.' . Inflector::camelize($this->request->params['controller']) . '/' . $this->request->params['action']);
  199. if (is_array($rowActions)) {
  200. foreach ($rowActions as $title => $link) {
  201. $linkOptions = $options;
  202. $confirmMessage = false;
  203. if (is_array($link)) {
  204. $config = $link[key($link)];
  205. if (isset($config['options'])) {
  206. $linkOptions = Hash::merge($options, $config['options']);
  207. }
  208. if (isset($config['confirmMessage'])) {
  209. $confirmMessage = $config['confirmMessage'];
  210. unset($config['confirmMessage']);
  211. }
  212. if (isset($config['title'])) {
  213. $title = $config['title'];
  214. }
  215. $link = key($link);
  216. }
  217. $link = $this->Menus->linkStringToArray(str_replace(':id', $id, $link));
  218. $output .= $this->adminRowAction($title, $link, $linkOptions, $confirmMessage);
  219. }
  220. }
  221. return $output;
  222. }
  223. /**
  224. * Show link under Actions column
  225. *
  226. * ### Options:
  227. *
  228. * - `method` - when 'POST' is specified, the FormHelper::postLink() will be
  229. * used instead of HtmlHelper::link()
  230. * - `rowAction` when bulk submissions is used, defines which action to use.
  231. *
  232. * @param string $title The content to be wrapped by <a> tags.
  233. * @param string|array $url Cake-relative URL or array of URL parameters, or external URL (starts with http://)
  234. * @param array $options Array of HTML attributes.
  235. * @param string $confirmMessage JavaScript confirmation message.
  236. * @return string An `<a />` element
  237. */
  238. public function adminRowAction($title, $url = null, $options = array(), $confirmMessage = false) {
  239. $action = false;
  240. $options = Hash::merge(array(
  241. 'escapeTitle' => false,
  242. 'escape' => true,
  243. ), $options);
  244. if (is_array($url)) {
  245. $action = $url['action'];
  246. if (isset($options['class'])) {
  247. $options['class'] .= ' ' . $url['action'];
  248. } else {
  249. $options['class'] = $url['action'];
  250. }
  251. }
  252. if (isset($options['icon']) && empty($title)) {
  253. $options['iconInline'] = false;
  254. }
  255. if (!empty($options['rowAction'])) {
  256. $options['data-row-action'] = $options['rowAction'];
  257. unset($options['rowAction']);
  258. return $this->_bulkRowAction($title, $url, $options, $confirmMessage);
  259. }
  260. if (!empty($options['method']) && strcasecmp($options['method'], 'post') == 0) {
  261. $usePost = true;
  262. unset($options['method']);
  263. }
  264. if ($action == 'delete' || isset($usePost)) {
  265. return $this->Form->postLink($title, $url, $options, $confirmMessage);
  266. }
  267. return $this->Html->link($title, $url, $options, $confirmMessage);
  268. }
  269. /**
  270. * Creates a special type of link for use in admin area.
  271. *
  272. * Clicking the link will automatically check a corresponding checkbox
  273. * where element id is equal to $url parameter and immediately submit the form
  274. * it's on. This works in tandem with Admin.processLink() in javascript.
  275. */
  276. protected function _bulkRowAction($title, $url = null, $options = array(), $confirmMessage = false) {
  277. if (!empty($confirmMessage)) {
  278. $options['data-confirm-message'] = $confirmMessage;
  279. }
  280. if (isset($options['icon'])) {
  281. $options['iconInline'] = false;
  282. }
  283. $output = $this->Html->link($title, $url, $options);
  284. return $output;
  285. }
  286. /**
  287. * Create an action button
  288. */
  289. public function adminAction($title, $url, $options = array()) {
  290. $options = Hash::merge(array(
  291. 'button' => 'default',
  292. 'method' => 'get',
  293. ), $options);
  294. if (strcasecmp($options['method'], 'post') == 0) {
  295. return $this->Html->tag('li',
  296. $this->Form->postLink($title, $url, $options)
  297. );
  298. }
  299. return $this->Html->tag('li',
  300. $this->Html->link($title, $url, $options)
  301. );
  302. }
  303. /**
  304. * Create a tab title/link
  305. */
  306. public function adminTab($title, $url, $options = array()) {
  307. return $this->Html->tag('li',
  308. $this->Html->link($title, $url, Hash::merge(array(
  309. 'data-toggle' => 'tab',
  310. ), $options)
  311. )
  312. );
  313. }
  314. /**
  315. * Show tabs
  316. *
  317. * @return string
  318. */
  319. public function adminTabs($show = null) {
  320. if (!isset($this->adminTabs)) {
  321. $this->adminTabs = false;
  322. }
  323. $output = '';
  324. $tabs = Configure::read('Admin.tabs.' . Inflector::camelize($this->request->params['controller']) . '/' . $this->request->params['action']);
  325. if (is_array($tabs)) {
  326. foreach ($tabs as $title => $tab) {
  327. $tab = Hash::merge(array(
  328. 'options' => array(
  329. 'linkOptions' => array(),
  330. 'elementData' => array(),
  331. 'elementOptions' => array(),
  332. ),
  333. ), $tab);
  334. if (!isset($tab['options']['type']) || (isset($tab['options']['type']) && (in_array($this->_View->viewVars['typeAlias'], $tab['options']['type'])))) {
  335. $domId = strtolower(Inflector::singularize($this->request->params['controller'])) . '-' . strtolower(Inflector::slug($title, '-'));
  336. if ($this->adminTabs) {
  337. list($plugin, $element) = pluginSplit($tab['element']);
  338. $elementOptions = Hash::merge(array(
  339. 'plugin' => $plugin,
  340. ), $tab['options']['elementOptions']);
  341. $output .= '<div id="' . $domId . '" class="tab-pane">';
  342. $output .= $this->_View->element($element, $tab['options']['elementData'], $elementOptions);
  343. $output .= '</div>';
  344. } else {
  345. $output .= $this->adminTab(__d('croogo', $title), '#' . $domId, $tab['options']['linkOptions']);
  346. }
  347. }
  348. }
  349. }
  350. $this->adminTabs = true;
  351. return $output;
  352. }
  353. /**
  354. * Show Boxes
  355. *
  356. * @param array $boxNames
  357. */
  358. public function adminBoxes($boxName = null) {
  359. if (!isset($this->boxAlreadyPrinted)) {
  360. $this->boxAlreadyPrinted = array();
  361. }
  362. $output = '';
  363. $allBoxes = Configure::read('Admin.boxes.' . Inflector::camelize($this->request->params['controller']) . '/' . $this->request->params['action']);
  364. $allBoxes = empty($allBoxes) ? array() : $allBoxes;
  365. $boxNames = array();
  366. if (is_null($boxName)) {
  367. foreach ($allBoxes as $boxName => $value) {
  368. if (!in_array($boxName, $this->boxAlreadyPrinted)) {
  369. $boxNames[$boxName] = $allBoxes[$boxName];
  370. }
  371. }
  372. } elseif (!in_array($boxName, $this->boxAlreadyPrinted)) {
  373. if (array_key_exists($boxName, $allBoxes)) {
  374. $boxNames[$boxName] = $allBoxes[$boxName];
  375. }
  376. }
  377. foreach ($boxNames as $title => $box) {
  378. $box = Hash::merge(array(
  379. 'options' => array(
  380. 'linkOptions' => array(),
  381. 'elementData' => array(),
  382. 'elementOptions' => array(),
  383. ),
  384. ), $box);
  385. $issetType = isset($box['options']['type']);
  386. $typeInTypeAlias = $issetType && in_array($this->_View->viewVars['typeAlias'], $box['options']['type']);
  387. if (!$issetType || $typeInTypeAlias) {
  388. list($plugin, $element) = pluginSplit($box['element']);
  389. $elementOptions = Hash::merge(array(
  390. 'plugin' => $plugin,
  391. ), $box['options']['elementOptions']);
  392. $output .= $this->Html->beginBox($title);
  393. $output .= $this->_View->element(
  394. $element,
  395. $box['options']['elementData'],
  396. $elementOptions
  397. );
  398. $output .= $this->Html->endBox();
  399. $this->boxAlreadyPrinted[] = $title;
  400. }
  401. }
  402. return $output;
  403. }
  404. }