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-controllers/Api/Messages.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\Api;

use Db;
use Proxim\Database\DbQuery;
use Proxim\User\Customer;
use Proxim\User\Employee;
use Proxim\Order\Order;
use Proxim\Order\OrderMessage;
use Proxim\Configuration;
use Proxim\Notification;
use Proxim\Tools;
use Proxim\Mail;
use Proxim\Validate;
use Proxim\Route;
use Proxim\Util\ArrayUtils;
use Proxim\Util\DateUtils;
use Proxim\Crypto\Hashing;

class Messages extends Route {
    public function postOrderMessage( $orderId ) { 
        $app = $this->app;
		$payload = $app->request->post();
		$user = $app->container->user;

		$order = new Order( (int) $orderId );
		if (!Validate::isLoadedObject($order)) {
			return $app->response([
				"success" => false,
			]);
		}

		$body = ArrayUtils::get($payload, 'body');
		if ( !$body ) {
			return $app->response([
				"success" => false,
			]);
		}

		$sendTo = ArrayUtils::get($payload, 'sendTo');
		if ( !$sendTo ) {
			return $app->response([
				"success" => false,
			]);
		}

		if ( $sendTo == "send_to_writer" ) {
			$to_department = DEPARTMENT_WRITER;
		} else {
			$to_department = DEPARTMENT_SUPPORT;
		}

		$message = new OrderMessage();
		$message->order_id = $order->id;
		$message->sender_id = $user->id;
		$message->from_department = DEPARTMENT_CUSTOMER;
		$message->to_department = $to_department;
		$message->body = $body;
		$success = $message->add();

		if(!$success) {
			return $app->response([
				"success" => false,
			]);
		}
 
		// send notification
		$notification = new Notification();
        $notification->from_department = DEPARTMENT_CUSTOMER;
        $notification->from_user_id = $user->id;
        $notification->action = Notification::ACTION_MESSAGE;
        $notification->node_type = Notification::TYPE_MESSAGE;
        $notification->node_url = $order->id;
		
		if($to_department == DEPARTMENT_SUPPORT) {
			$notification->send($to_department);
		} else {
			$notification->to_department = DEPARTMENT_WRITER;
        	$notification->to_user_id = $order->writer_id;
			$notification->add();

			$notification->send(DEPARTMENT_SUPPORT);
		}

		$receivers = array( Configuration::get('SITE_EMAIL', PROX_ADMIN_SITE_ID) );
		$notification_emails = $app->getNotificationEmails();
		$receivers = array_merge($receivers, $notification_emails);
		$receivers = array_unique($receivers);
		
        if( $to_department == DEPARTMENT_WRITER && $order->writer_id ) {
        	$writer = $order->getWriter();
        	if(Validate::isLoadedObject($writer)) {
        		$receivers[] = $writer->email;
        	}
        }

		try {

			foreach($receivers as $receiver_mail) {
				Mail::Send(
					'order_message',
					"Order #".$order->id.": New message",
					array(
						'orderId' => $order->id,
						'message' => Tools::nl2br($message->body),
						'order_url' => Configuration::get('SITE_DOMAIN', PROX_ADMIN_SITE_ID) . '/order/' . $order->id,
						'reply_url' => Configuration::get('SITE_DOMAIN', PROX_ADMIN_SITE_ID) . '/order/' . $order->id . '/messages',
					),
					$receiver_mail,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					PROX_ADMIN_SITE_ID
				);
			}
				
		} catch( \Exception $e ) {
		}
        
		return $app->response([
			"success" => true,
			"errors" => null,
			"newMessageId" => $message->id
		]);
    }

