PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/Formlibrary/FormLibrary.class.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 200 lines | 127 code | 21 blank | 52 comment | 8 complexity | 3086c2578368b581ad013736b3068001 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. require_once Path :: get_plugin_path() . 'FormLibrary/Rules/Rule.class.php';
  3. require_once Path :: get_plugin_path() . 'FormLibrary/ElementStorage/ElementStorage.class.php';
  4. require_once Path :: get_plugin_path() . 'FormLibrary/Elements/Element.class.php';
  5. require_once Path :: get_plugin_path() . 'FormLibrary/Containers/Container.class.php';
  6. require_once Path :: get_plugin_path() . 'FormLibrary/Renderer/Render.class.php';
  7. require_once Path :: get_plugin_path() . 'FormLibrary/Elements/CustomElements/form_library_html_editor.class.php';
  8. /*
  9. * Main class to create forms
  10. */
  11. class FormLibrary
  12. {
  13. private $name;
  14. private $method;
  15. private $action;
  16. private $elementStorage;
  17. private $renderer;
  18. /*
  19. * Constructor for a form
  20. */
  21. function FormLibrary($name = null, $method = null, $action = null)
  22. {
  23. //Set name of the form if the user gave one
  24. if (! empty($name))
  25. {
  26. $this->name = $name;
  27. }
  28. else
  29. //Set a unique name for the form
  30. {
  31. $a = "FormLibrary_" . mt_rand();
  32. }
  33. //Set method of the form if the user gave one
  34. if (! empty($method))
  35. {
  36. $this->method = $method;
  37. }
  38. else
  39. //Set a unique name for the form
  40. {
  41. $this->method = "POST";
  42. }
  43. //Set the action of the form if the user submitted one
  44. if (! empty($action))
  45. {
  46. $this->action = $action;
  47. }
  48. else
  49. {
  50. $this->action = $_SERVER['PHP_SELF'];
  51. if (! empty($_SERVER['QUERY_STRING']))
  52. {
  53. $this->action .= '?' . $_SERVER['QUERY_STRING'];
  54. }
  55. }
  56. //Make an ElementStorage object
  57. $this->elementStorage = new ElementStorage();
  58. $this->renderer = new DefaultRenderer($this);
  59. }
  60. /*
  61. * Add element to the form
  62. */
  63. function add_element($element)
  64. {
  65. $this->elementStorage->add_element($element);
  66. }
  67. /*
  68. * Retrieve element from the form
  69. */
  70. function retrieve_element($element)
  71. {
  72. return $this->elementStorage->retrieve_element($element);
  73. }
  74. /*
  75. * Delete element from the form
  76. */
  77. function delete_element($element)
  78. {
  79. $this->elementStorage->delete_element($element);
  80. }
  81. /*
  82. * Get the element storage of the form
  83. */
  84. function get_element_storage()
  85. {
  86. return $this->elementStorage;
  87. }
  88. /*
  89. * Check if the rules of every element are respected
  90. * Server side validation
  91. */
  92. function is_valid()
  93. {
  94. $postback = true;
  95. if (empty($_POST))
  96. {
  97. $postback = false;
  98. $valid = false;
  99. }
  100. $elements = $this->get_element_storage();
  101. if ($postback)
  102. {
  103. $valid = true;
  104. foreach ($elements->get_elements() as $element)
  105. {
  106. if (! $element->is_valid())
  107. $valid = false;
  108. }
  109. return $valid;
  110. }
  111. }
  112. /*
  113. * This function returns the HTML of the form and his elements
  114. */
  115. function render()
  116. {
  117. $render = $this->renderer->render();
  118. return $render;
  119. }
  120. /*
  121. * Via this function you can set a specific renderer to the form
  122. */
  123. function set_renderer($render)
  124. {
  125. $this->renderer = $render;
  126. }
  127. /*
  128. * This function returns the name of the form
  129. */
  130. function get_name()
  131. {
  132. return $this->name;
  133. }
  134. /*
  135. * This function returns the method of the form
  136. */
  137. function get_method()
  138. {
  139. return $this->method;
  140. }
  141. /*
  142. * This function returns the action of the form
  143. */
  144. function get_action()
  145. {
  146. return $this->action;
  147. }
  148. /*
  149. * This function set the action of the form
  150. */
  151. function set_action($action)
  152. {
  153. if (! is_null($action))
  154. {
  155. $this->action = $action;
  156. }
  157. }
  158. /*
  159. * This functions returns the input error messages
  160. */
  161. function get_error()
  162. {
  163. return $this->error;
  164. }
  165. /*
  166. * Collect all the javascript code per element
  167. */
  168. function get_javascripts()
  169. {
  170. $code = '';
  171. $elements = $this->elementstorage->get_elements();
  172. foreach ($elements as $element)
  173. {
  174. $code .= $element->get_javascript();
  175. }
  176. return $code;
  177. }
  178. }
  179. ?>