Update the Management for import of older plugin
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 5 Jan 2017 21:09:46 +0000 (16:09 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 5 Jan 2017 21:09:46 +0000 (16:09 -0500)
Now has an option to grab the coupon data and images from the older
coupon plugin.

classes/data/dataManagement.php
models/admin/management/coupons.php
views/admin/management/coupons.html

index 2d9e1a2..1c0c637 100644 (file)
@@ -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,
index 7e12704..d4e9de6 100644 (file)
@@ -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 = '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
-                    $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 .= '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+            $this->importCoupons();
+            break;
+
+        case 'couponimport':
+            $import = false;
+            if (isset($_REQUEST['import'])) {
+                $import = filter_var( $_REQUEST['import'], FILTER_VALIDATE_BOOLEAN );
+            }
+            if ( $import ) {
+                $import_result = '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+                $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 .= '<pre>$this->settings: ' . print_r($this->settings, true) . '</pre>';
-                    $import_result .= $this->addCoupons();
-                }
 
-                break;
+                    )
+                );
+                // Add the categories
+                $this->addCategories();
+                $import_result .= '<pre>$this->settings: ' . print_r($this->settings, true) . '</pre>';
+                $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 = '<p>All images imported</p>';
-                } else {
-                    $import_result = '<p>Failure: Not given owner id.</p>';
-                }
-                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 = '<p>All images imported</p>';
+            } else {
+                $import_result = '<p>Failure: Not given owner id.</p>';
+            }
+            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 '<pre>$coupon_settings: ' . print_r( $coupon_settings, true ) . '</pre>';
+            // 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 '<pre>$coupon_settings: ' . print_r( $coupon_settings, true ) . '</pre>';
 
-                        if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) {
-                            glmMembersAdmin::addNotice("<b>&nbsp;&nbsp;/models/admin/management/coupons.php: Unable to load coupons management settings.", 'Alert');
-                        }
+                if ($coupon_settings === false) {
 
+                    if (GLM_MEMBERS_PLUGIN_ADMIN_DEBUG) {
+                        glmMembersAdmin::addNotice("<b>&nbsp;&nbsp;/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 '<pre>$coupons: ' . print_r( $coupons, true ) . '</pre>';
+            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 '<pre>start: ' . print_r( date( 'Y-m-d H:i:s', $custom['glm_coupons_startdate'][0] + 86400 ), true ) . '</pre>';
+                $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 '<pre>$couponId: from insert ' . print_r( $coupon_id, true ) . '</pre>';
+                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 '<pre>$coupons: ' . print_r( $coupons, true ) . '</pre>';
+        }
+    }
     /**
      * 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(
index 7ab60f9..80789a3 100644 (file)
@@ -30,7 +30,7 @@
                             </td>
                         </tr>
                         <tr>
-                            <th {if $couponsSettings.fieldRequired.ribbon_text}class="glm-required"{/if}>Default date range for start and end dates:</th>
+                            <th {if $couponsSettings.fieldRequired.ribbon_text}class="glm-required"{/if}>Ribbon Text on Member list:</th>
                             <td {if $couponsSettings.fieldFail.ribbon_text}class="glm-form-bad-input glm-form-bad-input-misc"{/if}>
                                 <input type="text" name="ribbon_text" value="{$couponsSettings.fieldData.ribbon_text}" class="glm-form-text-input-medium">
                                 {if $couponsSettings.fieldFail.ribbon_text}<p>{$couponsSettings.fieldFail.ribbon_text}</p>{/if}
@@ -66,7 +66,7 @@
     </table>
     <!-- Import -->
 
-    <table id="glm-table-couponimport" class="glm-admin-table glm-settings-table{if $option!='couponimport'} glm-hidden{/if}">
+    <table id="glm-table-couponimport" class="glm-admin-table glm-settings-table{if $option!='couponimport' && $option!='couponPluginImport'} glm-hidden{/if}">
   {if $importResult}
         <tr><td><a href="{$thisUrl}?page={$thisPage}&glm_action=coupons&option=couponimport">Return to Import Coupons</a></td></tr>
         <tr>
                 </form>
             </td>
         </tr>
+        <tr>
+            <td colspan="2"><h1>Import from Gaslight Coupons (Plugin - glm-coupons) </h1></td>
+        </tr>
+        <tr>
+            <td colspan="2">
+                <a class="button" href="{$thisUrl}?page={$thisPage}&glm_action=coupons&option=couponPluginImport">Import Coupons</a>
+            </td>
+        </tr>
   {/if}
 
     </table>