FlexForm.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /**
  3. * @package Grav\Framework\Flex
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Framework\Flex;
  9. use Grav\Common\Data\Blueprint;
  10. use Grav\Common\Data\Data;
  11. use Grav\Common\Grav;
  12. use Grav\Common\Twig\Twig;
  13. use Grav\Common\Utils;
  14. use Grav\Framework\Flex\Interfaces\FlexFormInterface;
  15. use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
  16. use Grav\Framework\Form\Traits\FormTrait;
  17. use Grav\Framework\Route\Route;
  18. use Twig\Error\LoaderError;
  19. use Twig\Error\SyntaxError;
  20. use Twig\Template;
  21. use Twig\TemplateWrapper;
  22. /**
  23. * Class FlexForm
  24. * @package Grav\Framework\Flex
  25. */
  26. class FlexForm implements FlexFormInterface
  27. {
  28. use FormTrait {
  29. FormTrait::doSerialize as doTraitSerialize;
  30. FormTrait::doUnserialize as doTraitUnserialize;
  31. }
  32. /** @var array|null */
  33. private $form;
  34. /** @var FlexObjectInterface */
  35. private $object;
  36. /**
  37. * FlexForm constructor.
  38. * @param string $name
  39. * @param FlexObjectInterface $object
  40. * @param array|null $form
  41. */
  42. public function __construct(string $name, FlexObjectInterface $object, array $form = null)
  43. {
  44. $this->name = $name;
  45. $this->form = $form;
  46. $uniqueId = $object->exists() ? $object->getStorageKey() : "{$object->getFlexType()}:new";
  47. $this->setObject($object);
  48. $this->setId($this->getName());
  49. $this->setUniqueId(md5($uniqueId));
  50. $this->messages = [];
  51. $this->submitted = false;
  52. $flash = $this->getFlash();
  53. if ($flash->exists()) {
  54. $data = $flash->getData();
  55. $includeOriginal = (bool)($this->getBlueprint()->form()['images']['original'] ?? null);
  56. $this->data = $data ? new Data($data, $this->getBlueprint()) : null;
  57. $this->files = $flash->getFilesByFields($includeOriginal);
  58. } else {
  59. $this->data = null;
  60. $this->files = [];
  61. }
  62. }
  63. /**
  64. * @return string
  65. */
  66. public function getName(): string
  67. {
  68. $object = $this->getObject();
  69. $name = $this->name ?: 'object';
  70. return "flex-{$object->getFlexType()}-{$name}";
  71. }
  72. /**
  73. * @return Data|FlexObjectInterface|object
  74. */
  75. public function getData()
  76. {
  77. return $this->data ?? $this->getObject();
  78. }
  79. /**
  80. * Get a value from the form.
  81. *
  82. * Note: Used in form fields.
  83. *
  84. * @param string $name
  85. * @return mixed
  86. */
  87. public function getValue(string $name)
  88. {
  89. // Attempt to get value from the form data.
  90. $value = $this->data ? $this->data[$name] : null;
  91. // Return the form data or fall back to the object property.
  92. return $value ?? $this->getObject()->getFormValue($name);
  93. }
  94. public function getDefaultValue(string $name)
  95. {
  96. return $this->object->getDefaultValue($name);
  97. }
  98. /**
  99. * @return array
  100. */
  101. public function getDefaultValues(): array
  102. {
  103. return $this->object->getDefaultValues();
  104. }
  105. /**
  106. * @return string
  107. */
  108. public function getFlexType(): string
  109. {
  110. return $this->object->getFlexType();
  111. }
  112. /**
  113. * @return FlexObjectInterface
  114. */
  115. public function getObject(): FlexObjectInterface
  116. {
  117. return $this->object;
  118. }
  119. public function updateObject(): FlexObjectInterface
  120. {
  121. $data = $this->data instanceof Data ? $this->data->toArray() : [];
  122. $files = $this->files;
  123. return $this->getObject()->update($data, $files);
  124. }
  125. /**
  126. * @return Blueprint
  127. */
  128. public function getBlueprint(): Blueprint
  129. {
  130. if (null === $this->blueprint) {
  131. try {
  132. $blueprint = $this->getObject()->getBlueprint(Utils::isAdminPlugin() ? '' : $this->name);
  133. if ($this->form) {
  134. // We have field overrides available.
  135. $blueprint->extend(['form' => $this->form], true);
  136. $blueprint->init();
  137. }
  138. } catch (\RuntimeException $e) {
  139. if (!isset($this->form['fields'])) {
  140. throw $e;
  141. }
  142. // Blueprint is not defined, but we have custom form fields available.
  143. $blueprint = new Blueprint(null, ['form' => $this->form]);
  144. $blueprint->load();
  145. $blueprint->setScope('object');
  146. $blueprint->init();
  147. }
  148. $this->blueprint = $blueprint;
  149. }
  150. return $this->blueprint;
  151. }
  152. /**
  153. * @return Route|null
  154. */
  155. public function getFileUploadAjaxRoute(): ?Route
  156. {
  157. $object = $this->getObject();
  158. if (!method_exists($object, 'route')) {
  159. return null;
  160. }
  161. return $object->route('/edit.json/task:media.upload');
  162. }
  163. /**
  164. * @param string $field
  165. * @param string $filename
  166. * @return Route|null
  167. */
  168. public function getFileDeleteAjaxRoute($field, $filename): ?Route
  169. {
  170. $object = $this->getObject();
  171. if (!method_exists($object, 'route')) {
  172. return null;
  173. }
  174. return $object->route('/edit.json/task:media.delete');
  175. }
  176. public function getMediaTaskRoute(array $params = [], $extension = null): string
  177. {
  178. $grav = Grav::instance();
  179. /** @var Flex $flex */
  180. $flex = $grav['flex_objects'];
  181. if (method_exists($flex, 'adminRoute')) {
  182. return $flex->adminRoute($this->getObject(), $params, $extension ?? 'json');
  183. }
  184. return '';
  185. }
  186. /**
  187. * Implements \Serializable::unserialize().
  188. *
  189. * @param string $data
  190. */
  191. public function unserialize($data): void
  192. {
  193. $data = unserialize($data, ['allowed_classes' => [FlexObject::class]]);
  194. $this->doUnserialize($data);
  195. }
  196. public function __get($name)
  197. {
  198. $method = "get{$name}";
  199. if (method_exists($this, $method)) {
  200. return $this->{$method}();
  201. }
  202. $form = $this->getBlueprint()->form();
  203. return $form[$name] ?? null;
  204. }
  205. public function __set($name, $value)
  206. {
  207. $method = "set{$name}";
  208. if (method_exists($this, $method)) {
  209. $this->{$method}($value);
  210. }
  211. }
  212. public function __isset($name)
  213. {
  214. $method = "get{$name}";
  215. if (method_exists($this, $method)) {
  216. return true;
  217. }
  218. $form = $this->getBlueprint()->form();
  219. return isset($form[$name]);
  220. }
  221. public function __unset($name)
  222. {
  223. }
  224. /**
  225. * Note: this method clones the object.
  226. *
  227. * @param FlexObjectInterface $object
  228. * @return $this
  229. */
  230. protected function setObject(FlexObjectInterface $object): self
  231. {
  232. $this->object = clone $object;
  233. return $this;
  234. }
  235. /**
  236. * @param string $layout
  237. * @return Template|TemplateWrapper
  238. * @throws LoaderError
  239. * @throws SyntaxError
  240. */
  241. protected function getTemplate($layout)
  242. {
  243. $grav = Grav::instance();
  244. /** @var Twig $twig */
  245. $twig = $grav['twig'];
  246. return $twig->twig()->resolveTemplate(
  247. [
  248. "flex-objects/layouts/{$this->getFlexType()}/form/{$layout}.html.twig",
  249. "flex-objects/layouts/_default/form/{$layout}.html.twig",
  250. "forms/{$layout}/form.html.twig",
  251. 'forms/default/form.html.twig'
  252. ]
  253. );
  254. }
  255. /**
  256. * @param array $data
  257. * @param array $files
  258. * @throws \Exception
  259. */
  260. protected function doSubmit(array $data, array $files)
  261. {
  262. /** @var FlexObject $object */
  263. $object = clone $this->getObject();
  264. $object->update($data, $files);
  265. $object->save();
  266. $this->setObject($object);
  267. $this->reset();
  268. }
  269. protected function doSerialize(): array
  270. {
  271. return $this->doTraitSerialize() + [
  272. 'object' => $this->object,
  273. ];
  274. }
  275. protected function doUnserialize(array $data): void
  276. {
  277. $this->doTraitUnserialize($data);
  278. $this->object = $data['object'];
  279. }
  280. /**
  281. * Filter validated data.
  282. *
  283. * @param \ArrayAccess $data
  284. */
  285. protected function filterData(\ArrayAccess $data): void
  286. {
  287. if ($data instanceof Data) {
  288. $data->filter(true, true);
  289. }
  290. }
  291. }