form.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Iterator;
  4. use Grav\Common\Grav;
  5. use Grav\Common\GravTrait;
  6. use Grav\Common\Page\Page;
  7. use RocketTheme\Toolbox\Event\Event;
  8. class Form extends Iterator
  9. {
  10. use GravTrait;
  11. /**
  12. * @var array
  13. */
  14. protected $values = array();
  15. /**
  16. * @var Page $page
  17. */
  18. protected $page;
  19. /**
  20. * Create form for the given page.
  21. *
  22. * @param Page $page
  23. */
  24. public function __construct(Page $page)
  25. {
  26. $this->page = $page;
  27. $header = $page->header();
  28. $this->rules = isset($header->rules) ? $header->rules : array();
  29. $this->data = isset($header->data) ? $header->data : array();
  30. $this->items = $header->form;
  31. // Set form name if not set.
  32. if (empty($this->items['name'])) {
  33. $this->items['name'] = $page->slug();
  34. }
  35. }
  36. /**
  37. * Return page object for the form.
  38. *
  39. * @return Page
  40. */
  41. public function page()
  42. {
  43. return $this->page;
  44. }
  45. /**
  46. * Get value of given variable (or all values).
  47. *
  48. * @param string $name
  49. * @return mixed
  50. */
  51. public function value($name = null)
  52. {
  53. if (!$name) {
  54. return $this->values;
  55. }
  56. return $this->getField($name, 'values');
  57. }
  58. /**
  59. * Get value of given variable (or all values).
  60. *
  61. * @param string $name
  62. * @return mixed
  63. */
  64. public function setValue($name = null, $value = '')
  65. {
  66. if (!$name) {
  67. return;
  68. }
  69. $this->values[$name] = $value;
  70. }
  71. /**
  72. * Reset values.
  73. */
  74. public function reset()
  75. {
  76. $this->values = array();
  77. }
  78. /**
  79. * Handle form processing on POST action.
  80. */
  81. public function post()
  82. {
  83. if (isset($_POST)) {
  84. $this->values = (array) $_POST;
  85. }
  86. foreach($this->items['fields'] as $field) {
  87. if ($field['type'] == 'checkbox') {
  88. if (isset($this->values[$field['name']])) {
  89. $this->values[$field['name']] = true;
  90. } else {
  91. $this->values[$field['name']] = false;
  92. }
  93. }
  94. }
  95. $process = isset($this->items['process']) ? $this->items['process'] : array();
  96. if (is_array($process)) {
  97. foreach ($process as $action => $data) {
  98. if (is_numeric($action)) {
  99. $action = \key($data);
  100. $data = $data[$action];
  101. }
  102. self::getGrav()->fireEvent('onFormProcessed', new Event(['form' => $this, 'action' => $action, 'params' => $data]));
  103. }
  104. } else {
  105. // Default action.
  106. }
  107. }
  108. /**
  109. * @param string $name
  110. * @param string $scope
  111. * @return mixed|null
  112. * @internal
  113. */
  114. protected function getField($name, $scope = 'value')
  115. {
  116. $path = explode('.', $name);
  117. $current = $this->{$scope};
  118. foreach ($path as $field) {
  119. if (is_object($current) && isset($current->{$field})) {
  120. $current = $current->{$field};
  121. } elseif (is_array($current) && isset($current[$field])) {
  122. $current = $current[$field];
  123. } else {
  124. return null;
  125. }
  126. }
  127. return $current;
  128. }
  129. }