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.

126 lines
3.0 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. protected $fields = array();
  16. protected $messages = array(self::SUCCESS => 'Form data valid',
  17. self::ERROR => 'Form data invalid');
  18. protected $valid = true;
  19. public function __construct()
  20. {
  21. $this->init();
  22. }
  23. /**
  24. * @param string $name
  25. * @return FormField
  26. */
  27. protected function addField($name, $message = false)
  28. {
  29. $this->fields[$name] = new FormField($message);
  30. return $this->fields[$name];
  31. }
  32. public function isValid($data)
  33. {
  34. if (!is_array($data)) {
  35. throw new Exception(__CLASS__ . '::' . __METHOD__ . ' expects an array');
  36. }
  37. foreach ($this->fields as $field_name => $field) {
  38. if (isset($data[$field_name])) {
  39. $this->valid &= $field->isValid($data[$field_name], $data);
  40. } else {
  41. $this->valid &= $field->isValid(null, $data);
  42. }
  43. }
  44. if (!$this->valid) {
  45. $this->fillHelperData();
  46. }
  47. return $this->valid;
  48. }
  49. public function getMessages()
  50. {
  51. $messages = array();
  52. foreach ($this->fields as $name => $field) {
  53. if ($mess = $field->getMessage()) {
  54. $messages[$name] = $mess;
  55. }
  56. }
  57. return $messages;
  58. }
  59. public function getValue($key)
  60. {
  61. if (isset($this->fields[$key])) {
  62. return $this->fields[$key]->getValue();
  63. }
  64. return false;
  65. }
  66. public function getValues()
  67. {
  68. $values = array();
  69. foreach ($this->fields as $key => $field) {
  70. if (!$field->isIgnored()) {
  71. $values[$key] = $field->getValue();
  72. }
  73. }
  74. return $values;
  75. }
  76. public function getSourceValues()
  77. {
  78. $values = array();
  79. foreach ($this->fields as $key => $field) {
  80. $values[$key] = $field->getSourceValue();
  81. }
  82. return $values;
  83. }
  84. public function getMessageType()
  85. {
  86. return ($this->valid) ? self::SUCCESS : self::ERROR;
  87. }
  88. public function getMessage()
  89. {
  90. return $this->messages[$this->getMessageType()];
  91. }
  92. public function setSuccessMessage($message)
  93. {
  94. $this->messages[self::SUCCESS] = (string) $message;
  95. return $this;
  96. }
  97. public function setErrorMessage($message)
  98. {
  99. $this->messages[self::ERROR] = (string) $message;
  100. return $this;
  101. }
  102. protected function fillHelperData()
  103. {
  104. $data['messages'] = $this->getMessages();
  105. $data['values'] = $this->getSourceValues();
  106. Session::set(get_class($this), $data);
  107. }
  108. abstract protected function init();
  109. }