function isUniqueUser($id)
    {
        $hcf = new HierarchyControlFactory();
        $hotf = new HierarchyObjectTypeFactory();
        $ph = array('hierarchy_control_id' => $this->getHierarchyControl(), 'id' => $id);
        //$query = 'select a.id from '. $this->getTable() .' as a, '. $pglf->getTable() .' as b where a.hierarchy_control_id = b.id AND a.user_id = ? AND b.deleted=0';
        $query = '
					select *
					from ' . $hotf->getTable() . ' as a
					LEFT JOIN ' . $this->getTable() . ' as b ON a.hierarchy_control_id = b.hierarchy_control_id
					LEFT JOIN ' . $hcf->getTable() . ' as c ON a.hierarchy_control_id = c.id
					WHERE a.object_type_id in (
							select object_type_id
							from hierarchy_object_type
							where hierarchy_control_id = ? )
					AND b.user_id = ?
					AND c.deleted = 0
				';
        $user_id = $this->db->GetOne($query, $ph);
        //Debug::Arr($user_id,'Unique User ID: '. $user_id, __FILE__, __LINE__, __METHOD__,10);
        if ($user_id === FALSE) {
            return TRUE;
        }
        return FALSE;
    }
    function getHierarchyChildrenByCompanyIdAndUserIdAndObjectTypeID($company_id, $user_id, $object_type_id = 100)
    {
        global $profiler;
        $profiler->startTimer("getPermissionHierarchyChildrenByCompanyIdAndUserId");
        if ($company_id == '') {
            return FALSE;
        }
        if ($user_id == '') {
            return FALSE;
        }
        if ($object_type_id == '') {
            return FALSE;
        }
        $retval = FALSE;
        $uf = new UserFactory();
        $hlf = new HierarchyLevelFactory();
        $huf = new HierarchyUserFactory();
        $hotf = new HierarchyObjectTypeFactory();
        $hcf = new HierarchyControlFactory();
        //When it comes to permissions we only consider subordinates, not other supervisors/managers in the hierarchy.
        $ph = array('user_id' => $user_id, 'company_id' => $company_id);
        //w.user_id != x.user_id, is there to make sure we exclude the current user from the subordinate list,
        //as we now allow superiors to also be subordinates in the same hierarchy.
        $query = '
						select w.user_id as user_id
						from ' . $huf->getTable() . ' as w
						LEFT JOIN ' . $hlf->getTable() . ' as x ON w.hierarchy_control_id = x.hierarchy_control_id
						LEFT JOIN ' . $hotf->getTable() . ' as y ON w.hierarchy_control_id = y.hierarchy_control_id
						LEFT JOIN ' . $uf->getTable() . ' as z ON x.user_id = z.id
						LEFT JOIN ' . $hcf->getTable() . ' as z2 ON w.hierarchy_control_id = z2.id
						WHERE
							x.user_id = ?
							AND z.company_id = ?
							AND y.object_type_id in (' . $this->getListSQL($object_type_id, $ph) . ')
							AND w.user_id != x.user_id
							AND ( x.deleted = 0 AND z2.deleted = 0 AND z.deleted = 0 )
					';
        //Debug::Text(' Query: '. $query, __FILE__, __LINE__, __METHOD__,10);
        $rs = $this->db->Execute($query, $ph);
        //Debug::Text(' Rows: '. $rs->RecordCount(), __FILE__, __LINE__, __METHOD__,10);
        if ($rs->RecordCount() > 0) {
            foreach ($rs as $row) {
                $retval[] = $row['user_id'];
            }
        }
        $profiler->stopTimer("getPermissionHierarchyChildrenByCompanyIdAndUserId");
        return $retval;
    }
    function getHierarchyChildrenByCompanyIdAndUserIdAndObjectTypeID($company_id, $user_id, $object_type_id = 100)
    {
        global $profiler;
        $profiler->startTimer("getPermissionHierarchyChildrenByCompanyIdAndUserId");
        if ($company_id == '') {
            return FALSE;
        }
        if ($user_id == '') {
            return FALSE;
        }
        if ($object_type_id == '') {
            return FALSE;
        }
        $retval = FALSE;
        $uf = new UserFactory();
        $hlf = new HierarchyLevelFactory();
        $huf = new HierarchyUserFactory();
        $hotf = new HierarchyObjectTypeFactory();
        $hcf = new HierarchyControlFactory();
        //When it comes to permissions we only consider subordinates, not other supervisors/managers in the hierarchy.
        $ph = array('user_id' => $user_id, 'object_type_id' => $object_type_id, 'company_id' => $company_id);
        $query = '
						select w.user_id as user_id
						from ' . $huf->getTable() . ' as w
						LEFT JOIN ' . $hlf->getTable() . ' as x ON w.hierarchy_control_id = x.hierarchy_control_id
						LEFT JOIN ' . $hotf->getTable() . ' as y ON w.hierarchy_control_id = y.hierarchy_control_id
						LEFT JOIN ' . $uf->getTable() . ' as z ON x.user_id = z.id
						LEFT JOIN ' . $hcf->getTable() . ' as z2 ON w.hierarchy_control_id = z2.id
						WHERE
							x.user_id = ?
							AND y.object_type_id = ?
							AND z.company_id = ?
							AND z2.deleted = 0
					';
        /*
        		$query = '
        						select a.user_id as user_id
        						from '. $hlf->getTable() .' as a
        						LEFT JOIN '. $hlf->getTable() .' as b ON a.hierarchy_control_id = b.hierarchy_control_id AND b.user_id = ?
        						LEFT JOIN '. $hotf->getTable() .' as c ON a.hierarchy_control_id = c.hierarchy_control_id AND c.object_type_id = ?
        						LEFT JOIN '. $uf->getTable() .' as d ON a.user_id = d.id AND d.company_id = ?
        						LEFT JOIN '. $hcf->getTable() .' as e ON a.hierarchy_control_id = e.id
        						WHERE a.level > b.level
        							AND a.deleted = 0
        							AND b.deleted = 0
        							AND e.deleted = 0
        
        						UNION ALL
        
        						select w.user_id as user_id
        						from '. $huf->getTable() .' as w
        						LEFT JOIN '. $hlf->getTable() .' as x ON w.hierarchy_control_id = x.hierarchy_control_id
        						LEFT JOIN '. $hotf->getTable() .' as y ON w.hierarchy_control_id = y.hierarchy_control_id
        						LEFT JOIN '. $uf->getTable() .' as z ON x.user_id = z.id
        						LEFT JOIN '. $hcf->getTable() .' as z2 ON w.hierarchy_control_id = z2.id
        						WHERE
        							x.user_id = ?
        							AND y.object_type_id = ?
        							AND z.company_id = ?
        							AND z2.deleted = 0
        					';
        */
        //Debug::Text(' Query: '. $query, __FILE__, __LINE__, __METHOD__,10);
        $rs = $this->db->Execute($query, $ph);
        //Debug::Text(' Rows: '. $rs->RecordCount(), __FILE__, __LINE__, __METHOD__,10);
        if ($rs->RecordCount() > 0) {
            foreach ($rs as $row) {
                $retval[] = $row['user_id'];
            }
        }
        $profiler->stopTimer("getPermissionHierarchyChildrenByCompanyIdAndUserId");
        return $retval;
    }
    function getByCompanyIdAndObjectTypeId($id, $object_type_id, $where = NULL, $order = NULL)
    {
        if ($id == '') {
            return FALSE;
        }
        if ($object_type_id == '') {
            return FALSE;
        }
        $strict_order = TRUE;
        if ($order == NULL) {
            //$order = array('b.last_name' => 'asc');
            $strict_order = FALSE;
        }
        $cache_id = $id . $object_type_id;
        $hcf = new HierarchyControlFactory();
        $hotf = new HierarchyObjectTypeFactory();
        $this->rs = $this->getCache($cache_id);
        if ($this->rs === FALSE) {
            $ph = array('id' => $id, 'object_type_id' => $object_type_id);
            $query = '
						select 	*
						from	' . $this->getTable() . ' as a,
								' . $hcf->getTable() . ' as b,
								' . $hotf->getTable() . ' as c

						where	a.hierarchy_control_id = b.id
							AND a.hierarchy_control_id = c.hierarchy_control_id
							AND b.company_id = ?
							AND c.object_type_id = ?
							AND b.deleted = 0
					';
            $query .= $this->getWhereSQL($where);
            $query .= $this->getSortSQL($order, $strict_order);
            $this->rs = $this->db->Execute($query, $ph);
            $this->saveCache($this->rs, $cache_id);
        }
        return $this;
    }
