modules/XC/Reviews/src/Model/Review.php line 94

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\Reviews\Model;
  7. use ApiPlatform\Core\Annotation as ApiPlatform;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use XC\Reviews\API\Endpoint\ProductReview\DTO\ProductReviewInput as Input;
  10. use XC\Reviews\API\Endpoint\ProductReview\DTO\ProductReviewOutput as Output;
  11. /**
  12.  * The "review" model class
  13.  *
  14.  * @ORM\Entity
  15.  * @ORM\Table  (name="reviews",
  16.  *      indexes={
  17.  *          @ORM\Index (name="additionDate", columns={"additionDate"}),
  18.  *          @ORM\Index (name="status", columns={"status"}),
  19.  *      }
  20.  * )
  21.  * @ApiPlatform\ApiResource(
  22.  *     shortName="Product Review",
  23.  *     input=Input::class,
  24.  *     output=Output::class,
  25.  *     itemOperations={
  26.  *          "get"={
  27.  *              "method"="GET",
  28.  *              "path"="/products/{product_id}/reviews/{id}.{_format}",
  29.  *              "identifiers"={"product_id", "id"},
  30.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  31.  *              "openapi_context"={
  32.  *                  "parameters"={
  33.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  34.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  35.  *                  }
  36.  *              }
  37.  *          },
  38.  *          "put"={
  39.  *              "method"="PUT",
  40.  *              "path"="/products/{product_id}/reviews/{id}.{_format}",
  41.  *              "identifiers"={"product_id", "id"},
  42.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  43.  *              "openapi_context"={
  44.  *                  "parameters"={
  45.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  46.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  47.  *                  }
  48.  *              }
  49.  *          },
  50.  *          "delete"={
  51.  *              "method"="DELETE",
  52.  *              "path"="/products/{product_id}/reviews/{id}.{_format}",
  53.  *              "identifiers"={"product_id", "id"},
  54.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  55.  *              "openapi_context"={
  56.  *                  "parameters"={
  57.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  58.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  59.  *                  }
  60.  *              }
  61.  *          }
  62.  *     },
  63.  *     collectionOperations={
  64.  *          "get"={
  65.  *              "method"="GET",
  66.  *              "path"="/products/{product_id}/reviews.{_format}",
  67.  *              "identifiers"={"product_id"},
  68.  *              "requirements"={"product_id"="\d+"},
  69.  *              "openapi_context"={
  70.  *                  "parameters"={
  71.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  72.  *                  },
  73.  *              }
  74.  *          },
  75.  *          "post"={
  76.  *              "method"="POST",
  77.  *              "path"="/products/{product_id}/reviews.{_format}",
  78.  *              "controller"="xcart.api.xc.reviews.product_review.controller",
  79.  *              "identifiers"={"product_id"},
  80.  *              "requirements"={"product_id"="\d+"},
  81.  *              "openapi_context"={
  82.  *                  "parameters"={
  83.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  84.  *                  }
  85.  *              }
  86.  *          }
  87.  *     }
  88.  * )
  89.  */
  90. class Review extends \XLite\Model\AEntity
  91. {
  92.     public const STATUS_APPROVED               1;
  93.     public const STATUS_PENDING                0;
  94.     public const MAX_RATING                    5;
  95.     public const REGISTERED_CUSTOMERS          'R';
  96.     public const PURCHASED_CUSTOMERS           'P';
  97.     /**
  98.      * Review Unique ID
  99.      *
  100.      * @var integer
  101.      *
  102.      * @ORM\Id
  103.      * @ORM\GeneratedValue (strategy="AUTO")
  104.      * @ORM\Column         (type="integer", options={ "unsigned": true })
  105.      */
  106.     protected $id;
  107.     /**
  108.      * Review text
  109.      *
  110.      * @var string
  111.      *
  112.      * @ORM\Column (type="text")
  113.      */
  114.     protected $review '';
  115.     /**
  116.      * Response text
  117.      *
  118.      * @var string
  119.      *
  120.      * @ORM\Column (type="text", nullable=true)
  121.      */
  122.     protected $response '';
  123.     /**
  124.      * Review rating
  125.      *
  126.      * @var integer
  127.      *
  128.      * @ORM\Column (type="smallint")
  129.      */
  130.     protected $rating self::MAX_RATING;
  131.     /**
  132.      * Addition date (UNIX timestamp)
  133.      *
  134.      * @var integer
  135.      *
  136.      * @ORM\Column (type="integer")
  137.      */
  138.     protected $additionDate;
  139.     /**
  140.      * Respond date (UNIX timestamp)
  141.      *
  142.      * @var integer
  143.      *
  144.      * @ORM\Column (type="integer", nullable=true)
  145.      */
  146.     protected $responseDate;
  147.     /**
  148.      * Relation to a profile entity (who adds review)
  149.      *
  150.      * @var \XLite\Model\Profile
  151.      *
  152.      * @ORM\ManyToOne  (targetEntity="XLite\Model\Profile")
  153.      * @ORM\JoinColumn (name="profile_id", referencedColumnName="profile_id", onDelete="SET NULL")
  154.      */
  155.     protected $profile;
  156.     /**
  157.      * Respondent profile
  158.      *
  159.      * @var \XLite\Model\Profile
  160.      *
  161.      * @ORM\ManyToOne  (targetEntity="XLite\Model\Profile")
  162.      * @ORM\JoinColumn (name="respondent_id", referencedColumnName="profile_id", onDelete="SET NULL")
  163.      */
  164.     protected $respondent;
  165.     /**
  166.      * Reviewer name
  167.      *
  168.      * @var string
  169.      *
  170.      * @ORM\Column (type="string")
  171.      */
  172.     protected $reviewerName '';
  173.     /**
  174.      * Review status
  175.      *
  176.      * @var integer
  177.      *
  178.      * @ORM\Column (type="smallint")
  179.      */
  180.     protected $status self::STATUS_PENDING;
  181.     /**
  182.      * Relation to a product entity
  183.      *
  184.      * @var \XLite\Model\Product
  185.      *
  186.      * @ORM\ManyToOne  (targetEntity="XLite\Model\Product", inversedBy="reviews")
  187.      * @ORM\JoinColumn (name="product_id", referencedColumnName="product_id", onDelete="CASCADE")
  188.      */
  189.     protected $product;
  190.     /**
  191.      * Use for meta flag
  192.      *
  193.      * @var boolean
  194.      *
  195.      * @ORM\Column(type="boolean")
  196.      */
  197.     protected $useForMeta false;
  198.     /**
  199.      * Flag: New review (flag has reset after admin view the review in the list)
  200.      *
  201.      * @var boolean
  202.      *
  203.      * @ORM\Column(type="boolean")
  204.      */
  205.     protected $isNew true;
  206.     /**
  207.      * Flag to exporting entities
  208.      *
  209.      * @var boolean
  210.      *
  211.      * @ORM\Column (type="boolean")
  212.      */
  213.     protected $xcPendingExport false;
  214.     /**
  215.      * Related order review key
  216.      *
  217.      * @var \XC\Reviews\Model\OrderReviewKey
  218.      *
  219.      * @ORM\ManyToOne (targetEntity="XC\Reviews\Model\OrderReviewKey", inversedBy="reviews", fetch="LAZY")
  220.      * @ORM\JoinColumn (name="rkey_id", referencedColumnName="id", onDelete="SET NULL")
  221.      */
  222.     protected $reviewKey;
  223.     /**
  224.      * Define if review is new
  225.      *
  226.      * @return boolean
  227.      */
  228.     public function isNew()
  229.     {
  230.         return !$this->isPersistent();
  231.     }
  232.     /**
  233.      * Define if review is approved
  234.      *
  235.      * @return boolean
  236.      */
  237.     public function isApproved()
  238.     {
  239.         return $this->getStatus() == static::STATUS_APPROVED;
  240.     }
  241.     /**
  242.      * Define if review is not approved
  243.      *
  244.      * @return boolean
  245.      */
  246.     public function isNotApproved()
  247.     {
  248.         return !$this->isApproved() && !$this->isNew();
  249.     }
  250.     /**
  251.      * Returns meta description
  252.      *
  253.      * @return string
  254.      */
  255.     public function getMetaDescription()
  256.     {
  257.         $data = [
  258.             'rating'       => $this->getProduct()->getAverageRating(),
  259.             'maxRating'    => static::MAX_RATING,
  260.             'reviewerName' => $this->getReviewerName(),
  261.             'review'       => $this->getReview(),
  262.         ];
  263.         return \XLite::t('reviewMetaDescription'$data)->translate();
  264.     }
  265.     /**
  266.      * @return string
  267.      */
  268.     public function getURLForProductAdminPage()
  269.     {
  270.         return $this->getProduct()
  271.             ? \XLite\Core\Converter::makeURLValid(
  272.                 \XLite\Core\Converter::buildFullURL('product''', [
  273.                     'product_id'    => $this->getProduct()->getProductId(),
  274.                     'page'          => 'product_reviews'
  275.                 ], \XLite::getAdminScript())
  276.             )
  277.             : '';
  278.     }
  279.     /**
  280.      * Send email notification to owner
  281.      *
  282.      * @return string
  283.      */
  284.     public function sendNotificationToOwner()
  285.     {
  286.         return \XLite\Core\Mailer::sendNewReview($this);
  287.     }
  288.     /**
  289.      * Returns code for useForMeta selector
  290.      *
  291.      * @return integer
  292.      */
  293.     public function getCode()
  294.     {
  295.         return $this->getId();
  296.     }
  297.     /**
  298.      * Set rating
  299.      *
  300.      * @param integer $rating
  301.      *
  302.      * @return $this
  303.      */
  304.     public function setRating($rating)
  305.     {
  306.         $this->rating max(min($rating, static::MAX_RATING), 1);
  307.         return $this;
  308.     }
  309.     /**
  310.      * Map data to entity columns
  311.      *
  312.      * @param array $data Array of data
  313.      *
  314.      * @return $this
  315.      */
  316.     public function map(array $data)
  317.     {
  318.         $reviewData = [];
  319.         foreach ($data as $key => $value) {
  320.             if ($this->isPropertyExists($key)) {
  321.                 $reviewData[$key] = $data[$key];
  322.             }
  323.         }
  324.         return parent::map($reviewData);
  325.     }
  326.     /**
  327.      * Get id
  328.      *
  329.      * @return integer
  330.      */
  331.     public function getId()
  332.     {
  333.         return $this->id;
  334.     }
  335.     /**
  336.      * Get review
  337.      *
  338.      * @return string
  339.      */
  340.     public function getReview()
  341.     {
  342.         return $this->review;
  343.     }
  344.     /**
  345.      * Set review
  346.      *
  347.      * @param string $review
  348.      * @return Review
  349.      */
  350.     public function setReview($review)
  351.     {
  352.         $this->review $review;
  353.         return $this;
  354.     }
  355.     /**
  356.      * Return Response
  357.      *
  358.      * @return string
  359.      */
  360.     public function getResponse()
  361.     {
  362.         return (string)$this->response;
  363.     }
  364.     /**
  365.      * Set Response
  366.      *
  367.      * @param string $response
  368.      *
  369.      * @return $this
  370.      */
  371.     public function setResponse($response)
  372.     {
  373.         $this->response $response;
  374.         return $this;
  375.     }
  376.     /**
  377.      * Get rating
  378.      *
  379.      * @return int
  380.      */
  381.     public function getRating()
  382.     {
  383.         return $this->rating;
  384.     }
  385.     /**
  386.      * Set additionDate
  387.      *
  388.      * @param integer $additionDate
  389.      * @return Review
  390.      */
  391.     public function setAdditionDate($additionDate)
  392.     {
  393.         $this->additionDate $additionDate;
  394.         return $this;
  395.     }
  396.     /**
  397.      * Get additionDate
  398.      *
  399.      * @return integer
  400.      */
  401.     public function getAdditionDate()
  402.     {
  403.         return $this->additionDate;
  404.     }
  405.     /**
  406.      * Return ResponseDate
  407.      *
  408.      * @return int
  409.      */
  410.     public function getResponseDate()
  411.     {
  412.         return $this->responseDate;
  413.     }
  414.     /**
  415.      * Set ResponseDate
  416.      *
  417.      * @param int $responseDate
  418.      *
  419.      * @return $this
  420.      */
  421.     public function setResponseDate($responseDate)
  422.     {
  423.         $this->responseDate $responseDate;
  424.         return $this;
  425.     }
  426.     /**
  427.      * Set reviewerName
  428.      *
  429.      * @param string $reviewerName
  430.      * @return Review
  431.      */
  432.     public function setReviewerName($reviewerName)
  433.     {
  434.         $this->reviewerName $reviewerName;
  435.         return $this;
  436.     }
  437.     /**
  438.      * Get reviewerName
  439.      *
  440.      * @return string
  441.      */
  442.     public function getReviewerName()
  443.     {
  444.         return $this->reviewerName;
  445.     }
  446.     /**
  447.      * Get email
  448.      *
  449.      * @return string
  450.      */
  451.     public function getEmail()
  452.     {
  453.         return $this->getProfile()
  454.             ? $this->getProfile()->getLogin()
  455.             : null;
  456.     }
  457.     /**
  458.      * Set status
  459.      *
  460.      * @param int $status
  461.      * @return Review
  462.      */
  463.     public function setStatus($status)
  464.     {
  465.         $this->status $status;
  466.         return $this;
  467.     }
  468.     /**
  469.      * Get status
  470.      *
  471.      * @return int
  472.      */
  473.     public function getStatus()
  474.     {
  475.         return $this->status;
  476.     }
  477.     /**
  478.      * Set useForMeta
  479.      *
  480.      * @param boolean $useForMeta
  481.      * @return Review
  482.      */
  483.     public function setUseForMeta($useForMeta)
  484.     {
  485.         $this->useForMeta $useForMeta;
  486.         return $this;
  487.     }
  488.     /**
  489.      * Get useForMeta
  490.      *
  491.      * @return boolean
  492.      */
  493.     public function getUseForMeta()
  494.     {
  495.         return $this->useForMeta;
  496.     }
  497.     /**
  498.      * Set isNew
  499.      *
  500.      * @param boolean $isNew
  501.      * @return Review
  502.      */
  503.     public function setIsNew($isNew)
  504.     {
  505.         $this->isNew $isNew;
  506.         return $this;
  507.     }
  508.     /**
  509.      * Get isNew
  510.      *
  511.      * @return boolean
  512.      */
  513.     public function getIsNew()
  514.     {
  515.         return $this->isNew;
  516.     }
  517.     /**
  518.      * Set xcPendingExport
  519.      *
  520.      * @param boolean $xcPendingExport
  521.      * @return Review
  522.      */
  523.     public function setXcPendingExport($xcPendingExport)
  524.     {
  525.         $this->xcPendingExport $xcPendingExport;
  526.         return $this;
  527.     }
  528.     /**
  529.      * Get xcPendingExport
  530.      *
  531.      * @return boolean
  532.      */
  533.     public function getXcPendingExport()
  534.     {
  535.         return $this->xcPendingExport;
  536.     }
  537.     /**
  538.      * Set profile
  539.      *
  540.      * @param \XLite\Model\Profile $profile
  541.      * @return Review
  542.      */
  543.     public function setProfile($profile)
  544.     {
  545.         $this->profile $profile;
  546.         return $this;
  547.     }
  548.     /**
  549.      * Get profile
  550.      *
  551.      * @return \XLite\Model\Profile
  552.      */
  553.     public function getProfile()
  554.     {
  555.         return $this->profile;
  556.     }
  557.     /**
  558.      * Return Respondent
  559.      *
  560.      * @return \XLite\Model\Profile
  561.      */
  562.     public function getRespondent()
  563.     {
  564.         return $this->respondent;
  565.     }
  566.     /**
  567.      * Set Respondent
  568.      *
  569.      * @param \XLite\Model\Profile $respondent
  570.      *
  571.      * @return $this
  572.      */
  573.     public function setRespondent($respondent)
  574.     {
  575.         $this->respondent $respondent;
  576.         return $this;
  577.     }
  578.     /**
  579.      * Return respondent name
  580.      *
  581.      * @return string
  582.      */
  583.     public function getRespondentName()
  584.     {
  585.         return \XLite\Core\Config::getInstance()->Company->company_name;
  586.     }
  587.     /**
  588.      * Set product
  589.      *
  590.      * @param \XLite\Model\Product $product
  591.      * @return Review
  592.      */
  593.     public function setProduct($product)
  594.     {
  595.         $this->product $product;
  596.         return $this;
  597.     }
  598.     /**
  599.      * Get product
  600.      *
  601.      * @return \XLite\Model\Product
  602.      */
  603.     public function getProduct()
  604.     {
  605.         return $this->product;
  606.     }
  607.     /**
  608.      * Get reviewKey
  609.      *
  610.      * @return \XC\Reviews\Model\OrderReviewKey
  611.      */
  612.     public function getReviewKey()
  613.     {
  614.         return $this->reviewKey;
  615.     }
  616.     /**
  617.      * Set reviewKey
  618.      *
  619.      * @param \XC\Reviews\Model\OrderReviewKey $value
  620.      * @return $this
  621.      */
  622.     public function setReviewKey($value)
  623.     {
  624.         $this->reviewKey $value;
  625.         return $this;
  626.     }
  627. }