Skip to content

Commit

Permalink
#94738 wip adjust loading customer group assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwaclawczyk committed Jul 5, 2024
1 parent c6867e4 commit cd2db76
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 53 deletions.
4 changes: 2 additions & 2 deletions Model/Menu/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,8 @@ public function setSelectedItemId($selectedItemId)
public function getCustomerGroups()
{
$customerGroups = $this->_getData(NodeInterface::CUSTOMER_GROUPS);
if (!empty($customerGroups)) {
$customerGroups = $this->serializer->unserialize($customerGroups);
if ($customerGroups !== null) {
$customerGroups = explode(',', $customerGroups);
if (is_array($customerGroups) && !empty($customerGroups)) {
return $customerGroups;
}
Expand Down
2 changes: 1 addition & 1 deletion Model/Menu/Node/Image/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function updateNodeImage(int $nodeId, ?string $image): void
public function getNodeListImages(array $nodeIds): array
{
$searchCriteria = $this->searchCriteriaBuilder
->addFilter(NodeInterface::NODE_ID, $nodeIds, 'in')
->addFilter('main_table.' . NodeInterface::NODE_ID, $nodeIds, 'in')
->addFilter(NodeInterface::IMAGE, true, 'notnull')
->addFilter(NodeInterface::IMAGE, '', 'neq')
->create();
Expand Down
1 change: 1 addition & 0 deletions Model/Menu/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Snowdog\Menu\Model\Menu\NodeFactory;
use Snowdog\Menu\Model\ResourceModel\Menu\Node\CollectionFactory;
use Magento\Framework\Api\SortOrder;
use Magento\Framework\DB\Sql\Expression;

class NodeRepository implements NodeRepositoryInterface
{
Expand Down
23 changes: 23 additions & 0 deletions Model/ResourceModel/Menu/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ protected function _construct()
$this->_init('snowmenu_node', 'node_id');
}

protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
{
$connection = $this->getConnection();
$connection->delete('snowmenu_customer', ['node_id = ?' => $object->getNodeId()]);
$currentCustomerGroups = $object->getData('customer_groups');
if ($currentCustomerGroups && is_string($currentCustomerGroups)) {
$currentCustomerGroups = json_decode($currentCustomerGroups);
}
$insertData = [];
foreach ($currentCustomerGroups as $customerGroup) {
$insertData[] = [
'node_id' => $object->getNodeId(),
'group_id' => $customerGroup
];
}
if ($currentCustomerGroups) {
$connection->insertMultiple('snowmenu_customer', $insertData);
}

return parent::_afterSave($object); // TODO: Change the autogenerated stub

}

public function getFields(): array
{
return $this->getConnection()->describeTable($this->getMainTable());
Expand Down
12 changes: 12 additions & 0 deletions Model/ResourceModel/Menu/Node/Collection.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Snowdog\Menu\Model\ResourceModel\Menu\Node;

use Magento\Framework\DB\Sql\Expression;
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
Expand All @@ -12,4 +13,15 @@ protected function _construct()
\Snowdog\Menu\Model\ResourceModel\Menu\Node::class
);
}

protected function _initSelect()
{
parent::_initSelect(); // TODO: Change the autogenerated stub
$this->getSelect()->joinLeft(
['customer' => 'snowmenu_customer'],
'main_table.node_id = customer.node_id',
['customer_groups' => new Expression('GROUP_CONCAT(group_id SEPARATOR \',\')')]
)->group('main_table.node_id');
// die ((string) $collection->getSelect());
}
}
104 changes: 54 additions & 50 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="snowmenu_menu" resource="default" engine="innodb" comment="snowmenu_menu">
<column xsi:type="int" name="menu_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
<column xsi:type="varchar" name="title" nullable="false" length="255" comment="Demo Title"/>
<column xsi:type="varchar" name="identifier" nullable="false" length="255" comment="Menu identifier"/>
<column xsi:type="timestamp" name="creation_time" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Creation Time"/>
<column xsi:type="timestamp" name="update_time" on_update="true" nullable="false" default="CURRENT_TIMESTAMP" comment="Modification Time"/>
<column xsi:type="smallint" name="is_active" padding="6" unsigned="false" nullable="false" identity="false" default="1" comment="Is Active"/>
<column xsi:type="varchar" name="css_class" nullable="true" length="255" default="menu" comment="CSS Class"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="menu_id"/>
</constraint>
</table>
<table name="snowmenu_node" resource="default" engine="innodb" comment="snowmenu_node">
<column xsi:type="int" name="node_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Node ID"/>
<column xsi:type="int" name="menu_id" padding="9" unsigned="true" nullable="false" identity="false" comment="Menu ID"/>
<column xsi:type="varchar" name="type" nullable="false" length="255" comment="Node Type"/>
<column xsi:type="text" name="content" nullable="true" comment="Node contents"/>
<column xsi:type="varchar" name="classes" nullable="true" length="255" comment="CSS class name"/>
<column xsi:type="int" name="parent_id" padding="10" unsigned="true" nullable="true" identity="false" comment="Parent Node ID"/>
<column xsi:type="int" name="position" padding="10" unsigned="true" nullable="false" identity="false" comment="Node position"/>
<column xsi:type="int" name="level" padding="10" unsigned="true" nullable="false" identity="false" comment="Node level"/>
<column xsi:type="text" name="title" nullable="false"/>
<column xsi:type="timestamp" name="creation_time" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Creation Time"/>
<column xsi:type="timestamp" name="update_time" on_update="true" nullable="false" default="CURRENT_TIMESTAMP" comment="Modification Time"/>
<column xsi:type="smallint" name="is_active" padding="6" unsigned="false" nullable="false" identity="false" default="1" comment="Is Active"/>
<column xsi:type="boolean" name="target" nullable="true"/>
<column xsi:type="varchar" name="submenu_template" nullable="true" length="255" comment="Submenu Template"/>
<column xsi:type="varchar" name="node_template" nullable="true" length="255" comment="Node Template"/>
<column xsi:type="text" name="image" nullable="true" comment="Image"/>
<column xsi:type="text" name="image_alt_text" nullable="true" comment="Image Alt Text"/>
<column xsi:type="smallint" name="selected_item_id" padding="5" unsigned="true" nullable="true" identity="false" comment="Selected Item Id"/>
<column xsi:type="int" name="image_width" padding="11" unsigned="false" nullable="true" identity="false" comment="Image Width"/>
<column xsi:type="int" name="image_heigth" padding="11" unsigned="false" nullable="true" identity="false" comment="Image Height"/>
<column xsi:type="varchar" name="customer_groups" nullable="true" length="255" comment="Customer Groups Serialized"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="node_id"/>
</constraint>
<constraint xsi:type="foreign" referenceId="SNOWMENU_NODE_MENU_ID_SNOWMENU_MENU_MENU_ID" table="snowmenu_node" column="menu_id" referenceTable="snowmenu_menu" referenceColumn="menu_id" onDelete="CASCADE"/>
</table>
<table name="snowmenu_store" resource="default" engine="innodb" comment="snowmenu_store">
<column xsi:type="int" name="menu_id" padding="10" unsigned="true" nullable="false" identity="false" comment="Menu ID"/>
<column xsi:type="smallint" name="store_id" padding="4" unsigned="true" nullable="false" identity="false" comment="Store ID"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="menu_id"/>
<column name="store_id"/>
<column name="store_id"/>
</constraint>
<constraint xsi:type="foreign" referenceId="SNOWMENU_STORE_MENU_ID_SNOWMENU_MENU_MENU_ID" table="snowmenu_store" column="menu_id" referenceTable="snowmenu_menu" referenceColumn="menu_id" onDelete="CASCADE"/>
<constraint xsi:type="foreign" referenceId="SNOWMENU_STORE_STORE_ID_STORE_STORE_ID" table="snowmenu_store" column="store_id" referenceTable="store" referenceColumn="store_id" onDelete="CASCADE"/>
</table>
<table name="snowmenu_menu" resource="default" engine="innodb" comment="snowmenu_menu">
<column xsi:type="int" name="menu_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Entity ID"/>
<column xsi:type="varchar" name="title" nullable="false" length="255" comment="Demo Title"/>
<column xsi:type="varchar" name="identifier" nullable="false" length="255" comment="Menu identifier"/>
<column xsi:type="timestamp" name="creation_time" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Creation Time"/>
<column xsi:type="timestamp" name="update_time" on_update="true" nullable="false" default="CURRENT_TIMESTAMP" comment="Modification Time"/>
<column xsi:type="smallint" name="is_active" padding="6" unsigned="false" nullable="false" identity="false" default="1" comment="Is Active"/>
<column xsi:type="varchar" name="css_class" nullable="true" length="255" default="menu" comment="CSS Class"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="menu_id"/>
</constraint>
</table>
<table name="snowmenu_node" resource="default" engine="innodb" comment="snowmenu_node">
<column xsi:type="int" name="node_id" padding="10" unsigned="true" nullable="false" identity="true" comment="Node ID"/>
<column xsi:type="int" name="menu_id" padding="9" unsigned="true" nullable="false" identity="false" comment="Menu ID"/>
<column xsi:type="varchar" name="type" nullable="false" length="255" comment="Node Type"/>
<column xsi:type="text" name="content" nullable="true" comment="Node contents"/>
<column xsi:type="varchar" name="classes" nullable="true" length="255" comment="CSS class name"/>
<column xsi:type="int" name="parent_id" padding="10" unsigned="true" nullable="true" identity="false" comment="Parent Node ID"/>
<column xsi:type="int" name="position" padding="10" unsigned="true" nullable="false" identity="false" comment="Node position"/>
<column xsi:type="int" name="level" padding="10" unsigned="true" nullable="false" identity="false" comment="Node level"/>
<column xsi:type="text" name="title" nullable="false"/>
<column xsi:type="timestamp" name="creation_time" on_update="false" nullable="false" default="CURRENT_TIMESTAMP" comment="Creation Time"/>
<column xsi:type="timestamp" name="update_time" on_update="true" nullable="false" default="CURRENT_TIMESTAMP" comment="Modification Time"/>
<column xsi:type="smallint" name="is_active" padding="6" unsigned="false" nullable="false" identity="false" default="1" comment="Is Active"/>
<column xsi:type="boolean" name="target" nullable="true"/>
<column xsi:type="varchar" name="submenu_template" nullable="true" length="255" comment="Submenu Template"/>
<column xsi:type="varchar" name="node_template" nullable="true" length="255" comment="Node Template"/>
<column xsi:type="text" name="image" nullable="true" comment="Image"/>
<column xsi:type="text" name="image_alt_text" nullable="true" comment="Image Alt Text"/>
<column xsi:type="smallint" name="selected_item_id" padding="5" unsigned="true" nullable="true" identity="false" comment="Selected Item Id"/>
<column xsi:type="int" name="image_width" padding="11" unsigned="false" nullable="true" identity="false" comment="Image Width"/>
<column xsi:type="int" name="image_heigth" padding="11" unsigned="false" nullable="true" identity="false" comment="Image Height"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="node_id"/>
</constraint>
<constraint xsi:type="foreign" referenceId="SNOWMENU_NODE_MENU_ID_SNOWMENU_MENU_MENU_ID" table="snowmenu_node" column="menu_id" referenceTable="snowmenu_menu" referenceColumn="menu_id" onDelete="CASCADE"/>
</table>
<table name="snowmenu_store" resource="default" engine="innodb" comment="snowmenu_store">
<column xsi:type="int" name="menu_id" padding="10" unsigned="true" nullable="false" identity="false" comment="Menu ID"/>
<column xsi:type="smallint" name="store_id" padding="4" unsigned="true" nullable="false" identity="false" comment="Store ID"/>
<constraint xsi:type="primary" referenceId="PRIMARY">
<column name="menu_id"/>
<column name="store_id"/>
<column name="store_id"/>
</constraint>
<constraint xsi:type="foreign" referenceId="SNOWMENU_STORE_MENU_ID_SNOWMENU_MENU_MENU_ID" table="snowmenu_store" column="menu_id" referenceTable="snowmenu_menu" referenceColumn="menu_id" onDelete="CASCADE"/>
<constraint xsi:type="foreign" referenceId="SNOWMENU_STORE_STORE_ID_STORE_STORE_ID" table="snowmenu_store" column="store_id" referenceTable="store" referenceColumn="store_id" onDelete="CASCADE"/>
</table>
<table name="snowmenu_customer" resource="default" engine="innodb" comment="snowmenu_customer">
<column xsi:type="int" name="node_id" padding="10" unsigned="true" nullable="false" identity="false" comment="Node ID"/>
<column xsi:type="smallint" name="group_id" unsigned="true" nullable="false" identity="false" comment="Group ID"/>
<constraint xsi:type="foreign" referenceId="SNOWMENU_CUSTOMER_NODE_ID_SNOWMENU_NODE_NODE_ID" table="snowmenu_customer" column="node_id" referenceTable="snowmenu_node" referenceColumn="node_id" onDelete="CASCADE"/>
</table>
</schema>

0 comments on commit cd2db76

Please sign in to comment.