modules/CDev/FileAttachments/src/Model/Product/Attachment.php line 103

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 CDev\FileAttachments\Model\Product;
  7. use ApiPlatform\Core\Annotation as ApiPlatform;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use CDev\FileAttachments\API\Endpoint\ProductAttachment\DTO\ProductAttachmentInput as Input;
  10. use CDev\FileAttachments\API\Endpoint\ProductAttachment\DTO\ProductAttachmentUpdateInput as Update;
  11. use CDev\FileAttachments\API\Endpoint\ProductAttachment\DTO\ProductAttachmentOutput as Output;
  12. /**
  13.  * Product attachment
  14.  *
  15.  * @ORM\Entity
  16.  * @ORM\Table (
  17.  *     name="product_attachments",
  18.  *     indexes={
  19.  *         @ORM\Index (name="o", columns={"orderby"})
  20.  *     }
  21.  * )
  22.  * @ApiPlatform\ApiResource(
  23.  *     shortName="Product Attachment",
  24.  *     itemOperations={
  25.  *          "get"={
  26.  *              "method"="GET",
  27.  *              "path"="/products/{product_id}/attachments/{id}.{_format}",
  28.  *              "identifiers"={"product_id", "id"},
  29.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  30.  *              "input"=Input::class,
  31.  *              "output"=Output::class,
  32.  *              "openapi_context"={
  33.  *                  "parameters"={
  34.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  35.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  36.  *                  }
  37.  *              }
  38.  *          },
  39.  *          "put"={
  40.  *              "method"="PUT",
  41.  *              "path"="/products/{product_id}/attachments/{id}.{_format}",
  42.  *              "identifiers"={"product_id", "id"},
  43.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  44.  *              "input"=Update::class,
  45.  *              "output"=Output::class,
  46.  *              "openapi_context"={
  47.  *                  "parameters"={
  48.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  49.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  50.  *                  }
  51.  *              }
  52.  *          },
  53.  *          "delete"={
  54.  *              "method"="DELETE",
  55.  *              "path"="/products/{product_id}/attachments/{id}.{_format}",
  56.  *              "identifiers"={"product_id", "id"},
  57.  *              "requirements"={"product_id"="\d+", "id"="\d+"},
  58.  *              "input"=Input::class,
  59.  *              "output"=Output::class,
  60.  *              "openapi_context"={
  61.  *                  "parameters"={
  62.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}},
  63.  *                      {"name"="id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  64.  *                  }
  65.  *              }
  66.  *          }
  67.  *     },
  68.  *     collectionOperations={
  69.  *          "get"={
  70.  *              "method"="GET",
  71.  *              "path"="/products/{product_id}/attachments.{_format}",
  72.  *              "identifiers"={"product_id"},
  73.  *              "requirements"={"product_id"="\d+"},
  74.  *              "input"=Input::class,
  75.  *              "output"=Output::class,
  76.  *              "openapi_context"={
  77.  *                  "parameters"={
  78.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  79.  *                  },
  80.  *              }
  81.  *          },
  82.  *          "post"={
  83.  *              "method"="POST",
  84.  *              "path"="/products/{product_id}/attachments.{_format}",
  85.  *              "controller"="xcart.api.cdev.file_attachments.product_attachment.controller",
  86.  *              "identifiers"={"product_id"},
  87.  *              "requirements"={"product_id"="\d+"},
  88.  *              "input"=Input::class,
  89.  *              "output"=Output::class,
  90.  *              "openapi_context"={
  91.  *                  "parameters"={
  92.  *                      {"name"="product_id", "in"="path", "required"=true, "schema"={"type"="integer"}}
  93.  *                  }
  94.  *              }
  95.  *          }
  96.  *     }
  97.  * )
  98.  */
  99. class Attachment extends \XLite\Model\Base\I18n
  100. {
  101.     public const ACCESS_ANY 'A';
  102.     public const ACCESS_REGISTERED 'R';
  103.     // {{{ Collumns
  104.     /**
  105.      * @var int
  106.      *
  107.      * @ORM\Id
  108.      * @ORM\GeneratedValue (strategy="AUTO")
  109.      * @ORM\Column (type="integer", options={"unsigned": true})
  110.      */
  111.     protected $id;
  112.     /**
  113.      * @var int
  114.      *
  115.      * @ORM\Column (type="integer")
  116.      */
  117.     protected $orderby 0;
  118.     // }}}
  119.     // {{{ Associations
  120.     /**
  121.      * Relation to a product entity
  122.      *
  123.      * @var \XLite\Model\Product
  124.      *
  125.      * @ORM\ManyToOne (targetEntity="XLite\Model\Product", inversedBy="attachments")
  126.      * @ORM\JoinColumn (name="product_id", referencedColumnName="product_id", onDelete="CASCADE")
  127.      */
  128.     protected $product;
  129.     /**
  130.      * @var \CDev\FileAttachments\Model\Product\Attachment\Storage
  131.      *
  132.      * @ORM\OneToOne (targetEntity="CDev\FileAttachments\Model\Product\Attachment\Storage", mappedBy="attachment", cascade={"all"}, fetch="EAGER")
  133.      */
  134.     protected $storage;
  135.     /**
  136.      * Access - membership id or [self::ACCESS_ANY, self::ACCESS_REGISTERED]
  137.      *
  138.      * @var string
  139.      *
  140.      * @ORM\Column (type="string")
  141.      */
  142.     protected $access self::ACCESS_ANY;
  143.     /**
  144.      * @var string
  145.      *
  146.      * @ORM\Column (type="string", length=512, nullable=true)
  147.      */
  148.     protected $remoteUrl;
  149.     /**
  150.      * @var \Doctrine\Common\Collections\Collection
  151.      *
  152.      * @ORM\OneToMany (targetEntity="CDev\FileAttachments\Model\Product\AttachmentTranslation", mappedBy="owner", cascade={"all"})
  153.      */
  154.     protected $translations;
  155.     // }}}
  156.     // {{{ Getters / setters
  157.     /**
  158.      * Get storage
  159.      *
  160.      * @return \CDev\FileAttachments\Model\Product\Attachment\Storage
  161.      */
  162.     public function getStorage($method null)
  163.     {
  164.         if (!$this->storage) {
  165.             $this->setStorage(new \CDev\FileAttachments\Model\Product\Attachment\Storage());
  166.             if (isset($method)) {
  167.                 $this->storage->setStorageType($this->storage::STORAGE_URL);
  168.             }
  169.             $this->storage->setAttachment($this);
  170.         }
  171.         return $this->storage;
  172.     }
  173.     /**
  174.      * Get public title
  175.      *
  176.      * @return string
  177.      */
  178.     public function getPublicTitle()
  179.     {
  180.         return $this->getTitle() ?: $this->getStorage()->getFileName();
  181.     }
  182.     /**
  183.      * Get public url
  184.      *
  185.      * @return string
  186.      */
  187.     public function getURL()
  188.     {
  189.         return $this->getStorage()->getURL();
  190.     }
  191.     // }}}
  192.     /**
  193.      * Clone for product
  194.      *
  195.      * @param \XLite\Model\Product $product Product
  196.      *
  197.      * @return \XLite\Model\AEntity
  198.      */
  199.     public function cloneEntityForProduct(\XLite\Model\Product $product)
  200.     {
  201.         $newAttachment parent::cloneEntity();
  202.         $newAttachment->setProduct($product);
  203.         $product->addAttachments($newAttachment);
  204.         $this->getStorage()->cloneEntityForAttachment($newAttachment);
  205.         return $newAttachment;
  206.     }
  207.     /**
  208.      * Get id
  209.      *
  210.      * @return integer
  211.      */
  212.     public function getId()
  213.     {
  214.         return $this->id;
  215.     }
  216.     /**
  217.      * Set orderby
  218.      *
  219.      * @param integer $orderby
  220.      * @return Attachment
  221.      */
  222.     public function setOrderby($orderby)
  223.     {
  224.         $this->orderby $orderby;
  225.         return $this;
  226.     }
  227.     /**
  228.      * Get orderby
  229.      *
  230.      * @return integer
  231.      */
  232.     public function getOrderby()
  233.     {
  234.         return $this->orderby;
  235.     }
  236.     /**
  237.      * Set product
  238.      *
  239.      * @param \XLite\Model\Product $product
  240.      * @return Attachment
  241.      */
  242.     public function setProduct(\XLite\Model\Product $product null)
  243.     {
  244.         $this->product $product;
  245.         return $this;
  246.     }
  247.     /**
  248.      * Get product
  249.      *
  250.      * @return \XLite\Model\Product
  251.      */
  252.     public function getProduct()
  253.     {
  254.         return $this->product;
  255.     }
  256.     /**
  257.      * Set storage
  258.      *
  259.      * @param \CDev\FileAttachments\Model\Product\Attachment\Storage $storage
  260.      * @return Attachment
  261.      */
  262.     public function setStorage(\CDev\FileAttachments\Model\Product\Attachment\Storage $storage null)
  263.     {
  264.         $this->storage $storage;
  265.         return $this;
  266.     }
  267.     /**
  268.      * Return Access
  269.      *
  270.      * @return string
  271.      */
  272.     public function getAccess()
  273.     {
  274.         return !empty($this->access) ? $this->access : static::ACCESS_ANY;
  275.     }
  276.     /**
  277.      * Set Access
  278.      *
  279.      * @param string $access
  280.      *
  281.      * @return $this
  282.      */
  283.     public function setAccess($access)
  284.     {
  285.         if ($access instanceof \XLite\Model\Membership) {
  286.             $access $access->getMembershipId();
  287.         }
  288.         $this->access $access;
  289.         return $this;
  290.     }
  291.     /**
  292.      * Get attachment icon type
  293.      *
  294.      * @return string
  295.      */
  296.     public function getIconType()
  297.     {
  298.         $ext strtolower($this->getStorage()->getExtension());
  299.         if (in_array($ext\XLite\Core\Converter::getArchiveExtensions())) {
  300.             $icon 'zip';
  301.         } elseif (in_array($ext\XLite\Core\Converter::getImageExtensions())) {
  302.             $icon 'image';
  303.         } elseif (in_array($ext\XLite\Core\Converter::getPhotoshopExtensions())) {
  304.             $icon 'ps';
  305.         } elseif (in_array($ext\XLite\Core\Converter::getPresentationExtensions())) {
  306.             $icon 'powerpoint';
  307.         } elseif (in_array($ext\XLite\Core\Converter::getAudioExtensions())) {
  308.             $icon 'music';
  309.         } elseif (in_array($ext\XLite\Core\Converter::getVideoExtensions())) {
  310.             $icon 'video';
  311.         } elseif (in_array($ext, ['pdf''csv''ai''exe'])) {
  312.             $icon $ext;
  313.         } elseif (in_array($ext\XLite\Core\Converter::getDocumentExtensions())) {
  314.             $icon 'doc';
  315.         } elseif (in_array($ext\XLite\Core\Converter::getMSWordExtensions())) {
  316.             $icon 'word';
  317.         } else {
  318.             $icon $this->getStorage()->isURL() ? 'url' 'default';
  319.         }
  320.         return $icon;
  321.     }
  322.     // {{{ Translation Getters / setters
  323.     /**
  324.      * @return string
  325.      */
  326.     public function getTitle()
  327.     {
  328.         return $this->getTranslationField(__FUNCTION__);
  329.     }
  330.     /**
  331.      * @param string $title
  332.      *
  333.      * @return \XLite\Model\Base\Translation
  334.      */
  335.     public function setTitle($title)
  336.     {
  337.         return $this->setTranslationField(__FUNCTION__$title);
  338.     }
  339.     /**
  340.      * @return string
  341.      */
  342.     public function getDescription()
  343.     {
  344.         return $this->getTranslationField(__FUNCTION__);
  345.     }
  346.     /**
  347.      * @param string $description
  348.      *
  349.      * @return \XLite\Model\Base\Translation
  350.      */
  351.     public function setDescription($description)
  352.     {
  353.         return $this->setTranslationField(__FUNCTION__$description);
  354.     }
  355.     /**
  356.      * @param string $remoteUrl
  357.      *
  358.      * @return $this
  359.      */
  360.     public function setRemoteUrl($remoteUrl)
  361.     {
  362.         $this->remoteUrl $remoteUrl;
  363.         return $this;
  364.     }
  365.     /**
  366.      * @return string
  367.      */
  368.     public function getRemoteUrl()
  369.     {
  370.         return $this->remoteUrl;
  371.     }
  372.     // }}}
  373. }