22
33namespace Moox \Core \Entities \Items \Draft ;
44
5+ use Illuminate \Support \Facades \DB ;
56use Illuminate \Database \Eloquent \Model ;
6- use Illuminate \Database \Eloquent \Relations \MorphTo ;
77use Illuminate \Database \Eloquent \SoftDeletes ;
8+ use Illuminate \Database \Eloquent \Relations \MorphTo ;
89
910abstract class BaseDraftTranslationModel extends Model
1011{
@@ -108,6 +109,10 @@ protected static function boot(): void
108109 parent ::boot ();
109110
110111 static ::creating (function ($ model ) {
112+ if (empty ($ model ->translation_status )) {
113+ $ model ->translation_status = 'draft ' ;
114+ }
115+
111116 if (auth ()->check ()) {
112117 $ model ->createdBy ()->associate (auth ()->user ());
113118 }
@@ -121,6 +126,100 @@ protected static function boot(): void
121126 static ::updating (function ($ model ) {
122127 $ model ->updatedBy ()->associate (auth ()->user ());
123128 });
129+
130+ static ::saved (function ($ model ) {
131+ DB ::afterCommit (function () use ($ model ) {
132+ $ model ->checkAndUpdateMainEntryStatus ();
133+ });
134+ });
135+ }
136+
137+ /**
138+ * Check and update main entry status based on translation statuses
139+ */
140+ protected function checkAndUpdateMainEntryStatus (): void
141+ {
142+ $ mainEntry = $ this ->getMainEntry ();
143+
144+ if (!$ mainEntry ) {
145+ return ;
146+ }
147+
148+ $ config = config ('core.draft_publish_logic ' , [
149+ 'auto_publish_single ' => true ,
150+ 'prompt_when_all_published ' => true ,
151+ 'prompt_when_any_published ' => false ,
152+ ]);
153+
154+ $ mainEntry ->load ('translations ' );
155+ $ allTranslations = $ mainEntry ->translations ;
156+ $ translationCount = $ allTranslations ->count ();
157+
158+ $ publishedCount = $ allTranslations ->where ('translation_status ' , 'published ' )->count ();
159+
160+ if ($ translationCount === 1 && $ config ['auto_publish_single ' ]) {
161+ $ singleTranslation = $ allTranslations ->first ();
162+ $ newStatus = null ;
163+
164+ if ($ singleTranslation ->translation_status === 'published ' ) {
165+ $ newStatus = 'published ' ;
166+ } elseif (in_array ($ singleTranslation ->translation_status , ['draft ' , 'waiting ' , 'private ' , 'scheduled ' ])) {
167+ $ newStatus = $ singleTranslation ->translation_status ;
168+ }
169+
170+ if ($ newStatus && $ mainEntry ->status !== $ newStatus ) {
171+ $ mainEntry ->status = $ newStatus ;
172+ $ mainEntry ->timestamps = false ; // Prevent updated_at from changing
173+ $ mainEntry ->save ();
174+ $ mainEntry ->timestamps = true ;
175+ }
176+ }
177+
178+ if ($ translationCount > 1 ) {
179+ if ($ publishedCount === 0 && $ mainEntry ->status === 'published ' ) {
180+ $ mainEntry ->status = 'draft ' ;
181+ $ mainEntry ->timestamps = false ;
182+ $ mainEntry ->save ();
183+ $ mainEntry ->timestamps = true ;
184+ }
185+
186+ if ($ mainEntry ->status === 'published ' && $ publishedCount < $ translationCount ) {
187+ $ unpublishedStatuses = $ allTranslations
188+ ->where ('translation_status ' , '!= ' , 'published ' )
189+ ->pluck ('translation_status ' )
190+ ->countBy ()
191+ ->sortDesc ();
192+
193+ $ newStatus = $ unpublishedStatuses ->keys ()->first () ?? 'draft ' ;
194+ if ($ mainEntry ->status !== $ newStatus ) {
195+ $ mainEntry ->status = $ newStatus ;
196+ $ mainEntry ->timestamps = false ;
197+ $ mainEntry ->save ();
198+ $ mainEntry ->timestamps = true ;
199+ }
200+ }
201+ }
202+ }
203+
204+ /**
205+ * Get the main entry (parent model) for this translation
206+ * Uses the translatable relation that already exists
207+ */
208+ protected function getMainEntry ()
209+ {
210+ $ tableName = $ this ->getTable ();
211+ $ foreignKey = str_replace ('_translations ' , '_id ' , $ tableName );
212+
213+ if (isset ($ this ->attributes [$ foreignKey ]) && $ this ->attributes [$ foreignKey ]) {
214+ $ parentClass = get_class ($ this );
215+ $ parentClass = str_replace ('Translation ' , '' , $ parentClass );
216+
217+ if (class_exists ($ parentClass )) {
218+ return $ parentClass ::find ($ this ->attributes [$ foreignKey ]);
219+ }
220+ }
221+
222+ return null ;
124223 }
125224
126225 /**
0 commit comments