Working on setup for sending emails when creating invoices.
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 27 Dec 2017 21:33:10 +0000 (16:33 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 27 Dec 2017 21:33:10 +0000 (16:33 -0500)
classes/notifications.php
config/plugin.ini
models/admin/settings/notificationTypes.php
setup/adminHooks.php

index c1048da..7baacb6 100644 (file)
@@ -68,12 +68,12 @@ class GlmNotifications
      * @access public
      * @return void
      */
-    public function sendEmailNotification( $notification_id, $account )
+    public function sendEmailNotification( $notification_id, $account_id )
     {
         // Support Class
         $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
         // get the Account
-        $account = $BillingSupport->getAccountById( $account );
+        $account = $BillingSupport->getAccountById( $account_id );
         if ( !$account ) {
             // If there's no account then return false.
             return false;
@@ -135,6 +135,74 @@ class GlmNotifications
 
         // remove the filter to avoid conflicts
         remove_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) );
+
+        // Send notification to recordNotification
+        $notice_data = array(
+            'notification_type' => $notification_id,
+            'account'           => $account_id,
+            'from_replyto'      => $from_header,
+            'subject'           => $subject,
+            'message'           => $message,
+            'email_sent'        => $to_email,
+        );
+        $this->recordNotification( $notice_data );
+    }
+
+    /**
+     * 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;
+    }
+
+    public function getNotificationByType( $type )
+    {
+        echo '<pre>$this->config: ' . print_r( $this->config['send_action_numb'], true ) . '</pre>';
+        return $this->wpdb->get_results(
+            $this->wpdb->prepare(
+                "SELECT id
+                   FROM "  . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX .  "notification_types
+                  WHERE send_by_action
+                    AND send_action = %d",
+                $this->config['send_action_numb'][$type]
+            ),
+            ARRAY_A
+        );
     }
 
     /**
index eae86b3..3e92766 100644 (file)
@@ -38,6 +38,9 @@ send_date_period[40] = "Year"
 send_action[10] = "Create Invoice"
 send_action[20] = "Received Payment"
 
+send_action_numb['Create Invoice']   = 10;
+send_action_numb['Received Payment'] = 20;
+
 ; Send Date When
 send_date_when[10] = "Before"
 send_date_when[20] = "After"
index bd362b3..2032b58 100644 (file)
@@ -151,15 +151,27 @@ class GlmMembersAdmin_settings_notificationTypes extends GlmDataNotificationType
              * Currently this is just using a hard coded account id  of 1.
              * After the test it will call wp_die
              */
-            echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
-            require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/notifications.php';
-            $Notifications = new GlmNotifications( $this->wpdb, $this->config );
+            // echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
+            // require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/notifications.php';
+            // $Notifications = new GlmNotifications( $this->wpdb, $this->config );
+            // // Test
+            // $notices = $Notifications->getNotificationByType( 'Create Invoice' );
+            // echo '<pre>$notices: ' . print_r( $notices, true ) . '</pre>';
+            // if ( $notices ) {
+            //     foreach ( $notices as $notice ) {
+            //         $Notifications->sendEmailNotification( $notice['id'], 1 );
+            //     }
+            // }
+            // wp_die('here');
+
+
+
             $notification_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT );
             if ( $notification_id ) {
                 echo '<pre>$notification_id: ' . print_r( $notification_id, true ) . '</pre>';
                 $account = 1; // TODO: remove this. It's for testing only
                 $noticeReturned = $Notifications->sendEmailNotification( $notification_id, $account );
-                var_dump( $noticeReturned );
+                // var_dump( $noticeReturned );
             }
             wp_die('Testing here');
             break;
@@ -168,7 +180,7 @@ class GlmMembersAdmin_settings_notificationTypes extends GlmDataNotificationType
             /**
              * add:
              *
-             * This calls the dataabstract newEntry method and sets up the
+             * This calls the dataAbstract newEntry method and sets up the
              * edit for for creating a new notification_type.
              */
             $notification = $this->newEntry();
index ab234d1..ea860a1 100644 (file)
  *
  *  Also note that parameters will be in the context of the main admin controller constructor.
   */
+
+add_action(
+    'glm-member-db-billing-create-invoice',
+    function( $account ){
+        // Need to get notification id's for all notifications that are done when invoice is created.
+        $notices = $this->wpdb->get_results(
+            $this->wpdb->prepare(
+                "SELECT id
+                   FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notification_types
+                  WHERE send_action = %d",
+                $this->config['send_action_numb']['Create Invoice']
+            ),
+            ARRAY_A
+        );
+    },
+    10,
+    1
+);
+