Notifications.php 6.21 KB
<?php
namespace Tz\WordPress\Tools\Notifications;

use Tz\WordPress\Tools;
use Tz\Common;


const OPTION_NAME = "notif_options";

    call_user_func(function() {
        Tools\add_actions(__NAMESPACE__ . '\Actions');
        Vars::$options = new Tools\WP_Option(OPTION_NAME);
        if (is_admin()) {
            require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Validation.php');
            require_once(__DIR__ . DIRECTORY_SEPARATOR . 'Admin.php');
        }
    });


function get_notification_by_trigger($trigger) {
    $args = array(
        'post_type'     => 'notifications'
      , 'numberposts'   => -1
      , 'orderby'       => 'modified'
      , 'order'         => 'desc'
    );
    
    $my_notif = false;
    
    foreach(get_posts($args) as $entry) {
        $details = get_post_meta($entry->ID,'details',true);
        if ($details['type']=="triggered" && $details['trigger'] == $trigger) {
            $my_notif = $entry;
            break;
        }
    }
    
    return $my_notif;

}

/**
 * GET NUMBER OF NEW NOTICES
 @returns (Int)
*/
function get_num_notices() {
    // get number of notices for this user that are new...
    $notices = get_user_meta(Tools\getCurrentUser()->ID, 'notices',true);
    $notifications = array();
    // return $num;
    if(empty($notices)) { 
        return 0; 
    } else {
        foreach($notices as $notice) {
            if ($notice['status']=="show") {
                $notifications[] = $notice;
            }
        }
    }
    return count($notifications);
}

function remove_notice($notification_id = -1) {
    $notices = get_user_meta(Tools\getCurrentUser()->ID, 'notices',true);
    if (!empty($notices)) {
        foreach($notices as $id => $notice) {
            if ($id==$notification_id) {
                $notices[$id]['status']="hide";
                break;
            }
        }
    }
    update_user_meta(Tools\getCurrentUser()->ID,'notices',$notices);
}

/**
 Send Notifications
 @trigger   = notification unique slug name
*/
function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array()) {
    $notification = get_notification_by_trigger($trigger);
    if($notification) {
        
        // get the user and user details....
        $user                   = Tools\getCurrentUser();
        $notify_me              = get_user_meta($user->ID,'notify_me',true);
        $notify_format          = get_user_meta($user->ID,'notify_format',true);
        $email                  = get_user_meta($user->ID,'email_address_preference',true);
        $cell                   = get_user_meta($user->ID,'home_mobile',true);
        
        // get the notification and notificatio details....
        $nid                    = $notification->ID;
        $details                = get_post_meta($nid,'details',true);
        $email                  = get_post_meta($nid,'email',true);
        $system                 = get_post_meta($nid,'system',true);
        $sms                    = get_post_meta($nid,'sms',true);
        
        
        $notification->trigger         = $details['trigger'];
        $notification->status          = isset($details['status']) ? $details['status'] : "active";
        $notification->type            = $details['type'];
        $notification->sendto          = $details['sendto'];
        
        $notification->is_email        = (($email['text'] != "" || $email['html'] != "")) ? true : false;
        $notification->is_system       = (isset($system['message']) && $system['message'] != "") ? true : false;
        $notification->is_sms          = (isset($sms['message']) && $sms['message'] != "") ? true : false;
        
        
        
        // if is_sms =============================================
        if ($notification->is_sms && $notify_me && !empty($cell)) {
        }
        
        // if is_system ==========================================
        if ($notification->is_system) {
            $notices = get_user_meta($user->ID,'notices',true);
            
            $insert = array(
                'notification_id'   => $nid
              , 'status'            => 'show'
              , 'sent_date'         => time()
              , 'args'              => $args
            );
            if(empty($notices)) {
                $notices = array();
            }
                $notices[] = $insert; 
            
                       
            update_user_meta($user->ID,'notices',$notices);
        }
        
        
        // if is_email ===========================================
        if ($notification->is_email && $notify_me) {
        }
        
        
        
        
    } else {
        die('no notification');
    }
    // if the system notification has set current user than get current user otherwise loop through the users needed.
}

function getGroups($grpID=0) {
    global $userAccessManager;
        
    $groups = array();
    if($grpID > 0) {
        $userGroups = $userAccessManager->getAccessHandler()->getUserGroups($grpID);
        return $userGroups->getGroupName();        
    } else {
        $userGroups = $userAccessManager->getAccessHandler()->getUserGroups();
    }
    foreach($userGroups as $group) {
        $groups[$group->getId()] = $group->getGroupName();
    }
    return $groups;
}

function get_field_lookup($var) {
    return isset(Vars::$field_lookup[$var]) ? Vars::$field_lookup[$var] : $var;
}



function current_url() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
    $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
    $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
}

class Actions {
    public static function wp_ajax_update_notification_count() {
        $count = get_num_notices();
        $return = array('count'=>$count);
        die(json_encode($return));
    }
}

class Vars {
    public static $options;
    public static $field_lookup = array(
        'current_user'  => "Current Logged-in User"
      , 'allusers'      => "All Users"
    );
}
?>