GFPDFHelperHelper_Field_Container

The Gravity PDF GFPDF Helper Field Container class.

Defined (1)

The class is defined in the following location(s).

/src/helper/Helper_Field_Container.php  
  1. class Helper_Field_Container { 
  2.  
  3. /** 
  4. * Holds the current width of our container based on the field passed in 
  5. * The value is out of 100 
  6. * @var integer 
  7. * @since 4.0 
  8. */ 
  9. private $current_width = 0; 
  10.  
  11. /** 
  12. * Boolean value to tell if the element is currently opened 
  13. * @var boolean 
  14. * @since 4.0 
  15. */ 
  16. private $currently_open = false; 
  17.  
  18. /** 
  19. * Matches class names to width percentages 
  20. * @var array 
  21. * @since 4.0 
  22. */ 
  23. private $class_map = [ 
  24. 'gf_left_half' => 50,  
  25. 'gf_right_half' => 50,  
  26. 'gf_left_third' => 33.3,  
  27. 'gf_middle_third' => 33.3,  
  28. 'gf_right_third' => 33.3,  
  29. 'gf_first_quarter' => 25,  
  30. 'gf_second_quarter' => 25,  
  31. 'gf_third_quarter' => 25,  
  32. 'gf_fourth_quarter' => 25,  
  33. ]; 
  34.  
  35. /** 
  36. * The HTML tag used when opening the container 
  37. * @var string 
  38. * @since 4.0 
  39. */ 
  40. private $open_tag = '<div class="row-separator">'; 
  41.  
  42. /** 
  43. * The HTML tag used when closing the container 
  44. * @var string 
  45. * @since 4.0 
  46. */ 
  47. private $close_tag = '</div>'; 
  48.  
  49. /** 
  50. * The Gravity Form fields we should not wrap in a container 
  51. * @var array 
  52. * @since 4.0 
  53. */ 
  54. private $skip_fields = [ 
  55. 'page',  
  56. 'section',  
  57. 'html',  
  58. ]; 
  59.  
  60. /** 
  61. * Holds the number of times a new row has been open 
  62. * @var int 
  63. * @since 4.0 
  64. */ 
  65. private $counter = 0; 
  66.  
  67. /** 
  68. * Set up the object 
  69. * @param array $config Allow user to override the open / close tag and which fields are skipped 
  70. * @since 4.0 
  71. */ 
  72. public function __construct( $config = [] ) { 
  73. if ( isset( $config['open_tag'] ) ) { 
  74. $this->open_tag = $config['open_tag']; 
  75.  
  76. if ( isset( $config['close_tag'] ) ) { 
  77. $this->close_tag = $config['close_tag']; 
  78.  
  79. if ( isset( $config['skip_fields'] ) ) { 
  80. $this->skip_fields = $config['skip_fields']; 
  81.  
  82.  
  83. /** 
  84. * Handles the opening and closing of our container 
  85. * @param GF_Field $field The Gravity Form field currently being processed 
  86. * @return void 
  87. * @since 4.0 
  88. */ 
  89. public function generate( GF_Field $field ) { 
  90.  
  91. /** Check if we are processing a field that should not be floated and treat it as a 100% field */ 
  92. $this->process_skipped_fields( $field ); 
  93.  
  94. /** Check if we need to close the container */ 
  95. if ( $this->currently_open ) { 
  96. $this->handle_open_container( $field ); 
  97.  
  98. /** Open the tag if not currently opened*/ 
  99. if ( ! $this->currently_open ) { 
  100. $this->handle_closed_container( $field ); 
  101.  
  102. /** 
  103. * Close the current container if still open. 
  104. * This is usually called publically after the form loop 
  105. * @return void 
  106. * @since 4.0 
  107. */ 
  108. public function close() { 
  109. if ( $this->currently_open ) { 
  110. $this->close_container(); 
  111. $this->reset(); 
  112.  
  113. /** 
  114. * Will check if the current field will fit in the open row, or if a new row needs to be open 
  115. * to accomidate the field. 
  116. * @param GF_Field $field The Gravity Form field currently being processed 
  117. * @return boolean 
  118. * @since 4.0 
  119. */ 
  120. public function does_fit_in_row( GF_Field $field ) { 
  121.  
  122. if ( true === $this->currently_open ) { 
  123. $width = $this->get_field_width( $field->cssClass ); /** current field width */ 
  124.  
  125. /** Check if the new field will fit in the row */ 
  126. if ( 100 >= ( $this->current_width + $width ) ) { 
  127. return true; 
  128.  
  129. return false; 
  130.  
  131. /** 
  132. * Output placeholder HTML if empty or hidden field is part of CSS Ready Class columns 
  133. * and the row is currently open and the field will fit in that row without opening another row 
  134. * @param GF_Field $field The Gravity Form field currently being processed 
  135. * @return void 
  136. */ 
  137. public function maybe_display_faux_column( GF_Field $field ) { 
  138.  
  139. /** Check if we should create a placeholder column */ 
  140. if ( $this->does_fit_in_row( $field ) ) { 
  141. echo '<div id="field-' . $field->id . '" class="gfpdf-column-placeholder gfpdf-field ' . $field->cssClass . '"></div>'; 
  142.  
  143. /** Increase column width */ 
  144. $this->increment_width( $field->cssClass ); 
  145.  
  146. /** 
  147. * Open the container 
  148. * @param GF_Field $field The Gravity Form field currently being processed 
  149. * @return void 
  150. * @since 4.0 
  151. */ 
  152. private function handle_closed_container( GF_Field $field ) { 
  153. $this->start(); 
  154. $this->open_container(); 
  155. $this->increment_width( $field->cssClass ); 
  156.  
  157. /** 
  158. * Determine if we should close a container based on its classes 
  159. * @param GF_Field $field The Gravity Form field currently being processed 
  160. * @return void 
  161. * @since 4.0 
  162. */ 
  163. private function handle_open_container( GF_Field $field ) { 
  164.  
  165. /** if the current field width is more than 100 we will close the container */ 
  166. if ( false === $this->does_fit_in_row( $field ) ) { 
  167. $this->close(); 
  168. } else { 
  169. $this->increment_width( $field->cssClass ); 
  170.  
  171. /** 
  172. * Process our skipped Gravity Form fields (close the container if needed) 
  173. * @param GF_Field $field The Gravity Form field currently being processed 
  174. * @return boolean true if we processed a skipped field, false otherwise 
  175. * @since 4.0 
  176. */ 
  177. private function process_skipped_fields( GF_Field $field ) { 
  178. /** if we have a skipped field and the container is open we will close it */ 
  179. if ( in_array( $field->type, $this->skip_fields ) ) { 
  180. $this->strip_field_of_any_classmaps( $field ); 
  181. $this->close(); 
  182.  
  183. return true; 
  184.  
  185. return false; 
  186.  
  187. /** 
  188. * Remove any mapped classes from our skipped fields 
  189. * @param GF_Field $field The Gravity Form field currently being processed 
  190. * @return void 
  191. * @since 4.0 
  192. */ 
  193. private function strip_field_of_any_classmaps( GF_Field $field ) { 
  194. $field->cssClass = str_replace( array_keys( $this->class_map ), ' ', $field->cssClass ); 
  195.  
  196. /** 
  197. * Output the open tag 
  198. * @return void 
  199. * @since 4.0 
  200. */ 
  201. private function open_container() { 
  202.  
  203. $class = $this->is_row_odd_or_even(); 
  204. echo str_replace( 'row-separator', 'row-separator ' . $class, $this->open_tag ); 
  205.  
  206. $this->increment_row_counter(); 
  207.  
  208. /** 
  209. * Output the close tag 
  210. * @return void 
  211. * @since 4.0 
  212. */ 
  213. private function close_container() { 
  214. echo $this->close_tag; 
  215.  
  216. /** 
  217. * Mark our class as currently being open 
  218. * @return void 
  219. * @since 4.0 
  220. */ 
  221. private function start() { 
  222. $this->currently_open = true; 
  223.  
  224. /** 
  225. * Reset our class back to its original state 
  226. * @return void 
  227. * @since 4.0 
  228. */ 
  229. private function reset() { 
  230. $this->currently_open = false; 
  231. $this->current_width = 0; 
  232.  
  233. /** 
  234. * Increment our current field width 
  235. * @param string $classes The field classes 
  236. * @return void 
  237. * @since 4.0 
  238. */ 
  239. private function increment_width( $classes ) { 
  240. $this->current_width += $this->get_field_width( $classes ); 
  241.  
  242. /** 
  243. * Loop through all classes and return our class map if found, or 100 
  244. * @param String $classes The field classes 
  245. * @return integer The field width based on assigned class 
  246. * @since 4.0 
  247. */ 
  248. private function get_field_width( $classes ) { 
  249. $classes = explode( ' ', $classes ); 
  250.  
  251. foreach ( $classes as $class ) { 
  252. if ( isset ( $this->class_map[ $class ] ) ) { 
  253. /** return field width */ 
  254. return $this->class_map[ $class ]; 
  255.  
  256. /** no match, so assuming full width */ 
  257.  
  258. return 100; 
  259.  
  260. /** 
  261. * Checks if the row counter is currently odd or even 
  262. * @return string 
  263. * @since 4.0 
  264. */ 
  265. private function is_row_odd_or_even() { 
  266. return ( $this->counter % 2 ) ? 'even' : 'odd'; 
  267.  
  268. /** 
  269. * Increases the internal row counter 
  270. * @return void 
  271. * @since 4.0 
  272. */ 
  273. private function increment_row_counter() { 
  274. $this->counter++;