From 871037c4ac205ff88dc923a27f13bf398107fe0a Mon Sep 17 00:00:00 2001 From: Anthony Talarico Date: Tue, 19 Dec 2017 09:25:14 -0500 Subject: [PATCH] adding email notification sending email when a member submits a new forsale item from the admin --- classes/helper/notifications.php | 218 ++++++++++++++++++++++++++++ models/admin/forSale/index.php | 8 +- views/admin/forSale/edit.html | 2 +- views/admin/forSale/ownerEmail.html | 27 ++++ 4 files changed, 253 insertions(+), 2 deletions(-) create mode 100644 classes/helper/notifications.php create mode 100644 views/admin/forSale/ownerEmail.html diff --git a/classes/helper/notifications.php b/classes/helper/notifications.php new file mode 100644 index 0000000..38e3661 --- /dev/null +++ b/classes/helper/notifications.php @@ -0,0 +1,218 @@ + + * @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_items_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) + { + $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_FOR_SALE_PLUGIN_PATH . '/views'; + $smarty->template->setTemplateDir( $viewPath ); + + // Add standard parameters + require GLM_MEMBERS_PLUGIN_SETUP_PATH.'/standardTemplateParams.php'; + $viewFile = 'admin/forSale/ownerEmail.html'; + + $smarty->templateAssign( 'notify_message', $notify_message); + $smarty->templateAssign( 'member', $member ); + $smarty->templateAssign( 'name', $data['fieldData']['title'] ); + + // 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 For Sale Item'; + $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"; + } +} diff --git a/models/admin/forSale/index.php b/models/admin/forSale/index.php index dd83c00..d76366c 100644 --- a/models/admin/forSale/index.php +++ b/models/admin/forSale/index.php @@ -89,6 +89,10 @@ class GlmMembersAdmin_forSale_index extends GlmDataForSale { $members = new GlmDataMembers($this->wpdb, $this->config); $member_list = $members->getList(); + + // Load Notifications from the helper classes + include_once GLM_MEMBERS_FOR_SALE_PLUGIN_CLASS_PATH. '/helper/notifications.php'; + $notification = new GlmMembersAdmin_items_notification( $this->wpdb, $this->config ); $success_message = ""; @@ -217,7 +221,9 @@ class GlmMembersAdmin_forSale_index extends GlmDataForSale $option = 'add'; $itemAddError = true; } - + if($isModerated){ + $notification->sendAdminNotice($memberID, $item); + } $view_file = 'edit'; break; diff --git a/views/admin/forSale/edit.html b/views/admin/forSale/edit.html index bb18fb3..c3d2046 100644 --- a/views/admin/forSale/edit.html +++ b/views/admin/forSale/edit.html @@ -80,7 +80,7 @@ {if !$lockedToMember && $memberList} - +
Member diff --git a/views/admin/forSale/ownerEmail.html b/views/admin/forSale/ownerEmail.html new file mode 100644 index 0000000..c358c7e --- /dev/null +++ b/views/admin/forSale/ownerEmail.html @@ -0,0 +1,27 @@ + + + + + + +

New For Sale Item

+ + {if $notify_message} +
+

{$notify_message}

+
+ {/if} + + + + + + + + + + +
Member Name:{$member.name}
Item:{$name}
+ + + -- 2.17.1