Guest User

Untitled

a guest
May 10th, 2018
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.97 KB | None | 0 0
  1. namespace DacRecipesControllerAdminhtmlTest;
  2.  
  3.  
  4. use MagentoFrameworkControllerResultFactory;
  5.  
  6. /**
  7. * Class Upload
  8. */
  9. class Upload extends MagentoBackendAppAction
  10. {
  11. /**
  12. * Image uploader
  13. *
  14. * @var MagentoCatalogModelImageUploader
  15. */
  16. protected $imageUploader;
  17.  
  18. /**
  19. * Upload constructor.
  20. *
  21. * @param MagentoBackendAppActionContext $context
  22. * @param MagentoCatalogModelImageUploader $imageUploader
  23. */
  24. public function __construct(
  25. MagentoBackendAppActionContext $context,
  26. DacRecipesModelImageUploader $imageUploader
  27. ) {
  28. parent::__construct($context);
  29. $this->imageUploader = $imageUploader;
  30. }
  31.  
  32. /**
  33. * Check admin permissions for this controller
  34. *
  35. * @return boolean
  36. */
  37. protected function _isAllowed()
  38. {
  39. return $this->_authorization->isAllowed('Dac_Recipes::recipe');
  40. }
  41.  
  42. /**
  43. * Upload file controller action
  44. *
  45. * @return MagentoFrameworkControllerResultInterface
  46. */
  47. public function execute()
  48. {
  49. try {
  50. $result = $this->imageUploader->saveFileToTmpDir('recipes');
  51.  
  52. $result['cookie'] = [
  53. 'name' => $this->_getSession()->getName(),
  54. 'value' => $this->_getSession()->getSessionId(),
  55. 'lifetime' => $this->_getSession()->getCookieLifetime(),
  56. 'path' => $this->_getSession()->getCookiePath(),
  57. 'domain' => $this->_getSession()->getCookieDomain(),
  58. ];
  59. } catch (Exception $e) {
  60. $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
  61. }
  62.  
  63. // print_r($result); die('upload controller');
  64.  
  65. return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
  66. }
  67. }
  68.  
  69. <?php
  70.  
  71. namespace DacRecipesControllerAdminhtmlTest;
  72. //namespace DacRecipesControllerAdminhtmlTest;
  73.  
  74. use MagentoBackendAppAction;
  75. use MagentoFrameworkAppRequestDataPersistorInterface;
  76. use MagentoFrameworkExceptionLocalizedException;
  77.  
  78. class Save extends MagentoBackendAppAction
  79. {
  80.  
  81. const ADMIN_RESOURCE = 'Dac_Recipes::recipes';
  82. protected $dataProcessor;
  83. protected $dataPersistor;
  84. protected $imageUploader;
  85.  
  86.  
  87. public function __construct(
  88. ActionContext $context,
  89. PostDataProcessor $dataProcessor,
  90. DataPersistorInterface $dataPersistor
  91. )
  92. {
  93. $this->dataProcessor = $dataProcessor;
  94. $this->dataPersistor = $dataPersistor;
  95. parent::__construct($context);
  96. }
  97.  
  98. public function execute()
  99. {
  100.  
  101. $data = $this->getRequest()->getPostValue();
  102.  
  103.  
  104. $resultRedirect = $this->resultRedirectFactory->create();
  105. if ($data) {
  106.  
  107.  
  108. if (isset($data['recipes_image'][0]['name']) && isset($data['recipes_image'][0]['tmp_name'])) {
  109. $data['image'] = $data['recipes_image'][0]['name'];
  110. $this->imageUploader = MagentoFrameworkAppObjectManager::getInstance()->get(
  111. 'DacRecipesRecipesImageUpload'
  112. );
  113. $this->imageUploader->moveFileFromTmp($data['image']);
  114. } elseif (isset($data['recipes_image'][0]['image']) && !isset($data['recipes_image'][0]['tmp_name'])) {
  115. $data['image'] = $data['recipes_image'][0]['image'];
  116. } else {
  117. $data['image'] = null;
  118. }
  119.  
  120.  
  121. return $resultRedirect->setPath('*/*/');
  122. }
  123. }
  124. // public function execute(){
  125. // die('save image');
  126. // }
  127. }
  128.  
  129. <virtualType name="DacRecipesRecipesImageUpload" type="DacRecipesModelImageUploader">
  130. <arguments>
  131. <argument name="baseTmpPath" xsi:type="string">test/tmp</argument>
  132. <argument name="basePath" xsi:type="string">test</argument>
  133. <argument name="allowedExtensions" xsi:type="array">
  134. <item name="jpg" xsi:type="string">jpg</item>
  135. <item name="jpeg" xsi:type="string">jpeg</item>
  136. <item name="gif" xsi:type="string">gif</item>
  137. <item name="png" xsi:type="string">png</item>
  138. </argument>
  139. </arguments>
  140. </virtualType>
  141. <type name="DacRecipesControllerAdminhtmlTestUpload">
  142. <arguments>
  143. <argument name="imageUploader" xsi:type="object">DacRecipesRecipesImageUpload</argument>
  144. </arguments>
  145. </type>
  146.  
  147. <?php
  148.  
  149. namespace DacRecipesModel;
  150.  
  151. /**
  152. * Catalog image uploader
  153. */
  154. class ImageUploader
  155. {
  156. /**
  157. * Core file storage database
  158. *
  159. * @var MagentoMediaStorageHelperFileStorageDatabase
  160. */
  161. protected $coreFileStorageDatabase;
  162.  
  163. /**
  164. * Media directory object (writable).
  165. *
  166. * @var MagentoFrameworkFilesystemDirectoryWriteInterface
  167. */
  168. protected $mediaDirectory;
  169.  
  170. /**
  171. * Uploader factory
  172. *
  173. * @var MagentoMediaStorageModelFileUploaderFactory
  174. */
  175. private $uploaderFactory;
  176.  
  177. /**
  178. * Store manager
  179. *
  180. * @var MagentoStoreModelStoreManagerInterface
  181. */
  182. protected $storeManager;
  183.  
  184. /**
  185. * @var PsrLogLoggerInterface
  186. */
  187. protected $logger;
  188.  
  189. /**
  190. * Base tmp path
  191. *
  192. * @var string
  193. */
  194. protected $baseTmpPath;
  195.  
  196. /**
  197. * Base path
  198. *
  199. * @var string
  200. */
  201. protected $basePath;
  202.  
  203. /**
  204. * Allowed extensions
  205. *
  206. * @var string
  207. */
  208. protected $allowedExtensions;
  209.  
  210.  
  211. public function __construct(
  212. MagentoMediaStorageHelperFileStorageDatabase $coreFileStorageDatabase,
  213. MagentoFrameworkFilesystem $filesystem,
  214. MagentoMediaStorageModelFileUploaderFactory $uploaderFactory,
  215. MagentoStoreModelStoreManagerInterface $storeManager,
  216. PsrLogLoggerInterface $logger,
  217. $baseTmpPath,
  218. $basePath,
  219. $allowedExtensions
  220. ) {
  221. $this->coreFileStorageDatabase = $coreFileStorageDatabase;
  222. $this->mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
  223. $this->uploaderFactory = $uploaderFactory;
  224. $this->storeManager = $storeManager;
  225. $this->logger = $logger;
  226. $this->baseTmpPath = $baseTmpPath;
  227. $this->basePath = $basePath;
  228. $this->allowedExtensions = $allowedExtensions;
  229. }
  230.  
  231. /**
  232. * Set base tmp path
  233. *
  234. * @param string $baseTmpPath
  235. *
  236. * @return void
  237. */
  238. public function setBaseTmpPath($baseTmpPath)
  239. {
  240. $this->baseTmpPath = $baseTmpPath;
  241. }
  242.  
  243. /**
  244. * Set base path
  245. *
  246. * @param string $basePath
  247. *
  248. * @return void
  249. */
  250. public function setBasePath($basePath)
  251. {
  252. $this->basePath = $basePath;
  253. }
  254.  
  255. /**
  256. * Set allowed extensions
  257. *
  258. * @param string[] $allowedExtensions
  259. *
  260. * @return void
  261. */
  262. public function setAllowedExtensions($allowedExtensions)
  263. {
  264. $this->allowedExtensions = $allowedExtensions;
  265. }
  266.  
  267. /**
  268. * Retrieve base tmp path
  269. *
  270. * @return string
  271. */
  272. public function getBaseTmpPath()
  273. {
  274. return $this->baseTmpPath;
  275. }
  276.  
  277. /**
  278. * Retrieve base path
  279. *
  280. * @return string
  281. */
  282. public function getBasePath()
  283. {
  284. return $this->basePath;
  285. }
  286.  
  287. /**
  288. * Retrieve base path
  289. *
  290. * @return string[]
  291. */
  292. public function getAllowedExtensions()
  293. {
  294. return $this->allowedExtensions;
  295. }
  296.  
  297. /**
  298. * Retrieve path
  299. *
  300. * @param string $path
  301. * @param string $imageName
  302. *
  303. * @return string
  304. */
  305. public function getFilePath($path, $imageName)
  306. {
  307. return rtrim($path, '/') . '/' . ltrim($imageName, '/');
  308. }
  309.  
  310. /**
  311. * Checking file for moving and move it
  312. *
  313. * @param string $imageName
  314. *
  315. * @return string
  316. *
  317. * @throws MagentoFrameworkExceptionLocalizedException
  318. */
  319. public function moveFileFromTmp($imageName)
  320. {
  321. $baseTmpPath = $this->getBaseTmpPath();
  322. $basePath = $this->getBasePath();
  323.  
  324. $baseImagePath = $this->getFilePath($basePath, $imageName);
  325. $baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
  326. // echo 'sdfdsfsdfss'; die('move file');
  327. try {
  328. $this->coreFileStorageDatabase->copyFile(
  329. $baseTmpImagePath,
  330. $baseImagePath
  331. );
  332. $this->mediaDirectory->renameFile(
  333. $baseTmpImagePath,
  334. $baseImagePath
  335. );
  336. } catch (Exception $e) {
  337. throw new MagentoFrameworkExceptionLocalizedException(
  338. __('Something went wrong while saving the file(s).')
  339. );
  340. }
  341.  
  342. return $imageName;
  343. }
  344.  
  345.  
  346. public function saveFileToTmpDir($fileId)
  347. {
  348. $baseTmpPath = $this->getBaseTmpPath();
  349. // print_r($baseTmpPath); die();
  350. $uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
  351. $uploader->setAllowedExtensions($this->getAllowedExtensions());
  352. $uploader->setAllowRenameFiles(true);
  353.  
  354. $result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
  355.  
  356. if (!$result) {
  357. throw new MagentoFrameworkExceptionLocalizedException(
  358. __('File can not be saved to the destination folder.')
  359. );
  360. }
  361.  
  362. // $seatch=' ';
  363. // $subject='/';
  364. // die($result);
  365. $result['tmp_name'] =str_replace(" ", "/", $result['tmp_name']);
  366.  
  367. $result['path'] = str_replace(' ', '/', $result['path']);
  368. $result['url'] = $this->storeManager
  369. ->getStore()
  370. ->getBaseUrl(
  371. MagentoFrameworkUrlInterface::URL_TYPE_MEDIA
  372. ) . $this->getFilePath($baseTmpPath, $result['file']);
  373. $result['name'] = $result['file'];
  374.  
  375. if (isset($result['file'])) {
  376. try {
  377. $relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
  378. $this->coreFileStorageDatabase->saveFile($relativePath);
  379. } catch (Exception $e) {
  380. $this->logger->critical($e);
  381. throw new MagentoFrameworkExceptionLocalizedException(
  382. __('Something went wrong while saving the file(s).')
  383. );
  384. }
  385. }
  386.  
  387. return $result;
  388. }
  389. }
  390.  
  391. <field name="recipes_image">
  392. <argument name="data" xsi:type="array">
  393. <item name="config" xsi:type="array">
  394. <item name="dataType" xsi:type="string">string</item>
  395. <item name="source" xsi:type="string">recipe</item>
  396. <item name="label" xsi:type="string" translate="true">Image</item>
  397. <item name="visible" xsi:type="boolean">true</item>
  398. <item name="formElement" xsi:type="string">fileUploader</item>
  399. <item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
  400. <item name="previewTmpl" xsi:type="string">Dac_Recipes/image-preview</item>
  401. <item name="required" xsi:type="boolean">false</item>
  402. <item name="uploaderConfig" xsi:type="array">
  403. <item name="url" xsi:type="url" path="recipes/test/upload"/>
  404. </item>
  405. </item>
  406. </argument>
  407. </field>
  408.  
  409. <field name="logo">
  410. <argument name="data" xsi:type="array">
  411. <item name="config" xsi:type="array">
  412. <item name="dataType" xsi:type="string">string</item>
  413. <item name="source" xsi:type="string">helloworld</item>
  414. <item name="label" xsi:type="string" translate="true">Image</item>
  415. <item name="visible" xsi:type="boolean">true</item>
  416. <item name="formElement" xsi:type="string">fileUploader</item>
  417. <item name="elementTmpl" xsi:type="string">ui/form/element/uploader/uploader</item>
  418. <item name="previewTmpl" xsi:type="string">QaisarSatti_HelloWorld/image-preview</item>
  419. <item name="required" xsi:type="boolean">false</item>
  420. <item name="uploaderConfig" xsi:type="array">
  421. <item name="url" xsi:type="url" path="helloworld/index/upload"/>
  422. </item>
  423. </item>
  424. </argument>
  425. </field>
  426.  
  427. <div class="file-uploader-summary">
  428. <div class="file-uploader-preview">
  429. <a attr="href: $parent.getFilePreview($file)" target="_blank">
  430. <img
  431. class="preview-image"
  432. tabindex="0"
  433. event="load: $parent.onPreviewLoad.bind($parent)"
  434. attr="
  435. src: $parent.getFilePreview($file),
  436. alt: $file.name">
  437. </a>
  438.  
  439. <div class="actions">
  440. <button
  441. type="button"
  442. class="action-remove"
  443. data-role="delete-button"
  444. attr="title: $t('Delete image')"
  445. click="$parent.removeFile.bind($parent, $file)">
  446. <span translate="'Delete image'"/>
  447. </button>
  448. </div>
  449. </div>
  450.  
  451. <div class="file-uploader-filename" text="$file.name"/>
  452. <div class="file-uploader-meta">
  453. <text args="$file.previewWidth"/>x<text args="$file.previewHeight"/>
  454. </div>
  455. </div>
  456.  
  457. <?php
  458. /**
  459. * Simple Hello World Module
  460. *
  461. * @category QaisarSatti
  462. * @package QaisarSatti_HelloWorld
  463. * @author Muhammad Qaisar Satti
  464. * @Email qaisarssatti@gmail.com
  465. *
  466. */
  467. namespace QaisarSattiHelloWorldModel;
  468.  
  469. /**
  470. * Catalog image uploader
  471. */
  472. class ImageUploader
  473. {
  474. /**
  475. * Core file storage database
  476. *
  477. * @var MagentoMediaStorageHelperFileStorageDatabase
  478. */
  479. protected $coreFileStorageDatabase;
  480.  
  481. /**
  482. * Media directory object (writable).
  483. *
  484. * @var MagentoFrameworkFilesystemDirectoryWriteInterface
  485. */
  486. protected $mediaDirectory;
  487.  
  488. /**
  489. * Uploader factory
  490. *
  491. * @var MagentoMediaStorageModelFileUploaderFactory
  492. */
  493. private $uploaderFactory;
  494.  
  495. /**
  496. * Store manager
  497. *
  498. * @var MagentoStoreModelStoreManagerInterface
  499. */
  500. protected $storeManager;
  501.  
  502. /**
  503. * @var PsrLogLoggerInterface
  504. */
  505. protected $logger;
  506.  
  507. /**
  508. * Base tmp path
  509. *
  510. * @var string
  511. */
  512. protected $baseTmpPath;
  513.  
  514. /**
  515. * Base path
  516. *
  517. * @var string
  518. */
  519. protected $basePath;
  520.  
  521. /**
  522. * Allowed extensions
  523. *
  524. * @var string
  525. */
  526. protected $allowedExtensions;
  527.  
  528. /**
  529. * ImageUploader constructor
  530. *
  531. * @param MagentoMediaStorageHelperFileStorageDatabase $coreFileStorageDatabase
  532. * @param MagentoFrameworkFilesystem $filesystem
  533. * @param MagentoMediaStorageModelFileUploaderFactory $uploaderFactory
  534. * @param MagentoStoreModelStoreManagerInterface $storeManager
  535. * @param PsrLogLoggerInterface $logger
  536. * @param string $baseTmpPath
  537. * @param string $basePath
  538. * @param string[] $allowedExtensions
  539. */
  540. public function __construct(
  541. MagentoMediaStorageHelperFileStorageDatabase $coreFileStorageDatabase,
  542. MagentoFrameworkFilesystem $filesystem,
  543. MagentoMediaStorageModelFileUploaderFactory $uploaderFactory,
  544. MagentoStoreModelStoreManagerInterface $storeManager,
  545. PsrLogLoggerInterface $logger,
  546. $baseTmpPath,
  547. $basePath,
  548. $allowedExtensions
  549. ) {
  550. $this->coreFileStorageDatabase = $coreFileStorageDatabase;
  551. $this->mediaDirectory = $filesystem->getDirectoryWrite(MagentoFrameworkAppFilesystemDirectoryList::MEDIA);
  552. $this->uploaderFactory = $uploaderFactory;
  553. $this->storeManager = $storeManager;
  554. $this->logger = $logger;
  555. $this->baseTmpPath = $baseTmpPath;
  556. $this->basePath = $basePath;
  557. $this->allowedExtensions = $allowedExtensions;
  558. }
  559.  
  560. /**
  561. * Set base tmp path
  562. *
  563. * @param string $baseTmpPath
  564. *
  565. * @return void
  566. */
  567. public function setBaseTmpPath($baseTmpPath)
  568. {
  569. $this->baseTmpPath = $baseTmpPath;
  570. }
  571.  
  572. /**
  573. * Set base path
  574. *
  575. * @param string $basePath
  576. *
  577. * @return void
  578. */
  579. public function setBasePath($basePath)
  580. {
  581. $this->basePath = $basePath;
  582. }
  583.  
  584. /**
  585. * Set allowed extensions
  586. *
  587. * @param string[] $allowedExtensions
  588. *
  589. * @return void
  590. */
  591. public function setAllowedExtensions($allowedExtensions)
  592. {
  593. $this->allowedExtensions = $allowedExtensions;
  594. }
  595.  
  596. /**
  597. * Retrieve base tmp path
  598. *
  599. * @return string
  600. */
  601. public function getBaseTmpPath()
  602. {
  603. return $this->baseTmpPath;
  604. }
  605.  
  606. /**
  607. * Retrieve base path
  608. *
  609. * @return string
  610. */
  611. public function getBasePath()
  612. {
  613. return $this->basePath;
  614. }
  615.  
  616. /**
  617. * Retrieve base path
  618. *
  619. * @return string[]
  620. */
  621. public function getAllowedExtensions()
  622. {
  623. return $this->allowedExtensions;
  624. }
  625.  
  626. /**
  627. * Retrieve path
  628. *
  629. * @param string $path
  630. * @param string $imageName
  631. *
  632. * @return string
  633. */
  634. public function getFilePath($path, $imageName)
  635. {
  636. return rtrim($path, '/') . '/' . ltrim($imageName, '/');
  637. }
  638.  
  639. /**
  640. * Checking file for moving and move it
  641. *
  642. * @param string $imageName
  643. *
  644. * @return string
  645. *
  646. * @throws MagentoFrameworkExceptionLocalizedException
  647. */
  648. public function moveFileFromTmp($imageName)
  649. {
  650. $baseTmpPath = $this->getBaseTmpPath();
  651. $basePath = $this->getBasePath();
  652.  
  653. $baseImagePath = $this->getFilePath($basePath, $imageName);
  654. $baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
  655.  
  656. try {
  657. $this->coreFileStorageDatabase->copyFile(
  658. $baseTmpImagePath,
  659. $baseImagePath
  660. );
  661. $this->mediaDirectory->renameFile(
  662. $baseTmpImagePath,
  663. $baseImagePath
  664. );
  665. } catch (Exception $e) {
  666. throw new MagentoFrameworkExceptionLocalizedException(
  667. __('Something went wrong while saving the file(s).')
  668. );
  669. }
  670.  
  671. return $imageName;
  672. }
  673.  
  674. /**
  675. * Checking file for save and save it to tmp dir
  676. *
  677. * @param string $fileId
  678. *
  679. * @return string[]
  680. *
  681. * @throws MagentoFrameworkExceptionLocalizedException
  682. */
  683. public function saveFileToTmpDir($fileId)
  684. {
  685. $baseTmpPath = $this->getBaseTmpPath();
  686.  
  687. $uploader = $this->uploaderFactory->create(['fileId' => $fileId]);
  688. $uploader->setAllowedExtensions($this->getAllowedExtensions());
  689. $uploader->setAllowRenameFiles(true);
  690.  
  691. $result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath));
  692.  
  693. if (!$result) {
  694. throw new MagentoFrameworkExceptionLocalizedException(
  695. __('File can not be saved to the destination folder.')
  696. );
  697. }
  698.  
  699. /**
  700. * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS
  701. */
  702. $result['tmp_name'] = str_replace('', '/', $result['tmp_name']);
  703. $result['path'] = str_replace('', '/', $result['path']);
  704. $result['url'] = $this->storeManager
  705. ->getStore()
  706. ->getBaseUrl(
  707. MagentoFrameworkUrlInterface::URL_TYPE_MEDIA
  708. ) . $this->getFilePath($baseTmpPath, $result['file']);
  709. $result['name'] = $result['file'];
  710.  
  711. if (isset($result['file'])) {
  712. try {
  713. $relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/');
  714. $this->coreFileStorageDatabase->saveFile($relativePath);
  715. } catch (Exception $e) {
  716. $this->logger->critical($e);
  717. throw new MagentoFrameworkExceptionLocalizedException(
  718. __('Something went wrong while saving the file(s).')
  719. );
  720. }
  721. }
  722.  
  723. return $result;
  724. }
  725. }
  726.  
  727. <virtualType name="QaisarSattiHelloWorldHelloWorldImageUpload" type="QaisarSattiHelloWorldModelImageUploader">
  728. <arguments>
  729. <argument name="baseTmpPath" xsi:type="string">test/tmp</argument>
  730. <argument name="basePath" xsi:type="string">test</argument>
  731. <argument name="allowedExtensions" xsi:type="array">
  732. <item name="jpg" xsi:type="string">jpg</item>
  733. <item name="jpeg" xsi:type="string">jpeg</item>
  734. <item name="gif" xsi:type="string">gif</item>
  735. <item name="png" xsi:type="string">png</item>
  736. </argument>
  737. </arguments>
  738. </virtualType>
  739. <type name="QaisarSattiHelloWorldControllerAdminhtmlIndexUpload">
  740. <arguments>
  741. <argument name="imageUploader" xsi:type="object">QaisarSattiHelloWorldHelloWorldImageUpload</argument>
  742. </arguments>
  743. </type>
  744.  
  745. <?php
  746. /**
  747. * Simple Hello World Module
  748. *
  749. * @category QaisarSatti
  750. * @package QaisarSatti_HelloWorld
  751. * @author Muhammad Qaisar Satti
  752. * @Email qaisarssatti@gmail.com
  753. *
  754. */
  755. namespace QaisarSattiHelloWorldControllerAdminhtmlIndex;
  756.  
  757. use MagentoFrameworkControllerResultFactory;
  758.  
  759. /**
  760. * Class Upload
  761. */
  762. class Upload extends MagentoBackendAppAction
  763. {
  764. /**
  765. * Image uploader
  766. *
  767. * @var MagentoCatalogModelImageUploader
  768. */
  769. protected $imageUploader;
  770.  
  771. /**
  772. * Upload constructor.
  773. *
  774. * @param MagentoBackendAppActionContext $context
  775. * @param MagentoCatalogModelImageUploader $imageUploader
  776. */
  777. public function __construct(
  778. MagentoBackendAppActionContext $context,
  779. QaisarSattiHelloWorldModelImageUploader $imageUploader
  780. ) {
  781. parent::__construct($context);
  782. $this->imageUploader = $imageUploader;
  783. }
  784.  
  785. /**
  786. * Check admin permissions for this controller
  787. *
  788. * @return boolean
  789. */
  790. protected function _isAllowed()
  791. {
  792. return $this->_authorization->isAllowed('QaisarSatti_HelloWorld::helloworld1');
  793. }
  794.  
  795. /**
  796. * Upload file controller action
  797. *
  798. * @return MagentoFrameworkControllerResultInterface
  799. */
  800. public function execute()
  801. {
  802. try {
  803. $result = $this->imageUploader->saveFileToTmpDir('logo');
  804.  
  805. $result['cookie'] = [
  806. 'name' => $this->_getSession()->getName(),
  807. 'value' => $this->_getSession()->getSessionId(),
  808. 'lifetime' => $this->_getSession()->getCookieLifetime(),
  809. 'path' => $this->_getSession()->getCookiePath(),
  810. 'domain' => $this->_getSession()->getCookieDomain(),
  811. ];
  812. } catch (Exception $e) {
  813. $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
  814. }
  815. return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
  816. }
  817. }
  818.  
  819. <?php
  820. /**
  821. * Simple Hello World Module
  822. *
  823. * @category QaisarSatti
  824. * @package QaisarSatti_HelloWorld
  825. * @author Muhammad Qaisar Satti
  826. * @Email qaisarssatti@gmail.com
  827. *
  828. */
  829. namespace QaisarSattiHelloWorldControllerAdminhtmlIndex;
  830.  
  831. use MagentoBackendAppAction;
  832. use MagentoFrameworkAppRequestDataPersistorInterface;
  833. use MagentoFrameworkExceptionLocalizedException;
  834.  
  835. class Save extends MagentoBackendAppAction
  836. {
  837.  
  838. const ADMIN_RESOURCE = 'QaisarSatti_HelloWorld::helloworld';
  839. protected $dataProcessor;
  840. protected $dataPersistor;
  841. protected $imageUploader;
  842.  
  843.  
  844. public function __construct(
  845. ActionContext $context,
  846. PostDataProcessor $dataProcessor,
  847. DataPersistorInterface $dataPersistor
  848. ) {
  849. $this->dataProcessor = $dataProcessor;
  850. $this->dataPersistor = $dataPersistor;
  851. parent::__construct($context);
  852. }
  853.  
  854. public function execute()
  855. {
  856.  
  857. $data = $this->getRequest()->getPostValue();
  858.  
  859.  
  860. $resultRedirect = $this->resultRedirectFactory->create();
  861. if ($data) {
  862.  
  863.  
  864.  
  865.  
  866.  
  867. if (isset($data['logo'][0]['name']) && isset($data['logo'][0]['tmp_name'])) {
  868. $data['image'] =$data['logo'][0]['name'];
  869. $this->imageUploader = MagentoFrameworkAppObjectManager::getInstance()->get(
  870. 'QaisarSattiHelloWorldHelloWorldImageUpload'
  871. );
  872. $this->imageUploader->moveFileFromTmp($data['image']);
  873. } elseif (isset($data['logo'][0]['image']) && !isset($data['logo'][0]['tmp_name'])) {
  874. $data['image'] = $data['logo'][0]['image'];
  875. } else {
  876. $data['image'] = null;
  877. }
  878.  
  879.  
  880. return $resultRedirect->setPath('*/*/');
  881. }
  882. }
  883.  
  884. <?php
  885. /**
  886. * Simple Hello World Module
  887. *
  888. * @category QaisarSatti
  889. * @package QaisarSatti_HelloWorld
  890. * @author Muhammad Qaisar Satti
  891. * @Email qaisarssatti@gmail.com
  892. *
  893. */
  894. namespace QaisarSattiHelloWorldModelHelloWorld;
  895.  
  896. use QaisarSattiHelloWorldModelResourceModelHelloWorldCollectionFactory;
  897. use MagentoFrameworkAppRequestDataPersistorInterface;
  898.  
  899. /**
  900. * Class DataProvider
  901. */
  902. class DataProvider extends MagentoUiDataProviderAbstractDataProvider
  903. {
  904. /**
  905. * @var MagentoCmsModelResourceModelBlockCollection
  906. */
  907. protected $collection;
  908.  
  909. /**
  910. * @var DataPersistorInterface
  911. */
  912. protected $dataPersistor;
  913.  
  914. /**
  915. * @var array
  916. */
  917. public $_storeManager;
  918.  
  919. protected $loadedData;
  920.  
  921. /**
  922. * Constructor
  923. *
  924. * @param string $name
  925. * @param string $primaryFieldName
  926. * @param string $requestFieldName
  927. * @param CollectionFactory $blockCollectionFactory
  928. * @param DataPersistorInterface $dataPersistor
  929. * @param array $meta
  930. * @param array $data
  931. */
  932. public function __construct(
  933. $name,
  934. $primaryFieldName,
  935. $requestFieldName,
  936. CollectionFactory $helloworldCollectionFactory,
  937. MagentoStoreModelStoreManagerInterface $storeManager,
  938. DataPersistorInterface $dataPersistor,
  939. array $meta = [],
  940. array $data = []
  941. ) {
  942. $this->collection = $helloworldCollectionFactory->create();
  943. $this->dataPersistor = $dataPersistor;
  944. $this->_storeManager=$storeManager;
  945. parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
  946. }
  947.  
  948. /**
  949. * Get data
  950. *
  951. * @return array
  952. */
  953. public function getData()
  954. {
  955. $baseurl = $this->_storeManager->getStore()->getBaseUrl(MagentoFrameworkUrlInterface::URL_TYPE_MEDIA);
  956. if (isset($this->loadedData)) {
  957. return $this->loadedData;
  958. }
  959. $items = $this->collection->getItems();
  960. /** @var MagentoCmsModelBlock $block */
  961. foreach ($items as $helloworld) {
  962. $temp = $helloworld>getData();
  963. if($temp['image']):
  964. $img = [];
  965. $img[0]['image'] = $temp['image'];
  966. $img[0]['url'] = $baseurl.'test/'.$temp['image'];
  967. $temp['logo'] = $img;
  968. endif;
  969.  
  970.  
  971. $data = $this->dataPersistor->get('helloworld');
  972. if (!empty($data)) {
  973. $helloworld = $this->collection->getNewEmptyItem();
  974. $helloworld>setData($data);
  975. $this->loadedData[$helloworld->getLabelId()] = $helloworld->getData();
  976. $this->dataPersistor->clear('helloworld');
  977. }else {
  978. if($items):
  979. if ($helloworld->getData('image') != null) {
  980.  
  981. $t2[$helloworld>getId()] = $temp;
  982.  
  983. return $t2;
  984. } else {
  985.  
  986.  
  987. return $this->loadedData;
  988.  
  989. }
  990. endif;
  991. }
  992.  
  993.  
  994. return $this->loadedData;
  995. }
  996. }
Add Comment
Please, Sign In to add comment