Add front controller functionality
authorSteve Sutton <steve@gaslightmedia.com>
Mon, 24 Aug 2015 20:19:18 +0000 (16:19 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Mon, 24 Aug 2015 20:19:18 +0000 (16:19 -0400)
Output Coupons

controllers/admin.php
controllers/front.php
models/coupon.php
views/front/couponList.php [new file with mode: 0644]

index b8b4391..b005903 100644 (file)
@@ -1,8 +1,8 @@
 <?php
-defined('ABSPATH') or die();;
+defined('ABSPATH') or die();
 
 /**
- * AdminController
+ * Admin Controller
  *
  * Controller for the Admin side of Gaslight Coupons
  */
index e69de29..a82f823 100644 (file)
@@ -0,0 +1,104 @@
+<?php
+defined('ABSPATH') or die();
+
+/**
+ * Front Controller
+ */
+class glm_coupon_front
+{
+    public $pluginDirName;
+    public $wpdb;
+    public $frontViewDir;
+
+    const FORM_VIEW_DIR     = 'views';
+
+    public function __construct($path, $wpdb)
+    {
+        $this->pluginDirName = $path;
+        $this->wpdb = $wpdb;
+        $coupons = new glm_coupons_coupon($path);
+        add_shortcode('glmcoupons', array($this, 'glm_coupon_shortcode'));
+        $this->viewDir = $this->pluginDirName . self::FORM_VIEW_DIR;
+        $this->frontViewDir = $this->viewDir . '/front';
+    }
+
+    public function glm_coupon_shortcode($atts)
+    {
+        extract(shortcode_atts(
+            array('category' => '-1'), $atts, 'glmcoupons')
+        );
+
+        $output = 'do short code';
+        $output = 'glmcoupons category=' . $atts['category'];
+        $output .= '<pre>' . print_r($atts, true) . '</pre>';
+        ob_start();
+        $this->list_coupons();
+        $output = ob_get_contents();
+        ob_end_clean();
+
+        return $output;
+    }
+
+    public function search_form()
+    {
+        $data = array();
+        // use smarty template to output the form
+    }
+
+    public function list_coupons()
+    {
+        global $wpdb, $wp;
+
+        $glm_couponscategory   = (isset($_REQUEST['glm_couponscategory']))
+            ? filter_var($_REQUEST['glm_couponscategory'])
+            : false;
+
+        $midnight           = strtotime(date('Y-m-d',time()).' 00:00:00');
+        $args = array(
+            'post_type' => GLM_COUPON_POST_TYPE,
+            'meta_query' => array(
+                array(
+                    'key'     => 'glm_coupons_enddate',
+                    'value'   => $midnight,
+                    'compare' => '>='
+                ),
+                array(
+                    'key'     => 'glm_coupons_startdate',
+                    'value'   => $midnight,
+                    'compare' => '<='
+                )
+            )
+        );
+        if ($glm_couponscategory) {
+            $args['tax_query'] = array(
+                'relation' => 'AND'
+            );
+            if ($glm_couponscategory) {
+                $args['tax_query'][] = array(
+                    'taxonomy' => 'glm_couponscategory',
+                    'field'    => 'id',
+                    'terms'    => $glm_couponscategory
+                );
+            }
+        }
+        $coupons = get_posts($args);
+        $totalCoupons = count($coupons);
+        $current_url = esc_url(add_query_arg($wp->query_string, '', home_url($wp->request)));
+        $iterator = 1;
+        foreach ($coupons as $coupon) {
+            $custom = get_post_custom($coupon->ID);
+            $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];
+            if ($iterator == $totalCoupons) {
+                $coupon->end = true;
+            }
+            ++$iterator;
+        }
+        include $this->frontViewDir . '/couponList.php';
+        return;
+    }
+
+}
index b7b7551..db720f0 100644 (file)
@@ -300,7 +300,7 @@ class glm_coupons_coupon
             'menu_position'   => 21,
             'supports'        => array('title', 'editor'),
             'has_archive'     => true,
-            'menu_icon'       => 'dashicons-businessman',
+            'menu_icon'       => 'dashicons-products',
             'capability_type' => 'post',
             'hierarchical'    => false,
             'rewrite'         => array('slug' => 'coupons'),
diff --git a/views/front/couponList.php b/views/front/couponList.php
new file mode 100644 (file)
index 0000000..68f74d7
--- /dev/null
@@ -0,0 +1,12 @@
+<div>
+    <h1>List Coupons</h1>
+    <div class="row">
+        <?php if ($coupons): foreach($coupons as $coupon) :?>
+        <div class="small-6 medium-4 columns<?php if ($coupon->end){ echo ' end';}?>">
+            <h2><?php echo $coupon->post_title;?></h2>
+            <div><?php echo $coupon->post_content;?></div>
+            <div><b>Expires:</b> <?php echo date('F d,Y', $coupon->glm_coupons_expdate);?></div>
+        </div>
+        <?php endforeach; endif;?>
+    </div>
+</div>