    public function postMessageReply( $orderId, $messageId ) { 
        $app = $this->app;
        $user = $app->container->user;
		$payload = $app->request->post();

		$order = new Order( (int) $orderId );
		if (!Validate::isLoadedObject($order)) {
			return $app->response([
				"success" => false,
			]);
		}

		$message = new OrderMessage( (int) $messageId);
		if ( !Validate::isLoadedObject($message) ) {
			return $app->response([
				"success" => false,
			]);
		}

		$body = ArrayUtils::get($payload, 'body');
		if (!$body) {
			return $app->response([
				"success" => false,
				"errors" => [
					"Message can't be blank"
				]
			]);
        }

		$subject = ArrayUtils::get($payload, 'subject');

		$reply = new OrderMessage;
		$reply->order_id = $order->id;
		$reply->sender_id = $user->id;
		$reply->parent_id = $message->id;
		$reply->from_department = DEPARTMENT_CUSTOMER;
		$reply->to_department = $message->from_department;

		if ( $message->to_department == DEPARTMENT_SUPPORT ) {
			$reply->is_verified = true;
            $reply->verified_at = DateUtils::now();
		}
		$reply->body = $body;
		$success = $reply->add();
		if (!$success) {
			return $app->response([
				"success" => false,
			]);
		}

		// send notification
		$notification = new Notification();
        $notification->from_department = DEPARTMENT_CUSTOMER;
        $notification->from_user_id = $user->id;
        $notification->action = Notification::ACTION_REPLY;
        $notification->node_type = Notification::TYPE_REPLY;
        $notification->node_url = $order->id;
		
		if ($reply->to_department == DEPARTMENT_SUPPORT) {
			$notification->send(DEPARTMENT_SUPPORT);
        } elseif( $reply->to_department == DEPARTMENT_WRITER && !is_null($order->writer_id) ) {
        	$notification->to_user_id = $order->writer_id;
			$notification->to_department = DEPARTMENT_WRITER;
			$notification->add();
        } else {
			$notification->send(DEPARTMENT_SUPPORT);
        }

		$receivers = array( Configuration::get('SITE_EMAIL', PROX_ADMIN_SITE_ID) );
		$notification_emails = $app->getNotificationEmails();
		$receivers = array_merge($receivers, $notification_emails);
		$receivers = array_unique($receivers);
				
        if( $message->to_department == DEPARTMENT_WRITER && $order->writer_id ) {
        	$writer = $order->getWriter();
        	if(Validate::isLoadedObject($writer)) {
        		$receivers[] = $writer->email;
        	}
        }

		try {
			foreach($receivers as $receiver_mail) {
				Mail::Send(
					'order_message',
					"Order #".$order->id.": New message",
					array(
						'orderId' => $order->id,
						'message' => Tools::nl2br($reply->body),
						'order_url' => Configuration::get('SITE_DOMAIN', $order->site_id) . '/dashboard/order/' . $order->id,
						'reply_url' => Configuration::get('SITE_DOMAIN', $order->site_id) . '/dashboard/order/' . $order->id . '/messages',
					),
					$receiver_mail,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					null,
					PROX_ADMIN_SITE_ID
				);	
			}
		} catch( \Exception $e ) {
			
		}

		return $app->response([
			"success" => true,
			"errors" => null,
			"newMessageId" => (int) $reply->id,
		]);
    }

    public function getOrderMessages( $orderId ) {
        $app = $this->app;
        $payload = $app->request->get();
        $user = $app->container->user;
        
        $startFrom = (int) ArrayUtils::get($payload, 'startFrom', null);

		$order = new Order( (int) $orderId );
		if (!Validate::isLoadedObject($order)) {
			return $app->response([
				"success" => false,
			]);
        }

        $sql = new DbQuery();
        $sql->select('m.message_id');
        $sql->from('order_message', 'm');
        $sql->where('m.`order_id` = ' . (int) $order->id );
        $sql->where(' (m.`from_department` = ' . (int) DEPARTMENT_CUSTOMER . ') OR ( m.`to_department` = ' . (int) DEPARTMENT_CUSTOMER . ' AND is_verified = 1 ) ');

        $messages_sql = clone $sql;
        $show_more_sql = clone $sql;

        $messages = array();
        if( $startFrom ) {
            $messages_sql->where('m.`message_id` < ' . (int) $startFrom );
        }
        $messages_sql->orderBy('m.message_id DESC');
        $messages_sql->limit( Configuration::get('MAX_RESULTS', null, 10) );
        $result = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS( $messages_sql );

        foreach ($result as $message) {
        	$message = new OrderMessage( (int) $message['message_id'] );
            if(Validate::isLoadedObject($message)) {
                $messages[] = $message->present();
            }
        } 

        $showMoreButton = false;
        $lastMessageArray = $messages;
		$lastMessage = ArrayUtils::pop($lastMessageArray);
		if ($lastMessage) {
            $show_more_sql->where('m.`message_id` < ' . (int) $lastMessage['id'] );
            $show_more_sql->orderBy('message_id DESC');

            $showMoreButton = (bool) Db::getInstance(PROX_USE_SQL_SLAVE)->getValue( $show_more_sql );
		}

		return $app->response([
			"success" => true,
			"orderId" => $order->id,
			"messages" => $messages,
			"showMoreButton" => $showMoreButton,
			"startFrom" => $startFrom
		]);
    }

