Creating ajax scripts for setupQueue and runQueue.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 19 Jan 2018 20:56:08 +0000 (15:56 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 19 Jan 2018 20:56:08 +0000 (15:56 -0500)
Setting up queue creation and processing for the event attendee
notifications.

classes/regCartSupport.php
classes/regNotifications.php
models/admin/ajax/runEventRegQueue.php [new file with mode: 0644]
models/admin/ajax/setupEventRegQueue.php
setup/validActions.php

index 3b663be..d04883d 100644 (file)
@@ -1256,6 +1256,18 @@ class GlmRegCartSupport
 
     }
 
+    /**
+     * sendHtmlEmail
+     *
+     * Create html email and send using wp_mail.
+     *
+     * @param mixed $to          To email address
+     * @param mixed $subject     Subject line for the email
+     * @param mixed $htmlMessage Html message for the email
+     *
+     * @access public
+     * @return void
+     */
     public function sendHtmlEmail( $to, $subject, $htmlMessage )
     {
          // change the default wordpress from name when sending mail
@@ -1498,10 +1510,6 @@ class GlmRegCartSupport
 
     }
 
-
-
-
-
     /*
      * Get a quick summary of a registration request (cart)
      *
index 1326770..9e87745 100644 (file)
@@ -13,6 +13,9 @@
  * @link     http://dev.gaslightmedia.com/
  */
 
+// Load required class for support
+require_once GLM_MEMBERS_REGISTRATIONS_PLUGIN_CLASS_PATH . '/regCartSupport.php';
+
 class GlmRegNotifications
 {
     /**
@@ -57,108 +60,50 @@ class GlmRegNotifications
     }
 
     /**
-     * sendEmailNotification
+     * getAttendeesForEventWithEmails
      *
-     * Send out the email notification based on reg_notification and the regEventID.
+     * Grab the attendees for the Event.
+     * Must have the email.
      *
-     * @param mixed $reg_notification Id for reg_notifgication.
-     * @param mixed $reg_event_id     Id for the reg Event.
+     * @param mixed $reg_event Id for the reg_event
      *
      * @access public
      * @return void
      */
-    public function sendEmailNotification( $reg_notification, $reg_event_id )
+    public function getAttendeesForEventWithEmails( $reg_event )
     {
-        $to_email = $account['email'];
-        // echo '<pre>$to_email: ' . print_r( $to_email, true ) . '</pre>';
-        if ( !$to_email ) {
-            // If there's no email then return false.
-            return false;
-        }
-
-        // get the Notification type
-        var_dump( $reg_notification );
-        $notification = $this->getRegNotificationById( $reg_notification );
-        echo '<pre>$notification: ' . print_r( $notification, true ) . '</pre>';
-        if ( !$notification ) {
-            // If there's no notification type then return false.
-            return false;
-        }
-        $subject = $notification['subject'];
-        if ( !$subject ) {
-            // If there's no subject then return false.
-            return false;
-        }
-        $from_header = $notification['from_header'];
-        $replyto     = $notification['replyto'];
-
-        // Setup the Smarty Engine
-        $smarty   = new smartyTemplateSupport();
-        $viewPath = GLM_MEMBERS_BILLING_PLUGIN_PATH . '/views';
-        $smarty->template->setTemplateDir( $viewPath );
-
-        // Add standard parameters
-        require GLM_MEMBERS_PLUGIN_SETUP_PATH.'/standardTemplateParams.php';
-        $viewFile = 'admin/notifications/notification.html';
-
-        $account_data = array(
-            'account' => array(
-                'name'  => $account['ref_name'],
-                'email' => $account['email'],
-                'addr1' => $account['billing_addr1'],
-                'addr2' => $account['billing_addr2'],
-                'city'  => $account['billing_city'],
-                'state' => $account['billing_state'],
-                'zip'   => $account['billing_zip'],
-                'phone' => $account['billing_phone'],
-            )
+        $query = $this->wpdb->prepare(
+            "SELECT RRR.*,A.email,RE.event_name,RRC.class_name
+               FROM " . GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX .  "reg_request_registrant RRR
+                    LEFT OUTER JOIN " .  GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . "account A ON (A.id = RRR.account)
+                    LEFT OUTER JOIN " .  GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . "reg_event RE ON (RE.id = RRR.reg_event)
+                    LEFT OUTER JOIN " .  GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX .  "reg_request_class RRC ON (RRC.id = RRR.reg_request_class)
+              WHERE RRR.reg_event = %d
+                AND RRR.not_attending <> 1
+                AND A.email != ''
+                AND A.email IS NOT NULL
+             GROUP BY RRR.account",
+            $reg_event
         );
-        echo '<pre>$notification[message]: ' . print_r( $notification['message'], true ) . '</pre>';
-        $notification_message = $this->parseSmartyTemplateString(
-            $account_data,
-            wpautop( $notification['message'] )
-        );
-        echo '<pre>$account_data: ' . print_r( $account_data, true ) . '</pre>';
-        echo '<pre>$notification_message: ' . print_r( $notification_message, true ) . '</pre>';
-        // exit;
-
-        $smarty->templateAssign( 'title', $notification['subject'] );
-        $smarty->templateAssign( 'html_content', $notification_message );
-
-        // Generate output from model data and view
-        $htmlMessage = $smarty->template->fetch( $viewFile );
-
-
-        // change the default wordpress from name when sending mail
-        add_filter(
-            'wp_mail_from_name',
-            function ( $name ) {
-                $siteName = get_bloginfo( 'name' );
-                return $siteName;
-            }
-        );
-        // Send confirmation email, set the content type to allow html by using this filter
-        add_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) );
-
-        $message  = $htmlMessage;
-        $header[] = 'From:' . $from_header;
-        $header[] = 'Reply-To:' . $replyto;
-
-        wp_mail( $to_email, $subject, $message, $header );
-
-        // remove the filter to avoid conflicts
-        remove_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) );
+        return $this->wpdb->get_results( $query, ARRAY_A );
+    }
 
