Compare commits

...

5 Commits

Author SHA1 Message Date
2cd207b946 handling checking/unchecking subtaxonomies
All checks were successful
continuous-integration/drone/push Build is passing
2024-11-13 13:53:53 +01:00
fb729e9bc1 introducing some utilities functions 2024-11-13 13:53:17 +01:00
b77e053b79 renaming function 2024-11-13 13:53:03 +01:00
7c87c99fd5 handling localisation meta query 2024-11-13 13:52:49 +01:00
655ff4bc73 handlign artisans custom functions 2024-11-13 13:52:27 +01:00
8 changed files with 360 additions and 143 deletions

View File

@ -5,7 +5,9 @@
require_once(__DIR__ . '/includes/errorlog.php');
require_once(__DIR__ . '/includes/init.php');
require_once(__DIR__ . '/includes/post_types.php');
require_once(__DIR__ . '/includes/artisans.php');
require_once(__DIR__ . '/includes/admin.php');
require_once(__DIR__ . '/includes/taxonomy.php');
require_once(__DIR__ . '/includes/api.php');
require_once(__DIR__ . '/includes/blocks.php');
require_once(__DIR__ . '/includes/utilities.php');

View File

@ -22,11 +22,11 @@ add_action('rest_api_init', function () {
function build_search_artisan_posts_cards($request)
{
// write_log($request);
$currentLanguage = esc_html($request->get_param('current-page-language')) ?? 'fr';
$previousActivePage = esc_html($request->get_param('active-page')) ?? 1;
$postsPerPage = esc_html($request->get_param('posts-per-page')) ?? 12;
$taxonomy = esc_html($request->get_param('taxonomy')) ?? 'metiers';
$localisation = esc_html($request->get_param('localisation')) ?? null;
$StringifiedTaxonomyIds = esc_html($request->get_param('taxonomy-ids')) ?? null;
$taxonomyIds = explode(',', $StringifiedTaxonomyIds);
@ -42,6 +42,16 @@ function build_search_artisan_posts_cards($request)
'field' => 'term_id',
)
);
$metaQuery = [];
if ($localisation && $localisation !== 'all') {
$metaQuery[] = array(
'key' => 'state',
'value' => $localisation,
'compare' => '='
);
// write_log($metaQuery);
}
do_action('wpml_switch_language', $currentLanguage);
@ -52,15 +62,17 @@ function build_search_artisan_posts_cards($request)
"posts_per_page" => -1,
"paged" => $activePage,
"tax_query" => $taxQuery,
// "meta_query" => [],
"meta_query" => $metaQuery,
);
$newsPostsDatas = new WP_Query($args);
ob_start();
foreach ($newsPostsDatas->posts as $key => $post) {
// continue en fonction
$post_thumbnail = get_the_post_thumbnail($post->ID, 'full', array('class' => 'card-artisans__thumbnail')) ?? null;
$news_type = get_the_terms($post->ID, "news_type") ?? null;
$post_date = get_the_date('j.m.Y', $post->ID) ?? null;
@ -76,7 +88,6 @@ function build_search_artisan_posts_cards($request)
'news_type' => $news_type,
'post_date' => $post_date,
'current_taxonomy' => $taxonomy,
)
);
}
@ -97,3 +108,33 @@ function build_search_artisan_posts_cards($request)
return $response;
}
function debugQueryTest()
{
$taxQuery = array(
'taxonomy' => 'metiers',
'terms' => array(17, 18, 19, 20),
'field' => 'term_id'
);
$metaQuery = array(
array(
'key' => 'state',
'value' => "brussels",
'compare' => '='
)
);
$args = array(
"status" => "publish",
"post_type" => "artisans",
"posts_per_page" => -1,
"tax_query" => $taxQuery,
"meta_query" => $metaQuery,
);
$newsPostsDatas = new WP_Query($args);
}
// debugQueryTest();

28
includes/artisans.php Normal file
View File

