working on forms #v1

This commit is contained in:
Antoine M 2024-04-15 17:38:16 +02:00
parent c5ab2936db
commit 8627995253
7 changed files with 519 additions and 34 deletions

View File

@ -1,38 +1,5 @@
<?php <?php
add_filter('gform_pre_render_18', 'populate_posts');
add_filter('gform_pre_validation_18', 'populate_posts');
add_filter('gform_pre_submission_filter_18', 'populate_posts');
add_filter('gform_admin_pre_render_18', 'populate_posts');
function populate_posts($form)
{
foreach ($form['fields'] as &$field) { require_once(__DIR__ . '/forms/form-OrderPaper.php');
if ($field->type != 'checkbox' || strpos($field->inputName, 'publication-list') === false) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
$brochures_query = new WP_Query(array(
'post_type' => 'brochures',
'posts_per_page' => -1,
'post_status' => 'publish',
));
// $posts = get_posts('numberposts=-1&post_status=publish&post_type=brochures');
$choices = array();
foreach ($brochures_query->posts as $post) {
$choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a Post';
$field->choices = $choices;
}
return $form;
}

View File

@ -0,0 +1,124 @@
<?php
if (class_exists('GF_Field')) {
class OrderPaperPublications extends GF_Field
{
public $type = 'order_paper_publications';
private $brochures_query; // Déplacez la déclaration de la propriété ici
public function __construct()
{
$post_query = new WP_Query(array(
'post_type' => 'brochures',
'posts_per_page' => -1,
'post_status' => 'publish',
));;
$this->brochures_query = $post_query;
$choices = [];
foreach ($this->brochures_query->posts as $post) {
$choices[] = ['text' => $post->post_title];
}
}
public function get_form_editor_field_title()
{
return esc_attr__('Commande de publications papier', 'txtdomain');
}
public function get_form_editor_button()
{
return [
'group' => 'advanced_fields',
'text' => $this->get_form_editor_field_title(),
];
}
public function get_form_editor_field_icon()
{
return 'gform-icon--multi-select';
}
public function get_form_editor_field_settings()
{
return [
'label_setting',
'choices_setting',
'description_setting',
'rules_setting',
'error_message_setting',
'css_class_setting',
'conditional_logic_field_setting'
];
}
public $choices = [
['text' => 'Publication 1'],
['text' => 'Publication 2'],
['text' => 'Publication 3'],
];
public function is_value_submission_array()
{
return true;
}
public function get_field_input($form, $value = '', $entry = null)
{
// if ($this->is_form_editor()) {
// return '';
// }
$form_id = $form['id'];
$is_entry_detail = $this->is_entry_detail();
$id = (int) $this->id;
write_log($id);
write_log($form_id);
ob_start();
?>
<fieldset id='<?php echo "field_" . $id . "[]" ?>' class="field-order-paper-publications gfield gfield--type-checkbox gfield--type-choice gfield--input-type-checkbox gf_list_2col gfield_contains_required">
<div class="gfield_checkbox" id='<?php echo "field_" . $form_id . "_" . $id ?>'>
<?php foreach ($this->brochures_query->posts as $index => $brochurePost) { ?>
<div class="gchoice">
<input id='<?php echo "field_" . $id . "_" . $index ?>' class="gfield-choice-input" name="<?php echo "input_" . $id . "" . $index ?> " type="checkbox" value="<?php echo $brochurePost->post_title ?>">
<label id='<?php echo "field_" . $id . "_" . $index ?>' for="choice_18_18_1" class="gform-field-label gform-field-label--type-inline"><?php echo $brochurePost->post_title ?></label>
<input class="publications-amount-fr" type="number" size="1" name="<?php echo "amount_fr" ?>" />
<input class="publications-amount-nl" type="number" size="1" name="<?php echo "amount_nl" ?>" />
</div>
<?php } ?>
</div>
</fieldset>
<?php
$table_html = ob_get_clean();
return $table_html;
// .. Rest of code for frontend and edit entry here...
}
private function translateValueArray($value)
{
if (empty($value)) {
return [];
}
$table_value = [];
$counter = 0;
foreach ($this->choices as $course) {
foreach ($this->delivery_days as $day) {
$table_value[$course['text']][$day] = $value[$counter++];
}
}
return $table_value;
}
}
GF_Fields::register(new OrderPaperPublications());
}

View File

@ -0,0 +1,146 @@
<?php
if (class_exists('GF_Field')) {
class FoodDelivery extends GF_Field {
public $type = 'food_delivery';
public $choices = [
[ 'text' => 'Food Choice 1' ],
[ 'text' => 'Food Choice 2' ],
[ 'text' => 'Food Choice 3' ],
];
private $delivery_days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
public function get_form_editor_field_title() {
return esc_attr__('Food Delivery', 'txtdomain');
}
public function get_form_editor_button() {
return [
'group' => 'advanced_fields',
'text' => $this->get_form_editor_field_title(),
];
}
public function get_form_editor_field_settings() {
return [
'label_setting',
'choices_setting',
'description_setting',
'rules_setting',
'error_message_setting',
'css_class_setting',
'conditional_logic_field_setting',
];
}
public function is_value_submission_array() {
return true;
}
public function get_field_input($form, $value = '', $entry = null) {
if ($this->is_form_editor()) {
return '';
}
$id = (int) $this->id;
if ($this->is_entry_detail()) {
$table_value = maybe_unserialize($value);
} else {
$table_value = $this->translateValueArray($value);
}
$table = '<table class="delivery-table"><tbody><tr>';
$table .= '<th>' . __('Course', 'txtdomain') . '</th>';
foreach ($this->delivery_days as $day) {
$table .= '<th>' . $day . '</th>';
}
$table .= '</tr>';
foreach ($this->choices as $course) {
$table .= '<tr>';
$table .= '<td>' . $course['text'] . '</td>';
foreach ($this->delivery_days as $day) {
$table .= '<td><input type="number" size="1" name="input_' . $id . '[]" value="' . $table_value[$course['text']][$day] . '" /></td>';
}
$table .= '</tr>';
}
$table .= '</tbody></table>';
return $table;
}
private function translateValueArray($value) {
if (empty($value)) {
return [];
}
$table_value = [];
$counter = 0;
foreach ($this->choices as $course) {
foreach ($this->delivery_days as $day) {
$table_value[$course['text']][$day] = $value[$counter++];
}
}
return $table_value;
}
public function get_value_save_entry($value, $form, $input_name, $lead_id, $lead) {
if (empty($value)) {
$value = '';
} else {
$table_value = $this->translateValueArray($value);
$value = serialize($table_value);
}
return $value;
}
private function prettyListOutput($value) {
$str = '<ul>';
foreach ($value as $course => $days) {
$week = '';
foreach ($days as $day => $delivery_number) {
if (!empty($delivery_number)) {
$week .= '<li>' . $day . ': ' . $delivery_number . '</li>';
}
}
// Only add week if there were any requests at all
if (!empty($week)) {
$str .= '<li><h3>' . $course . '</h3><ul class="days">' . $week . '</ul></li>';
}
}
$str .= '</ul>';
return $str;
}
public function get_value_entry_list($value, $entry, $field_id, $columns, $form) {
return __('Enter details to see delivery details', 'txtdomain');
}
public function get_value_entry_detail($value, $currency = '', $use_text = false, $format = 'html', $media = 'screen') {
$value = maybe_unserialize($value);
if (empty($value)) {
return $value;
}
$str = $this->prettyListOutput($value);
return $str;
}
public function get_value_merge_tag($value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br) {
return $this->prettyListOutput($value);
}
public function is_value_submission_empty($form_id) {
$value = rgpost('input_' . $this->id);
foreach ($value as $input) {
if (strlen(trim($input)) > 0) {
return false;
}
}
return true;
}
}
GF_Fields::register(new FoodDelivery());
}

View File

@ -0,0 +1,38 @@
<?php
add_filter('gform_pre_render_18', 'populate_posts');
add_filter('gform_pre_validation_18', 'populate_posts');
add_filter('gform_pre_submission_filter_18', 'populate_posts');
add_filter('gform_admin_pre_render_18', 'populate_posts');
function populate_posts($form)
{
foreach ($form['fields'] as &$field) {
if ($field->type != 'checkbox' || strpos($field->inputName, 'publication-list') === false) {
continue;
}
// you can add additional parameters here to alter the posts that are retrieved
// more info: http://codex.wordpress.org/Template_Tags/get_posts
$brochures_query = new WP_Query(array(
'post_type' => 'brochures',
'posts_per_page' => -1,
'post_status' => 'publish',
));
// $posts = get_posts('numberposts=-1&post_status=publish&post_type=brochures');
$choices = array();
foreach ($brochures_query->posts as $post) {
$choices[] = array('text' => $post->post_title, 'value' => $post->post_title);
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a Post';
$field->choices = $choices;
}
return $form;
}

View File

@ -0,0 +1,162 @@
<?php
if (class_exists('GF_Field')) {
class OrderPaperPublications extends GF_Field_Select
{
public $type = 'order_paper_publications';
// public function __construct()
// {
// parent::__construct();
// $this->buildChoices();
// }
private function buildChoices()
{
$brochures_query = new WP_Query(array(
'post_type' => 'brochures',
'posts_per_page' => -1,
'post_status' => 'publish',
));
$choices = [];
foreach ($brochures_query->posts as $key => $post) {
array_push($choices, ['text' => $post->post_title, 'slug' => $post->post_name]);
}
$this->choices = $choices;
}
public function get_form_editor_field_title()
{
return esc_attr__('Commande de publications papier', 'txtdomain');
}
public function get_form_editor_button()
{
return [
'group' => 'advanced_fields',
'text' => $this->get_form_editor_field_title(),
];
}
public function get_form_editor_field_icon()
{
return 'gform-icon--multi-select';
}
public function get_form_editor_field_settings()
{
return [
'label_setting',
'choices_setting',
'description_setting',
'rules_setting',
'error_message_setting',
'css_class_setting',
'conditional_logic_field_setting'
];
}
// public $choices = [
// ['text' => 'Publication 1'],
// ['text' => 'Publication 2'],
// ['text' => 'Publication 3'],
// ];
public function is_value_submission_array()
{
return true;
}
public function get_field_input($form, $value = '', $entry = null)
{
$this->buildChoices();
$form_id = $form['id'];
$id = $this->id;
ob_start();
include 'template-form-order-paper.php';
$table = ob_get_clean();
return $table;
}
private function translateValueArray($value)
{
$this->buildChoices();
$table_value = [];
$counter = 0;
foreach ($this->choices as $brochure) {
$table_value[$brochure['text']]['quantity_fr'] = $value[$counter] ?? 0;
$table_value[$brochure['text']]['quantity_nl'] = $value[$counter + 1] ?? 0;
$counter += 2;
}
return $table_value;
}
public function get_value_save_entry($value, $form, $input_name, $lead_id, $lead)
{
if (empty($value)) {
$value = '';
} else {
$table_value = $this->translateValueArray($value);
$value = serialize($table_value);
}
return $value;
}
private function PrettyListBrochureOrder($value)
{
$brochureOrderPreviewHtml = '<ul>';
foreach ($value as $brochure => $quantities) {
$brochureOrderAmount = null;
if ($quantities["quantity_fr"]) {
$brochureOrderAmount .= '<li>' . __("Quantité FR: ", "homegrade-theme__texte-fonctionnel") . '<span class="brochure_quantity">' . $quantities["quantity_fr"] . '</span></li>';
}
if ($quantities["quantity_nl"]) {
$brochureOrderAmount .= '<li>' . __("Quantité NL: ", "homegrade-theme__texte-fonctionnel") . '<span class="brochure_quantity">' . $quantities["quantity_nl"] . '</span></li>';
}
if (!$brochureOrderAmount) continue;
if (!empty($brochureOrderAmount)) {
$brochureOrderPreviewHtml .= '<li><h3>' . $brochure . '</h3><ul class="days">' . $brochureOrderAmount . '</ul></li>';
}
}
$brochureOrderPreviewHtml .= '</ul>';
return $brochureOrderPreviewHtml;
}
public function get_value_entry_list($value, $entry, $field_id, $columns, $form)
{
return __('Enter details to see delivery details', 'txtdomain');
}
public function get_value_entry_detail($value, $currency = '', $use_text = false, $format = 'html', $media = 'screen')
{
$value = maybe_unserialize($value);
if (empty($value)) {
return;
}
$str = $this->PrettyListBrochureOrder($value);
return $str;
}
public function get_value_merge_tag($value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format, $nl2br)
{
return $this->PrettyListBrochureOrder($value);
}
}
GF_Fields::register(new OrderPaperPublications());
}

View File

@ -0,0 +1,21 @@
<?php
?>
<fieldset class="field-order-paper-publications">
<?php
?>
<?php foreach ($this->choices as $index => $brochurePost) : ?>
<div class=" gchoice brochure-order-field">
<label id='<?php echo "field_" . $id . "_" . $index ?>' for="choice_18_18_1" class="gform-field-label gform-field-label--type-inline"><?php echo $brochurePost['text'] ?></label>
<div class="publications-amount-fr">
<label id="publications-amount__label-fr" for="<?php echo 'input_' . $id . '[]' ?>">fr</label>
<input type="number" size="1" name="<?php echo 'input_' . $id . '[]' ?>" />
</div>
<div class="publications-amount publications-amount-nl">
<label id="publications-amount__label-nl" for="<?php echo 'input_' . $id . '[]' ?>">nl</label>
<input type="number" size="1" name="<?php echo 'input_' . $id . '[]' ?>" />
</div>
</div>
<?php endforeach; ?>
</fieldset>

View File

@ -173,3 +173,30 @@ form:not(.metabox-location-side):not(.metabox-location-normal):not(
@apply max-w-5xl !mx-auto; @apply max-w-5xl !mx-auto;
} }
} }
/* .publications-amount-fr,
.publications-amount-nl {
@apply !hidden;
} */
.field-order-paper-publications {
@apply !grid !grid-cols-2 gap-x-12 gap-y-2;
.brochure-order-field {
@apply flex justify-end gap-3 items-center;
label {
@apply block !mr-auto;
}
.publications-amount-fr,
.publications-amount-nl {
input {
@apply h-2 !w-12;
}
#publications-amount__label-nl,
#publications-amount__label-fr {
@apply !text-neutral-600 uppercase !text-sm text-center;
}
}
}
}