Update for coupons
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 24 Sep 2015 16:30:22 +0000 (12:30 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 24 Sep 2015 16:30:22 +0000 (12:30 -0400)
Tying coupons into the Gaslight Media Member DB Plugin.
if they have the member plugin it will output a list of the members for
the coupon edit page to choose from.

controllers/front.php
css/front/coupons.css
models/coupon.php
views/admin/coupon_meta.php
views/front/couponList.php
views/front/couponPrint.php

index 8b04abb..87b6c21 100644 (file)
@@ -102,11 +102,16 @@ class glm_coupon_front
         $coupons      = get_posts($args);
         foreach ($coupons as $coupon) {
             $custom = get_post_custom($coupon->ID);
+            if ($custom['glm_coupons_member'][0]) {
+                 $member_data = $this->_getMemberData($custom['glm_coupons_member'][0]);
+            }
             $coupon->end                   = false;
             $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       = $custom['glm_coupons_url'][0];
+            $coupon->glm_coupons_url       = preg_replace('/http:\/\//i', '', $coupon->glm_coupons_url);
+            $coupon->glm_coupons_member    = ($member_data['member_name']) ? $member_data['member_name'] : '';
+            $coupon->glm_coupons_address   = ($member_data['address']) ? $member_data['address'] : '';
             $coupon->glm_coupons_thumb     = get_the_post_thumbnail($coupon->ID, 'thumbnail', array('class' => 'aligncenter'));
             $post_categories               = wp_get_object_terms(
                 $coupon->ID,
@@ -132,4 +137,42 @@ class glm_coupon_front
         return;
     }
 
+    private function _getMemberData($id)
+    {
+        global $wpdb;
+        $data = array();
+        if (!filter_var($id, FILTER_VALIDATE_INT)) {
+            return false;
+        }
+        if ( !defined( 'GLM_MEMBERS_PLUGIN_DB_PREFIX' ) ) {
+            return false;
+        } else {
+            $sql = "
+              SELECT m.member_name, addr1,c.name as city,state,zip
+                FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info m
+                     LEFT OUTER JOIN " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "cities c ON (c.id = m.city)
+               WHERE m.id = {$id}";
+            $results = $wpdb->get_results($sql, ARRAY_A);
+            if ($results[0]) {
+                $address = array();
+                if ($results[0]['addr1']) {
+                     $address[] = $results[0]['addr1'];
+                }
+                if ($results[0]['city']) {
+                     $address[] = $results[0]['city'];
+                }
+                if ($results[0]['state']) {
+                     $address[] = $results[0]['state'];
+                }
+                if ($results[0]['zip']) {
+                     $address[] = $results[0]['zip'];
+                }
+                $data = array(
+                     'member_name' => $results[0]['member_name'],
+                     'address' => implode(',', $address)
+                );
+            }
+            return $data;
+        }
+    }
 }
index 66746f6..cafc15a 100644 (file)
@@ -12,6 +12,9 @@
 .coupon-item-wrap {
     padding: 20px;
 }
+.coupon-item-wrap h3 {
+    text-align: center;
+}
 .coupon-row h2 {
     font-size: 1.45rem;
 }
index e432b50..849b0f7 100644 (file)
@@ -19,7 +19,6 @@ class glm_coupons_coupon
      */
     public function __construct($path)
     {
-//        global $typenow;
         $this->pluginDirName = $path;
         add_action('init', array($this, 'addPostTypes'));
         add_action('init', array($this, 'createCouponTaxonomy'));
@@ -38,15 +37,12 @@ class glm_coupons_coupon
         add_action('restrict_manage_posts',
                    array($this, 'taxonomy_filter_restrict_manage_posts'));
         add_filter('parse_query', array($this, 'couponFilter'));
-//        var_dump($typenow);
-//        add_filter('user_row_actions', array($this, 'remove_row_actions'));
     }
 
     // Filter the request to just give posts for the given taxonomy, if applicable.
     function taxonomy_filter_restrict_manage_posts()
     {
         global $typenow;
-//        var_dump($typenow);
         if ($typenow == GLM_COUPON_POST_TYPE) {
             $filters = get_object_taxonomies(GLM_COUPON_POST_TYPE);
 
@@ -63,10 +59,7 @@ class glm_coupons_coupon
                     'hide_empty'      => true
                 ));
             }
