Files
2024_WEBSITE_fipf/plugins/cipf_plugin/php/profs_states.php

398 lines
12 KiB
PHP

<?php
/*
* it means someone outside wp is accessing the file, in this case kill it.
*/
if (!defined('ABSPATH')) {
die('You can not access this file!');
}
/*
* [/] etat compte ('etat_compte') :
* 1. new . 'nouveau prof' -> ok 1/1 : [1: at inscription - ok]
* 2. to_pay . 'doit payer' -> ko 3/4 : [1: after form & new - ok], [2: after form choose paypal & expired - ok], [3: after payment failed - ok], [4: after transfert failed - ko]
* 3. valid . 'carte valide' -> ko 1/2 : [1: after payment success - ok], [2: after transfert success - ko]
* 4. waiting_invalid . 'en attente invalide' -> ko 3/4 : [1: after form choose transfert & expired - ok], [2: after card changes to expired & was waiting valid - ok], [3: verify at prof profil page - ok], [4: when event for expire fire - ko]
* 5. waiting_valid . 'en attente valide' -> ok 1/1 : [1: after form choose transfert & not expired - ok]
* 6. expired . 'carte expiree' -> ko 1/2 : [1: when prof access profil & expired - ok], [2: when event card expired fire - ko]
*
* [/] etat carte ('etat_carte') :
* - 'Commande' -> ok 1/1 : [1: at inscription - ok]
* - 'Renouvellement' -> ko 1/1 : [1: after succees payement - ok]
*
* [ ] activation du compte ('compte-actif') :
* - 'Actif' -> equivalent a etat compte [3] carte valide
* - 'Inactif' -> equivalent a etat compte [2] doit payer
*
* [/] etat paiement ('etat_paiement') :
* - 'en_cours' -> ok 1/1 : [1: start payment - ok]
* - 'reussi' -> ok 1/1 : [1: after payment success - ok]
* - 'echec' -> ok 1/1 : [1: after payment failure - ok]
* - 'aucun' -> ok 2/2 : [1: default - ko], [2: after payment message is shown one time on profil page - ok]
*
* [/] type paiement ('paiement') :
* - 'Paypal' -> ok 1/1 : [1: modified by diviformbuilder at form validation - ok]
* - 'Virement' -> ok 1/1 : [1: modified by diviformbuilder at form validation - ok]
*
* [/] numero de carte ('numero_de_la_carte') -> ok 1/1 : [1: after payment & card is 'commande' - ok]
*
* [/] cgv
*
* [/] etat_virement -> ko 0/2 : [1: at form validation - ko], [2: check on profil page - ko]
*
*/
function get_field_init_CIPF($acf_field_name, $acf_id) {
$acf_state = get_field($acf_field_name, $acf_id);
if ($acf_state !== null) {
return $acf_state;
}
/*
* if get_field returns null, it means it is not initialized
* - initialize it with 'temp' value
* - then find it's default value, and update with it
* - if no default value, update with first value
*
*/
update_field($acf_field_name, 'temp', $acf_id);
$acf_object = get_field_object($acf_field_name, $acf_id);
$default = $acf_object['default_value'];
if (empty($default)) {
$choices = $acf_object['choices'];
if (!is_array($choices)) {
return false;
}
$default = reset($choices);
}
update_field($acf_field_name, $default, $acf_id);
$acf_state = get_field($acf_field_name, $acf_id);
return $acf_state;
}
/*
* global 'setter' and 'izzer' for this file
*
*/
function is_acf_state_CIPF($acf_field, $state_name, $user_id = null) {
PLGNTLS_class::debug_infos();
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
/*
* when acf fields have not been initated a first time, you can't find them by name
* - one solution is to use key instead
* but it means knowing the key, in my case it prevents fabien to create a field himself
* - another solution would be to :
* try if is null,
* if yes it means it was not initalize,
* then initalize with any value,
* find the default value,
* and assign it
*
$acf_state = get_field($acf_field['_key'], $acf_id);
*/
$acf_state = get_field_init_CIPF($acf_field['_name'], $acf_id);;
if ($acf_state === $acf_field[$state_name]) {
return true;
}
return false;
}
function set_acf_state_CIPF($acf_field, $state_name, $user_id = null) {
PLGNTLS_class::debug_infos();
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
update_field($acf_field['_name'], $acf_field[$state_name], $acf_id);
}
/*
* etat compte ('etat_compte') :
* 1. new . 'nouveau prof' -> ok : [1: a l'inscription - ok]
* 2. to_pay . 'doit payer' -> ko : [1: apres premier form - ok], [2: apres form qui suit carte expiree - ok], [3: apres paiement immediat echoue - ko], [4: apres virement echoue - ko]
* 3. valid . 'carte valide' ->
* 4. waiting_invalid . 'en attente invalide' ->
* 5. waiting_valid . 'en attente valide' ->
* 6. expired . 'carte expiree' ->
*
* receives the name of the state, and compare it to the current state
* either use the is_acf_card_state_CIPF() function directly,
* or one of the specific functions
*
*/
function is_account_state_CIPF($state_name, $user_id = null) {
PLGNTLS_class::debug_infos();
$acf_account_state = PLGNTLS_class::ACF_ACCOUNT_STATE;
return is_acf_state_CIPF($acf_account_state, $state_name, $user_id);
}
function is_account_new_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF('new', $user_id);
}
function is_account_to_pay_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF('to_pay', $user_id);
}
function is_account_valid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF('valid', $user_id);
}
function is_account_waiting_invalid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF('waiting_invalid', $user_id);
}
function is_account_waiting_valid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF('waiting_valid', $user_id);
}
function is_account_expired_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_account_state_CIPF('expired', $user_id);
}
/*
* setters :
*/
function set_account_state_CIPF($state_name, $user_id = null) {
PLGNTLS_class::debug_infos();
$acf_account_state = PLGNTLS_class::ACF_ACCOUNT_STATE;
set_acf_state_CIPF($acf_account_state, $state_name, $user_id);
}
function set_account_new_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF('new', $user_id);
}
function set_account_to_pay_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF('to_pay', $user_id);
}
function set_account_valid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF('valid', $user_id);
}
function set_account_waiting_invalid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF('waiting_invalid', $user_id);
}
function set_account_waiting_valid_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF('waiting_valid', $user_id);
}
function set_account_expired_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_account_state_CIPF('expired', $user_id);
}
/*
* etat carte ('etat_carte') :
* - 'Commande'
* - 'Renouvellement'
*
*/
function is_card_new_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_state = PLGNTLS_class::ACF_CARD_STATE;
return is_acf_state_CIPF($acf_card_state, 'new', $user_id);
}
function is_card_renew_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_state = PLGNTLS_class::ACF_CARD_STATE;
return is_acf_state_CIPF($acf_card_state, 'renew', $user_id);
}
function set_card_new_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_state = PLGNTLS_class::ACF_CARD_STATE;
set_acf_state_CIPF($acf_card_state, 'new', $user_id);
}
function set_card_renew_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_state = PLGNTLS_class::ACF_CARD_STATE;
set_acf_state_CIPF($acf_card_state, 'renew', $user_id);
}
/*
* type paiement ('paiement') :
* - 'paypal'=>'Paypal'
* - 'transfert'=>'Virement'
*
*/
function is_payment_method_paypal_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_payment_method = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
return is_acf_state_CIPF($acf_card_payment_method, 'paypal', $user_id);
}
function is_payment_method_transfert_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_payment_method = PLGNTLS_class::ACF_CARD_PAYMENT_METHOD;
return is_acf_state_CIPF($acf_card_payment_method, 'transfert', $user_id);
}
/*
* etat paiement ('etat_paiement') :
* - 'en_cours'
* - 'reussi'
* - 'echec'
* - 'aucun'
*
* 'started'=>'en_cours', 'success'=>'reussi', 'failure'=>'echec', 'nothing'=>'aucun'
*
*/
function is_payment_state_CIPF($type, $user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_payment_state = PLGNTLS_class::ACF_CARD_PAYMENT_STATE;
return is_acf_state_CIPF($acf_card_payment_state, $type, $user_id);
}
function is_payment_started_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_payment_state_CIPF('started', $user_id);
}
function is_payment_success_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_payment_state_CIPF('success', $user_id);
}
function is_payment_failure_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_payment_state_CIPF('failure', $user_id);
}
function is_payment_nothing_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
return is_payment_state_CIPF('nothing', $user_id);
}
/*
* setters
*/
function set_payment_state_CIPF($type, $user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_payment_state = PLGNTLS_class::ACF_CARD_PAYMENT_STATE;
set_acf_state_CIPF($acf_card_payment_state, $type, $user_id);
}
function set_payment_started_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_payment_state_CIPF('started', $user_id);
}
function set_payment_success_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_payment_state_CIPF('success', $user_id);
}
function set_payment_failure_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_payment_state_CIPF('failure', $user_id);
}
function set_payment_nothing_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
set_payment_state_CIPF('nothing', $user_id);
}
/*
* numero de carte
*
*/
function set_card_number_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_card_number = PLGNTLS_class::ACF_CARD_NUMBER;
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
$date_now = date_create('today');
$card_id = $date_now->format('Ymd') . $user_id;
update_field($acf_card_number['_name'], $card_id, $acf_id);
}
/*
* cgv
*
*/
function reset_acf_cgv_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_cgv = PLGNTLS_class::ACF_PROF_CGV;
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
update_field($acf_cgv['_name'], array(""), $acf_id);
}
/*
* etat virement
*
*/
function is_transfert_success_CIPF($user_id = null) {
PLGNTLS_class::debug_infos();
$acf_transfert_state = PLGNTLS_class::ACF_TRANSFERT_STATE;
if (is_null($user_id)) {
$user_id = get_current_user_id();
}
$acf_id = 'user_'.$user_id;
$transfert_state = get_field($acf_transfert_state['_name'], $acf_id);
/*
* before first use : returns null
* if checked : returns value
* if unchecked : returns empty
*/
if (empty($transfert_state)) {
return false;
}
if (is_null($transfert_state)) {
return false;
}
if (reset($transfert_state) === $acf_transfert_state['success']) {
return true;
}
return false;
}
?>