@@ -299,56 +299,113 @@ public function setIsActive($status)
299299 }
300300
301301 /**
302- * Check for existing terms in other thesaurus
302+ * Check for existing terms in other thesaurus.
303+ *
304+ * For thesaurus with type 'expansion' we only show a warning about duplicate when the reference term (LHS)
305+ * exists as a reference term in another thesaurus (duplicate LHS). Duplicate values (RHS) are ignored entirely.
306+ *
307+ * For thesaurus with type 'synonym' all synonym terms are checked on duplicate across all tables.
308+ * If duplicates exist, a warning about duplicate will be displayed.
303309 *
304310 * @return void
311+ *
312+ * @SuppressWarnings(PHPMD.CyclomaticComplexity)
313+ * @SuppressWarnings(PHPMD.NPathComplexity)
305314 */
306- public function checkThesaurusTerms ()
315+ public function checkThesaurusTerms (): void
307316 {
308317 $ termsData = $ this ->getTermsData ();
318+ $ currentType = $ this ->getType ();
319+
320+ $ connection = $ this ->resourceConnection ->getConnection ();
321+ $ referenceTable = $ this ->resourceConnection ->getTableName (self ::THESAURUS_REFERENCE_TERMS_TABLE_NAME );
322+ $ expandedTable = $ this ->resourceConnection ->getTableName (self ::THESAURUS_EXPANDED_TERMS_TABLE_NAME );
323+
324+ $ lhs = [];
325+ $ rhs = [];
309326
310- $ terms = [];
311- foreach ($ termsData as $ termData ) {
312- if (isset ($ termData ['reference_term ' ]) && !empty ($ termData ['reference_term ' ])) {
313- $ terms [] = $ termData ['reference_term ' ];
327+ foreach ($ termsData as $ row ) {
328+ if (!empty ($ row ['reference_term ' ])) {
329+ $ lhs [] = trim ($ row ['reference_term ' ]);
314330 }
331+ if (!empty ($ row ['values ' ])) {
332+ foreach (explode (', ' , $ row ['values ' ]) as $ v ) {
333+ $ value = trim ($ v );
334+ if ($ value !== '' ) {
335+ $ rhs [] = $ value ;
336+ }
337+ }
338+ }
339+ }
340+
341+ $ lhs = array_values (array_unique ($ lhs ));
342+ $ rhs = array_values (array_unique ($ rhs ));
315343
316- $ terms = array_merge ($ terms , explode (', ' , $ termData ['values ' ]));
344+ if (empty ($ lhs ) && empty ($ rhs )) {
345+ return ;
317346 }
318347
319- $ connection = $ this ->resourceConnection ->getConnection ();
320- $ expandedTableName = $ this ->resourceConnection ->getTableName (self ::THESAURUS_EXPANDED_TERMS_TABLE_NAME );
321- $ referenceTableName = $ this ->resourceConnection ->getTableName (self ::THESAURUS_REFERENCE_TERMS_TABLE_NAME );
322-
323- $ selectExpanded = $ connection ->select ()
324- ->from (['expanded ' => $ expandedTableName ], ['term ' , 'thesaurus_id ' , 'count ' => 'COUNT(*) ' ])
325- ->where ('expanded.term IN (?) ' , $ terms )
326- ->where ('expanded.thesaurus_id != ? ' , $ this ->getId ())
327- ->group (['term ' , 'thesaurus_id ' ])
328- ->order ('expanded.term ASC ' );
329-
330- $ selectReference = $ connection ->select ()
331- ->from (['reference ' => $ referenceTableName ], ['term ' , 'thesaurus_id ' , 'count ' => 'COUNT(*) ' ])
332- ->where ('reference.term IN (?) ' , $ terms )
333- ->where ('reference.thesaurus_id != ? ' , $ this ->getId ())
348+ // Logic for thesaurus with type 'expansion'.
349+ if ($ currentType === self ::TYPE_EXPANSION ) {
350+ if (empty ($ lhs )) {
351+ return ;
352+ }
353+
354+ $ select = $ connection ->select ()
355+ ->from (['r ' => $ referenceTable ], ['term ' , 'thesaurus_id ' , 'count ' => 'COUNT(*) ' ])
356+ ->where ('r.term IN (?) ' , $ lhs )
357+ ->where ('r.thesaurus_id != ? ' , (int ) $ this ->getId ())
358+ ->group (['term ' , 'thesaurus_id ' ]);
359+
360+ $ rows = $ connection ->fetchAll ($ select );
361+
362+ foreach ($ rows as $ row ) {
363+ if ($ row ['count ' ] > 0 ) {
364+ $ name = $ this ->getThesaurusNameById ($ row ['thesaurus_id ' ]);
365+ $ this ->messageManager ->addWarning (__ (
366+ 'The reference term "<strong>%1</strong>" is already defined as a ' .
367+ 'reference term in the <strong>%2</strong> thesaurus. ' ,
368+ $ row ['term ' ],
369+ $ name
370+ ));
371+ }
372+ }
373+
374+ return ;
375+ }
376+
377+ // Logic for thesaurus with type 'synonym'.
378+ $ allTerms = array_unique (array_merge ($ lhs , $ rhs ));
379+
380+ if (empty ($ allTerms )) {
381+ return ;
382+ }
383+
384+ $ selectRef = $ connection ->select ()
385+ ->from (['r ' => $ referenceTable ], ['term ' , 'thesaurus_id ' , 'count ' => 'COUNT(*) ' ])
386+ ->where ('r.term IN (?) ' , $ allTerms )
387+ ->where ('r.thesaurus_id != ? ' , (int ) $ this ->getId ())
334388 ->group (['term ' , 'thesaurus_id ' ]);
335389
336- $ resultExpanded = $ connection ->fetchAll ($ selectExpanded );
337- $ resultReference = $ connection ->fetchAll ($ selectReference );
390+ $ selectExp = $ connection ->select ()
391+ ->from (['e ' => $ expandedTable ], ['term ' , 'thesaurus_id ' , 'count ' => 'COUNT(*) ' ])
392+ ->where ('e.term IN (?) ' , $ allTerms )
393+ ->where ('e.thesaurus_id != ? ' , (int ) $ this ->getId ())
394+ ->group (['term ' , 'thesaurus_id ' ]);
338395
339- $ result = array_merge ($ resultReference , $ resultExpanded );
396+ $ rows = array_merge (
397+ $ connection ->fetchAll ($ selectRef ),
398+ $ connection ->fetchAll ($ selectExp )
399+ );
340400
341- foreach ($ result as $ row ) {
401+ foreach ($ rows as $ row ) {
342402 if ($ row ['count ' ] > 0 ) {
343- $ existingThesaurusId = $ row ['thesaurus_id ' ];
344- $ existingThesaurusName = $ this ->getThesaurusNameById ($ existingThesaurusId );
345-
346- $ message = __ (
403+ $ name = $ this ->getThesaurusNameById ($ row ['thesaurus_id ' ]);
404+ $ this ->messageManager ->addWarning (__ (
347405 'The term "<strong>%1</strong>" is already existing in the <strong>%2</strong> thesaurus. ' ,
348406 $ row ['term ' ],
349- $ existingThesaurusName
350- );
351- $ this ->messageManager ->addWarning ($ message );
407+ $ name
408+ ));
352409 }
353410 }
354411 }
0 commit comments