Admin.php 8.25 KB
<?php
namespace Tz\WordPress\Tools\Notifications\Settings;
error_reporting(E_ALL^E_DEPRECATED);

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

const CAPABILITY                    = "manage_notifications";
const MANAGE_SYSTEM_NOTIFICATIONS   = "create_system_notifications";

    call_user_func(function() {
        $role = get_role('administrator');
        $role->add_cap(CAPABILITY);
        $role->add_cap(MANAGE_SYSTEM_NOTIFICATIONS);
        //$role->remove_cap(SUB_ADMIN_CAPABILITY);
        Tools\add_actions(__NAMESPACE__ . '\Actions');
    });

function display_page() {
    
   
    if (isset($_GET['action']) && $_GET['action']=="edit") {
        require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'edit.php');
        
    } else {
    
        if (isset($_GET['action']) && $_GET['action']=="delete") {
           wp_delete_post($_GET['page_id'],true);
           
        } elseif (isset($_GET['action']) && $_GET['action']=="delete") {
            
            $id = $_GET['page_id'];
            
            $postdata = get_post_meta($id,'details',true);
            $postdata['status'] = "archived";
            
            update_post_meta($id,'details',$postdata);
            
        } 
            
    
        // get all the notifications that status != "archived";
        $notifications = array();
        $notifications['triggered']   = array();
        $notifications['scheduled'] = array();
        
        $args = array(
            'post_type'     => 'notifications'
          , 'numberposts'   => -1
          , 'orderby'       => 'modified'
          , 'order'         => 'desc'
        );
        
        
        foreach(get_posts($args) as $entry) {
            $id = $entry->ID;
            
            $details                = get_post_meta($id,'details',true);
            $email                  = get_post_meta($id,'email',true);
            $system                 = get_post_meta($id,'system',true);
            $sms                    = get_post_meta($id,'sms',true);
            
            $entry->trigger         = $details['trigger'];
            $entry->status          = isset($details['status']) ? $details['status'] : "active";
            $entry->type            = $details['type'];
            $entry->sendto          = $details['sendto'];
            
            $entry->is_email        = (($email['text'] != "" || $email['html'] != "")) ? true : false;
            $entry->is_system       = ($system != "") ? true : false;
            $entry->is_sms          = ($sms != "") ? true : false;
            
            $entry->execute_date    = $details['execute_date'];
            
            if ($entry->status != "archived") {
                $notifications[$entry->type][] = $entry;
            }
            
            
        }
    
        require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'admin.php');
        
        
    
    } 
    

    
}

function mysqldatetime_to_timestamp($datetime = "")
{
  // function is only applicable for valid MySQL DATETIME (19 characters) and DATE (10 characters)
  $l = strlen($datetime);
    if(!($l == 10 || $l == 19))
      return 0;

    //
    $date = $datetime;
    $hours = 0;
    $minutes = 0;
    $seconds = 0;

    // DATETIME only
    if($l == 19)
    {
      list($date, $time) = explode(" ", $datetime);
      list($hours, $minutes, $seconds) = explode(":", $time);
    }

    list($year, $month, $day) = explode("-", $date);

    return mktime($hours, $minutes, $seconds, $month, $day, $year);
}

function fixFilesArray(&$files)
{
    $names = array( 'name' => 1, 'type' => 1, 'tmp_name' => 1, 'error' => 1, 'size' => 1);

    foreach ($files as $key => $part) {
        // only deal with valid keys and multiple files
        $key = (string) $key;
        if (isset($names[$key]) && is_array($part)) {
            foreach ($part as $position => $value) {
                $files[$position][$key] = $value;
            }
            // remove old key reference
            unset($files[$key]);
        }
    }
}

function create_trigger_notification() {
    print "hello";
}

function notification_settings() {
    print "settings";
}

function create_notification() {
    
    global $wpdb;
    
    
    if ($_POST && isset($_POST['_POSTED_']) && $_POST['_POSTED_']=="yes") {
        
        // ok, so now we need to create the notification.
        class postTemplate {
            var $post_title     = '';
            var $post_content   = '';
            var $post_status    = 'publish';
            var $post_type      = 'notifications'; 
            var $comment_status = 'closed';
        }
        
        //details
        $type                   = $_POST['type'];
        $title                  = $_POST['title'];
        $sendto                 = $_POST['sendto'];
        $execute_date           = ($type=="scheduled") ? $_POST['execute_date'] : "0000-00-00 00:00:00";
        $trigger                = ($type=="scheduled") ? "scheduled-cron-job" : $_POST['trigger'];
        
        // email
        $subject                = $_POST['subject'];
        $text                   = $_POST['text'];
        $html                   = $_POST['html'];
        $attachments    = array();
        $upload_dir     = __DIR__ . "/uploads/";
        fixFilesArray($_FILES['attachment']);
        foreach ($_FILES['attachment'] as $position => $file) {
            // should output array with indices name, type, tmp_name, error, size
            if($file['name'] != "") { 
                move_uploaded_file($file['tmp_name'],$upload_dir . $file['name']);
                $attachments[] = $file['name'];
            }
        }
        
        // system
        $system                 = $_POST['system'];
        
        // SMS
        $sms                    = $_POST['sms'];
        

        // make post...
        
        $notification = new postTemplate();
        $notification->post_title       = $title;
        $notification->post_content     = "Notification created ".date('Y-m-d H:i:s')." --- to be sent on $execute_date";
        $notification->post_date_gmt    = date("Y-m-d H:i:s",time());
        
        $id = wp_insert_post($notification);
        
        add_post_meta($id, "details", array(
            'type'          => $type
          , 'sendto'        => $sendto
          , 'status'        => 'pending'
          , 'trigger'       => $trigger
          , 'execute_date'  => $execute_date
        ));
        
        add_post_meta($id, "email", array(
            'subject'       => $subject
          , 'text'          => $text
          , 'html'          => $html
          , 'attachments'   => $attachments
        ));
        add_post_meta($id, "system", $system);
        add_post_meta($id, "sms", $sms);
         
        
        $flash = "Notification Saved Successfully!";
        require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'create.php');
        
    } else {
        require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'create.php');
    }

    
}

class Actions {

    public static function init() {
        global $wpdb;
        $wpdb->show_errors();
        
        register_post_type( 'notifications', array(
            'label'                 => __('Notifs')
          , 'public'                => true
          , 'show_ui'               => false
          , 'hierarchical'          => false
          , 'exclude_from_search'   => true
        ));
        
    }

    public static function admin_menu() {
        add_menu_page('Notifications','Notifications',CAPABILITY,'notifications',__NAMESPACE__ . '\display_page' );
        add_submenu_page('notifications','New Notification', 'New Notification',CAPABILITY,'notifications-create-new',__NAMESPACE__ . '\create_notification');
        add_submenu_page('notifications','Settings', 'Settings',CAPABILITY,'notifications-settings',__NAMESPACE__ . '\notification_settings');        
    }
    
    public function admin_init() {
        register_setting(Notifications\OPTION_NAME, Notifications\OPTION_NAME);
    }

}


?>