PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/fields/class-gf-field-multiselect.php

https://gitlab.com/BenjaminHolm/gravity-forms
PHP | 336 lines | 128 code | 38 blank | 170 comment | 22 complexity | af891b55adbd2626660cbaccaa5a0113 MD5 | raw file
  1. <?php
  2. // If the GF_Field class isn't available, bail.
  3. if ( ! class_exists( 'GFForms' ) ) {
  4. die();
  5. }
  6. /**
  7. * Class GF_Field_MultiSelect
  8. *
  9. * Allows the creation of multiselect fields.
  10. *
  11. * @since Unknown
  12. *
  13. * @uses GF_Field
  14. */
  15. class GF_Field_MultiSelect extends GF_Field {
  16. public $type = 'multiselect';
  17. /**
  18. * Returns the field title.
  19. *
  20. * @since Unknown
  21. * @access public
  22. *
  23. * @return string The field title. Escaped.
  24. */
  25. public function get_form_editor_field_title() {
  26. return esc_attr__( 'Multi Select', 'gravityforms' );
  27. }
  28. /**
  29. * Returns the class names of the settings which should be available on the field in the form editor.
  30. *
  31. * @since Unknown
  32. * @access public
  33. *
  34. * @return array Settings available within the field editor.
  35. */
  36. function get_form_editor_field_settings() {
  37. return array(
  38. 'conditional_logic_field_setting',
  39. 'prepopulate_field_setting',
  40. 'error_message_setting',
  41. 'enable_enhanced_ui_setting',
  42. 'label_setting',
  43. 'label_placement_setting',
  44. 'admin_label_setting',
  45. 'size_setting',
  46. 'choices_setting',
  47. 'rules_setting',
  48. 'visibility_setting',
  49. 'description_setting',
  50. 'css_class_setting',
  51. );
  52. }
  53. /**
  54. * Indicates this field type can be used when configuring conditional logic rules.
  55. *
  56. * @return bool
  57. */
  58. public function is_conditional_logic_supported() {
  59. return true;
  60. }
  61. /**
  62. * Returns the field inner markup.
  63. *
  64. * @since Unknown
  65. * @access public
  66. *
  67. * @uses GF_Field_MultiSelect::is_entry_detail()
  68. * @uses GF_Field_MultiSelect::is_form_editor()
  69. * @uses GF_Field_MultiSelect::get_conditional_logic_event()
  70. * @uses GF_Field_MultiSelect::get_tabindex()
  71. *
  72. * @param array $form The Form Object currently being processed.
  73. * @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
  74. * @param null|array $entry Null or the Entry Object currently being edited.
  75. *
  76. * @return string The field input HTML markup.
  77. */
  78. public function get_field_input( $form, $value = '', $entry = null ) {
  79. $form_id = absint( $form['id'] );
  80. $is_entry_detail = $this->is_entry_detail();
  81. $is_form_editor = $this->is_form_editor();
  82. $id = $this->id;
  83. $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id";
  84. $logic_event = $this->get_conditional_logic_event( 'keyup' );
  85. $size = $this->size;
  86. $class_suffix = $is_entry_detail ? '_admin' : '';
  87. $class = $size . $class_suffix;
  88. $css_class = trim( esc_attr( $class ) . ' gfield_select' );
  89. $tabindex = $this->get_tabindex();
  90. $disabled_text = $is_form_editor ? 'disabled="disabled"' : '';
  91. /**
  92. * Allow the placeholder used by the enhanced ui to be overridden
  93. *
  94. * @since 1.9.14 Third parameter containing the field ID was added.
  95. * @since Unknown
  96. *
  97. * @param string $placeholder The placeholder text.
  98. * @param integer $form_id The ID of the current form.
  99. */
  100. $placeholder = gf_apply_filters( array(
  101. 'gform_multiselect_placeholder',
  102. $form_id,
  103. $this->id
  104. ), __( 'Click to select...', 'gravityforms' ), $form_id, $this );
  105. $placeholder = $this->enableEnhancedUI ? "data-placeholder='" . esc_attr( $placeholder ) . "'" : '';
  106. $size = $this->multiSelectSize;
  107. if ( empty( $size ) ) {
  108. $size = 7;
  109. }
  110. return sprintf( "<div class='ginput_container ginput_container_multiselect'><select multiple='multiple' {$placeholder} size='{$size}' name='input_%d[]' id='%s' {$logic_event} class='%s' $tabindex %s>%s</select></div>", $id, esc_attr( $field_id ), $css_class, $disabled_text, $this->get_choices( $value ) );
  111. }
  112. /**
  113. * Helper for retrieving the markup for the choices.
  114. *
  115. * @since Unknown
  116. * @access public
  117. *
  118. * @uses GFCommon::get_select_choices()
  119. *
  120. * @param string|array $value The field value. From default/dynamic population, $_POST, or a resumed incomplete submission.
  121. *
  122. * @return string Returns the choices available within the multi-select field.
  123. */
  124. public function get_choices( $value ) {
  125. return GFCommon::get_select_choices( $this, $value, false );
  126. }
  127. /**
  128. * Format the entry value for display on the entries list page.
  129. *
  130. * @since Unknown
  131. * @access public
  132. *
  133. * @param string|array $value The field value.
  134. * @param array $entry The Entry Object currently being processed.
  135. * @param string $field_id The field or input ID currently being processed.
  136. * @param array $columns The properties for the columns being displayed on the entry list page.
  137. * @param array $form The Form Object currently being processed.
  138. *
  139. * @return string $value The value of the field. Escaped.
  140. */
  141. public function get_value_entry_list( $value, $entry, $field_id, $columns, $form ) {
  142. // Add space after comma-delimited values.
  143. $value = implode( ', ', explode( ',', $value ) );
  144. return esc_html( $value );
  145. }
  146. /**
  147. * Format the entry value for display on the entry detail page and for the {all_fields} merge tag.
  148. *
  149. * @since Unknown
  150. * @access public
  151. *
  152. * @uses GFCommon::selection_display()
  153. *
  154. * @param string|array $value The field value.
  155. * @param string $currency The entry currency code.
  156. * @param bool|false $use_text When processing choice based fields should the choice text be returned instead of the value.
  157. * @param string $format The format requested for the location the merge is being used. Possible values: html, text or url.
  158. * @param string $media The location where the value will be displayed. Possible values: screen or email.
  159. *
  160. * @return string The list items, stored within an unordered list.
  161. */
  162. public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
  163. if ( empty( $value ) || $format == 'text' ) {
  164. return $value;
  165. }
  166. $value = explode( ',', $value );
  167. $items = '';
  168. foreach ( $value as $item ) {
  169. $item_value = GFCommon::selection_display( $item, $this, $currency, $use_text );
  170. $items .= '<li>' . esc_html( $item_value ) . '</li>';
  171. }
  172. return "<ul class='bulleted'>{$items}</ul>";
  173. }
  174. /**
  175. * Format the value before it is saved to the Entry Object.
  176. *
  177. * @since Unknown
  178. * @access public
  179. *
  180. * @uses GF_Field_MultiSelect::sanitize_entry_value()
  181. *
  182. * @param array|string $value The value to be saved.
  183. * @param array $form The Form Object currently being processed.
  184. * @param string $input_name The input name used when accessing the $_POST.
  185. * @param int $lead_id The ID of the Entry currently being processed.
  186. * @param array $lead The Entry Object currently being processed.
  187. *
  188. * @return string $value The field value. Comma separated if an array.
  189. */
  190. public function get_value_save_entry( $value, $form, $input_name, $lead_id, $lead ) {
  191. if ( is_array( $value ) ) {
  192. foreach ( $value as &$v ) {
  193. $v = $this->sanitize_entry_value( $v, $form['id'] );
  194. }
  195. } else {
  196. $value = $this->sanitize_entry_value( $value, $form['id'] );
  197. }
  198. return empty( $value ) ? '' : is_array( $value ) ? implode( ',', $value ) : $value;
  199. }
  200. /**
  201. * Format the entry value for when the field/input merge tag is processed.
  202. *
  203. * @since Unknown
  204. * @access public
  205. *
  206. * @uses GFCommon::format_post_category()
  207. * @uses GFCommon::format_variable_value()
  208. * @uses GFCommon::selection_display()
  209. * @uses GFCommon::implode_non_blank()
  210. *
  211. * @param string|array $value The field value. Depending on the location the merge tag is being used the following functions may have already been applied to the value: esc_html, nl2br, and urlencode.
  212. * @param string $input_id The field or input ID from the merge tag currently being processed.
  213. * @param array $entry The Entry Object currently being processed.
  214. * @param array $form The Form Object currently being processed.
  215. * @param string $modifier The merge tag modifier. e.g. value
  216. * @param string|array $raw_value The raw field value from before any formatting was applied to $value.
  217. * @param bool $url_encode Indicates if the urlencode function may have been applied to the $value.
  218. * @param bool $esc_html Indicates if the esc_html function may have been applied to the $value.
  219. * @param string $format The format requested for the location the merge is being used. Possible values: html, text or url.
  220. * @param bool $nl2br Indicates if the nl2br function may have been applied to the $value.
  221. *
  222. * @return string $return The merge tag value.
  223. */
  224. public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br ) {
  225. $items = explode( ',', $raw_value );
  226. if ( $this->type == 'post_category' ) {
  227. $use_id = $modifier == 'id';
  228. if ( is_array( $items ) ) {
  229. foreach ( $items as &$item ) {
  230. $cat = GFCommon::format_post_category( $item, $use_id );
  231. $item = GFCommon::format_variable_value( $cat, $url_encode, $esc_html, $format );
  232. }
  233. }
  234. } elseif ( $modifier != 'value' ) {
  235. foreach ( $items as &$item ) {
  236. $item = GFCommon::selection_display( $item, $this, rgar( $entry, 'currency' ), true );
  237. $item = GFCommon::format_variable_value( $item, $url_encode, $esc_html, $format );
  238. }
  239. }
  240. $return = GFCommon::implode_non_blank( ', ', $items );
  241. if ( $format == 'html' || $esc_html ) {
  242. $return = esc_html( $return );
  243. }
  244. return $return;
  245. }
  246. /**
  247. * Format the entry value before it is used in entry exports and by framework add-ons using GFAddOn::get_field_value().
  248. *
  249. * @since Unknown
  250. * @access public
  251. *
  252. * @uses GFCommon::selection_display()
  253. * @uses GFCommon::implode_non_blank()
  254. *
  255. * @param array $entry The entry currently being processed.
  256. * @param string $input_id The field or input ID.
  257. * @param bool|false $use_text When processing choice based fields should the choice text be returned instead of the value.
  258. * @param bool|false $is_csv Is the value going to be used in the .csv entries export?
  259. *
  260. * @return string $value The value of a field from an export file.
  261. */
  262. public function get_value_export( $entry, $input_id = '', $use_text = false, $is_csv = false ) {
  263. if ( empty( $input_id ) ) {
  264. $input_id = $this->id;
  265. }
  266. $value = rgar( $entry, $input_id );
  267. if ( ! empty( $value ) && ! $is_csv ) {
  268. $items = explode( ',', $value );
  269. foreach ( $items as &$item ) {
  270. $item = GFCommon::selection_display( $item, $this, rgar( $entry, 'currency' ), $use_text );
  271. }
  272. $value = GFCommon::implode_non_blank( ', ', $items );
  273. }
  274. return $value;
  275. }
  276. /**
  277. * Forces settings into expected values while saving the form object.
  278. *
  279. * No escaping should be done at this stage to prevent double escaping on output.
  280. *
  281. * Currently called only for forms created after version 1.9.6.10.
  282. *
  283. * @since Unknown
  284. * @access public
  285. *
  286. * @return void
  287. *
  288. */
  289. public function sanitize_settings() {
  290. parent::sanitize_settings();
  291. $this->enableEnhancedUI = (bool) $this->enableEnhancedUI;
  292. if ( $this->type === 'post_category' ) {
  293. $this->displayAllCategories = (bool) $this->displayAllCategories;
  294. }
  295. }
  296. }
  297. // Register the new field type.
  298. GF_Fields::register( new GF_Field_MultiSelect() );