adding email notifications when a member submits a job
authorAnthony Talarico <talarico@gaslightmedia.com>
Tue, 19 Dec 2017 20:41:54 +0000 (15:41 -0500)
committerAnthony Talarico <talarico@gaslightmedia.com>
Tue, 19 Dec 2017 20:41:54 +0000 (15:41 -0500)
admin notifications being sent when a member submits a job from the backend

classes/helper/notifications.php [new file with mode: 0644]
models/admin/jobs/index.php
views/admin/jobs/index.html
views/admin/jobs/ownerEmail.html [new file with mode: 0644]

diff --git a/classes/helper/notifications.php b/classes/helper/notifications.php
new file mode 100644 (file)
index 0000000..28c1a1a
--- /dev/null
@@ -0,0 +1,218 @@
+<?php
+/**
+ * Gaslight Media Members Database
+ * Admin Jobs/Item Member Notification
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Steve Sutton <steve@gaslightmedia.com>
+ * @license  http://www.galightmedia.com Gaslightmedia
+ * @release  1
+ */
+
+// Load the Member Data class
+require_once GLM_MEMBERS_PLUGIN_CLASS_PATH . '/data/dataMembers.php';
+require_once GLM_MEMBERS_PLUGIN_CLASS_PATH . '/data/dataMemberInfo.php';
+
+// Extend the member class
+class GlmMembersAdmin_jobs_notification extends GlmDataMembers
+{
+    /**
+     * WordPress Database Object
+     *
+     * @var $wpdb
+     * @access public
+     */
+    public $wpdb;
+    /**
+     * Plugin Configuration Data
+     *
+     * @var $config
+     * @access public
+     */
+    public $config;
+
+    /**
+     * Constructor
+     *
+     * This constructor performs the work for this model. This model returns
+     * an array containing the following.
+     *
+     * 'status'
+     *
+     * True if successful and false if there was a fatal failure.
+     *
+     * 'view'
+     *
+     * A suggested view name that the controller should use instead of the
+     * default view for this model or false to indicate that the default view
+     * should be used.
+     *
+     * 'data'
+     *
+     * Data that the model is returning for use in merging with the view to
+     * produce output.
+     *
+     * @wpdb object Word Press database object
+     *
+     * @return array Array containing status, suggested view, and any data
+     */
+    public function __construct ($wpdb, $config)
+    {
+
+        // Save WordPress Database object
+        $this->wpdb = $wpdb;
+
+        // Save plugin configuration object
+        $this->config = $config;
+
+        /*
+         * Run constructor for the Events class
+         */
+        parent::__construct($wpdb, $config);
+    }
+
+
+    /**
+     * Send the member a notice.
+     *
+     * This will be the primary contact email if found.
+     * If not found use the member info record email.
+     * If nothing in either one then return.
+     */
+    public function sendMemberNotice( $memberId, $data )
+    {
+        $member = $this->getEntry( $memberId );
+        // find notify_to
+        // See if there's a primary member contact for this member
+        $notify_to = apply_filters( 'glm-member-db-admin-get-member-primary-email', '', $memberId );
+        if ( !$notify_to ) {
+            return false;
+        }
+        $validEmail = filter_var( $notify_to, FILTER_VALIDATE_EMAIL );
+        if ( !$validEmail ) {
+            return false;
+        }
+
+        $memberInfoData = new GlmDataMemberInfo( $this->wpdb, $this->config );
+        $memberInfo     = $memberInfoData->getActiveInfoForMember( $memberId );
+        // get settings
+        $settings    = $this->config['settings'];
+        $notify_from = $settings['from_email'];
+
+        // Setup the Smarty Engine
+        $smarty   = new smartyTemplateSupport();
+        $viewPath = GLM_MEMBERS_EVENTS_PLUGIN_PATH . '/views';
+        $smarty->template->setTemplateDir( $viewPath );
+
+        // Add standard parameters
+        require GLM_MEMBERS_PLUGIN_SETUP_PATH.'/standardTemplateParams.php';
+        $viewFile   = 'admin/events/memberEmail.html';
+
+        $dataList = GLM_MEMBERS_EVENTS_PLUGIN_ADMIN_URL ."?page=glm-members-admin-menu-events-list&glm_action=list";
+        $smarty->templateAssign( 'location', 'Member Events' );
+        $smarty->templateAssign( 'name', $data['fieldData']['name'] );
+        $smarty->templateAssign( 'pendingEvents', $dataList );
+
+        // 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  = 'Event Approved';
+        $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' ) );
+    }
+
+    /**
+     * Send the admin user a notice.
+     *
+     * Sending email notice to the Admin user setting form Event Management.
+     */
+    public function sendAdminNotice( $memberId, $data, $job_id = false)
+    {
+        $member         = $this->getEntry( $memberId );
+        $settings       = $this->config['settings'];
+        $notify_to      = $settings['to_email'];
+        $notify_from    = $settings['from_email'];
+        $notify_message = $settings['email_notification'];
+        if ( !$notify_to ) {
+            return false;
+        }
+        // Double check the email for valid address
+        $emails = explode( ',', $notify_to );
+        foreach ( $emails as $email ) {
+            $validEmail = filter_var( $email, FILTER_VALIDATE_EMAIL );
+            if ( !$validEmail ) {
+                return false;
+            }
+        }
+
+        // Setup the Smarty Engine
+        $smarty   = new smartyTemplateSupport();
+        $viewPath = GLM_MEMBERS_JOBS_PLUGIN_PATH . '/views';
+        $smarty->template->setTemplateDir( $viewPath );
+
+        // Add standard parameters
+        require GLM_MEMBERS_PLUGIN_SETUP_PATH.'/standardTemplateParams.php';
+        $viewFile   = 'admin/jobs/ownerEmail.html';
+
+        $smarty->templateAssign( 'notify_message', $notify_message);
+        $smarty->templateAssign( 'member', $member );
+        $smarty->templateAssign( 'name', $data['fieldData']['title']['name'] );
+     
+        // 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' ) );
+
+        // Set the Reply-To to use Admin Contact E-Mail
+        $replyTo  = $notify_from;
+        $subject  = 'New Job Posting';
+        $message  = $htmlMessage;
+        $header[] = 'From:' . $notify_from;
+        $header[] = 'Reply-To:' . $replyTo;
+
+        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.
+     *
+     * Used as filter for the wp_mail_content_type
+     */
+    function set_content_type()
+    {
+        return "text/html";
+    }
+}
index aa2c496..6418170 100644 (file)
@@ -115,6 +115,11 @@ class GlmMembersAdmin_jobs_index extends GlmDataJobs
         $isPending  = ( !empty( $_REQUEST['pending-job'] ) ) ? true : false;
         $textSearch = ( !empty( $_REQUEST['text-search'] ) ) ? filter_var($_REQUEST['text-search'], FILTER_SANITIZE_STRING) : false;
         
+        // Load Notifications from the helper classes
+        include_once GLM_MEMBERS_JOBS_PLUGIN_CLASS_PATH. '/helper/notifications.php';
+        $notification = new GlmMembersAdmin_jobs_notification( $this->wpdb, $this->config );
+        
+        
         // Check if there's a logged in user who is locked to their own entity
         $lockedToMember = apply_filters('glm_members_locked_to_member_id', false);
 
@@ -227,15 +232,23 @@ class GlmMembersAdmin_jobs_index extends GlmDataJobs
                     
                     $option = 'edit';
                     $jobAdded = true;
+                    
+                    
 
                 } else {
                     $option = 'add';
                     $jobAddError = true;
                 }
