[ '@namespace' => 'default_objects/SystemObject', '@primaryKey' => 'idZasob', 'idZasob' => [ '@type' => 'xsd:integer' ], 'idDatabase' => [ '@type' => 'xsd:integer' ], 'namespace' => [ '@type' => 'xsd:string' ], '_rootTableName' => [ '@type' => 'xsd:string' ], '_type' => [ '@type' => 'xsd:string' ], 'isActive' => [ '@type' => 'xsd:integer' ], // installed 'description' => [ '@type' => 'xsd:string' ], 'name' => [ '@type' => 'p5:string' ], 'typeName' => [ '@type' => 'p5:string' ], 'reinstallLink' => [ '@type' => 'p5:www_link' ], // 'A_RECORD_CREATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'autor' ], // 'A_RECORD_CREATE_DATE' => [ '@type' => 'xsd:date' , '@label' => 'utworzono' ], // 'A_RECORD_UPDATE_AUTHOR' => [ '@type' => 'xsd:string' , '@label' => 'zaktualizowaƂ' ], // 'A_RECORD_UPDATE_DATE' => [ '@type' => 'xsd:date', '@label' => 'zaktualizowano' ], '@key' => 'namespace' ] ]; // public $_rootTableName = 'CRM_LISTA_ZASOBOW'; public $_rootTableName = 'CRM_#CACHE_ACL_OBJECT_FIELD'; public $_version = '1'; public function _parseWhere($params = []) { $sqlWhere = []; DBG::log($params, 'array', "SystemObject::_parseWhere"); if (!empty($params['#refFrom'])) { // '#refFrom' => [ // 'namespace' => 'default_objects/SystemSource', // 'primaryKey' => $sourceItem['idZasob'] // ] if (empty($params['#refFrom']['namespace'])) throw new Exception("Missing refFrom/namespace"); if (empty($params['#refFrom']['primaryKey'])) throw new Exception("Missing refFrom/primaryKey"); if ('default_objects/SystemSource' != $params['#refFrom']['namespace']) throw new Exception("Unsupported refFrom/namespace '{$params['#refFrom']['namespace']}'"); $sqlWhere[] = "t.idDatabase = " . DB::getPDO()->quote($params['#refFrom']['primaryKey'], PDO::PARAM_INT); } { $filterParams = []; $xsdFields = $this->getXsdTypes(); foreach ($params as $k => $v) { if ('f_' != substr($k, 0, 2)) continue; $fieldName = substr($k, 2); if (!array_key_exists($fieldName, $xsdFields)) { // TODO: check query by xpath or use different param prefix throw new Exception("Field '{$fieldName}' not found in '{$this->_namespace}'"); } if ('p5:' == substr($xsdFields[$fieldName], 0, 3)) { continue; } $filterParams[$fieldName] = $v; } } if (!empty($filterParams)) { DBG::log($filterParams, 'array', "SystemObject::_parseWhere TODO \$filterParams"); foreach ($filterParams as $fieldName => $value) { if (is_array($value)) { DBG::log($value, 'array', "TODO SystemObject::_parseWhere array value for \$filterParams[{$fieldName}]"); } else if (is_scalar($value)) { if ('=' == substr($value, 0, 1)) { $sqlWhere[] = "t.{$fieldName} = " . DB::getPDO()->quote(substr($value, 1), PDO::PARAM_STR); } else { $sqlWhere[] = "t.{$fieldName} like " . DB::getPDO()->quote("%{$value}%", PDO::PARAM_STR); } } else { DBG::log($value, 'array', "BUG SystemObject::_parseWhere unknown type for \$filterParams[{$fieldName}]"); } } } return (!empty($sqlWhere)) ? "where " . implode(" and ", $sqlWhere) : ''; } public function getTotal($params = []) { $sqlWhere = $this->_parseWhere($params); return DB::getPDO()->fetchValue(" select count(1) as cnt from `{$this->_rootTableName}` t {$sqlWhere} "); } public function getItems($params = []) { $sqlWhere = $this->_parseWhere($params); $currSortCol = V::get('order_by', 'idZasob', $params); $currSortFlip = strtolower(V::get('order_dir', 'desc', $params)); // TODO: validate $currSortCol is in field list // TODO: validate $currSortFlip ('asc' or 'desc') $xsdFields = $this->getXsdTypes(); if (!array_key_exists($currSortCol, $xsdFields)) throw new Exception("Field '{$currSortCol}' not found in '{$this->_namespace}'"); if (!in_array($currSortFlip, ['asc', 'desc'])) throw new Exception("Sort dir not allowed"); $sqlOrderBy = "order by t.`{$currSortCol}` {$currSortFlip}"; $limit = V::get('limit', 0, $params, 'int'); $limit = ($limit < 0) ? 0 : $limit; $offset = V::get('limitstart', 0, $params, 'int'); $offset = ($offset < 0) ? 0 : $offset; $sqlLimit = ($limit > 0) ? "limit {$limit} offset {$offset}" : ''; return array_map(array($this, 'buildFeatureFromSqlRow'), DB::getPDO()->fetchAll(" select t.* from `{$this->_rootTableName}` t {$sqlWhere} {$sqlOrderBy} {$sqlLimit} ")); } public function buildFeatureFromSqlRow($item) { $exNs = explode('/', $item['namespace']); $item['name'] = array_pop($exNs); $item['typeName'] = implode('__x3A__', $exNs) . ':' . $item['name']; $item['reinstallLink'] = Router::getRoute('Storage')->getLink('objectReinstall', [ 'namespace' => $item['namespace'] ]); return $item; } }