203 lines
7.0 KiB
PHP
203 lines
7.0 KiB
PHP
<?php
|
|
/**
|
|
* Gestionnaire des shortcodes pour le plugin ESI_creditDirect
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
class ESI_CreditDirect_Shortcodes {
|
|
|
|
public function __construct() {
|
|
add_action('init', array($this, 'register_shortcodes'));
|
|
}
|
|
|
|
/**
|
|
* Enregistrement de tous les shortcodes
|
|
*/
|
|
public function register_shortcodes() {
|
|
add_shortcode('credit_manager_modal', array($this, 'credit_manager_modal_shortcode'));
|
|
add_shortcode('credit_manager_table', array($this, 'credit_manager_table_shortcode'));
|
|
}
|
|
|
|
/**
|
|
* Shortcode pour afficher le modal de gestion des crédits
|
|
* Usage: [credit_manager_modal]
|
|
*/
|
|
public function credit_manager_modal_shortcode($atts) {
|
|
$atts = shortcode_atts(array(
|
|
'mode' => 'create', // 'create' ou 'edit'
|
|
'credit_id' => 0
|
|
), $atts);
|
|
|
|
// Inclure le template du modal
|
|
ob_start();
|
|
include plugin_dir_path(__FILE__) . '../../templates/modules/credit-manager-edit-form.php';
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* Shortcode pour afficher le tableau de gestion des crédits
|
|
* Usage: [credit_manager_table]
|
|
*/
|
|
public function credit_manager_table_shortcode($atts) {
|
|
$atts = shortcode_atts(array(
|
|
'show_actions' => 'true',
|
|
'limit' => 50
|
|
), $atts);
|
|
|
|
// Récupérer les crédits
|
|
$credits = $this->get_credits($atts['limit']);
|
|
|
|
// Inclure le template du tableau
|
|
ob_start();
|
|
include plugin_dir_path(__FILE__) . '../../templates/admin/credit_manager_table.php';
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* Récupérer la liste des crédits
|
|
*/
|
|
private function get_credits($limit = 50) {
|
|
global $wpdb;
|
|
|
|
// TODO: Remplacer par la vraie table des crédits
|
|
$table_name = $wpdb->prefix . 'credit_direct_credits';
|
|
|
|
$sql = $wpdb->prepare("
|
|
SELECT * FROM {$table_name}
|
|
ORDER BY date DESC
|
|
LIMIT %d
|
|
", $limit);
|
|
|
|
return $wpdb->get_results($sql);
|
|
}
|
|
|
|
/**
|
|
* Traitement AJAX pour créer un crédit
|
|
*/
|
|
public function ajax_create_credit() {
|
|
check_ajax_referer('credit_manager_action', 'nonce');
|
|
|
|
$data = array(
|
|
'title' => sanitize_text_field($_POST['title']),
|
|
'nom' => sanitize_text_field($_POST['nom']),
|
|
'prenom' => sanitize_text_field($_POST['prenom']),
|
|
'adresse' => sanitize_textarea_field($_POST['adresse']),
|
|
'localite' => sanitize_text_field($_POST['localite']),
|
|
'email' => sanitize_email($_POST['email']),
|
|
'telephone' => sanitize_text_field($_POST['telephone']),
|
|
'gsm' => sanitize_text_field($_POST['gsm']),
|
|
'societe_credit' => sanitize_text_field($_POST['societe_credit']),
|
|
'montant' => floatval($_POST['montant']),
|
|
'date' => sanitize_text_field($_POST['date']),
|
|
'signature' => sanitize_text_field($_POST['signature']),
|
|
'numero_dossier' => sanitize_text_field($_POST['numero_dossier']),
|
|
'code' => sanitize_text_field($_POST['code']),
|
|
'remarques' => sanitize_textarea_field($_POST['remarques']),
|
|
'created_at' => current_time('mysql')
|
|
);
|
|
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'credit_direct_credits';
|
|
|
|
$result = $wpdb->insert($table_name, $data);
|
|
|
|
if ($result) {
|
|
wp_send_json_success(array(
|
|
'message' => 'Crédit créé avec succès',
|
|
'credit_id' => $wpdb->insert_id
|
|
));
|
|
} else {
|
|
wp_send_json_error(array(
|
|
'message' => 'Erreur lors de la création du crédit'
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Traitement AJAX pour mettre à jour un crédit
|
|
*/
|
|
public function ajax_update_credit() {
|
|
check_ajax_referer('credit_manager_action', 'nonce');
|
|
|
|
$credit_id = intval($_POST['credit_id']);
|
|
|
|
if (!$credit_id) {
|
|
wp_send_json_error(array('message' => 'ID de crédit invalide'));
|
|
}
|
|
|
|
$data = array(
|
|
'title' => sanitize_text_field($_POST['title']),
|
|
'nom' => sanitize_text_field($_POST['nom']),
|
|
'prenom' => sanitize_text_field($_POST['prenom']),
|
|
'adresse' => sanitize_textarea_field($_POST['adresse']),
|
|
'localite' => sanitize_text_field($_POST['localite']),
|
|
'email' => sanitize_email($_POST['email']),
|
|
'telephone' => sanitize_text_field($_POST['telephone']),
|
|
'gsm' => sanitize_text_field($_POST['gsm']),
|
|
'societe_credit' => sanitize_text_field($_POST['societe_credit']),
|
|
'montant' => floatval($_POST['montant']),
|
|
'date' => sanitize_text_field($_POST['date']),
|
|
'signature' => sanitize_text_field($_POST['signature']),
|
|
'numero_dossier' => sanitize_text_field($_POST['numero_dossier']),
|
|
'code' => sanitize_text_field($_POST['code']),
|
|
'remarques' => sanitize_textarea_field($_POST['remarques']),
|
|
'updated_at' => current_time('mysql')
|
|
);
|
|
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'credit_direct_credits';
|
|
|
|
$result = $wpdb->update(
|
|
$table_name,
|
|
$data,
|
|
array('id' => $credit_id),
|
|
array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%s', '%s', '%s', '%s', '%s', '%s'),
|
|
array('%d')
|
|
);
|
|
|
|
if ($result !== false) {
|
|
wp_send_json_success(array('message' => 'Crédit mis à jour avec succès'));
|
|
} else {
|
|
wp_send_json_error(array('message' => 'Erreur lors de la mise à jour du crédit'));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Traitement AJAX pour récupérer un crédit
|
|
*/
|
|
public function ajax_get_credit() {
|
|
check_ajax_referer('credit_manager_action', 'nonce');
|
|
|
|
$credit_id = intval($_POST['credit_id']);
|
|
|
|
if (!$credit_id) {
|
|
wp_send_json_error(array('message' => 'ID de crédit invalide'));
|
|
}
|
|
|
|
global $wpdb;
|
|
$table_name = $wpdb->prefix . 'credit_direct_credits';
|
|
|
|
$credit = $wpdb->get_row($wpdb->prepare(
|
|
"SELECT * FROM {$table_name} WHERE id = %d",
|
|
$credit_id
|
|
));
|
|
|
|
if ($credit) {
|
|
wp_send_json_success($credit);
|
|
} else {
|
|
wp_send_json_error(array('message' => 'Crédit non trouvé'));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Initialiser la classe
|
|
new ESI_CreditDirect_Shortcodes();
|
|
|
|
// Enregistrer les actions AJAX
|
|
add_action('wp_ajax_create_credit', array('ESI_CreditDirect_Shortcodes', 'ajax_create_credit'));
|
|
add_action('wp_ajax_update_credit', array('ESI_CreditDirect_Shortcodes', 'ajax_update_credit'));
|
|
add_action('wp_ajax_get_credit', array('ESI_CreditDirect_Shortcodes', 'ajax_get_credit'));
|