PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/html/html/list.php

https://bitbucket.org/joomla/joomla-platform/
PHP | 272 lines | 189 code | 26 blank | 57 comment | 26 complexity | 33ff824ef865d36c4c003cc522d46416 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage HTML
  5. *
  6. * @copyright Copyright (C) 2005 - 2011 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. * Utility class for creating different select lists
  12. *
  13. * @static
  14. * @package Joomla.Platform
  15. * @subpackage HTML
  16. * @since 11.1
  17. */
  18. abstract class JHtmlList
  19. {
  20. /**
  21. * Use JHtml::_('access.assetgrouplist', 'access', $selected) instead
  22. * @deprecated
  23. */
  24. public static function accesslevel(&$row)
  25. {
  26. return JHtml::_('access.assetgrouplist', 'access', $row->access);
  27. }
  28. /**
  29. * Build the select list to choose an image
  30. */
  31. public static function images($name, $active = NULL, $javascript = NULL, $directory = NULL, $extensions = "bmp|gif|jpg|png")
  32. {
  33. if (!$directory) {
  34. $directory = '/images/';
  35. }
  36. if (!$javascript) {
  37. $javascript = "onchange=\"javascript:if (document.forms.adminForm." . $name . ".options[selectedIndex].value!='') {document.imagelib.src='..$directory' + document.forms.adminForm." . $name . ".options[selectedIndex].value} else {document.imagelib.src='templates/bluestork/images/admin/blank.png'}\"";
  38. }
  39. jimport('joomla.filesystem.folder');
  40. $imageFiles = JFolder::files(JPATH_SITE.DS.$directory);
  41. $images = array(JHtml::_('select.option', '', JText::_('JOPTION_SELECT_IMAGE')));
  42. foreach ($imageFiles as $file) {
  43. if (preg_match('#('.$extensions.')$#', $file)) {
  44. $images[] = JHtml::_('select.option', $file);
  45. }
  46. }
  47. $images = JHtml::_(
  48. 'select.genericlist',
  49. $images,
  50. $name,
  51. array(
  52. 'list.attr' => 'class="inputbox" size="1" '. $javascript,
  53. 'list.select' => $active
  54. )
  55. );
  56. return $images;
  57. }
  58. /**
  59. * Returns an array of options
  60. *
  61. * @param string $sql SQL with ordering As value and 'name field' AS text
  62. * @param integer $chop The length of the truncated headline
  63. *
  64. * @return array An array of objects formatted for JHtml list processing
  65. * @since 11.1
  66. */
  67. public static function genericordering($sql, $chop = '30')
  68. {
  69. $db = JFactory::getDbo();
  70. $options = array();
  71. $db->setQuery($sql);
  72. $items = $db->loadObjectList();
  73. // Check for a database error.
  74. if ($db->getErrorNum()) {
  75. JError::raiseNotice(500, $db->getErrorMsg());
  76. return false;
  77. }
  78. if (empty($items)) {
  79. $options[] = JHtml::_('select.option', 1, JText::_('JOPTION_ORDER_FIRST'));
  80. return $options;
  81. }
  82. $options[] = JHtml::_('select.option', 0, '0 '. JText::_('JOPTION_ORDER_FIRST'));
  83. for ($i=0, $n=count($items); $i < $n; $i++)
  84. {
  85. $items[$i]->text = JText::_($items[$i]->text);
  86. if (JString::strlen($items[$i]->text) > $chop) {
  87. $text = JString::substr($items[$i]->text,0,$chop)."...";
  88. } else {
  89. $text = $items[$i]->text;
  90. }
  91. $options[] = JHtml::_('select.option', $items[$i]->value, $items[$i]->value.'. '.$text);
  92. }
  93. $options[] = JHtml::_('select.option', $items[$i-1]->value+1, ($items[$i-1]->value+1).' '. JText::_('JOPTION_ORDER_LAST'));
  94. return $options;
  95. }
  96. /**
  97. * @deprecated 1.6 Use JHtml::_('list.ordering') instead
  98. */
  99. public static function specificordering($value, $id, $query, $neworder = 0)
  100. {
  101. if (is_object($value)) {
  102. $value = $value->ordering;
  103. }
  104. if ($id) {
  105. $neworder = 0;
  106. } else {
  107. if ($neworder) {
  108. $neworder = 1;
  109. } else {
  110. $neworder = -1;
  111. }
  112. }
  113. return JHtmlList::ordering('ordering', $query, '', $value, $neworder);
  114. }
  115. /**
  116. * Build the select list for Ordering derived from a query
  117. *
  118. * @param int $value The scalar value
  119. * @param string $query
  120. * @param string $attribs HTML tag attributes
  121. * @param int $neworder 1 if new and first, -1 if new and last, 0 or null if existing item
  122. * @param string $prefix An optional prefix for the task
  123. *
  124. * @return string
  125. * @since 11.1
  126. */
  127. public static function ordering($name, $query, $attribs = null, $selected = null, $neworder = null, $chop = null)
  128. {
  129. if (empty($attribs)) {
  130. $attribs = 'class="inputbox" size="1"';
  131. }
  132. if (empty($neworder))
  133. {
  134. $orders = JHtml::_('list.genericordering', $query);
  135. $html = JHtml::_(
  136. 'select.genericlist',
  137. $orders,
  138. $name,
  139. array('list.attr' => $attribs, 'list.select' => (int) $selected)
  140. );
  141. }
  142. else
  143. {
  144. if ($neworder > 0) {
  145. $text = JText::_('JGLOBAL_NEWITEMSLAST_DESC');
  146. }
  147. else if ($neworder <= 0) {
  148. $text = JText::_('JGLOBAL_NEWITEMSFIRST_DESC');
  149. }
  150. $html = '<input type="hidden" name="'.$name.'" value="'. (int) $selected .'" />'. '<span class="readonly">' . $text . '</span>';
  151. }
  152. return $html;
  153. }
  154. /**
  155. * Select list of active users
  156. */
  157. public static function users($name, $active, $nouser = 0, $javascript = NULL, $order = 'name', $reg = 1)
  158. {
  159. $db = JFactory::getDbo();
  160. $and = '';
  161. if ($reg) {
  162. // Does not include registered users in the list
  163. $and = ' AND m.group_id != 2';
  164. }
  165. $query = 'SELECT u.id AS value, u.name AS text'
  166. . ' FROM #__users AS u'
  167. . ' JOIN #__user_usergroup_map AS m ON m.user_id = u.id'
  168. . ' WHERE u.block = 0'
  169. . $and
  170. . ' ORDER BY '. $order
  171. ;
  172. $db->setQuery($query);
  173. if ($nouser) {
  174. $users[] = JHtml::_('select.option', '0', JText::_('JOPTION_NO_USER'));
  175. $users = array_merge($users, $db->loadObjectList());
  176. } else {
  177. $users = $db->loadObjectList();
  178. }
  179. $users = JHtml::_(
  180. 'select.genericlist',
  181. $users,
  182. $name,
  183. array('list.attr' => 'class="inputbox" size="1" '. $javascript, 'list.select' => $active)
  184. );
  185. return $users;
  186. }
  187. /**
  188. * Select list of positions - generally used for location of images
  189. */
  190. public static function positions(
  191. $name,
  192. $active = null,
  193. $javascript = null,
  194. $none = 1,
  195. $center = 1,
  196. $left = 1,
  197. $right = 1,
  198. $id = false
  199. )
  200. {
  201. $pos = array();
  202. if ($none) {
  203. $pos[''] = JText::_('JNONE');
  204. }
  205. if ($center) {
  206. $pos['center'] = JText::_('JGLOBAL_CENTER');
  207. }
  208. if ($left) {
  209. $pos['left'] = JText::_('JGLOBAL_LEFT');
  210. }
  211. if ($right) {
  212. $pos['right'] = JText::_('JGLOBAL_RIGHT');
  213. }
  214. $positions = JHtml::_(
  215. 'select.genericlist',
  216. $pos,
  217. $name,
  218. array(
  219. 'id' => $id,
  220. 'list.attr' => 'class="inputbox" size="1"'. $javascript,
  221. 'list.select' => $active,
  222. 'option.key' => null,
  223. )
  224. );
  225. return $positions;
  226. }
  227. /**
  228. * @deprecated
  229. */
  230. public static function category($name, $extension, $selected = NULL, $javascript = NULL, $order = null, $size = 1, $sel_cat = 1)
  231. {
  232. $categories = JHtml::_('category.options', $extension);
  233. if ($sel_cat) {
  234. array_unshift($categories, JHTML::_('select.option', '0', JText::_('JOPTION_SELECT_CATEGORY')));
  235. }
  236. $category = JHTML::_(
  237. 'select.genericlist',
  238. $categories,
  239. $name,
  240. 'class="inputbox" size="'. $size .'" '. $javascript,
  241. 'value', 'text',
  242. $selected
  243. );
  244. return $category;
  245. }
  246. }