HEX
Server: LiteSpeed
System: Linux s917.lon1.mysecurecloudhost.com 4.18.0-477.21.1.lve.1.el8.x86_64 #1 SMP Tue Sep 5 23:08:35 UTC 2023 x86_64
User: assibfaf (3034)
PHP: 7.4.33
Disabled: NONE
Upload Files
File: /home/assibfaf/customassignment.us/prox-classes/Notification.php
<?php 
/**
 * @package    Proxim
 * @author     Davison Pro, Chris Mugo <davisonpro.coder@gmail.com | https://davisonpro.dev>
 * @copyright  2019 Proxim
 * @version    1.5.0
 * @since      File available since Release 1.0.0
 */

namespace Proxim;

use Db;
use Proxim\Database\DbQuery;
use Proxim\Cache\Cache;
use Proxim\User\Customer;
use Proxim\User\Employee;
use Proxim\Configuration;
use Proxim\ObjectModel;
use Proxim\Validate;

/**
 * Class Notification.
 */
class Notification extends ObjectModel
{
    CONST ACTION_ORDER = 'order';
    CONST ACTION_MESSAGE = 'message';
    CONST ACTION_REPLY = 'reply';
    CONST ACTION_FILE = 'file';

    const TYPE_NEW_ORDER = 'new_order'; 
    const TYPE_NEW_INQUIRY = 'new_inquiry'; 

    const TYPE_PAYMENT = 'new_payment'; 

    const TYPE_MESSAGE = 'new_message'; 
    const TYPE_REPLY = 'new_reply'; 

    const TYPE_DELETE_FILE = 'delete_file'; 

    /** @var $id Notification ID */
    public $id;
    
    /** @var string to_department */
    public $to_department;
    
    /** @var int to_user_id */
    public $to_user_id;
    
    /** @var string from_department */
    public $from_department;
    
    /** @var int from_user_id */
    public $from_user_id;

    /** @var string action */
    public $action;

    /** @var string node_type */
    public $node_type;

    /** @var string node_url */
    public $node_url;

    /** @var string message */
    public $message;

    /** @var bool is_seen */
    public $is_seen = 0;

    /** @var string date_upd */
    public $date_upd;

    /** @var string date_add */
    public $date_add;
    
    /**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'notification',
        'primary' => 'notification_id',
        'fields' => array(
            'to_department' => array('type' => self::TYPE_STRING, 'size' => 255, 'required' => true),
            'to_user_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
            'from_department' => array('type' => self::TYPE_STRING, 'size' => 255, 'required' => true),
            'from_user_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
            'action' => array('type' => self::TYPE_STRING, 'size' => 255, 'required' => true),
            'node_type' => array('type' => self::TYPE_STRING, 'size' => 255, 'required' => true),
            'node_url' => array('type' => self::TYPE_STRING, 'size' => 255, 'required' => true),
            'message' => array('type' => self::TYPE_STRING),
            'is_seen' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
            'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull'),
            'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull'),
        ),
    );

    public function __construct($id = null)
    {
        parent::__construct($id);
    }

    public function send( $departments = array() ) {

        if(!is_array($departments)) {
            $departments = array($departments);
        }

        foreach($departments as $department) {
            if($department == DEPARTMENT_SUPPORT) {
                $sql = new DbQuery();
                $sql->select('e.employee_id');
                $sql->from('employee', 'e');
                $sql->where('e.`employee_group` = ' . (int) Employee::GROUP_ADMIN . ' OR e.`employee_group` = ' . (int) Employee::GROUP_SUB_ADMIN );
                $result = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS( $sql );

                foreach($result as $employee) {
                    $this->to_department = DEPARTMENT_SUPPORT;
                    $this->to_user_id = $employee['employee_id'];
                    $this->add();
                }
            }

            if($department == DEPARTMENT_WRITER) {
                $sql = new DbQuery();
                $sql->select('e.employee_id');
                $sql->from('employee', 'e');
                $sql->where('e.`employee_group` = ' . (int) Employee::GROUP_WRITER );
                $result = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS( $sql );

                foreach($result as $employee) {
                    $this->to_department = DEPARTMENT_WRITER;
                    $this->to_user_id = $employee['employee_id'];
                    $this->add();
                }
            }
        }

    }

    public function add($autodate = true, $null_values = true) {
        $app = Application::getInstance();
        $user = $app->container->user;

        /* if the viewer is the target */
        if( $user->id == $this->to_user_id && $this->to_department == DEPARTMENT_CUSTOMER) {
            return;
        }

        /* get receiver user */
        $receiver = new Employee( (int) $this->to_user_id );
        if (!validate::isLoadedObject($receiver)) {
            return;
        }

        /* insert notification */
        $success = parent::add($autodate, $null_values);

        /* update notifications counter +1 */
        $receiver->live_notifications_counter = $receiver->live_notifications_counter+1;
        $receiver->update();

        /** Email notifications **/
        switch ($this->action) {

            case '':
                
                break;
        }

        return $success;
    }
}