EditHistory.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. class Application_Form_EditHistory extends Zend_Form
  3. {
  4. const VALIDATE_DATETIME_FORMAT = 'yyyy-MM-dd HH:mm:ss';
  5. //this is used by the javascript widget, unfortunately h/H is opposite from Zend.
  6. const TIMEPICKER_DATETIME_FORMAT = 'yyyy-MM-dd hh:mm:ss';
  7. const VALIDATE_DATE_FORMAT = 'yyyy-MM-dd';
  8. const VALIDATE_TIME_FORMAT = 'HH:mm:ss';
  9. const ITEM_TYPE = "type";
  10. const ITEM_CLASS = "class";
  11. const ITEM_OPTIONS = "options";
  12. const ITEM_ID_SUFFIX = "name";
  13. const TEXT_INPUT_CLASS = "input_text";
  14. private $formElTypes = array(
  15. TEMPLATE_DATE => array(
  16. "class" => "Zend_Form_Element_Text",
  17. "attrs" => array(
  18. "class" => self::TEXT_INPUT_CLASS
  19. ),
  20. "validators" => array(
  21. array(
  22. "class" => "Zend_Validate_Date",
  23. "params" => array(
  24. "format" => self::VALIDATE_DATE_FORMAT
  25. )
  26. )
  27. ),
  28. "filters" => array(
  29. "StringTrim"
  30. )
  31. ),
  32. TEMPLATE_TIME => array(
  33. "class" => "Zend_Form_Element_Text",
  34. "attrs" => array(
  35. "class" => self::TEXT_INPUT_CLASS
  36. ),
  37. "validators" => array(
  38. array(
  39. "class" => "Zend_Validate_Date",
  40. "params" => array(
  41. "format" => self::VALIDATE_TIME_FORMAT
  42. )
  43. )
  44. ),
  45. "filters" => array(
  46. "StringTrim"
  47. )
  48. ),
  49. TEMPLATE_DATETIME => array(
  50. "class" => "Zend_Form_Element_Text",
  51. "attrs" => array(
  52. "class" => self::TEXT_INPUT_CLASS
  53. ),
  54. "validators" => array(
  55. array(
  56. "class" => "Zend_Validate_Date",
  57. "params" => array(
  58. "format" => self::VALIDATE_DATETIME_FORMAT
  59. )
  60. )
  61. ),
  62. "filters" => array(
  63. "StringTrim"
  64. )
  65. ),
  66. TEMPLATE_STRING => array(
  67. "class" => "Zend_Form_Element_Text",
  68. "attrs" => array(
  69. "class" => self::TEXT_INPUT_CLASS
  70. ),
  71. "filters" => array(
  72. "StringTrim"
  73. )
  74. ),
  75. TEMPLATE_BOOLEAN => array(
  76. "class" => "Zend_Form_Element_Checkbox",
  77. "validators" => array(
  78. array(
  79. "class" => "Zend_Validate_InArray",
  80. "options" => array(
  81. "haystack" => array(0,1)
  82. )
  83. )
  84. )
  85. ),
  86. TEMPLATE_INT => array(
  87. "class" => "Zend_Form_Element_Text",
  88. "validators" => array(
  89. array(
  90. "class" => "Zend_Validate_Int",
  91. )
  92. ),
  93. "attrs" => array(
  94. "class" => self::TEXT_INPUT_CLASS
  95. )
  96. ),
  97. TEMPLATE_FLOAT => array(
  98. "class" => "Zend_Form_Element_Text",
  99. "attrs" => array(
  100. "class" => self::TEXT_INPUT_CLASS
  101. ),
  102. "validators" => array(
  103. array(
  104. "class" => "Zend_Validate_Float",
  105. )
  106. )
  107. ),
  108. );
  109. public function init() {
  110. $history_id = new Zend_Form_Element_Hidden($this::ID_PREFIX.'id');
  111. $history_id->setValidators(array(
  112. new Zend_Validate_Int()
  113. ));
  114. $history_id->setDecorators(array('ViewHelper'));
  115. $this->addElement($history_id);
  116. $dynamic_attrs = new Zend_Form_SubForm();
  117. $this->addSubForm($dynamic_attrs, $this::ID_PREFIX.'template');
  118. // Add the submit button
  119. $this->addElement('button', $this::ID_PREFIX.'save', array(
  120. 'ignore' => true,
  121. 'class' => 'btn '.$this::ID_PREFIX.'save',
  122. 'label' => _('Save'),
  123. 'decorators' => array(
  124. 'ViewHelper'
  125. )
  126. ));
  127. // Add the cancel button
  128. $this->addElement('button', $this::ID_PREFIX.'cancel', array(
  129. 'ignore' => true,
  130. 'class' => 'btn '.$this::ID_PREFIX.'cancel',
  131. 'label' => _('Cancel'),
  132. 'decorators' => array(
  133. 'ViewHelper'
  134. )
  135. ));
  136. }
  137. public function createFromTemplate($template, $required) {
  138. $templateSubForm = $this->getSubForm($this::ID_PREFIX.'template');
  139. for ($i = 0, $len = count($template); $i < $len; $i++) {
  140. $item = $template[$i];
  141. //don't dynamically add this as it should be included in the
  142. //init() function already if it should show up in the UI..
  143. if (in_array($item["name"], $required)) {
  144. continue;
  145. }
  146. $formElType = $this->formElTypes[$item[self::ITEM_TYPE]];
  147. $label = $item[self::ITEM_ID_SUFFIX];
  148. $id = $this::ID_PREFIX.$label;
  149. $el = new $formElType[self::ITEM_CLASS]($id);
  150. $el->setLabel($item["label"]);
  151. if (isset($formElType["attrs"])) {
  152. $attrs = $formElType["attrs"];
  153. foreach ($attrs as $key => $value) {
  154. $el->setAttrib($key, $value);
  155. }
  156. }
  157. if (isset($formElType["filters"])) {
  158. $filters = $formElType["filters"];
  159. foreach ($filters as $filter) {
  160. $el->addFilter($filter);
  161. }
  162. }
  163. if (isset($formElType["validators"])) {
  164. $validators = $formElType["validators"];
  165. foreach ($validators as $index => $arr) {
  166. $options = isset($arr[self::ITEM_OPTIONS]) ? $arr[self::ITEM_OPTIONS] : null;
  167. $validator = new $arr[self::ITEM_CLASS]($options);
  168. //extra validator info
  169. if (isset($arr["params"])) {
  170. foreach ($arr["params"] as $key => $value) {
  171. $method = "set".ucfirst($key);
  172. $validator->$method($value);
  173. }
  174. }
  175. $el->addValidator($validator);
  176. }
  177. }
  178. $el->setDecorators(array('ViewHelper'));
  179. $templateSubForm->addElement($el);
  180. }
  181. }
  182. }