From 73b4c6f1f9ba6f5039cf15acc63394e400290594 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 5 Jan 2017 16:09:46 -0500 Subject: [PATCH] Update the Management for import of older plugin Now has an option to grab the coupon data and images from the older coupon plugin. --- classes/data/dataManagement.php | 2 +- models/admin/management/coupons.php | 307 ++++++++++++++++++++-------- views/admin/management/coupons.html | 12 +- 3 files changed, 231 insertions(+), 90 deletions(-) diff --git a/classes/data/dataManagement.php b/classes/data/dataManagement.php index 2d9e1a2..1c0c637 100644 --- a/classes/data/dataManagement.php +++ b/classes/data/dataManagement.php @@ -144,7 +144,7 @@ class GlmDataCouponsManagement extends GlmDataAbstract 'use' => 'a' ), // Default Date Range - 'default_date_rang' => array( + 'default_date_range' => array( 'field' => 'default_date_range', 'type' => 'text', 'required' => true, diff --git a/models/admin/management/coupons.php b/models/admin/management/coupons.php index 7e12704..d4e9de6 100644 --- a/models/admin/management/coupons.php +++ b/models/admin/management/coupons.php @@ -133,115 +133,120 @@ class GlmMembersAdmin_management_coupons extends GlmDataCouponsManagement } switch ($option) { - case 'couponimport': - $import = false; - if (isset($_REQUEST['import'])) { - $import = filter_var( $_REQUEST['import'], FILTER_VALIDATE_BOOLEAN ); - } - if ( $import ) { - $import_result = '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; - $db_host = filter_var( $_REQUEST['db_host'], FILTER_SANITIZE_STRING ); - $db_name = filter_var( $_REQUEST['db_name'], FILTER_SANITIZE_STRING ); - $db_user = filter_var( $_REQUEST['db_user'], FILTER_SANITIZE_STRING ); - $db_password = filter_var( $_REQUEST['db_password'], FILTER_SANITIZE_STRING ); - $this->connectPostgresDb( $db_host, $db_name, $db_user, $db_password ); - - $this->settings = filter_var_array( - $_REQUEST, - array( - 'schema' => FILTER_SANITIZE_STRING, - 'cattablename' => FILTER_SANITIZE_STRING, - 'tablename' => FILTER_SANITIZE_STRING, - 'sdate' => array( - 'filter' => FILTER_VALIDATE_REGEXP, - 'options' => array( - 'regexp' => '%[0-9]{2}/[0-9]{2}/[0-9]{4}%' - ) + case 'couponPluginImport': + $import_result .= '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + $this->importCoupons(); + break; + + case 'couponimport': + $import = false; + if (isset($_REQUEST['import'])) { + $import = filter_var( $_REQUEST['import'], FILTER_VALIDATE_BOOLEAN ); + } + if ( $import ) { + $import_result = '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + $db_host = filter_var( $_REQUEST['db_host'], FILTER_SANITIZE_STRING ); + $db_name = filter_var( $_REQUEST['db_name'], FILTER_SANITIZE_STRING ); + $db_user = filter_var( $_REQUEST['db_user'], FILTER_SANITIZE_STRING ); + $db_password = filter_var( $_REQUEST['db_password'], FILTER_SANITIZE_STRING ); + $this->connectPostgresDb( $db_host, $db_name, $db_user, $db_password ); + + $this->settings = filter_var_array( + $_REQUEST, + array( + 'schema' => FILTER_SANITIZE_STRING, + 'cattablename' => FILTER_SANITIZE_STRING, + 'tablename' => FILTER_SANITIZE_STRING, + 'sdate' => array( + 'filter' => FILTER_VALIDATE_REGEXP, + 'options' => array( + 'regexp' => '%[0-9]{2}/[0-9]{2}/[0-9]{4}%' ) - ) - ); - // Add the categories - $this->addCategories(); - $import_result .= '
$this->settings: ' . print_r($this->settings, true) . '
'; - $import_result .= $this->addCoupons(); - } - break; + ) + ); + // Add the categories + $this->addCategories(); + $import_result .= '
$this->settings: ' . print_r($this->settings, true) . '
'; + $import_result .= $this->addCoupons(); + } - case 'couponImagesImport': - $this->image_owner = filter_var( $_REQUEST['img_owner_id'], FILTER_SANITIZE_STRING ); - if ( $this->image_owner ) { - $this->importCouponImages(); - $import_result = '

All images imported

'; - } else { - $import_result = '

Failure: Not given owner id.

'; - } - break; + break; - case 'settings': + case 'couponImagesImport': + $this->image_owner = filter_var( $_REQUEST['img_owner_id'], FILTER_SANITIZE_STRING ); + if ( $this->image_owner ) { + $this->importCouponImages(); + $import_result = '