-    
+                
                 $job_data = $this->getList($where);
                 $view_file = 'edit';
 
+                if ( $isModerated ) {
+                    if ( isset( $_REQUEST['member'] ) && $memberID = filter_var( $_REQUEST['member'] ) ) {
+                        $notification->sendAdminNotice( $memberID, $job );
+                    }
+                }
+
                 break;
 
             case 'edit':
index e27b543..2ca7be2 100644 (file)
@@ -65,7 +65,7 @@
                             <span>{if $data.company} {$data.company} {else} &nbsp; {/if}</span>
                         </div>
                         <div class="job-member-list glm-small-12 glm-large-2 glm-columns no-padding">
-<!--                            <pre>{$data|@print_r}</pre>-->
+                   
                             {if $data.member} {$data.member} {else} &nbsp; {/if}
                         </div>
                         <div class="job-date-list glm-small-12 glm-large-2 glm-columns no-padding">
diff --git a/views/admin/jobs/ownerEmail.html b/views/admin/jobs/ownerEmail.html
new file mode 100644 (file)
index 0000000..b42c19a
--- /dev/null
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta http-equiv="content-type" content="text/html;charset=utf-8">
+    </head>
+    <body>
+        <h1 id="title"> New Job Posting 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;">Member Name:</td>
+                <td style="width:200px;padding:5px;border:1px solid #ccc;">{$member.name}</td>
+            </tr>
+            <tr>
+                <td style="font-weight:bold;width:200px;padding:5px;border:1px solid #ccc;">Job Title:</td>
+                <td style="width:200px;padding:5px;border:1px solid #ccc;">{$name}</td>
+            </tr>
+        </table>
+
+        <div id="emailFooter">
+   
+        </div>
+    </body>
+</html>