@@ -54,37 +54,58 @@ public function execute()
5454 /** @var \Lof\ProductTags\Model\Tag $model */
5555 $ model = $ this ->TagFactory ->create ();
5656 $ id = $ this ->getRequest ()->getParam ('tag_id ' );
57- if ($ id ) {
58- try {
59- $ model = $ model ->load ($ id );
57+ $ identifier = $ this ->getRequest ()->getParam ('identifier ' );
58+ //print_r($identifier);die();
59+ $ objectManager = \Magento \Framework \App \ObjectManager::getInstance (); // Instance of object manager
60+ $ resource = $ objectManager ->get ('Magento\Framework\App\ResourceConnection ' );
61+ $ connection = $ resource ->getConnection ();
62+ $ tableName = $ resource ->getTableName ('lof_producttags_tag ' );
63+ $ select = $ connection ->select ()->from (
64+ ['main_table ' => $ tableName ],
65+ [new \Zend_Db_Expr ('COUNT(main_table.identifier) ' )]
66+ )->where (
67+ 'main_table.identifier = :identifier '
68+ );
69+
70+ $ bind = ['identifier ' => $ identifier ];
71+ $ counts = $ connection ->fetchOne ($ select , $ bind );
72+
73+ if ($ counts > 0 ){
74+ $ this ->messageManager ->addErrorMessage (__ ('The identifier already exists ' ));
75+ }
76+ else {
77+ if ($ id ) {
78+ try {
79+ $ model = $ model ->load ($ id );
80+ } catch (LocalizedException $ e ) {
81+ $ this ->messageManager ->addErrorMessage (__ ('This tag no longer exists. ' ));
82+ return $ resultRedirect ->setPath ('*/*/ ' );
83+ }
84+ }
85+ $ model ->setData ($ data );
86+ if (isset ($ data ['tag_products ' ])
87+ && is_string ($ data ['tag_products ' ])) {
88+ $ products = json_decode ($ data ['tag_products ' ], true );
89+ $ model ->setPostedProducts ($ products );
90+ }
91+ $ this ->_eventManager ->dispatch (
92+ 'lof_producttags_prepare_save ' ,
93+ ['tag ' => $ model , 'request ' => $ this ->getRequest ()]
94+ );
95+ $ products = $ model ->getPostedProducts ();
96+ try {
97+ $ model ->save ($ model );
98+ $ this ->messageManager ->addSuccessMessage (__ ('You saved the tag. ' ));
99+ $ this ->dataPersistor ->clear ('lof_productags_tag ' );
100+ return $ this ->processBlockReturn ($ model , $ data , $ resultRedirect );
60101 } catch (LocalizedException $ e ) {
61- $ this ->messageManager ->addErrorMessage (__ ('This tag no longer exists. ' ));
62- return $ resultRedirect ->setPath ('*/*/ ' );
102+ $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
103+ } catch (\Exception $ e ) {
104+ $ this ->messageManager ->addExceptionMessage ($ e , __ ('Something went wrong while saving the tag. ' ));
63105 }
106+ $ this ->dataPersistor ->set ('lof_productags_tag ' , $ data );
107+ return $ resultRedirect ->setPath ('*/*/edit ' , ['tag_id ' => $ id ]);
64108 }
65- $ model ->setData ($ data );
66- if (isset ($ data ['tag_products ' ])
67- && is_string ($ data ['tag_products ' ])) {
68- $ products = json_decode ($ data ['tag_products ' ], true );
69- $ model ->setPostedProducts ($ products );
70- }
71- $ this ->_eventManager ->dispatch (
72- 'lof_producttags_prepare_save ' ,
73- ['tag ' => $ model , 'request ' => $ this ->getRequest ()]
74- );
75- $ products = $ model ->getPostedProducts ();
76- try {
77- $ model ->save ($ model );
78- $ this ->messageManager ->addSuccessMessage (__ ('You saved the tag. ' ));
79- $ this ->dataPersistor ->clear ('lof_productags_tag ' );
80- return $ this ->processBlockReturn ($ model , $ data , $ resultRedirect );
81- } catch (LocalizedException $ e ) {
82- $ this ->messageManager ->addErrorMessage ($ e ->getMessage ());
83- } catch (\Exception $ e ) {
84- $ this ->messageManager ->addExceptionMessage ($ e , __ ('Something went wrong while saving the tag. ' ));
85- }
86- $ this ->dataPersistor ->set ('lof_productags_tag ' , $ data );
87- return $ resultRedirect ->setPath ('*/*/edit ' , ['tag_id ' => $ id ]);
88109 }
89110 return $ resultRedirect ->setPath ('*/*/ ' );
90111 }
0 commit comments