SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/LightweightForm.php

Copyright (C) 2017 Karmabunny Pty Ltd.

This file is a part of SproutCMS.

SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.

For more information, visit <http://getsproutcms.com>.

This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team
  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers;
  17.  
  18.  
  19.  
  20. /**
  21.  * A variation of the {@see Form} helper which doesn't output errors, labels or helptext
  22.  * Wraps form fields (e.g. from {@see Fb}) with additional HTML.
  23.  */
  24. class LightweightForm extends Form
  25. {
  26.  
  27. /**
  28.   * Return HTML for a 'plain' field, i.e. one which doesn't require a FIELDSET wrapped around it.
  29.   *
  30.   * The main wrapping DIV will contain additional classes if the field is required, disabled or has an error.
  31.   * A class is also output for hte field method name (if the name contains "Sprout\Helpers\Fb::" this is removed)
  32.   * If the field has an explicit ID set, that will be added as a class on the wrapper too.
  33.   *
  34.   * @param callable $method The actual field rendering method
  35.   * @param string $name The field name - this is passed to the rendering method
  36.   * @param array $attrs The field attrs - this is passed to the rendering method
  37.   * @param array $options The field options - this is passed to the rendering method
  38.   * @return string HTML
  39.   */
  40. public static function fieldPlain(callable $method, $name, array $attrs = [], array $options = [])
  41. {
  42. $classes = array('field-element', 'field-element--lightweight');
  43. $classes[] = 'field-element--' . self::fieldMethodClass($method);
  44. if (isset($attrs['id'])) {
  45. $classes[] = 'field-element--id-' . Enc::id($attrs['id']);
  46. }
  47. if (self::$next_required) {
  48. $classes[] = 'field-element--required';
  49. }
  50. if (isset($attrs['disabled']) or in_array('disabled', $attrs, true)) {
  51. $classes[] = 'field-element--disabled';
  52. }
  53. if (isset(self::$errors[$name])) {
  54. $classes[] = 'field-element--error';
  55. }
  56. if (isset($attrs['-wrapper-class'])) {
  57. if (is_string($attrs['-wrapper-class'])) {
  58. $attrs['-wrapper-class'] = preg_split('/\s+/', $attrs['-wrapper-class']);
  59. }
  60. foreach ($attrs['-wrapper-class'] as $class) {
  61. $classes[] = 'field-element--' . $class;
  62. }
  63. unset($attrs['-wrapper-class']);
  64. }
  65.  
  66. $out = '<div class="' . Enc::html(implode(' ', $classes)) . '">';
  67.  
  68. if (!isset($attrs['id'])) {
  69. $attrs['id'] = self::genId();
  70. }
  71.  
  72. $field_html = call_user_func($method, $name, $attrs, $options);
  73.  
  74. // Field itself
  75. $out .= '<div class="field-input">';
  76. $out .= $field_html;
  77. $out .= '</div>';
  78.  
  79. $out .= '</div>';
  80. $out .= PHP_EOL . PHP_EOL;
  81.  
  82. self::resetField();
  83.  
  84. return $out;
  85. }
  86.  
  87.  
  88. /**
  89.   * Return HTML for a field wrapped in a FIELDSET
  90.   *
  91.   * The main wrapping DIV will contain additional classes if the field is required, disabled or has an error.
  92.   * A class is also output for hte field method name (if the name contains "Sprout\Helpers\Fb::" this is removed)
  93.   * If the field has an explicit ID set, that will be added as a class on the wrapper too.
  94.   *
  95.   * @param callable $method The actual field rendering method
  96.   * @param string $name The field name - this is passed to the rendering method
  97.   * @param array $attrs The field attrs - this is passed to the rendering method
  98.   * @param array $options The field options - this is passed to the rendering method
  99.   * @return string HTML
  100.   */
  101. public static function fieldFieldset(callable $method, $name, array $attrs = [], array $options = [])
  102. {
  103. $classes = array('field-element', 'field-element--lightweight');
  104. $classes[] = 'field-element--' . self::fieldMethodClass($method);
  105. if (isset($attrs['id'])) {
  106. $classes[] = 'field-element--id-' . Enc::id($attrs['id']);
  107. }
  108. if (self::$next_required) {
  109. $classes[] = 'field-element--required';
  110. }
  111. if (isset($attrs['disabled']) or in_array('disabled', $attrs, true)) {
  112. $classes[] = 'field-element--disabled';
  113. }
  114. if (isset(self::$errors[$name])) {
  115. $classes[] = 'field-element--error';
  116. }
  117. if (isset($attrs['-wrapper-class'])) {
  118. if (is_string($attrs['-wrapper-class'])) {
  119. $attrs['-wrapper-class'] = preg_split('/\s+/', $attrs['-wrapper-class']);
  120. }
  121. foreach ($attrs['-wrapper-class'] as $class) {
  122. $classes[] = 'field-element--' . $class;
  123. }
  124. unset($attrs['-wrapper-class']);
  125. }
  126.  
  127. if (!isset($attrs['id'])) {
  128. $attrs['id'] = self::genId();
  129. }
  130.  
  131. $out = '<div class="' . Enc::html(implode(' ', $classes)) . '">';
  132. $out .= '<fieldset class="fieldset--' . self::fieldMethodClass($method) . '">';
  133.  
  134. // Field itself
  135. $out .= '<div class="field-element__input-set">';
  136. $out .= call_user_func($method, $name, $attrs, $options);
  137. $out .= '</div>';
  138.  
  139. $out .= '</fieldset>';
  140.  
  141. $out .= '</div>';
  142. $out .= PHP_EOL . PHP_EOL;
  143.  
  144. self::resetField();
  145.  
  146. return $out;
  147. }
  148.  
  149. }
  150.  
  151.