/src/helper/fields/Field_Post_Category.php

  1. <?php 
  2.  
  3. namespace GFPDF\Helper\Fields; 
  4.  
  5. use GFPDF\Helper\Helper_Abstract_Form; 
  6. use GFPDF\Helper\Helper_Misc; 
  7. use GFPDF\Helper\Helper_Abstract_Fields; 
  8.  
  9. use GFFormsModel; 
  10. use GFCommon; 
  11.  
  12. use Exception; 
  13.  
  14. /** 
  15. * Gravity Forms Field 
  16. * 
  17. * @package Gravity PDF 
  18. * @copyright Copyright (c) 2016, Blue Liquid Designs 
  19. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License 
  20. * @since 4.0 
  21. */ 
  22.  
  23. /** Exit if accessed directly */ 
  24. if ( ! defined( 'ABSPATH' ) ) { 
  25. exit; 
  26.  
  27. /** 
  28. This file is part of Gravity PDF. 
  29.   
  30. Gravity PDF * Copyright (C) 2016, Blue Liquid Designs 
  31.   
  32. This program is free software; you can redistribute it and/or modify 
  33. it under the terms of the GNU General Public License as published by 
  34. the Free Software Foundation; either version 2 of the License, or 
  35. (at your option) any later version. 
  36.   
  37. This program is distributed in the hope that it will be useful,  
  38. but WITHOUT ANY WARRANTY; without even the implied warranty of 
  39. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  40. GNU General Public License for more details. 
  41.   
  42. You should have received a copy of the GNU General Public License 
  43. along with this program; if not, write to the Free Software 
  44. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
  45. */ 
  46.  
  47. /** 
  48. * Controls the display and output of a Gravity Form field 
  49. * 
  50. * @since 4.0 
  51. */ 
  52. class Field_Post_Category extends Helper_Abstract_Fields { 
  53.  
  54. /** 
  55. * Check the appropriate variables are parsed in send to the parent construct 
  56. * 
  57. * @param object $field The GF_Field_* Object 
  58. * @param array $entry The Gravity Forms Entry 
  59. * 
  60. * @param \GFPDF\Helper\Helper_Abstract_Form $gform 
  61. * @param \GFPDF\Helper\Helper_Misc $misc 
  62. * 
  63. * @throws Exception 
  64. * 
  65. * @since 4.0 
  66. */ 
  67. public function __construct( $field, $entry, Helper_Abstract_Form $gform, Helper_Misc $misc ) { 
  68.  
  69. /** call our parent method */ 
  70. parent::__construct( $field, $entry, $gform, $misc ); 
  71.  
  72. /** 
  73. * Category can be multiple field types 
  74. * eg. Select, MultiSelect, Radio, Dropdown 
  75. */ 
  76. $class = $this->misc->get_field_class( $field->inputType ); 
  77.  
  78. try { 
  79. /** check load our class */ 
  80. if ( class_exists( $class ) ) { 
  81.  
  82. /** See https://gravitypdf.com/documentation/v4/gfpdf_field_class/ for more details about these filters */ 
  83. $this->fieldObject = apply_filters( 'gfpdf_field_class', new $class( $field, $entry, $gform, $misc ), $field, $entry, $this->form ); 
  84. $this->fieldObject = apply_filters( 'gfpdf_field_class_' . $field->inputType, $this->fieldObject, $field, $entry, $this->form ); 
  85. } else { 
  86. throw new Exception( 'Class not found' ); 
  87. } catch ( Exception $e ) { 
  88. /** Exception thrown. Load generic field loader */ 
  89. $this->fieldObject = apply_filters( 'gfpdf_field_default_class', new Field_Default( $field, $entry, $gform, $misc ), $field, $entry, $this->form ); 
  90.  
  91. /** force the fieldObject value cache */ 
  92. $this->value(); 
  93.  
  94. /** 
  95. * Used to check if the current field has a value 
  96. * 
  97. * @since 4.0 
  98. */ 
  99. public function is_empty() { 
  100. return $this->fieldObject->is_empty(); 
  101.  
  102. /** 
  103. * Return the HTML form data 
  104. * 
  105. * @return array 
  106. * 
  107. * @since 4.0 
  108. */ 
  109. public function form_data() { 
  110.  
  111. $field_value = $this->value(); 
  112. $label = GFFormsModel::get_label( $this->field ); 
  113. $data = []; 
  114. $value = [ 
  115. 'value' => '',  
  116. 'label' => '',  
  117. ]; 
  118.  
  119. /** If Radio of Select boxes */ 
  120. if ( ! isset( $field_value[0] ) ) { 
  121.  
  122. /** Set up our basic values */ 
  123. $value['value'] = ( isset( $field_value['value'] ) && ! is_array( $field_value['value'] ) ) ? $field_value['value'] : ''; 
  124. $value['label'] = ( isset( $field_value['label'] ) && ! is_array( $field_value['label'] ) ) ? $field_value['label'] : ''; 
  125.  
  126. } else { /** If Checkboxes or Multiselects */ 
  127.  
  128. /** Loop through the results and store in array */ 
  129. foreach ( $field_value as $item ) { 
  130. $value['value'][] = $item['value']; 
  131. $value['label'][] = $item['label']; 
  132.  
  133. $data['field'][ $this->field->id . '.' . $label ] = $value['value']; 
  134. $data['field'][ $this->field->id ] = $value['value']; 
  135. $data['field'][ $label ] = $value['value']; 
  136.  
  137. /** Name Format */ 
  138. $data['field'][ $this->field->id . '.' . $label . '_name' ] = $value['label']; 
  139. $data['field'][ $this->field->id . '_name' ] = $value['label']; 
  140. $data['field'][ $label . '_name' ] = $value['label']; 
  141.  
  142. return $data; 
  143.  
  144. /** 
  145. * Display the HTML version of this field 
  146. * 
  147. * @param string $value 
  148. * @param bool $label 
  149. * 
  150. * @return string 
  151. * 
  152. * @since 4.0 
  153. */ 
  154. public function html( $value = '', $label = true ) { 
  155. return $this->fieldObject->html(); 
  156.  
  157. /** 
  158. * Get the standard GF value of this field 
  159. * 
  160. * @return string|array 
  161. * 
  162. * @since 4.0 
  163. */ 
  164. public function value() { 
  165. if ( $this->fieldObject->has_cache() ) { 
  166. return $this->fieldObject->cache(); 
  167.  
  168. /** get the value from the correct field object */ 
  169. $items = $this->fieldObject->value(); 
  170.  
  171. /** 
  172. * Standardise the $items format 
  173. * The Radio / Select box will return a single-dimensional array,  
  174. * while checkbox and multiselect will not. 
  175. */ 
  176. $single_dimension = false; 
  177. if ( ! isset( $items[0] ) ) { /** convert single-dimensional array to multi-dimensional */ 
  178. $items = [ $items ]; 
  179. $single_dimension = true; 
  180.  
  181. /** Loop through standardised array and convert the label / value to their appropriate category */ 
  182. foreach ( $items as &$val ) { 
  183.  
  184. /** Process the category label */ 
  185. if ( isset( $val['label'] ) ) { 
  186. $val['label'] = GFCommon::prepare_post_category_value( $val['label'], $this->field ); 
  187.  
  188. if ( is_array( $val['label'] ) ) { 
  189. $val['label'] = ( isset( $val['label'][0] ) ) ? $val['label'][0] : ''; 
  190.  
  191. $val['label'] = esc_html( $val['label'] ); 
  192.  
  193. /** process the category value */ 
  194. if ( isset( $val['value'] ) ) { 
  195. $val['value'] = GFCommon::prepare_post_category_value( $val['value'], $this->field, 'conditional_logic' ); 
  196.  
  197. if ( is_array( $val['value'] ) ) { 
  198. $val['value'] = ( isset( $val['value'][0] ) ) ? $val['value'][0] : ''; 
  199.  
  200. $val['value'] = esc_html( $val['value'] ); 
  201.  
  202. /** 
  203. * Return in the appropriate format. 
  204. * Select / Radio Buttons will not have a multidimensional array 
  205. */ 
  206. if ( $single_dimension ) { 
  207. $items = $items[0]; 
  208.  
  209. /** force the fieldObject cache to be set so it doesn't run their 'value' method directly */ 
  210. $this->fieldObject->cache( $items ); 
  211.  
  212. return $this->fieldObject->cache(); 
.