function setUser($ids) { Debug::text('Setting User IDs : ', __FILE__, __LINE__, __METHOD__, 10); if (is_array($ids) and count($ids) > 0) { //Remove any of the selected employees from other permission control objects first. //So there we can switch employees from one group to another in a single action. $pulf = new PermissionUserListFactory(); //$pculf->getByCompanyIdAndUserId( $this->getCompany(), $ids ); $pulf->getByCompanyIdAndUserIdAndNotPermissionControlId($this->getCompany(), $ids, (int) $this->getId()); if ($pulf->getRecordCount() > 0) { Debug::text('Found User IDs assigned to another Permission Group, unassigning them!', __FILE__, __LINE__, __METHOD__, 10); foreach ($pulf as $pu_obj) { $pu_obj->Delete(); } } unset($pulf, $pu_obj); $tmp_ids = array(); $pf = new PermissionFactory(); if (!$this->isNew()) { //If needed, delete mappings first. $pulf = new PermissionUserListFactory(); $pulf->getByPermissionControlId($this->getId()); $tmp_ids = array(); foreach ($pulf as $obj) { $id = $obj->getUser(); Debug::text('Permission Control ID: ' . $obj->getPermissionControl() . ' ID: ' . $id, __FILE__, __LINE__, __METHOD__, 10); //Delete users that are not selected. if (!in_array($id, $ids)) { Debug::text('Deleting: ' . $id, __FILE__, __LINE__, __METHOD__, 10); $this->tmp_previous_user_ids[] = $id; $obj->Delete(); } else { //Save ID's that need to be updated. Debug::text('NOT Deleting : ' . $id, __FILE__, __LINE__, __METHOD__, 10); $tmp_ids[] = $id; } } unset($id, $obj); } //Insert new mappings. $ulf = new UserListFactory(); foreach ($ids as $id) { if (isset($ids) and !in_array($id, $tmp_ids)) { //Remove users from any other permission control object //first, otherwise there is a gab where an employee has //no permissions, this is especially bad for administrators //who are currently logged in. $puf = new PermissionUserFactory(); $puf->setPermissionControl($this->getId()); $puf->setUser($id); $obj = $ulf->getById($id)->getCurrent(); if ($this->Validator->isTrue('user', $puf->Validator->isValid(), TTi18n::gettext('Selected employee is invalid, or already assigned to another permission group') . ' (' . $obj->getFullName() . ')')) { $puf->save(); } } } return TRUE; } Debug::text('No User IDs to set.', __FILE__, __LINE__, __METHOD__, 10); return FALSE; }