@@ -606,11 +606,38 @@ public function getMultiWordForEdit(int $textId, int $position, ?string $text =
606606 ];
607607 }
608608
609- // New multi-word expression
609+ // Check if text is provided
610610 if ($ text === null || $ text === '' ) {
611611 return ['error ' => 'Multi-word text is required for new expressions ' ];
612612 }
613613
614+ // Try to find existing term by text (case-insensitive)
615+ $ textLc = mb_strtolower ($ text , 'UTF-8 ' );
616+ $ existingWord = QueryBuilder::table ('words ' )
617+ ->select (['WoID ' , 'WoText ' , 'WoTranslation ' , 'WoRomanization ' , 'WoSentence ' , 'WoStatus ' , 'WoWordCount ' ])
618+ ->where ('WoTextLC ' , '= ' , $ textLc )
619+ ->where ('WoLgID ' , '= ' , $ lgid )
620+ ->where ('WoWordCount ' , '> ' , 1 )
621+ ->firstPrepared ();
622+
623+ if ($ existingWord ) {
624+ // Found existing multi-word term
625+ return [
626+ 'id ' => (int ) $ existingWord ['WoID ' ],
627+ 'text ' => $ existingWord ['WoText ' ],
628+ 'textLc ' => $ textLc ,
629+ 'translation ' => $ existingWord ['WoTranslation ' ] ?? '' ,
630+ 'romanization ' => $ existingWord ['WoRomanization ' ] ?? '' ,
631+ 'sentence ' => $ existingWord ['WoSentence ' ] ?? '' ,
632+ 'notes ' => '' ,
633+ 'status ' => (int ) $ existingWord ['WoStatus ' ],
634+ 'langId ' => $ lgid ,
635+ 'wordCount ' => (int ) $ existingWord ['WoWordCount ' ],
636+ 'isNew ' => false
637+ ];
638+ }
639+
640+ // New multi-word expression
614641 // Get sentence at position
615642 $ sentence = $ this ->wordService ->getSentenceTextAtPosition ($ textId , $ position );
616643
@@ -620,7 +647,7 @@ public function getMultiWordForEdit(int $textId, int $position, ?string $text =
620647 return [
621648 'id ' => null ,
622649 'text ' => $ text ,
623- 'textLc ' => mb_strtolower ( $ text , ' UTF-8 ' ) ,
650+ 'textLc ' => $ textLc ,
624651 'translation ' => '' ,
625652 'romanization ' => '' ,
626653 'sentence ' => $ sentence ?? '' ,
0 commit comments