diff --git a/modules/dolibarr/doli-associate/action/class-doli-associate-action.php b/modules/dolibarr/doli-associate/action/class-doli-associate-action.php index a58812a..5b6b182 100644 --- a/modules/dolibarr/doli-associate/action/class-doli-associate-action.php +++ b/modules/dolibarr/doli-associate/action/class-doli-associate-action.php @@ -43,7 +43,7 @@ public function load_associate_modal() { $id = ! empty( $_POST['wp_id'] ) ? (int) $_POST['wp_id'] : 0; $type = ! empty( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : ''; - + $sync_info = Doli_Sync::g()->get_sync_infos( $type ); $entries = Request_Util::get( $sync_info['endpoint'] . '?limit=-1' ); @@ -59,24 +59,25 @@ public function load_associate_modal() { } } } - - ob_start(); - View_Util::exec( 'wpshop', 'doli-associate', 'main', array( - 'entries' => $entries, - 'wp_id' => $id, - 'type' => $type, - 'label' => $sync_info['title'], - ) ); - $view = ob_get_clean(); - - ob_start(); - View_Util::exec( 'wpshop', 'doli-associate', 'single-footer' ); - $buttons_view = ob_get_clean(); - - wp_send_json_success( array( - 'view' => $view, - 'buttons_view' => $buttons_view, - ) ); + if ($type == 'wps-product') { + ob_start(); + View_Util::exec( 'wpshop', 'doli-associate', 'main', array( + 'entries' => $entries, + 'wp_id' => $id, + 'type' => $type, + 'label' => $sync_info['title'], + ) ); + $view = ob_get_clean(); + + ob_start(); + View_Util::exec( 'wpshop', 'doli-associate', 'single-footer' ); + $buttons_view = ob_get_clean(); + + wp_send_json_success( array( + 'view' => $view, + 'buttons_view' => $buttons_view, + ) ); + } } /** diff --git a/modules/dolibarr/doli-categories/action/class-doli-categories-action.php b/modules/dolibarr/doli-categories/action/class-doli-categories-action.php new file mode 100644 index 0000000..f284099 --- /dev/null +++ b/modules/dolibarr/doli-categories/action/class-doli-categories-action.php @@ -0,0 +1,169 @@ + + * @copyright (c) 2011-2020 Eoxia . + * @since 2.1.0 + * @version 2.1.0 + */ + +namespace wpshop; + +use eoxia\LOG_Util; +use eoxia\View_Util; +use stdClass; + +defined( 'ABSPATH' ) || exit; + +/** + * Doli Category Action Class. + */ +class Doli_Category_Action { + + /** + * Définition des metaboxes sur la page. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @var array + */ + public $metaboxes = null; + + /** + * Le constructeur. + * + * @since 2.1.0 + * @version 2.1.0 + */ + public function __construct() { + + add_action( 'admin_menu', array( $this, 'callback_admin_menu' ), 60 ); + + add_action( 'wps_checkout_create_category', array( $this, 'create_category' ), 10, 1 ); + add_action( 'admin_post_wps_download_category', array( $this, 'download_category' ) ); + + } + + /** + * Initialise la page "Catégories". + * + * @since 2.1.0 + * @version 2.1.0 + */ + public function callback_admin_menu() { + if ( Settings::g()->dolibarr_is_active() ) { + $hook = add_submenu_page( 'wpshop', __( 'Categories', 'wpshop' ), __( 'Categories', 'wpshop' ), 'manage_options', 'wps-product-cat', array( $this, 'callback_add_menu_page' ) ); + + if ( ! isset( $_GET['id'] ) ) { + add_action( 'load-' . $hook, array( $this, 'callback_add_screen_option' ) ); + } + } + } + + /** + * Affichage de la vue du menu. + * + * @since 2.1.0 + * @version 2.1.0 + */ + public function callback_add_menu_page() { + global $wpdb; + if ( isset( $_GET['id'] ) ) { + // Single page. + $id = ! empty( $_GET['id'] ) ? (int) $_GET['id'] : 0; + + $doli_category = Request_Util::get( 'categories/' . $id ); + $wp_category = Doli_Category::g()->get( array( 'schema' => true ), true ); + $wp_category = Doli_Category::g()->doli_to_wp( $doli_category, $wp_category); + + $wp_category->data['datec'] = \eoxia\Date_Util::g()->fill_date( $wp_category->data['datec'] ); + + $third_party = Third_Party::g()->get( array( 'id' => $wp_category->data['parent_id'] ), true ); + + View_Util::exec( 'wpshop', 'doli-categories', 'single', array( + 'third_party' => $third_party, + 'category' => $wp_category, + ) ); + } else { + // Listing page. + // @todo: Doublon avec Class Doli Category display() ? + $per_page = get_user_meta( get_current_user_id(), Doli_Category::g()->option_per_page, true ); + $dolibarr_option = get_option( 'wps_dolibarr', Settings::g()->default_settings ); + $dolibarr_url = $dolibarr_option['dolibarr_url']; + + $dolibarr_create_category = 'categories/card.php?action=create&type=product&backtopage=%2Fdolibarr%2Fhtdocs%2Fcategories%2Findex.php%3Ftype%3Dproduct'; + + if ( empty( $per_page ) || 1 > $per_page ) { + $per_page = Doli_Category::g()->limit; + } + + $s = ! empty( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : ''; + + $count = Doli_Category::g()->search( $s, array(), true ); + $number_page = ceil( $count / $per_page ); + $current_page = isset( $_GET['current_page'] ) ? (int) $_GET['current_page'] : 1; + + $base_url = admin_url( 'admin.php?page=wps-product-cat' ); + + $begin_url = $base_url . '¤t_page=1'; + $end_url = $base_url . '¤t_page=' . $number_page; + + $prev_url = $base_url . '¤t_page=' . ( $current_page - 1 ); + $next_url = $base_url . '¤t_page=' . ( $current_page + 1 ); + + if ( ! empty( $s ) ) { + $begin_url .= '&s=' . $s; + $end_url .= '&s=' . $s; + $prev_url .= '&s=' . $s; + $next_url .= '&s=' . $s; + } + + $wp_categories = Doli_Category::g()->get(); + + if ( ! empty($wp_categories)) { + foreach( $wp_categories as $wp_category) { + if (empty($wp_category->data['external_id'] )) { + wp_delete_term($wp_category->data['id'],'wps-product-cat'); + } + } + } + + View_Util::exec( 'wpshop', 'doli-categories', 'main', array( + 'number_page' => $number_page, + 'current_page' => $current_page, + 'count' => $count, + 'begin_url' => $begin_url, + 'end_url' => $end_url, + 'prev_url' => $prev_url, + 'next_url' => $next_url, + 's' => $s, + 'wp_categories'=> $wp_categories, + + 'dolibarr_create_category' => $dolibarr_create_category, + 'dolibarr_url' => $dolibarr_url, + ) ); + } + } + + /** + * Ajoute le menu "Options de l'écran". + * + * @since 2.1.0 + * @version 2.1.0 + */ + public function callback_add_screen_option() { + add_screen_option( + 'per_page', + array( + 'label' => _x( 'Categories', 'Category per page', 'wpshop' ), + 'default' => Doli_Category::g()->limit, + 'option' => Doli_Category::g()->option_per_page, + ) + ); + } +} + +new Doli_Category_Action(); diff --git a/modules/dolibarr/doli-categories/action/index.php b/modules/dolibarr/doli-categories/action/index.php new file mode 100644 index 0000000..6220032 --- /dev/null +++ b/modules/dolibarr/doli-categories/action/index.php @@ -0,0 +1,2 @@ + + * @copyright (c) 2011-2020 Eoxia . + * @since 2.1.0 + * @version 2.1.0 + */ + +namespace wpshop; + +use eoxia\Term_Class; +use eoxia\View_Util; +use stdClass; + +defined( 'ABSPATH' ) || exit; + +/** + * Doli Category Class. + */ +class Doli_Category extends Term_Class { + + /** + * Le nom du modèle. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @var string + */ + protected $model_name = '\wpshop\Doli_Category_Model'; + + /** + * Le term type. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @var string + */ + protected $type = 'wps-product-cat'; + + /** + * La clé principale du modèle. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @var string + */ + protected $meta_key = 'doli-categories'; + + /** + * La route pour accéder à l'objet dans la rest API. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @var string + */ + protected $base = 'doli-categories'; + + /** + * Le nom du term type. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @var string + */ + protected $post_type_name = 'Doli Category'; + + /** + * La limite par page. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @var integer + */ + public $limit = 10; + + /** + * Le nom de l'option pour la limite par page. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @var string + */ + public $option_per_page = 'category_doli_per_page'; + + /** + * Appel la vue "list" du module "doli-categories". + * + * @since 2.1.0 + * @version 2.1.0 + */ + public function display() { + global $wpdb; + $dolibarr_option = get_option( 'wps_dolibarr', Settings::g()->default_settings ); + $per_page = get_user_meta( get_current_user_id(), Doli_Category::g()->option_per_page, true ); + + if ( empty( $per_page ) || 1 > $per_page ) { + $per_page = Doli_Category::g()->limit; + } + + $current_page = isset( $_GET['current_page'] ) ? (int) $_GET['current_page'] : 1; + + $s = ! empty( $_GET['s'] ) ? sanitize_text_field( $_GET['s'] ) : ''; + + $route = 'categories?sortfield=t.rowid&sortorder=DESC&limit=' . $per_page . '&page=' . ( $current_page - 1 ); + + if ( ! empty( $s ) ) { + // La route de dolibarr ne fonctionne pas avec des caractères en base10 + $route .= '&sqlfilters=(t.ref%3Alike%3A\'%25' . $s . '%25\')'; + } + + $wp_categories = Doli_Category::g()->get(); + + if ( ! empty($wp_categories)) { + foreach( $wp_categories as $wp_category) { + if (empty($wp_category->data['external_id'] )) { + wp_delete_term($wp_category->data['id'],'wps-product-cat'); + } + } + } + + View_Util::exec( 'wpshop', 'doli-categories', 'list', array( + 'categories' => $wp_categories, + 'doli_url' => $dolibarr_option['dolibarr_url'], + ) ); + } + + /** + * Appel la vue "item" d'une catégorie. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @param Doli_Category $category Les données d'une catégorie. + * @param string $doli_url L'url de Dolibarr. + */ + public function display_item( $category, $doli_url = '' ) { + View_Util::exec( 'wpshop', 'doli-categories', 'item', array( + 'category' => $category, + ) ); + } + + /** + * Convertit un tableau Category Object provenant de Dolibarr vers un format Category Object WPshop afin de normisé pour l'affichage. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @param stdClass $doli_categories Le tableau contenant toutes les données des catégories provenant de Dolibarr. + * + * @return Doli_Category Le tableau contenant toutes les données des catégories convertis depuis le format de Dolibarr. + */ + public function convert_to_wp_category_format( $doli_categories ) { + $wp_categories = array(); + + if ( ! empty( $doli_categories ) ) { + foreach ( $doli_categories as $doli_category ) { + $wp_category = $this->get( array( 'schema' => true ), true ); + $wp_categories[] = $this->doli_to_wp( $doli_category, $wp_category); + } + } + + return $wp_categories; + } + + /** + * Synchronise depuis Dolibarr vers WP. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @param stdClass $doli_category Les données d'une catégorie Dolibarr. + * @param Doli_Category $wp_category Les données d'une catégorie WordPress. + * @param boolean $save Enregistres les données sinon renvoies l'objet remplit sans l'enregistrer en base de donnée. + * @param array $notices Gestion des erreurs et informations de l'évolution de la méthode. * + * @return Doli_Category Les données d'une catégorie WordPress avec ceux de Dolibarr. + */ + public function doli_to_wp( $doli_category, $wp_category, $save = true, &$notices = array( + 'errors' => array(), + 'messages' => array(), + ) ) { + $category = null; + $doli_category = Request_Util::get( 'categories/' . $doli_category->id ); // Charges par la route single des factures pour avoir accès à linkedObjectsIds->commande. + + $wp_category->data['external_id'] = (int) $doli_category->id; + + $wp_category->data['name'] = $doli_category->label; + if ( ! empty($doli_category->array_options->options__wps_slug) ) { + $wp_category->data['slug'] = $doli_category->array_options->options__wps_slug; + } + if ( ! empty($doli_category->array_options->options__wps_id) ) { + $wp_category->data['id'] = (int) $doli_category->array_options->options__wps_id; + } + + $wp_category = Doli_Category::g()->update( $wp_category->data ); + + if ( $save ) { + $data_sha = array(); + $data_sha['doli_id'] = (int) $wp_category->data['external_id']; + $data_sha['wp_id'] = $wp_category->data['id']; + $data_sha['name'] = $wp_category->data['name']; + $data_sha['slug'] = $wp_category->data['slug']; + + $wp_category->data['sync_sha_256'] = hash( 'sha256', implode( ',', $data_sha ) ); + //@todo save_post utilisé ? + + update_term_meta( $wp_category->data['id'], '_sync_sha_256', $wp_category->data['sync_sha_256'] ); + update_term_meta( $wp_category->data['id'], '_external_id', (int) $doli_category->id ); + $notices['messages'][] = sprintf( __( 'Erase data for the product %s with the dolibarr data', 'wpshop' ), $wp_category->data['name'] ); + } + + return $wp_category; + } + + /** + * Fonction de recherche. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @param string $s Le terme de la recherche. + * @param array $default_args Les arguments par défaut. + * @param boolean $count Si true compte le nombre d'élement, sinon renvoies l'ID des éléments trouvés. + * + * @return array|integer Les ID des éléments trouvés ou le nombre d'éléments trouvés. + */ + public function search( $s = '', $default_args = array(), $count = false ) { + $route = 'categories?sortfield=t.rowid&sortorder=DESC'; + + if ( ! empty( $s ) ) { + $route .= '&sqlfilters=(t.ref%3Alike%3A\'%25' . $s . '%25\')'; + } + + $doli_categories = Request_Util::get( $route ); + + if ( $count && ! empty( $doli_categories ) ) { + return count( $doli_categories ); + } else { + return 0; + } + } +} + +Doli_Category::g(); diff --git a/modules/dolibarr/doli-categories/class/index.php b/modules/dolibarr/doli-categories/class/index.php new file mode 100644 index 0000000..6220032 --- /dev/null +++ b/modules/dolibarr/doli-categories/class/index.php @@ -0,0 +1,2 @@ + + * @copyright (c) 2011-2020 Eoxia . + * @since 2.1.0 + * @version 2.1.0 + */ + +namespace wpshop; + +defined( 'ABSPATH' ) || exit; + +/** + * Dolibarr Category Filter Class. + */ +class Doli_Category_Filter { + + /** + * Le constructeur. + * + * @since 2.1.0 + * @version 2.1.0 + */ + public function __construct() { + if ( Settings::g()->dolibarr_is_active() ) { + //add_filter( 'wps-product-cat_after_get', array( $this, 'auto_sync' ), 10, 2 ); + add_filter( 'wps_category_filter_sync', array( $this, 'category_sync' ), 10, 1 ); + } + } + + /** + * La synchronisation automatique d'une catégorie. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @param Doli_Category $object Les données d'une catégorie. + * @param array $args_cb Les arguments supplémentaires. + * + * @return Doli_Category Les données d'une catégorie synchronisé. + */ + public function auto_sync( $object, $args_cb ) { + if ( empty( $object->data['external_id'] ) ) { + return $object; + } + $status = Doli_Sync::g()->check_status( $object->data['id'], $object->data['type'] ); + + if ($status['status_code'] != '0x3') { + return $object; + } + + $doli_category = Request_Util::get( 'categories/' . $object->data['external_id'] ); + $object = Doli_Category::g()->doli_to_wp( $doli_category, $object ); + + return $object; + } + + /** + * La synchronisation manuelle d'unecatégorie. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @param Doli_Category $category Les données d'une catégorie. + * + * @return Doli_Category Les données d'une catégorie synchronisé. + */ + public function category_sync( $category ) { + $external_id = get_term_meta( $category->ID, '_external_id', true ); + $status = Doli_Sync::g()->check_status( $category->ID, $category->term_type ); + + if ($status['status_code'] != '0x3') { + return $category; + } + + $doli_category = Request_Util::get( 'categories/' . $external_id ); + $object = Doli_Category::g()->get( array( 'id' => $category->ID ), true ); + $object = Doli_Category::g()->doli_to_wp( $doli_category, $object ); + + $category = get_term( $category->ID ); + + return $category; + } +} + +new Doli_Category_Filter(); diff --git a/modules/dolibarr/doli-categories/filter/index.php b/modules/dolibarr/doli-categories/filter/index.php new file mode 100644 index 0000000..6220032 --- /dev/null +++ b/modules/dolibarr/doli-categories/filter/index.php @@ -0,0 +1,2 @@ + + * @copyright (c) 2011-2020 Eoxia . + * @since 2.1.0 + * @version 2.1.0 + */ + +namespace wpshop; + +use eoxia\Term_Model; + +defined( 'ABSPATH' ) || exit; + +/** + * Doli Category Model Class. + */ +class Doli_Category_Model extends Term_Model { + + /** + * Le constructeur. + * + * @since 2.1.0 + * @version 2.1.0 + * + * @param Doli_Category $object Les données de l'objet. + * @param string $req_method La méthode de la requête. + */ + public function __construct( $object, $req_method = null ) { + $this->schema['external_id'] = array( + 'type' => 'integer', + 'meta_type' => 'single', + 'field' => '_external_id', + 'since' => '2.1.0', + 'version' => '2.1.0', + 'version' => '2.1.0', + 'description' => 'L\'ID provenant de dolibarr', + ); + + $this->schema['sync_sha_256'] = array( + 'type' => 'string', + 'meta_type' => 'single', + 'field' => '_sync_sha_256', + 'since' => '2.1.0', + 'version' => '2.1.0', + 'description' => 'La cohérence de la synchronisation avec un ERP', + ); + + $this->schema['datec'] = array( + 'type' => 'wpeo_date', + 'meta_type' => 'single', + 'field' => 'datec', + 'context' => array( 'GET' ), + 'since' => '2.1.0', + 'version' => '2.1.0', + 'description' => 'Date de création de la fature. Relation avec dolibarr', + ); + + $this->schema['date_category'] = array( + 'type' => 'wpeo_date', + 'meta_type' => 'single', + 'field' => 'date_category', + 'context' => array( 'GET' ), + 'since' => '2.1.0', + 'version' => '2.1.0', + 'description' => 'Date de la fature. Relation avec dolibarr', + ); + + parent::__construct( $object, $req_method ); + } +} diff --git a/modules/dolibarr/doli-categories/model/index.php b/modules/dolibarr/doli-categories/model/index.php new file mode 100644 index 0000000..6220032 --- /dev/null +++ b/modules/dolibarr/doli-categories/model/index.php @@ -0,0 +1,2 @@ + + * @copyright (c) 2011-2020 Eoxia . + * @since 2.1.0 + * @version 2.1.0 + */ + +namespace wpshop; + +defined( 'ABSPATH' ) || exit; + +/** + * Documentation des variables utilisées dans la vue. + * + * @var Doli_Category $category Les données d'une facture. + * @var string $doli_url L'url de Dolibarr. + */ + +?> + +
+
+
    + data['external_id'] ) ) : ?> +
  • Doli : data['external_id'] ); ?>
  • + +
  • data['datec']['rendered']['date_time'] ); ?>
  • +
+
    + data['name'] ) ) : ?> + data['name'] ); ?> + - +
+
    +
  • + data['external_id'] ) ) : ?> +
  • + +
+
+
+ data['name'] ) ) : ?> + data['name'] ); ?> + - +
+
+ data['slug'] ) ) : ?> + data['slug'] ); ?> + - +
+
+ data['parent_id'] ) ) : ?> + data['parent_id'] ); ?> + - +
+
+ +
+ +
diff --git a/modules/dolibarr/doli-categories/view/list.view.php b/modules/dolibarr/doli-categories/view/list.view.php new file mode 100644 index 0000000..5619d2c --- /dev/null +++ b/modules/dolibarr/doli-categories/view/list.view.php @@ -0,0 +1,45 @@ + + * @copyright (c) 2011-2020 Eoxia . + * @since 2.1.0 + * @version 2.1.0 + */ + + +namespace wpshop; + +use eoxia\View_Util; + +defined( 'ABSPATH' ) || exit; + +/** + * Documentation des variables utilisées dans la vue. + * + * @var array $categories Le tableau contanant toutes les données des factures. + * @var Doli_Category $category Les données d'une catégorie. + * @var string $doli_url L'url de Dolibarr. + */ + +?> + +
+
+
+
+
+
+
+
+ $category, + 'doli_url' => $doli_url, + ) ); + endforeach; + endif; ?> +
diff --git a/modules/dolibarr/doli-categories/view/main.view.php b/modules/dolibarr/doli-categories/view/main.view.php new file mode 100644 index 0000000..a5dcc99 --- /dev/null +++ b/modules/dolibarr/doli-categories/view/main.view.php @@ -0,0 +1,33 @@ + + * @copyright (c) 2011-2020 Eoxia . + * @since 2.1.0 + * @version 2.1.0 + */ + +namespace wpshop; + +defined( 'ABSPATH' ) || exit; + +/** + * Documentation des variables utilisées dans la vue. + * @var string $dolibarr_create_category Endpoint de création de catégorie dolibarr. + * @var string $dolibarr_url L'url de fin dolibarr. + */ +?> + +
+

