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 \Doctrine\Common\Collections\Collection
  145.      *
  146.      * @ORM\OneToMany (targetEntity="CDev\FileAttachments\Model\Product\AttachmentTranslation", mappedBy="owner", cascade={"all"})
  147.      */
  148.     protected $translations;
  149.     // }}}
  150.     // {{{ Getters / setters
  151.     /**
  152.      * Get storage
  153.      *
  154.      * @return \CDev\FileAttachments\Model\Product\Attachment\Storage
  155.      */
  156.     public function getStorage($method null)
  157.     {
  158.         if (!$this->storage) {
  159.             $this->setStorage(new \CDev\FileAttachments\Model\Product\Attachment\Storage());
  160.             if (isset($method)) {
  161.                 $this->storage->setStorageType($this->storage::STORAGE_URL);
  162.             }
  163.             $this->storage->setAttachment($this);
  164.         }
  165.         return $this->storage;
  166.     }
  167.     /**
  168.      * Get public title
  169.      *
  170.      * @return string
  171.      */
  172.     public function getPublicTitle()
  173.     {
  174.         return $this->getTitle() ?: $this->getStorage()->getFileName();
  175.     }
  176.     /**
  177.      * Get public url
  178.      *
  179.      * @return string
  180.      */
  181.     public function getURL()
  182.     {
  183.         return $this->getStorage()->getURL();
  184.     }
  185.     // }}}
  186.     /**
  187.      * Clone for product
  188.      *
  189.      * @param \XLite\Model\Product $product Product
  190.      *
  191.      * @return \XLite\Model\AEntity
  192.      */
  193.     public function cloneEntityForProduct(\XLite\Model\Product $product)
  194.     {
  195.         $newAttachment parent::cloneEntity();
  196.         $newAttachment->setProduct($product);
  197.         $product->addAttachments($newAttachment);
  198.         $this->getStorage()->cloneEntityForAttachment($newAttachment);
  199.         return $newAttachment;
  200.     }
  201.     /**
  202.      * Get id
  203.      *
  204.      * @return integer
  205.      */
  206.     public function getId()
  207.     {
  208.         return $this->id;
  209.     }
  210.     /**
  211.      * Set orderby
  212.      *
  213.      * @param integer $orderby
  214.      * @return Attachment
  215.      */
  216.     public function setOrderby($orderby)
  217.     {
  218.         $this->orderby $orderby;
  219.         return $this;
  220.     }
  221.     /**
  222.      * Get orderby
  223.      *
  224.      * @return integer
  225.      */
  226.     public function getOrderby()
  227.     {
  228.         return $this->orderby;
  229.     }
  230.     /**
  231.      * Set product
  232.      *
  233.      * @param \XLite\Model\Product $product
  234.      * @return Attachment
  235.      */
  236.     public function setProduct(\XLite\Model\Product $product null)
  237.     {
  238.         $this->product $product;
  239.         return $this;
  240.     }
  241.     /**
  242.      * Get product
  243.      *
  244.      * @return \XLite\Model\Product
  245.      */
  246.     public function getProduct()
  247.     {
  248.         return $this->product;
  249.     }
  250.     /**
  251.      * Set storage
  252.      *
  253.      * @param \CDev\FileAttachments\Model\Product\Attachment\Storage $storage
  254.      * @return Attachment
  255.      */
  256.     public function setStorage(\CDev\FileAttachments\Model\Product\Attachment\Storage $storage null)
  257.     {
  258.         $this->storage $storage;
  259.         return $this;
  260.     }
  261.     /**
  262.      * Return Access
  263.      *
  264.      * @return string
  265.      */
  266.     public function getAccess()
  267.     {
  268.         return !empty($this->access) ? $this->access : static::ACCESS_ANY;
  269.     }
  270.     /**
  271.      * Set Access
  272.      *
  273.      * @param string $access
  274.      *
  275.      * @return $this
  276.      */
  277.     public function setAccess($access)
  278.     {
  279.         if ($access instanceof \XLite\Model\Membership) {
  280.             $access $access->getMembershipId();
  281.         }
  282.         $this->access $access;
  283.         return $this;
  284.     }
  285.     /**
  286.      * Get attachment icon type
  287.      *
  288.      * @return string
  289.      */
  290.     public function getIconType()
  291.     {
  292.         $ext strtolower($this->getStorage()->getExtension());
  293.         if (in_array($ext\XLite\Core\Converter::getArchiveExtensions())) {
  294.             $icon 'zip';
  295.         } elseif (in_array($ext\XLite\Core\Converter::getImageExtensions())) {
  296.             $icon 'image';
  297.         } elseif (in_array($ext\XLite\Core\Converter::getPhotoshopExtensions())) {
  298.             $icon 'ps';
  299.         } elseif (in_array($ext\XLite\Core\Converter::getPresentationExtensions())) {
  300.             $icon 'powerpoint';
  301.         } elseif (in_array($ext\XLite\Core\Converter::getAudioExtensions())) {
  302.             $icon 'music';
  303.         } elseif (in_array($ext\XLite\Core\Converter::getVideoExtensions())) {
  304.             $icon 'video';
  305.         } elseif (in_array($ext, ['pdf''csv''ai''exe'])) {
  306.             $icon $ext;
  307.         } elseif (in_array($ext\XLite\Core\Converter::getDocumentExtensions())) {
  308.             $icon 'doc';
  309.         } elseif (in_array($ext\XLite\Core\Converter::getMSWordExtensions())) {
  310.             $icon 'word';
  311.         } else {
  312.             $icon $this->getStorage()->isURL() ? 'url' 'default';
  313.         }
  314.         return $icon;
  315.     }
  316.     // {{{ Translation Getters / setters
  317.     /**
  318.      * @return string
  319.      */
  320.     public function getTitle()
  321.     {
  322.         return $this->getTranslationField(__FUNCTION__);
  323.     }
  324.     /**
  325.      * @param string $title
  326.      *
  327.      * @return \XLite\Model\Base\Translation
  328.      */
  329.     public function setTitle($title)
  330.     {
  331.         return $this->setTranslationField(__FUNCTION__$title);
  332.     }
  333.     /**
  334.      * @return string
  335.      */
  336.     public function getDescription()
  337.     {
  338.         return $this->getTranslationField(__FUNCTION__);
  339.     }
  340.     /**
  341.      * @param string $description
  342.      *
  343.      * @return \XLite\Model\Base\Translation
  344.      */
  345.     public function setDescription($description)
  346.     {
  347.         return $this->setTranslationField(__FUNCTION__$description);
  348.     }
  349.     // }}}
  350. }