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
|
2013-01-21 01:22:01 -05:00
|
|
|
* Copyright (C) 2000-2013 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.
|
|
|
|
|
*/
|
2010-10-25 21:04:48 -07:00
|
|
|
class Group_Model_Core extends ORM implements Group_Definition {
|
2008-11-14 05:09:07 +00:00
|
|
|
protected $has_and_belongs_to_many = array("users");
|
2010-12-15 16:28:18 -08:00
|
|
|
protected $users_cache = null;
|
2008-11-18 00:38:36 +00:00
|
|
|
|
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);
|
2011-08-04 20:29:06 -07:00
|
|
|
|
|
|
|
|
db::build()
|
|
|
|
|
->delete("groups_users")
|
|
|
|
|
->where("group_id", "=", empty($id) ? $old->id : $id)
|
|
|
|
|
->execute();
|
|
|
|
|
|
2009-07-16 11:19:34 -07:00
|
|
|
module::event("group_deleted", $old);
|
2010-12-15 19:57:09 -08:00
|
|
|
$this->users_cache = null;
|
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() {
|
2010-12-15 16:28:18 -08:00
|
|
|
if (!$this->users_cache) {
|
2010-12-15 19:57:09 -08:00
|
|
|
$this->users_cache = $this->users->find_all()->as_array();
|
2010-12-15 16:28:18 -08:00
|
|
|
}
|
|
|
|
|
return $this->users_cache;
|
2009-12-17 21:32:53 -08:00
|
|
|
}
|
|
|
|
|
|
2010-01-16 21:15:12 -08:00
|
|
|
/**
|
2010-01-17 12:30:24 -08:00
|
|
|
* Specify our rules here so that we have access to the instance of this model.
|
2010-01-16 21:15:12 -08:00
|
|
|
*/
|
2010-01-29 14:04:27 -08:00
|
|
|
public function validate(Validation $array=null) {
|
2010-01-16 21:15:12 -08:00
|
|
|
// validate() is recursive, only modify the rules on the outermost call.
|
|
|
|
|
if (!$array) {
|
2010-01-17 12:30:24 -08:00
|
|
|
$this->rules = array(
|
2010-09-22 21:43:44 -07:00
|
|
|
"name" => array("rules" => array("required", "length[1,255]"),
|
2010-01-17 12:30:24 -08:00
|
|
|
"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
|
2010-01-27 22:34:11 -08:00
|
|
|
$original = ORM::factory("group", $this->id);
|
2010-01-16 21:15:12 -08:00
|
|
|
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
|
|
|
|
2010-12-15 19:57:09 -08:00
|
|
|
$this->users_cache = null;
|
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) {
|
2010-01-17 12:30:24 -08:00
|
|
|
$v->add_error("name", "conflict");
|
2010-01-16 21:15:12 -08:00
|
|
|
}
|
|
|
|
|
}
|
2008-11-05 05:32:47 +00:00
|
|
|
}
|