Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
validation for unique slugs per content type. Closes croogo#197
  • Loading branch information
fahad19 committed Sep 11, 2011
1 parent bcc09a1 commit 301f99b
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions models/node.php
Expand Up @@ -67,10 +67,10 @@ class Node extends AppModel {
'message' => 'This field cannot be left blank.',
),
'slug' => array(
/*'isUniquePerType' => array(
'isUniquePerType' => array(
'rule' => 'isUniquePerType',
'message' => 'This slug has already been taken.',
),*/
),
'minLength' => array(
'rule' => array('minLength', 1),
'message' => 'Slug cannot be empty.',
Expand Down Expand Up @@ -195,5 +195,50 @@ public function __cacheTerms() {
));
}
}
/**
* Returns false if any fields passed match any (by default, all if $or = false) of their matching values.
*
* @param array $fields Field/value pairs to search (if no values specified, they are pulled from $this->data)
* @param boolean $or If false, all fields specified must match in order for a false return value
* @return boolean False if any records matching any fields are found
* @access public
*/
function isUniquePerType($fields, $or = true) {
if (!is_array($fields)) {
$fields = func_get_args();
if (is_bool($fields[count($fields) - 1])) {
$or = $fields[count($fields) - 1];
unset($fields[count($fields) - 1]);
}
}

foreach ($fields as $field => $value) {
if (is_numeric($field)) {
unset($fields[$field]);

$field = $value;
if (isset($this->data[$this->alias][$field])) {
$value = $this->data[$this->alias][$field];
} else {
$value = null;
}
}

if (strpos($field, '.') === false) {
unset($fields[$field]);
$fields[$this->alias . '.' . $field] = $value;
}
}
if ($or) {
$fields = array('or' => $fields);
}
if (!empty($this->id)) {
$fields[$this->alias . '.' . $this->primaryKey . ' !='] = $this->id;
}
if (!empty($this->type)) {
$fields[$this->alias . '.type'] = $this->type;
}
return ($this->find('count', array('conditions' => $fields, 'recursive' => -1)) == 0);
}
}
?>

0 comments on commit 301f99b

Please sign in to comment.