浏览代码

Merge pull request #52 from dereuromark/master-slugged

Multi slug is now properly handled by slug() method
Mark 11 年之前
父节点
当前提交
563852dbbd
共有 1 个文件被更改,包括 26 次插入31 次删除
  1. 26 31
      Model/Behavior/SluggedBehavior.php

+ 26 - 31
Model/Behavior/SluggedBehavior.php

@@ -167,12 +167,8 @@ class SluggedBehavior extends ModelBehavior {
 	/**
 	 * Generate slug method
 	 *
-	 * if a new row, or overwrite is set to true, check for a change to a label field and add the slug to the data
+	 * If a new row, or overwrite is set to true, check for a change to a label field and add the slug to the data
 	 * to be saved
-	 * If no slug at all is returned (should not be permitted and prevented by validating the label fields) the model
-	 * alias will be used as a slug.
-	 * If unique is set to true, check for a unique slug and if unavailable suffix the slug with -1, -2, -3 etc.
-	 * until a unique slug is found
 	 *
 	 * @param Model $Model
 	 * @return void
@@ -220,30 +216,6 @@ class SluggedBehavior extends ModelBehavior {
 				$slug = $this->display($Model);
 			}
 			$slug = $Model->slug($slug);
-			if (!$slug) {
-				$slug = $Model->alias;
-			}
-			if ($unique) {
-				$conditions = array($Model->alias . '.' . $slugField => $slug);
-				$conditions = array_merge($conditions, $this->settings[$Model->alias]['scope']);
-				if ($Model->id) {
-					$conditions['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
-				}
-				$i = 0;
-				$suffix = '';
-
-				while ($Model->hasAny($conditions)) {
-					$i++;
-					$suffix	= $separator . $i;
-					if (strlen($slug . $suffix) > $length) {
-						$slug = substr($slug, 0, $length - strlen($suffix));
-					}
-					$conditions[$Model->alias . '.' . $slugField] = $slug . $suffix;
-				}
-				if ($suffix) {
-					$slug .= $suffix;
-				}
-			}
 			$this->_addToWhitelist($Model, array($slugField));
 			$Model->data[$Model->alias][$slugField] = $slug;
 		}
@@ -361,6 +333,8 @@ class SluggedBehavior extends ModelBehavior {
 	 * (only possible if directly called - primarily for tracing and testing) separators will not be cleaned up
 	 * and so slugs like "-----as---df-----" are possible, which by default would otherwise be returned as "as-df".
 	 * If the mode is "id" and the first charcter of the regex-ed slug is numeric, it will be prefixed with an x.
+	 * If unique is set to true, check for a unique slug and if unavailable suffix the slug with -1, -2, -3 etc.
+	 * until a unique slug is found
 	 *
 	 * @param Model $Model
 	 * @param mixed $string
@@ -419,6 +393,27 @@ class SluggedBehavior extends ModelBehavior {
 				}
 			}
 		}
+		if ($unique) {
+			$conditions = array($Model->alias . '.' . $slugField => $slug);
+			$conditions = array_merge($conditions, $this->settings[$Model->alias]['scope']);
+			if ($Model->id) {
+				$conditions['NOT'][$Model->alias . '.' . $Model->primaryKey] = $Model->id;
+			}
+			$i = 0;
+			$suffix = '';
+
+			while ($Model->hasAny($conditions)) {
+				$i++;
+				$suffix	= $separator . $i;
+				if (strlen($slug . $suffix) > $length) {
+					$slug = substr($slug, 0, $length - strlen($suffix));
+				}
+				$conditions[$Model->alias . '.' . $slugField] = $slug . $suffix;
+			}
+			if ($suffix) {
+				$slug .= $suffix;
+			}
+		}
 
 		return $slug;
 	}
@@ -498,7 +493,7 @@ class SluggedBehavior extends ModelBehavior {
 	/**
 	 * Multi slug method
 	 *
-	 * Handle both slug and lable fields using the translate behavior, and being edited
+	 * Handle both slug and label fields using the translate behavior, and being edited
 	 * in multiple locales at once
 	 *
 	 * @param Model $Model
@@ -511,7 +506,7 @@ class SluggedBehavior extends ModelBehavior {
 		foreach ($Model->data[$Model->alias][$field] as $locale => $_) {
 			foreach ($label as $field) {
 				if (is_array($data[$Model->alias][$field])) {
-					$Model->data[$Model->alias][$field] = $data[$Model->alias][$field][$locale];
+					$Model->data[$Model->alias][$field] = $Model->slug($data[$Model->alias][$field][$locale]);
 				}
 			}
 			$this->beforeValidate($Model);