Send the email to notify_to from management
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 23 Nov 2016 19:16:26 +0000 (14:16 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 23 Nov 2016 19:16:26 +0000 (14:16 -0500)
When member edits coupon send notify message to the notify_to address.

classes/helper/notification.php
models/admin/coupons/list.php
views/front/coupons/ownerEmail.html [new file with mode: 0644]

index fc13911..1012324 100644 (file)
@@ -85,14 +85,60 @@ class GlmMembersAdmin_coupon_notification extends GlmDataMembers
     /**
      * Send the admin user a notice
      */
-    public function sendAdminNotice( $memberId )
+    public function sendAdminNotice( $memberId, $coupon )
     {
-        echo '<pre>$memberId: ' . print_r( $memberId, true ) . '</pre>';
         $member = $this->getEntry( $memberId );
-        echo '<pre>$member: ' . print_r( $member, true ) . '</pre>';
         $settings       = $this->config['settings'];
         $notify_to      = $settings['notify_to'];
         $notify_from    = $settings['notify_from'];
         $notify_message = $settings['notify_message'];
+
+        // Setup the Smarty Engine
+        $smarty = new smartyTemplateSupport();
+        $viewPath = GLM_MEMBERS_COUPONS_PLUGIN_PATH . '/views';
+        $smarty->template->setTemplateDir($viewPath);
+
+        // Add standard parameters
+        require GLM_MEMBERS_PLUGIN_SETUP_PATH.'/standardTemplateParams.php';
+        $viewFile = 'front/coupons/ownerEmail.html';
+
+        $smarty->templateAssign('notify_message', $notify_message);
+        $smarty->templateAssign('name', $coupon['fieldData']['name']);
+        $smarty->templateAssign('start_date', $coupon['fieldData']['start_date']);
+        $smarty->templateAssign('end_date', $coupon['fieldData']['end_date']);
+        $smarty->templateAssign('expire', $coupon['fieldData']['expire']);
+
+        // 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' ) );
+
+        $subject  = 'Coupon Form Submission';
+        $message  = $htmlMessage;
+        $header[] = 'From:' . $notify_from;
+        $header[] = 'Reply-To:' . $notify_from;
+
+        wp_mail( $notify_to, $subject, $message, $header );
+
+        // remove the filter to avoid conflicts
+        remove_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) );
+    }
+
+    /**
+     * Set content type of the email
+     */
+    function set_content_type()
+    {
+        return "text/html";
     }
 }
index cc95829..650c173 100644 (file)
@@ -239,6 +239,11 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons
 
                         $option = 'edit';
                         $couponAdded = true;
+                        if ( $lockedToMember ) {
+                            if ( isset( $_REQUEST['ref_dest'] ) && $member_id = filter_var( $_REQUEST['ref_dest']) ) {
+                                $notification->sendAdminNotice( $member_id, $coupon);
+                            }
+                        }
 
                     } else {
                         $option = 'add';
@@ -264,12 +269,6 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons
                 break;
 
             case 'update':
-                // test conditions for sending out notice need to be setup here
-                // True if the member is editing the record and the status
-                // turns to pending
-                if ( isset( $_REQUEST['ref_dest'] ) && $member_id = filter_var( $_REQUEST['ref_dest']) ) {
-                    $notification->sendAdminNotice( $member_id );
-                }
                 // Get the original Coupon Status. Before the update.
                 $old_coupon_status = $this->wpdb->get_var(
                     $this->wpdb->prepare(
@@ -296,6 +295,10 @@ class GlmMembersAdmin_coupons_list extends GlmDataCoupons
                         if ( $old_coupon_status == 20 && $new_status == 10 ) {
                             // Update approved timestamp.
                             $this->updateTimestamp('approved', $this->couponID);
+                        } else if ( $lockedToMember && $old_coupon_status == 10 && $new_status == 20 ) {
+                            if ( isset( $_REQUEST['ref_dest'] ) && $member_id = filter_var( $_REQUEST['ref_dest']) ) {
+                                $notification->sendAdminNotice( $member_id, $coupon);
+                            }
                         }
                     }
 
diff --git a/views/front/coupons/ownerEmail.html b/views/front/coupons/ownerEmail.html
new file mode 100644 (file)
index 0000000..1ab995b
--- /dev/null
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta http-equiv="content-type" content="text/html;charset=utf-8">
+    </head>
+    <body>
+        <h1 id="title"> Coupon Submission Notification</h1>
+
+        {if $notify_message}
+        <div style="width: 500px">
+            <p> {$notify_message} </p>
+        </div>
+        {/if}
+
+        <table cellspacing="0" cellpadding="0" width="500" style="background: lightgrey;border:1px solid #ccc;border-collapse:collapse;">
+            <tr>
+                <td style="font-weight:bold;width:200px;padding:5px;border:1px solid #ccc;">Coupon Name:</td>
+                <td style="width:200px;padding:5px;border:1px solid #ccc;">{$name}</td>
+            </tr>
+            <tr>
+                <td style="font-weight:bold;width:200px;padding:5px;border:1px solid #ccc;">Coupon Start Date: </td>
+                <td style="width:200px;padding:5px;border:1px solid #ccc;"> {$start_date} </td>
+            </tr>
+            <tr>
+                <td style="font-weight:bold;width:200px;padding:5px;border:1px solid #ccc;" >Coupon End Date:</td>
+                <td style="width:200px;padding:5px;border:1px solid #ccc;" >{$end_date}</td>
+            </tr>
+            <tr>
+                <td style="font-weight:bold;width:200px;padding:5px;border:1px solid #ccc;" >Coupon Expire Date:</td>
+                <td style="width:200px;padding:5px;border:1px solid #ccc;" >{$expire}</td>
+            </tr>
+        </table>
+
+        <div id="emailFooter">
+            <p style="margin-bottom: 0px;">A new Coupon has been added to your Website from your &quot;{$location}&quot; page.</p>
+            <p style="margin-top: 0px;">To approve it, please go to the <a style="color: rgb(5, 104, 179);" href="{$pendingCoupons}"> coupons </a> page in your admin.</p>
+        </div>
+    </body>
+</html>