diff --git a/Block/Banner.php b/Block/Banner.php new file mode 100644 index 0000000..ff67283 --- /dev/null +++ b/Block/Banner.php @@ -0,0 +1,20 @@ +scopeConfig = $scopeConfig; + $this->page = $page; + $this->dir = $dir; + } + + public function getPageBanner() { + $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE; + $image = (null == $this->page->getCmsBanner()) ? null : $this->scopeConfig->getValue('web/unsecure/base_url', $storeScope) . 'pub/media/cms/' . $this->page->getCmsBanner(); + return $image; + } +} diff --git a/Controller/Adminhtml/Cms/Image.php b/Controller/Adminhtml/Cms/Image.php new file mode 100644 index 0000000..1a5218a --- /dev/null +++ b/Controller/Adminhtml/Cms/Image.php @@ -0,0 +1,46 @@ +imageUploader = $imageUploader; + } + + + /** + * Upload file controller action + * + * @return \Magento\Framework\Controller\ResultInterface + */ + public function execute() + { + try { + $result = $this->imageUploader->saveFileToTmpDir('cms_banner'); + + $result['cookie'] = [ + 'name' => $this->_getSession()->getName(), + 'value' => $this->_getSession()->getSessionId(), + 'lifetime' => $this->_getSession()->getCookieLifetime(), + 'path' => $this->_getSession()->getCookiePath(), + 'domain' => $this->_getSession()->getCookieDomain(), + ]; + } catch (\Exception $e) { + $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()]; + } + return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result); + } + +} diff --git a/Controller/Adminhtml/Cms/Save.php b/Controller/Adminhtml/Cms/Save.php new file mode 100644 index 0000000..4f6d4bc --- /dev/null +++ b/Controller/Adminhtml/Cms/Save.php @@ -0,0 +1,98 @@ +getRequest()->getPostValue(); + + /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */ + $resultRedirect = $this->resultRedirectFactory->create(); + if ($data) { + $data = $this->dataProcessor->filter($data); + if (isset($data['is_active']) && $data['is_active'] === 'true') { + $data['is_active'] = Page::STATUS_ENABLED; + } + if (empty($data['page_id'])) { + $data['page_id'] = null; + } + + /** @var \Magento\Cms\Model\Page $model */ + $model = $this->_objectManager->create('Magento\Cms\Model\Page'); + + $id = $this->getRequest()->getParam('page_id'); + if ($id) { + $model->load($id); + } + + if(isset($data['cms_banner']) && is_array($data['cms_banner'])){ + if (isset($data['cms_banner'][0]['tmp_name'])) { + // delete old picture + $data['cms_banner']=$data['cms_banner'][0]['name']; + $uploader = $this->_objectManager->create('PrateekKarki\Cmsbanner\Controller\Adminhtml\Cms\Uploader'); + $uploader->moveFileFromTmp($data['cms_banner']); + } + } + + + $model->setData($data); + + $this->_eventManager->dispatch( + 'cms_page_prepare_save', + ['page' => $model, 'request' => $this->getRequest()] + ); + + if (!$this->dataProcessor->validate($data)) { + return $resultRedirect->setPath('*/*/edit', ['page_id' => $model->getId(), '_current' => true]); + } + + try { + $model->save(); + $this->messageManager->addSuccess(__('You saved the page.')); + $this->dataPersistor->clear('cms_page'); + if ($this->getRequest()->getParam('back')) { + return $resultRedirect->setPath('*/*/edit', ['page_id' => $model->getId(), '_current' => true]); + } + return $resultRedirect->setPath('*/*/'); + } catch (LocalizedException $e) { + $this->messageManager->addError($e->getMessage()); + } catch (\Exception $e) { + $this->messageManager->addException($e, __('Something went wrong while saving the page.')); + } + + $this->dataPersistor->set('cms_page', $data); + return $resultRedirect->setPath('*/*/edit', ['page_id' => $this->getRequest()->getParam('page_id')]); + } + return $resultRedirect->setPath('*/*/'); + } +} +?> diff --git a/Controller/Adminhtml/Cms/Uploader.php b/Controller/Adminhtml/Cms/Uploader.php new file mode 100644 index 0000000..827c8c7 --- /dev/null +++ b/Controller/Adminhtml/Cms/Uploader.php @@ -0,0 +1,17 @@ +" or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. diff --git a/Model/Cms/DataProvider.php b/Model/Cms/DataProvider.php new file mode 100644 index 0000000..b0f8e75 --- /dev/null +++ b/Model/Cms/DataProvider.php @@ -0,0 +1,51 @@ +loadedData)) + return $this->loadedData; + + $items = $this->collection->getItems(); + foreach ($items as $page) { + $this->loadedData[$page->getId()] = $page->getData(); + } + + $data = $this->dataPersistor->get('cms_page'); + + if (!empty($data)) { + $page = $this->collection->getNewEmptyItem(); + + $page->setData($data); + $this->loadedData[$page->getId()] = $page->getData(); + $this->dataPersistor->clear('cms_page'); + } + + if(!empty($this->loadedData[$page->getId()]['cms_banner'])){ + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + $storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface'); + $currentStore = $storeManager->getStore(); + $media_url=$currentStore->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); + + $image_name=$this->loadedData[$page->getId()]['cms_banner']; + unset($this->loadedData[$page->getId()]['cms_banner']); + $this->loadedData[$page->getId()]['cms_banner'][0]['name']=$image_name; + $this->loadedData[$page->getId()]['cms_banner'][0]['url']=$media_url."cms/".$image_name; + } + return $this->loadedData; + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..1043c2c --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +CMS Banner +========================= +Magento Extension + +Convenient and easy to use extension if you need to add banners to your cms pages like about us or homepage in your Magento store. + + +Features +-------- + +* Adds an image uploader to the Magento backend, on the CMS edit forms +* Images uploaded in the backend are viewed in the frontend int their respective pages +* Easy to use to any magento installation + + + +Installation +-------- + +### ✓ Install via composer (recommend) +Run the following command in Magento 2 root folder: + +``` +composer require prateekkarki/module-cmsbanner +php bin/magento setup:upgrade +php bin/magento setup:static-content:deploy +``` diff --git a/Setup/InstallSchema.php b/Setup/InstallSchema.php new file mode 100644 index 0000000..290f19b --- /dev/null +++ b/Setup/InstallSchema.php @@ -0,0 +1,27 @@ +startSetup(); + $connection = $installer->getConnection(); + + $connection->addColumn('cms_page','cms_banner',['type' =>\Magento\Framework\DB\Ddl\Table::TYPE_TEXT,'comment' => 'Page Banner']); + $installer->endSetup(); + + + $setup->endSetup(); + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c21005e --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "prateekkarki/module-cmsbanner", + "description": "Magento 2 extension for adding banners to cms pages", + "type": "magento2-module", + "license": "OSL-3.0", + "authors": [ + { + "email": "pratiek.karki@gmail.com", + "name": "Prateek Karki" + } + ], + "keywords": ["magento2", "cms", "banner"], + "repositories": [ + { + "type": "composer", + "url": "https://repo.magento.com/" + } + ], + "require": { + "magento/framework": ">=101.0.0" + }, + "autoload": { + "psr-4": { + "PrateekKarki\\Cmsbanner\\": "" + }, + "files": [ + "registration.php" + ] + }, + "version": "1.0.0", + "minimum-stability": "dev", + "prefer-stable": true +} diff --git a/etc/adminhtml/di.xml b/etc/adminhtml/di.xml new file mode 100644 index 0000000..a027d94 --- /dev/null +++ b/etc/adminhtml/di.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/etc/adminhtml/routes.xml b/etc/adminhtml/routes.xml new file mode 100644 index 0000000..c6ed246 --- /dev/null +++ b/etc/adminhtml/routes.xml @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/etc/module.xml b/etc/module.xml new file mode 100644 index 0000000..b626036 --- /dev/null +++ b/etc/module.xml @@ -0,0 +1,4 @@ + + + + diff --git a/registration.php b/registration.php new file mode 100644 index 0000000..efc3a4e --- /dev/null +++ b/registration.php @@ -0,0 +1,6 @@ + +
+ +
+ +
+
+ + + + string + category + Page Banner + true + fileUploader + ui/form/element/uploader/uploader + PrateekKarki_Cmsbanner/image-preview + false + + + + + + +
+
diff --git a/view/adminhtml/web/template/image-preview.html b/view/adminhtml/web/template/image-preview.html new file mode 100644 index 0000000..d7f77a8 --- /dev/null +++ b/view/adminhtml/web/template/image-preview.html @@ -0,0 +1,29 @@ +
+
+ + + + +
+ +
+
+ +
+
+ x +
+
diff --git a/view/frontend/layout/cms_page_view.xml b/view/frontend/layout/cms_page_view.xml new file mode 100644 index 0000000..df5ce32 --- /dev/null +++ b/view/frontend/layout/cms_page_view.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/view/frontend/layout/default_head_blocks.xml b/view/frontend/layout/default_head_blocks.xml new file mode 100644 index 0000000..8060322 --- /dev/null +++ b/view/frontend/layout/default_head_blocks.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/view/frontend/templates/page_banner.phtml b/view/frontend/templates/page_banner.phtml new file mode 100644 index 0000000..2668e10 --- /dev/null +++ b/view/frontend/templates/page_banner.phtml @@ -0,0 +1,14 @@ +
+ +
+getPageBanner())): ?> + + \ No newline at end of file diff --git a/view/frontend/web/styles/banner.css b/view/frontend/web/styles/banner.css new file mode 100644 index 0000000..99196cb --- /dev/null +++ b/view/frontend/web/styles/banner.css @@ -0,0 +1,6 @@ +.banner-image img{ + width: 100%; + height: auto; + margin: 0 auto; + padding: 0; +} \ No newline at end of file