You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.3 KiB

  1. <?php
  2. /**
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage form
  7. * @since 2010-04-24
  8. * @version SVN: $Id$
  9. * @filesource $URL$
  10. */
  11. abstract class Form
  12. {
  13. const SUCCESS = 'success';
  14. const ERROR = 'error';
  15. /**
  16. * @var FormField[]
  17. */
  18. protected $fields = array();
  19. /**
  20. * @var array
  21. */
  22. protected $messages = array(
  23. self::SUCCESS => 'Form data valid',
  24. self::ERROR => 'Form data invalid');
  25. protected $valid = true;
  26. public function __construct()
  27. {
  28. $this->init();
  29. }
  30. /**
  31. * @param string $name
  32. * @param bool|string $message
  33. * @return FormField
  34. */
  35. protected function addField($name, $message = false)
  36. {
  37. $this->fields[$name] = new FormField($message);
  38. return $this->fields[$name];
  39. }
  40. public function isValid($data)
  41. {
  42. if (!is_array($data)) {
  43. throw new InitializationException(__CLASS__ . '::' . __METHOD__ . ' expects an array');
  44. }
  45. foreach ($this->fields as $field_name => $field) {
  46. if (isset($data[$field_name])) {
  47. $this->valid &= $field->isValid($data[$field_name], $data);
  48. } else {
  49. $this->valid &= $field->isValid(null, $data);
  50. }
  51. }
  52. if (!$this->valid) {
  53. $this->fillHelperData();
  54. }
  55. return $this->valid;
  56. }
  57. public function getMessages()
  58. {
  59. $messages = array();
  60. foreach ($this->fields as $name => $field) {
  61. if ($mess = $field->getMessage()) {
  62. $messages[$name] = $mess;
  63. }
  64. }
  65. return $messages;
  66. }
  67. public function getValue($key)
  68. {
  69. if (isset($this->fields[$key])) {
  70. return $this->fields[$key]->getValue();
  71. }
  72. return false;
  73. }
  74. /**
  75. * @return array
  76. */
  77. public function getValues()
  78. {
  79. $values = array();
  80. foreach ($this->fields as $key => $field) {
  81. if (!$field->isIgnored()) {
  82. $values[$key] = $field->getValue();
  83. }
  84. }
  85. return $values;
  86. }
  87. /**
  88. * @return array
  89. */
  90. public function getSourceValues()
  91. {
  92. $values = array();
  93. foreach ($this->fields as $key => $field) {
  94. $values[$key] = $field->getSourceValue();
  95. }
  96. return $values;
  97. }
  98. /**
  99. * @return string
  100. */
  101. public function getMessageType()
  102. {
  103. return ($this->valid) ? self::SUCCESS : self::ERROR;
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function getMessage()
  109. {
  110. return $this->messages[$this->getMessageType()];
  111. }
  112. /**
  113. * @param string $message
  114. * @return Form
  115. */
  116. public function setSuccessMessage($message)
  117. {
  118. $this->messages[self::SUCCESS] = (string) $message;
  119. return $this;
  120. }
  121. /**
  122. * @param string $message
  123. * @return Form
  124. */
  125. public function setErrorMessage($message)
  126. {
  127. $this->messages[self::ERROR] = (string) $message;
  128. return $this;
  129. }
  130. protected function fillHelperData()
  131. {
  132. $data['messages'] = $this->getMessages();
  133. $data['values'] = $this->getSourceValues();
  134. Session::set(get_class($this), $data);
  135. }
  136. abstract protected function init();
  137. }