modules/XC/VendorMessages/src/Model/Conversation.php line 18

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XC\VendorMessages\Model;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * Conversation
  10.  *
  11.  * @ORM\Entity
  12.  * @ORM\Table (name="conversations")
  13.  */
  14. class Conversation extends \XLite\Model\AEntity
  15. {
  16.     /**
  17.      * Unique ID
  18.      *
  19.      * @var integer
  20.      *
  21.      * @ORM\Id
  22.      * @ORM\GeneratedValue (strategy="AUTO")
  23.      * @ORM\Column         (type="integer", options={ "unsigned": true })
  24.      */
  25.     protected $id;
  26.     /**
  27.      * Messages
  28.      *
  29.      * @var \Doctrine\Common\Collections\Collection
  30.      *
  31.      * @ORM\OneToMany (targetEntity="XC\VendorMessages\Model\Message", mappedBy="conversation", cascade={"all"})
  32.      * @ORM\OrderBy   ({"date" = "ASC"})
  33.      */
  34.     protected $messages;
  35.     /**
  36.      * Order
  37.      *
  38.      * @var \XLite\Model\Order
  39.      *
  40.      * @ORM\OneToOne (targetEntity="XLite\Model\Order", inversedBy="conversation")
  41.      * @ORM\JoinColumn (name="order_id", referencedColumnName="order_id", onDelete="CASCADE")
  42.      */
  43.     protected $order;
  44.     /**
  45.      * Conversation members
  46.      *
  47.      * @var \Doctrine\Common\Collections\ArrayCollection
  48.      *
  49.      * @ORM\ManyToMany (targetEntity="XLite\Model\Profile", inversedBy="conversations")
  50.      * @ORM\JoinTable (name="conversation_members",
  51.      *      joinColumns={@ORM\JoinColumn (name="conversation_id", referencedColumnName="id", onDelete="CASCADE")},
  52.      *      inverseJoinColumns={@ORM\JoinColumn (name="profile_id", referencedColumnName="profile_id", onDelete="CASCADE")}
  53.      * )
  54.      */
  55.     protected $members;
  56.     /**
  57.      * @inheritdoc
  58.      */
  59.     public function __construct(array $data = [])
  60.     {
  61.         parent::__construct($data);
  62.         $this->messages = new \Doctrine\Common\Collections\ArrayCollection();
  63.         $this->members = new \Doctrine\Common\Collections\ArrayCollection();
  64.     }
  65.     /**
  66.      * Return Id
  67.      *
  68.      * @return int
  69.      */
  70.     public function getId()
  71.     {
  72.         return $this->id;
  73.     }
  74.     /**
  75.      * Get messages
  76.      *
  77.      * @return \Doctrine\Common\Collections\Collection
  78.      */
  79.     public function getMessages()
  80.     {
  81.         return $this->messages;
  82.     }
  83.     /**
  84.      * Set messages
  85.      *
  86.      * @param \Doctrine\Common\Collections\Collection $messages Messages
  87.      *
  88.      * @return static
  89.      */
  90.     public function setMessages($messages)
  91.     {
  92.         $this->messages $messages;
  93.         return $this;
  94.     }
  95.     /**
  96.      * Set messages
  97.      *
  98.      * @param \XC\VendorMessages\Model\Message $message Message
  99.      *
  100.      * @return $this
  101.      */
  102.     public function addMessage($message)
  103.     {
  104.         $this->messages->add($message);
  105.         return $this;
  106.     }
  107.     /**
  108.      * Return Members
  109.      *
  110.      * @return \Doctrine\Common\Collections\ArrayCollection
  111.      */
  112.     public function getMembers()
  113.     {
  114.         return $this->members;
  115.     }
  116.     /**
  117.      * Set Members
  118.      *
  119.      * @param mixed $members
  120.      *
  121.      * @return $this
  122.      */
  123.     public function setMembers($members)
  124.     {
  125.         $this->members $members;
  126.         return $this;
  127.     }
  128.     /**
  129.      * Set Members
  130.      *
  131.      * @param \XLite\Model\Profile $member
  132.      *
  133.      * @return $this
  134.      */
  135.     public function addMember($member)
  136.     {
  137.         if (!$this->members->contains($member)) {
  138.             $this->members->add($member);
  139.         }
  140.         return $this;
  141.     }
  142.     /**
  143.      * Return Order
  144.      *
  145.      * @return \XLite\Model\Order
  146.      */
  147.     public function getOrder()
  148.     {
  149.         return $this->order;
  150.     }
  151.     /**
  152.      * Set Order
  153.      *
  154.      * @param \XLite\Model\Order $order
  155.      *
  156.      * @return $this
  157.      */
  158.     public function setOrder($order)
  159.     {
  160.         $this->order $order;
  161.         return $this;
  162.     }
  163.     /**
  164.      * Build new message
  165.      *
  166.      * @param \XLite\Model\Profile $author
  167.      * @param string               $body
  168.      *
  169.      * @return \XC\VendorMessages\Model\Message
  170.      */
  171.     public function buildNewMessage($author$body)
  172.     {
  173.         $message = new \XC\VendorMessages\Model\Message();
  174.         $message->setConversation($this);
  175.         $message->setBody($body);
  176.         $this->addMessage($message);
  177.         $message->setAuthor($author);
  178.         $message->markAsRead($author);
  179.         return $message;
  180.     }
  181.     /**
  182.      * Count unread messages
  183.      *
  184.      * @param \XLite\Model\Profile $profile Profile OPTIONAL
  185.      *
  186.      * @return integer
  187.      */
  188.     public function countUnreadMessages(\XLite\Model\Profile $profile null)
  189.     {
  190.         $profile $profile ?: \XLite\Core\Auth::getInstance()->getProfile();
  191.         $count 0;
  192.         foreach ($this->getMessages() as $message) {
  193.             if (!$message->isRead($profile)) {
  194.                 $count++;
  195.             }
  196.         }
  197.         return $count;
  198.     }
  199.     /**
  200.      * Get last message
  201.      *
  202.      * @return \XC\VendorMessages\Model\Message
  203.      */
  204.     public function getLastMessage()
  205.     {
  206.         return count($this->getMessages()) > $this->getMessages()->last() : null;
  207.     }
  208.     /**
  209.      * Return conversation name
  210.      *
  211.      * @param \XLite\Model\Profile $profile
  212.      *
  213.      * @return string
  214.      */
  215.     public function getName($profile null)
  216.     {
  217.         if ($order $this->getOrder()) {
  218.             $orderNumber $order->getOrderNumber();
  219.             if (!$orderNumber && \XC\VendorMessages\Main::isMultivendor() && $order->isChild()) {
  220.                 $orderNumber $order->getParent()->getOrderNumber();
  221.                 if (
  222.                     (!$profile || $order->getVendor()->getProfileId() !== $profile->getProfileId())
  223.                     && $order->getParent()->getChildren()->count() > 1
  224.                 ) {
  225.                     return static::t('Order X Customer', ['id' => ($orderNumber ' - ' . ($order->getNameForMessages()))]);
  226.                 }
  227.             }
  228.             return static::t('Order X Customer', ['id' => $orderNumber]);
  229.         }
  230.         $profile $profile ?? \Xlite\Core\Auth::getInstance()->getProfile();
  231.         $names array_filter(array_map(static function ($member) use ($profile) {
  232.             return $profile->getProfileId() !== $member->getProfileId()
  233.                 ? $member->getNameForMessages()
  234.                 : null;
  235.         }, $this->getMembers()->toArray()));
  236.         return static::t('Conversation: X', [
  237.             'members' => implode(', 'array_unique($names))
  238.         ]);
  239.     }
  240.     /**
  241.      * Check if user is member of conversation
  242.      *
  243.      * @param $profile
  244.      *
  245.      * @return bool
  246.      */
  247.     public function isMember($profile)
  248.     {
  249.         return $profile && $this->getMembers()->contains($profile);
  250.     }
  251.     /**
  252.      * Is user has access to conversation(non-order conversations)
  253.      *
  254.      * @param null $profile
  255.      *
  256.      * @return bool
  257.      */
  258.     public function checkAccess($profile null)
  259.     {
  260.         $auth \XLite\Core\Auth::getInstance();
  261.         $result false;
  262.         $profile $profile ?: $auth->getProfile();
  263.         if ($profile && !$this->getOrder()) {
  264.             $result $auth->isPermissionAllowed('ROLE_MANAGE_CONVERSATIONS') || $this->isMember($profile);
  265.         }
  266.         return $result;
  267.     }
  268. }