| @@ -46,13 +46,18 @@ | ||
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | public function createLabel($labelData, $boardId) |
| 49 | 49 | { |
| 50 | + $labelData = $this->normalizeLabelColorData($labelData); | |
| 51 | + | |
| 50 | 52 | $label = new Label(); |
| 51 | 53 | $label->board_id = $boardId; |
| 52 | - $label->title = $labelData['label']; | |
| 53 | - $label->bg_color = $labelData['bg_color']; | |
| 54 | - $label->color = $labelData['color']; | |
| 54 | + $label->title = $labelData['label'] ?? ''; | |
| 55 | + $label->bg_color = $labelData['bg_color'] ?? ''; | |
| 56 | + $label->color = $labelData['color'] ?? ''; | |
| 57 | + if (isset($labelData['settings'])) { | |
| 58 | + $label->settings = $labelData['settings']; | |
| 59 | + } | |
| 55 | 60 | $label->save(); |
| 56 | 61 | |
| 57 | 62 | return $label; |
| 58 | 63 | } |
| @@ -58,29 +63,26 @@ | ||
| 58 | 63 | } |
| 59 | 64 | |
| 60 | 65 | public function createDefaultLabel($boardId) |
| 61 | 66 | { |
| 62 | - $defaultColors = [ | |
| 63 | - "green" => "#4bce97", | |
| 64 | - "yellow" => "#f5cd47", | |
| 65 | - "orange" => "#fea362", | |
| 66 | - "red" => "#f87168", | |
| 67 | - "purple" => "#9f8fef" | |
| 68 | - ]; | |
| 67 | + $defaultColors = ['green-bold', 'yellow-bold', 'orange-bold', 'red-bold', 'purple-bold']; | |
| 69 | 68 | |
| 70 | 69 | $data = []; |
| 71 | 70 | |
| 72 | - foreach ($defaultColors as $index => $bg_color) | |
| 71 | + foreach ($defaultColors as $presetId) | |
| 73 | 72 | { |
| 73 | + $preset = Constant::getLabelColorPreset($presetId); | |
| 74 | + $colorName = strtok($presetId, '-'); | |
| 74 | 75 | $data[] = [ |
| 75 | 76 | 'board_id' => $boardId, |
| 76 | 77 | // Titles match the create-board modal defaults, otherwise boards created |
| 77 | 78 | // outside that modal end up with colour chips carrying no text. |
| 78 | - 'title' => ucfirst($index), | |
| 79 | - 'slug' => $index, | |
| 79 | + 'title' => ucfirst($colorName), | |
| 80 | + 'slug' => $colorName, | |
| 80 | 81 | 'type' => 'label', |
| 81 | - 'bg_color' => $bg_color, | |
| 82 | - 'color' => Constant::TEXT_COLOR_MAP[$index], | |
| 82 | + 'bg_color' => $preset['light_bg_color'], | |
| 83 | + 'color' => $preset['light_text_color'], | |
| 84 | + 'settings' => maybe_serialize([Constant::LABEL_COLOR_PRESET_SETTING => $presetId]), | |
| 83 | 85 | 'created_at' => current_time('mysql'), |
| 84 | 86 | 'updated_at' => current_time('mysql') |
| 85 | 87 | ]; |
| 86 | 88 | } |
| @@ -132,9 +134,10 @@ | ||
| 132 | 134 | |
| 133 | 135 | public function editLabelofBoard($labelData, $id, $boardId = null) |
| 134 | 136 | { |
| 135 | 137 | $label = $boardId ? $this->findLabelOnBoard($id, $boardId) : Label::findOrFail($id); |
| 136 | - $label->title = $labelData['label']; | |
| 138 | + $labelData = $this->normalizeLabelColorData($labelData, $label); | |
| 139 | + $label->title = $labelData['label'] ?? $label->title; | |
| 137 | 140 | |
| 138 | 141 | // Background and text colour move independently: coupling them dropped a |
| 139 | 142 | // text-colour-only change on the floor while still reporting success. |
| 140 | 143 | if (isset($labelData['bg_color']) && $labelData['bg_color'] !== '') { |
| @@ -144,8 +147,16 @@ | ||
| 144 | 147 | if (isset($labelData['color']) && $labelData['color'] !== '') { |
| 145 | 148 | $label->color = $labelData['color']; |
| 146 | 149 | } |
| 147 | 150 | |
| 151 | + if (array_key_exists('settings', $labelData)) { | |
| 152 | + if (array_key_exists('color_preset', $labelData) && $labelData['color_preset'] === '') { | |
| 153 | + $label->replaceSettings($labelData['settings']); | |
| 154 | + } else { | |
| 155 | + $label->settings = $labelData['settings']; | |
| 156 | + } | |
| 157 | + } | |
| 158 | + | |
| 148 | 159 | $label->save(); |
| 149 | 160 | return $label; |
| 150 | 161 | } |
| 151 | 162 | |
| @@ -186,13 +197,61 @@ | ||
| 186 | 197 | $labelToSave['type'] = 'label'; |
| 187 | 198 | $labelToSave['position'] = 0; |
| 188 | 199 | $labelToSave['color'] = $label->color; |
| 189 | 200 | $labelToSave['bg_color'] = $label->bg_color; |
| 201 | + $settings = (array) $label->settings; | |
| 202 | + $presetId = $settings[Constant::LABEL_COLOR_PRESET_SETTING] ?? ''; | |
| 203 | + if (Constant::getLabelColorPreset($presetId)) { | |
| 204 | + $labelToSave['settings'] = [Constant::LABEL_COLOR_PRESET_SETTING => $presetId]; | |
| 205 | + } | |
| 190 | 206 | $copiedLabel = Label::create($labelToSave); |
| 191 | 207 | |
| 192 | 208 | $labelMap[$label['id']] = $copiedLabel->id; |
| 193 | 209 | } |
| 194 | 210 | return $labelMap; |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * Converts a selected preset into stable light-mode fallback colors. | |
| 215 | + * | |
| 216 | + * @param array $labelData | |
| 217 | + * @param Label|null $label | |
| 218 | + * @return array | |
| 219 | + * @throws \Exception | |
| 220 | + */ | |
| 221 | + private function normalizeLabelColorData($labelData, $label = null) | |
| 222 | + { | |
| 223 | + if (!array_key_exists('color_preset', $labelData)) { | |
| 224 | + return $labelData; | |
| 225 | + } | |
| 226 | + | |
| 227 | + $presetId = $labelData['color_preset']; | |
| 228 | + $settings = $label ? (array) $label->settings : []; | |
| 229 | + | |
| 230 | + // Framework request extraction can represent an omitted optional field | |
| 231 | + // as null. That must preserve an existing preset rather than reject it. | |
| 232 | + if ($presetId === null) { | |
| 233 | + unset($labelData['color_preset']); | |
| 234 | + return $labelData; | |
| 235 | + } | |
| 236 | + | |
| 237 | + if ($presetId === '') { | |
| 238 | + unset($settings[Constant::LABEL_COLOR_PRESET_SETTING]); | |
| 239 | + $labelData['settings'] = $settings; | |
| 240 | + return $labelData; | |
| 241 | + } | |
| 242 | + | |
| 243 | + $preset = Constant::getLabelColorPreset($presetId); | |
| 244 | + if (!$preset) { | |
| 245 | + throw new \Exception(esc_html__('Invalid label color preset', 'fluent-boards')); | |
| 246 | + } | |
| 247 | + | |
| 248 | + $settings[Constant::LABEL_COLOR_PRESET_SETTING] = $preset['id']; | |
| 249 | + $labelData['settings'] = $settings; | |
| 250 | + $labelData['bg_color'] = $preset['light_bg_color']; | |
| 251 | + $labelData['color'] = $preset['light_text_color']; | |
| 252 | + | |
| 253 | + return $labelData; | |
| 195 | 254 | } |
| 196 | 255 | public function getLastOneMinuteUpdatedLabels($boardId, $lastUpdated = null, $includeArchived = true) |
| 197 | 256 | { |
| 198 | 257 | if (!$lastUpdated) { |