-        // Send notification to recordNotification
-        $notice_data = array(
-            'reg_notification' => $reg_notification,
-            'account'          => $account_id,
-            'from_replyto'     => $from_header,
-            'subject'          => $subject,
-            'message'          => $message,
-            'email_sent'       => $to_email,
-        );
-        $this->recordNotification( $notice_data );
+    /**
+     * getRegNotificationsByDate
+     *
+     * Grab all reg notifications. Include the first_datetime from the related event.
+     *
+     * @access public
+     * @return void
+     */
+    public function getRegNotificationsByDate()
+    {
+        $query =
+        "SELECT RN.*, date_format( RE.first_datetime, '%Y-%m-%d' ) as first_datetime
+           FROM " . GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX .  "reg_notification RN
+                LEFT OUTER JOIN " . GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . "reg_event RE
+                ON (RE.id = RN.reg_event)";
+        return $this->wpdb->get_results( $query, ARRAY_A );
     }
 
     /**
@@ -213,88 +158,36 @@ class GlmRegNotifications
         return $smarty->template->fetch( 'eval:' . $template_string );
     }
 
-    /**
-     * recordNotification
-     *
-     * Record the notification.
-     *
-     * @param mixed $notification Array of data for the notification
-     i
-     * @access public
-     * @return void
-     */
-    public function recordNotification( $notification )
-    {
-        if ( !$notification['notification_type'] || !$notification['account']
-            || !$notification['from_replyto'] || !$notification['subject']
-            || !$notification['message'] || !$notification['email_sent']
-        ) {
-            return false;
-        }
-        $this->wpdb->insert(
-            GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'notifications',
-            array(
-                'notification_type' => $notification['notification_type'],
-                'account'           => $notification['account'],
-                'from_replyto'      => $notification['from_replyto'],
-                'subject'           => $notification['subject'],
-                'message'           => $notification['message'],
-                'date_sent'         => date('Y-m-d H:i;s'),
-                'email_sent'        => $notification['email_sent'],
-            ),
-            array(
-                '%d', // notification_type
-                '%d', // account
-                '%s', // from_replyto
-                '%s', // subject
-                '%s', // message
-                '%s', // date_sent
-                '%s', // email_sent
-            )
-        );
-        return true;
-    }
-
-    /**
-     * Set content type of the email.
-     *
-     * Used as filter for the wp_mail_content_type
-     */
-    function set_content_type()
-    {
-        return "text/html";
-    }
-
     /**
      * queueNotice
      *
-     * Queue the noticifation.
+     * Queue the notification.
      *
-     * @param mixed $notification_type Id for the notification type
-     * @param mixed $account           Id for the account
+     * @param mixed $reg_notification Id for the notification type
+     * @param mixed $account          Id for the account
      *
      * @access public
      * @return void
      */
