PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/legacy/util/HtmlUtil.php

https://github.com/antoniom/core
PHP | 960 lines | 502 code | 119 blank | 339 comment | 86 complexity | 4bda81e7dfeb6d57945093d7326373e1 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT
  1. <?php
  2. /**
  3. * Copyright Zikula Foundation 2009 - Zikula Application Framework
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Util
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. /**
  15. * HTMLUtil is a class used to generate specific HTML code.
  16. */
  17. class HtmlUtil
  18. {
  19. const MARKER_NONE = '&nbsp;&nbsp;';
  20. const REQUIRED_MARKER = '<span class="z-form-mandatory-flag">*</span>';
  21. const VALIDATION_MARKER = '<span class="z-form-mandatory-flag">!</span>';
  22. /**
  23. * Return the HTML code for the specified date selector input box.
  24. *
  25. * @param string $objectname The name of the object the field will be placed in.
  26. * @param string $htmlname The html fieldname under which the date value will be submitted.
  27. * @param string $dateFormat The dateformat to use for displaying the chosen date.
  28. * @param string $defaultString The String to display before a value has been selected.
  29. * @param string $defaultDate The Date the calendar should to default to.
  30. *
  31. * @return The resulting HTML string.
  32. */
  33. public static function buildCalendarInputBox($objectname, $htmlname, $dateFormat, $defaultString = '', $defaultDate = '')
  34. {
  35. $html = '';
  36. if (!$htmlname) {
  37. return z_exit(__f('%1$s: Missing %2$s.', array('HtmlUtil::buildCalendarInputBox', 'htmlname')));
  38. }
  39. if (!$dateFormat) {
  40. return z_exit(__f('%1$s: Missing %2$s.', array('HtmlUtil::buildCalendarInputBox', 'dateFormat')));
  41. }
  42. $fieldKey = $htmlname;
  43. if ($objectname) {
  44. $fieldKey = $objectname . '[' . $htmlname . ']';
  45. }
  46. $triggerName = 'trigger_' . $htmlname;
  47. $displayName = 'display_' . $htmlname;
  48. //$daFormat = preg_replace ('/([a-z|A-Z])/', '%$1', $dateFormat); // replace 'x' -> '%x'
  49. $html .= '<span id="' . DataUtil::formatForDisplay($displayName) . '">' . DataUtil::formatForDisplay($defaultString) . '</span>';
  50. $html .= '&nbsp;';
  51. $html .= '<input type="hidden" name="' . DataUtil::formatForDisplay($fieldKey) . '" id="' . DataUtil::formatForDisplay($htmlname) . '" value="' . DataUtil::formatForDisplay($defaultDate) . '" />';
  52. $html .= '<img src="javascript/jscalendar/img.gif" id="' . DataUtil::formatForDisplay($triggerName) . '" style="cursor: pointer; border: 0px solid blue;" title="Date selector" alt="Date selector" onmouseover="this.style.background=\'blue\';" onmouseout="this.style.background=\'\'" />';
  53. $html .= '<script type="text/javascript"> Calendar.setup({';
  54. $html .= 'ifFormat : "%Y-%m-%d %H:%M:00",'; // universal format, don't change this!
  55. $html .= 'inputField : "' . DataUtil::formatForDisplay($htmlname) . '",';
  56. $html .= 'displayArea : "' . DataUtil::formatForDisplay($displayName) . '",';
  57. $html .= 'daFormat : "' . DataUtil::formatForDisplay($dateFormat) . '",';
  58. $html .= 'button : "' . DataUtil::formatForDisplay($triggerName) . '",';
  59. $html .= 'align : "Tl",';
  60. if ($defaultDate) {
  61. $d = strtotime($defaultDate);
  62. $d = date('Y/m/d', $d);
  63. $html .= 'date : "' . $d . '",';
  64. }
  65. $html .= 'singleClick : true }); </script>';
  66. return $html;
  67. }
  68. /**
  69. * Return the HTML for a generic selector.
  70. *
  71. * @param string $name The name of the generated selector (default='countries') (optional).
  72. * @param array $data The data to build the selector from (default='array()') (optional).
  73. * @param string $selectedValue The value which is currently selected (default='') (optional).
  74. * @param string $defaultValue The default value to select (default='') (optional).
  75. * @param string $defaultText The text for the default value (default='') (optional).
  76. * @param string $allValue The value to assign for the "All" choice (optional) (default=0).
  77. * @param string $allText The text to display for the "All" choice (optional) (default='').
  78. * @param boolean $submit Whether or not to auto-submit the selector.
  79. * @param boolean $disabled Whether or not to disable selector (optional) (default=false).
  80. * @param integer $multipleSize The size to use for a multiple selector, 1 produces a normal/single selector (optional (default=1).
  81. *
  82. * @return The generated HTML for the selector.
  83. */
  84. public static function getSelector_Generic($name = 'genericSelector', $data = array(), $selectedValue = null, $defaultValue = null, $defaultText = null, $allValue = null, $allText = null, $submit = false, $disabled = false, $multipleSize = 1)
  85. {
  86. if (!$name) {
  87. return LogUtil::registerError(__f('Invalid %1$s [%2$s] passed to %3$s.', array('name', $name, 'HtmlUtil::getSelector_Generic')));
  88. }
  89. $id = strtr($name, '[]', '__');
  90. $disabled = $disabled ? 'disabled="disabled"' : '';
  91. $multiple = $multipleSize > 1 ? 'multiple="multiple"' : '';
  92. $multipleSize = $multipleSize > 1 ? "size=\"$multipleSize\"" : '';
  93. $submit = $submit ? 'onchange="this.form.submit();"' : '';
  94. $html = "<select name=\"$name\" id=\"$id\" $multipleSize $multiple $submit $disabled>";
  95. if ($defaultText && !$selectedValue) {
  96. $sel = ((string)$defaultValue == (string)$selectedValue ? 'selected="selected"' : '');
  97. $html .= "<option value=\"" . DataUtil::formatForDisplay($defaultValue) . "\" $sel>" . DataUtil::formatForDisplay($defaultText) . "</option>";
  98. }
  99. if ($allText) {
  100. $sel = ((string)$allValue == (string)$selectedValue ? 'selected="selected"' : '');
  101. $html .= "<option value=\"" . DataUtil::formatForDisplay($allValue) . "\" $sel>" . DataUtil::formatForDisplay($allText) . "</option>";
  102. }
  103. foreach ($data as $k => $v) {
  104. $sel = ((string)$selectedValue == (string)$k ? 'selected="selected"' : '');
  105. $html .= "<option value=\"" . DataUtil::formatForDisplay($k) . "\" $sel>" . DataUtil::formatForDisplay($v) . "</option>";
  106. }
  107. $html .= '</select>';
  108. return $html;
  109. }
  110. /**
  111. * Creates an object array selector.
  112. *
  113. * @param string $modname Module name.
  114. * @param string $objectType Object type.
  115. * @param string $name Select field name.
  116. * @param string $field Value field.
  117. * @param string $displayField Display field.
  118. * @param string $where Where clause.
  119. * @param string $sort Sort clause.
  120. * @param string $selectedValue Selected value.
  121. * @param string $defaultValue Value for "default" option.
  122. * @param string $defaultText Text for "default" option.
  123. * @param string $allValue Value for "all" option.
  124. * @param string $allText Text for "all" option.
  125. * @param string $displayField2 Second display field.
  126. * @param boolean $submit Submit on choose.
  127. * @param boolean $disabled Add Disabled attribute to select.
  128. * @param string $fieldSeparator Field seperator if $displayField2 is given.
  129. * @param integer $multipleSize Size for multiple selects.
  130. *
  131. * @return string The rendered output.
  132. */
  133. public static function getSelector_ObjectArray($modname, $objectType, $name, $field = '', $displayField = 'name', $where = '', $sort = '', $selectedValue = '', $defaultValue = 0, $defaultText = '', $allValue = 0, $allText = '', $displayField2 = null, $submit = true, $disabled = false, $fieldSeparator = ', ', $multipleSize = 1)
  134. {
  135. if (!$modname) {
  136. return z_exit(__f('Invalid %1$s passed to %2$s.', array('modname', 'HtmlUtil::getSelector_ObjectArray')));
  137. }
  138. if (!$objectType) {
  139. return z_exit(__f('Invalid %1$s passed to %2$s.', array('objectType', 'HtmlUtil::getSelector_ObjectArray')));
  140. }
  141. if (!ModUtil::dbInfoLoad($modname)) {
  142. return __f('Unavailable/Invalid %1$s [%2$s] passed to %3$s.', array('modulename', $modname, 'HtmlUtil::getSelector_ObjectArray'));
  143. }
  144. if (!SecurityUtil::checkPermission("$objectType::", '::', ACCESS_OVERVIEW)) {
  145. return __f('Security check failed for %1$s [%2$s] passed to %3$s.', array('modulename', $modname, 'HtmlUtil::getSelector_ObjectArray'));
  146. }
  147. $cacheKey = md5("$modname|$objectType|$where|$sort");
  148. if (isset($cache[$cacheKey])) {
  149. $dataArray = $cache[$cacheKey];
  150. } else {
  151. $classname = "{$modname}\DBObject\\" . StringUtil::camelize($objectType) . 'Array';
  152. $class = new $classname();
  153. //$dataArray = $class->get($where, $sort, -1, -1, '', false, $distinct);
  154. $dataArray = $class->get($where, $sort, -1, -1, '', false);
  155. $cache[$cacheKey] = $dataArray;
  156. if (!$field) {
  157. $field = $class->_objField;
  158. }
  159. }
  160. $data2 = array();
  161. foreach ($dataArray as $object) {
  162. $val = $object[$field];
  163. $disp = $object[$displayField];
  164. if ($displayField2) {
  165. $disp .= $fieldSeparator . $object[$displayField2];
  166. }
  167. $data2[$val] = $disp;
  168. }
  169. return self::getSelector_Generic($name, $data2, $selectedValue, $defaultValue, $defaultText, $allValue, $allText, $submit, $disabled, $multipleSize);
  170. }
  171. /**
  172. * Creates an entity array selector.
  173. *
  174. * @param string $entity Entity name.
  175. * @param string $name Select field name.
  176. * @param string $field Value field.
  177. * @param string $displayField Display field.
  178. * @param string $where Where clause.
  179. * @param string $sort Sort clause.
  180. * @param string $selectedValue Selected value.
  181. * @param string $defaultValue Value for "default" option.
  182. * @param string $defaultText Text for "default" option.
  183. * @param string $allValue Value for "all" option.
  184. * @param string $allText Text for "all" option.
  185. * @param string $displayField2 Second display field.
  186. * @param boolean $submit Submit on choose.
  187. * @param boolean $disabled Add Disabled attribute to select.
  188. * @param string $fieldSeparator Field seperator if $displayField2 is given.
  189. * @param integer $multipleSize Size for multiple selects.
  190. *
  191. * @return string The rendered output.
  192. */
  193. public static function getSelector_EntityArray($entity, $name, $field, $displayField = 'name', $where = '', $sort = '', $selectedValue = '', $defaultValue = 0, $defaultText = '', $allValue = 0, $allText = '', $displayField2 = null, $submit = true, $disabled = false, $fieldSeparator = ', ', $multipleSize = 1)
  194. {
  195. if (!$entity) {
  196. return z_exit(__f('Invalid %1$s passed to %2$s.', array('entity', 'HtmlUtil::getSelector_EntityArray')));
  197. }
  198. if (!$field) {
  199. return z_exit(__f('Invalid %1$s passed to %2$s.', array('field', 'HtmlUtil::getSelector_EntityArray')));
  200. }
  201. $em = \ServiceUtil::get('doctrine')->getManager();
  202. $filters = array();
  203. if (!empty($where)) {
  204. $where = explode("=", $where);
  205. $filters[$where[0]] = $where[1];
  206. }
  207. $ordering = array();
  208. if (!empty($sort)) {
  209. $sort = explode(" ", $sort);
  210. $ordering[$sort[0]] = $sort[1];
  211. }
  212. $dataArray = $em->getRepository($entity)->findBy($filters, $ordering);
  213. $data2 = array();
  214. foreach ($dataArray as $object) {
  215. if (strpos($field, '->') !== false) {
  216. $field_exp = explode('->', $field);
  217. $val = $object[$field_exp[0]][$field_exp[1]];
  218. } else {
  219. $val = $object[$field];
  220. }
  221. if (strpos($displayField, '->') !== false) {
  222. $displayField_exp = explode('->', $displayField);
  223. $disp = $object[$displayField_exp[0]][$displayField_exp[1]];
  224. } else {
  225. $disp = $object[$displayField];
  226. }
  227. if ($displayField2) {
  228. if (strpos($displayField2, '->') !== false) {
  229. $displayField2_exp = explode('->', $displayField2);
  230. $disp2 = $object[$displayField2_exp[0]][$displayField2_exp[1]];
  231. } else {
  232. $disp2 = $object[$displayField2];
  233. }
  234. $disp .= $fieldSeparator . $disp2;
  235. }
  236. $data2[$val] = $disp;
  237. }
  238. return self::getSelector_Generic($name, $data2, $selectedValue, $defaultValue, $defaultText, $allValue, $allText, $submit, $disabled, $multipleSize);
  239. }
  240. /**
  241. * Get selector by table field.
  242. *
  243. * @param string $modname Module name.
  244. * @param string $tablekey Table name.
  245. * @param string $name Select field name.
  246. * @param string $field Field name.
  247. * @param string $where Where clause.
  248. * @param string $sort Sort clause.
  249. * @param string $selectedValue Selected value.
  250. * @param string $defaultValue Value for "default" option.
  251. * @param string $defaultText Text for "default" option.
  252. * @param string $allValue Value for "all" option.
  253. * @param string $allText Text for "all" option.
  254. * @param string $assocKey Key for associative array.
  255. * @param boolean $distinct Use distinct for selection.
  256. * @param boolean $submit Submit on choose.
  257. * @param boolean $disabled Add Disabled attribute to select.
  258. * @param integer $truncate Truncate field to given length.
  259. * @param integer $multipleSize Size for multiple selects.
  260. *
  261. * @return string The rendered output.
  262. */
  263. public static function getSelector_FieldArray($modname, $tablekey, $name, $field = 'id', $where = '', $sort = '', $selectedValue = '', $defaultValue = 0, $defaultText = '', $allValue = 0, $allText = '', $assocKey = '', $distinct = false, $submit = true, $disabled = false, $truncate = 0, $multipleSize = 1)
  264. {
  265. if (!$tablekey) {
  266. return z_exit(__f('Invalid %1$s [%2$s] passed to %3$s.', array('tablekey', $modname, 'HtmlUtil::getSelector_FieldArray')));
  267. }
  268. if (!$name) {
  269. return z_exit(__f('Invalid %1$s [%2$s] passed to %3$s.', array('name', $name, 'HtmlUtil::getSelector_FieldArray')));
  270. }
  271. if ($modname) {
  272. ModUtil::dbInfoLoad($modname, '', true);
  273. }
  274. $fa = DBUtil::selectFieldArray($tablekey, $field, $where, $sort, $distinct, $assocKey);
  275. $data = array();
  276. foreach ($fa as $k => $v) {
  277. if ($v) {
  278. if ($truncate > 0 && strlen($v) > $truncate) {
  279. $v = StringUtil::getTruncatedString($v, $truncate);
  280. }
  281. $data[$k] = $v;
  282. }
  283. }
  284. return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, $allValue, $allText, $submit, $disabled, $multipleSize);
  285. }
  286. /**
  287. * Return the HTML selector code for the given category hierarchy, maps to CategoryUtil::getSelector_Categories().
  288. *
  289. * @param array $cats The category hierarchy to generate a HTML selector for.
  290. * @param string $name The name of the selector field to generate (optional) (default='category[parent_id]').
  291. * @param string $field The field value to return (optional) (default='id').
  292. * @param integer $selectedValue The selected category (optional) (default=0).
  293. * @param integer $defaultValue The default value to present to the user (optional) (default=0).
  294. * @param string $defaultText The default text to present to the user (optional) (default='').
  295. * @param integer $allValue The value to assign for the "All" choice (optional) (default=0).
  296. * @param string $allText The text to display for the "All" choice (optional) (default='').
  297. * @param boolean $submit Whether or not to submit the form upon change (optional) (default=false).
  298. * @param boolean $displayPath If false, the path is simulated, if true, the full path is shown (optional) (default=false).
  299. * @param boolean $doReplaceRootCat Whether or not to replace the root category with a localized string (optional) (default=true).
  300. * @param integer $multipleSize If > 1, a multiple selector box is built, otherwise a normal/single selector box is build (optional) (default=1).
  301. *
  302. * @return The HTML selector code for the given category hierarchy.
  303. */
  304. public static function getSelector_Categories($cats, $name, $field = 'id', $selectedValue = '0', $defaultValue = 0, $defaultText = '', $allValue = 0, $allText = '', $submit = false, $displayPath = false, $doReplaceRootCat = true, $multipleSize = 1)
  305. {
  306. return CategoryUtil::getSelector_Categories($cats, $field, $selectedValue, $name, $defaultValue, $defaultText, $allValue, $allText, $submit, $displayPath, $doReplaceRootCat, $multipleSize);
  307. }
  308. /**
  309. * Return the HTML code for the values in a given category.
  310. *
  311. * @param string $categoryPath The identifying category path.
  312. * @param array $values The values used to populate the defautl states (optional) (default=array()).
  313. * @param string $namePrefix The path/object prefix to apply to the field name (optional) (default='').
  314. * @param string $excludeList A (string) list of IDs to exclude (optional) (default=null).
  315. * @param boolean $disabled Whether or not the checkboxes are to be disabled (optional) (default=false).
  316. *
  317. * @return The resulting dropdown data.
  318. */
  319. public static function getCheckboxes_CategoryField($categoryPath, $values = array(), $namePrefix = '', $excludeList = null, $disabled = false)
  320. {
  321. if (!$categoryPath) {
  322. return z_exit(__f('Invalid %1$s passed to %2$s.', array('category', 'HtmlUtil::getCheckboxes_CategoryField')));
  323. }
  324. if (!$lang) {
  325. $lang = ZLanguage::getLanguageCode();
  326. }
  327. $cats = CategoryUtil::getSubCategoriesByPath($categoryPath, 'path', false, true, false, true, false, '', 'value');
  328. foreach ($cats as $k => $v) {
  329. $val = $k;
  330. $fname = $val;
  331. if ($namePrefix) {
  332. $fname = $namePrefix . '[' . $k . ']';
  333. }
  334. if (strpos($excludeList, ',' . $k . ',') === false) {
  335. $disp = $v['display_name'][$lang];
  336. if (!$disp) {
  337. $disp = $v['name'];
  338. }
  339. $html .= "<input type=\"checkbox\" name=\"" . DataUtil::formatForDisplay($fname) . "\" " . ($values[$k] ? ' checked="checked" ' : '') . ($disabled ? ' disabled="disabled" ' : '') . " />&nbsp;&nbsp;&nbsp;&nbsp;" . DataUtil::formatForDisplay($disp) . "<br />";
  340. }
  341. }
  342. return $html;
  343. }
  344. /**
  345. * Selector for a module's tables.
  346. *
  347. * @param string $modname Module name.
  348. * @param string $name Select field name.
  349. * @param string $selectedValue Selected value.
  350. * @param string $defaultValue Value for "default" option.
  351. * @param string $defaultText Text for "default" option.
  352. * @param boolean $submit Submit on choose.
  353. * @param string $remove Remove string from table name.
  354. * @param boolean $disabled Add Disabled attribute to select.
  355. * @param integer $nStripChars Strip the first n characters.
  356. * @param integer $multipleSize Size for multiple selects.
  357. *
  358. * @return string The rendered output.
  359. */
  360. public static function getSelector_ModuleTables($modname, $name, $selectedValue = '', $defaultValue = 0, $defaultText = '', $submit = false, $remove = '', $disabled = false, $nStripChars = 0, $multipleSize = 1)
  361. {
  362. if (!$modname) {
  363. return z_exit(__f('Invalid %1$s passed to %2$s.', array('modname', 'HtmlUtil::getSelector_ModuleTables')));
  364. }
  365. $modinfo = ModUtil::getInfo(ModUtil::getIdFromName($modname));
  366. $modpath = ($modinfo['type'] == ModUtil::TYPE_SYSTEM) ? 'system' : 'modules';
  367. $osdir = DataUtil::formatForOS($modinfo['directory']);
  368. $entityDir = "$modpath/$osdir/Entity/";
  369. $entities = array();
  370. if (file_exists($entityDir)) {
  371. $files = scandir($entityDir);
  372. foreach ($files as $file) {
  373. if ($file != '.' && $file != '..' && substr($file, -4) === '.php') {
  374. $entities[] = $file;
  375. }
  376. }
  377. }
  378. $data = array();
  379. foreach ($entities as $entity) {
  380. $class = $modname . '\\Entity\\' . substr($entity, 0, strlen($entity) - 4);
  381. if (class_exists($class)) {
  382. $entityName = substr($entity, 0, strlen($entity) - 4);
  383. if ($remove) {
  384. $entityName = str_replace($remove, '', $entityName);
  385. }
  386. if ($nStripChars) {
  387. $entityName = ucfirst(substr($entityName, $nStripChars));
  388. }
  389. $data[$entityName] = $entityName;
  390. }
  391. }
  392. return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, null, null, $submit, $disabled, $multipleSize);
  393. }
  394. /**
  395. * Selector for a module's tables.
  396. *
  397. * @param string $modname Module name.
  398. * @param string $tablename Table name.
  399. * @param string $name Select field name.
  400. * @param string $selectedValue Selected value.
  401. * @param string $defaultValue Value for "default" option.
  402. * @param string $defaultText Text for "default" option.
  403. * @param boolean $submit Submit on choose.
  404. * @param boolean $showSystemColumns Whether or not to show the system columns.
  405. * @param boolean $disabled Add Disabled attribute to select.
  406. * @param integer $multipleSize Size for multiple selects.
  407. *
  408. * @return string The rendered output.
  409. */
  410. public static function getSelector_TableFields($modname, $tablename, $name, $selectedValue = '', $defaultValue = 0, $defaultText = '', $submit = false, $showSystemColumns = false, $disabled = false, $multipleSize = 1)
  411. {
  412. if (!$modname) {
  413. return z_exit(__f('Invalid %1$s passed to %2$s.', array('modname', 'HtmlUtil::getSelector_TableFields')));
  414. }
  415. if (!$tablename) {
  416. return z_exit(__f('Invalid %1$s passed to %2$s.', array('tablename', 'HtmlUtil::getSelector_TableFields')));
  417. }
  418. if (!$name) {
  419. return z_exit(__f('Invalid %1$s passed to %2$s.', array('name', 'HtmlUtil::getSelector_TableFields')));
  420. }
  421. $tables = ModUtil::dbInfoLoad($modname, '', true);
  422. $colkey = $tablename . '_column';
  423. $cols = $tables[$colkey];
  424. if (!$cols) {
  425. return z_exit(__f('Invalid %1$s [%2$s] in %3$s.', array('column key', $colkey, 'HtmlUtil::getSelector_TableFields')));
  426. }
  427. if (!$showSystemColumns) {
  428. $filtercols = array();
  429. ObjectUtil::addStandardFieldsToTableDefinition($filtercols, '');
  430. }
  431. $data = array();
  432. foreach ($cols as $k => $v) {
  433. if ($showSystemColumns) {
  434. $data[$v] = $k;
  435. } else {
  436. if (!$filtercols[$k]) {
  437. $data[$v] = $k;
  438. }
  439. }
  440. }
  441. return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, $allValue, $allText, $submit, $disabled, $multipleSize);
  442. }
  443. /**
  444. * Return the HTML code for the Yes/No dropdown.
  445. *
  446. * @param integer $selected The value which should be selected (default=1) (optional).
  447. * @param string $name The name of the generated selector (optional).
  448. *
  449. * @return The resulting HTML string.
  450. */
  451. public static function getSelector_YesNo($selected = '1', $name = '')
  452. {
  453. if (!$name) {
  454. $name = 'permission';
  455. }
  456. $vals = array();
  457. $vals[0] = __('No');
  458. $vals[1] = __('Yes');
  459. return self::getSelector_Generic($name, $vals);
  460. }
  461. /**
  462. * Return the localized string for the specified yes/no value.
  463. *
  464. * @param integer $val The value for which we wish to obtain the string representation.
  465. *
  466. * @return The string representation for the selected value.
  467. */
  468. public static function getSelectorValue_YesNo($val)
  469. {
  470. $vals = array();
  471. $vals[0] = __('No');
  472. $vals[1] = __('Yes');
  473. return $vals[$val];
  474. }
  475. /**
  476. * Return the dropdown data for the language selector.
  477. *
  478. * @param boolean $includeAll Whether or not to include the 'All' choice.
  479. *
  480. * @return The string representation for the selected value.
  481. */
  482. public static function getSelectorData_Language($includeAll = true)
  483. {
  484. $langlist = array();
  485. $dropdown = array();
  486. if ($includeAll) {
  487. $dropdown[] = array('id' => '', 'name' => __('All'));
  488. }
  489. $langlist = ZLanguage::getInstalledLanguageNames();
  490. asort($langlist);
  491. foreach ($langlist as $k => $v) {
  492. $dropdown[] = array('id' => $k, 'name' => $v);
  493. }
  494. return $dropdown;
  495. }
  496. /**
  497. * Return the localized string for the given value.
  498. *
  499. * @param mixed $value The currently active/selected value.
  500. *
  501. * @return The resulting HTML string.
  502. */
  503. public static function getSelectorValue_Permission($value)
  504. {
  505. $perms = array();
  506. $perms[_Z_PERMISSION_BASIC_PRIVATE] = __('Private');
  507. $perms[_Z_PERMISSION_BASIC_GROUP] = __('Group');
  508. $perms[_Z_PERMISSION_BASIC_USER] = __('User');
  509. $perms[_Z_PERMISSION_BASIC_PUBLIC] = __('Public');
  510. return $perms[$value];
  511. }
  512. /**
  513. * Return the HTML code for the Permission dropdown.
  514. *
  515. * @param string $name The name of the generated selector (optional) (default='permission').
  516. * @param integer $selectedValue The value which should be selected (optional) (default=2).
  517. *
  518. * @return The resulting HTML string.
  519. */
  520. public static function getSelector_Permission($name = 'permission', $selectedValue = 'U')
  521. {
  522. if (!$name) {
  523. $name = 'permission';
  524. }
  525. $perms = array();
  526. $perms[_Z_PERMISSION_BASIC_PRIVATE] = __('Private');
  527. $perms[_Z_PERMISSION_BASIC_GROUP] = __('Group');
  528. $perms[_Z_PERMISSION_BASIC_USER] = __('User');
  529. $perms[_Z_PERMISSION_BASIC_PUBLIC] = __('Public');
  530. return self::getSelector_Generic($name, $perms, $selectedValue, $defaultValue, $defaultText, $allValue, $allText, $submit, $disabled, $multipleSize);
  531. }
  532. /**
  533. * Return the HTML code for the Permission Level dropdown.
  534. *
  535. * @param string $name The name of the generated selector (optional) (default='permission').
  536. * @param integer $selectedValue The value which should be selected (optional) (default=0).
  537. *
  538. * @return The resulting HTML string.
  539. */
  540. public static function getSelector_PermissionLevel($name = 'permission', $selectedValue = '0')
  541. {
  542. $perms = array();
  543. $perms[_Z_PERMISSION_LEVEL_NONE] = __('No access');
  544. $perms[_Z_PERMISSION_LEVEL_READ] = __('Read access');
  545. $perms[_Z_PERMISSION_LEVEL_WRITE] = __('Write access');
  546. return self::getSelector_Generic($name, $perms, $selectedValue, $defaultValue, $defaultText, $allValue, $allText, $submit, $disabled, $multipleSize);
  547. }
  548. /**
  549. * Return the html for the PN user group selector.
  550. *
  551. * @param string $name The selector name.
  552. * @param integer $selectedValue The currently selected value of the selector (optional) (default=0).
  553. * @param integer $defaultValue The default value of the selector (optional) (default=0).
  554. * @param string $defaultText The text of the default value (optional) (default='').
  555. * @param integer $allValue The value to assign for the "All" choice (optional) (default=0).
  556. * @param string $allText The text to display for the "All" choice (optional) (default='').
  557. * @param string $excludeList A (string) list of IDs to exclude (optional) (default=null).
  558. * @param boolean $submit Whether or not to auto-submit the selector (optional) (default=false).
  559. * @param boolean $disabled Whether or not to disable selector (optional) (default=false).
  560. * @param integer $multipleSize The size to use for a multiple selector, 1 produces a normal/single selector (optional (default=1).
  561. *
  562. * @return The html for the user group selector.
  563. */
  564. public static function getSelector_Group($name = 'groupid', $selectedValue = 0, $defaultValue = 0, $defaultText = '', $allValue = 0, $allText = '', $excludeList = '', $submit = false, $disabled = false, $multipleSize = 1)
  565. {
  566. $data = array();
  567. $grouplist = UserUtil::getGroups(array(), array('name' => 'ASC'));
  568. foreach ($grouplist as $k => $v) {
  569. $id = $v['gid'];
  570. $disp = $v['name'];
  571. if (strpos($excludeList, ",$id,") === false) {
  572. $data[$id] = $disp;
  573. }
  574. }
  575. return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, $allValue, $allText, $submit, $disabled, $multipleSize);
  576. }
  577. /**
  578. * Return a PN array strcuture for the PN user dropdown box.
  579. *
  580. * @param string $name The selector name.
  581. * @param integer $gid The group ID to get users for (optional) (default=null).
  582. * @param integer $selectedValue The currently selected value of the selector (optional) (default=0).
  583. * @param integer $defaultValue The default value of the selector (optional) (default=0).
  584. * @param string $defaultText The text of the default value (optional) (default='').
  585. * @param integer $allValue The value to assign for the "All" choice (optional) (default='').
  586. * @param string $allText The text to display for the "All" choice (optional) (default='').
  587. * @param string $excludeList A (string) list of IDs to exclude (optional) (default=null).
  588. * @param boolean $submit Whether or not to auto-submit the selector (optional) (default=false).
  589. * @param boolean $disabled Whether or not to disable selector (optional) (default=false).
  590. * @param integer $multipleSize The size to use for a multiple selector, 1 produces a normal/single selector (optional (default=1).
  591. *
  592. * @return The string for the user group selector.
  593. */
  594. public static function getSelector_User($name = 'userid', $gid = null, $selectedValue = 0, $defaultValue = 0, $defaultText = '', $allValue = 0, $allText = '', $excludeList = '', $submit = false, $disabled = false, $multipleSize = 1)
  595. {
  596. $where = '';
  597. if ($excludeList) {
  598. $where = "WHERE uid NOT IN ($excludeList)";
  599. }
  600. if ($gid) {
  601. $users = UserUtil::getUsersForGroup($gid);
  602. if ($users) {
  603. $and = $where ? ' AND ' : '';
  604. $where .= $and . 'uid IN (' . implode(',', $users) . ')';
  605. }
  606. }
  607. $data = array();
  608. $userlist = UserUtil::getUsers($where, 'ORDER BY uname');
  609. foreach ($userlist as $k => $v) {
  610. $data[$k] = $v['uname'];
  611. }
  612. return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, $allValue, $allText, $submit, $disabled, $multipleSize);
  613. }
  614. /**
  615. * Return the html for the PNModule selector.
  616. *
  617. * @param string $name The selector name.
  618. * @param integer $selectedValue The currently selected value of the selector (optional) (default=0).
  619. * @param integer $defaultValue The default value of the selector (optional) (default=0).
  620. * @param string $defaultText The text of the default value (optional) (default='').
  621. * @param integer $allValue The value to assign the "All" choice (optional) (default=0).
  622. * @param string $allText The text to display for the "All" choice (optional) (default='').
  623. * @param boolean $submit Whether or not to auto-submit the selector.
  624. * @param boolean $disabled Whether or not to disable selector (optional) (default=false).
  625. * @param integer $multipleSize The size to use for a multiple selector, 1 produces a normal/single selector (optional (default=1).
  626. * @param string $field The field to use for value.
  627. *
  628. * @return The string for the user group selector.
  629. */
  630. public static function getSelector_Module($name='moduleName', $selectedValue=0, $defaultValue=0, $defaultText='', $allValue=0, $allText='', $submit=false, $disabled=false, $multipleSize=1, $field='name')
  631. {
  632. $data = array();
  633. $modules = ModUtil::getModulesByState(3, 'displayname');
  634. foreach ($modules as $module) {
  635. $value = $module[$field];
  636. $displayname = $module['displayname'];
  637. $data[$value] = $displayname;
  638. }
  639. return self::getSelector_Generic($name, $data, $selectedValue, $defaultValue, $defaultText, $allValue, $allText, $submit, $disabled, $multipleSize);
  640. }
  641. /**
  642. * Return the HTML for the date day selector.
  643. *
  644. * @param integer $selectedValue The value which should be selected (default=0) (optional).
  645. * @param string $name The name of the generated selector (default='day') (optional).
  646. * @param boolean $submit Whether or not to auto-submit the selector.
  647. * @param boolean $disabled Whether or not to disable selector (optional) (default=false).
  648. * @param integer $multipleSize The size to use for a multiple selector, 1 produces a normal/single selector (optional (default=1).
  649. *
  650. * @return The generated HTML for the selector.
  651. */
  652. public static function getSelector_DatetimeDay($selectedValue = 0, $name = 'day', $submit = false, $disabled = false, $multipleSize = 1)
  653. {
  654. if (!$name) {
  655. $name = 'day';
  656. }
  657. $data = array();
  658. for ($i = 1; $i < 32; $i++) {
  659. $val = sprintf("%02d", $i);
  660. $data[$val] = $val;
  661. }
  662. return self::getSelector_Generic($name, $data, $selectedValue, null, null, null, null, $submit, $disabled, $multipleSize = 1);
  663. }
  664. /**
  665. * Return the HTML for the date hour selector.
  666. *
  667. * @param integer $selectedValue The value which should be selected (default=0) (optional).
  668. * @param string $name The name of the generated selector (default='hour') (optional).
  669. * @param boolean $submit Whether or not to auto-submit the selector.
  670. * @param boolean $disabled Whether or not to disable selector (optional) (default=false).
  671. * @param integer $multipleSize The size to use for a multiple selector, 1 produces a normal/single selector (optional (default=1).
  672. *
  673. * @return The generated HTML for the selector.
  674. */
  675. public static function getSelector_DatetimeHour($selectedValue = 0, $name = 'hour', $submit = false, $disabled = false, $multipleSize = 1)
  676. {
  677. if (!$name) {
  678. $name = 'hour';
  679. }
  680. $data = array();
  681. for ($i = 0; $i < 24; $i++) {
  682. $val = sprintf("%02d", $i);
  683. $data[$val] = $val;
  684. }
  685. return self::getSelector_Generic($name, $data, $selectedValue, null, null, null, null, $submit, $disabled, $multipleSize = 1);
  686. }
  687. /**
  688. * Return the HTML for the date minute selector.
  689. *
  690. * @param integer $selectedValue The value which should be selected (default=0) (optional).
  691. * @param string $name The name of the generated selector (default='minute') (optional).
  692. * @param boolean $submit Whether or not to auto-submit the selector.
  693. * @param boolean $disabled Whether or not to disable selector (optional) (default=false).
  694. * @param integer $multipleSize The size to use for a multiple selector, 1 produces a normal/single selector (optional (default=1).
  695. *
  696. * @return The generated HTML for the selector.
  697. */
  698. public static function getSelector_DatetimeMinute($selectedValue = 0, $name = 'minute', $submit = false, $disabled = false, $multipleSize = 1)
  699. {
  700. if (!$name) {
  701. $name = 'minute';
  702. }
  703. $data = array();
  704. for ($i = 0; $i < 60; $i += 5) {
  705. $val = sprintf('%02d', $i);
  706. $data[$val] = $val;
  707. }
  708. return self::getSelector_Generic($name, $data, $selectedValue, null, null, null, null, $submit, $disabled, $multipleSize = 1);
  709. }
  710. /**
  711. * Return the HTML for the date month selector.
  712. *
  713. * @param integer $selected The value which should be selected (default=0) (optional).
  714. * @param string $name The name of the generated selector (default='month') (optional).
  715. * @param boolean $submit Whether or not to auto-submit the selector.
  716. * @param boolean $disabled Whether or not to disable selector (optional) (default=false).
  717. * @param integer $multipleSize The size to use for a multiple selector, 1 produces a normal/single selector (optional (default=1).
  718. * @param string $text Text to print.
  719. *
  720. * @return The generated HTML for the selector.
  721. */
  722. public static function getSelector_DatetimeMonth($selected=0, $name='month', $submit=false, $disabled=false, $multipleSize=1, $text=0)
  723. {
  724. if (!$name) {
  725. $name = 'month';
  726. }
  727. if ($text) {
  728. $mnames = explode(' ', __('January February March April May June July August September October November December'));
  729. }
  730. array_unshift($mnames, 'noval');
  731. $id = strtr($name, '[]', '__');
  732. $disabled = $disabled ? 'disabled="disabled"' : '';
  733. $multiple = $multipleSize > 1 ? 'multiple="multiple"' : '';
  734. $multipleSize = $multipleSize > 1 ? "size=\"$multipleSize\"" : '';
  735. $submit = $submit ? 'onchange="this.form.submit();"' : '';
  736. $html = "<select name=\"" . DataUtil::formatForDisplay($name) . "\" id=\"" . DataUtil::formatForDisplay($id) . "\" " . DataUtil::formatForDisplay($multipleSize) . " $multiple $submit $disabled>";
  737. for ($i = 1; $i < 13; $i++) {
  738. $val = sprintf("%02d", $i);
  739. $opt = $text ? $mnames[$i] : $val;
  740. $sel = ($i == $selected ? 'selected="selected"' : '');
  741. $html = $html . "<option value=\"$val\" $sel>" . DataUtil::formatForDisplay($opt) . "</option>";
  742. }
  743. $html = $html . '</select>';
  744. return $html;
  745. }
  746. /**
  747. * Return the HTML for the date year selector.
  748. *
  749. * @param integer $selectedValue The value which should be selected (default=2009) (optional).
  750. * @param string $name The name of the generated selector (default='year') (optional).
  751. * @param integer $first The start year for the selector (default=2003) (optional).
  752. * @param integer $last The name of the generated selector (default=2007) (optional).
  753. * @param boolean $submit Whether or not to auto-submit the selector.
  754. * @param boolean $disabled Whether or not to disable selector (optional) (default=false).
  755. * @param integer $multipleSize The size to use for a multiple selector, 1 produces a normal/single selector (optional (default=1).
  756. *
  757. * @return The generated HTML for the selector.
  758. */
  759. public static function getSelector_DatetimeYear($selectedValue = 2009, $name = 'year', $first = 2003, $last = 2008, $submit = false, $disabled = false, $multipleSize = 1)
  760. {
  761. if (!$name) {
  762. $name = 'year';
  763. }
  764. $data = array();
  765. for ($i = $first; $i < $last; $i++) {
  766. $data[$i] = $i;
  767. }
  768. return self::getSelector_Generic($name, $data, $selectedValue, null, null, null, null, $submit, $disabled, $multipleSize = 1);
  769. }
  770. /**
  771. * Return the HTML for the country selector.
  772. *
  773. * @param string $name The name of the generated selector (default='countries') (optional).
  774. * @param string $selectedValue The value which is currently selected (default='') (optional).
  775. * @param string $defaultValue The default value to select (default='') (optional).
  776. * @param string $defaultText The text for the default value (default='') (optional).
  777. * @param integer $allValue The value to assign for the "All" choice (optional) (default=0).
  778. * @param string $allText The text to display for the "All" choice (optional) (default='').
  779. * @param boolean $submit Whether or not to auto-submit the selector.
  780. * @param boolean $disabled Whether or not to disable selector (optional) (default=false).
  781. * @param integer $multipleSize The size to use for a multiple selector, 1 produces a normal/single selector (optional (default=1).
  782. *
  783. * @return The generated HTML for the selector.
  784. */
  785. public static function getSelector_Countries($name = 'countries', $selectedValue = '', $defaultValue = 0, $defaultText = '', $allValue = 0, $allText = '', $submit = false, $disabled = false, $multipleSize = 1)
  786. {
  787. $countries = ZLanguage::countryMap();
  788. asort($countries);
  789. return self::getSelector_Generic($name, $countries, $selectedValue, $defaultValue, $defaultText, $allValue, $allText, $submit, $disabled, $multipleSize);
  790. }
  791. /**
  792. * Same as PN HTMLApi function but adds javascript form submit code to selector.
  793. *
  794. * @param string $fieldname Field name.
  795. * @param array $data Data array.
  796. * @param integer $multiple Whether or not this is a multiple select.
  797. * @param integer $size Size for multiple selects.
  798. * @param string $selected Selected value.
  799. * @param string $accesskey Access key.
  800. * @param string $onchange OnChange event.
  801. *
  802. * @return string The rendered output.
  803. */
  804. public static function FormSelectMultipleSubmit($fieldname, $data, $multiple = 0, $size = 1, $selected = '', $accesskey = '', $onchange = '')
  805. {
  806. if (empty($fieldname)) {
  807. return '';
  808. }
  809. // Set up selected if required
  810. if (!empty($selected)) {
  811. for ($i = 0; !empty($data[$i]); $i++) {
  812. if ($data[$i]['id'] == $selected) {
  813. $data[$i]['selected'] = 1;
  814. }
  815. }
  816. }
  817. $c = count($data);
  818. if ($c < $size) {
  819. $size = $c;
  820. }
  821. $idname = strtr($fieldname, '[]', '__');
  822. $output = '<select' . ' name="' . DataUtil::formatForDisplay($fieldname) . '"'
  823. . ' id="' . DataUtil::formatForDisplay($idname) . '"'
  824. . ' size="' . DataUtil::formatForDisplay($size) . '"'
  825. . (($multiple == 1) ? ' multiple="multiple"' : '')
  826. . ((empty($accesskey)) ? '' : ' accesskey="' . DataUtil::formatForDisplay($accesskey) . '"')
  827. //. ' tabindex="'.$this->tabindex.'"'
  828. . ($onchange ? " onchange=\"$onchange\"" : '') . '>';
  829. foreach ($data as $datum) {
  830. $output .= '<option value="' . DataUtil::formatForDisplay($datum['id']) . '"' . ((empty($datum['selected'])) ? '' : " selected='$datum[selected]'") . '>' . DataUtil::formatForDisplay($datum['name']) . '</option>';
  831. }
  832. $output .= '</select>';
  833. return $output;
  834. }
  835. }