|
| 1 | +# Document Module for Magento 2 |
| 2 | + |
| 3 | +[](https://packagist.org/packages/opengento/module-document) |
| 4 | +[](./LICENSE) |
| 5 | +[](https://packagist.org/packages/opengento/module-document/stats) |
| 6 | +[](https://packagist.org/packages/opengento/module-document/stats) |
| 7 | + |
| 8 | +This module add the many countries to many stores relation and make it available to the storefront. |
| 9 | + |
| 10 | + - [Setup](#setup) |
| 11 | + - [Composer installation](#composer-installation) |
| 12 | + - [Setup the module](#setup-the-module) |
| 13 | + - [Features](#features) |
| 14 | + - [Settings](#settings) |
| 15 | + - [Documentation](#documentation) |
| 16 | + - [Support](#support) |
| 17 | + - [Authors](#authors) |
| 18 | + - [License](#license) |
| 19 | + |
| 20 | +## Setup |
| 21 | + |
| 22 | +Magento 2 Open Source or Commerce edition is required. |
| 23 | + |
| 24 | +### Composer installation |
| 25 | + |
| 26 | +Run the following composer command: |
| 27 | + |
| 28 | +``` |
| 29 | +composer require opengento/module-document |
| 30 | +``` |
| 31 | + |
| 32 | +### Setup the module |
| 33 | + |
| 34 | +Run the following magento command: |
| 35 | + |
| 36 | +``` |
| 37 | +bin/magento setup:upgrade |
| 38 | +``` |
| 39 | + |
| 40 | +**If you are in production mode, do not forget to recompile and redeploy the static resources.** |
| 41 | + |
| 42 | +## Features |
| 43 | + |
| 44 | +This module aims to help merchants to manage easily their documents in Magento 2. |
| 45 | +Documents are sorted by types and can be manipulated with ease. |
| 46 | + |
| 47 | +- Declare new document types: |
| 48 | + - from the back-office |
| 49 | + - from xml config files |
| 50 | + |
| 51 | +- Create new documents: |
| 52 | + - from the back-office |
| 53 | + - from command line |
| 54 | + - from a cron job |
| 55 | + |
| 56 | +Documents can be uploaded with a thumbnail. The default thumbnail of the document type can be used. |
| 57 | +The thumbnail can be resized in order to optimize the performance. |
| 58 | + |
| 59 | +## Documentation |
| 60 | + |
| 61 | +### How to declare a document type from config file |
| 62 | + |
| 63 | +Create a new file `resource_document_types.xml` in the `etc/` folder of your module: |
| 64 | + |
| 65 | +```xml |
| 66 | +<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:mpbio:document:etc/resource_document_types.xsd"> |
| 67 | + <documentType code="cert"> |
| 68 | + <scheduledImport>true</scheduledImport> |
| 69 | + <visibility>public</visibility> |
| 70 | + <name>Certificates</name> |
| 71 | + <fileSourcePath>cert/import/</fileSourcePath> |
| 72 | + <fileDestPath>coa/</fileDestPath> |
| 73 | + <subPathLength>3</subPathLength> |
| 74 | + <filePattern>CERT-*.[pP][dD][fF]</filePattern> |
| 75 | + <fileAllowedExtensions> |
| 76 | + <extension>pdf</extension> |
| 77 | + </fileAllowedExtensions> |
| 78 | + <defaultImageFileName>document/image/cert/thumbnail.png</defaultImageFileName> |
| 79 | + </documentType> |
| 80 | +</config> |
| 81 | +``` |
| 82 | + |
| 83 | +### How to add a new import file processor |
| 84 | + |
| 85 | +When files are import from command line or cron jobs, you cannot set manually metadata. |
| 86 | +The code, name, and pivot field has to be filled and that is what the file import processor do. |
| 87 | +If you need to implement your own logic on how a document should be created on the import fly, check the following code: |
| 88 | + |
| 89 | +Implement the interface `\Opengento\Document\Model\Document\ProcessorInterface`: |
| 90 | + |
| 91 | +```php |
| 92 | + |
| 93 | +namespace Vendor\Module\Model\Document\Processor; |
| 94 | + |
| 95 | +use Opengento\Document\Api\Data\DocumentInterface; |
| 96 | +use Opengento\Document\Api\Data\DocumentTypeInterface; |
| 97 | +use Opengento\Document\Model\Document\Helper\File; |
| 98 | +use Opengento\Document\Model\Document\Helper\Format; |
| 99 | +use Opengento\Document\Model\Document\ProcessorInterface; |
| 100 | +use Opengento\Document\Model\DocumentBuilder; |
| 101 | +use function basename; |
| 102 | +use function dirname; |
| 103 | + |
| 104 | +final class CustomProcessor implements ProcessorInterface |
| 105 | +{ |
| 106 | + public const CODE = 'custom'; |
| 107 | + |
| 108 | + /** |
| 109 | + * @var DocumentBuilder |
| 110 | + */ |
| 111 | + private $documentBuilder; |
| 112 | + |
| 113 | + /** |
| 114 | + * @var File |
| 115 | + */ |
| 116 | + private $fileHelper; |
| 117 | + |
| 118 | + public function __construct( |
| 119 | + DocumentBuilder $documentBuilder, |
| 120 | + File $fileHelper |
| 121 | + ) { |
| 122 | + $this->documentBuilder = $documentBuilder; |
| 123 | + $this->fileHelper = $fileHelper; |
| 124 | + } |
| 125 | + |
| 126 | + public function execute(DocumentTypeInterface $documentType, string $filePath): DocumentInterface |
| 127 | + { |
| 128 | + // $filePath is the path where the file is being moved |
| 129 | + // You can change the destination path if you edit the file path value |
| 130 | + // with $this->documentBuilder->setFilePath($newDestPath) |
| 131 | + // and $this->documentBuilder->setFileName($newFileName) |
| 132 | + |
| 133 | + $fileName = basename($filePath); |
| 134 | + $this->documentBuilder->setTypeId($documentType->getId()); |
| 135 | + $this->documentBuilder->setCode(Format::formatCode($fileName)); |
| 136 | + $this->documentBuilder->setName(Format::formatName($fileName)); |
| 137 | + $this->documentBuilder->setFileName($fileName); |
| 138 | + $this->documentBuilder->setFilePath(dirname($this->fileHelper->getRelativeFilePath($filePath))); |
| 139 | + |
| 140 | + return $this->documentBuilder->create(); |
| 141 | + } |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +## Support |
| 146 | + |
| 147 | +Raise a new [request](https://github.com/opengento/magento2-document/issues) to the issue tracker. |
| 148 | + |
| 149 | +## Authors |
| 150 | + |
| 151 | +- **Opengento Community** - *Lead* - [](https://twitter.com/opengento) |
| 152 | +- **Thomas Klein** - *Maintainer* - [](https://github.com/thomas-kl1) |
| 153 | +- **Contributors** - *Contributor* - [](https://github.com/opengento/magento2-document/graphs/contributors) |
| 154 | + |
| 155 | +## License |
| 156 | + |
| 157 | +This project is licensed under the MIT License - see the [LICENSE](./LICENSE) details. |
| 158 | + |
| 159 | +***That's all folks!*** |
0 commit comments