@ -0,0 +1,28 @@
<?php
function metiers_patrimoine_artisant_post_updater($post_id)
{
if (!$post_id) return;
$my_post = array();
$my_post['ID'] = $post_id;
$name = get_field("name", $post_id);
$address = get_field("adresse", $post_id);
if (get_post_type() == 'artisans' && $name) {
$my_post['post_title'] = "";
$my_post['post_title'] = $name;
}
if ($address && isset($address['country'])) {
$stateGenericName = getGenericStateNameFromAcfStateName($address['state']) ?? "";
// write_log($stateGenericName);
// Met à jour le champ personnalisé 'country' avec la valeur du pays de l'adresse
update_post_meta($post_id, 'state', $stateGenericName);
}
// write_log($address);
wp_update_post($my_post);
}
add_action('acf/save_post', 'metiers_patrimoine_artisant_post_updater', 20);

View File

@ -43,22 +43,3 @@ function metiers_patrimoine_create_posttype()
}
add_action('init', 'metiers_patrimoine_create_posttype');
function metiers_patrimoine_artisant_post_title_updater($post_id)
{
if (!$post_id) return;
$my_post = array();
$my_post['ID'] = $post_id;
$name = get_field("name", $post_id);
if (get_post_type() == 'artisans' && $name) {
$my_post['post_title'] = "";
$my_post['post_title'] = $name;
}
wp_update_post($my_post);
}
add_action('acf/save_post', 'metiers_patrimoine_artisant_post_title_updater', 20);

150
includes/utilities.php Normal file
View File

@ -0,0 +1,150 @@
<?php
function getAllBatimentsTermsByParents()
{
// Récupérer tous les termes de la taxonomie 'elementsbatiments' avec une hiérarchie
$ElementBatimentsTerms = get_terms([
'taxonomy' => 'elementsbatiments',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
]);
$terms_by_parent = [];
$child_terms = [];
if ($ElementBatimentsTerms) {
// Boucle pour organiser les termes en fonction de leurs parents
foreach ($ElementBatimentsTerms as $term) {
if ($term->parent == 0) {
// Si c'est un parent, on le place dans le tableau des parents
$terms_by_parent[$term->term_id] = ['term' => $term, 'children' => []];
} else {
$child_terms[$term->parent][] = $term;
}
}
// Réunir les termes parents et leurs enfants
foreach ($terms_by_parent as $parent_id => &$parent_data) {
// Ajouter les enfants au parent
if (isset($child_terms[$parent_id])) {
$parent_data['children'] = $child_terms[$parent_id];
}
}
}
return $terms_by_parent;
}
function getAllMetiersTermsByParents()
{
// Récupérer tous les termes de la taxonomie 'elementsbatiments' avec une hiérarchie
$MetiersTerms = get_terms([
'taxonomy' => 'metiers',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
]);
$terms_by_parent = [];
$child_terms = [];
if ($MetiersTerms) {
// Boucle pour organiser les termes en fonction de leurs parents
foreach ($MetiersTerms as $term) {
if ($term->parent == 0) {
// Si c'est un parent, on le place dans le tableau des parents
$terms_by_parent[$term->term_id] = ['term' => $term, 'children' => []];
} else {
$child_terms[$term->parent][] = $term;
}
}
// Réunir les termes parents et leurs enfants
foreach ($terms_by_parent as $parent_id => &$parent_data) {
// Ajouter les enfants au parent
if (isset($child_terms[$parent_id])) {
$parent_data['children'] = $child_terms[$parent_id];
}
}
}
return $terms_by_parent;
}
function get_region_by_postcode($postcode)
{
// Conversion du code postal en entier pour faciliter les comparaisons
$postcode = (int) $postcode;
// Bruxelles (1000 - 1200)
if ($postcode >= 1000 && $postcode <= 1200) {
return 'Bruxelles';
}
// Flandre (8000 - 8999 : Flandre occidentale, 9000 - 9999 : Flandre orientale, 2000 - 2999 : Anvers, etc.)
if (($postcode >= 8000 && $postcode <= 8999) ||
($postcode >= 9000 && $postcode <= 9999) ||
($postcode >= 2000 && $postcode <= 2999) ||
($postcode >= 1500 && $postcode <= 1999) ||
($postcode >= 3500 && $postcode <= 3999)
) {
return 'Flandre';
}
// Wallonie (4000 - 4999 : Liège, 6000 - 6999 : Hainaut, 6600 - 6999 : Luxembourg, 5000 - 5999 : Namur, etc.)
if (($postcode >= 4000 && $postcode <= 4999) ||
($postcode >= 6000 && $postcode <= 6999) ||
($postcode >= 6600 && $postcode <= 6999) ||
($postcode >= 5000 && $postcode <= 5999) ||
($postcode >= 1300 && $postcode <= 1499)
) {
return 'Wallonie';
}
// Retourne une valeur par défaut si le code postal ne correspond à aucune région
return 'Inconnu';
}
function getAcfStateNameFromEnglish($localisation)
{
if ($localisation === 'brussels') {
return 'Bruxelles';
}
if ($localisation === 'flanders') {
return 'Vlaams Gewest';
}
if ($localisation === 'wallonia') {
return 'Région Wallonne';
}
if ($localisation === 'all') {
return 'all';
}
}
function getGenericStateNameFromAcfStateName($state)
{
if ($state === 'Bruxelles') {
return 'brussels';
}
if ($state === 'Vlaams Gewest') {
return 'flanders';
}
if ($state === 'Région Wallonne') {
return 'wallonia';
}
if ($state === 'all') {
return 'all';
}
};

