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/public_html/prox-classes/Order/OrderMessage.php
<?php
/**
 * @package    Proxim
 * @author     Davison Pro <davisonpro.coder@gmail.com | https://davisonpro.dev>
 * @copyright  2019 Proxim
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

namespace Proxim\Order;

use Db;
use Proxim\Database\DbQuery;
use Proxim\Cache\Cache;
use Proxim\Application;
use Proxim\ObjectModel;
use Proxim\User\Customer;
use Proxim\Util\ArrayUtils;
use Proxim\Util\DateUtils;
use Proxim\Validate;
use Proxim\Tools;

/**
 * OrderMessage
 */
class OrderMessage extends ObjectModel
{
	/** @var $id OrderMessage ID */
	public $id;

	/** @var int order_id */
	public $order_id;

	/** @var int sender_id */
	public $sender_id;

	/** @var int parent_id */
	public $parent_id;
	
	/** @var int from_department */
	public $from_department;
	
	/** @var int to_department */
	public $to_department;
	
	/** @var string subject */
	public $subject;
	
	/** @var string body */
	public $body;
	
	/** @var bool is_deleted */
	public $is_deleted = 0;
	
	/** @var bool is_viewed */
    public $is_viewed = 0;
    
    /** @var bool is_verified */
	public $is_verified = 1;
	
	/** @var string date_add */
	public $date_add;
	
	/** @var string date_upd */
    public $date_upd;

	/**
     * @see ObjectModel::$definition
     */
    public static $definition = array(
        'table' => 'order_message',
        'primary' => 'message_id',
        'fields' => array( 
			'order_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
			'sender_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
			'parent_id' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
			'from_department' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
			'to_department' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
			'subject' => array('type' => self::TYPE_STRING),
			'body' => array('type' => self::TYPE_HTML),
			'is_deleted' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
            'is_viewed' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'is_verified' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull'),
			'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDateOrNull'),
		)
	);

	/**
     * constructor.
     *
     * @param null $id
     */
    public function __construct($id = null)
    {
        parent::__construct($id);
	}

	public function present() {
		global $globals;
		$app = Application::getInstance();
		$user = $app->container->user;

		$departments = ArrayUtils::get($globals, 'interlocutor_departments');

		$is_sender = $user->id == $this->sender_id ? true : false;
		$is_viewed = $is_sender ? true : $this->is_viewed;

		$body = Tools::getStringClean($this->body);

		$message = array(
			"id" => (int) $this->id, 
			"parentMessageId" => ($this->parent_id) ? (int) $this->parent_id : null, 
			"fromDepartment"=> (int) $this->from_department, 
			"fromName" => $departments[$this->from_department], 
			"fromWide" => $departments[$this->from_department], 
			"toDepartment" => (int) $this->to_department, 
			"toName" => $departments[$this->to_department], 
			"toWide" => $departments[$this->to_department], 
			"subject" => $this->subject ? Tools::getStringClean($this->subject) : $body, 
			"body" => Tools::nl2br( $this->body ), 
			"isDeleted" => (bool) $this->is_deleted, 
			"isViewed" => (bool) $is_viewed,
			"createdAt" => $this->date_add,  
			"createdAtUnixTime" => $this->date_add 
		);

		if($is_sender) {
	        $message['interlocutor'] = Tools::strtolower( $departments[$this->to_department] );
            $message['fromWide'] = 'Me';
        } else {
            $message['toWide'] = 'Me';
	        $message['interlocutor'] = Tools::strtolower( $departments[$this->from_department] );
        }

		return $message;
    }
}