-    public function queueNotice( $notification_type, $account )
+    public function queueNotice( $reg_notification, $account )
     {
         // First check to see if there's already one in the queue.
         $queue_id = $this->wpdb->get_var(
             $this->wpdb->prepare(
                 "SELECT id
-                   FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notification_queue
-                  WHERE notification_type = %d
+                   FROM " . GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . "notification_queue
+                  WHERE reg_notification = %d
                     AND account = %d",
-                $notification_type,
+                $reg_notification,
                 $account
             )
         );
         if ( !$queue_id ) {
             $this->wpdb->insert(
-                GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'notification_queue',
+                GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . 'notification_queue',
                 array(
-                    'notification_type' => $notification_type,
-                    'account'           => $account
+                    'reg_notification' => $reg_notification,
+                    'account'          => $account
                 ),
                 array(
                     '%d',
@@ -315,11 +208,12 @@ class GlmRegNotifications
      * @access public
      * @return array
      */
-    public function getQueuedNotifications( $limit )
+    public function getQueuedNotifications( $limit = 10 )
     {
         $query =
         "SELECT *
-           FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notification_queue";
+           FROM " . GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . "notification_queue
+          WHERE processed_time IS NULL";
         if ( $numb = filter_var( $limit, FILTER_VALIDATE_INT ) ) {
             $query .= " LIMIT $limit OFFSET 0";
         }
@@ -329,22 +223,4 @@ class GlmRegNotifications
         );
     }
 
-    /**
-     * deleteQueue
-     *
-     * Remove notice from the queue.
-     *
-     * @param mixed $queue_id Queue id to delete.
-     *
-     * @access public
-     * @return void
-     */
-    public function deleteQueue( $queue_id )
-    {
-        $this->wpdb->delete(
-            GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'notification_queue',
-            array( 'id' => $queue_id ),
-            array( '%d' )
-        );
-    }
 }
diff --git a/models/admin/ajax/runEventRegQueue.php b/models/admin/ajax/runEventRegQueue.php
new file mode 100644 (file)
index 0000000..9838091
--- /dev/null
@@ -0,0 +1,114 @@
+<?php
+
+/**
+ * Gaslight Media Members Database
+ * PDF Output by admin-ajax
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @license  http://www.gaslightmedia.com Gaslightmedia
+ * @version  0.1
+ */
+
+// Load Support class for notifications
+require_once GLM_MEMBERS_REGISTRATIONS_PLUGIN_CLASS_PATH . '/regCartSupport.php';
+require_once GLM_MEMBERS_REGISTRATIONS_PLUGIN_CLASS_PATH . '/regNotifications.php';
+
+/**
+ * This class performs the work of handling images passed to it via
+ * an AJAX call that goes through the WorPress AJAX Handler.
+ *
+ */
+class GlmMembersAdmin_ajax_runEventRegQueue //extends GlmDataRegistrationsAccount
+{
+
+    /**
+     * WordPress Database Object
+     *
+     * @var $wpdb
+     * @access public
+     */
+    public $wpdb;
+    /**
+     * Plugin Configuration Data
+     *
+     * @var $config
+     * @access public
+     */
+    public $config;
+
+    /*
+     * Constructor
+     *
+     * This contructor sets up this model. At this time that only includes
+     * storing away the WordPress data object.
+     *
+     * @return object Class object
+     *
+     */
+    public function __construct ($wpdb, $config)
+    {
+
+        // Save WordPress Database object
+        $this->wpdb = $wpdb;
+
+        // Save plugin configuration object
+        $this->config = $config;
+
+        // Run constructor for data class
+        // parent::__construct(false, false);
+
+    }
+
+    /*
+     * Perform Model Action
+     *
+     * This model checks to see if the creditials passed in are correct.
+     *
+     * This model action does not return, it simply does it's work then calls die();
+     *
+     * @param $actionData
+     *
+     * Echos JSON string as response and does not return
+     */
+    public function modelAction( $actionData = false )
+    {
+        $return = false;
+
+        echo '<p>From the admin ajax runEventRegQueue in Event Registrations.</p>';
+
+        // Support class for sending emails.
+        $GlmRegCartSupport = new GlmRegCartSupport( $this->wpdb, $this->config );
+
+        // Grab list of reg_notifications that are due today.
+        $Notifications = new GlmRegNotifications( $this->wpdb, $this->config );
+        $notifications = $Notifications->getQueuedNotifications( 10 );
+        // echo '<pre>$notifications: ' . print_r( $notifications, true ) . '</pre>';
+
+        if ( isset( $notifications ) && is_array( $notifications ) && !empty( $notifications ) ) {
+            foreach ( $notifications as $notice ) {
+                // Send out the notice.
+                echo '<pre>$notice: ' . print_r( $notice, true ) . '</pre>';
+                $GlmRegCartSupport->sendHtmlEmail(
+                    $notice['to_email'],
+                    $notice['subject'],
+                    wpautop( $notice['html_message'] )
+                );
+                // Mark it as processed.
+                $this->wpdb->update(
+                    GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . 'notification_queue',
+                    array( 'processed_time' => date( 'Y-m-d' ) ),
+                    array( 'id' => $notice['id'] ),
+                    array( '%s' ),
+                    array( '%d' )
+                );
+            }
+        }
+
+
+        exit();
+    }
+}
index 18ce6d6..3576999 100644 (file)
@@ -13,8 +13,8 @@
  * @version  0.1
  */
 
-// Load Members data abstract
-// require_once GLM_MEMBERS_REGISTRATIONS_PLUGIN_CLASS_PATH . '/data/dataAccount.php';
+// Load Support class for notifications
+require_once GLM_MEMBERS_REGISTRATIONS_PLUGIN_CLASS_PATH . '/regNotifications.php';
 
 /**
  * This class performs the work of handling images passed to it via
@@ -79,6 +79,91 @@ class GlmMembersAdmin_ajax_setupEventRegQueue //extends GlmDataRegistrationsAcco
 
         echo '<p>From the admin ajax setupEventRegQueue in Event Registrations.</p>';
 
+        // Grab list of reg_notifications that are due today.
+        $Notifications = new GlmRegNotifications( $this->wpdb, $this->config );
+        $notifications = $Notifications->getRegNotificationsByDate();
+        echo '<pre>$notifications: ' . print_r( $notifications, true ) . '</pre>';
+
+        foreach ( $notifications as $notice ) {
+            // have to check to see when this notice will go out.
+            $notice_date = new DateTime( $notice['first_datetime'] );
+            $notice_date->add( DateInterval::createFromDateString( $notice['notification_days'] . ' day' ) );
+            $current_date = new DateTime( date( 'Y-m-d' ) );
+            $interval = $current_date->diff($notice_date);
+            $daysAway = (int)$interval->format( '%d' );
+
+            // We're only going to queue notifications if they are 0 days away.
+            if ( $daysAway === 0 ) {
+                // get list of attendees for adding notices to queue.
+                $attendees = $Notifications->getAttendeesForEventWithEmails( $notice['reg_event'] );
+                echo '<pre>$attendees: ' . print_r( $attendees, true ) . '</pre>';
+
+                foreach ( $attendees as $attendee ) {
+                    // Check to see if the attendee is already in the queue
+                    $notice_id = $this->wpdb->get_var(
+                        $this->wpdb->prepare(
+                            "SELECT id
+                               FROM " . GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . "notification_queue
+                              WHERE reg_notification = %d
+                                AND account = %d",
+                            $notice['id'],
+                            $attendee['id']
+                        )
+                    );
+                    if ( !$notice_id ) {
+                        // Create the html_message to be sent
+                        $template_data = array(
+                            'event' => array(
+                                'name' => $attendee['event_name'],
+                                'date' => date( 'n/j/Y', strtotime( $attendee['event_datetime'] ) ),
+                                'time' => date( 'h:i A', strtotime( $attendee['event_datetime'] ) ),
+                            ),
+                            'registration' => array(
+                                'level' => $attendee['class_name'],
+                            ),
+                            'last' => array(
+                                'name' => $attendee['lname']
+                            ),
+                            'first' => array(
+                                'name' => $attendee['fname']
+                            ),
+                        );
+                        echo '<pre>$template_data: ' . print_r( $template_data, true ) . '</pre>';
+                        $RegCartSupport = new GlmRegCartSupport( $this->wpdb, $this->config );
+                        $html_message = $RegCartSupport->generateHTML($template_data, $notice['message'], true);
+
+
+                        // Setup the queue_date
+                        $queue_data = array(
+                            'reg_notification' => $notice['id'],
+                            'account'          => $attendee['id'],
+                            'queued_time'      => date( 'Y-m-d' ),
+                            'to_email'         => $attendee['email'],
+                            'from_email'       => $this->config['settings']['reg_org_from_email'],
+                            'subject'          => $notice['name'],
+                            'html_message'     => $html_message,
+                        );
+                        echo '<pre>$queue_data: ' . print_r( $queue_data, true ) . '</pre>';
+                        // Now add them to the queue
+                        $this->wpdb->insert(
+                            GLM_MEMBERS_REGISTRATIONS_PLUGIN_DB_PREFIX . 'notification_queue',
+                            $queue_data,
+                            array(
+                                '%d',
+                                '%d',
+                                '%s',
+                                '%s',
+                                '%s',
+                                '%s',
+                                '%s',
+                            )
+                        );
+                    }
+                }
+            }
+        }
+
+
         exit();
     }
 }
index 6790a88..a278b4b 100644 (file)
@@ -68,6 +68,7 @@ $glmMembersRegistrationsAddOnValidActions = array(
             'updateAvailability'        => GLM_MEMBERS_REGISTRATIONS_PLUGIN_SLUG,
             'registrantsListExport'     => GLM_MEMBERS_REGISTRATIONS_PLUGIN_SLUG,
             'setupEventRegQueue'        => GLM_MEMBERS_REGISTRATIONS_PLUGIN_SLUG,
+            'runEventRegQueue'        => GLM_MEMBERS_REGISTRATIONS_PLUGIN_SLUG,
         ),
         'registrations' => array(
             'index'                     => GLM_MEMBERS_REGISTRATIONS_PLUGIN_SLUG,