diff --git a/Api/Data/NodeInterface.php b/Api/Data/NodeInterface.php index bf831ddd..113bfef9 100644 --- a/Api/Data/NodeInterface.php +++ b/Api/Data/NodeInterface.php @@ -18,6 +18,8 @@ interface NodeInterface const TARGET = 'target'; const IMAGE = 'image'; const IMAGE_ALT_TEXT = 'image_alt_text'; + const IMAGE_WIDTH = 'image_width'; + const IMAGE_HEIGHT = 'image_heigth'; const CREATION_TIME = 'creation_time'; const UPDATE_TIME = 'update_time'; const IS_ACTIVE = 'is_active'; @@ -192,18 +194,40 @@ public function setImage($image); /** * Get image alt text * - * @return string + * @return string|null */ public function getImageAltText(); /** * Set image alt text * - * @param string $altText + * @param string|null $altText * @return $this */ public function setImageAltText($altText); + /** + * @return int|null + */ + public function getImageWidth(); + + /** + * @param int|null $width + * @return $this + */ + public function setImageWidth($width); + + /** + * @return int|null + */ + public function getImageHeight(); + + /** + * @param int|null $height + * @return $this + */ + public function setImageHeight($height); + /** * Get creation time * diff --git a/Block/Adminhtml/Edit/Tab/Nodes.php b/Block/Adminhtml/Edit/Tab/Nodes.php index 1b55ebf9..17997069 100644 --- a/Block/Adminhtml/Edit/Tab/Nodes.php +++ b/Block/Adminhtml/Edit/Tab/Nodes.php @@ -179,6 +179,8 @@ private function renderNodeList($level, $parent, $data) 'image' => $node->getImage(), 'image_url' => $node->getImage() ? $this->imageFile->getUrl($node->getImage()) : null, 'image_alt_text' => $node->getImageAltText(), + 'image_width' => $node->getImageWidth(), + 'image_height' => $node->getImageHeight(), 'columns' => $this->renderNodeList($level + 1, $node->getId(), $data) ?: [], 'selected_item_id' => $node->getSelectedItemId() ]; diff --git a/Model/GraphQl/Resolver/DataProvider/Node.php b/Model/GraphQl/Resolver/DataProvider/Node.php index 9eba6cd1..4fc84e3b 100644 --- a/Model/GraphQl/Resolver/DataProvider/Node.php +++ b/Model/GraphQl/Resolver/DataProvider/Node.php @@ -103,6 +103,8 @@ private function convertData(NodeInterface $node): array NodeInterface::TARGET => (bool) $node->getTarget(), NodeInterface::IMAGE => $node->getImage(), NodeInterface::IMAGE_ALT_TEXT => $node->getImageAltText(), + NodeInterface::IMAGE_WIDTH => $node->getImageWidth(), + NodeInterface::IMAGE_HEIGHT => $node->getImageHeight(), self::TEMPLATE_FIELD => $node->getNodeTemplate(), self::SUBMENU_TEMPLATE_FIELD => $node->getSubmenuTemplate(), NodeInterface::CREATION_TIME => $node->getCreationTime(), diff --git a/Model/Menu/Node.php b/Model/Menu/Node.php index be2e040f..4e39d4a4 100644 --- a/Model/Menu/Node.php +++ b/Model/Menu/Node.php @@ -214,6 +214,38 @@ public function setImageAltText($altText) return $this->setData(NodeInterface::IMAGE_ALT_TEXT, $altText); } + /** + * @inheritdoc + */ + public function getImageWidth() + { + return $this->_getData(NodeInterface::IMAGE_WIDTH); + } + + /** + * @inheritdoc + */ + public function setImageWidth($width) + { + return $this->setData(NodeInterface::IMAGE_WIDTH, $width); + } + + /** + * @inheritdoc + */ + public function getImageHeight() + { + return $this->_getData(NodeInterface::IMAGE_HEIGHT); + } + + /** + * @inheritdoc + */ + public function setImageHeight($height) + { + return $this->setData(NodeInterface::IMAGE_HEIGHT, $height); + } + /** * @inheritdoc */ diff --git a/Model/Menu/Node/Image/File.php b/Model/Menu/Node/Image/File.php index e71ab8bb..2d5b0398 100644 --- a/Model/Menu/Node/Image/File.php +++ b/Model/Menu/Node/Image/File.php @@ -113,6 +113,14 @@ public function clone(string $file): string return $fileClonePath; } + public function getImageSize(string $file): array + { + $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); + $fileFullPath = $mediaDirectory->getAbsolutePath(self::PATH . $file); + + return getimagesize($fileFullPath); + } + private function getAbsolutePath(): string { $mediaDirectory = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA); diff --git a/Model/MenuManagement.php b/Model/MenuManagement.php index fc5a70b6..2c5ae695 100644 --- a/Model/MenuManagement.php +++ b/Model/MenuManagement.php @@ -101,6 +101,8 @@ private function getCategoriesNodeList($level, $parent, array $data): array 'node_template' => null, 'image' => null, 'image_alt_text' => null, + 'image_width' => null, + 'image_height' => null, 'submenu_template' => null, 'columns' => $this->getCategoriesNodeList($level + 1, $nodeId, $data) ?: [] ]; diff --git a/Service/Menu/SaveRequestProcessor.php b/Service/Menu/SaveRequestProcessor.php index 546feec8..3011bbab 100644 --- a/Service/Menu/SaveRequestProcessor.php +++ b/Service/Menu/SaveRequestProcessor.php @@ -202,14 +202,46 @@ private function processNodeObject( $nodeObject->setLevel((string) $level); $nodeObject->setPosition((string) $position); + $this->processImageParameters($nodeData, $nodeObject); + + $nodeObject->setSelectedItemId($nodeData['selected_item_id'] ?? null); + } + + private function processImageParameters(array $nodeData, NodeInterface &$nodeObject): void + { + $nodeObject->setImageAltText($nodeData[NodeInterface::IMAGE_ALT_TEXT] ?? null); + if ($nodeObject->getImage() && empty($nodeData['image'])) { $this->nodeImageFile->delete($nodeObject->getImage()); } - $nodeObject->setImage($nodeData['image'] ?? null); - $nodeObject->setImageAltText($nodeData['image_alt_text'] ?? null); + if (empty($nodeData[NodeInterface::IMAGE])) { + $nodeObject->setImageWidth(null); + $nodeObject->setImageHeight(null); + $nodeObject->setImage(null); + return; + } - $nodeObject->setSelectedItemId($nodeData['selected_item_id'] ?? null); + if (empty($nodeData[NodeInterface::IMAGE_WIDTH]) + || empty($nodeData[NodeInterface::IMAGE_HEIGHT]) + ) { + $imageSize = $this->nodeImageFile->getImageSize($nodeData[NodeInterface::IMAGE]); + + if (!empty($imageSize)) { + $nodeObject + ->setImageWidth( + !empty($nodeData[NodeInterface::IMAGE_WIDTH]) + ? $nodeData[NodeInterface::IMAGE_WIDTH] + : $imageSize[0] + ); + $nodeObject + ->setImageHeight( + !empty($nodeData[NodeInterface::IMAGE_HEIGHT]) + ? $nodeData[NodeInterface::IMAGE_HEIGHT] + : $imageSize[1] + ); + } + } } /** diff --git a/Setup/UpgradeSchema.php b/Setup/UpgradeSchema.php index 69f29186..11dcc572 100644 --- a/Setup/UpgradeSchema.php +++ b/Setup/UpgradeSchema.php @@ -59,6 +59,10 @@ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $con $this->addNodeSelectedItemId($setup); } + if (version_compare($context->getVersion(), '0.2.7', '<')) { + $this->addNodeImageSizeFields($setup); + } + $setup->endSetup(); } @@ -298,4 +302,37 @@ private function addNodeSelectedItemId(SchemaSetupInterface $setup) return $this; } + + /** + * @return $this + */ + private function addNodeImageSizeFields(SchemaSetupInterface $setup) + { + $connection = $setup->getConnection(); + $table = $setup->getTable('snowmenu_node'); + + $connection->addColumn( + $table, + NodeInterface::IMAGE_WIDTH, + [ + 'type' => Table::TYPE_INTEGER, + 'nullable' => true, + 'after' => NodeInterface::IMAGE_ALT_TEXT, + 'comment' => 'Image Width' + ] + ); + + $connection->addColumn( + $table, + NodeInterface::IMAGE_HEIGHT, + [ + 'type' => Table::TYPE_INTEGER, + 'nullable' => true, + 'after' => NodeInterface::IMAGE_WIDTH, + 'comment' => 'Image Height' + ] + ); + + return $this; + } } diff --git a/etc/module.xml b/etc/module.xml index f58fd655..634814e2 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/view/adminhtml/templates/menu/nodes.phtml b/view/adminhtml/templates/menu/nodes.phtml index e4a61867..ecb17e5c 100644 --- a/view/adminhtml/templates/menu/nodes.phtml +++ b/view/adminhtml/templates/menu/nodes.phtml @@ -70,6 +70,8 @@ $vueComponents = $block->getVueComponents(); "product" : "", "productId" : "", "imageAltText" : "", + "imageWidth" : "", + "imageHeight" : "", "selectedItemId" : "" } } diff --git a/view/adminhtml/web/vue/app.vue b/view/adminhtml/web/vue/app.vue index 3e5494de..ea999de5 100644 --- a/view/adminhtml/web/vue/app.vue +++ b/view/adminhtml/web/vue/app.vue @@ -131,6 +131,8 @@ content: null, image: null, image_alt_text: '', + image_width: '', + image_height: '', node_template: null, submenu_template: null, columns: [], diff --git a/view/adminhtml/web/vue/menu-type.vue b/view/adminhtml/web/vue/menu-type.vue index 6479f277..95051e7f 100644 --- a/view/adminhtml/web/vue/menu-type.vue +++ b/view/adminhtml/web/vue/menu-type.vue @@ -65,6 +65,22 @@ type="text" /> + + + +

{{ templatesLabel }}