    public function getMessageReplies( $orderId, $messageId ) {
        $app = $this->app;
		$payload = $app->request->post();

		$order = new Order( (int) $orderId );
		if (!Validate::isLoadedObject($order)) {
			return $app->response([
				"success" => false,
			]);
		}

		$message = new OrderMessage( (int) $messageId);
		if ( !Validate::isLoadedObject($message) ) {
			return $app->response([
				"success" => false,
			]);
        }

        $replies = array();

        $sql = new DbQuery();
        $sql->select('m.message_id');
        $sql->from('order_message', 'm');
        $sql->where('m.`order_id` = ' . (int) $order->id );
        $sql->where('m.`parent_id` = ' . (int) $message->id );
        $sql->where(' (m.`from_department` = ' . (int) DEPARTMENT_CUSTOMER . ') OR ( m.`to_department` = ' . (int) DEPARTMENT_CUSTOMER . ' AND is_verified = 1 ) ');
        $sql->orderBy('m.message_id DESC');
        $result = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS( $sql );
 	
        foreach ($result as $reply) { 
            $reply = new OrderMessage( (int) $reply['message_id']);
			if ( Validate::isLoadedObject($reply) ) {
				$replies[] = $reply->present();
			}
        }

		return $app->response([
			"success" => true,
			"data" => $replies
		]);
    }

    public function markAllMessagesAsRead( $orderId ) {
        $app = $this->app;
		$payload = $app->request->post();
		$user = $app->container->user;

		$order = new Order( (int) $orderId );
		if (!Validate::isLoadedObject($order)) {
			return $app->response([
				"success" => false,
			]);
		}

		$sql = new DbQuery();
		$sql->select('m.message_id');
		$sql->from('order_message', 'm');
		$sql->where('m.`order_id` = ' . (int) $order->id);
		$sql->where('m.`to_department` = ' . (int) DEPARTMENT_CUSTOMER . ' AND is_verified = 1');

		$messages = Db::getInstance(PROX_USE_SQL_SLAVE)->executeS($sql);

		foreach ( $messages as $message ) {
			$message = new OrderMessage( (int) $message['message_id']);
			if ( Validate::isLoadedObject($message) ) {
				$message->is_viewed = true;
				$message->update();
			}
		}
    }

    public function updateMessage( $orderId, $messageId ) {
        $app = $this->app;
		$payload = $app->request->post();
		$user = $app->container->user;

		$order = new Order( (int) $orderId );
		if (!Validate::isLoadedObject($order)) {
			return $app->response([
				"success" => false,
			]);
		}

		$message = new OrderMessage( (int) $messageId);
		if ( Validate::isLoadedObject($message) ) {
			$viewed =  ($user->id != $message->sender_id && ArrayUtils::has($payload, 'isViewed')) ? true : false;
			$deleted = ArrayUtils::has($payload, 'isDeleted') ? true : false;

			$message->is_viewed = $viewed;
			$message->is_deleted = $deleted;
			$message->update();
		}

		return $app->response([
			"success" => true
		]);
    }
}