View File

@ -13,17 +13,29 @@
h4 {
@apply my-0;
}
.checkbox-choice {
@apply pl-0;
@apply flex flex-wrap;
.checkbox-choice {
@apply pl-0 pt-1;
@apply flex flex-wrap;
transition: all 0.3s ease;
&__subtaxonomy-list {
@apply pl-4 my-1 w-full hidden;
}
&:has(> input[type='checkbox']:checked) {
.checkbox-choice__subtaxonomy-list {
@apply block bg-patrimoine-sante-securite-light;
@apply block;
}
}
&.checkbox-choice-parent {
@apply px-2 rounded-2xl;
}
&.checkbox-choice-parent:has(.child-checkbox:checked) {
transition: all 0.3s ease;
@apply bg-patrimoine-sante-securite-light pt-3;
.checkbox-choice__subtaxonomy-list {
@apply block;
}
}
@ -93,6 +105,7 @@
&__metiers-filters {
@apply border border-neutral-300 p-4 rounded-3xl my-4 w-full;
}
&__elements-batiments-filters,
&__metiers-filters {
@apply hidden;

View File

@ -26,17 +26,20 @@ async function hydrateFields(formObjectDatas) {
.querySelector('body')
.getAttribute('current-language');
const taxonomy = formObjectDatas.search_by;
const localisation = formObjectDatas.localisation ?? null;
console.log(`localisation : ${localisation}`);
const taxonomyIds =
taxonomy === 'metiers'
? formObjectDatas.metiers
: formObjectDatas.elementsbatiments;
const response = await fetch(
`/wp-json/metiers-patrimoine-datas/v1/build/artisans?current-page-language=${currentLanguage}&taxonomy=${taxonomy}&taxonomy-ids=${taxonomyIds}`
`/wp-json/metiers-patrimoine-datas/v1/build/artisans?current-page-language=${currentLanguage}&taxonomy=${taxonomy}&taxonomy-ids=${taxonomyIds}&localisation=${localisation}`
);
const artisansDatas = await response.json();
console.log(artisansDatas);
// console.log(artisansDatas);
const artisansGrid = document.querySelector(
'.artisans-posts__grid'
@ -48,6 +51,69 @@ async function hydrateFields(formObjectDatas) {
// );
}
function handleHierarchicalCheckboxRelation(e) {
// If parent is checked, check all children
if (
e.target.checked &&
e.target.classList.contains('parent-checkbox')
) {
const parentRootTag = e.target.closest(
'.checkbox-choice'
);
const children = parentRootTag.querySelectorAll(
'.child-checkbox'
);
children.forEach((child) => {
child.checked = true;
});
}
if (
!e.target.checked &&
e.target.classList.contains('parent-checkbox')
) {
// alert('uncheck parent');
const parent = e.target.closest('.checkbox-choice');
const children = parent.querySelectorAll(
'.child-checkbox'
);
children.forEach((child) => {
child.checked = false;
});
}
if (
!e.target.checked &&
e.target.classList.contains('child-checkbox')
) {
const checkboxRootTag = e.target.closest(
'.checkbox-choice'
);
const parentCheckbox =
checkboxRootTag.parentElement.parentElement.querySelector(
'.parent-checkbox'
);
parentCheckbox.checked = false;
}
}
function handleFormChange(e) {
e.preventDefault();
if (
e.target.type === 'checkbox' &&
e.target.classList.contains('taxonomy-checkbox')
) {
handleHierarchicalCheckboxRelation(e);
}
const form = e.target.closest('form');
const formData = new FormData(form);
let formObjectDatas = formatFormDataArray(formData);
hydrateFields(formObjectDatas);
}
export default function dynamicSearch() {
const form = document.querySelector(
'.metier-patrimoine-searchform'
@ -56,15 +122,5 @@ export default function dynamicSearch() {
if (!form) {
return;
}
form.addEventListener('change', (e) => {
e.preventDefault();
const form = e.target.closest('form');
const formData = new FormData(form);
let formObjectDatas = formatFormDataArray(formData);
console.log(formObjectDatas);
hydrateFields(formObjectDatas);
});
form.addEventListener('change', handleFormChange);
}

View File

@ -44,7 +44,6 @@ $pageIcon = get_field('page_icon', get_queried_object_id()) ?? null;
?>
<section class="artisans-posts">
<aside class="metier-patrimoine-searchbar">
<div class="metier-patrimoine-searchbar__results-indications">
@ -82,56 +81,34 @@ $pageIcon = get_field('page_icon', get_queried_object_id()) ?? null;
</div>
<ul>
<?php
// Récupérer tous les termes de la taxonomie 'elementsbatiments' avec une hiérarchie
$ElementBatimentsTerms = get_terms([
'taxonomy' => 'elementsbatiments',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
]);
if ($ElementBatimentsTerms) :
// Organiser les termes par parent (hiérarchie)
$terms_by_parent = [];
$terms_by_parent = getAllBatimentsTermsByParents();
// Boucle pour organiser les termes en fonction de leurs parents
foreach ($ElementBatimentsTerms as $term) {
if ($term->parent == 0) {
// Si c'est un parent, on le place dans le tableau des parents
$terms_by_parent[$term->term_id] = [
'term' => $term,
'children' => [],
];
} else {
$terms_by_parent[$term->parent]['children'][] = $term;
}
}
// Afficher les termes parents et leurs enfants
foreach ($terms_by_parent as $parent_term_data) :
$parent_term = $parent_term_data['term'];
// Afficher les termes parents et leurs enfants
foreach ($terms_by_parent as $parent_term_data) :
$parent_term = $parent_term_data['term'];
?>
<li class="checkbox-choice">
<input type="checkbox" name="elementsbatiments[]" value="<?php echo esc_attr($parent_term->term_id); ?>" data-term="<?php echo esc_attr($parent_term->slug); ?>">
<label>
<?php echo esc_html($parent_term->name); ?>
</label>
<li class="checkbox-choice checkbox-choice-parent">
<input class="parent-checkbox taxonomy-checkbox" type="checkbox" name="elementsbatiments[]" value="<?php echo esc_attr($parent_term->term_id); ?>" data-term="<?php echo esc_attr($parent_term->slug); ?>">
<label>
<?php echo esc_html($parent_term->name); ?>
</label>
<?php if (!empty($parent_term_data['children'])) : ?>
<ul class="checkbox-choice__subtaxonomy-list">
<?php foreach ($parent_term_data['children'] as $child_term) : ?>
<li class="checkbox-choice">
<input type="checkbox" name="elementsbatiments[]" value="<?php echo esc_attr($child_term->term_id); ?>" data-term="<?php echo esc_attr($child_term->slug); ?>">
<label>
<?php echo esc_html($child_term->name); ?>
</label>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php if (!empty($parent_term_data['children'])) : ?>
<ul class="checkbox-choice__subtaxonomy-list">
<?php foreach ($parent_term_data['children'] as $child_term) : ?>
<li class="checkbox-choice checkbox-choice-child">
<input class="child-checkbox taxonomy-checkbox" type="checkbox" name="elementsbatiments[]" value="<?php echo esc_attr($child_term->term_id); ?>" data-term="<?php echo esc_attr($child_term->slug); ?>">
<label>
<?php echo esc_html($child_term->name); ?>
</label>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach;
endif; ?>
?>
</ul>
</fieldset>
@ -145,67 +122,36 @@ $pageIcon = get_field('page_icon', get_queried_object_id()) ?? null;
<ul>
<?php
// Récupérer tous les termes de la taxonomie 'metiers' avec une hiérarchie
$MertiersTerms = get_terms([
'taxonomy' => 'metiers',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'stat'
]);
if ($MertiersTerms) :
$terms_by_parent = [];
foreach ($MertiersTerms as $term) {
if ($term->parent == 0 && !isset($terms_by_parent[$term->term_id])) {
$terms_by_parent[$term->term_id] = [
'term' => $term,
'children' => [],
];
}
if ($term->parent == 0 && isset($terms_by_parent[$term->term_id])) {
$terms_by_parent[$term->term_id]['term'] = $term;
} else {
$terms_by_parent = getAllMetiersTermsByParents();
if (!isset($terms_by_parent[$term->parent])) {
$terms_by_parent[$term->parent] = [
'term' => null,
'children' => [],
];
}
$terms_by_parent[$term->parent]['children'][] = $term;
}
}
foreach ($terms_by_parent as $parent_term_data) :
$parent_term = $parent_term_data['term'];
foreach ($terms_by_parent as $parent_term_data) :
$parent_term = $parent_term_data['term'];
?>
<li class="checkbox-choice">
<input type="checkbox" name="metiers[]" value="<?php echo esc_attr($parent_term->term_id); ?>" data-term="<?php echo esc_attr($parent_term->slug); ?>">
<label>
<?php echo esc_html($parent_term->name); ?>
<li class="checkbox-choice checkbox-choice-parent">
<input class="parent-checkbox taxonomy-checkbox" type="checkbox" name="metiers[]" value="<?php echo esc_attr($parent_term->term_id); ?>" data-term="<?php echo esc_attr($parent_term->slug); ?>">
<label>
<?php echo esc_html($parent_term->name); ?>
</label>
</label>
<!-- Afficher les enfants sous forme de checkboxes -->
<?php if (!empty($parent_term_data['children'])) : ?>
<ul class="checkbox-choice__subtaxonomy-list">
<?php foreach ($parent_term_data['children'] as $child_term) : ?>
<li class="checkbox-choice">
<!-- Afficher les enfants sous forme de checkboxes -->
<?php if (!empty($parent_term_data['children'])) : ?>
<ul class="checkbox-choice__subtaxonomy-list">
<?php foreach ($parent_term_data['children'] as $child_term) : ?>
<li class="checkbox-choice checkbox-choice-child">
<input type="checkbox" name="metiers[]" value="<?php echo esc_attr($child_term->term_id); ?>" data-term="<?php echo esc_attr($child_term->slug); ?>">
<label>
<?php echo esc_html($child_term->name); ?>
</label>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<input class="child-checkbox taxonomy-checkbox" type="checkbox" name="metiers[]" value="<?php echo esc_attr($child_term->term_id); ?>" data-term="<?php echo esc_attr($child_term->slug); ?>">
<label>
<?php echo esc_html($child_term->name); ?>
</label>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php endforeach;
endif; ?>
?>
</ul>
</fieldset>
@ -223,15 +169,15 @@ $pageIcon = get_field('page_icon', get_queried_object_id()) ?? null;
<label> <?php echo __("Toute la Belgique", "metiers-patrimoine-theme") ?></label>
</li>
<li class="checkbox-choice">
<input type="checkbox" name="localisation[]" value="wallonie">
<input type="checkbox" name="localisation[]" value="wallonia">
<label> <?php echo __("Wallonie", "metiers-patrimoine-theme") ?></label>
</li>
<li class="checkbox-choice">
<input type="checkbox" name="localisation[]" value="bruxelles">
<input type="checkbox" name="localisation[]" value="brussels" checked>
<label> <?php echo __("Bruxelles", "metiers-patrimoine-theme") ?></label>
</li>
<li class="checkbox-choice">
<input type="checkbox" name="localisation[]" value="flandre">
<input type="checkbox" name="localisation[]" value="flanders">
<label> <?php echo __("Flandre", "metiers-patrimoine-theme") ?></label>
</li>
</ul>