Newer
Older
<?php
/*********************************************************************
class.group.php
User Group - Everything about a group!
Peter Rotich <peter@osticket.com>
http://www.osticket.com
Released under the GNU General Public License WITHOUT ANY WARRANTY.
See LICENSE.TXT for details.
vim: expandtab sw=4 ts=4 sts=4:
**********************************************************************/
static $meta = array(
'table' => GROUP_TABLE,
'pk' => array('id'),
'joins' => array(
'members' => array(
'null' => true,
'list' => true,
'reverse' => 'Staff.group',
),
'depts' => array(
'null' => true,
'list' => true,
'reverse' => 'GroupDeptAccess.group',
),
'role' => array(
'constraint' => array('role_id' => 'Role.id')
),
),
$base['name'] = $base['name'];
$base['isactive'] = $base['flags'];
function getId() {
return $this->id;
}
function getRoleId() {
return $this->role_id;
function getRole($deptId=0) {
if ($deptId // Department specific role.
&& ($roles=$this->getDepartmentsAccess())
&& isset($roles[$deptId])
&& $roles[$deptId]
&& ($role=Role::lookup($roles[$deptId]))
&& $role->isEnabled())
return $role;
// Default role for this group.
return $this->role;
function getCreateDate() {
return $this->created;
}
function getUpdateDate() {
return $this->updated;
}
function getNumMembers() {
return $this->members ? $this->members->count() : 0;
}
function isEnabled() {
return ($this->get('flags') & self::FLAG_ENABLED !== 0);
}
function isActive(){
return $this->isEnabled();
}
function getTranslateTag($subtag) {
return _H(sprintf('group.%s.%s', $subtag, $this->getId()));
}
function getLocal($subtag) {
$tag = $this->getTranslateTag($subtag);
$T = CustomDataTranslation::translate($tag);
return $T != $tag ? $T : $this->ht[$subtag];
}
static function getLocalById($id, $subtag, $default) {
$tag = _H(sprintf('group.%s.%s', $subtag, $id));
$T = CustomDataTranslation::translate($tag);
return $T != $tag ? $T : $default;
}
//Get members of the group.
function getMembers() {
->filter(array('group_id'=>$this->getId()))
->order_by('lastname', 'firstname')
->all();
//Get departments & roles the group is allowed to access.
return array_keys($this->getDepartmentsAccess());
}
function getDepartmentsAccess() {
if (!isset($this->departments)) {
$this->departments = array();
foreach (GroupDeptAccess::objects()
->filter(array('group_id'=>$this->getId()))
function updateDeptAccess($dept_ids, $vars=array()) {
if (is_array($dept_ids)) {
$groups = GroupDeptAccess::objects()
->filter(array('group_id' => $this->getId()));
foreach ($groups as $group) {
if ($idx = array_search($group->dept_id, $dept_ids)) {
$roleId = $vars['dept'.$group->dept_id.'_role_id'];
if ($roleId != $group->role_id) {
$group->set('role_id', $roleId ?: 0);
$group->save();
}
} else {
'group_id' => $this->getId(),
'dept_id' => $id,
'role_id' => $roleId ?: 0
// Remove dept access entries
GroupDeptAccess::objects()
->filter(array('group_id'=>$this->getId()))
->delete();
function __toString() {
return $this->getName();
function save($refetch=false) {
if ($this->dirty) {
$this->updated = SqlFunction::NOW();
}
return parent::save($refetch || $this->dirty);
if (isset($this->id) && $this->getId() != $vars['id'])
$errors['err'] = __('Missing or invalid group ID');
$errors['name'] = __('Group name must be at least 3 chars.');
} elseif (($gid=static::getIdByName($vars['name']))
&& (!isset($this->id) || $gid!=$this->getId())) {
$errors['name'] = __('Group name already exists');
if (!$vars['role_id'])
$errors['role_id'] = __('Role selection required');
$this->name = Format::striptags($vars['name']);
$this->role_id = $vars['role_id'];
$this->notes = Format::sanitize($vars['notes']);
if ($vars['isactive'])
$this->flags = ($this->flags | self::FLAG_ENABLED);
else
$this->flags = ($this->flags & ~self::FLAG_ENABLED);
return $this->updateDeptAccess($vars['depts'] ?: array(), $vars);
$errors['err']=sprintf(__('Unable to update %s.'), __('this group'))
.' '.__('Internal error occurred');
$errors['err']=sprintf(__('Unable to create %s.'), __('this group'))
.' '.__('Internal error occurred');
/*** Static functions ***/
static function getIdByName($name){
$id = static::objects()->filter(array('name'=>trim($name)))
->values_flat('id')->first();
return $id ? $id[0] : 0;
}
static function create($vars=false) {
if (!isset($vars['flags']))
$vars['flags'] = 0;
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
$group = parent::create($vars);
$group->created = SqlFunction::NOW();
return $group;
}
static function __create($vars, &$errors) {
$g = self::create($vars);
$g->save();
if ($vars['depts'])
$g->updateDeptAccess($vars['depts'], $vars);
return $g;
}
static function getGroups($criteria=array()) {
static $groups = null;
if (!isset($groups) || $criteria) {
$groups = array();
$query = static::objects()
->values_flat('id', 'name', 'flags')
->order_by('name');
$filters = array();
if (isset($criteria['active']))
$filters += array(
'isactive' => $criteria['active'] ? 1 : 0);
if ($filters)
$query->filter($filters);
$names = array();
foreach ($query as $row) {
list($id, $name, $flags) = $row;
$names[$id] = sprintf('%s%s',
self::getLocalById($id, 'name', $name),
$flags ? '' : ' ' . __('(disabled)'));
}
//TODO: sort if $criteria['localize'];
if ($criteria)
return $names;
$groups = $names;
}
return $groups;
}
static function getActiveGroups() {
static $groups = null;
if (!isset($groups))
$groups = self::getGroups(array('active'=>true));
return $groups;
}