getName(); } public function getSourceName() { return 'default_objects'; } public function init($force = false) {} public function isInitialized() { return true; } public function getName() { return 'AccessGroup'; } public function getRootTableName() { return 'CRM_LISTA_ZASOBOW'; } public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); } public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); } public function getVirtualFieldListByIdZasob() { return array(); } public function getXsdTypes() { // @returns [ fieldName => xsdType, ... ] return array_map(function ($field) { return $field['xsdType']; }, $this->getFieldsWithXsdTypes()); } public function getFieldsWithXsdTypes() { $xsdTypes = array(); foreach ($this->getFields() as $idZasob => $field) { $xsdTypes[ $field['name'] ] = $field; $xsdTypes[ $field['name'] ][ 'xsdType' ] = $this->getXsdFieldType($field['name']); } return $xsdTypes; } public function getRealFieldListByIdZasob($force = false) { $cols[100000] = 'id';// CRM_LISTA_ZASOBOW.ID $cols[100001] = 'name';// CRM_LISTA_ZASOBOW.DESC $cols[100002] = 'uid';// Ldap.uid -> value stored in fields: A_ADM_COMPANY, A_CLASSIFIED return $cols; } public function getFields() { $fields = array(); $fields[100000] = ['name'=>'id', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>100]; $fields[100001] = ['name'=>'name', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>101]; $fields[100002] = ['name'=>'uid', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>102]; return $fields; } public function getSqlFieldName($fieldName) { switch ($fieldName) { case 'id': return 'ID'; case 'name': return 'DESC'; case 'uid': return 'ID'; } throw new Exception("Unknown field '{$fieldName}' in AccessGroup (" . $this->getName() . ")"); } public function getFieldType($fieldName) { return null; } // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName public function canCreateField($fieldName) { return false; } public function canReadField($fieldName) { return true; } public function canReadObjectField($fieldName, $record) {return true; } public function canWriteField($fieldName) { return false; } public function canWriteObjectField($fieldName, $record) { return false; } public function getTotal($params = array()) { return count($this->getItems($params)); } public function getItem($primaryKey, $params = []) { $items = $this->getItems(['primaryKey'=>$primaryKey]); return (!empty($items[$primaryKey])) ? $items[$primaryKey] : null; } public function getItems($params = array()) { DBG::log($params, 'array', $this->getName() . "::getItems \$params"); $items = array(); // TODO: fetch groups connectes with current user { $userLdapGroups = UsersHelper::getLDAPGroupByUserName(User::getLogin()); DBG::log($userLdapGroups, 'array', $this->getName() . "::getItems \$userLdapGroups"); if (empty($userLdapGroups)) throw new Exception("User groups not found", 404); foreach ($userLdapGroups as $vLdapGroup) { $allowGroup = false; if ('workgroup' == $vLdapGroup->cn) { $items[0] = ['id'=>'0', 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn]; } else { $cnTest = str_replace('-', '_', $vLdapGroup->cn); $cnTest = explode('_', $cnTest); $idZasob = $cnTest[0]; if (!is_numeric($idZasob)) { DBG::log($vLdapGroup->cn, 'array', $this->getName() . "::getItems skip cn - missing id zasob \$vLdapGroup->cn"); continue; } $items[$idZasob] = ['id'=>$idZasob, 'name'=>$vLdapGroup->name, 'uid'=>$vLdapGroup->cn]; } } } $remotePrimaryKey = null; if (!empty($params['__backRef'])) { $backRef = $params['__backRef']; if (!is_array($backRef)) throw new Exception("Wrong back ref structure - expected array"); if (empty($backRef['namespace'])) throw new Exception("Wrong back ref structure - missing namespace"); if (empty($backRef['primaryKey'])) throw new Exception("Wrong back ref structure - missing primaryKey"); if (empty($backRef['fieldName'])) throw new Exception("Wrong back ref structure - missing fieldName"); $refAcl = ACL::getAclByNamespace($backRef['namespace']); if ($refAcl->getSourceName() !== 'default_db') throw new Exception("Not implemented join with different source"); $sqlRefRootTableName = $refAcl->getRootTableName(); $refTable = ACL::getRefTable($refAcl->getNamespace(), $backRef['fieldName']); $sqlBackRefPk = DB::getPDO()->quote($backRef['primaryKey']); $remotePrimaryKey = DB::getPDO()->fetchValue(" select refTable.REMOTE_PRIMARY_KEY from `{$refTable}` refTable where refTable.PRIMARY_KEY = {$sqlBackRefPk} "); if (!$remotePrimaryKey) return array(); } DBG::log($this->getName() . "::getItems \$remotePrimaryKey({$remotePrimaryKey})"); if ($remotePrimaryKey) { if (!array_key_exists($remotePrimaryKey, $items)) return array(); $items = array($remotePrimaryKey => $items[$remotePrimaryKey]); } if ($pk = V::get('primaryKey', '', $params, 'int')) {// [primaryKey] => 2948 if (!array_key_exists($pk, $items)) return array(); $items = array($pk => $items[$pk]); } if (!empty($params['ogc:Filter'])) { $parser = new ParseOgcFilter(); $parser->loadOgcFilter($params['ogc:Filter']); $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder(); DBG::log($queryWhereBuilder, 'array', $this->getName() . "::getItems \$queryWhereBuilder"); DBG::log($items, 'array', $this->getName() . "::getItems \$items"); $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray')); } $filterId = trim(V::get('f_id', '', $params)); if (strlen($filterId)) {// allow '0' $queryWhereBuilder = new SqlQueryWhereBuilder(); if (is_numeric($filterId)) { $queryWhereBuilder->addComparisonFieldToValue('id', '=', $filterId); } else if (false !== strpos($filterId, '%') && is_numeric(trim($filterId, '%'))) { $queryWhereBuilder->addComparisonFieldToValue('id', 'like', $filterId); } else if ('>=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) { $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThenOrEqualTo', substr($filterId, 2)); } else if ('<=' == substr($filterId, 0, 2) && is_numeric(substr($filterId, 2))) { $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThenOrEqualTo', substr($filterId, 2)); } else if ('>' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) { $queryWhereBuilder->addComparisonFieldToValue('id', 'GreaterThen', substr($filterId, 1)); } else if ('<' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) { $queryWhereBuilder->addComparisonFieldToValue('id', 'LessThen', substr($filterId, 1)); } else if ('=' == substr($filterId, 0, 1) && is_numeric(substr($filterId, 1))) { $queryWhereBuilder->addComparisonFieldToValue('id', '=', substr($filterId, 1)); } else { $filterId = null;// TODO: BUG uniimplemented comparison sign } if ($filterId) $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray')); } foreach (['name', 'uid'] as $fieldName) { $filterValue = trim(V::get("f_{$fieldName}", '', $params)); if (strlen($filterValue)) {// allow '0' $queryWhereBuilder = new SqlQueryWhereBuilder(); if (!is_scalar($filterValue)) { } else if ('=' == substr($filterValue, 0, 1)) { $queryWhereBuilder->addComparisonFieldToValue($fieldName, '=', substr($filterValue, 1)); } else { if ('%' != substr($filterValue, 0, 1)) $filterValue = "%{$filterValue}"; if ('%' != substr($filterValue, -1)) $filterValue = "{$filterValue}%"; $queryWhereBuilder->addComparisonFieldToValue($fieldName, 'like', $filterValue); } $items = array_filter($items, array($queryWhereBuilder, 'filterRawArray')); } } $orderBy = strtolower(V::get('order_by', 'id', $params)); $orderDir = strtolower(V::get('order_dir', 'desc', $params)); if (!in_array($orderBy, ['id', 'name', 'uid'])) throw new HttpException("Bad Request - wrong or missing order by", 400); if (!in_array($orderDir, ['desc', 'asc'])) throw new HttpException("Bad Request - wrong or missing order dir", 400); uasort($items, function ($a, $b) use ($orderBy, $orderDir) { if ('desc' == $orderDir) { return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? -1 : 1; } else if ('asc' == $orderDir) { return (V::geti($orderBy, '', $a) > V::geti($orderBy, '', $b)) ? 1 : -1; } return 0; }); DBG::log($items, 'array', $this->getName() . "::getItems \$items"); return $items; } public function addItem($itemTodo) { throw new Exception("Insert not allowed"); } public function updateItem($itemPatch) { throw new Exception("Update not allowed"); } public function getGeomFieldType($fieldName) { return null; } public function getPrimaryKeyField() { return 'id'; } public function getSqlPrimaryKeyField() { return 'ID'; } public function getAttributesFromZasoby() { return array(); } public function isEnumerationField($fieldName) { return false; } public function getEnumerations($fieldName) { return null; } public function getXsdFieldType($fieldName) { if ('id' == $fieldName) return 'xsd:string'; if ('name' == $fieldName) return 'xsd:string'; if ('uid' == $fieldName) return 'xsd:string'; } public function isGeomField($fldName) { return false; } }