PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/anahkiasen/former/src/Former/Form/Group.php

https://gitlab.com/hatemdigify/digifyblog
PHP | 472 lines | 186 code | 66 blank | 220 comment | 15 complexity | 2468b09211b5091c0a01b0eda670ece0 MD5 | raw file
  1. <?php
  2. namespace Former\Form;
  3. use BadMethodCallException;
  4. use Former\Helpers;
  5. use HtmlObject\Element;
  6. use HtmlObject\Traits\Tag;
  7. use Illuminate\Container\Container;
  8. /**
  9. * Helper class to build groups
  10. */
  11. class Group extends Tag
  12. {
  13. /**
  14. * The Container
  15. *
  16. * @var Container
  17. */
  18. protected $app;
  19. /**
  20. * The current state of the group
  21. *
  22. * @var string
  23. */
  24. protected $state = null;
  25. /**
  26. * Whether the field should be displayed raw or not
  27. *
  28. * @var boolean
  29. */
  30. protected $raw = false;
  31. /**
  32. * The group label
  33. *
  34. * @var Element
  35. */
  36. protected $label;
  37. /**
  38. * The group help
  39. *
  40. * @var array
  41. */
  42. protected $help = array();
  43. /**
  44. * An array of elements to preprend the field
  45. *
  46. * @var array
  47. */
  48. protected $prepend = array();
  49. /**
  50. * An array of elements to append the field
  51. *
  52. * @var array
  53. */
  54. protected $append = array();
  55. /**
  56. * The field validations to be checked for errors
  57. *
  58. * @var array
  59. */
  60. protected $validations = array();
  61. /**
  62. * The group's element
  63. *
  64. * @var string
  65. */
  66. protected $element = 'div';
  67. /**
  68. * Whether a custom group is opened or not
  69. *
  70. * @var boolean
  71. */
  72. public static $opened = false;
  73. /**
  74. * The custom group that is open
  75. *
  76. * @var Former\Form\Group
  77. */
  78. public static $openGroup = null;
  79. ////////////////////////////////////////////////////////////////////
  80. /////////////////////////// CORE METHODS ///////////////////////////
  81. ////////////////////////////////////////////////////////////////////
  82. /**
  83. * Creates a group
  84. *
  85. * @param string $label Its label
  86. */
  87. public function __construct(Container $app, $label, $validations = null)
  88. {
  89. // Get special classes
  90. $this->app = $app;
  91. $this->addClass($this->app['former.framework']->getGroupClasses());
  92. // Invisible if Nude
  93. if ($this->app['former.framework']->is('Nude')) {
  94. $this->element = '';
  95. }
  96. // Set group label
  97. if ($label) {
  98. $this->setLabel($label);
  99. }
  100. // Set validations used to override groups own conclusions
  101. $this->validations = (array) $validations;
  102. }
  103. /**
  104. * Prints out the opening of the Control Group
  105. *
  106. * @return string A control group opening tag
  107. */
  108. public function __toString()
  109. {
  110. return $this->open().$this->getFormattedLabel();
  111. }
  112. /**
  113. * Opens a group
  114. *
  115. * @return string Opening tag
  116. */
  117. public function open()
  118. {
  119. if ($this->getErrors()) {
  120. $this->state($this->app['former.framework']->errorState());
  121. }
  122. // Retrieve state and append it to classes
  123. if ($this->state) {
  124. $this->addClass($this->state);
  125. }
  126. // Required state
  127. if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) {
  128. $this->addClass($this->app['former']->getOption('required_class'));
  129. }
  130. return parent::open();
  131. }
  132. /**
  133. * Set the contents of the current group
  134. *
  135. * @param string $contents The group contents
  136. *
  137. * @return string A group
  138. */
  139. public function contents($contents)
  140. {
  141. return $this->wrap($contents, $this->getFormattedLabel());
  142. }
  143. /**
  144. * Wrap a Field with the current group
  145. *
  146. * @param \Former\Traits\Field $field A Field instance
  147. *
  148. * @return string A group
  149. */
  150. public function wrapField($field)
  151. {
  152. $label = $this->getLabel($field);
  153. $field = $this->prependAppend($field);
  154. $field .= $this->getHelp();
  155. return $this->wrap($field, $label);
  156. }
  157. ////////////////////////////////////////////////////////////////////
  158. //////////////////////////// FIELD METHODS /////////////////////////
  159. ////////////////////////////////////////////////////////////////////
  160. /**
  161. * Set the state of the group
  162. *
  163. * @param string $state A Bootstrap state class
  164. */
  165. public function state($state)
  166. {
  167. // Filter state
  168. $state = $this->app['former.framework']->filterState($state);
  169. $this->state = $state;
  170. }
  171. /**
  172. * Set a class on the Group
  173. *
  174. * @param string $class The class to add
  175. */
  176. public function addGroupClass($class)
  177. {
  178. $this->addClass($class);
  179. }
  180. /**
  181. * Adds a label to the group
  182. *
  183. * @param string $label A label
  184. */
  185. public function setLabel($label)
  186. {
  187. if (!$label instanceof Element) {
  188. $label = Helpers::translate($label);
  189. $label = Element::create('label', $label)->for($label);
  190. }
  191. $this->label = $label;
  192. }
  193. /**
  194. * Get the formatted group label
  195. *
  196. * @return string|null
  197. */
  198. public function getFormattedLabel()
  199. {
  200. if (!$this->label) {
  201. return false;
  202. }
  203. return $this->label->addClass($this->app['former.framework']->getLabelClasses());
  204. }
  205. /**
  206. * Disables the control group for the current field
  207. */
  208. public function raw()
  209. {
  210. $this->raw = true;
  211. }
  212. /**
  213. * Check if the current group is to be displayed or not
  214. *
  215. * @return boolean
  216. */
  217. public function isRaw()
  218. {
  219. return (bool) $this->raw;
  220. }
  221. ////////////////////////////////////////////////////////////////////
  222. ///////////////////////////// HELP BLOCKS //////////////////////////
  223. ////////////////////////////////////////////////////////////////////
  224. /**
  225. * Alias for inlineHelp
  226. *
  227. * @param string $help The help text
  228. * @param array $attributes Facultative attributes
  229. */
  230. public function help($help, $attributes = array())
  231. {
  232. return $this->inlineHelp($help, $attributes);
  233. }
  234. /**
  235. * Add an inline help
  236. *
  237. * @param string $help The help text
  238. * @param array $attributes Facultative attributes
  239. */
  240. public function inlineHelp($help, $attributes = array())
  241. {
  242. // If no help text, do nothing
  243. if (!$help) {
  244. return false;
  245. }
  246. $this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes);
  247. }
  248. /**
  249. * Add an block help
  250. *
  251. * @param string $help The help text
  252. * @param array $attributes Facultative attributes
  253. */
  254. public function blockHelp($help, $attributes = array())
  255. {
  256. // Reserved method
  257. if ($this->app['former.framework']->isnt('TwitterBootstrap') && $this->app['former.framework']->isnt('TwitterBootstrap3')) {
  258. throw new BadMethodCallException('This method is only available on the Bootstrap framework');
  259. }
  260. // If no help text, do nothing
  261. if (!$help) {
  262. return false;
  263. }
  264. $this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes);
  265. }
  266. ////////////////////////////////////////////////////////////////////
  267. ///////////////////////// PREPEND/APPEND METHODS ///////////////////
  268. ////////////////////////////////////////////////////////////////////
  269. /**
  270. * Prepend elements to the field
  271. */
  272. public function prepend()
  273. {
  274. $this->placeAround(func_get_args(), 'prepend');
  275. }
  276. /**
  277. * Append elements to the field
  278. */
  279. public function append()
  280. {
  281. $this->placeAround(func_get_args(), 'append');
  282. }
  283. /**
  284. * Prepends an icon to a field
  285. *
  286. * @param string $icon The icon to prepend
  287. * @param array $attributes Its attributes
  288. */
  289. public function prependIcon($icon, $attributes = array(), $iconSettings = array())
  290. {
  291. $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
  292. $this->prepend($icon);
  293. }
  294. /**
  295. * Append an icon to a field
  296. *
  297. * @param string $icon The icon to prepend
  298. * @param array $attributes Its attributes
  299. */
  300. public function appendIcon($icon, $attributes = array(), $iconSettings = array())
  301. {
  302. $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings);
  303. $this->append($icon);
  304. }
  305. ////////////////////////////////////////////////////////////////////
  306. //////////////////////////////// HELPERS ///////////////////////////
  307. ////////////////////////////////////////////////////////////////////
  308. /**
  309. * Get the errors for the group
  310. *
  311. * @return string
  312. */
  313. public function getErrors()
  314. {
  315. $errors = '';
  316. if (!self::$opened) {
  317. // for non-custom groups, normal error handling applies
  318. $errors = $this->app['former']->getErrors();
  319. } elseif (!empty($this->validations)) {
  320. // error handling only when validations specified for custom groups
  321. foreach ($this->validations as $validation) {
  322. $errors .= $this->app['former']->getErrors($validation);
  323. }
  324. }
  325. return $errors;
  326. }
  327. /**
  328. * Wraps content in a group
  329. *
  330. * @param string $contents The content
  331. * @param string $label The label to add
  332. *
  333. * @return string A group
  334. */
  335. public function wrap($contents, $label = null)
  336. {
  337. $group = $this->open();
  338. $group .= $label;
  339. $group .= $this->app['former.framework']->wrapField($contents);
  340. $group .= $this->close();
  341. return $group;
  342. }
  343. /**
  344. * Prints out the current label
  345. *
  346. * @param string $field The field to create a label for
  347. *
  348. * @return string A <label> tag
  349. */
  350. protected function getLabel($field = null)
  351. {
  352. // Don't create a label if none exist
  353. if (!$field or !$this->label) {
  354. return null;
  355. }
  356. // Wrap label in framework classes
  357. $this->label->addClass($this->app['former.framework']->getLabelClasses());
  358. $this->label = $this->app['former.framework']->createLabelOf($field, $this->label);
  359. $this->label = $this->app['former.framework']->wrapLabel($this->label);
  360. return $this->label;
  361. }
  362. /**
  363. * Prints out the current help
  364. *
  365. * @return string A .help-block or .help-inline
  366. */
  367. protected function getHelp()
  368. {
  369. $inline = array_get($this->help, 'inline');
  370. $block = array_get($this->help, 'block');
  371. // Replace help text with error if any found
  372. $errors = $this->app['former']->getErrors();
  373. if ($errors and $this->app['former']->getOption('error_messages')) {
  374. $inline = $this->app['former.framework']->createHelp($errors);
  375. }
  376. return join(null, array($inline, $block));
  377. }
  378. /**
  379. * Format the field with prepended/appended elements
  380. *
  381. * @param Field $field The field to format
  382. *
  383. * @return string Field plus supplementary elements
  384. */
  385. protected function prependAppend($field)
  386. {
  387. if (!$this->prepend and !$this->append) {
  388. return $field->render();
  389. }
  390. return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append);
  391. }
  392. /**
  393. * Place elements around the field
  394. *
  395. * @param array $items An array of items to place
  396. * @param string $place Where they should end up (prepend|append)
  397. */
  398. protected function placeAround($items, $place)
  399. {
  400. // Iterate over the items and place them where they should
  401. foreach ((array) $items as $item) {
  402. $item = $this->app['former.framework']->placeAround($item);
  403. $this->{$place}[] = $item;
  404. }
  405. }
  406. }