PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_zoo/helpers/html.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 710 lines | 260 code | 105 blank | 345 comment | 20 complexity | c0c694f299274549df75b97da9dbd90c MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /**
  9. * HTML helper class.
  10. *
  11. * @package Component.Helpers
  12. * @since 2.0
  13. */
  14. class HTMLHelper extends AppHelper {
  15. /**
  16. * Wrapper function
  17. *
  18. * @param string $type The function to call
  19. *
  20. * @return string The html output
  21. * @since 2.0
  22. */
  23. public function _($type) {
  24. // get arguments
  25. $args = func_get_args();
  26. // Check to see if we need to load a helper file
  27. $parts = explode('.', $type);
  28. if (count($parts) >= 2) {
  29. $func = array_pop($parts);
  30. $file = array_pop($parts);
  31. if (in_array($file, array('zoo', 'control')) && method_exists($this, $func)) {
  32. array_shift($args);
  33. return call_user_func_array(array($this, $func), $args);
  34. }
  35. }
  36. return call_user_func_array(array('JHTML', '_'), $args);
  37. }
  38. /**
  39. * Get zoo datepicker.
  40. *
  41. * @param string $value The value
  42. * @param string $name The html name
  43. * @param string $id The html id
  44. * @param string|array $attribs The html attributes
  45. * @param boolean $time
  46. *
  47. * @return string datepicker html output
  48. * @since 2.0
  49. */
  50. public function calendar($value, $name, $id, $attribs = null, $time = false) {
  51. if (!defined('ZOO_CALENDAR_SCRIPT_DECLARATION')) {
  52. define('ZOO_CALENDAR_SCRIPT_DECLARATION', true);
  53. $this->app->document->addScript('assets:js/date.js');
  54. $translations = array(
  55. 'closeText' => 'Done',
  56. 'currentText' => 'Today',
  57. 'dayNames' => array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'),
  58. 'dayNamesMin' => array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'),
  59. 'dayNamesShort' => array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'),
  60. 'monthNames' => array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'),
  61. 'monthNamesShort' => array('JANUARY_SHORT', 'FEBRUARY_SHORT', 'MARCH_SHORT', 'APRIL_SHORT', 'MAY_SHORT',
  62. 'JUNE_SHORT', 'JULY_SHORT', 'AUGUST_SHORT', 'SEPTEMBER_SHORT', 'OCTOBER_SHORT', 'NOVEMBER_SHORT', 'DECEMBER_SHORT'),
  63. 'prevText' => 'Prev',
  64. 'nextText' => 'Next',
  65. 'weekHeader' => 'Wk',
  66. 'appendText' => '(yyyy-mm-dd)'
  67. );
  68. foreach ($translations as $key => $translation) {
  69. $translations[$key] = is_array($translation) ? array_map(array('JText', '_'), $translation) : JText::_($translation);
  70. }
  71. $timepicker_translations = array_map(array('JText', '_'), array(
  72. 'currentText' => 'Now',
  73. 'closeText' => 'Done',
  74. 'timeOnlyTitle' => 'Choose Time',
  75. 'timeText' => 'Time',
  76. 'hourText' => 'Hour',
  77. 'minuteText' => 'Minute',
  78. 'secondText' => 'Second'
  79. ));
  80. $javascript = 'jQuery(function($) { $("body").Calendar({ translations: '.json_encode($translations).', timepicker_translations: '.json_encode($timepicker_translations).' }); });';
  81. $this->app->document->addScriptDeclaration($javascript);
  82. }
  83. if (is_array($attribs)) {
  84. $attribs = JArrayHelper::toString($attribs);
  85. }
  86. $data = $time ? ' data-timepicker="timepicker"' : '';
  87. return '<input'.$data.' style="width: 110px" type="text" name="'.$name.'" id="'.$id.'" value="'.htmlspecialchars($value, ENT_COMPAT, 'UTF-8').'" '.$attribs.' />'
  88. .'<img src="'.JURI::root(true).'/templates/system/images/calendar.png'.'" class="zoo-calendar" />';
  89. }
  90. /**
  91. * Get image resource info.
  92. *
  93. * @param string $image the image path
  94. * @param int $width the image width
  95. * @param int $height the image height
  96. *
  97. * @return array|null image info
  98. * @since 2.0
  99. */
  100. public function image($image, $width = null, $height = null) {
  101. $resized_image = $this->app->zoo->resizeImage(JPATH_ROOT.'/'.$image, $width, $height);
  102. $inner_path = $this->app->path->relative($resized_image);
  103. $path = JPATH_ROOT.'/'.$inner_path;
  104. if (is_file($path) && $size = getimagesize($path)) {
  105. $info['path'] = $path;
  106. $info['src'] = JURI::root().$inner_path;
  107. $info['mime'] = $size['mime'];
  108. $info['width'] = $size[0];
  109. $info['height'] = $size[1];
  110. $info['width_height'] = sprintf('width="%d" height="%d"', $info['width'], $info['height']);
  111. return $info;
  112. }
  113. return null;
  114. }
  115. /**
  116. * Returns category select list html string.
  117. *
  118. * @param Application $application The application object
  119. * @param array $options The options
  120. * @param string $name The hmtl name
  121. * @param string|array $attribs The html attributes
  122. * @param string $key
  123. * @param string $text
  124. * @param string $selected The selected value
  125. * @param string $idtag
  126. * @param boolean $translate
  127. * @param string $category The category id to build the select list for
  128. *
  129. * @return string category select list html
  130. * @since 2.0
  131. */
  132. public function categoryList($application, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = NULL, $idtag = false, $translate = false, $category = 0) {
  133. // set options
  134. settype($options, 'array');
  135. reset($options);
  136. // get category tree list
  137. $list = $this->app->tree->buildList($category, $application->getCategoryTree(), array(), '-&nbsp;', '.&nbsp;&nbsp;&nbsp;', '&nbsp;&nbsp;');
  138. // create options
  139. foreach ($list as $category) {
  140. $options[] = $this->_('select.option', $category->id, $category->treename);
  141. }
  142. return $this->_('zoo.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  143. }
  144. /**
  145. * Returns edit row html string.
  146. *
  147. * @param string $name The hmtl name
  148. * @param string $value The hmtl value
  149. *
  150. * @return string the editor row html output
  151. * @since 2.0
  152. */
  153. public function editRow($name, $value) {
  154. $html[] = "\t<tr>\n";
  155. $html[] = "\t\t<td style=\"color:#666666;\">$name</td>\n";
  156. $html[] = "\t\t<td>$value</td>\n";
  157. $html[] = "\t</tr>\n";
  158. return implode("\n", $html);
  159. }
  160. /**
  161. * Returns the country select list
  162. *
  163. * @param array $countries Countries
  164. * @param string $name The hmtl name
  165. * @param string $selected The selected value
  166. * @param boolean $multiselect Show as multiselect
  167. *
  168. * @return string the country select list html output
  169. * @since 2.0
  170. */
  171. public function countrySelectList($countries, $name, $selected, $multiselect) {
  172. $options = array();
  173. if (!$multiselect) {
  174. $options[] = $this->app->html->_('select.option', '', '-'.JText::_('Select Country').'-');
  175. }
  176. foreach ($countries as $key => $country) {
  177. $options[] = $this->app->html->_('select.option', $key, JText::_($country));
  178. }
  179. $attribs = $multiselect ? 'size="'.max(min(count($options), 10), 3).'" multiple="multiple"' : '';
  180. return $this->app->html->_('select.genericlist', $options, $name, $attribs, 'value', 'text', $selected);
  181. }
  182. /**
  183. * Returns layout select list html string.
  184. *
  185. * @param Type $type The type object
  186. * @param string $layout_type The layout type filter
  187. * @param array $options The options
  188. * @param string $name The hmtl name
  189. * @param array|string $attribs The html attributes
  190. * @param string $key
  191. * @param string $text
  192. * @param string $selected The selected value
  193. * @param string $idtag
  194. * @param boolean $translate
  195. *
  196. * @return string the layout select list html output
  197. * @since 2.0
  198. */
  199. public function layoutList($type, $layout_type, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = NULL, $idtag = false, $translate = false) {
  200. // set options
  201. settype($options, 'array');
  202. reset($options);
  203. $layouts = $this->app->type->layouts($type, $layout_type);
  204. foreach ($layouts as $layout => $metadata) {
  205. $options[] = $this->_('select.option', $layout, $metadata->get('name'));
  206. }
  207. return $this->_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  208. }
  209. /**
  210. * Returns type select list html string.
  211. *
  212. * @param Application $application The Application object
  213. * @param array $options The options
  214. * @param string $name The html name
  215. * @param array|string $attribs The html attributes
  216. * @param string $key
  217. * @param string $text
  218. * @param string $selected The selected value
  219. * @param string $idtag
  220. * @param boolean $translate
  221. * @param array $filter The type filter
  222. *
  223. * @return string the type select list html output
  224. * @since 2.0
  225. */
  226. public function typeList($application, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false, $filter = array()) {
  227. // set options
  228. settype($options, 'array');
  229. reset($options);
  230. foreach ($application->getTypes() as $type) {
  231. if (empty($filter) || in_array($type->id, $filter)) {
  232. $options[] = $this->_('select.option', $type->id, JText::_($type->name));
  233. }
  234. }
  235. return $this->_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  236. }
  237. /**
  238. * Returns module select list html string.
  239. *
  240. * @param array $options The options
  241. * @param string $name The html name
  242. * @param array|string $attribs The html attributes
  243. * @param string $key
  244. * @param string $text
  245. * @param string $selected The selected value
  246. * @param string $idtag
  247. * @param boolean $translate
  248. *
  249. * @return string the module select list html output
  250. * @since 2.0
  251. */
  252. public function moduleList($options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  253. // set options
  254. settype($options, 'array');
  255. reset($options);
  256. // get modules
  257. $modules = $this->app->module->load();
  258. if (count($modules)) {
  259. foreach ($modules as $module) {
  260. $options[] = $this->app->html->_('select.option', $module->id, $module->title.' ('.$module->position.')');
  261. }
  262. return $this->_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  263. }
  264. return JText::_("There are no modules to choose from.");
  265. }
  266. /**
  267. * Returns plugin select list html string.
  268. *
  269. * @param array $options The options
  270. * @param string $name The html name
  271. * @param array|string $attribs The html attributes
  272. * @param string $key
  273. * @param string $text
  274. * @param string $selected The selected value
  275. * @param string $idtag
  276. * @param boolean $translate
  277. * @param string $folder The folder path to filter plugins by
  278. * @param boolean $enabled Show selected plugins only
  279. *
  280. * @return string the plugin select list html output
  281. * @since 2.0
  282. */
  283. public function pluginList($options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false, $folder = false, $enabled = true) {
  284. // set options
  285. settype($options, 'array');
  286. reset($options);
  287. $plugins = JPluginHelper::getPlugin($folder);
  288. if (count($plugins)) {
  289. foreach ($plugins as $plugin) {
  290. if ($enabled && JPluginHelper::isEnabled($plugin->type, $plugin->name)) {
  291. $plugin_name = 'plg_'.$plugin->type.'_'.$plugin->name;
  292. JFactory::getLanguage()->load($plugin_name.'.sys', JPATH_ADMINISTRATOR);
  293. $options[] = $this->app->html->_('select.option', $plugin->name, $plugin_name);
  294. }
  295. }
  296. return $this->_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  297. }
  298. return JText::_("There are no plugins to choose from.");
  299. }
  300. /**
  301. * Returns author select list html string.
  302. *
  303. * @param array $options The options
  304. * @param string $name The html name
  305. * @param array|string $attribs The html attributes
  306. * @param string $key
  307. * @param string $text
  308. * @param string $selected The selected value
  309. * @param string $idtag
  310. * @param boolean $translate
  311. * @param boolean $show_registered_users Show registered users only
  312. *
  313. * @return string the author select list html output
  314. * @since 2.0
  315. */
  316. public function authorList($options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false, $show_registered_users = true) {
  317. $query = 'SELECT DISTINCT u.id AS value, u.name AS text'
  318. .' FROM #__users AS u'
  319. .' WHERE u.block = 0'
  320. .($show_registered_users ? '' : ' AND u.gid > 18');
  321. return $this->queryList($query, $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  322. }
  323. /**
  324. * Returns item author select list html string.
  325. *
  326. * @param array $options The options
  327. * @param string $name The html name
  328. * @param array|string $attribs The html attributes
  329. * @param string $key
  330. * @param string $text
  331. * @param string $selected The selected value
  332. * @param string $idtag
  333. * @param boolean $translate
  334. *
  335. * @return string the item author select list html output
  336. * @since 2.0
  337. */
  338. public function itemAuthorList($options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  339. $query = 'SELECT DISTINCT u.id AS value, u.name AS text'
  340. .' FROM '.ZOO_TABLE_ITEM.' AS i'
  341. .' JOIN #__users AS u ON i.created_by = u.id';
  342. return $this->queryList($query, $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  343. }
  344. /**
  345. * Returns item select list html string.
  346. *
  347. * @param Application $application The Application object
  348. * @param array $options The options
  349. * @param string $name The html name
  350. * @param array|string $attribs The html attributes
  351. * @param string $key
  352. * @param string $text
  353. * @param string $selected The selected value
  354. * @param string $idtag
  355. * @param boolean $translate
  356. *
  357. * @return string the item select list html output
  358. * @since 2.0
  359. */
  360. public function itemList($application, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  361. // set options
  362. settype($options, 'array');
  363. reset($options);
  364. $query = 'SELECT DISTINCT c.item_id as value, a.name as text'
  365. .' FROM '.ZOO_TABLE_COMMENT.' AS c'
  366. .' LEFT JOIN '.ZOO_TABLE_ITEM.' AS a ON c.item_id = a.id'
  367. .' WHERE a.application_id = '.(int) $application->id
  368. .' ORDER BY a.name';
  369. $rows = $this->app->database->queryAssocList($query);
  370. foreach ($rows as $row) {
  371. $options[] = $this->_('select.option', $row['value'], $row['text']);
  372. }
  373. return $this->_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  374. }
  375. /**
  376. * Returns user access select list.
  377. *
  378. * @param array $options The options
  379. * @param string $name The html name
  380. * @param array|string $attribs The html attributes
  381. * @param string $key
  382. * @param string $text
  383. * @param string $selected The selected value
  384. * @param string $idtag
  385. * @param boolean $translate
  386. * @param array $exclude Access level ids to exclude
  387. *
  388. * @return string the user access list html output
  389. * @since 2.0
  390. */
  391. public function accessLevel($options, $name, $attribs = 'class="inputbox"', $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false, $exclude = array()) {
  392. // set options
  393. settype($options, 'array');
  394. reset($options);
  395. // set exclude
  396. $exclude = (array) $exclude;
  397. reset($exclude);
  398. $groups = $this->app->zoo->getGroups();
  399. foreach ($exclude as $key) {
  400. unset($groups[$key]);
  401. }
  402. foreach ($groups as $group) {
  403. $options[] = $this->_('select.option', $group->id, JText::_($group->name), $key, $text);
  404. }
  405. if (!isset($groups[$selected])) {
  406. $selected = $this->app->joomla->getDefaultAccess();
  407. }
  408. return $this->_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  409. }
  410. /**
  411. * Returns comment author select list html string.
  412. *
  413. * @param Application $application The Application object
  414. * @param array $options The options
  415. * @param string $name The html name
  416. * @param array|string $attribs The html attributes
  417. * @param string $key
  418. * @param string $text
  419. * @param string $selected The selected value
  420. * @param string $idtag
  421. * @param boolean $translate
  422. *
  423. * @return string the comment author select list html output
  424. * @since 2.0
  425. */
  426. public function commentAuthorList($application, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  427. $query = "SELECT DISTINCT c.author AS value, c.author AS text"
  428. ." FROM ".ZOO_TABLE_COMMENT." AS c"
  429. .' LEFT JOIN '.ZOO_TABLE_ITEM.' AS a ON c.item_id = a.id'
  430. ." WHERE c.author <> ''"
  431. ." AND a.application_id = ".$application->id
  432. ." ORDER BY c.author";
  433. return $this->queryList($query, $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  434. }
  435. /**
  436. * Returns select list html string.
  437. *
  438. * @param string $query The database query
  439. * @param array $options The options
  440. * @param string $name The html name
  441. * @param array|string $attribs The html attributes
  442. * @param string $key
  443. * @param string $text
  444. * @param string $selected The selected value
  445. * @param string $idtag
  446. * @param boolean $translate
  447. *
  448. * @return string select list html output
  449. * @since 2.0
  450. */
  451. public function queryList($query, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  452. // set options
  453. settype($options, 'array');
  454. reset($options);
  455. $options = array_merge($options, $this->app->database->queryObjectList($query));
  456. return $this->_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  457. }
  458. /**
  459. * Wrapper for Joomla 1.5/1.6
  460. *
  461. * @param array $data
  462. * @param string $name The html name
  463. * @param array|string $attribs The html attributes
  464. * @param string $optKey
  465. * @param string $optText
  466. * @param string $selected The selected value
  467. * @param string $idtag
  468. * @param boolean $translate
  469. *
  470. * @return string generic list html output
  471. * @since 2.0
  472. */
  473. public function genericList($data, $name, $attribs = null, $optKey = 'value', $optText = 'text', $selected = null, $idtag = false, $translate = false) {
  474. if ($this->app->joomla->isVersion('1.5')) {
  475. return $this->app->html->_('select.genericlist', $data, $name, $attribs, $optKey, $optText, $selected);
  476. } else {
  477. $attributes['list.attr'] = $attribs;
  478. $attributes['id'] = $idtag;
  479. $attributes['list.translate'] = $translate;
  480. $attributes['option.key'] = $optKey;
  481. $attributes['option.text'] = $optText;
  482. $attributes['list.select'] = $selected;
  483. $attributes['option.text.toHtml'] = false;
  484. return $this->app->html->_('select.genericlist', $data, $name, $attributes);
  485. }
  486. }
  487. //******************************
  488. //* ZOO Controls *
  489. //******************************
  490. /**
  491. * Get array list
  492. *
  493. * @param array $array
  494. * @param array $options
  495. * @param string $name The html name
  496. * @param array|string $attribs The html attributes
  497. * @param string $key
  498. * @param string $text
  499. * @param string $selected The selected value
  500. * @param string $idtag
  501. * @param boolean $translate If the text should be translated.
  502. *
  503. * @return string array list html output
  504. *
  505. * @return type
  506. */
  507. public function arrayList($array, $options, $name, $attribs = null, $key = 'value', $text = 'text', $selected = null, $idtag = false, $translate = false) {
  508. // set options
  509. settype($options, 'array');
  510. reset($options);
  511. $options = array_merge($options, $this->listOptions($array));
  512. return $this->_('select.genericlist', $options, $name, $attribs, $key, $text, $selected, $idtag, $translate);
  513. }
  514. /**
  515. * Returns select option as JHTML compatible array.
  516. *
  517. * @param array $array
  518. * @param string $value
  519. * @param string $text
  520. *
  521. * @return array
  522. * @since 2.0
  523. */
  524. public function listOptions($array, $value = 'value', $text = 'text') {
  525. $options = array();
  526. if (is_array($array)) {
  527. foreach ($array as $val => $txt) {
  528. $options[] = $this->_('select.option', strval($val), $txt, $value, $text);
  529. }
  530. }
  531. return $options;
  532. }
  533. /**
  534. * Returns directory select html string.
  535. *
  536. * @param string $directory
  537. * @param string $filter
  538. * @param string $name
  539. * @param string $value
  540. * @param array|string $attribs
  541. *
  542. * @return string directory select list html output
  543. * @since 2.0
  544. */
  545. public function selectdirectory($directory, $filter, $name, $value = null, $attribs = null) {
  546. // get directories
  547. $options = array($this->app->html->_('select.option', '', '- '.JText::_('Select Directory').' -'));
  548. $dirs = $this->app->path->dirs($directory, true, $filter);
  549. natsort($dirs);
  550. foreach ($dirs as $dir) {
  551. $options[] = $this->app->html->_('select.option', $dir, $dir);
  552. }
  553. return $this->app->html->_('select.genericlist', $options, $name, $attribs, 'value', 'text', $value);
  554. }
  555. /**
  556. * Returns form textarea html string.
  557. *
  558. * @param string $name
  559. * @param string $value
  560. * @param array|string $attribs
  561. *
  562. * @return string form textarea html output
  563. * @since 2.0
  564. */
  565. public function textarea($name, $value = null, $attribs = null) {
  566. if (is_array($attribs)) {
  567. $attribs = JArrayHelper::toString($attribs);
  568. }
  569. $value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  570. // convert <br /> tags so they are not visible when editing
  571. $value = str_replace('<br />', "\n", $value);
  572. return "\n\t<textarea name=\"$name\" $attribs />".$value."</textarea>\n";
  573. }
  574. /**
  575. * Returns form text input html string.
  576. *
  577. * @param string $name
  578. * @param string $value
  579. * @param array|string $attribs
  580. *
  581. * @return string form text input html output
  582. * @since 2.0
  583. */
  584. public function text($name, $value = null, $attribs = null) {
  585. $value = htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
  586. return $this->input('text', $name, $value, $attribs);
  587. }
  588. /**
  589. * Returns form input html string.
  590. *
  591. * @param string $type
  592. * @param string $name
  593. * @param string $value
  594. * @param array|string $attribs
  595. *
  596. * @return string form input html output
  597. * @since 2.0
  598. */
  599. public function input($type, $name, $value = null, $attribs = null) {
  600. if (is_array($attribs)) {
  601. $attribs = JArrayHelper::toString($attribs);
  602. }
  603. return "\n\t<input type=\"$type\" name=\"$name\" value=\"$value\" $attribs />\n";
  604. }
  605. }