getName(); } public function getSourceName() { return 'default_objects'; } public function init($force = false) {} public function isInitialized() { return true; } public function getName() { return 'AccessOwner'; } public function getRootTableName() { return 'ADMIN_USERS'; }// TODO: turn off - use getName for generating ref's 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 = array(); $cols[100000] = 'id';// ADMIN_USERS.ID $cols[100001] = 'login';// ADMIN_USERS.ADM_ACCOUNT $cols[100002] = 'name';// ADMIN_USERS.ADM_NAME return $cols; } public function getFields() { $fields = array(); $fields[100000] = ['name'=>'id', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>100]; $fields[100001] = ['name'=>'login', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>101]; $fields[100002] = ['name'=>'name', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>102]; return $fields; } public function getSqlFieldName($fieldName) { switch ($fieldName) { case 'id': return 'ID'; case 'login': return 'ADM_ACCOUNT'; case 'name': return 'ADM_NAME'; } throw new Exception("Unknown field '{$fieldName}' in AccessOwner"); } 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 = V::get('DBG_DS', 0, $_GET, 'int'); if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$params:";print_r($params);echo "\n";} DBG::log($params, 'array', "AccessOwner::getItems \$params"); $sqlWhereAndPk = ""; if ($pk = V::get('@primaryKey', null, $params)) {// [@primaryKey] => TODO: int or array of int if (!is_array($pk)) $sqlPk = array($pk); else $sqlPk = $pk; $sqlWhereAndPk = "and t.ID in (" . implode(", ", $sqlPk) . ")"; } $sqlWhereAndBackRef = ""; 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"); // TODO: $this->_query->where([ '__backRef' ]); or $this->_query->join([ '__backRef' ]); $refAcl = ACL::getAclByNamespace($backRef['namespace']); if ($refAcl->getSourceName() !== 'default_db') throw new Exception("Not implemented join with different source"); $refTable = ACL::getRefTable($refAcl->getNamespace(), $backRef['fieldName']); $sqlPk = $this->getAclSqlPrimaryKeyField(); $sqlBackRefPk = DB::getPDO()->quote($backRef['primaryKey']); $sqlWhereAndBackRef = " and t.{$sqlPk} in ( select refTable.REMOTE_PRIMARY_KEY from `{$refTable}` refTable where refTable.PRIMARY_KEY = {$sqlBackRefPk} ) "; } $items = DB::getPDO()->fetchAllByKey(" select t.ID as id, t.ADM_ACCOUNT as login, t.ADM_NAME as name from ADMIN_USERS t where t.A_STATUS = 'NORMAL' and t.ADM_TECH_WORKER != 'NO' {$sqlWhereAndPk} {$sqlWhereAndBackRef} ", $key = 'id'); if ($pk = V::get('primaryKey', 0, $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::_('DBG_DS', '>2', "ogc:Filter \$queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__); if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";} $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 (['login', 'name'] 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', 'login', 'name'])) 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; }); if($DBG>2){echo 'C.'.get_class($this).' L.' . __LINE__ . " getItems \$items:";print_r($items);echo "\n";} 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 getAclSqlPrimaryKeyField() { 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 ('login' == $fieldName) return 'xsd:string'; if ('name' == $fieldName) return 'xsd:string'; } public function isGeomField($fldName) { return false; } }