-            }
-
-
-
+        }
     }
 
     function taxonomy_filter_post_type_request($query)
@@ -74,11 +67,8 @@ class glm_coupons_coupon
         global $pagenow, $typenow;
         if ('edit.php' == $pagenow && $typenow == GLM_COUPON_POST_TYPE) {
             $filters = get_object_taxonomies(GLM_COUPON_POST_TYPE);
-//            var_dump($filters);
             foreach ($filters as $tax_slug) {
                 $var = &$query->query_vars[$tax_slug];
-//                var_dump($var);
-//                var_dump($tax_slug);
                 if (isset($var)) {
                     $term = get_term_by('id', $var, $tax_slug);
                     var_dump($term);
@@ -134,11 +124,9 @@ class glm_coupons_coupon
                 if (isset($qv[GLM_COUPON_TAX_CATEGORIES]) && $qv[GLM_COUPON_TAX_CATEGORIES] != 0) {
                     $term = get_term_by('id', $qv[GLM_COUPON_TAX_CATEGORIES], GLM_COUPON_TAX_CATEGORIES);
                     $qv[GLM_COUPON_TAX_CATEGORIES] = $term->slug;
-
                     if (isset($query->tax_query)) {
                         $tax_query =& $query->tax_query;
                         $queries =& $tax_query->queries;
-//                        echo '<pre>'.print_r($queries, true).'</pre>';
                         foreach ($queries as $key => &$qs) {
                             if ($qs['taxonomy'] == GLM_COUPON_TAX_CATEGORIES) {
                                 $qs['terms'][$key] = $term->slug;
@@ -147,13 +135,8 @@ class glm_coupons_coupon
                     }
 
                 }
-                if ($qv['post_type'] == GLM_COUPON_POST_TYPE) {
-//                    echo '<pre>'.print_r($qv, true).'</pre>';
-                }
-//                echo '<pre>'.print_r($query, true).'</pre>';
             }
 
-
             return $query;
         }
     }
@@ -218,13 +201,41 @@ class glm_coupons_coupon
         $clean_ed = date('D, M d, Y', $meta_ed);
         $clean_ex = date('D, M d, Y', $meta_ex);
 
-        $glm_coupon_url = (isset($glm_coupon_url))
-            ? $glm_coupon_url
+        $glm_coupons_url = (isset($custom['glm_coupons_url']))
+            ? $custom['glm_coupons_url'][0]
+            : '';
+
+        // if they have member db plugin get a list of the members
+        $glm_coupons_member = (isset($custom['glm_coupons_member']))
+            ? $custom['glm_coupons_member'][0]
             : '';
 
+
+        $memberList = $this->_getMemberList();
+
         include $this->pluginDirName . 'views/admin/coupon_meta.php';
     }
 
+    private function _getMemberList()
+    {
+        global $wpdb;
+        $members = array();
+        if ( !defined( 'GLM_MEMBERS_PLUGIN_DB_PREFIX' ) ) {
+            return false;
+        } else {
+            $sql = "
+              SELECT id,member_name as name
+                FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info
+               WHERE status = 10
+            ORDER BY member_name";
+            $results = $wpdb->get_results($sql, ARRAY_A);
+            foreach ($results as $row) {
+                 $members[] = $row;
+            }
+        }
+        return $members;
+    }
+
     /**
      * Save the coupon meta data
      *
@@ -268,6 +279,8 @@ class glm_coupons_coupon
         update_post_meta($post->ID, 'glm_coupons_expdate', $updateexpd);
 
         update_post_meta($post->ID, 'glm_coupons_url', $_POST['glm_coupons_url']);
+
+        update_post_meta($post->ID, 'glm_coupons_member', $_POST['glm_coupons_member']);
     }
 
     /**
@@ -294,13 +307,12 @@ class glm_coupons_coupon
             'menu_name'          => 'Coupons'
         );
         $args   = array(
-//            'label'           => __('Coupons'),
             'labels'          => $labels,
             'description'     => 'Stores Coupons and Coupon data',
             'public'          => true,
             'can_export'      => true,
             'menu_position'   => 21,
-            'supports'        => array('title', 'editor', 'excerpt', 'thumbnail', 'author'),
+            'supports'        => array('title', 'editor', 'thumbnail', 'author'),
             'has_archive'     => true,
             'menu_icon'       => 'dashicons-products',
             'capability_type' => 'post',
index b8f51f0..998d586 100644 (file)
        value="<?php echo wp_create_nonce( 'glm-coupons-nonce' );?>" />
 <div class="tf-meta">
     <ul>
+        <?php if( $memberList ) : ?>
+        <li>
+            <label>Member: </label>
+            <select name="glm_coupons_member">
+                <?php foreach ($memberList as $memberOption)  :?>
+                <option value="<?php echo $memberOption['id']; ?>" <?php if ($glm_coupons_member == $memberOption['id']) { echo  ' selected'; } ?>><?php echo $memberOption['name']; ?></option>
+                <?php endforeach; ?>
+            </select>
+        </li>
+        <?php endif; ?>
         <li>
             <label>Start Date: </label>
             <input name="glm_coupons_startdate" class="tfdate"
@@ -29,7 +39,7 @@
         <li>
             <label>Url:</label>
             <input name="glm_coupons_url"
-                   value="<?php echo $glm_coupons_url[0]; ?>" />
+                   value="<?php echo $glm_coupons_url; ?>" />
         </li>
     </ul>
 </div>
index 63fbdac..d599397 100644 (file)
             <div class="<?php if ($coupon->end){ echo ' end';}?> coupon-item">
                 <div class="coupon-select"><label><input type="checkbox" name="coupon-id-<?php echo $coupon->ID;?>"> Select this coupon</label></div>
                 <div class="coupon-item-wrap">
+                    <?php if ($coupon->glm_coupons_member) : ?>
+                    <h3><?php echo $coupon->glm_coupons_member;?></h3>
+                    <?php endif;?>
                     <h3><?php echo $coupon->post_title;?></h3>
                     <?php if ($coupon->glm_coupons_thumb) : ?>
                     <?php echo $coupon->glm_coupons_thumb;?>
                     <?php endif;?>
                     <div class="coupon-descr"><?php echo $coupon->post_content;?></div>
                     <?php if ($coupon->glm_coupons_url) :?>
-                    <div class="coupon-url"><a href="<?php echo date('F d,Y', $coupon->glm_coupons_url);?>">Web Site</a></div>
+                    <div class="coupon-url"><a target="_blank" href="http://<?php echo $coupon->glm_coupons_url;?>">Web Site</a></div>
                     <?php endif;?>
                     <div class="coupon-expires">Expires: <?php echo date('m/d/Y', $coupon->glm_coupons_expdate);?></div>
                 </div>
index 08cbc84..65573bf 100644 (file)
@@ -93,8 +93,8 @@ foreach ($coupons as $coupon) {
     $pdf->ezSetY($y);
     $y = $pdf->ezText("\n".$header, 12, $header_options);
     $y = $pdf->ezText($descr, 10, $options);
-    if ($memberAddress) {
-        $y = $pdf->ezText($memberAddress, 9, $header_options);
+    if ($coupon->glm_coupons_address) {
+        $y = $pdf->ezText($coupon->glm_coupons_address, 9, $header_options);
     }
     if ($phone) {
         $y = $pdf->ezText($phone, 9, $header_options);