Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for Multiple Choice and Image Choice fields #1573

Merged
merged 1 commit into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Helper/Fields/Field_Checkbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ public function form_data() {
*/
public function html( $value = '', $label = true ) {

$items = $this->value();
$value = apply_filters( 'gfpdf_show_field_value', false, $this->field, $items ); /* Set to `true` to show a field's value instead of the label */
$html = '';
$items = $this->value();
$show_value = apply_filters( 'gfpdf_show_field_value', false, $this->field, $items ); /* Set to `true` to show a field's value instead of the label */
$html = '';

/* Generate our drop down list */
/* Generate our dropdown list */
if ( count( $items ) > 0 ) {
$html = '<ul class="bulleted checkbox">';
$i = 1;
foreach ( $items as $item ) {
$sanitized_option = ( $value ) ? $item['value'] : $item['label'];
$html .= '<li id="field-' . $this->field->id . '-option-' . $i . '">' . $sanitized_option . '</li>';
$sanitized_option = $show_value ? $item['value'] : $item['label'];
$html .= '<li id="' . esc_attr( 'field-' . $this->field->id . '-option-' . $i ) . '">' . $sanitized_option . '</li>';
$i++;
}

Expand Down
102 changes: 102 additions & 0 deletions src/Helper/Fields/Field_Image_Choice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace GFPDF\Helper\Fields;

/**
* @package Gravity PDF
* @copyright Copyright (c) 2024, Blue Liquid Designs
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/

/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
exit;

Check warning on line 13 in src/Helper/Fields/Field_Image_Choice.php

View check run for this annotation

Codecov / codecov/patch

src/Helper/Fields/Field_Image_Choice.php#L13

Added line #L13 was not covered by tests
}

/**
* Controls the display and output of a Gravity Form field
*
* @since 6.12
*
* @property \GF_Field_Image_Choice $field
*/
class Field_Image_Choice extends Field_Multi_Choice {

/**
* Return the correct form data information for the selected fields
*
* @return array
*
* @since 6.12
*/
public function form_data() {
if ( method_exists( $this->fieldObject, 'form_data' ) ) {
$data = $this->fieldObject->form_data();
} else {
$data = parent::form_data();

Check warning on line 36 in src/Helper/Fields/Field_Image_Choice.php

View check run for this annotation

Codecov / codecov/patch

src/Helper/Fields/Field_Image_Choice.php#L36

Added line #L36 was not covered by tests
}

/* Add image URL / Paths */
$label = $this->get_label();
$choices = $this->get_value();

/* Treat radio and checkbox fields the same */
$choices = array_filter( ! is_array( $choices ) ? [ $choices ] : $choices );

foreach ( $choices as $choice ) {
$attachment_id = $this->get_attachment_id_from_value( $choice );
$attachment_data = $this->get_attachment_information( $attachment_id );

$data['field'][ $this->field->id . '.' . $label . '_image' ][] = $attachment_data;
$data['field'][ $this->field->id . '_image' ][] = $attachment_data;
$data['field'][ $label . '_image' ][] = $attachment_data;
}

return $data;
}

/**
* Search for a field choice that has the same value and return the attachment ID
*
* @param string $value
*
* @return int The attachment ID or 0 if not found
*
* @since 6.12
*/
protected function get_attachment_id_from_value( $value ) {
$choices = $this->field->choices;
if ( ! is_array( $choices ) ) {
return 0;

Check warning on line 70 in src/Helper/Fields/Field_Image_Choice.php

View check run for this annotation

Codecov / codecov/patch

src/Helper/Fields/Field_Image_Choice.php#L70

Added line #L70 was not covered by tests
}

foreach ( $choices as $choice ) {
$choice_value = $choice['value'] ?? '';
if ( $choice_value === $value ) {
return $choice['attachment_id'] ?? 0;
}
}

return 0;

Check warning on line 80 in src/Helper/Fields/Field_Image_Choice.php

View check run for this annotation

Codecov / codecov/patch

src/Helper/Fields/Field_Image_Choice.php#L80

Added line #L80 was not covered by tests
}

/**
* Return details about the full size image file
*
* @param int $attachment_id
*
* @return array
*
* @since 6.12
*/
protected function get_attachment_information( $attachment_id ) {
$image_url = wp_get_attachment_image_url( $attachment_id, 'full' );

return [
'attachment_id' => $attachment_id,
'url' => $image_url,
'path' => $this->misc->convert_url_to_path( $image_url ),
'alt' => get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ),
];
}
}
29 changes: 29 additions & 0 deletions src/Helper/Fields/Field_Multi_Choice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace GFPDF\Helper\Fields;

use GF_Field_Multiple_Choice;
use GFPDF\Helper\Helper_Abstract_Fields;
use GFPDF\Helper\Helper_Abstract_Fields_Input_Type;
use GFPDF\Helper\Helper_Abstract_Form;
use GFPDF\Helper\Helper_Misc;

/**
* @package Gravity PDF
* @copyright Copyright (c) 2024, Blue Liquid Designs
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/

/* Exit if accessed directly */
if ( ! defined( 'ABSPATH' ) ) {
exit;

Check warning on line 19 in src/Helper/Fields/Field_Multi_Choice.php

View check run for this annotation

Codecov / codecov/patch

src/Helper/Fields/Field_Multi_Choice.php#L19

Added line #L19 was not covered by tests
}

/**
* Controls the display and output of a Gravity Form field
*
* @since 6.12
*/
class Field_Multi_Choice extends Helper_Abstract_Fields_Input_Type {

}
102 changes: 3 additions & 99 deletions src/Helper/Fields/Field_Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace GFPDF\Helper\Fields;

use Exception;
use GFPDF\Helper\Helper_Abstract_Fields;
use GFPDF\Helper\Helper_Abstract_Form;
use GFPDF\Helper\Helper_Misc;
use GFPDF\Helper\Helper_Abstract_Fields_Input_Type;

/**
* @package Gravity PDF
Expand All @@ -23,61 +20,7 @@
*
* @since 4.0
*/
class Field_Poll extends Helper_Abstract_Fields {

/**
* Check the appropriate variables are parsed in send to the parent construct
*
* @param object $field The GF_Field_* Object
* @param array $entry The Gravity Forms Entry
*
* @param Helper_Abstract_Form $gform
* @param Helper_Misc $misc
*
* @throws Exception
*
* @since 4.0
*/
public function __construct( $field, $entry, Helper_Abstract_Form $gform, Helper_Misc $misc ) {

/* call our parent method */
parent::__construct( $field, $entry, $gform, $misc );

/*
* Custom Field can be any of the following field types:
* single line text, paragraph, dropdown, select, number, checkbox, radio, hidden,
* date, time, phone, website, email, file upload or list
*/
$class = $this->misc->get_field_class( $field->inputType );

try {
/* check load our class */
if ( class_exists( $class ) ) {

/* See https://docs.gravitypdf.com/v6/developers/filters/gfpdf_field_class/ for more details about these filters */
$this->fieldObject = apply_filters( 'gfpdf_field_class', new $class( $field, $entry, $gform, $misc ), $field, $entry, $this->form );
$this->fieldObject = apply_filters( 'gfpdf_field_class_' . $field->inputType, $this->fieldObject, $field, $entry, $this->form );
} else {
throw new Exception( 'Class not found' );
}
} catch ( Exception $e ) {
/* Exception thrown. Load generic field loader */
$this->fieldObject = apply_filters( 'gfpdf_field_default_class', new Field_Default( $field, $entry, $gform, $misc ), $field, $entry, $this->form );
}

if ( $this->fieldObject instanceof \GFPDF\Helper\Helper_Interface_Field_Pdf_Config ) {
$this->fieldObject->set_pdf_config( $this->get_pdf_config() );
}
}

/**
* Used to check if the current field has a value
*
* @since 4.0
*/
public function is_empty() {
return $this->fieldObject->is_empty();
}
class Field_Poll extends Helper_Abstract_Fields_Input_Type {

/**
* Return the HTML form data
Expand All @@ -94,15 +37,13 @@ public function form_data() {
$label = $this->get_label();

if ( isset( $value[0] ) ) {

$field = [];

foreach ( $value as $item ) {
/* For backwards compatibility, we'll wrap these in their own array key */
$field[0][] = $item['label'];
}
} else {
$field = ( isset( $value['label'] ) ) ? $value['label'] : '';
$field = $value['label'] ?? '';
}

$data[ $field_id . '.' . $label ] = $field;
Expand All @@ -112,41 +53,4 @@ public function form_data() {

return [ 'field' => $data ];
}

/**
* Display the HTML version of this field
*
* @param string $value
* @param bool $label
*
* @return string
*
* @since 4.0
*/
public function html( $value = '', $label = true ) {
if ( $this->get_output() ) {
$this->fieldObject->enable_output();
}

return $this->fieldObject->html();
}

/**
* Get the standard GF value of this field
*
* @return string|array
*
* @since 4.0
*/
public function value() {
if ( $this->fieldObject->has_cache() ) {
return $this->cache();
}

$value = $this->fieldObject->value();

$this->fieldObject->cache( $value );

return $this->fieldObject->cache();
}
}
Loading