├── .gitattributes ├── .github └── FUNDING.yml ├── Api └── Cms │ ├── SimpleBlock.php │ └── SimplePage.php ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── Setup └── Patch │ └── SimpleDataPatch.php ├── composer.json ├── etc └── module.xml └── registration.php /.gitattributes: -------------------------------------------------------------------------------- 1 | /docs export-ignore 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: markshust 4 | -------------------------------------------------------------------------------- /Api/Cms/SimpleBlock.php: -------------------------------------------------------------------------------- 1 | blockInterfaceFactory = $blockInterfaceFactory; 49 | $this->blockRepository = $blockRepository; 50 | $this->getBlockByIdentifier = $getBlockByIdentifier; 51 | $this->logger = $logger; 52 | } 53 | 54 | /** 55 | * Delete a block from a given identifier and optional store id. 56 | * @param string $identifier 57 | * @param int $storeId 58 | */ 59 | public function delete(string $identifier, int $storeId = Store::DEFAULT_STORE_ID): void 60 | { 61 | try { 62 | $block = $this->getBlockByIdentifier->execute($identifier, $storeId); 63 | } catch (NoSuchEntityException $e) { 64 | $this->logger->critical($e->getMessage()); 65 | return; 66 | } 67 | 68 | try { 69 | $this->blockRepository->delete($block); 70 | } catch (CouldNotDeleteException $e) { 71 | $this->logger->critical($e->getMessage()); 72 | } 73 | } 74 | 75 | /** 76 | * If the CMS block identifier is found, attempt to update the record. 77 | * If it is not found, attempt to create a new record. 78 | * @param array $data 79 | */ 80 | public function save(array $data): void 81 | { 82 | $identifier = $data['identifier']; 83 | $storeId = $data['store_id'] ?? Store::DEFAULT_STORE_ID; 84 | 85 | try { 86 | $block = $this->getBlockByIdentifier->execute($identifier, $storeId); 87 | } catch (NoSuchEntityException $e) { 88 | // Rather than throwing an exception, create a new block instance 89 | 90 | /** @var BlockInterface|AbstractModel $block */ 91 | $block = $this->blockInterfaceFactory->create(); 92 | $block->setIdentifier($identifier); 93 | 94 | // Set initial store data to "all stores" 95 | $block->setData('store_id', $storeId); 96 | $block->setData('stores', [$storeId]); 97 | } 98 | 99 | $elements = [ 100 | 'content', 101 | 'is_active', 102 | 'stores', 103 | 'title', 104 | ]; 105 | 106 | foreach ($elements as $element) { 107 | if (isset($data[$element])) { 108 | $block->setData($element, $data[$element]); 109 | } 110 | } 111 | 112 | try { 113 | $this->blockRepository->save($block); 114 | } catch (CouldNotSaveException $e) { 115 | $this->logger->critical($e->getMessage()); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /Api/Cms/SimplePage.php: -------------------------------------------------------------------------------- 1 | getPageByIdentifier = $getPageByIdentifier; 49 | $this->logger = $logger; 50 | $this->pageInterfaceFactory = $pageInterfaceFactory; 51 | $this->pageRepository = $pageRepository; 52 | } 53 | 54 | /** 55 | * Delete a page from a given identifier and optional store id. 56 | * @param string $identifier 57 | * @param int $storeId 58 | */ 59 | public function delete(string $identifier, int $storeId = Store::DEFAULT_STORE_ID): void 60 | { 61 | try { 62 | $page = $this->getPageByIdentifier->execute($identifier, $storeId); 63 | } catch (NoSuchEntityException $e) { 64 | $this->logger->critical($e->getMessage()); 65 | return; 66 | } 67 | 68 | try { 69 | $this->pageRepository->delete($page); 70 | } catch (CouldNotDeleteException $e) { 71 | $this->logger->critical($e->getMessage()); 72 | } 73 | } 74 | 75 | /** 76 | * If the CMS page identifier is found, attempt to update the record. 77 | * If it is not found, attempt to create a new record. 78 | * @param array $data 79 | */ 80 | public function save(array $data): void 81 | { 82 | $identifier = $data['identifier']; 83 | $storeId = $data['store_id'] ?? Store::DEFAULT_STORE_ID; 84 | 85 | try { 86 | $page = $this->getPageByIdentifier->execute($identifier, $storeId); 87 | } catch (NoSuchEntityException $e) { 88 | // Rather than throwing an exception, create a new page instance 89 | 90 | /** @var PageInterface|AbstractModel $page */ 91 | $page = $this->pageInterfaceFactory->create(); 92 | $page->setIdentifier($identifier); 93 | 94 | // Set initial store data to "all stores" 95 | $page->setData('store_id', $storeId); 96 | $page->setData('stores', [$storeId]); 97 | 98 | // Set a default page layout 99 | $page->setData('page_layout', '1column'); 100 | } 101 | 102 | $elements = [ 103 | 'content', 104 | 'content_heading', 105 | 'custom_layout_update_xml', 106 | 'custom_root_template', 107 | 'custom_theme', 108 | 'custom_theme_from', 109 | 'custom_theme_to', 110 | 'is_active', 111 | 'layout_update_selected', 112 | 'layout_update_xml', 113 | 'meta_description', 114 | 'meta_keywords', 115 | 'meta_title', 116 | 'page_layout', 117 | 'sort_order', 118 | 'stores', 119 | 'title', 120 | ]; 121 | 122 | foreach ($elements as $element) { 123 | if (isset($data[$element])) { 124 | $page->setData($element, $data[$element]); 125 | } 126 | } 127 | 128 | try { 129 | $this->pageRepository->save($page); 130 | } catch (CouldNotSaveException $e) { 131 | $this->logger->critical($e->getMessage()); 132 | } 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 5 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 6 | 7 | ## [2.0.2] - 2022-06-28 8 | 9 | ### Fixed 10 | - Change return type to match PatchInterface. 11 | 12 | ## [2.0.1] - 2020-01-13 13 | 14 | ### Fixed 15 | - Removed typed properties for PHP 7.3 support. 16 | 17 | ## [2.0.0] - 2020-01-12 18 | 19 | ### Added 20 | - Added type declarations to class properties. 21 | 22 | ### Removed 23 | - Removed `SimpleConfig` class_alias as it caused issues in production mode ([#4](https://github.com/markshust/magento2-module-simpledata/issues/4)). 24 | 25 | ## [1.0.0] - 2020-05-20 26 | 27 | ### Added 28 | - Initial release. 29 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |