diff --git a/Block/Adminhtml/Advertisment/Edit/BackButton.php b/Block/Adminhtml/Advertisment/Edit/BackButton.php new file mode 100644 index 0000000..2262b9e --- /dev/null +++ b/Block/Adminhtml/Advertisment/Edit/BackButton.php @@ -0,0 +1,29 @@ + __('Back'), + 'on_click' => sprintf("location.href = '%s';", 'advertisment/adverts'), + 'class' => 'back', + 'sort_order' => 10 + ]; + } +} diff --git a/Block/Adminhtml/Advertisment/Edit/GenericButton.php b/Block/Adminhtml/Advertisment/Edit/GenericButton.php new file mode 100644 index 0000000..1727840 --- /dev/null +++ b/Block/Adminhtml/Advertisment/Edit/GenericButton.php @@ -0,0 +1,69 @@ +urlBuilder = $context->getUrlBuilder(); + $this->registry = $registry; + } + + /** + * Return the synonyms group Id. + * + * @return int|null + */ + public function getId() + { + $contact = $this->registry->registry('contact'); + return $contact ? $contact->getId() : null; + } + + /** + * Generate url by route and parameters + * + * @param string $route + * @param array $params + * @return string + */ + public function getUrl($route = '', $params = []) + { + return $this->urlBuilder->getUrl($route, $params); + } +} diff --git a/Block/Adminhtml/Advertisment/Edit/SaveButton.php b/Block/Adminhtml/Advertisment/Edit/SaveButton.php new file mode 100644 index 0000000..8a6a358 --- /dev/null +++ b/Block/Adminhtml/Advertisment/Edit/SaveButton.php @@ -0,0 +1,26 @@ +_adverts = $adverts; + $this->_storeManager = $storeManager; + $this->_DateTime = $DateTime; + parent::__construct($context); + } + + /** + * @return void + */ + public function _prepareLayout() + { + $this->pageConfig->getTitle()->set(__('Advertisments')); + + return parent::_prepareLayout(); + } + + /** + * Retrieve collection of 'advertisments', filtered with (from-to date, status and display on pages) + * @param string $page + * @return array + */ + public function getAdverts($page = null) + { + $currentTime = $this->_DateTime->date(); + $model = $this->_adverts->create(); + $collection = $model->getCollection(); + $collection->addFieldToFilter('from_date', ['lteq' => $currentTime]); + $collection->addFieldToFilter('to_date', ['gteq' => $currentTime]); + $collection->addFieldToFilter('ad_status', 1); + $collection->addFieldToFilter('ad_pages', array('finset' => $page)); + + return $collection; + } + + /** + * Creates Media Url + custom folder path + imageName.extension + * @param string $imgName + * @return string + */ + public function getMediaUrl($imgName) + { + $mediaUrl = $this->_storeManager->getStore() + ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'salecto/advertisment/'.$imgName; + return $mediaUrl; + } +} diff --git a/Console/Adverts.php b/Console/Adverts.php new file mode 100644 index 0000000..21d0fd2 --- /dev/null +++ b/Console/Adverts.php @@ -0,0 +1,100 @@ +_model = $model; + parent::__construct(); + } + + + /** + * @inheritDoc + */ + protected function configure() + { + $this->setName('add:advert'); + $this->setDescription('This is my first console command.'); + $this->addOption( + self::TITLE, + null, + InputOption::VALUE_REQUIRED, + 'TITLE' + ); + $this->addOption( + self::STATUS, + null, + InputOption::VALUE_REQUIRED, + 'STATUS' + ); + + parent::configure(); + } + + /** + * Execute the command + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return null|int + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + + $title = $input->getOption(self::TITLE); + $status = $input->getOption(self::STATUS); + $rowData = $this->_model->create(); + if (($status) && ($title) && (($status == 0) || ($status == 1))) { + try{ + $rowData->setTitle($title); + $rowData->setAdStatus($status); + $result = $rowData->save(); + $output->writeln('Success: New Advertisment added with Id Number- `' . $result->getAdId() . '`'); + + }catch (\Exception $e){ + $output->writeln('Can not save new advertisment - `' . $e . '`'); + } + } else { + $output->writeln(' not Allowed'); + } + } +} diff --git a/Console/RfGen.php b/Console/RfGen.php new file mode 100644 index 0000000..a4fe190 --- /dev/null +++ b/Console/RfGen.php @@ -0,0 +1,53 @@ +setName('rf:gen'); + $this->setDescription('Remove Generated Folder'); + + parent::configure(); + } + + /** + * Execute the command + * + * @param InputInterface $input + * @param OutputInterface $output + * + * @return null|int + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $fs = new Filesystem(); + try { + if($fs->exists('generated')){ + $fs->remove(array('generated')); + $output->writeln('Generated Folder Removed!'); + } + else { + $output->writeln('Generated Folder\'s aleady deleted'); + } + } catch (IOExceptionInterface $e) { + echo "An error occurred while deleting your directory at ".$e->getPath(); + } + } +} diff --git a/Controller/Adminhtml/Adverts/Active.php b/Controller/Adminhtml/Adverts/Active.php new file mode 100644 index 0000000..ac00a7b --- /dev/null +++ b/Controller/Adminhtml/Adverts/Active.php @@ -0,0 +1,65 @@ +_model = $model; + } + + /** + * status active action + * + * @return \Magento\Framework\Controller\ResultInterface + */ + public function execute() + { + $id = $this->getRequest()->getParam('ad_id'); + /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ + $resultRedirect = $this->resultRedirectFactory->create(); + if ($id) { + try { + $model = $this->_model; + $model->load($id); + $model->setAdStatus(1)->save(); + $this->messageManager->addSuccess(__('Advertisment %1 Activated', $model->getTitle())); + return $resultRedirect->setPath('*/*/'); + } catch (\Exception $e) { + $this->messageManager->addError($e->getMessage()); + return $resultRedirect->setPath('*/*/edit', ['id' => $id]); + } + } + $this->messageManager->addError(__('Advertisment does not exist')); + return $resultRedirect->setPath('*/*/'); + } + + /** + * {@inheritdoc} + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Salecto_Advertisment::Advertisment_delete'); + } +} diff --git a/Controller/Adminhtml/Adverts/Add.php b/Controller/Adminhtml/Adverts/Add.php new file mode 100644 index 0000000..41fd384 --- /dev/null +++ b/Controller/Adminhtml/Adverts/Add.php @@ -0,0 +1,41 @@ +resultPageFactory = $resultPageFactory; + parent::__construct($context); + } + + /** + * Page Load Action + * + * @return \Magento\Framework\View\Result\Page + */ + public function execute() + { + $resultPage = $this->resultPageFactory->create(); + $resultPage->setActiveMenu('Salecto_Advertisment::advertisment'); + $resultPage->getConfig()->getTitle()->prepend(__('Add Advertisment')); + return $resultPage; + } +} diff --git a/Controller/Adminhtml/Adverts/Delete.php b/Controller/Adminhtml/Adverts/Delete.php new file mode 100644 index 0000000..7b10e8c --- /dev/null +++ b/Controller/Adminhtml/Adverts/Delete.php @@ -0,0 +1,104 @@ +fileSystem = $fileSystem; + $this->file = $file; + $this->_model = $model; + parent::__construct($context); + } + + /** + * Delete action + * + * @return \Magento\Framework\Controller\ResultInterface + */ + public function execute() + { + $id = $this->getRequest()->getParam('ad_id'); + /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ + $resultRedirect = $this->resultRedirectFactory->create(); + if ($id) { + try { + $model = $this->_model; + $model->load($id); + $this->deleteFile($model->getAdImg()); + $model->delete(); + $this->messageManager->addSuccess(__('Advertisment Deteted')); + return $resultRedirect->setPath('*/*/'); + } catch (\Exception $e) { + $this->messageManager->addError($e->getMessage()); + return $resultRedirect->setPath('*/*/edit', ['id' => $id]); + } + } + $this->messageManager->addError(__('Advertisment does not exist')); + return $resultRedirect->setPath('*/*/'); + } + + /** + * Delete image from folder + * @param string $fileName + * @return bool + */ + public function deleteFile($fileName) + { + $path = 'salecto/advertisment/'; + $mediaRootDir = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath().$path; + try{ + if ($this->file->isExists($mediaRootDir . $fileName)) { + return $this->file->deleteFile($mediaRootDir . $fileName); + } + }catch (\Exception $e) { + $this->messageManager->addException($e, __('Can\'t delete the file')); + } + } + + /** + * {@inheritdoc} + * @return bool + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Salecto_Advertisment::Advertisment_delete'); + } +} diff --git a/Controller/Adminhtml/Adverts/Image/Upload.php b/Controller/Adminhtml/Adverts/Image/Upload.php new file mode 100644 index 0000000..fb8a106 --- /dev/null +++ b/Controller/Adminhtml/Adverts/Image/Upload.php @@ -0,0 +1,57 @@ +imageUploader = $imageUploader; + } + /** + * Upload file controller action + * + * @return ResultInterface + */ + public function execute() + { + $imageId = $this->_request->getParam('param_name', 'ad_img'); + + try { + $result = $this->imageUploader->saveFileToTmpDir($imageId); + } catch (Exception $e) { + $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()]; + } + return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result); + } +} diff --git a/Controller/Adminhtml/Adverts/Inactive.php b/Controller/Adminhtml/Adverts/Inactive.php new file mode 100644 index 0000000..66b80a5 --- /dev/null +++ b/Controller/Adminhtml/Adverts/Inactive.php @@ -0,0 +1,66 @@ +_model = $model; + } + + /** + * status inactive action + * + * @return \Magento\Framework\Controller\ResultInterface + */ + public function execute() + { + $id = $this->getRequest()->getParam('ad_id'); + /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ + $resultRedirect = $this->resultRedirectFactory->create(); + if ($id) { + try { + $model = $this->_model; + $model->load($id); + $model->setAdStatus(0)->save(); + $this->messageManager->addSuccess(__('Advertisment Inactivated')); + return $resultRedirect->setPath('*/*/'); + } catch (\Exception $e) { + $this->messageManager->addError($e->getMessage()); + return $resultRedirect->setPath('*/*/edit', ['id' => $id]); + } + } + $this->messageManager->addError(__('Advertisment does not exist')); + return $resultRedirect->setPath('*/*/'); + } + + /** + * {@inheritdoc} + * @return bool + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Salecto_Advertisment::Advertisment_inactive'); + } +} diff --git a/Controller/Adminhtml/Adverts/Index.php b/Controller/Adminhtml/Adverts/Index.php new file mode 100644 index 0000000..2537b38 --- /dev/null +++ b/Controller/Adminhtml/Adverts/Index.php @@ -0,0 +1,49 @@ +resultPageFactory = $resultPageFactory; + } + + /** + * @return \Magento\Backend\Model\View\Result\Page + */ + public function execute() + { + $resultPage = $this->resultPageFactory->create(); + $resultPage->setActiveMenu('Salecto_Advertisment::advertisment'); + $resultPage->getConfig()->getTitle()->prepend(__('Advertisments')); + return $resultPage; + } + + /** + * {@inheritdoc} + * @return bool + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Salecto_Advertisment::advertisment'); + } +} diff --git a/Controller/Adminhtml/Adverts/MassDelete.php b/Controller/Adminhtml/Adverts/MassDelete.php new file mode 100644 index 0000000..9cb8980 --- /dev/null +++ b/Controller/Adminhtml/Adverts/MassDelete.php @@ -0,0 +1,107 @@ +_filter = $filter; + $this->_collectionFactory = $collectionFactory; + $this->fileSystem = $fileSystem; + $this->file = $file; + parent::__construct($context); + } + + /** + * @return \Magento\Backend\Model\View\Result\Redirect + */ + public function execute() + { + $collection = $this->_filter->getCollection($this->_collectionFactory->create()); + $recordDeleted = 0; + foreach ($collection->getItems() as $record) { + $this->deleteFile($record->getAdImg()); + $record->delete(); + $recordDeleted++; + } + $this->messageManager->addSuccess(__('A total of %1 advertisment(s) activated.', $recordDeleted)); + + return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/index'); + } + + /** + * Execute the command + * + * @param string $fileName + * @throws \Exception + * @return bool + */ + public function deleteFile($fileName) + { + $path = 'salecto/advertisment/'; + $mediaRootDir = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath().$path; + try{ + if ($this->file->isExists($mediaRootDir . $fileName)) { + return $this->file->deleteFile($mediaRootDir . $fileName); + } + }catch (\Exception $e) { + $this->messageManager->addException($e, __('Can\'t delete the file')); + } + } + + /** + * {@inheritdoc} + * @return bool + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Salecto_Advertisment::advertisment_massinactive'); + } +} diff --git a/Controller/Adminhtml/Adverts/MassStatus.php b/Controller/Adminhtml/Adverts/MassStatus.php new file mode 100644 index 0000000..cb3876a --- /dev/null +++ b/Controller/Adminhtml/Adverts/MassStatus.php @@ -0,0 +1,72 @@ +_filter = $filter; + $this->_collectionFactory = $collectionFactory; + parent::__construct($context); + } + + /** + * Advertisment mass status changed (active/inactive) + * @return \Magento\Backend\Model\View\Result\Redirect + */ + public function execute() + { + ($this->getRequest()->getParam('status')) ? $status = 'Activated' : $status = 'Inactivated'; + $collection = $this->_filter->getCollection($this->_collectionFactory->create()); + $recordActivated = 0; + foreach ($collection->getItems() as $record) { + $record->setAdStatus($this->getRequest()->getParam('status'))->save(); + $recordActivated++; + } + $this->messageManager->addSuccess(__('A total of %1 advertisment(s) %2.', $recordActivated, $status)); + + return $this->resultFactory->create(ResultFactory::TYPE_REDIRECT)->setPath('*/*/index'); + } + + /** + * {@inheritdoc} + * @return bool + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Salecto_Advertisment::advertisment_massinactive'); + } +} diff --git a/Controller/Adminhtml/Adverts/Save.php b/Controller/Adminhtml/Adverts/Save.php new file mode 100644 index 0000000..677f818 --- /dev/null +++ b/Controller/Adminhtml/Adverts/Save.php @@ -0,0 +1,127 @@ +gridFactory = $gridFactory; + $this->imgUpload = $ImgUpload; + $this->fileSystem = $fileSystem; + $this->file = $file; + } + + /** + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * @SuppressWarnings(PHPMD.NPathComplexity) + * Advertisment save/update + * @return \Magento\Backend\Model\View\Result\Redirect + */ + public function execute() + { + $data = $this->getRequest()->getPostValue(); + if (!$data) { + $this->_redirect('*/*/index'); + $this->messageManager->addSuccess(__('No form Data Found.')); + return; + } + + + if (isset($data['ad_img'][0]['name']) && isset($data['ad_img'][0]['tmp_name'])) { + $data['ad_img'] = $data['ad_img'][0]['name']; + $this->imgUpload->moveFileFromTmp($data['ad_img']); + if(isset($data['ad_id'])){ + $loadImage = $this->gridFactory->load($data['ad_id']); + $getImage = $loadImage->getAdImg(); + $this->deleteFile($getImage); + } + } elseif (isset($data['ad_img'][0]['name']) && !isset($data['ad_img'][0]['tmp_name'])) { + $data['ad_img'] = $data['ad_img'][0]['name']; + } else { + $data['ad_img'] = ''; + } + + $data['ad_pages'] = (!empty($data['ad_pages'])) ? implode(",", $data['ad_pages']) : null; + + try { + $this->gridFactory->setData($data)->save(); + $this->messageManager->addSuccess(__('Advertisment saved successfully.')); + } catch (\Exception $e) { + $this->messageManager->addError(__($e->getMessage())); + } + $this->_redirect('*/*/index'); + } + + /** + * Delete image from folder + * + * @return bool + */ + public function deleteFile($fileName) + { + $path = 'salecto/advertisment/'; + $mediaRootDir = $this->fileSystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath().$path; + try{ + if ($this->file->isExists($mediaRootDir . $fileName)) { + return $this->file->deleteFile($mediaRootDir . $fileName); + } + }catch (\Exception $e) { + $this->messageManager->addException($e, __('Can\'t delete the file')); + } + } + + /** + * {@inheritdoc} + * @return bool + */ + protected function _isAllowed() + { + return $this->_authorization->isAllowed('Salecto_Advertisment::save'); + } +} diff --git a/Controller/Index/Index.php b/Controller/Index/Index.php new file mode 100644 index 0000000..daae0ac --- /dev/null +++ b/Controller/Index/Index.php @@ -0,0 +1,36 @@ +_pageFactory = $pageFactory; + return parent::__construct($context); + } + + /** + * @return \Magento\Backend\Model\View\Result\Redirect + */ + public function execute() + { + return $this->_pageFactory->create(); + } +} diff --git a/Cron/InactiveAdverts.php b/Cron/InactiveAdverts.php new file mode 100644 index 0000000..0174f19 --- /dev/null +++ b/Cron/InactiveAdverts.php @@ -0,0 +1,60 @@ +_collectionFactory = $collectionFactory; + $this->logger = $logger; + $this->_DateTime = $DateTime; + } + + /** + * Retrieve collection of 'advertisments', loop collection condition, item which * are not within date range and set status to 1 (inactive). + * @return null + */ + public function execute() + { + $currentTime = $this->_DateTime->date(); + $collection = $this->_collectionFactory->create(); + foreach ($collection->getItems() as $record) { + if ((($record->getFromDate() <= $currentTime) && ($record->getToDate() >= $currentTime))===false) + { + $record->setAdStatus(0)->save(); + } + } + $this->logger->info('All Status Deactivated'); + } +} diff --git a/Model/DataProvider.php b/Model/DataProvider.php new file mode 100644 index 0000000..ea7375e --- /dev/null +++ b/Model/DataProvider.php @@ -0,0 +1,99 @@ +collection = $formCollectionFactory->create(); + $this->_file = $file; + parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data); + $this->_storeManager = $storeManager; + } + + /** + * Creates data for for admin ui form (including image data [name,url,size]) + * + * @return array + */ + public function getData() + { + if (isset($this->loadedData)) { + return $this->loadedData; + } + $items = $this->collection->getItems(); + foreach ($items as $advert) { + $fileName = 'salecto/advertisment/'.$advert->getData("ad_img"); + $fileSize = $this->_file->getFileSize($fileName); + if ($advert->getData("ad_img") !== '') { + $image = []; + $image[0]['name'] = $advert->getData("ad_img"); + $image[0]['url'] = $this->getMediaUrl($image[0]['name']); + $image[0]['size'] = $fileSize; + $advert->setData("ad_img",$image); + } + $this->loadedData[$advert->getId()] = $advert->getData(); + } + return $this->loadedData; + } + + /** + * creates media url along with advertisment image name and folder path. + * @param string $imgName + * @return string + */ + public function getMediaUrl($imgName) + { + $mediaUrl = $this->_storeManager->getStore() + ->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA).'salecto/advertisment/'.$imgName; + return $mediaUrl; + } + +} diff --git a/Model/GridModel.php b/Model/GridModel.php new file mode 100644 index 0000000..c8920aa --- /dev/null +++ b/Model/GridModel.php @@ -0,0 +1,217 @@ +_init('Salecto\Advertisment\Model\ResourceModel\GridModel'); + } + /** + * Get EntityId. + * + * @return int + */ + public function getEntityId() + { + return $this->getData(self::ENTITY_ID); + } + + /** + * Set EntityId. + * + * @param int $entityId + * @return void + */ + public function setEntityId($entityId) + { + return $this->setData(self::ENTITY_ID, $entityId); + } + + /** + * Get Title. + * + * @return varchar + */ + public function getTitle() + { + return $this->getData(self::TITLE); + } + + /** + * Set Title. + * + * @param int $title + * @return void + */ + public function setTitle($title) + { + return $this->setData(self::TITLE, $title); + } + + /** + * Get getContent. + * + * @return varchar + */ + public function getContent() + { + return $this->getData(self::CONTENT); + } + + /** + * Set Content. + * + * @param int $content + * @return void + */ + public function setContent($content) + { + return $this->setData(self::CONTENT, $content); + } + + /** + * Get PublishDate. + * + * @return varchar + */ + public function getPublishDate() + { + return $this->getData(self::PUBLISH_DATE); + } + + /** + * Set PublishDate. + * + * @param int $publishDate + * @return void + */ + public function setPublishDate($publishDate) + { + return $this->setData(self::PUBLISH_DATE, $publishDate); + } + + /** + * Get IsActive. + * + * @return bool + */ + public function getIsActive() + { + return $this->getData(self::IS_ACTIVE); + } + + /** + * Set IsActive. + * + * @param int $isActive + * @return void + */ + public function setIsActive($isActive) + { + return $this->setData(self::IS_ACTIVE, $isActive); + } + + /** + * Get UpdateTime. + * + * @return varchar + */ + public function getUpdateTime() + { + return $this->getData(self::UPDATE_TIME); + } + + /** + * Set UpdateTime. + * + * @param int $updatetime + * @return void + */ + public function setUpdateTime($updateTime) + { + return $this->setData(self::UPDATE_TIME, $updateTime); + } + + /** + * Get CreatedAt. + * + * @return varchar + */ + public function getAdPages() + { + return $this->getData(self::PAGES); + } + + /** + * Set CreatedAt. + * + * @param int $createdAt + * @return void + */ + public function setAdPages($createdAt) + { + return $this->setData(self::PAGES, $createdAt); + } + + + /** + * Get CreatedAt. + * + * @return varchar + */ + public function getCreatedAt() + { + return $this->getData(self::CREATED_AT); + } + + /** + * Set CreatedAt. + * + * @param int $createdAt + * @return void + */ + public function setCreatedAt($createdAt) + { + return $this->setData(self::CREATED_AT, $createdAt); + } +} diff --git a/Model/ImageUploader.php b/Model/ImageUploader.php new file mode 100644 index 0000000..8d04582 --- /dev/null +++ b/Model/ImageUploader.php @@ -0,0 +1,304 @@ +coreFileStorageDatabase = $coreFileStorageDatabase; + $this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA); + $this->uploaderFactory = $uploaderFactory; + $this->storeManager = $storeManager; + $this->logger = $logger; + $this->baseTmpPath = $baseTmpPath; + $this->basePath = $basePath; + $this->allowedExtensions = $allowedExtensions; + $this->allowedMimeTypes = $allowedMimeTypes; + } + + /** + * Set base tmp path + * + * @param string $baseTmpPath + * + * @return void + */ + public function setBaseTmpPath($baseTmpPath) + { + $this->baseTmpPath = $baseTmpPath; + } + + /** + * Set base path + * + * @param string $basePath + * + * @return void + */ + public function setBasePath($basePath) + { + $this->basePath = $basePath; + } + + /** + * Set allowed extensions + * + * @param string[] $allowedExtensions + * + * @return void + */ + public function setAllowedExtensions($allowedExtensions) + { + $this->allowedExtensions = $allowedExtensions; + } + + /** + * Retrieve base tmp path + * + * @return string + */ + public function getBaseTmpPath() + { + return $this->baseTmpPath; + } + + /** + * Retrieve base path + * + * @return string + */ + public function getBasePath() + { + return $this->basePath; + } + + /** + * Retrieve allowed extensions + * + * @return string[] + */ + public function getAllowedExtensions() + { + return $this->allowedExtensions; + } + + /** + * Retrieve path + * + * @param string $path + * @param string $imageName + * + * @return string + */ + public function getFilePath($path, $imageName) + { + return rtrim($path, '/') . '/' . ltrim($imageName, '/'); + } + + /** + * Checking file for moving and move it + * + * @param string $imageName + * + * @return string + * + * @throws LocalizedException + */ + public function moveFileFromTmp($imageName) + { + $baseTmpPath = $this->getBaseTmpPath(); + $basePath = $this->getBasePath(); + + $baseImagePath = $this->getFilePath( + $basePath, + Uploader::getNewFileName( + $this->mediaDirectory->getAbsolutePath( + $this->getFilePath($basePath, $imageName) + ) + ) + ); + $baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName); + + try { + $this->coreFileStorageDatabase->copyFile( + $baseTmpImagePath, + $baseImagePath + ); + $this->mediaDirectory->renameFile( + $baseTmpImagePath, + $baseImagePath + ); + } catch (Exception $e) { + throw new LocalizedException( + __('Something went wrong while saving the file(s).') + ); + } + + return $imageName; + } + + /** + * Checking file for save and save it to tmp dir + * + * @param string $fileId + * + * @return string[] + * + * @throws LocalizedException + */ + public function saveFileToTmpDir($fileId) + { + $baseTmpPath = $this->getBaseTmpPath(); + + /** @var \Magento\MediaStorage\Model\File\Uploader $uploader */ + $uploader = $this->uploaderFactory->create(['fileId' => $fileId]); + $uploader->setAllowedExtensions($this->getAllowedExtensions()); + $uploader->setAllowRenameFiles(true); + if (!$uploader->checkMimeType($this->allowedMimeTypes)) { + throw new LocalizedException(__('File validation failed.')); + } + $result = $uploader->save($this->mediaDirectory->getAbsolutePath($baseTmpPath)); + unset($result['path']); + + if (!$result) { + throw new LocalizedException( + __('File can not be saved to the destination folder.') + ); + } + + /** + * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS + */ + $result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']); + $result['url'] = $this->storeManager + ->getStore() + ->getBaseUrl( + UrlInterface::URL_TYPE_MEDIA + ) . $this->getFilePath($baseTmpPath, $result['file']); + $result['name'] = $result['file']; + + if (isset($result['file'])) { + try { + $relativePath = rtrim($baseTmpPath, '/') . '/' . ltrim($result['file'], '/'); + $this->coreFileStorageDatabase->saveFile($relativePath); + } catch (Exception $e) { + $this->logger->critical($e); + throw new LocalizedException( + __('Something went wrong while saving the file(s).') + ); + } + } + + return $result; + } +} diff --git a/Model/ResourceModel/GridModel.php b/Model/ResourceModel/GridModel.php new file mode 100644 index 0000000..9d791e1 --- /dev/null +++ b/Model/ResourceModel/GridModel.php @@ -0,0 +1,48 @@ +_date = $date; + } + + /** + * Initialize resource model. + * @return object + */ + protected function _construct() + { + $this->_init('salecto_advert', 'ad_id'); + } +} diff --git a/Model/ResourceModel/GridModel/Collection.php b/Model/ResourceModel/GridModel/Collection.php new file mode 100644 index 0000000..42c4e00 --- /dev/null +++ b/Model/ResourceModel/GridModel/Collection.php @@ -0,0 +1,30 @@ +_init( + 'Salecto\Advertisment\Model\GridModel', + 'Salecto\Advertisment\Model\ResourceModel\GridModel' + ); + } +} diff --git a/Model/Status.php b/Model/Status.php new file mode 100644 index 0000000..95ff763 --- /dev/null +++ b/Model/Status.php @@ -0,0 +1,56 @@ + __('Active'),'0' => __('Inactive')]; + return $options; + } + + /** + * Get Grid row status labels array with empty value for option element. + * + * @return array + */ + public function getAllOptions() + { + $res = $this->getOptions(); + array_unshift($res, ['value' => '', 'label' => '']); + return $res; + } + + /** + * Get Grid row type array for option element. + * @return array + */ + public function getOptions() + { + $res = []; + foreach ($this->getOptionArray() as $index => $value) { + $res[] = ['value' => $index, 'label' => $value]; + } + return $res; + } + + /** + * {@inheritdoc} + */ + public function toOptionArray() + { + return $this->getOptions(); + } +} diff --git a/Plugin/AdvertMenu.php b/Plugin/AdvertMenu.php new file mode 100644 index 0000000..051c1e6 --- /dev/null +++ b/Plugin/AdvertMenu.php @@ -0,0 +1,91 @@ +nodeFactory = $nodeFactory; + $this->urlBuilder = $urlBuilder; + } + + /** + * Before GetHtml handler + * + * @param object \Magento\Theme\Block\Html\Topmenu $subject + * @param string $outermostClass + * @param string $childrenWrapClass + * @param int $limit + */ + public function beforeGetHtml( + \Magento\Theme\Block\Html\Topmenu $subject, + $outermostClass = '', + $childrenWrapClass = '', + $limit = 0 + ) { + /** + * Parent Menu + */ + $menuNode = $this->nodeFactory->create( + [ + 'data' => $this->getNodeAsArray("Advertisment", "adverts"), + 'idField' => 'id', + 'tree' => $subject->getMenu()->getTree(), + ] + ); + /** + * Add Child Menu + */ + /*$menuNode->addChild( + $this->nodeFactory->create( + [ + 'data' => $this->getNodeAsArray("Sub Menu", "sub-menu"), + 'idField' => 'id', + 'tree' => $subject->getMenu()->getTree(), + ] + ) + );*/ + $subject->getMenu()->addChild($menuNode); + } + + /** + * creates value array for '$menuNode' + * string $name (menu Text) + * string $id (menu url) + * @return array + */ + protected function getNodeAsArray($name, $id) { + $url = $this->urlBuilder->getUrl($id); + return [ + 'name' => __($name), + 'id' => $id, + 'url' => $url, + 'has_active' => false, + 'is_active' => false, + ]; + } +} diff --git a/Ui/Component/Listing/Column/AdCondition.php b/Ui/Component/Listing/Column/AdCondition.php new file mode 100644 index 0000000..914f182 --- /dev/null +++ b/Ui/Component/Listing/Column/AdCondition.php @@ -0,0 +1,67 @@ +_DateTime = $DateTime; + parent::__construct($context, $uiComponentFactory, $components, $data); + } + + /** + * Prepare Data Source + * + * @param array $dataSource + * + * @return array + */ + public function prepareDataSource(array $dataSource) + { + $customColumnName = $this->getData('name'); + + if (isset($dataSource['data']['items'])) { + foreach($dataSource['data']['items'] as &$item) { + $item[$customColumnName] = $this->getAdCondition($item); + } + } + return $dataSource; + } + + /** + * Conditional function to set key and value in datasource array + * based on date range if condition. + * + * @param array $item + * + * @return array key and value. + */ + public function getAdCondition($item){ + $currentTime = $this->_DateTime->date(); + $adCondition = ((($item['from_date'] <= $currentTime) && ($item['to_date'] >= $currentTime))===false) ? 'Not in date range' : 'In date range'; + return $adCondition; + } +} diff --git a/Ui/Component/Listing/Column/GridActions.php b/Ui/Component/Listing/Column/GridActions.php new file mode 100644 index 0000000..88d2c44 --- /dev/null +++ b/Ui/Component/Listing/Column/GridActions.php @@ -0,0 +1,99 @@ +urlBuilder = $urlBuilder; + parent::__construct($context, $uiComponentFactory, $components, $data); + } + + /** + * Built action Urls + * + * @param array|int|null $ids + * @return SourceProviderInterface + */ + public function prepareDataSource(array $dataSource) + { + if (isset($dataSource['data']['items'])) { + foreach ($dataSource['data']['items'] as &$item) { + if (isset($item['ad_id'])) { + $item[$this->getData('name')] = [ + 'edit' => [ + 'href' => $this->urlBuilder->getUrl( + static::URL_EDIT_PATH, + [ + 'ad_id' => $item['ad_id' + ], + ] + ), + 'label' => __('Edit'), + ], + 'delete' => [ + 'href' => $this->urlBuilder->getUrl( + static::URL_DELETE_PATH, + [ + 'ad_id' => $item['ad_id' + ], + ] + ), + 'label' => __('Delete'), + ], + 'active' => [ + 'href' => $this->urlBuilder->getUrl( + static::URL_ACTIVE_PATH, + [ + 'ad_id' => $item['ad_id' + ], + ] + ), + 'label' => __('Active'), + ], + 'inactive' => [ + 'href' => $this->urlBuilder->getUrl( + static::URL_INACTIVE_PATH, + [ + 'ad_id' => $item['ad_id' + ], + ] + ), + 'label' => __('Inactive'), + ], + ]; + } + } + } + return $dataSource; + } +} diff --git a/Ui/Component/Listing/Column/Thumbnail.php b/Ui/Component/Listing/Column/Thumbnail.php new file mode 100644 index 0000000..34391ee --- /dev/null +++ b/Ui/Component/Listing/Column/Thumbnail.php @@ -0,0 +1,79 @@ +storeManager = $storeManager; + $this->imageHelper = $imageHelper; + $this->urlBuilder = $urlBuilder; + parent::__construct($context, $uiComponentFactory, $components, $data); + } + + /** + * Prepare Data Source + * + * @param array $dataSource + * + * @return array + */ + public function prepareDataSource(array $dataSource) + { + if (isset($dataSource['data']['items'])) { + $fieldName = $this->getData('name'); + foreach($dataSource['data']['items'] as &$item) { + $url = ''; + if($item[$fieldName] != '') { + $url = $this->storeManager->getStore()->getBaseUrl( + UrlInterface::URL_TYPE_MEDIA + ) . 'salecto/advertisment/' . $item[$fieldName]; + } + $item[$fieldName . '_src'] = $url; + $item[$fieldName . '_orig_src'] = $url; + } + } + + return $dataSource; + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8ffe3d3 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "salecto2/magento2-advertisment", + "description": "Salecto Advertisment Module", + "type": "magento2-module", + "version": "1.0.0", + "license": [ + "OSL-3.0", + "AFL-3.0" + ], + "require": { + "php": "~7.3.0||~7.4.0", + "magento/framework": "102.0.*|103.0.*", + "salecto2/magento2-admin-theme": "*" + }, + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Salecto\\Advertisment\\": "" + } + } +} diff --git a/etc/acl.xml b/etc/acl.xml new file mode 100644 index 0000000..503b8fc --- /dev/null +++ b/etc/acl.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/etc/adminhtml/menu.xml b/etc/adminhtml/menu.xml new file mode 100644 index 0000000..90a79d3 --- /dev/null +++ b/etc/adminhtml/menu.xml @@ -0,0 +1,20 @@ + + + + + + + diff --git a/etc/adminhtml/routes.xml b/etc/adminhtml/routes.xml new file mode 100644 index 0000000..13f281a --- /dev/null +++ b/etc/adminhtml/routes.xml @@ -0,0 +1,16 @@ + + + + + + + + + diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml new file mode 100644 index 0000000..508dcf2 --- /dev/null +++ b/etc/adminhtml/system.xml @@ -0,0 +1,34 @@ + + + + +
+ Salecto_Advertisment::salecto_advertisment +
+ + + + +
+ separator-top + + salecto + Salecto_Advertisment::configuration + + + + + Magento\Config\Model\Config\Source\Yesno + + +
+
+ +
diff --git a/etc/crontab.xml b/etc/crontab.xml new file mode 100644 index 0000000..2587385 --- /dev/null +++ b/etc/crontab.xml @@ -0,0 +1,8 @@ + + + + + * * * * * + + + diff --git a/etc/db_schema.xml b/etc/db_schema.xml new file mode 100644 index 0000000..fc172a9 --- /dev/null +++ b/etc/db_schema.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + +
+
diff --git a/etc/di.xml b/etc/di.xml new file mode 100644 index 0000000..b66855f --- /dev/null +++ b/etc/di.xml @@ -0,0 +1,74 @@ + + + + + + + + salecto_advert + Salecto\Advertisment\Model\ResourceModel\GridModel + + + + + + Salecto\Advertisment\Model\ResourceModel\GridModel\GridModel\Collection + + + + + + Magento\Framework\Filesystem\Driver\File + + + + + customLogHandler + + Salecto\Advertisment\Logger\Handler + + + + + + + salecto/tmp/advertisment + salecto/advertisment + + jpg + jpeg + gif + png + + + image/jpg + image/jpeg + image/gif + image/png + + + + + + + Salecto\Advertisment\ReferenceImageUpload + + + + + + + + Salecto\Advertisment\Console\RfGen + Salecto\Advertisment\Console\Adverts + + + + + diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml new file mode 100644 index 0000000..1df9550 --- /dev/null +++ b/etc/frontend/di.xml @@ -0,0 +1,13 @@ + + + + + + + diff --git a/etc/frontend/routes.xml b/etc/frontend/routes.xml new file mode 100644 index 0000000..91f20a6 --- /dev/null +++ b/etc/frontend/routes.xml @@ -0,0 +1,16 @@ + + + + + + + + + diff --git a/etc/module.xml b/etc/module.xml new file mode 100644 index 0000000..8d99cd1 --- /dev/null +++ b/etc/module.xml @@ -0,0 +1,13 @@ + + + + + + diff --git a/readme-images/admin-form.png b/readme-images/admin-form.png new file mode 100644 index 0000000..06a0733 Binary files /dev/null and b/readme-images/admin-form.png differ diff --git a/readme-images/admin-grid.png b/readme-images/admin-grid.png new file mode 100644 index 0000000..88aba49 Binary files /dev/null and b/readme-images/admin-grid.png differ diff --git a/readme-images/admin-option.png b/readme-images/admin-option.png new file mode 100644 index 0000000..cacc99f Binary files /dev/null and b/readme-images/admin-option.png differ diff --git a/readme-images/config-option.png b/readme-images/config-option.png new file mode 100644 index 0000000..b8ec259 Binary files /dev/null and b/readme-images/config-option.png differ diff --git a/readme-images/edit-form.png b/readme-images/edit-form.png new file mode 100644 index 0000000..7e12044 Binary files /dev/null and b/readme-images/edit-form.png differ diff --git a/readme-images/front-end.png b/readme-images/front-end.png new file mode 100644 index 0000000..be4bbfa Binary files /dev/null and b/readme-images/front-end.png differ diff --git a/registration.php b/registration.php new file mode 100644 index 0000000..0a4c3c0 --- /dev/null +++ b/registration.php @@ -0,0 +1,12 @@ + + + + + + + + + + diff --git a/view/adminhtml/layout/advertisment_adverts_index.xml b/view/adminhtml/layout/advertisment_adverts_index.xml new file mode 100644 index 0000000..80eabe0 --- /dev/null +++ b/view/adminhtml/layout/advertisment_adverts_index.xml @@ -0,0 +1,16 @@ + + + + + + + + + diff --git a/view/adminhtml/ui_component/salecto_advert_form.xml b/view/adminhtml/ui_component/salecto_advert_form.xml new file mode 100644 index 0000000..6f4014d --- /dev/null +++ b/view/adminhtml/ui_component/salecto_advert_form.xml @@ -0,0 +1,232 @@ + + +
+ + + salecto_advert_form.salecto_advert_form_data_source + salecto_advert_form.salecto_advert_form_data_source + + + data + salecto_advert_form + + templates/form/collapsible + + + Save + Save Form + primary + + + + + + + + + +
+
+ x, + +
+
diff --git a/view/frontend/layout/default.xml b/view/frontend/layout/default.xml new file mode 100644 index 0000000..5c1b8e5 --- /dev/null +++ b/view/frontend/layout/default.xml @@ -0,0 +1,16 @@ + + + + + + + + + diff --git a/view/frontend/templates/adverts.phtml b/view/frontend/templates/adverts.phtml new file mode 100644 index 0000000..bc9ae28 --- /dev/null +++ b/view/frontend/templates/adverts.phtml @@ -0,0 +1,25 @@ +getRequest()->getFullActionName(); +$adverts = $block->getAdverts($currentPage); + +if(count($adverts) > 0): ?> +
+ '; + echo '
'; + echo "
" .$advert->getTitle(). "
"; + //echo "
" .$advert->getAdDescription(). "
"; + echo '
'; + } + ?> + + + +