+ + dolibarr_is_active() ) : ?> + + + + +

+ display(); ?> +
diff --git a/modules/dolibarr/doli-sync/action/class-doli-sync-action.php b/modules/dolibarr/doli-sync/action/class-doli-sync-action.php index a4da5bc..b31e932 100644 --- a/modules/dolibarr/doli-sync/action/class-doli-sync-action.php +++ b/modules/dolibarr/doli-sync/action/class-doli-sync-action.php @@ -111,7 +111,7 @@ public function sync() { $last = ( ! empty( $_POST['last'] ) && '1' == $_POST['last'] ) ? true : false; $sync_info = Doli_Sync::g()->get_sync_infos( $type ); - + // @todo: Do Array http_build_query. $doli_entries = Request_Util::get( $sync_info['endpoint'] . '?sortfield=t.rowid&sortorder=ASC&limit=' . Doli_Sync::g()->limit_entries_by_request . '&page=' . $done_number / Doli_Sync::g()->limit_entries_by_request ); @@ -177,17 +177,17 @@ public function sync_entry() { $entry_id = ! empty( $_POST['entry_id'] ) ? (int) $_POST['entry_id'] : 0; $type = ! empty( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : ''; - $sync_status = Doli_Sync::g()->sync( $wp_id, $entry_id, $type ); + $sync_status = Doli_Sync::g()->sync( $wp_id, $entry_id, $type ); $sync_info = Doli_Sync::g()->get_sync_infos( $type ); - + ob_start(); // @todo: Add display_item for contact. - if ( $type !== 'wps-user' ) { + if ( $type !== 'wps-user' || $type !== 'wps-product-cat' ) { $sync_info['wp_class']::g()->display_item( $sync_status['wp_object'], true, $dolibarr_option['dolibarr_url'] ); } - + $item_view = ob_get_clean(); - + wp_send_json_success( array( 'id' => $wp_id, 'namespace' => 'wpshop', @@ -224,7 +224,7 @@ public function add_sync_header( $type ) { * @param boolean $sync_status Le statut de la synchronisation. */ public function add_sync_item( $object, $sync_status ) { - if ( Settings::g()->dolibarr_is_active() && in_array( $object->data['type'], array( 'wps-product', 'wps-third-party', 'wps-proposal' ) ) ) { + if ( Settings::g()->dolibarr_is_active() && in_array( $object->data['type'], array( 'wps-product', 'wps-third-party', 'wps-proposal','wps-product-cat' ) ) ) { Doli_Sync::g()->display_sync_status( $object, $object->data['type'], $sync_status ); } } @@ -240,14 +240,14 @@ public function check_sync_status() { $wp_id = ! empty( $_POST['wp_id'] ) ? (int) $_POST['wp_id'] : 0; $type = ! empty( $_POST['type'] ) ? sanitize_text_field( $_POST['type'] ) : ''; - if ( empty( $wp_id ) && ! in_array( $type, array( 'wps-product', 'wps-third-party', 'wps-proposals', 'wps-user' ) ) ) { + if ( empty( $wp_id ) && ! in_array( $type, array( 'wps-product', 'wps-third-party', 'wps-proposals', 'wps-user', 'wps-product-cat' ) ) ) { wp_send_json_error(); } - + $sync_info = Doli_Sync::g()->get_sync_infos( $type ); - + $object = $sync_info['wp_class']::g()->get( array( 'id' => $wp_id ), true ); - + ob_start(); $status = Doli_Sync::g()->display_sync_status( $object, $type ); $view = ob_get_clean(); diff --git a/modules/dolibarr/doli-sync/asset/js/doli-sync.backend.js b/modules/dolibarr/doli-sync/asset/js/doli-sync.backend.js index 6509435..0e31b03 100644 --- a/modules/dolibarr/doli-sync/asset/js/doli-sync.backend.js +++ b/modules/dolibarr/doli-sync/asset/js/doli-sync.backend.js @@ -117,7 +117,6 @@ window.eoxiaJS.wpshop.doliSync.syncEntrySuccess = function( triggeredElement, re var modal = jQuery( '.wpeo-modal.modal-active' ); // If it is associate action. - if ( modal.length > 0 ) { modal.addClass( 'modal-force-display' ); modal.find( '.modal-content' ).html( response.data.modal_view ); diff --git a/modules/dolibarr/doli-sync/class/class-doli-sync.php b/modules/dolibarr/doli-sync/class/class-doli-sync.php index 28451c7..cc69422 100644 --- a/modules/dolibarr/doli-sync/class/class-doli-sync.php +++ b/modules/dolibarr/doli-sync/class/class-doli-sync.php @@ -80,6 +80,16 @@ protected function construct() { 'doli_class' => '\wpshop\\Doli_Proposals', 'doli_type' => 'propal', ), + 'wps-product-cat' => array( + 'title' => __( 'Categories', 'wpshop' ), + 'action' => 'sync_categories', + 'nonce' => 'sync_categories', + 'endpoint' => 'categories', + 'associate_endpoint' => 'Category', + 'wp_class' => '\wpshop\\Doli_Category', + 'doli_class' => '\wpshop\\Doli_Category', + 'doli_type' => 'category', + ), ); } @@ -173,7 +183,7 @@ public function sync( $wp_id, $entry_id, $type ) { $doli_product = Request_Util::get( 'products/' . $entry_id ); $wp_product = Product::g()->get( array( 'id' => $wp_id ), true ); $wp_product = Doli_Products::g()->doli_to_wp( $doli_product, $wp_product ); - + $messages[] = sprintf( __( 'Erase data for the product %s with the dolibarr data', 'wpshop' ), $wp_product->data['title'] ); $wp_object = $wp_product; @@ -183,21 +193,20 @@ public function sync( $wp_id, $entry_id, $type ) { $wp_proposal = Proposals::g()->get( array( 'id' => $wp_id ), true ); Doli_Proposals::g()->doli_to_wp( $doli_proposal, $wp_proposal ); - + $wp_object = $wp_proposal; break; - /*case 'wps-documents': - $doli_documents = Request_Util::get( 'documents?modulepart=product&id=' . $entry_id ); - $wp_document = Doli_Documents::g()->get( array( 'id' => $wp_id ), true ); - - Doli_Proposals::g()->doli_to_wp( $doli_documents, $wp_proposal ); - - $wp_object = $wp_proposal; - break;*/ + case 'wps-product-cat': + $doli_category = Request_Util::get( 'categories/' . $entry_id ); + $wp_category = Doli_Category::g()->get( array( 'id' => $wp_id ), true ); + $wp_category = Doli_Category::g()->doli_to_wp( $doli_category, $wp_category); + + $wp_object = $wp_category; + break; default: break; } - + return array( 'messages' => $messages, 'wp_error' => $wp_error, @@ -219,27 +228,34 @@ public function sync( $wp_id, $entry_id, $type ) { public function check_status( $id, $type ) { $external_id = 0; $sha_256 = 0; + global $wpdb; if ( $type == 'wps-user' ) { $external_id = get_user_meta( $id, '_external_id', true ); $sha_256 = get_user_meta( $id, '_sync_sha_256', true ); + } elseif ( $type == 'wps-product-cat' ) { + $external_id = get_term_meta( $id, '_external_id', true ); + $sha_256 = get_term_meta( $id, '_sync_sha_256', true ); } else { $external_id = get_post_meta( $id, '_external_id', true ); $sha_256 = get_post_meta( $id, '_sync_sha_256', true ); $sha_documents = get_post_meta( $id, 'sha256_documents', true ); } - + $sync_info = $this->sync_infos[ $type ]; - + $response = Request_Util::get( $sync_info['endpoint'] . '/' . $external_id ); - + // Dolibarr return false when object is not found. if ( ! $response ) { // @todo: Doublon if ( $type == 'wps-user' ) { delete_user_meta( $id, '_external_id' ); delete_user_meta( $id, '_sync_sha_256' ); - } else { + } elseif ( $type == 'wps-product-cat' ) { + delete_term_meta( $id, '_external_id' ); + delete_term_meta( $id, '_sync_sha_256' ); + } else { delete_post_meta( $id, '_external_id' ); delete_post_meta( $id, '_sync_sha_256' ); } @@ -250,13 +266,16 @@ public function check_status( $id, $type ) { 'status_message' => 'Dolibarr Object: #' . $external_id . ' not exist. Automatically delete external_id.', ); } - + // Dolibarr Object is not linked to this WP Object. if ( $response->array_options->options__wps_id != $id ) { // @todo: Doublon if ( $type == 'wps-user' ) { delete_user_meta( $id, '_external_id' ); delete_user_meta( $id, '_sync_sha_256' ); + } elseif ( $type == 'wps-product-cat' ) { + delete_term_meta( $id, '_external_id' ); + delete_term_meta( $id, '_sync_sha_256' ); } else { delete_post_meta( $id, '_external_id' ); delete_post_meta( $id, '_sync_sha_256' ); @@ -268,7 +287,7 @@ public function check_status( $id, $type ) { 'status_message' => 'Dolibarr Object is not linked to this WP Object.', ); } - + $response = apply_filters( 'doli_build_sha_' . $type, $response, $id ); // WP Object is not equal Dolibarr Object. if ( $response->sha !== $sha_256 || $response->sha_documents != $sha_documents ) { @@ -299,7 +318,7 @@ public function check_status( $id, $type ) { * @return string Le statut de la synchronisation. */ public function display_sync_status( $object, $type, $load_erp_status = true ) - { + { $data_view = array( 'object' => $object, 'type' => $type, @@ -308,24 +327,24 @@ public function display_sync_status( $object, $type, $load_erp_status = true ) 'message_tooltip' => __( 'Looking for sync status', 'wpshop' ), 'can_sync' => false, ); - + if ( ! $load_erp_status ) { View_Util::exec('wpshop', 'doli-sync', 'sync-item', $data_view); return; } if ( empty($object->data['external_id'] ) ) { - $data_view['status_color'] = 'red'; + $data_view['status_color'] = 'none'; $data_view['message_tooltip'] = __('No associated to an ERP Entity', 'wpshop'); View_Util::exec('wpshop', 'doli-sync', 'sync-item', $data_view); return; } - + $response = Doli_Sync::g()->check_status($object->data['id'], $type); - + if ( ! $response || ! $response['status'] ) { - $data_view['status_color'] = 'red'; + $data_view['status_color'] = 'none'; } else { // @todo: Do Const for status_code. switch ($response['status_code']) { @@ -334,22 +353,22 @@ public function display_sync_status( $object, $type, $load_erp_status = true ) $data_view['can_sync'] = true; break; case '0x1': - $data_view['status_color'] = 'red'; + $data_view['status_color'] = 'none'; break; case '0x2': - $data_view['status_color'] = 'red'; + $data_view['status_color'] = 'none'; $object->data['external_id'] = ''; break; case '0x3': - $data_view['status_color'] = 'orange'; + $data_view['status_color'] = 'red'; $data_view['can_sync'] = true; break; } } - + $data_view['message_tooltip'] = isset ( $response['status_message'] ) ? $response['status_message'] : __( 'Error not defined', 'wpshop' ); View_Util::exec( 'wpshop', 'doli-sync', 'sync-item', $data_view ); - + return $response; } } diff --git a/modules/dolibarr/doli-sync/filter/class-doli-sync-filter.php b/modules/dolibarr/doli-sync/filter/class-doli-sync-filter.php index 6085ba2..b65cfeb 100644 --- a/modules/dolibarr/doli-sync/filter/class-doli-sync-filter.php +++ b/modules/dolibarr/doli-sync/filter/class-doli-sync-filter.php @@ -6,7 +6,7 @@ * @author Eoxia * @copyright (c) 2011-2020 Eoxia . * @since 2.0.0 - * @version 2.0.0 + * @version 2.1.0 */ namespace wpshop; @@ -24,14 +24,14 @@ class Doli_Sync_Filter extends Singleton_Util { * Le constructeur. * * @since 2.0.0 - * @version 2.0.0 + * @version 2.1.0 */ protected function construct() { add_filter( 'wps_countries', array( $this, 'doli_countries' ) ); add_filter( 'doli_build_sha_wps-product', array( $this, 'build_sha_product' ), 10, 2 ); add_filter( 'doli_build_sha_wps-third-party', array( $this, 'build_sha_third_party' ), 10, 2 ); - add_filter( 'doli_build_sha_wps-document', array( $this, 'build_sha_document' ), 10, 2 ); + add_filter( 'doli_build_sha_wps-product-cat', array( $this, 'build_sha_categories' ), 10, 2 ); } /** @@ -79,7 +79,7 @@ public function doli_countries( $countries ) { * La construction du SHA256 d'une synchronisation d'un produit. * * @since 2.0.0 - * @version 2.0.0 + * @version 2.1.0 * * @param Product $response Les données d'un produit. * @param integer $wp_id L'id d'un produit WordPress. @@ -149,18 +149,25 @@ public function build_sha_third_party( $response, $wp_id ) { } /** - * La construction du SHA256 d'une synchronisation d'un document. + * La construction du SHA256 d'une synchronisation d'une catégorie. * - * @since 2.01.0 + * @since 2.1.0 * @version 2.1.0 * - * @param Doli_Documents $response Les données d'un document. - * @param integer $wp_id L'id d'un tier WordPress. + * @param Doli_Category $response Les données d'une catégorie. + * @param integer $wp_id L'id d'un tier WordPress. * - * @return Doli_Documents Les données d'un document avec le SHA256. + * @return Doli_Category Les données d'un catégorie avec le SHA256. */ - public function build_sha_document( $response, $wp_id ) { + public function build_sha_categories( $response, $wp_id ) { + $data_sha = array(); + + $data_sha['doli_id'] = (int) $response->id; + $data_sha['wp_id'] = $wp_id; + $data_sha['name'] = $response->label; + $data_sha['slug'] = $response->array_options->options__wps_slug; + $response->sha = hash( 'sha256', implode( ',', $data_sha ) ); return $response; } diff --git a/modules/dolibarr/doli-sync/view/sync-item.view.php b/modules/dolibarr/doli-sync/view/sync-item.view.php index 100717b..2e80258 100644 --- a/modules/dolibarr/doli-sync/view/sync-item.view.php +++ b/modules/dolibarr/doli-sync/view/sync-item.view.php @@ -22,12 +22,11 @@ * @var string $type Le type d'entité. * @var string $status_color La couleur du statut d'une synchronisation. * @var string $message_tooltip Le message de la tooltip. - */ + */ ?> -
    -
  • WP : data['id'] ); ?>
  • +
  • WP : data['id'] ); ?>
  • Doli : data['external_id'] ) ? esc_html( $object->data['external_id'] ) : "N/A"; ?>
