diff options
| author | Nathan Kinkade <nkinkade@nkinka.de> | 2010-02-02 02:59:20 +0000 |
|---|---|---|
| committer | Nathan Kinkade <nkinkade@nkinka.de> | 2010-02-02 02:59:20 +0000 |
| commit | 9d0927dda936756f1f5003813f437d714fe481f8 (patch) | |
| tree | fe1b887345b37387ab0ddcfd78bf344f6150b6cc /modules/user/models/group.php | |
| parent | a6f794c20dc3592bcaef17c622413c1b670a20d8 (diff) | |
| parent | 43985ea2fb137aa7d532617271e37d7c20def3c5 (diff) | |
Merge branch 'master' of git://github.com/gallery/gallery3
Diffstat (limited to 'modules/user/models/group.php')
| -rw-r--r-- | modules/user/models/group.php | 41 |
1 files changed, 32 insertions, 9 deletions
diff --git a/modules/user/models/group.php b/modules/user/models/group.php index 10f6f4b3..82843ad1 100644 --- a/modules/user/models/group.php +++ b/modules/user/models/group.php @@ -20,9 +20,6 @@ class Group_Model extends ORM implements Group_Definition { protected $has_and_belongs_to_many = array("users"); - var $form_rules = array( - "name" => "required|length[4,255]"); - /** * @see ORM::delete() */ @@ -37,18 +34,44 @@ class Group_Model extends ORM implements Group_Definition { return $this->users->find_all(); } - public function save() { - if (!$this->loaded()) { - $created = 1; + /** + * Specify our rules here so that we have access to the instance of this model. + */ + public function validate(Validation $array=null) { + // validate() is recursive, only modify the rules on the outermost call. + if (!$array) { + $this->rules = array( + "name" => array("rules" => array("required", "length[4,255]"), + "callbacks" => array(array($this, "valid_name")))); } - $original = clone $this->original(); - parent::save(); - if (isset($created)) { + parent::validate($array); + } + + public function save() { + if (!$this->loaded()) { + // New group + parent::save(); module::event("group_created", $this); } else { + // Updated group + $original = ORM::factory("group", $this->id); + parent::save(); module::event("group_updated", $original, $this); } + return $this; } + + /** + * Validate the user name. Make sure there are no conflicts. + */ + public function valid_name(Validation $v, $field) { + if (db::build()->from("groups") + ->where("name", "=", $this->name) + ->where("id", "<>", $this->id) + ->count_records() == 1) { + $v->add_error("name", "conflict"); + } + } }
\ No newline at end of file |
