366 lines
9.5 KiB
PHP
366 lines
9.5 KiB
PHP
<?php
|
||
|
||
function get_the_parent_terms($taxonomy, $postID = null)
|
||
{
|
||
if (!$postID || !$taxonomy || !taxonomy_exists($taxonomy)) return null;
|
||
|
||
$terms = get_the_terms($postID, $taxonomy);
|
||
if (!$terms || empty($terms)) return [];
|
||
|
||
$parentTerms = array_filter($terms, function ($term) {
|
||
return $term->parent == 0;
|
||
});
|
||
return $parentTerms;
|
||
}
|
||
|
||
function get_the_children_terms($taxonomy, $postID)
|
||
{
|
||
if (!$postID || !$taxonomy || !taxonomy_exists($taxonomy)) return null;
|
||
|
||
$terms = get_the_terms($postID, $taxonomy);
|
||
if (!$terms || empty($terms)) return [];
|
||
|
||
$childrenTerms = array_filter($terms, function ($term) {
|
||
return $term->parent !== 0;
|
||
});
|
||
return $childrenTerms;
|
||
}
|
||
|
||
function get_children_terms_from_specific_parent($taxonomy, $postID, $parentTerm)
|
||
{
|
||
if (!$postID || !$taxonomy || !taxonomy_exists($taxonomy)) return null;
|
||
|
||
$childrenTerms = get_the_children_terms($taxonomy, $postID);
|
||
$filteredChildrenTerms = array_filter($childrenTerms, function ($term) use ($parentTerm) {
|
||
return $term->parent === $parentTerm->term_id;
|
||
});
|
||
return $filteredChildrenTerms;
|
||
}
|
||
|
||
function get_the_terms_organised_by_parent($taxonomy, $postID = null)
|
||
{
|
||
if (!$postID) return;
|
||
|
||
$terms = get_the_terms($postID, $taxonomy);
|
||
|
||
$sortedTermsByParent = [];
|
||
if (!$terms) return $sortedTermsByParent;
|
||
|
||
$parentTerms = get_the_parent_terms($taxonomy, $postID);
|
||
|
||
foreach ($parentTerms as $parentTerm) {
|
||
$sortedTermsByParent[] = $parentTerm;
|
||
|
||
$childrenTerms = get_children_terms_from_specific_parent($taxonomy, $postID, $parentTerm);
|
||
|
||
foreach ($childrenTerms as $childTerm) {
|
||
$sortedTermsByParent[] = $childTerm;
|
||
}
|
||
}
|
||
|
||
return $sortedTermsByParent;
|
||
}
|
||
|
||
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 'Metiers' 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 getAllUsers()
|
||
{
|
||
$users = get_users();
|
||
return $users;
|
||
}
|
||
|
||
|
||
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)
|
||
{
|
||
switch ($state) {
|
||
case 'Bruxelles':
|
||
return 'brussels';
|
||
case 'Région flamande':
|
||
case 'Vlaams Gewest':
|
||
return 'flanders';
|
||
case 'Région Wallonne':
|
||
return 'wallonia';
|
||
case 'all':
|
||
default:
|
||
return 'all';
|
||
}
|
||
};
|
||
|
||
|
||
function format_belgian_vat_number($vat_number)
|
||
{
|
||
$vat_number = strval($vat_number);
|
||
$has_letters = preg_match('/[a-zA-Z]/', $vat_number);
|
||
|
||
if ($has_letters) {
|
||
return $vat_number;
|
||
}
|
||
if (strlen($vat_number) === 9) {
|
||
return 0 . substr($vat_number, 0, 4) . '.' . substr($vat_number, 4, 3) . '.' . substr($vat_number, 7, 3);
|
||
}
|
||
if (strlen($vat_number) !== 10) {
|
||
return $vat_number;
|
||
}
|
||
return substr($vat_number, 0, 4) . '.' . substr($vat_number, 4, 3) . '.' . substr($vat_number, 7, 3);
|
||
}
|
||
|
||
|
||
function getArtisanConseillerName($postID)
|
||
{
|
||
if (!$postID) return;
|
||
$conseiller = get_field('conseiller', $postID);
|
||
if (!$conseiller) return;
|
||
$conseiller_name = $conseiller['user_firstname'] . ' ' . $conseiller['user_lastname'];
|
||
return $conseiller_name;
|
||
}
|
||
|
||
|
||
function translate_wordpress_online_statuses($post_status)
|
||
{
|
||
$status_object = get_post_status_object($post_status);
|
||
|
||
if ($post_status === 'publish') {
|
||
return 'En ligne';
|
||
}
|
||
if ($post_status === 'draft') {
|
||
return 'Brouillon';
|
||
}
|
||
if ($post_status === 'pending') {
|
||
return 'En attente';
|
||
}
|
||
if ($post_status === 'offline') {
|
||
return 'Hors ligne';
|
||
}
|
||
if ($post_status === 'trash') {
|
||
return 'Corbeille';
|
||
}
|
||
return $status_object->label;
|
||
}
|
||
|
||
|
||
function getFrenchDateFromTimestamp($timestamp)
|
||
{
|
||
$date = new DateTime($timestamp);
|
||
$formatter = new IntlDateFormatter(
|
||
'fr_FR',
|
||
IntlDateFormatter::FULL,
|
||
IntlDateFormatter::NONE,
|
||
'Europe/Paris',
|
||
IntlDateFormatter::GREGORIAN,
|
||
'd MMMM y'
|
||
);
|
||
$formatted_date = $formatter->format($date);
|
||
return $formatted_date;
|
||
}
|
||
|
||
|
||
function getRelativeTimeFromTimestamp($timestamp)
|
||
{
|
||
|
||
$date = new DateTime($timestamp);
|
||
$now = new DateTime();
|
||
$interval = $date->diff($now);
|
||
|
||
// Créer le message relatif
|
||
$relative_time = '';
|
||
if ($interval->y > 0) {
|
||
$relative_time = 'il y a ' . $interval->y . ' an' . ($interval->y > 1 ? 's' : '');
|
||
} elseif ($interval->m > 0) {
|
||
$relative_time = 'il y a ' . $interval->m . ' mois';
|
||
} else {
|
||
$relative_time = 'il y a ' . $interval->d . ' jour' . ($interval->d > 1 ? 's' : '');
|
||
}
|
||
|
||
return $relative_time;
|
||
}
|
||
|
||
|
||
function format_phone_number($phoneNumber)
|
||
{
|
||
// Nettoyage du numéro : retirer espaces, tirets, points, etc.
|
||
if (!$phoneNumber) return;
|
||
|
||
$cleanedNumber = preg_replace('/\D+/', '', $phoneNumber);
|
||
$phoneType = detectBelgianPhoneType($cleanedNumber);
|
||
|
||
|
||
if ($phoneType === 'mobile') {
|
||
$formattedPhoneNumber = preg_replace('/(\d{2})(\d{3})(\d{2})(\d{2})(\d{2})/', '+$1 $2 $3 $4 $5', $cleanedNumber);
|
||
} else if ($phoneType === 'landline') {
|
||
$formattedPhoneNumber = preg_replace('/(\d{2})(\d{1})(\d{3})(\d{2})(\d{2})/', '+$1 $2 $3 $4 $5', $cleanedNumber);
|
||
} else if ($phoneType === 'fr_mobile') {
|
||
$formattedPhoneNumber = preg_replace('/(\d{2})(\d{1})(\d{2})(\d{2})(\d{2})(\d{2})/', '+$1 $2 $3 $4 $5 $6', $cleanedNumber);
|
||
} else {
|
||
$formattedPhoneNumber = $cleanedNumber;
|
||
}
|
||
return $formattedPhoneNumber;
|
||
}
|
||
|
||
|
||
function detectBelgianPhoneType($number)
|
||
{
|
||
|
||
// Détection mobile
|
||
if (preg_match('/^324(55|56|60|61|6[5-8]|7[0-9]|80|8[3-9]|9[0-9])[0-9]{6}$/', $number)) {
|
||
return 'mobile';
|
||
}
|
||
// Détection fixe
|
||
if (preg_match('/^32[0-9]{8,10}$/', $number)) {
|
||
return 'landline';
|
||
}
|
||
|
||
if (preg_match('/^33[0-9]{9}$/', $number)) {
|
||
return 'fr_mobile';
|
||
}
|
||
|
||
return 'unknown';
|
||
}
|
||
|
||
|
||
function translate_city_name($city, $required_language = 'fr')
|
||
{
|
||
$translation_file_path = get_stylesheet_directory() . '/includes/city_translations.json';
|
||
$city_translations = json_decode(file_get_contents($translation_file_path), true);
|
||
|
||
if ($required_language === '' || !isset($city_translations[$required_language])) return $city;
|
||
|
||
foreach ($city_translations as $translation) {
|
||
if ($translation[$required_language] === $city) return $city;
|
||
|
||
if ($translation['fr'] === $city && $required_language === 'nl') {
|
||
return $translation['nl'];
|
||
}
|
||
if ($translation['nl'] === $city && $required_language === 'fr') {
|
||
return $translation['fr'];
|
||
}
|
||
}
|
||
return $city;
|
||
}
|