dolibarr_is_active() ) { add_submenu_page( 'wpshop', __( 'Add', 'wpshop' ), __( 'Add', 'wpshop' ), 'manage_options', 'post-new.php?post_type=wps-product' ); } - add_submenu_page( 'wpshop', __( 'Products Category', 'wpshop' ), __( 'Products Category', 'wpshop' ), 'manage_options', 'edit-tags.php?taxonomy=wps-product-cat&post_type=wps-product' ); - add_action( 'load-' . $hook, array( $this, 'callback_add_screen_option' ) ); } /** @@ -89,7 +87,7 @@ public function callback_add_menu_page() { $prev_url .= '&s=' . $s; $next_url .= '&s=' . $s; } - + View_Util::exec( 'wpshop', 'products', 'main', array( 'number_page' => $number_page, 'current_page' => $current_page, diff --git a/modules/products/filter/class-product-filter.php b/modules/products/filter/class-product-filter.php index 7782544..d3eabeb 100644 --- a/modules/products/filter/class-product-filter.php +++ b/modules/products/filter/class-product-filter.php @@ -124,7 +124,7 @@ public function callback_register_post_type_args( $args ) { */ public function callback_taxonomy( $args ) { $labels = array( - 'name' => _x( 'Products category', 'taxonomy general name', 'wpshop' ), + 'name' => _x( 'Product category', 'taxonomy general name', 'wpshop' ), 'singular_name' => _x( 'Product category', 'taxonomy singular name', 'wpshop' ), 'search_items' => __( 'Search Products category', 'wpshop' ), 'all_items' => __( 'All Products category', 'wpshop' ), @@ -132,7 +132,7 @@ public function callback_taxonomy( $args ) { 'parent_item_colon' => __( 'Parent Product: category', 'wpshop' ), 'edit_item' => __( 'Edit Product category', 'wpshop' ), 'update_item' => __( 'Update Product category', 'wpshop' ), - 'add_new_item' => __( 'Add New Product category', 'wpshop' ), + 'add_new_item' => __( 'Add with Dolibarr', 'wpshop' ), 'new_item_name' => __( 'New Product categoryName', 'wpshop' ), 'menu_name' => __( 'Product category', 'wpshop' ), ); @@ -145,7 +145,7 @@ public function callback_taxonomy( $args ) { 'show_in_nav_menus' => true, 'query_var' => true, 'rewrite' => array( - 'slug' => __( 'category-product', 'wpshop' ), + 'slug' => __( 'wps-product-cat', 'wpshop' ), ), ); diff --git a/wpshop.config.json b/wpshop.config.json index cb0d80e..f9b30e9 100644 --- a/wpshop.config.json +++ b/wpshop.config.json @@ -39,6 +39,7 @@ "modules/dolibarr/doli-statut/doli-statut.config.json", "modules/dolibarr/doli-proposals/doli-proposals.config.json", "modules/dolibarr/doli-order/doli-order.config.json", + "modules/dolibarr/doli-categories/doli-categories.config.json", "modules/dolibarr/doli-third-parties/doli-third-parties.config.json", "modules/dolibarr/doli-user/doli-user.config.json", "modules/dolibarr/doli-invoice/doli-invoice.config.json",