All images imported

'; + } else { + $import_result = '

Failure: Not given owner id.

'; + } + break; - default: + case 'settings': - // Make sure option is set if default - $option = 'settings'; - - // Determine if current user can edit configurations - if (!current_user_can('glm_members_management')) { - return array( - 'status' => false, - 'menuItemRedirect' => 'error', - 'modelRedirect' => 'index', - 'view' => 'admin/error/index.html', - 'data' => array( - 'reason' => 'User does not have rights to make configuration changes.' - ) - ); - } + default: - // Check for submission option - $option2 = ''; - if (isset($_REQUEST['option2'])) { - $option2 = $_REQUEST['option2']; - } - switch($option2) { + // Make sure option is set if default + $option = 'settings'; + + // Determine if current user can edit configurations + if (!current_user_can('glm_members_management')) { + return array( + 'status' => false, + 'menuItemRedirect' => 'error', + 'modelRedirect' => 'index', + 'view' => 'admin/error/index.html', + 'data' => array( + 'reason' => 'User does not have rights to make configuration changes.' + ) + ); + } + // Check for submission option + $option2 = ''; + if (isset($_REQUEST['option2'])) { + $option2 = $_REQUEST['option2']; + } + switch($option2) { - // Update the settings and redisplay the form - case 'submit': - // Update the coupon management settings - $coupon_settings = $this->updateEntry(1); - if ($coupon_settings['status']) { - $settings_updated = true; - } else { - $settings_update_error = true; - } + // Update the settings and redisplay the form + case 'submit': - break; + // Update the coupon management settings + $coupon_settings = $this->updateEntry(1); + if ($coupon_settings['status']) { + $settings_updated = true; + } else { + $settings_update_error = true; + } - // Default is to get the current settings and display the form - default: + break; - // Try to get the first (should be only) entry for general settings. - $coupon_settings = $this->editEntry(1); - //echo '
$coupon_settings: ' . print_r( $coupon_settings, true ) . '
'; + // Default is to get the current settings and display the form + default: - if ($coupon_settings === false) { + // Try to get the first (should be only) entry for general settings. + $coupon_settings = $this->editEntry(1); + //echo '
$coupon_settings: ' . print_r( $coupon_settings, true ) . '
'; - if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { - glmMembersAdmin::addNotice("  /models/admin/management/coupons.php: Unable to load coupons management settings.", 'Alert'); - } + if ($coupon_settings === false) { + if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) { + glmMembersAdmin::addNotice("  /models/admin/management/coupons.php: Unable to load coupons management settings.", 'Alert'); } - break; - } break; + } + + break; + } // Populate the state list for coupon default state selection @@ -318,6 +323,134 @@ class GlmMembersAdmin_management_coupons extends GlmDataCouponsManagement } } + /** + * importCoupons + * + * This function is used to pull the coupons from the glm-coupon plugin + * that may be installed on a site. + * + * @access public + * @return void + */ + public function importCoupons() + { + $this->clearData(); + $coupon_abstract = new GlmDataCoupons( $this->wpdb, $this->config ); + require_once GLM_MEMBERS_PLUGIN_PATH.'/models/admin/ajax/imageUpload.php'; + $ImageUpload = new GlmMembersAdmin_ajax_imageUpload($this->wpdb, $this->config); + $refType = $this->config['ref_type_numb']['Member']; + // get the coupons from the glm-coupon plugin + // post type GLM_COUPON_POST_TYPE + // GLM_COUPON_POST_TYPE must be defined + if ( defined('GLM_COUPON_POST_TYPE') && GLM_COUPON_POST_TYPE ) { + // First get all of the categories + $oldCategories = get_terms( + array( + 'taxonomy' => GLM_COUPON_TAX_CATEGORIES, + 'hide_empty' => false + ) + ); + foreach ( $oldCategories as $catObject ) { + $catID = $this->getCategoryId( $catObject->name ); + if ( !$catID ) { + $this->wpdb->insert( + GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX . 'categories', + array( 'name' => $catObject->name ), + array( '%s' ) + ); + } + } + // Now get all Coupons + $coupons = get_posts( + array( + 'posts_per_page' => -1, + 'post_type' => GLM_COUPON_POST_TYPE + ) + ); + //echo '
$coupons: ' . print_r( $coupons, true ) . '
'; + foreach ( $coupons as $coupon ) { + $custom = get_post_custom($coupon->ID); + $coupon->glm_coupons_startdate = $custom['glm_coupons_startdate'][0]; + $coupon->glm_coupons_enddate = $custom['glm_coupons_enddate'][0]; + $coupon->glm_coupons_expdate = $custom['glm_coupons_expdate'][0]; + $coupon->glm_coupons_url = preg_replace('/http:\/\//i', '', $coupon->glm_coupons_url); + $coupon->glm_coupons_member = $custom['glm_coupons_member'][0]; + $coupon->glm_coupons_image = get_the_post_thumbnail_url($coupon->ID, 'full'); + if ( $coupon->glm_coupons_image ) { + $res = $ImageUpload->storeImage( $coupon->glm_coupons_image ); + if ( isset( $res['newFileName'] ) && $res['newFileName'] ) { + $coupon->glm_coupons_image = $res['newFileName']; + } else { + $coupon->glm_coupons_image = ''; + } + } + $post_categories = wp_get_object_terms( + $coupon->ID, + GLM_COUPON_TAX_CATEGORIES, + array('fields' => 'names') + ); + $coupon->post_categories = $post_categories; + echo '
start: ' . print_r( date( 'Y-m-d H:i:s', $custom['glm_coupons_startdate'][0] + 86400 ), true ) . '
'; + $coupon_data = array( + 'status' => ( ( $coupon->post_status == 'publish' ) + ? $this->config['status_numb']['Active'] + : $this->config['status_numb']['Inactive'] ), + 'created' => $coupon->post_date, + 'updated' => $coupon->post_modified, + 'start_date' => date( 'Y-m-d H:i:s', $custom['glm_coupons_startdate'][0] + 86400 ), + 'end_date' => date( 'Y-m-d H:i:s', $custom['glm_coupons_enddate'][0] + 86400 ), + 'expire' => date( 'Y-m-d H:i:s', $custom['glm_coupons_expdate'][0] + 86400 ), + 'name' => $coupon->post_title, + 'ref_type' => $refType, + 'ref_dest' => $coupon->glm_coupons_member, + 'descr' => strip_tags( trim( $coupon->post_content ) ), + 'image' => $coupon->glm_coupons_image, + 'url' => $coupon->glm_coupons_url, + ); + $coupon_data_format = array( + '%d', + '%s', + '%s', + '%s', + '%s', + '%s', + '%s', + '%d', + '%d', + '%s', + '%s', + '%s', + ); + $this->wpdb->insert( + GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX . 'coupons', + $coupon_data, + $coupon_data_format + ); + $coupon_id = $this->wpdb->insert_id; + echo '
$couponId: from insert ' . print_r( $coupon_id, true ) . '
'; + if ( !$coupon_id ) { + die( 'something is wrong no couponId' ); + } + $coupon_abstract->updateSlug( $coupon_id ); + if ( !empty( $coupon->post_categories ) ) { + foreach ( $coupon->post_categories as $key => $category ) { + $this->wpdb->insert( + GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX . 'coupon_categories', + array( + 'coupon' => $coupon_id, + 'category' => $this->getCategoryId( $category ) + ), + array( + '%d', + '%d' + ) + ); + } + } + } + echo '
$coupons: ' . print_r( $coupons, true ) . '
'; + } + } /** * Add coupons from the old site * @@ -468,7 +601,7 @@ class GlmMembersAdmin_management_coupons extends GlmDataCouponsManagement if ( !isset($found) ) { $this->wpdb->insert( GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX . 'categories', - array( 'name' => trim($category[$category_label]) ), + array( 'name' => trim($category[$category_label]) ), array( '%s' ) ); $catid = $this->wpdb->insert_id; @@ -545,7 +678,7 @@ class GlmMembersAdmin_management_coupons extends GlmDataCouponsManagement foreach ( $results as $coupon ) { $imageFullUrl = $imgUrl . $coupon['image']; $res = $ImageUpload->storeImage($imageFullUrl); - if ( isset( $res['newFileName']) && $res['newFileName'] ) { + if ( isset( $res['newFileName'] ) && $res['newFileName'] ) { $this->wpdb->update( GLM_MEMBERS_COUPONS_PLUGIN_DB_PREFIX . 'coupons', array( diff --git a/views/admin/management/coupons.html b/views/admin/management/coupons.html index 7ab60f9..80789a3 100644 --- a/views/admin/management/coupons.html +++ b/views/admin/management/coupons.html @@ -30,7 +30,7 @@ - Default date range for start and end dates: + Ribbon Text on Member list: {if $couponsSettings.fieldFail.ribbon_text}

{$couponsSettings.fieldFail.ribbon_text}

{/if} @@ -66,7 +66,7 @@ - +
{if $importResult} @@ -133,6 +133,14 @@ + + + + + + {/if}
Return to Import Coupons

Import from Gaslight Coupons (Plugin - glm-coupons)

+ Import Coupons +
-- 2.17.1