Esempio n. 5
0
    static function getPermissionHierarchySQL($company_id, $user_id, $outer_column)
    {
        $hlf = new HierarchyLevelFactory();
        $huf = new HierarchyUserFactory();
        $hotf = new HierarchyObjectTypeFactory();
        $hcf = new HierarchyControlFactory();
        $query = '
						LEFT JOIN (
							select phc_huf.user_id as user_id, 1 as is_child
							from ' . $huf->getTable() . ' as phc_huf
							LEFT JOIN ' . $hlf->getTable() . ' as phc_hlf ON phc_huf.hierarchy_control_id = phc_hlf.hierarchy_control_id
							LEFT JOIN ' . $hotf->getTable() . ' as phc_hotf ON phc_huf.hierarchy_control_id = phc_hotf.hierarchy_control_id
							LEFT JOIN ' . $hcf->getTable() . ' as phc_hcf ON phc_huf.hierarchy_control_id = phc_hcf.id
							WHERE
								phc_hlf.user_id = ' . (int) $user_id . '
								AND phc_hcf.company_id = ' . (int) $company_id . '
								AND phc_hotf.object_type_id = 100
								AND phc_huf.user_id != phc_hlf.user_id
								AND ( phc_hlf.deleted = 0 AND phc_hcf.deleted = 0 )
						) as phc ON ' . $outer_column . ' = phc.user_id
					';
        return $query;
    }
    function getLevelsByUserIdAndObjectTypeID($user_id, $object_type_id = 50)
    {
        //Requests
        if ($user_id == '') {
            return FALSE;
        }
        if ($object_type_id == '') {
            return FALSE;
        }
        $uf = new UserFactory();
        $hotf = new HierarchyObjectTypeFactory();
        $hcf = new HierarchyControlFactory();
        $ph = array('user_id' => $user_id);
        $query = '
				select 	distinct (x.level) as level
				from	' . $this->getTable() . ' as x,
						' . $hcf->getTable() . ' as y,
					(
								select 	a.hierarchy_control_id,a.level
								from	' . $this->getTable() . ' as a
									LEFT JOIN ' . $hotf->getTable() . ' as b ON a.hierarchy_control_id = b.hierarchy_control_id
								where a.user_id = ?
									AND b.object_type_id in (' . $this->getListSQL($object_type_id, $ph) . ')
									AND a.deleted = 0
					) as z
				where
					x.hierarchy_control_id = y.id
					AND x.hierarchy_control_id = z.hierarchy_control_id
					AND x.level >= z.level
					AND ( x.deleted = 0 AND y.deleted = 0 )
				ORDER BY x.level asc
				';
        $rs = $this->db->Execute($query, $ph);
        //Debug::Text(' Rows: '. $rs->RecordCount(), __FILE__, __LINE__, __METHOD__,10);
        if ($rs->RecordCount() > 0) {
            //The retarr key is the value that will be displayed to the user when switching levels on the authorization page,
            //so we need to start that from 1 and increasing sequentially, regardless of what the actual hierarchy level is.
            $i = 1;
            foreach ($rs as $row) {
                $retarr[$i] = $row['level'];
                $i++;
            }
            return $retarr;
        }
        return FALSE;
    }
    function getLevelsAndHierarchyControlIDsByUserIdAndObjectTypeID($user_id, $object_type_id = 50)
    {
        //Requests
        if ($user_id == '') {
            return FALSE;
        }
        if ($object_type_id == '') {
            return FALSE;
        }
        $uf = new UserFactory();
        $hotf = new HierarchyObjectTypeFactory();
        $hcf = new HierarchyControlFactory();
        $ph = array('user_id' => $user_id);
        //Include object_type_ids for each hierarchy_control_id, because we need to do additional filtering by hierarchy_control_id, level, object_type_ids
        $query = '
				select
						x.hierarchy_control_id as hierarchy_control_id,
						x.level as level,
						z.object_type_id as object_type_id
				from	' . $this->getTable() . ' as x,
						' . $hcf->getTable() . ' as y,
					(
								select 	a.hierarchy_control_id,a.level,b.object_type_id
								from	' . $this->getTable() . ' as a
									LEFT JOIN ' . $hotf->getTable() . ' as b ON a.hierarchy_control_id = b.hierarchy_control_id
								where a.user_id = ?
									AND b.object_type_id in (' . $this->getListSQL($object_type_id, $ph) . ')
									AND a.deleted = 0
					) as z
				where
					x.hierarchy_control_id = y.id
					AND x.hierarchy_control_id = z.hierarchy_control_id
					AND x.level >= z.level
					AND ( x.deleted = 0 AND y.deleted = 0 )
				ORDER BY x.level asc
				';
        $rs = $this->db->Execute($query, $ph);
        //Debug::Text(' Rows: '. $rs->RecordCount(), __FILE__, __LINE__, __METHOD__,10);
        if ($rs->RecordCount() > 0) {
            foreach ($rs as $row) {
                $hierarchy_to_level_map[$row['hierarchy_control_id']][] = (int) $row['level'];
                $hierarchy_to_object_type_map[$row['hierarchy_control_id']][] = (int) $row['object_type_id'];
            }
            //Debug::Arr($hierarchy_to_level_map, ' Hierarchy To Level Map: ', __FILE__, __LINE__, __METHOD__,10);
            //Debug::Arr($hierarchy_to_object_type_map, ' Hierarchy To Object Type Map: ', __FILE__, __LINE__, __METHOD__,10);
            //Take each hierarchy_control and level element and convert it into virtual levels, where the first level (regardless of what it is in the actual hierarchy)
            //is always virtual_level 1, so the supervisor can see all necessary requests that are waiting on them at level 1. Dropping down any other levels
            //is looking and requests waiting on OTHER supervisors.
            //Track the last level for each hierarchy, so we know when to include all requests that may be higher than that level, so if the hierarchy is changed
            //and levels are taken out, requests don't sit in limbo forever.
            foreach ($hierarchy_to_level_map as $hierarchy_control_id => $level_arr) {
                //Unique each level arr so we don't start creating extra virtual levels when multiple superiors are at the same level.
                //This fixes a bug where if there were 5 superiors at the same level, 5 virtual levels would be created.
                $level_arr = array_unique($level_arr);
                $i = 1;
                foreach ($level_arr as $level) {
                    if ($level == end($hierarchy_to_level_map[$hierarchy_control_id])) {
                        $last_level = TRUE;
                    } else {
                        $last_level = FALSE;
                    }
                    $retarr[$i][] = array('hierarchy_control_id' => $hierarchy_control_id, 'level' => $level, 'last_level' => $last_level, 'object_type_id' => array_unique($hierarchy_to_object_type_map[$hierarchy_control_id]));
                    $i++;
                }
            }
            //Debug::Arr($retarr, ' Final Hierarchy To Level Map: ', __FILE__, __LINE__, __METHOD__,10);
            return $retarr;
        }
        return FALSE;
    }
    function getAPISearchByCompanyIdAndArrayCriteria($company_id, $filter_data, $limit = NULL, $page = NULL, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if (!is_array($order)) {
            //Use Filter Data ordering if its set.
            if (isset($filter_data['sort_column']) and $filter_data['sort_order']) {
                $order = array(Misc::trimSortPrefix($filter_data['sort_column']) => $filter_data['sort_order']);
            }
        }
        $additional_order_fields = array('superiors', 'subordinates');
        $sort_column_aliases = array();
        $order = $this->getColumnsFromAliases($order, $sort_column_aliases);
        if ($order == NULL) {
            $order = array('name' => 'asc', 'description' => 'asc');
            $strict = FALSE;
        } else {
            //Always sort by last name,first name after other columns
            if (!isset($order['name'])) {
                $order['name'] = 'asc';
            }
            $strict = TRUE;
        }
        //Debug::Arr($order,'Order Data:', __FILE__, __LINE__, __METHOD__,10);
        //Debug::Arr($filter_data,'Filter Data:', __FILE__, __LINE__, __METHOD__,10);
        $uf = new UserFactory();
        $hlf = new HierarchyLevelFactory();
        $huf = new HierarchyUserFactory();
        $hotf = new HierarchyObjectTypeFactory();
        $ph = array('company_id' => $company_id);
        //Count total users in HierarchyControlFactory factory, so we can disable it when needed. That way it doesn't slow down Hierarchy dropdown boxes.
        //(select count(*) from '. $hlf->getTable().' as hlf WHERE a.id = hlf.hierarchy_control_id AND hlf.deleted = 0 AND a.deleted = 0) as superiors,
        //(select count(*) from '. $huf->getTable().' as hulf WHERE a.id = hulf.hierarchy_control_id AND a.deleted = 0 ) as subordinates,
        $query = '
					select 	distinct a.*,
							y.first_name as created_by_first_name,
							y.middle_name as created_by_middle_name,
							y.last_name as created_by_last_name,
							z.first_name as updated_by_first_name,
							z.middle_name as updated_by_middle_name,
							z.last_name as updated_by_last_name
					from 	' . $this->getTable() . ' as a
						LEFT JOIN ' . $hlf->getTable() . ' as hlf ON ( a.id = hlf.hierarchy_control_id AND hlf.deleted = 0 )
						LEFT JOIN ' . $huf->getTable() . ' as huf ON ( a.id = huf.hierarchy_control_id )
						LEFT JOIN ' . $hotf->getTable() . ' as hotf ON ( a.id = hotf.hierarchy_control_id )
						LEFT JOIN ' . $uf->getTable() . ' as y ON ( a.created_by = y.id AND y.deleted = 0 )
						LEFT JOIN ' . $uf->getTable() . ' as z ON ( a.updated_by = z.id AND z.deleted = 0 )
					where	a.company_id = ?
					';
        if (isset($filter_data['permission_children_ids']) and isset($filter_data['permission_children_ids'][0]) and !in_array(-1, (array) $filter_data['permission_children_ids'])) {
            $query .= ' AND a.created_by in (' . $this->getListSQL($filter_data['permission_children_ids'], $ph) . ') ';
        }
        if (isset($filter_data['id']) and isset($filter_data['id'][0]) and !in_array(-1, (array) $filter_data['id'])) {
            $query .= ' AND a.id in (' . $this->getListSQL($filter_data['id'], $ph) . ') ';
        }
        if (isset($filter_data['exclude_id']) and isset($filter_data['exclude_id'][0]) and !in_array(-1, (array) $filter_data['exclude_id'])) {
            $query .= ' AND a.id not in (' . $this->getListSQL($filter_data['exclude_id'], $ph) . ') ';
        }
        if (isset($filter_data['name']) and trim($filter_data['name']) != '') {
            $ph[] = strtolower(trim($filter_data['name']));
            $query .= ' AND lower(a.name) LIKE ?';
        }
        if (isset($filter_data['description']) and trim($filter_data['description']) != '') {
            $ph[] = strtolower(trim($filter_data['description']));
            $query .= ' AND lower(a.description) LIKE ?';
        }
        if (isset($filter_data['object_type']) and isset($filter_data['object_type'][0]) and !in_array(-1, (array) $filter_data['object_type'])) {
            $query .= ' AND hotf.object_type_id in (' . $this->getListSQL($filter_data['object_type'], $ph) . ') ';
        }
        if (isset($filter_data['superior_user_id']) and isset($filter_data['superior_user_id'][0]) and !in_array(-1, (array) $filter_data['superior_user_id'])) {
            $query .= ' AND hlf.user_id in (' . $this->getListSQL($filter_data['superior_user_id'], $ph) . ') ';
        }
        if (isset($filter_data['user_id']) and isset($filter_data['user_id'][0]) and !in_array(-1, (array) $filter_data['user_id'])) {
            $query .= ' AND huf.user_id in (' . $this->getListSQL($filter_data['user_id'], $ph) . ') ';
        }
        $query .= isset($filter_data['created_by']) ? $this->getWhereClauseSQL(array('a.created_by', 'y.first_name', 'y.last_name'), $filter_data['created_by'], 'user_id_or_name', $ph) : NULL;
        $query .= isset($filter_data['updated_by']) ? $this->getWhereClauseSQL(array('a.updated_by', 'z.first_name', 'z.last_name'), $filter_data['updated_by'], 'user_id_or_name', $ph) : NULL;
        //Don't filter hlf.deleted=0 here as that will not shown hierarchies without any superiors assigned to them. Do the filter on the JOIN instead.
        $query .= '
						AND ( a.deleted = 0 )
					';
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order, $strict, $additional_order_fields);
        $this->ExecuteSQL($query, $ph, $limit, $page);
        return $this;
    }
    function getObjectTypeAppendedListByCompanyIDAndUserID($company_id, $user_id, $where = NULL, $order = NULL)
    {
        if ($company_id == '') {
            return FALSE;
        }
        if ($user_id == '') {
            return FALSE;
        }
        $hotf = new HierarchyObjectTypeFactory();
        $huf = new HierarchyUserFactory();
        $ph = array('company_id' => $company_id, 'user_id' => $user_id);
        $query = '
					select 	a.*,
							b.object_type_id
					from ' . $this->getTable() . ' as a
					LEFT JOIN ' . $hotf->getTable() . ' as b ON a.id = b.hierarchy_control_id
					LEFT JOIN ' . $huf->getTable() . ' as c ON a.id = c.hierarchy_control_id
					where 	a.company_id = ?
							AND c.user_id = ?
							AND a.deleted = 0
				';
        $query .= $this->getWhereSQL($where);
        $query .= $this->getSortSQL($order);
        $this->rs = $this->db->Execute($query, $ph);
        return $this;
    }