/mod/rafl/classes/class_form.php

https://github.com/nadavkav/MoodleTAO · PHP · 418 lines · 275 code · 70 blank · 73 comment · 55 complexity · 60dc241fab3dbd1f4f55626284c14a66 MD5 · raw file

  1. <?php
  2. //----------------------------------------------------------------------------------------------
  3. // Desc: Print out form elements and their labels in a standardised way
  4. // Depd: -
  5. //----------------------------------------------------------------------------------------------
  6. class form {
  7. //----------------------------------------------------------------------------------------------
  8. // Desc: Make input boxes look nice
  9. // Depd: -
  10. //----------------------------------------------------------------------------------------------
  11. function getStylesheet($argExternalStylesheet) {
  12. if (strlen($argExternalStylesheet)) {
  13. echo $argExternalStylesheet;
  14. } else {
  15. echo "<style type=\"text/css\">\n";
  16. echo " div.error {\n";
  17. echo " display: none;\n";
  18. echo " }\n";
  19. echo " input.input {\n";
  20. echo " width: 400px;\n";
  21. echo " }\n";
  22. echo " file.file {\n";
  23. echo " width: 405px;\n";
  24. echo " }\n";
  25. echo " select.select {\n";
  26. echo " width: 405px;\n";
  27. echo " }\n";
  28. echo " select.selectAddRemove {\n";
  29. echo " width: 180px;\n";
  30. echo " }\n";
  31. echo " textarea.textarea {\n";
  32. echo " width: 400px;\n";
  33. echo " height: 150px;\n";
  34. echo " }\n";
  35. echo "</style>\n";
  36. }
  37. }
  38. //----------------------------------------------------------------------------------------------
  39. // Desc: Create an input box using a common style
  40. // Depd: -
  41. //----------------------------------------------------------------------------------------------
  42. function getInputField($elementId, $labelText='', $defaultValue='', $extraAttribute='') {
  43. if (strlen($labelText)) {
  44. $label = '<label id="' . $elementId . 'Label" for="' . $elementId . '" class="label">' . $labelText . '</label>' . "\n";
  45. }
  46. return($label . '<div id="' . $elementId . 'Error" class="error"></div>
  47. <div id="' . $elementId . 'Element" class="element">
  48. <input class="input" type="text" name="' . $elementId . '" id="' . $elementId . '"
  49. value="' . htmlentities($defaultValue) . '" ' . $extraAttribute . ' />
  50. </div>' . "\n");
  51. }
  52. //----------------------------------------------------------------------------------------------
  53. // Desc: Create an password box using a common style
  54. // Depd: -
  55. //----------------------------------------------------------------------------------------------
  56. function getInputPassword($elementId, $labelText='', $extraAttribute='') {
  57. if (strlen($labelText)) {
  58. $label = '<label id="' . $elementId . 'Label" for="' . $elementId . '" class="label">' . $labelText . '</label>' . "\n";
  59. }
  60. return($label . '<div id="' . $elementId . 'Error" class="error"></div>
  61. <div id="' . $elementId . 'Element" class="element">
  62. <input class="input" type="password" name="' . $elementId . '" id="' . $elementId . '"
  63. value="" ' . $extraAttribute . ' />
  64. </div>' . "\n");
  65. }
  66. //----------------------------------------------------------------------------------------------
  67. // Desc: Create a checkbox using a common style
  68. // Depd: -
  69. //----------------------------------------------------------------------------------------------
  70. function getInputCheckbox($elementId, $labelText='', $defaultValue, $isChecked=false, $extraAttribute='') {
  71. // WORKAROUND: Count checkboxes of the same name within this function
  72. global $idCheckElementIds, $idCheckCount;
  73. if (is_array($idCheckElementIds) && in_array($elementId, $idCheckElementIds)) {
  74. $idCheckCount++;
  75. } else {
  76. $idCheckCount = 1;
  77. }
  78. $idCheckElementIds[] = $elementId;
  79. $checkedAttribute = $isChecked == true ? ' checked="checked" ' : '';
  80. if (strlen($labelText)) {
  81. $label = '<label id="' . $elementId . 'Label_' . $idCheckCount . '" for="' . $elementId . '_' . $idCheckCount . '" class="checkboxLabel">' . $labelText . '</label>' . "\n";
  82. }
  83. return('<div id="' . $elementId . '_' . $idCheckCount . 'Error" class="error"></div>
  84. <div id="' . $elementId . 'Element_' . $idCheckCount . '" class="element">
  85. <input class="checkbox" type="checkbox" name="' . $elementId . '[]" id="' . $elementId . '_' . $idCheckCount . '"
  86. value="' . htmlentities($defaultValue) . '" ' . $checkedAttribute . $extraAttribute . ' />
  87. ' . $label . '
  88. </div>' . "\n");
  89. }
  90. //----------------------------------------------------------------------------------------------
  91. // Desc: Create a radio button using a common style
  92. // Depd: -
  93. //----------------------------------------------------------------------------------------------
  94. function getInputRadio($elementId, $labelText='', $defaultValue, $isChecked=false, $extraAttribute='') {
  95. // WORKAROUND: Count radios of the same name within this function
  96. global $idRadioElementIds, $idRadioCount;
  97. if (is_array($idRadioElementIds) && in_array($elementId, $idRadioElementIds)) {
  98. $idRadioCount++;
  99. } else {
  100. $idRadioCount = 1;
  101. }
  102. $idRadioElementIds[] = $elementId;
  103. $checkedAttribute = $isChecked == true ? ' checked="checked" ' : '';
  104. if (strlen($labelText)) {
  105. $label = '<label id="' . $elementId . 'Label_' . $idRadioCount . '" for="' . $elementId . '_' . $idRadioCount . '" class="radioLabel">' . $labelText . '</label>' . "\n";
  106. }
  107. return('<div id="' . $elementId . '_' . $idRadioCount . 'Error" class="error"></div>
  108. <div id="' . $elementId . 'Element_' . $idRadioCount . '" class="element">
  109. <input class="radio" type="radio" name="' . $elementId . '" id="' . $elementId . '_' . $idRadioCount . '"
  110. value="' . htmlentities($defaultValue) . '" ' . $checkedAttribute . $extraAttribute . ' />
  111. ' . $label . '
  112. </div>' . "\n");
  113. }
  114. //----------------------------------------------------------------------------------------------
  115. // Desc: Create an input file upload box using a common style
  116. // Depd: -
  117. //----------------------------------------------------------------------------------------------
  118. function getInputFile($elementId, $labelText='', $extraAttribute='') {
  119. if (strlen($labelText)) {
  120. $label = '<label id="' . $elementId . 'Label" for="' . $elementId . '" class="label">' . $labelText . '</label>' . "\n";
  121. }
  122. return($label . '<div id="' . $elementId . 'Error" class="error"></div>
  123. <div id="' . $elementId . 'Element" class="element">
  124. <input class="file" name="' . $elementId . '" id="' . $elementId . '"
  125. type="file" ' . $extraAttribute . ' />
  126. </div>' . "\n");
  127. }
  128. //----------------------------------------------------------------------------------------------
  129. // Desc: Create an input text box using a common style
  130. // Depd: -
  131. //----------------------------------------------------------------------------------------------
  132. function getInputTextarea($elementId, $labelText='', $defaultValue='', $extraAttribute='') {
  133. if (strlen($labelText)) {
  134. $label = '<label id="' . $elementId . 'Label" for="' . $elementId . '" class="label">' . $labelText . '</label>' . "\n";
  135. }
  136. return($label . '<div id="' . $elementId . 'Error" class="error"></div>
  137. <div id="' . $elementId . 'Element" class="element">
  138. <textarea class="textarea" name="' . $elementId . '"
  139. id="' . $elementId . '" ' . $extraAttribute . '>' . htmlentities($defaultValue) . '</textarea>
  140. </div>' . "\n");
  141. }
  142. //----------------------------------------------------------------------------------------------
  143. // Desc: Create a select box
  144. // Depd: -
  145. //----------------------------------------------------------------------------------------------
  146. function getSelectField($elementId, $labelText='', $arrayValues, $defaultValue='', $sizeOfDropdown=1, $multipleSelect=0, $extraAttribute='') {
  147. if (strlen($labelText)) {
  148. $label = '<label id="' . $elementId . 'Label" for="' . $labelText . '" class="label">' . $labelText . '</label>' . "\n";
  149. }
  150. $selectHtml = $label;
  151. $selectHtml .= '<div id="' . $elementId . 'Error" class="error"></div>
  152. <div id="' . $elementId . 'Element" class="element">
  153. ';
  154. // Multi-select box?
  155. if ($multipleSelect!=0) {
  156. $selectHtml .= '<select name="' . $elementId . '[]" multiple="multiple"';
  157. } else {
  158. $selectHtml .= '<select name="' . $elementId . '"';
  159. }
  160. $selectHtml .= ' class="select" id="' . $elementId . '" size="' . $sizeOfDropdown . '" ' . $extraAttribute . '>' . "\n";
  161. if (count($arrayValues)) {
  162. foreach ($arrayValues as $arrayKey=>$arrayData) {
  163. // Now check if we need to select this value
  164. if (is_array($defaultValue) && in_array($arrayKey, $defaultValue)) {
  165. $selectHtml .= '<option value="' . $arrayKey . '" selected="selected">' . $arrayData . '</option>' . "\n";
  166. } elseif ($arrayKey == $defaultValue) {
  167. $selectHtml .= '<option value="' . $arrayKey . '" selected="selected">' . $arrayData . '</option>' . "\n";
  168. } else {
  169. $selectHtml .= '<option value="' . $arrayKey . '">' . $arrayData . '</option>' . "\n";
  170. }
  171. }
  172. }
  173. $selectHtml .='</select></div>' . "\n";
  174. return $selectHtml;
  175. }
  176. //----------------------------------------------------------------------------------------------
  177. // Desc: Create two select boxes that can have entries be added to or removed from
  178. // Depd: -
  179. //----------------------------------------------------------------------------------------------
  180. function getAddRemoveSelectField($elementId, $labelText='', $arrayValues, $defaultValue='', $sizeOfDropdown=1, $extraAttribute='') {
  181. $this->getAddRemoveSupportingJavascript();
  182. if (strlen($labelText)) {
  183. $label = '<label id="' . $elementId . 'Label" for="' . $labelText . '" class="label">' . $labelText . '</label>' . "\n";
  184. }
  185. $selectHtml = $label;
  186. $selectHtml .= '<div id="' . $elementId . 'Error" class="error"></div>
  187. <div id="' . $elementId . 'Element" class="element">
  188. <div id="' . $elementId . 'ElementSelectPoolHolder" class="elementAddRemoveSelectPoolHolder">
  189. <select class="selectAddRemove" id="' . $elementId . 'Pool" name="' . $elementId . 'Pool[]" size="' . $sizeOfDropdown . '" multiple="multiple">' . "\n";
  190. if (is_array($arrayValues)) {
  191. foreach ($arrayValues as $arrayKey=>$arrayData) {
  192. $selectHtml .= ' <option value="' . $arrayKey . '">' . $arrayData . '</option>' . "\n";
  193. }
  194. }
  195. $selectHtml .=' </select>
  196. </div>
  197. <div class="elementAddRemoveSelectButtons">
  198. <input type="button" onclick="javascript:addBrowseToDestList(this.form.' . $elementId . 'Pool, this.form.' . $elementId . ') " class="addButton" value="Add &raquo;&raquo;" />
  199. <input type="button" onclick="javascript:deleteFromDestList(this.form.' . $elementId . ');" class="removeButton" value="&laquo;&laquo; Remove" />
  200. </div>
  201. <div id="' . $elementId . 'ElementSelectHolder" class="elementAddRemoveSelectHolder">
  202. <select class="selectAddRemove" id="' . $elementId . '" name="' . $elementId . '[]" size="' . $sizeOfDropdown . '" multiple="multiple"' . "\n";
  203. $selectHtml .= ' ' . $extraAttribute . '>' . "\n";
  204. // Now check if we need to select this value
  205. if (is_array($arrayValues)) {
  206. foreach ($arrayValues as $arrayKey=>$arrayData) {
  207. // Now check if we need to select this value
  208. if (is_array($defaultValue) && in_array($arrayKey, $defaultValue)) {
  209. $selectHtml .= '<option value="' . $arrayKey . '" selected="selected">' . $arrayData . '</option>' . "\n";
  210. } elseif ($arrayKey == $defaultValue) {
  211. $selectHtml .= '<option value="' . $arrayKey . '" selected="selected">' . $arrayData . '</option>' . "\n";
  212. }
  213. }
  214. }
  215. $selectHtml .=' </select>' . "\n";
  216. $selectHtml .=' </div>' . "\n";
  217. $selectHtml .='</div>' . "\n";
  218. return $selectHtml;
  219. }
  220. //----------------------------------------------------------------------------------------------
  221. // Desc: Output supporting javascript functions for the add remove select
  222. // Depd: -
  223. //----------------------------------------------------------------------------------------------
  224. function getAddRemoveSupportingJavascript() {
  225. // WARNING: Make sure the javascript only gets outputted once on the page
  226. if (! strpos(ob_get_contents(), 'addSearchToDestList')) {
  227. ob_start();
  228. ?>
  229. <script type="text/javascript">
  230. // Adds to the destination list via the search input field
  231. function addSearchToDestList(argFormField, argDestList) {
  232. var len = argDestList.length;
  233. var found = false;
  234. for(var count = 0; count < len; count++) {
  235. if (argDestList.options[count] != null) {
  236. if (argFormField.value == argDestList.options[count].value) {
  237. found = true;
  238. break;
  239. }
  240. }
  241. }
  242. if (found != true) {
  243. argDestList.options[len] = new Option(argFormField.value, argFormField.value);
  244. }
  245. // Reset search box
  246. argFormField.value = '';
  247. // Select all right column entries
  248. allSelect(argDestList);
  249. }
  250. // Adds to the destination list via browse select box
  251. function addBrowseToDestList(argSrcList, argDestList) {
  252. var len = argDestList.length;
  253. for(var i = 0; i < argSrcList.length; i++) {
  254. if ((argSrcList.options[i] != null) && (argSrcList.options[i].selected)) {
  255. // Check if this value already exist in the destList or not
  256. // if not then add it otherwise do not add it.
  257. var found = false;
  258. for(var count = 0; count < len; count++) {
  259. if (argDestList.options[count] != null) {
  260. if (argSrcList.options[i].value == argDestList.options[count].value) {
  261. found = true;
  262. break;
  263. }
  264. }
  265. }
  266. if (found != true) {
  267. argDestList.options[len] = new Option(argSrcList.options[i].text, argSrcList.options[i].value);
  268. len++;
  269. }
  270. }
  271. }
  272. // Select all right column entries
  273. allSelect(argDestList);
  274. }
  275. // Deletes from the destination list
  276. function deleteFromDestList(argDestList) {
  277. var len = argDestList.options.length;
  278. for(var i = (len-1); i >= 0; i--) {
  279. if ((argDestList.options[i] != null) && (argDestList.options[i].selected == true)) {
  280. argDestList.options[i] = null;
  281. }
  282. }
  283. // Select all right column entries
  284. allSelect(argDestList);
  285. }
  286. // Selects all entries in a multiple select box
  287. function allSelect(list) {
  288. for (i=0;i<list.length;i++) {
  289. list.options[i].selected = true;
  290. }
  291. }
  292. </script>
  293. <?php
  294. }
  295. }
  296. //----------------------------------------------------------------------------------------------
  297. // Desc: Create a submit button using a common style
  298. // Depd: -
  299. //----------------------------------------------------------------------------------------------
  300. function getSubmitButton($elementId, $elementValue='Save', $extraAttribute='') {
  301. return('<div id="' . $elementId . 'Element" class="elementSubmit">
  302. <input class="submit" type="submit"
  303. name="' . $elementId . '"
  304. id="' . $elementId . '"
  305. value="' . $elementValue . '" ' . $extraAttribute . '/>
  306. </div>' . "\n");
  307. }
  308. //----------------------------------------------------------------------------------------------
  309. // Desc: Checks for DOM compliant javascript and prints out a button, if successful
  310. // Depd: -
  311. //----------------------------------------------------------------------------------------------
  312. function getJavascriptSubmitButtonCheck($elementId, $elementValue='Save', $extraAttribute='') {
  313. return('<div id="' . $elementId . 'Element" class="elementButton">
  314. <script type="text/javascript">
  315. if (document.getElementById) {
  316. document.write(\' <input class="button" type="button" name="' . $elementId . '" id="' . $elementId . '" value="' . $elementValue . '" ' . $extraAttribute . ' />\');
  317. } else {
  318. document.write(\' <p style="font-weight:bold; color:#FF0000;">\');
  319. document.write(\' Error: It appears that your browser does not support the current JavaScript web standard.\');
  320. document.write(\' Please upgrade your browser software.\');
  321. document.write(\' You may then reload this page to submit this form.\');
  322. document.write(\' <\/p>\');
  323. }
  324. </script>
  325. <noscript>
  326. <p style="font-weight:bold; color:#FF0000;">
  327. Error: It appears that your browser does not support JavaScript, or you have it disabled.
  328. Please adjust your browser settings to support JavaScript.
  329. You may then reload this page to submit this form.
  330. </p>
  331. </noscript>
  332. </div>' . "\n");
  333. }
  334. //----------------------------------------------------------------------------------------------
  335. // Desc: Get form element for a hidden field
  336. // Depd: -
  337. //----------------------------------------------------------------------------------------------
  338. function getInputHidden($elementId, $elementValue='', $extraAttribute='') {
  339. return('<div id="' . $elementId . 'Error" class="error"></div>
  340. <div id="' . $elementId . 'Element">
  341. <input type="hidden"
  342. id="' . $elementId . '"
  343. name="' . $elementId . '"
  344. value="' . htmlentities($elementValue) . '" ' . $extraAttribute . '/>
  345. </div>' . "\n");
  346. }
  347. }
  348. ?>