| @@ -139,21 +139,56 @@ | ||
| 139 | 139 | 'document_lists' => [], |
| 140 | 140 | ]; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - protected static function generateNewSlug($newModel) | |
| 143 | + /** | |
| 144 | + * A lesson slug only has to be unique among the lessons of one course. | |
| 145 | + * | |
| 146 | + * A lesson is read at course/{courseSlug}/lessons/{lessonSlug} and | |
| 147 | + * CourseController::getLessonBySlug() looks it up with a space_id filter, so the | |
| 148 | + * same slug in two courses never competes. The type global scope keeps this off | |
| 149 | + * the other row types sharing fcom_posts. | |
| 150 | + * | |
| 151 | + * Every write path goes through here - the creating hook below for generated | |
| 152 | + * slugs, CourseAdminController::patchLesson() for author supplied ones. A | |
| 153 | + * collision gets a -{time()} suffix. | |
| 154 | + * | |
| 155 | + * @param string $slug | |
| 156 | + * @param int|null $courseId the owning course, fcom_posts.space_id | |
| 157 | + * @param int|null $ignoreId the lesson being renamed, so it can keep its own slug | |
| 158 | + * @param string $fallbackTitle used when $slug sanitizes down to nothing | |
| 159 | + * @return string | |
| 160 | + */ | |
| 161 | + public static function uniqueSlug($slug, $courseId, $ignoreId = null, $fallbackTitle = '') | |
| 144 | 162 | { |
| 145 | - $slug = Utility::slugify($newModel->title, 'lesson-' . time()); | |
| 163 | + $slug = sanitize_title($slug); | |
| 146 | 164 | |
| 147 | - // check if the slug is available for this type | |
| 148 | - $exist = self::where('slug', $slug) | |
| 149 | - ->exists(); | |
| 165 | + if (!$slug) { | |
| 166 | + $slug = Utility::slugify($fallbackTitle, 'lesson-' . time()); | |
| 167 | + } | |
| 150 | 168 | |
| 151 | - if ($exist) { | |
| 169 | + $query = self::where('slug', $slug); | |
| 170 | + | |
| 171 | + if ($courseId) { | |
| 172 | + $query->where('space_id', $courseId); | |
| 173 | + } else { | |
| 174 | + $query->whereNull('space_id'); | |
| 175 | + } | |
| 176 | + | |
| 177 | + if ($ignoreId) { | |
| 178 | + $query->where('id', '!=', $ignoreId); | |
| 179 | + } | |
| 180 | + | |
| 181 | + if ($query->exists()) { | |
| 152 | 182 | $slug = $slug . '-' . time(); |
| 153 | 183 | } |
| 154 | 184 | |
| 155 | 185 | return $slug; |
| 186 | + } | |
| 187 | + | |
| 188 | + protected static function generateNewSlug($newModel) | |
| 189 | + { | |
| 190 | + return self::uniqueSlug('', $newModel->space_id, null, $newModel->title); | |
| 156 | 191 | } |
| 157 | 192 | |
| 158 | 193 | public function topic() |
| 159 | 194 | { |