PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/html/html/access.php

https://bitbucket.org/gnomeontherun/square-one
PHP | 322 lines | 174 code | 40 blank | 108 comment | 15 complexity | b64ce672f439527f1a0a34360d5f16d8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Extended Utility class for all HTML drawing classes.
  12. *
  13. * @package Joomla.Platform
  14. * @subpackage HTML
  15. * @since 11.1
  16. */
  17. abstract class JHtmlAccess
  18. {
  19. /**
  20. * A cached array of the asset groups
  21. *
  22. * @var array
  23. * @since 11.1
  24. */
  25. protected static $asset_groups = null;
  26. /**
  27. * Displays a list of the available access view levels
  28. *
  29. * @param string $name The form field name.
  30. * @param string $selected The name of the selected section.
  31. * @param string $attribs Additional attributes to add to the select field.
  32. * @param mixed $params True to add "All Sections" option or and array of options
  33. * @param string $id The form field id
  34. *
  35. * @return string The required HTML for the SELECT tag.
  36. *
  37. * @since 11.1
  38. *
  39. * @see JFormFieldAccessLevel
  40. */
  41. public static function level($name, $selected, $attribs = '', $params = true, $id = false)
  42. {
  43. $db = JFactory::getDbo();
  44. $query = $db->getQuery(true);
  45. $query->select('a.id AS value, a.title AS text');
  46. $query->from('#__viewlevels AS a');
  47. $query->group('a.id, a.title, a.ordering');
  48. $query->order('a.ordering ASC');
  49. $query->order($query->qn('title') . ' ASC');
  50. // Get the options.
  51. $db->setQuery($query);
  52. $options = $db->loadObjectList();
  53. // Check for a database error.
  54. if ($db->getErrorNum())
  55. {
  56. JError::raiseWarning(500, $db->getErrorMsg());
  57. return null;
  58. }
  59. // If params is an array, push these options to the array
  60. if (is_array($params))
  61. {
  62. $options = array_merge($params, $options);
  63. }
  64. // If all levels is allowed, push it into the array.
  65. elseif ($params)
  66. {
  67. array_unshift($options, JHtml::_('select.option', '', JText::_('JOPTION_ACCESS_SHOW_ALL_LEVELS')));
  68. }
  69. return JHtml::_(
  70. 'select.genericlist',
  71. $options,
  72. $name,
  73. array(
  74. 'list.attr' => $attribs,
  75. 'list.select' => $selected,
  76. 'id' => $id
  77. )
  78. );
  79. }
  80. /**
  81. * Displays a list of the available user groups.
  82. *
  83. * @param string $name The form field name.
  84. * @param string $selected The name of the selected section.
  85. * @param string $attribs Additional attributes to add to the select field.
  86. * @param boolean $allowAll True to add "All Groups" option.
  87. *
  88. * @return string The required HTML for the SELECT tag.
  89. *
  90. * @see JFormFieldUsergroup
  91. *
  92. * @since 11.1
  93. */
  94. public static function usergroup($name, $selected, $attribs = '', $allowAll = true)
  95. {
  96. $db = JFactory::getDbo();
  97. $query = $db->getQuery(true);
  98. $query->select('a.id AS value, a.title AS text, COUNT(DISTINCT b.id) AS level');
  99. $query->from($db->quoteName('#__usergroups') . ' AS a');
  100. $query->join('LEFT', $db->quoteName('#__usergroups') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
  101. $query->group('a.id, a.title, a.lft, a.rgt');
  102. $query->order('a.lft ASC');
  103. $db->setQuery($query);
  104. $options = $db->loadObjectList();
  105. // Check for a database error.
  106. if ($db->getErrorNum())
  107. {
  108. JError::raiseNotice(500, $db->getErrorMsg());
  109. return null;
  110. }
  111. for ($i = 0, $n = count($options); $i < $n; $i++)
  112. {
  113. $options[$i]->text = str_repeat('- ', $options[$i]->level) . $options[$i]->text;
  114. }
  115. // If all usergroups is allowed, push it into the array.
  116. if ($allowAll)
  117. {
  118. array_unshift($options, JHtml::_('select.option', '', JText::_('JOPTION_ACCESS_SHOW_ALL_GROUPS')));
  119. }
  120. return JHtml::_('select.genericlist', $options, $name, array('list.attr' => $attribs, 'list.select' => $selected));
  121. }
  122. /**
  123. * Returns a UL list of user groups with check boxes
  124. *
  125. * @param string $name The name of the checkbox controls array
  126. * @param array $selected An array of the checked boxes
  127. * @param boolean $checkSuperAdmin If false only super admins can add to super admin groups
  128. *
  129. * @return string
  130. *
  131. * @since 11.1
  132. */
  133. public static function usergroups($name, $selected, $checkSuperAdmin = false)
  134. {
  135. static $count;
  136. $count++;
  137. $isSuperAdmin = JFactory::getUser()->authorise('core.admin');
  138. $db = JFactory::getDbo();
  139. $query = $db->getQuery(true);
  140. $query->select('a.*, COUNT(DISTINCT b.id) AS level');
  141. $query->from($db->quoteName('#__usergroups') . ' AS a');
  142. $query->join('LEFT', $db->quoteName('#__usergroups') . ' AS b ON a.lft > b.lft AND a.rgt < b.rgt');
  143. $query->group('a.id, a.title, a.lft, a.rgt, a.parent_id');
  144. $query->order('a.lft ASC');
  145. $db->setQuery($query);
  146. $groups = $db->loadObjectList();
  147. // Check for a database error.
  148. if ($db->getErrorNum())
  149. {
  150. JError::raiseNotice(500, $db->getErrorMsg());
  151. return null;
  152. }
  153. $html = array();
  154. $html[] = '<ul class="checklist usergroups">';
  155. for ($i = 0, $n = count($groups); $i < $n; $i++)
  156. {
  157. $item = &$groups[$i];
  158. // If checkSuperAdmin is true, only add item if the user is superadmin or the group is not super admin
  159. if ((!$checkSuperAdmin) || $isSuperAdmin || (!JAccess::checkGroup($item->id, 'core.admin')))
  160. {
  161. // Setup the variable attributes.
  162. $eid = $count . 'group_' . $item->id;
  163. // Don't call in_array unless something is selected
  164. $checked = '';
  165. if ($selected)
  166. {
  167. $checked = in_array($item->id, $selected) ? ' checked="checked"' : '';
  168. }
  169. $rel = ($item->parent_id > 0) ? ' rel="' . $count . 'group_' . $item->parent_id . '"' : '';
  170. // Build the HTML for the item.
  171. $html[] = ' <li>';
  172. $html[] = ' <input type="checkbox" name="' . $name . '[]" value="' . $item->id . '" id="' . $eid . '"';
  173. $html[] = ' ' . $checked . $rel . ' />';
  174. $html[] = ' <label for="' . $eid . '">';
  175. $html[] = ' ' . str_repeat('<span class="gi">|&mdash;</span>', $item->level) . $item->title;
  176. $html[] = ' </label>';
  177. $html[] = ' </li>';
  178. }
  179. }
  180. $html[] = '</ul>';
  181. return implode("\n", $html);
  182. }
  183. /**
  184. * Returns a UL list of actions with check boxes
  185. *
  186. * @param string $name The name of the checkbox controls array
  187. * @param array $selected An array of the checked boxes
  188. * @param string $component The component the permissions apply to
  189. * @param string $section The section (within a component) the permissions apply to
  190. *
  191. * @return string
  192. *
  193. * @see JAccess
  194. * @since 11.1
  195. */
  196. public static function actions($name, $selected, $component, $section = 'global')
  197. {
  198. static $count;
  199. $count++;
  200. $actions = JAccess::getActions($component, $section);
  201. $html = array();
  202. $html[] = '<ul class="checklist access-actions">';
  203. for ($i = 0, $n = count($actions); $i < $n; $i++)
  204. {
  205. $item = &$actions[$i];
  206. // Setup the variable attributes.
  207. $eid = $count . 'action_' . $item->id;
  208. $checked = in_array($item->id, $selected) ? ' checked="checked"' : '';
  209. // Build the HTML for the item.
  210. $html[] = ' <li>';
  211. $html[] = ' <input type="checkbox" name="' . $name . '[]" value="' . $item->id . '" id="' . $eid . '"';
  212. $html[] = ' ' . $checked . ' />';
  213. $html[] = ' <label for="' . $eid . '">';
  214. $html[] = ' ' . JText::_($item->title);
  215. $html[] = ' </label>';
  216. $html[] = ' </li>';
  217. }
  218. $html[] = '</ul>';
  219. return implode("\n", $html);
  220. }
  221. /**
  222. * Gets a list of the asset groups as an array of JHtml compatible options.
  223. *
  224. * @param array $config An array of options for the options
  225. *
  226. * @return mixed An array or false if an error occurs
  227. *
  228. * @since 11.1
  229. */
  230. public static function assetgroups($config = array())
  231. {
  232. if (empty(JHtmlAccess::$asset_groups))
  233. {
  234. $db = JFactory::getDbo();
  235. $query = $db->getQuery(true);
  236. $query->select('a.id AS value, a.title AS text');
  237. $query->from($db->quoteName('#__viewlevels') . ' AS a');
  238. $query->group('a.id, a.title, a.ordering');
  239. $query->order('a.ordering ASC');
  240. $db->setQuery($query);
  241. JHtmlAccess::$asset_groups = $db->loadObjectList();
  242. // Check for a database error.
  243. if ($db->getErrorNum())
  244. {
  245. JError::raiseNotice(500, $db->getErrorMsg());
  246. return false;
  247. }
  248. }
  249. return JHtmlAccess::$asset_groups;
  250. }
  251. /**
  252. * Displays a Select list of the available asset groups
  253. *
  254. * @param string $name The name of the select element
  255. * @param mixed $selected The selected asset group id
  256. * @param string $attribs Optional attributes for the select field
  257. * @param array $config An array of options for the control
  258. *
  259. * @return mixed An HTML string or null if an error occurs
  260. *
  261. * @since 11.1
  262. */
  263. public static function assetgrouplist($name, $selected, $attribs = null, $config = array())
  264. {
  265. static $count;
  266. $options = JHtmlAccess::assetgroups();
  267. if (isset($config['title']))
  268. {
  269. array_unshift($options, JHtml::_('select.option', '', $config['title']));
  270. }
  271. return JHtml::_(
  272. 'select.genericlist',
  273. $options,
  274. $name,
  275. array(
  276. 'id' => isset($config['id']) ? $config['id'] : 'assetgroups_' . ++$count,
  277. 'list.attr' => (is_null($attribs) ? 'class="inputbox" size="3"' : $attribs),
  278. 'list.select' => (int) $selected
  279. )
  280. );
  281. }
  282. }