+<?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;
+ }
+
+}