| @@ -5,8 +5,9 @@ | ||
| 5 | 5 | namespace Yatra\Services; |
| 6 | 6 | |
| 7 | 7 | use Yatra\Repositories\CategoryRepository; |
| 8 | 8 | use Yatra\Repositories\TripCategoryRepository; |
| 9 | +use Yatra\Helpers\ClassificationLandingPageMetadata; | |
| 9 | 10 | use Yatra\Helpers\SlugHelper; |
| 10 | 11 | use Yatra\Helpers\FormatHelper; |
| 11 | 12 | use Yatra\Database\Tables\ClassificationsTable; |
| 12 | 13 | |
| @@ -41,8 +42,10 @@ | ||
| 41 | 42 | * Process before create |
| 42 | 43 | */ |
| 43 | 44 | protected function processBeforeCreate(array $data): array |
| 44 | 45 | { |
| 46 | + unset($data['preserve_slug']); | |
| 47 | + | |
| 45 | 48 | // Set the type to 'category' for the ClassificationsTable |
| 46 | 49 | $data['type'] = 'category'; |
| 47 | 50 | |
| 48 | 51 | // Sanitize name |
| @@ -77,8 +80,12 @@ | ||
| 77 | 80 | } else { |
| 78 | 81 | $data['status'] = 'draft'; |
| 79 | 82 | } |
| 80 | 83 | |
| 84 | + if (array_key_exists('is_featured', $data)) { | |
| 85 | + $data['is_featured'] = !empty($data['is_featured']) ? 1 : 0; | |
| 86 | + } | |
| 87 | + | |
| 81 | 88 | // Set created_by and updated_by to current user |
| 82 | 89 | $current_user_id = get_current_user_id(); |
| 83 | 90 | $data['created_by'] = absint($current_user_id); |
| 84 | 91 | $data['updated_by'] = absint($current_user_id); |
| @@ -101,9 +108,10 @@ | ||
| 101 | 108 | elseif (is_numeric($value)) { |
| 102 | 109 | $data['icon']['value'] = (int) $value; |
| 103 | 110 | } |
| 104 | 111 | } |
| 105 | - | |
| 112 | + | |
| 113 | + $data['icon'] = yatra_normalize_icon_picker_for_storage($data['icon']); | |
| 106 | 114 | $data['icon'] = maybe_serialize($data['icon']); |
| 107 | 115 | } |
| 108 | 116 | } |
| 109 | 117 | |
| @@ -108,15 +116,14 @@ | ||
| 108 | 116 | } |
| 109 | 117 | |
| 110 | 118 | // Handle SEO metadata |
| 111 | 119 | if (isset($data['seo_title']) || isset($data['seo_description']) || isset($data['seo_keywords'])) { |
| 112 | - $existing_metadata = []; | |
| 113 | - if (isset($data['metadata']) && is_array($data['metadata'])) { | |
| 114 | - $existing_metadata = $data['metadata']; | |
| 115 | - } elseif (isset($data['metadata'])) { | |
| 116 | - $existing_metadata = maybe_unserialize($data['metadata']); | |
| 117 | - } | |
| 118 | - | |
| 120 | + $existing_metadata = ClassificationLandingPageMetadata::baseMetadataMergedWithRequest( | |
| 121 | + null, | |
| 122 | + $this->repository, | |
| 123 | + $data | |
| 124 | + ); | |
| 125 | + | |
| 119 | 126 | // Add SEO fields to metadata |
| 120 | 127 | if (isset($data['seo_title'])) { |
| 121 | 128 | $existing_metadata['seo_title'] = sanitize_text_field($data['seo_title']); |
| 122 | 129 | unset($data['seo_title']); // Remove from main data |
| @@ -132,8 +139,10 @@ | ||
| 132 | 139 | |
| 133 | 140 | $data['metadata'] = $existing_metadata; |
| 134 | 141 | } |
| 135 | 142 | |
| 143 | + ClassificationLandingPageMetadata::mergeLandingPageIntoData($data, null, $this->repository); | |
| 144 | + | |
| 136 | 145 | // Sanitize metadata if it's an array |
| 137 | 146 | if (isset($data['metadata'])) { |
| 138 | 147 | if (is_array($data['metadata'])) { |
| 139 | 148 | $data['metadata'] = maybe_serialize($data['metadata']); |
| @@ -147,9 +156,10 @@ | ||
| 147 | 156 | * Process before update |
| 148 | 157 | */ |
| 149 | 158 | protected function processBeforeUpdate(int $id, array $data): array |
| 150 | 159 | { |
| 151 | - | |
| 160 | + unset($data['preserve_slug']); | |
| 161 | + | |
| 152 | 162 | // Ensure the type remains 'category' for the ClassificationsTable |
| 153 | 163 | $data['type'] = 'category'; |
| 154 | 164 | |
| 155 | 165 | // Sanitize name |
| @@ -183,8 +193,12 @@ | ||
| 183 | 193 | ? $data['status'] |
| 184 | 194 | : 'draft'; |
| 185 | 195 | } |
| 186 | 196 | |
| 197 | + if (array_key_exists('is_featured', $data)) { | |
| 198 | + $data['is_featured'] = !empty($data['is_featured']) ? 1 : 0; | |
| 199 | + } | |
| 200 | + | |
| 187 | 201 | // Set updated_by to current user |
| 188 | 202 | $data['updated_by'] = absint(get_current_user_id()); |
| 189 | 203 | |
| 190 | 204 | // Sanitize and serialize icon if it's an array |
| @@ -205,9 +219,10 @@ | ||
| 205 | 219 | elseif (is_numeric($value)) { |
| 206 | 220 | $data['icon']['value'] = (int) $value; |
| 207 | 221 | } |
| 208 | 222 | } |
| 209 | - | |
| 223 | + | |
| 224 | + $data['icon'] = yatra_normalize_icon_picker_for_storage($data['icon']); | |
| 210 | 225 | $data['icon'] = maybe_serialize($data['icon']); |
| 211 | 226 | } |
| 212 | 227 | } |
| 213 | 228 | |
| @@ -212,22 +227,14 @@ | ||
| 212 | 227 | } |
| 213 | 228 | |
| 214 | 229 | // Handle SEO metadata |
| 215 | 230 | if (isset($data['seo_title']) || isset($data['seo_description']) || isset($data['seo_keywords'])) { |
| 216 | - // Get existing metadata | |
| 217 | - $existing_metadata = []; | |
| 218 | - if (isset($data['metadata']) && is_array($data['metadata'])) { | |
| 219 | - $existing_metadata = $data['metadata']; | |
| 220 | - } elseif (isset($data['metadata'])) { | |
| 221 | - $existing_metadata = maybe_unserialize($data['metadata']); | |
| 222 | - } else { | |
| 223 | - // Get existing metadata from database | |
| 224 | - $existing = $this->repository->find($id); | |
| 225 | - if ($existing && isset($existing->metadata)) { | |
| 226 | - $existing_metadata = maybe_unserialize($existing->metadata); | |
| 227 | - } | |
| 228 | - } | |
| 229 | - | |
| 231 | + $existing_metadata = ClassificationLandingPageMetadata::baseMetadataMergedWithRequest( | |
| 232 | + $id, | |
| 233 | + $this->repository, | |
| 234 | + $data | |
| 235 | + ); | |
| 236 | + | |
| 230 | 237 | // Add/update SEO fields in metadata |
| 231 | 238 | if (isset($data['seo_title'])) { |
| 232 | 239 | $existing_metadata['seo_title'] = sanitize_text_field($data['seo_title']); |
| 233 | 240 | unset($data['seo_title']); // Remove from main data |
| @@ -242,8 +249,10 @@ | ||
| 242 | 249 | } |
| 243 | 250 | |
| 244 | 251 | $data['metadata'] = $existing_metadata; |
| 245 | 252 | } |
| 253 | + | |
| 254 | + ClassificationLandingPageMetadata::mergeLandingPageIntoData($data, $id, $this->repository); | |
| 246 | 255 | |
| 247 | 256 | // Sanitize metadata if it's an array |
| 248 | 257 | if (isset($data['metadata'])) { |
| 249 | 258 | if (is_array($data['metadata'])) { |