2008-11-05 05:32:47 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
|
/**
|
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
2009-05-13 20:04:58 +00:00
|
|
|
* Copyright (C) 2000-2009 Bharat Mediratta
|
2008-11-05 05:32:47 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
|
* your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
2009-10-19 12:53:44 -07:00
|
|
|
class Group_Model extends ORM implements Group_Definition {
|
2008-11-14 05:09:07 +00:00
|
|
|
protected $has_and_belongs_to_many = array("users");
|
2008-11-18 00:38:36 +00:00
|
|
|
|
2010-01-16 21:15:12 -08:00
|
|
|
var $rules = array("name" => array("rules" => array("required", "length[4,255]")));
|
2008-12-09 08:47:30 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see ORM::delete()
|
|
|
|
|
*/
|
|
|
|
|
public function delete($id=null) {
|
2009-07-16 11:19:34 -07:00
|
|
|
$old = clone $this;
|
2008-12-09 08:47:30 +00:00
|
|
|
module::event("group_before_delete", $this);
|
|
|
|
|
parent::delete($id);
|
2009-07-16 11:19:34 -07:00
|
|
|
module::event("group_deleted", $old);
|
2008-12-09 08:47:30 +00:00
|
|
|
}
|
2009-07-16 12:29:16 -07:00
|
|
|
|
2009-12-17 21:32:53 -08:00
|
|
|
public function users() {
|
|
|
|
|
return $this->users->find_all();
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-16 21:15:12 -08:00
|
|
|
/**
|
|
|
|
|
* Add some custom per-instance rules.
|
|
|
|
|
*/
|
|
|
|
|
public function validate($array=null) {
|
|
|
|
|
// validate() is recursive, only modify the rules on the outermost call.
|
|
|
|
|
if (!$array) {
|
|
|
|
|
$this->rules["name"]["callbacks"] = array(array($this, "valid_name"));
|
2009-07-16 12:29:16 -07:00
|
|
|
}
|
2010-01-16 00:13:28 -08:00
|
|
|
|
2010-01-16 21:15:12 -08:00
|
|
|
parent::validate($array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function save() {
|
|
|
|
|
if (!$this->loaded()) {
|
|
|
|
|
// New group
|
|
|
|
|
parent::save();
|
2009-07-16 12:29:16 -07:00
|
|
|
module::event("group_created", $this);
|
|
|
|
|
} else {
|
2010-01-16 21:15:12 -08:00
|
|
|
// Updated group
|
|
|
|
|
$original = clone $this->original();
|
|
|
|
|
parent::save();
|
2010-01-16 00:13:28 -08:00
|
|
|
module::event("group_updated", $original, $this);
|
2009-07-16 12:29:16 -07:00
|
|
|
}
|
2010-01-16 21:15:12 -08:00
|
|
|
|
2009-07-16 12:29:16 -07:00
|
|
|
return $this;
|
|
|
|
|
}
|
2010-01-16 21:15:12 -08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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", "in_use");
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-11-05 05:32:47 +00:00
|
|
|
}
|