db = &$GLOBALS['workflow']['workflowObjects']->getDBWorkflow()->Link_ID; $this->db->SetFetchMode(ADODB_FETCH_ASSOC); } /** * Obtain the subnet of an IP Address based on the numer of bits of the subnet * @param string $ip The IP Address * @param int $bits Number of bits of the subnet * @return string The subnet of the IP * @access private */ private function getSubnet($ip, $bits) { $octets = explode('.', $ip); $output = array(); for ($i = 0; $i < 4; $i++) { if ($bits >= 8) { $output[] = $octets[$i]; $bits -= 8; continue; } $output[] = $octets[$i] & bindec(str_repeat('1', $bits) . str_repeat('0',8 - $bits)); $bits = 0; } return implode('.', $output); } /** * Get External Applications * @access public * @return array list of external applications */ function getExternalApplications() { /* load the intranet subnetworks */ $oldDB = $GLOBALS['phpgw']->db; $GLOBALS['phpgw']->db = $GLOBALS['ajax']->db; $config = new config('workflow'); $configValues = $config->read_repository(); $submasksString = $configValues['intranet_subnetworks']; $GLOBALS['phpgw']->db = $oldDB; $userIP = getenv('REMOTE_ADDR'); if (getenv('HTTP_X_FORWARDED_FOR')) { $tmpIP = explode(',', getenv('HTTP_X_FORWARDED_FOR')); $userIP = $tmpIP[0]; } /* check if the user has access to intranet applications, i.e., is in the intranet */ $showIntranetApplications = false; $submasks = explode(';', $submasksString); foreach ($submasks as $submask) { list($ip,$bits) = explode('/', trim($submask) . '/32', 2); if ($this->getSubnet($ip, $bits) == $this->getSubnet($userIP, $bits)) { $showIntranetApplications = true; break; } } $preOutput = array(); $output = array(); /* select the sites that the user can access */ $externalApplicationsID = $GLOBALS['ajax']->acl->getUserGroupPermissions('APX', $_SESSION['phpgw_info']['workflow']['account_id']); if (!empty($externalApplicationsID)) { $result = $GLOBALS['workflow']['workflowObjects']->getDBGalaxia()->Link_ID->query("SELECT DISTINCT external_application_id, name, address, image, authentication, intranet_only FROM egw_wf_external_application WHERE (external_application_id IN (" . implode(', ', $externalApplicationsID) . ")) ORDER BY name"); $preOutput = $result->GetArray(-1); /* keep only associative elments and check if the user can access an intranet application */ for ($i = 0; $i < count($preOutput); $i++) { if (($preOutput[$i]['intranet_only'] == '1') && (!$showIntranetApplications)) continue; for ($j = 0; $j < $result->_numOfFields; $j++) unset($preOutput[$i][$j]); $output[] = $preOutput[$i]; } } return $output; } /** * Get User Organization ID * @param int $userID User identifier * @return mixed Informações sobre a organização ou false em caso de erro. * @access public */ function getUserOrganization($userID) { $query = "SELECT o.organizacao_id, o.nome, o.descricao, o.url_imagem, o.ativa FROM funcionario f, organizacao o WHERE (o.organizacao_id = f.organizacao_id) AND (f.funcionario_id = ?)"; $result = $this->db->query($query, array((int) $userID)); $output = $result->fetchRow(DB_FETCHMODE_ASSOC); if (!$output) return false; for ($i = 0; $i < $result->_numOfFields; $i++) unset($output[$i]); return $output; } /** * Get cost center list * @param int $organizationID The organization ID * @return array Lista de centros de custo * @access public */ function getCostCenters($organizationID) { $result = $this->db->query("SELECT nm_centro_custo, grupo, descricao FROM centro_custo WHERE (organizacao_id = ?) ORDER BY descricao", array($organizationID)); $output = $result->GetArray(-1); for ($i = 0; $i < count($output); $i++) for ($j = 0; $j < $result->_numOfFields; $j++) unset($output[$i][$j]); return $output; } /** * Get hierarchical Area * @return array * @access public */ function getHierarchicalArea($organizationID, $parent = null, $depth = 0) { if (is_null($parent)) $result = $this->db->query("SELECT area_id, sigla, titular_funcionario_id FROM area WHERE (superior_area_id IS NULL) AND (organizacao_id = ?) AND (ativa = 'S') ORDER BY sigla", array($organizationID)); else $result = $this->db->query("SELECT area_id, sigla, titular_funcionario_id FROM area WHERE (superior_area_id = ?) AND (ativa = 'S') ORDER BY sigla", array($parent)); $output = $result->GetArray(-1); for ($i = 0; $i < count($output); $i++) { for ($j = 0; $j < $result->_numOfFields; $j++) unset($output[$i][$j]); $output[$i]['children'] = $this->getHierarchicalArea($organizationID, $output[$i]['area_id'], $depth + 1); $output[$i]['depth'] = $depth; } return $output; } /** * Get organization area list * @return array * @access public */ function getAreaList($organizationID) { $result = $this->db->query("SELECT area_id, sigla FROM area WHERE (organizacao_id = ?) AND (ativa = 'S') ORDER BY sigla", array($organizationID)); $output = $result->GetArray(-1); for ($i = 0; $i < count($output); $i++) for ($j = 0; $j < $result->_numOfFields; $j++) unset($output[$i][$j]); return $output; } /** * Get organization categories list * @param int $organizationID The organization ID * @return array The categories list * @access public */ function getCategoriesList($organizationID) { $output = $this->db->query('SELECT funcionario_categoria_id, descricao FROM funcionario_categoria WHERE (organizacao_id = ?)', array($organizationID))->GetArray(); $numerOfEmployees = $this->db->GetAssoc('SELECT COALESCE(f.funcionario_categoria_id, 0) AS funcionario_categoria_id, COUNT(*) FROM funcionario f, funcionario_status fs WHERE (f.organizacao_id = ?) AND (f.funcionario_status_id = fs.funcionario_status_id) AND (fs.exibir = \'S\') GROUP BY funcionario_categoria_id', array($organizationID)); $output[] = array( 'funcionario_categoria_id' => 0, 'descricao' => 'Sem Vínculo' ); foreach ($output as &$row) $row['contagem'] .= (isset($numerOfEmployees[$row['funcionario_categoria_id']]) ? $numerOfEmployees[$row['funcionario_categoria_id']] : 0); return $output; } /** * Get Area Employees * @param int $areaID * @param int $organizationID * @return array * @access public */ function getAreaEmployees($areaID, $organizationID) { $organizationID = (int) $organizationID; $areaID = (int) $areaID; /* gather some info from the area */ $areaInfo = $this->db->query('SELECT COALESCE(titular_funcionario_id, -1) AS titular_funcionario_id, COALESCE(substituto_funcionario_id, -1) AS substituto_funcionario_id FROM area WHERE (organizacao_id = ?) AND (area_id = ?)', array($organizationID, $areaID))->GetArray(-1); if (empty($areaInfo)) return false; $areaInfo = $areaInfo[0]; $supervisors = '{' . implode(', ', $areaInfo) . '}'; /* load the employees from the area */ $query = "SELECT f.funcionario_id, f.organizacao_id, f.area_id, COALESCE(f.funcionario_categoria_id, 0) AS funcionario_categoria_id FROM funcionario f, funcionario_status s WHERE ((f.area_id = ?) OR (f.funcionario_id = ANY (?))) AND (f.funcionario_status_id = s.funcionario_status_id) AND (s.exibir = ?)"; $result = $this->db->query($query, array($areaID, $supervisors, 'S')); $employees = $result->GetArray(-1); $cachedLDAP = $GLOBALS['workflow']['factory']->newInstance('CachedLDAP'); $cachedLDAP->setOperationMode($cachedLDAP->OPERATION_MODE_LDAP_DATABASE); $categoriesCount = array(); for ($i = 0; $i < count($employees); $i++) { /* remove numeric fields */ for ($j = 0; $j < $result->_numOfFields; $j++) unset($employees[$i][$j]); $employees[$i]['cn'] = ''; $employees[$i]['telephoneNumber'] = ''; if (in_array($employees[$i]['funcionario_id'], $areaInfo)) $employees[$i]['chief'] = ($employees[$i]['funcionario_id'] == $areaInfo['titular_funcionario_id']) ? 1 : 2; /* try to find the telephone number */ $entry = $cachedLDAP->getEntryByID($employees[$i]['funcionario_id']); if ($entry) { $employees[$i]['telephoneNumber'] = is_null($entry['telephonenumber']) ? '' : $entry['telephonenumber']; $employees[$i]['cn'] = is_null($entry['cn']) ? '' : $entry['cn']; $employees[$i]['removed'] = is_null($entry['last_update']); } /* count the number of employees in each category */ $categoryID = $employees[$i]['funcionario_categoria_id']; if (isset($categoriesCount[$categoryID])) $categoriesCount[$categoryID]++; else $categoriesCount[$categoryID] = 1; } $usedCategories = array_keys($categoriesCount); $availableCategories = $this->getCategoriesList($organizationID); $output = array(); $output['employees'] = $employees; $output['categories'] = array(); foreach ($availableCategories as $category) { if (!in_array($category['funcionario_categoria_id'], $usedCategories)) continue; $category['contagem'] = $categoriesCount[$category['funcionario_categoria_id']]; $output['categories'][] = $category; } usort($output['employees'], create_function('$a,$b', 'return strcasecmp($a[\'cn\'],$b[\'cn\']);')); return $output; } /** * Get employees from a specific category * @param int $categoryID The category ID * @param int $organizationID The organization ID * @return array The list o employees of that category * @access public */ function getCategoryEmployees($categoryID, $organizationID) { $organizationID = (int) $organizationID; /* load the employees from the area */ if ($categoryID == 0) { $query = "SELECT f.funcionario_id, f.organizacao_id, a.area_id, a.sigla AS area FROM funcionario f, funcionario_status s, area a WHERE (f.funcionario_status_id = s.funcionario_status_id) AND (f.area_id = a.area_id) AND (f.funcionario_categoria_id IS NULL) AND (s.exibir = ?) AND (f.organizacao_id = ?)"; $result = $this->db->query($query, array('S', $organizationID)); } else { $query = "SELECT f.funcionario_id, f.organizacao_id, a.area_id, a.sigla AS area FROM funcionario f, funcionario_status s, area a WHERE (f.funcionario_status_id = s.funcionario_status_id) AND (f.area_id = a.area_id) AND (f.funcionario_categoria_id = ?) AND (s.exibir = ?) AND (f.organizacao_id = ?)"; $result = $this->db->query($query, array((int)$categoryID, 'S', $organizationID)); } $employees = $result->GetArray(-1); $cachedLDAP = $GLOBALS['workflow']['factory']->newInstance('CachedLDAP'); $cachedLDAP->setOperationMode($cachedLDAP->OPERATION_MODE_LDAP_DATABASE); for ($i = 0; $i < count($employees); $i++) { /* remove numeric fields */ for ($j = 0; $j < $result->_numOfFields; $j++) unset($employees[$i][$j]); $employees[$i]['cn'] = ''; $employees[$i]['telephoneNumber'] = ''; /* try to find the telephone number */ $entry = $cachedLDAP->getEntryByID($employees[$i]['funcionario_id']); if ($entry) { $employees[$i]['telephoneNumber'] = is_null($entry['telephonenumber']) ? '' : $entry['telephonenumber']; $employees[$i]['cn'] = is_null($entry['cn']) ? '' : $entry['cn'] . (is_null($entry['last_update']) ? ' (excluído)' : ''); } } $output = array('employees' => $employees); return $output; } /** * Search Employee * @param int $searchTerm term to search * @param int $organizationID Id of organization * @return array employee data information * @access public */ function searchEmployee($searchTerm, $organizationID) { /* initialize some variables */ $output = array('employees' => array()); $ldapResult = array(); /* search for the $searchTerm in the LDAP */ $ldap = &$GLOBALS['workflow']['workflowObjects']->getLDAP(); $list = @ldap_search($ldap, $GLOBALS['workflow']['factory']->getInstance('WorkflowLDAP')->getLDAPContext(), ('(&(cn=*' . $searchTerm . '*)(phpgwaccounttype=u))'), array('uidNumber', 'cn', 'telephoneNumber')); if ($list === false) return false; $entries = ldap_get_entries($ldap, $list); $employeesID = array(); for ($i = 0; $i < $entries['count']; $i++) $ldapResult[$entries[$i]['uidnumber'][0]] = array('cn' => $entries[$i]['cn'][0], 'telephoneNumber' => $entries[$i]['telephonenumber'][0]); /* check if any result was found */ if (count($ldapResult) < 1) return $output; /* load employee information */ $query = "SELECT f.funcionario_id AS funcionario_id, f.area_id AS area_id, a.sigla AS area FROM funcionario f, area a, funcionario_status s WHERE (f.area_id = a.area_id) AND (f.organizacao_id = $organizationID) AND (f.funcionario_id IN (" . implode(',', array_keys($ldapResult)) .")) AND (f.funcionario_status_id = s.funcionario_status_id) AND (s.exibir = 'S')"; $result = $this->db->query($query); $employees = $result->GetArray(-1); for ($i = 0; $i < count($employees); $i++) { for ($j = 0; $j < $result->_numOfFields; $j++) unset($employees[$i][$j]); $employees[$i]['cn'] = is_null($ldapResult[$employees[$i]['funcionario_id']]['cn']) ? '' : $ldapResult[$employees[$i]['funcionario_id']]['cn']; $employees[$i]['telephoneNumber'] = is_null($ldapResult[$employees[$i]['funcionario_id']]['telephoneNumber']) ? '' : $ldapResult[$employees[$i]['funcionario_id']]['telephoneNumber']; } $output['employees'] = $employees; return $output; } /** * Busca informações sobre um funcionário. * @param array $params Uma array contendo o ID do funcionário cujas informações serão extraídas e de sua organização (Ajax). * @param int $employeeID O ID do funcionário. * @param int $organizationID O ID da organização. * @return array Informações sobre o funcionário. * @access public */ function getEmployeeInfo($employeeID, $organizationID) { require_once dirname(__FILE__) . '/class.so_orgchart.inc.php'; $SOOrgchart = new so_orgchart(); $SOOrgchart->setExternalCalls(true); $output = $SOOrgchart->getEmployeeInfo($employeeID, $organizationID); $SOOrgchart->setExternalCalls(false); return $output; } /** * Busca informações sobre uma área. * @param array $params Uma array contendo o ID da área cujas informações serão extraídas e de sua organização (Ajax). * @param int $areaID O ID da área. * @param int $organizationID O ID da organização. * @return array Informações sobre a área. * @access public */ function getAreaInfo($areaID, $organizationID) { require_once dirname(__FILE__) . '/class.so_orgchart.inc.php'; $SOOrgchart = new so_orgchart(); $SOOrgchart->setExternalCalls(true); $output = $SOOrgchart->getAreaInfo($areaID, $organizationID); $SOOrgchart->setExternalCalls(false); return $output; } } ?>