SetupData.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Setup;
  6. use Magento\Catalog\Model\Product;
  7. use Magento\Catalog\Model\Product\Type;
  8. use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
  9. use Magento\Eav\Setup\EavSetup;
  10. use Magento\Eav\Setup\EavSetupFactory;
  11. use Magento\Email\Model\ResourceModel\Template as TemplateResource;
  12. use Magento\Email\Model\TemplateFactory;
  13. use Magento\Framework\App\TemplateTypesInterface;
  14. use Magento\Framework\Filesystem\Driver\File as Filesystem;
  15. use Magento\Framework\Module\Dir\Reader;
  16. use Magento\Framework\Setup\ModuleDataSetupInterface;
  17. /**
  18. * Data setup for use during installation / upgrade
  19. *
  20. * @package Temando\Shipping\Setup
  21. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  22. * @author Benjamin Heuer <benjamin.heuer@netresearch.de>
  23. * @author Sebastian Ertner <sebastian.ertner@netresearch.de>
  24. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. * @link https://www.temando.com/
  26. */
  27. class SetupData
  28. {
  29. const ATTRIBUTE_CODE_LENGTH = 'ts_dimensions_length';
  30. const ATTRIBUTE_CODE_WIDTH = 'ts_dimensions_width';
  31. const ATTRIBUTE_CODE_HEIGHT = 'ts_dimensions_height';
  32. const PICKUP_ORDER_TEMPLATE = 'order_pickup_new.html';
  33. const PICKUP_ORDER_GUEST_TEMPLATE = 'order_pickup_new_guest.html';
  34. /**
  35. * @var EavSetupFactory
  36. */
  37. private $eavSetupFactory;
  38. /**
  39. * Template factory
  40. *
  41. * @var TemplateFactory
  42. */
  43. private $templateFactory;
  44. /**
  45. * @var TemplateResource
  46. */
  47. private $templateResource;
  48. /**
  49. * @var Reader
  50. */
  51. private $moduleReader;
  52. /**
  53. * @var Filesystem
  54. */
  55. private $fileSystemDriver;
  56. /**
  57. * SetupData constructor.
  58. * @param EavSetupFactory $eavSetupFactory
  59. * @param TemplateFactory $templateFactory
  60. * @param TemplateResource $templateResource
  61. * @param Reader $moduleReader
  62. * @param Filesystem $fileSystemDriver
  63. */
  64. public function __construct(
  65. EavSetupFactory $eavSetupFactory,
  66. TemplateFactory $templateFactory,
  67. TemplateResource $templateResource,
  68. Reader $moduleReader,
  69. Filesystem $fileSystemDriver
  70. ) {
  71. $this->eavSetupFactory = $eavSetupFactory;
  72. $this->templateFactory = $templateFactory;
  73. $this->templateResource = $templateResource;
  74. $this->moduleReader = $moduleReader;
  75. $this->fileSystemDriver = $fileSystemDriver;
  76. }
  77. /**
  78. * Add dimension attributes. Need to be editable on store level due to the
  79. * weight unit (that dimensions unit is derived from) is configurable on
  80. * store level.
  81. *
  82. * @param ModuleDataSetupInterface $setup
  83. * @return void
  84. */
  85. public function addDimensionAttributes(ModuleDataSetupInterface $setup)
  86. {
  87. /** @var EavSetup $eavSetup */
  88. $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
  89. $eavSetup->addAttribute(Product::ENTITY, self::ATTRIBUTE_CODE_LENGTH, [
  90. 'type' => 'decimal',
  91. 'label' => 'Length',
  92. 'input' => 'text',
  93. 'required' => false,
  94. 'class' => 'not-negative-amount',
  95. 'sort_order' => 65,
  96. 'global' => ScopedAttributeInterface::SCOPE_STORE,
  97. 'is_used_in_grid' => false,
  98. 'is_visible_in_grid' => false,
  99. 'is_filterable_in_grid' => false,
  100. 'user_defined' => false,
  101. 'apply_to' => Type::TYPE_SIMPLE
  102. ]);
  103. $eavSetup->addAttribute(Product::ENTITY, self::ATTRIBUTE_CODE_WIDTH, [
  104. 'type' => 'decimal',
  105. 'label' => 'Width',
  106. 'input' => 'text',
  107. 'required' => false,
  108. 'class' => 'not-negative-amount',
  109. 'sort_order' => 66,
  110. 'global' => ScopedAttributeInterface::SCOPE_STORE,
  111. 'is_used_in_grid' => false,
  112. 'is_visible_in_grid' => false,
  113. 'is_filterable_in_grid' => false,
  114. 'user_defined' => false,
  115. 'apply_to' => Type::TYPE_SIMPLE
  116. ]);
  117. $eavSetup->addAttribute(Product::ENTITY, self::ATTRIBUTE_CODE_HEIGHT, [
  118. 'type' => 'decimal',
  119. 'label' => 'Height',
  120. 'input' => 'text',
  121. 'required' => false,
  122. 'class' => 'not-negative-amount',
  123. 'sort_order' => 67,
  124. 'global' => ScopedAttributeInterface::SCOPE_STORE,
  125. 'is_used_in_grid' => false,
  126. 'is_visible_in_grid' => false,
  127. 'is_filterable_in_grid' => false,
  128. 'user_defined' => false,
  129. 'apply_to' => Type::TYPE_SIMPLE
  130. ]);
  131. }
  132. /**
  133. * Add new Pickup Order Email Template to DB.
  134. *
  135. * @throws \Magento\Framework\Exception\AlreadyExistsException
  136. * @throws \Magento\Framework\Exception\FileSystemException
  137. */
  138. public function addPickupOrderEmailTemplate()
  139. {
  140. $template = $this->templateFactory->create();
  141. $template->setTemplateCode('New Pickup Order');
  142. $template->setTemplateText($this->getEmailTemplate());
  143. $template->setTemplateType(TemplateTypesInterface::TYPE_HTML);
  144. $template->setTemplateSubject(
  145. '{{trans "Your %store_name order confirmation" store_name=$store.getFrontendName()}}'
  146. );
  147. $template->setOrigTemplateCode('sales_email_order_template');
  148. // @codingStandardsIgnoreLine
  149. $template->setOrigTemplateVariables('{"var formattedBillingAddress|raw":"Billing Address","var order.getEmailCustomerNote()":"Email Order Note","var order.increment_id":"Order Id","layout handle=\"sales_email_order_items\" order=$order area=\"frontend\"":"Order Items Grid","var payment_html|raw":"Payment Details","var formattedShippingAddress|raw":"Shipping Address","var order.getShippingDescription()":"Shipping Description","var shipping_msg":"Shipping message"}');
  150. $this->templateResource->save($template);
  151. }
  152. /**
  153. * Add New Order Pickup Email Template.
  154. *
  155. * @throws \Magento\Framework\Exception\AlreadyExistsException
  156. * @throws \Magento\Framework\Exception\FileSystemException
  157. */
  158. public function addPickupOrderGuestEmailTemplate()
  159. {
  160. $template = $this->templateFactory->create();
  161. $template->setTemplateCode('New Pickup Order For Guest');
  162. $template->setTemplateText($this->getEmailTemplateForGuest());
  163. $template->setTemplateType(TemplateTypesInterface::TYPE_HTML);
  164. $template->setTemplateSubject(
  165. '{{trans "Your %store_name order confirmation" store_name=$store.getFrontendName()}}'
  166. );
  167. $template->setOrigTemplateCode('sales_email_order_guest_template');
  168. // @codingStandardsIgnoreLine
  169. $template->setOrigTemplateVariables('{"var formattedBillingAddress|raw":"Billing Address","var order.getEmailCustomerNote()":"Email Order Note","var order.getBillingAddress().getName()":"Guest Customer Name","var order.getCreatedAtFormatted(2)":"Order Created At (datetime)","var order.increment_id":"Order Id","layout handle=\"sales_email_order_items\" order=$order":"Order Items Grid","var payment_html|raw":"Payment Details","var formattedShippingAddress|raw":"Shipping Address","var order.getShippingDescription()":"Shipping Description","var shipping_msg":"Shipping message"}');
  170. $this->templateResource->save($template);
  171. }
  172. /**
  173. * @return string
  174. * @throws \Magento\Framework\Exception\FileSystemException
  175. */
  176. private function getEmailTemplate()
  177. {
  178. $viewDir = $this->getDirectory();
  179. $templateContent = $this->fileSystemDriver->fileGetContents($viewDir . self::PICKUP_ORDER_TEMPLATE);
  180. return $templateContent;
  181. }
  182. /**
  183. * @return string
  184. * @throws \Magento\Framework\Exception\FileSystemException
  185. */
  186. private function getEmailTemplateForGuest()
  187. {
  188. $viewDir = $this->getDirectory();
  189. $templateContent = $this->fileSystemDriver->fileGetContents($viewDir . self::PICKUP_ORDER_GUEST_TEMPLATE);
  190. return $templateContent;
  191. }
  192. /**
  193. * @return string
  194. */
  195. private function getDirectory()
  196. {
  197. $viewDir = $this->moduleReader->getModuleDir(
  198. \Magento\Framework\Module\Dir::MODULE_VIEW_DIR,
  199. 'Temando_Shipping'
  200. );
  201. return $viewDir . '/frontend/email/';
  202. }
  203. }