context->table('lusergroups')->where('name=?', $name); if (count($rows)>0) { // Should return only a single row foreach($rows as $row) { return $database->createModel('Usergroup', $row); } } // It doesn't exist, create $activerow = $database->context->table('lusergroups')->insert(['name' => $name]); return $database->createModel('Usergroup', $activerow); } public function getName() { return $this->activerow->name; } public function setName($name) { $this->activerow->update(['name' => $name]); } public function Isadmin($user) { $rows = $this->context->table('lusergroupmembers') ->where('luser.name', $user)->where('lusergroup = ?', $this->activerow->lusergroup); foreach($rows as $row) return $row->admin; return false; } public function getUsers() { $rows = $this->context->table('lusers') ->where(':lusergroupmembers.lusergroup', $this->activerow->lusergroup); $users = []; $database = $this->database; foreach($rows as $row) { $e = $database->createModel('User', $row); $users[strtolower($e->name)] = $e; } return $users; } public function setUsers($users) { $oldusers = $this->getUsers(); $newusers = []; $database = $this->database; $context = $this->context; $usergroup = $this->primary; // own database ID foreach($users as $newuser) { if(is_object($newuser)) { $key = strtolower($newuser->user); } else { $key = strtolower($newuser); if(array_key_exists($key, $oldusers)) $newuser = $oldusers[$key]; else $newuser = Emailuser::create($database, $newuser); } $newusers[$key] = $newuser; if(!array_key_exists($key, $oldusers)) $newuser->attach_to_usergroup($usergroup); } foreach($oldusers as $olduser) if(!array_key_exists(strtolower($olduser->user), $newusers)) $olduser->detach_from_usergroup($usergroup); } }