AdminForm.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Admin
  4. *
  5. * @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Admin;
  9. use ArrayAccess;
  10. use Exception;
  11. use Grav\Common\Data\Blueprint;
  12. use Grav\Common\Data\Data;
  13. use Grav\Framework\Form\Interfaces\FormFlashInterface;
  14. use Grav\Framework\Form\Interfaces\FormInterface;
  15. use Grav\Framework\Form\Traits\FormTrait;
  16. use InvalidArgumentException;
  17. use JsonSerializable;
  18. /**
  19. * Class AdminForm
  20. * @package Grav\Plugin\Admin
  21. */
  22. class AdminForm implements FormInterface, JsonSerializable
  23. {
  24. use FormTrait;
  25. /** @var string */
  26. protected $nonce_name;
  27. /** @var string */
  28. protected $nonce_action;
  29. /** @var callable */
  30. protected $submitMethod;
  31. /**
  32. * AdminForm constructor.
  33. *
  34. * @param string $name
  35. * @param array $options
  36. */
  37. public function __construct(string $name, array $options)
  38. {
  39. $this->name = $name;
  40. $this->nonce_name = $options['nonce_name'] ?? 'admin-nonce';
  41. $this->nonce_action = $options['nonce_action'] ?? 'admin-form';
  42. $this->setId($options['id'] ?? $this->getName());
  43. $this->setUniqueId($options['unique_id'] ?? $this->getName());
  44. $this->setBlueprint($options['blueprint']);
  45. $this->setSubmitMethod($options['submit_method'] ?? null);
  46. $this->setFlashLookupFolder('tmp://admin/forms/[SESSIONID]');
  47. if (!empty($options['reset'])) {
  48. $this->getFlash()->delete();
  49. }
  50. $this->initialize();
  51. }
  52. /**
  53. * @return $this
  54. */
  55. public function initialize(): AdminForm
  56. {
  57. $this->messages = [];
  58. $this->submitted = false;
  59. $this->unsetFlash();
  60. /** @var FormFlashInterface $flash */
  61. $flash = $this->getFlash();
  62. if ($flash->exists()) {
  63. $data = $flash->getData();
  64. if (null !== $data) {
  65. $data = new Data($data, $this->getBlueprint());
  66. $data->setKeepEmptyValues(true);
  67. $data->setMissingValuesAsNull(true);
  68. }
  69. $this->data = $data;
  70. $this->files = $flash->getFilesByFields(false);
  71. } else {
  72. $this->data = new Data([], $this->getBlueprint());
  73. $this->files = [];
  74. }
  75. return $this;
  76. }
  77. /**
  78. * @return string
  79. */
  80. public function getNonceName(): string
  81. {
  82. return $this->nonce_name;
  83. }
  84. /**
  85. * @return string
  86. */
  87. public function getNonceAction(): string
  88. {
  89. return $this->nonce_action;
  90. }
  91. /**
  92. * @return string
  93. */
  94. public function getScope(): string
  95. {
  96. return 'data.';
  97. }
  98. /**
  99. * @param Blueprint $blueprint
  100. */
  101. public function setBlueprint(Blueprint $blueprint): void
  102. {
  103. if (null === $blueprint) {
  104. throw new InvalidArgumentException('Blueprint is required');
  105. }
  106. $this->blueprint = $blueprint;
  107. }
  108. /**
  109. * @param string $field
  110. * @param mixed $value
  111. */
  112. public function setData(string $field, $value): void
  113. {
  114. $this->getData()->set($field, $value);
  115. }
  116. /**
  117. * @return Blueprint
  118. */
  119. public function getBlueprint(): Blueprint
  120. {
  121. return $this->blueprint;
  122. }
  123. /**
  124. * @param callable|null $submitMethod
  125. */
  126. public function setSubmitMethod(?callable $submitMethod): void
  127. {
  128. if (null === $submitMethod) {
  129. throw new InvalidArgumentException('Submit method is required');
  130. }
  131. $this->submitMethod = $submitMethod;
  132. }
  133. /**
  134. * @param array $data
  135. * @param array $files
  136. * @return void
  137. * @throws Exception
  138. */
  139. protected function doSubmit(array $data, array $files): void
  140. {
  141. $method = $this->submitMethod;
  142. $method($data, $files);
  143. $this->reset();
  144. }
  145. /**
  146. * Filter validated data.
  147. *
  148. * @param ArrayAccess|Data|null $data
  149. * @return void
  150. */
  151. protected function filterData($data = null): void
  152. {
  153. if ($data instanceof Data) {
  154. $data->filter(true, true);
  155. }
  156. }
  157. }