@@ -226,39 +226,85 @@ protected function add_reasons_to($request)
226226 */
227227 protected function add_tags_to ($ request )
228228 {
229- $ tag_rules = $ tag_messages = [];
229+ $ a_kept_tags = $ a_key_names = $ a_names = $ tag_rules = $ tag_messages = [];
230230
231231 // Allow alphanumeric and the following: ? @ / - _
232- $ tag_rule = "required|regex:/^[A-Za-z0-9?@\/\-_\s]+$/ " ;
232+ $ tag_rule = "required|regex: " . trans ( ' panichd::admin.tag-regex ' ) ;
233233
234- // Add validation for new tags like it were fields
235- $ a_tags_new = [];
236- if ($ request ->input ('new_tags ' )) {
237- $ i = 0 ;
238- foreach ($ request ->input ('new_tags ' ) as $ tag ) {
239- $ a_tags_new [] = $ tag ;
240- $ request ['tag ' .++$ i ] = $ tag ;
241- $ tag_rules ['tag ' .$ i ] = $ tag_rule ;
242- $ tag_messages ['tag ' .$ i .'.regex ' ] = trans ('panichd::admin.category-tag-not-valid-format ' , ['tag ' =>$ tag ]);
243- }
244- }
234+ $ c_tags = Tag::all ();
245235
246236 $ a_tags_update = [];
247237 for ($ i = 0 ; $ i < $ request ->input ('tags_count ' ); $ i ++) {
248- if (!$ request ->input ('jquery_delete_tag_ ' .$ i ) != "" ) {
249- // Add validation for renamed tags
250- if ($ request ->exists ('jquery_tag_name_ ' .$ i ) and $ request ->input ('jquery_tag_id_ ' .$ i ) != "" ) {
251- $ tag = $ request ->input ('jquery_tag_name_ ' .$ i );
252- $ request ->merge (['jquery_tag_name_ ' .$ i =>$ tag ]);
253- $ a_tags_update [$ request ->input ('jquery_tag_id_ ' .$ i )]['name ' ] = $ tag ;
254- $ request ['jquery_tag_name_ ' .$ i ] = $ tag ;
255- $ tag_rules ['jquery_tag_name_ ' .$ i ] = $ tag_rule ;
256- $ tag_messages ['jquery_tag_name_ ' .$ i .'.regex ' ] = trans ('panichd::admin.category-tag-not-valid-format ' , ['tag ' =>$ tag ]);
238+ $ tag = $ c_tags ->first (function ($ q )use ($ request , $ i ){
239+ return $ q ->id == $ request ->{'jquery_tag_id_ ' .$ i };
240+ });
241+
242+ if ($ request ->has ('jquery_tag_id_ ' .$ i ) and !$ request ->has ('jquery_delete_tag_ ' .$ i ) and !is_null ($ tag )) {
243+ // Tag has been kept in category
244+ $ a_kept_tags [$ i ] = $ tag ;
245+
246+ if ($ request ->exists ('jquery_tag_name_ ' .$ i )) {
247+ // Add new name for rule preparation
248+ $ a_key_names [$ i ] = $ request ->{'jquery_tag_name_ ' . $ i };
249+
250+ }else {
251+ // Add current name (has not changed) for rule preparation
252+ $ a_key_names [$ i ] = $ tag ->name ;
257253 }
254+ }
255+ }
256+
257+ // Names for new tags rule preparation
258+ $ a_names = array_values ($ a_key_names );
259+
260+ foreach ($ a_kept_tags as $ i => $ tag ){
261+ // Add validation for renamed tags
262+ if ($ request ->exists ('jquery_tag_name_ ' .$ i )) {
263+ // New tag name
264+ $ new_name = $ request ->input ('jquery_tag_name_ ' .$ i );
265+
266+ $ request ->merge (['jquery_tag_name_ ' .$ i =>$ new_name ]);
267+ $ a_tags_update [$ request ->input ('jquery_tag_id_ ' .$ i )]['name ' ] = $ new_name ;
268+ $ request ['jquery_tag_name_ ' .$ i ] = $ new_name ;
269+
270+ // Rule for an updated tag
271+ $ a_this_not_in = $ a_key_names ;
272+ unset($ a_this_not_in [$ i ]);
273+ $ tag_rules ['jquery_tag_name_ ' .$ i ] = $ tag_rule . ($ a_this_not_in ? '|not_in: ' . implode (', ' , array_values ($ a_this_not_in )) : '' );
274+
275+ // Add specific validation error messages
276+ $ tag_messages ['jquery_tag_name_ ' . $ i . '.required ' ] = trans ('panichd::admin.update-tag-validation-empty ' , ['name ' => $ tag ->name ]);
277+ $ tag_messages ['jquery_tag_name_ ' . $ i . '.regex ' ] = trans ('panichd::admin.category-tag-not-valid-format ' , ['tag ' =>$ new_name ]);
278+ $ tag_messages ['jquery_tag_name_ ' . $ i . '.not_in ' ] = trans ('panichd::admin.tag-validation-two ' , ['name ' => $ new_name ]);
279+
280+ }
258281
259- // Add colors for tag update
260- if ($ request ->input ('jquery_tag_color_ ' .$ i ) != "" ) {
261- $ a_tags_update [$ request ->input ('jquery_tag_id_ ' .$ i )]['color ' ] = $ request ->input ('jquery_tag_color_ ' .$ i );
282+ // Add colors for tag update
283+ if ($ request ->input ('jquery_tag_color_ ' .$ i ) != "" ) {
284+ $ a_tags_update [$ request ->input ('jquery_tag_id_ ' .$ i )]['color ' ] = $ request ->input ('jquery_tag_color_ ' .$ i );
285+ }
286+ }
287+
288+ // Add validation for new tags
289+ $ a_tags_new = [];
290+ if ($ request ->filled ('new_tags ' )) {
291+ foreach ($ request ->input ('new_tags ' ) as $ id ) {
292+ if (!$ request ->has ('jquery_delete_tag_ ' . $ id )){
293+ // Rule for new tag
294+ $ tag_rules ['jquery_tag_name_ ' . $ id ] = $ tag_rule . ($ a_names ? '|not_in: ' . implode (', ' , $ a_names ) : '' );
295+
296+ // Add tag name to array
297+ $ a_names [] = $ request ->{'jquery_tag_name_ ' . $ id };
298+
299+ // Add specific validation error messages
300+ $ tag_messages ['jquery_tag_name_ ' . $ id . '.required ' ] = trans ('panichd::admin.new-tag-validation-empty ' );
301+ $ tag_messages ['jquery_tag_name_ ' . $ id . '.regex ' ] = trans ('panichd::admin.category-tag-not-valid-format ' , ['tag ' => $ request ->{'jquery_tag_name_ ' . $ id }]);
302+ $ tag_messages ['jquery_tag_name_ ' . $ id . '.not_in ' ] = trans ('panichd::admin.tag-validation-two ' , ['name ' => $ request ->{'jquery_tag_name_ ' . $ id }]);
303+
304+ $ a_tags_new [] = [
305+ 'name ' => $ request ->{'jquery_tag_name_ ' . $ id },
306+ 'color ' => $ request ->{'jquery_tag_color_ ' .$ id }
307+ ];
262308 }
263309 }
264310 }
@@ -284,7 +330,7 @@ protected function do_validate($request, $rules, $reason_messages)
284330 'email_name ' => 'required|string ' ,
285331 'email ' => 'required|email ' ,
286332 ]);
287- }
333+ }
288334
289335 $ this ->validate ($ request , $ rules , $ reason_messages );
290336 }
@@ -396,12 +442,16 @@ protected function sync_category_tags($request, $category, $a_tags_new, $a_tags_
396442 }
397443
398444 // Add new tags
399- foreach ($ a_tags_new as $ tag ) {
400- $ new = Tag::whereHas ('categories ' , function ($ q ) use ($ category ) {
401- $ q ->where ('id ' , $ category ->id );
402- })->where ('name ' , $ tag )->firstOrCreate (['name ' =>$ tag ]);
445+ foreach ($ a_tags_new as $ a_tag ) {
446+ $ a_colors = explode ('_ ' , $ a_tag ['color ' ]);
447+
448+ $ new_tag = Tag::create ([
449+ 'name ' => $ a_tag ['name ' ],
450+ 'bg_color ' => $ a_colors [0 ],
451+ 'text_color ' => $ a_colors [1 ]
452+ ]);
403453
404- $ tags [] = $ new ->id ;
454+ $ tags [] = $ new_tag ->id ;
405455 }
406456
407457 // Sync all category tags
0 commit comments