introducing export module
This commit is contained in:
parent
db9d6671d8
commit
932287ff60
145
includes/export-datas.php
Normal file
145
includes/export-datas.php
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
<?php
|
||||
|
||||
add_action('admin_menu', 'export_datas_page_in_menu');
|
||||
|
||||
function export_datas_page_in_menu()
|
||||
{
|
||||
|
||||
add_menu_page(
|
||||
'Export de données', // page <title>Title</title>
|
||||
'Export', // link text
|
||||
'manage_options', // user capabilities
|
||||
'export_datas', // page slug
|
||||
'export_datas_page_callback', // this function prints the page content
|
||||
'dashicons-external', // icon (from Dashicons for example)
|
||||
12 // menu position
|
||||
);
|
||||
}
|
||||
|
||||
function export_datas_page_callback()
|
||||
{
|
||||
?>
|
||||
<div class="wrap ">
|
||||
<h1>Exports de données</h1>
|
||||
|
||||
<div class="metiers-patrimoine-data-exports-page">
|
||||
|
||||
<div class="card export-card">
|
||||
<h2>Export des artisans</h2>
|
||||
<img class="export-card__illustration" src="<?php echo get_template_directory_uri() . '/resources/img/illustrations/homegrade_formations-peb.svg' ?>" alt="">
|
||||
<p> Exporter les données des artisans au format CSV. </p>
|
||||
<form method="post" action="<?php echo admin_url('admin-post.php'); ?>">
|
||||
<input type="hidden" name="action" value="export_artisans_datas">
|
||||
<?php wp_nonce_field('export_artisans_datas_nonce', 'export_artisans_datas_nonce'); ?>
|
||||
<button type="submit" name="custom_action_button" class="button cta--primary">
|
||||
<?php echo __("Exporter les artisans", "metiers-patrimoine-theme"); ?>
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
|
||||
add_action('admin_post_export_artisans_datas', 'metiers_handle_download_artisans_request');
|
||||
|
||||
function metiers_handle_download_artisans_request()
|
||||
{
|
||||
// Vérifier la nonce pour des raisons de sécurité
|
||||
if (!isset($_POST['export_artisans_datas_nonce']) || !wp_verify_nonce($_POST['export_artisans_datas_nonce'], 'export_artisans_datas_nonce')) {
|
||||
wp_die('Vérification de sécurité échouée.');
|
||||
}
|
||||
|
||||
// Vérifier les capacités de l'utilisateur
|
||||
if (!current_user_can('read')) {
|
||||
wp_die('Permissions insuffisantes.');
|
||||
}
|
||||
|
||||
generate_artisans_datas_to_csv();
|
||||
exit;
|
||||
}
|
||||
|
||||
function generate_artisans_datas_to_csv()
|
||||
{
|
||||
do_action('wpml_switch_language', 'fr');
|
||||
|
||||
// Récupérer tous les posts du type "artisans"
|
||||
$args = array(
|
||||
'post_type' => 'artisans',
|
||||
'posts_per_page' => -1, // Récupérer tous les posts
|
||||
'post_status' => 'publish',
|
||||
);
|
||||
|
||||
$artisans = new WP_Query($args);
|
||||
|
||||
// Vérifier si des posts ont été trouvés
|
||||
if (empty($artisans)) {
|
||||
wp_die('Aucun post trouvé pour le type "artisans".');
|
||||
}
|
||||
|
||||
// Définir le nom du fichier CSV
|
||||
$filename = 'artisans_data_' . date('Ymd_His') . '.csv';
|
||||
|
||||
// Ouvrir un fichier CSV en mode écriture
|
||||
$file = fopen($filename, 'w');
|
||||
|
||||
if ($file === false) {
|
||||
wp_die('Erreur lors de la création du fichier CSV.');
|
||||
}
|
||||
|
||||
// Ajouter les en-têtes du CSV
|
||||
fputcsv($file, ['Artisan', 'N° Téléphone', 'Mail', 'Adresse', 'Site web', 'TVA', 'Conseiller', 'Action requise', 'Précision pour contact', 'ID de l\'artisan', 'Date de création',]);
|
||||
|
||||
// Boucler sur chaque post et écrire les données dans le CSV
|
||||
foreach ($artisans->posts as $artisan) {
|
||||
|
||||
$postID = $artisan->ID;
|
||||
$phoneNumber = get_field('phone_number', $postID);
|
||||
$formattedPhoneNumber = preg_replace('/^(\+\d{2})(\d{3})(\d{2})(\d{2})(\d{2})$/', '$1 $2 $3 $4 $5', $phoneNumber);
|
||||
$email = get_field('email', $postID);
|
||||
$website = get_field('website', $postID);
|
||||
$vat_number = get_field('vat_number', $postID);
|
||||
$tva = $vat_number ? format_belgian_vat_number($vat_number) : '';
|
||||
|
||||
$adresseDatas = get_field('adresse', $postID);
|
||||
$street_number = isset($adresseDatas['street_number']) ? $adresseDatas['street_number'] . ' ' : '';
|
||||
$street_name = isset($adresseDatas['street_name']) ? $adresseDatas['street_name'] . ', ' : '';
|
||||
$post_code = isset($adresseDatas['post_code']) ? $adresseDatas['post_code'] . ' ' : '';
|
||||
$city = isset($adresseDatas['city']) ? $adresseDatas['city'] : '';
|
||||
$adresse = $street_number . $street_name . $post_code . ' ' . $city;
|
||||
|
||||
|
||||
$conseiller = getArtisanConseillerName($postID) ?? '';
|
||||
$requiredAction = get_field('required_action', $postID);
|
||||
$requiredAction = $requiredAction ? 'Oui' : '';
|
||||
$contactComments = get_field('contact_comments', $postID);
|
||||
|
||||
fputcsv($file, [
|
||||
$artisan->post_title,
|
||||
$formattedPhoneNumber,
|
||||
$email,
|
||||
$adresse,
|
||||
$website,
|
||||
$tva,
|
||||
$conseiller,
|
||||
$requiredAction,
|
||||
$contactComments,
|
||||
$artisan->ID,
|
||||
$artisan->post_date,
|
||||
]);
|
||||
}
|
||||
|
||||
// Fermer le fichier
|
||||
fclose($file);
|
||||
|
||||
// Télécharger le fichier CSV
|
||||
header('Content-Type: text/csv');
|
||||
header('Content-Disposition: attachment;filename="' . $filename . '"');
|
||||
readfile($filename);
|
||||
|
||||
// Supprimer le fichier après téléchargement
|
||||
unlink($filename);
|
||||
|
||||
exit();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user