/** * 静态方法, 单例统一访问入口 * @return object 返回对象的唯一实例 */ public static function getInstance() { if (is_null(self::$_instance) || !isset(self::$_instance)) { self::$_instance = new self(); } return self::$_instance; }
public function modifyData() { $criteria = new CDbCriteria(); $items = Group::model()->findAll($criteria); foreach ($items as $item) { $groupRelation = MiniGroupRelation::getInstance()->getByGroupId($item->id); $group = Group::model()->findByPk($item->id); $group->parent_group_id = $groupRelation['parent_group_id']; $group->save(); } }
private function getGroupIds($groupId, $ids) { $group = MiniGroupRelation::getInstance()->getByGroupId($groupId); if (isset($group)) { if ($group['parent_group_id'] != -1) { array_push($ids, $group['parent_group_id']); return $this->getGroupIds($group['parent_group_id'], $ids); } else { return $ids; } } }
private function getGroups($departmentId) { $departments = MiniGroupRelation::getInstance()->getByParentId($departmentId); if (count($departments) > 0) { foreach ($departments as $department) { $userGroups = MiniUserGroupRelation::getInstance()->getByGroupId($department['group_id']); if (count($userGroups) > 0) { foreach ($userGroups as $userGroup) { $this->ids[] = $userGroup['user_id']; } } $this->getGroups($department['group_id']); } } else { return; } }
/** * 获取部门ID */ private function getId($data, $parentId = -1, $index = 0, $createCount = 0) { for ($i = $index; $i < count($data); $i++) { $groupRelations = MiniGroupRelation::getInstance()->getByParentId($parentId); $isExist = false; if (!empty($groupRelations)) { foreach ($groupRelations as $groupRelation) { $group = MiniGroup::getInstance()->findById($groupRelation['group_id']); if ($group['group_name'] == $data[$i]) { $isExist = true; $groupId = $groupRelation['group_id']; break; } } } if (!$isExist) { $id = $this->create($data[$i], $parentId); $groupRelation = MiniGroupRelation::getInstance()->getById($id); $groupId = $groupRelation['group_id']; $createCount++; } if ($i == count($data) - 1) { return $groupId; } else { return $this->getId($data, $groupId, $index + 1, $createCount); } } }
/** * 获取目录树 * @param $parentGroupId * @param bool $showUser * @return array */ public function getTreeNodes($parentGroupId, $showUser = true) { $relations = MiniGroupRelation::getInstance()->getByParentId($parentGroupId); $userRelations = MiniUserGroupRelation::getInstance()->getByGroupId($parentGroupId); if (isset($relations)) { foreach ($relations as $relation) { $group = $this->getById($relation['group_id']); $newGroup[] = $group['id']; $newGroup[] = $group['group_name']; $groups[] = $group; } } if (0 < count($groups)) { for ($i = 0; $i < count($groups); $i++) { $groups[$i]['nodes'] = $this->getTreeNodes($groups[$i]['id'], $showUser); if ($groups[$i]['nodes'] == NULL) { $groups[$i]['nodes'] = array(); } } } if ($showUser) { if ($userRelations) { foreach ($userRelations as $userRelation) { $user = array(); $userInfo = MiniUser::getInstance()->getById($userRelation['user_id']); $user['id'] = $userInfo['id']; $user['user_name'] = $userInfo['nick']; $user['group_id'] = $parentGroupId; $groups[] = $user; } } } return $groups; }
public function getId($item, $data, $parentId = -1, $index = 0, $createCount = 0) { for ($i = $index; $i < count($data); $i++) { $groupRelations = MiniGroupRelation::getInstance()->getByParentId($parentId); $isExist = false; if (!empty($groupRelations)) { foreach ($groupRelations as $groupRelation) { $group = MiniGroup::getInstance()->findById($groupRelation['group_id']); if ($group['group_name'] == $data[$i]) { $isExist = true; $groupId = $groupRelation['group_id']; break; } } } if (!$isExist) { $id = $this->create($data[$i], $parentId); // $id = Yii::app()->db->getLastInsertID(); $groupRelation = MiniGroupRelation::getInstance()->getById($id); $groupId = $groupRelation['group_id']; $createCount++; } if ($i == count($data) - 1) { if ($createCount == 0) { if (count($item) == 1 && strlen(trim($item[0]))) { $error = array(); $this->duplicateCount = $this->duplicateCount + 1; $error[] = "数据库中已经有相同的数据出现"; $error[] = $item[0]; $this->errorList[] = $error; } } return $groupId; } else { return $this->getId($item, $data, $groupId, $index + 1, $createCount); } } }