getName(); }
public function getSourceName() { return 'default_objects'; }
public function init($force = false) {}
public function isInitialized() { return true; }
public function getName() { return 'File'; }
public function getRootTableName() { return 'CRM_FILES'; }
public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
public function getVirtualFieldListByIdZasob() { return array(); }
public function getRealFieldListByIdZasob() {
$cols = array();// FileStorage::getFileById()
$cols[1] = 'id';
$cols[2] = 'name';
$cols[3] = 'size';
$cols[4] = 'mimeType';
$cols[5] = 'version';
$cols[6] = 'content';
// $cols[] = 'relativePath';
// $cols[] = 'absolutePath';
// $cols[] = 'exists';
return $cols;
}
public function getFields() {// @returns array - $this->_fields
$fields[1] = ['name'=>'id', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>999];
$fields[2] = ['name'=>'name', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>999];
$fields[3] = ['name'=>'size', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>999];
$fields[4] = ['name'=>'mimeType', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>999];
$fields[5] = ['name'=>'version', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>999];
$fields[6] = ['name'=>'content', 'perms'=>'R', 'opis'=>'', 'label'=>'', 'sort_prio'=>999];
return $fields;
}
public function getFieldIdByName($fieldName) {
$fields = $this->getRealFieldListByIdZasob();
if (empty($fieldName)) return null;
foreach ($fields as $idField => $vFieldName) {
if ($vFieldName == $fieldName) return $idField;
}
return null;
}
public function isDecimalField($fieldName) { return false; }
public function isGeomField($fieldName) { return false; }
public function isDateField($fieldName) { return false; }
public function isDateTimeField($fieldName) { return false; }
public function isStringField($fieldName) {
if ('name' == $fieldName) return true;
if ('mimeType' == $fieldName) return true;
return false;
}
public function isTextField($fieldName) { return false; }
public function isBinaryField($fieldName) {
if ('content' == $fieldName) return true;
return false;
}
public function isEnumerationField($fieldName) { return false; }
public function getFieldType($fieldName) {
switch ($fieldName) {
case 'id': return ['name'=>'id', 'type'=>'int']; break;
case 'name': return ['name'=>'name', 'type'=>'string']; break;
}
return null;
}
public function isAllowed($idZasob, $taskPerm, $record = null) {
if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
return false;
}
public function hasFieldPerm($idZasob, $taskPerm) {
if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
return false;
}
// TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
public function canCreateField($fieldName) {
$fields = $this->getRealFieldListByIdZasob();
if (!in_array($fieldName, $fields)) return false;
return true;
}
public function canReadField($fieldName) {
$fields = $this->getRealFieldListByIdZasob();
if (!in_array($fieldName, $fields)) return false;
return true;
}
public function canReadObjectField($fieldName, $record) {
return $this->canReadField($fieldName);
}
public function canWriteField($fieldName) {
$fields = $this->getRealFieldListByIdZasob();
if (!in_array($fieldName, $fields)) return false;
return true;
}
public function canWriteObjectField($fieldName, $record) {
return $this->canWriteField($fieldName);
}
public function getTotal($params = array()) {// TODO: use ParseOgcQuery
$sqlLimit = V::get('limit', 10000, $params);
$sqlOffset = V::get('limitstart', 0, $params);
// TODO: parse params:
// [sortBy] => ID D,test_date A
// [cols] => Array( [0] => ID
// [1] => test_date
// [2] => A_STATUS )
// [ogc:Filter] => "id35"
$sqlWhereAddOgcFilter = '';
$ogcFilter = V::get('ogc:Filter', '', $params);
if (!empty($ogcFilter)) {
Lib::loadClass('ParseOgcFilter');
$parser = new ParseOgcFilter();
$parser->loadOgcFilter($ogcFilter);
$queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
$usedFields = $queryWhereBuilder->getUsedFields();
foreach ($usedFields as $fieldName) {
if (!$this->getFieldIdByName($fieldName)) throw new Exception("Not allowed PropertyName '{$fieldName}'");
}
$sqlWhereAddOgcFilter = $queryWhereBuilder->getQueryWhere('t');
if (!empty($sqlWhereAddOgcFilter)) $sqlWhereAddOgcFilter = " and {$sqlWhereAddOgcFilter}";
DBG::_('DBG_DS', '>1', "ogc:Filter parser", $parser, __CLASS__, __FUNCTION__, __LINE__);
DBG::_('DBG_DS', '>1', "ogc:Filter queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
DBG::_('DBG_DS', '>1', "ogc:Filter usedFields", $usedFields, __CLASS__, __FUNCTION__, __LINE__);
DBG::_('DBG_DS', '>1', "ogc:Filter sqlWhereAddOgcFilter", $sqlWhereAddOgcFilter, __CLASS__, __FUNCTION__, __LINE__);
}
$sqlTblName = FileStorage::getTableName();
$sqlUserLogin = User::getLogin();
$sqlWhere = "1=1";
// $sqlWhere .= " and t.`A_RECORD_CREATE_AUTHOR` = '{$sqlUserLogin}' ";// TODO: Acl to read file
$sqlWhere .= $sqlWhereAddOgcFilter;
if ($pk = V::get('primaryKey', 0, $params, 'int')) $sqlWhere .= " and t.`ID` = {$pk} ";
return DB::getPDO()->fetchValue("
select count(1) as total
from `{$sqlTblName}` t
where {$sqlWhere}
");
}
public function getItem($primaryKey, $params = []) {
$items = $this->getItems(['primaryKey'=>$primaryKey]);
return (!empty($items[$primaryKey])) ? $items[$primaryKey] : null;
}
public function getItems($params = array()) {// TODO: use ParseOgcQuery
$sqlLimit = V::get('limit', 10000, $params);
$sqlOffset = V::get('limitstart', 0, $params);
// TODO: parse params:
// [sortBy] => ID D,test_date A
// [cols] => Array( [0] => ID
// [1] => test_date
// [2] => A_STATUS )
// [ogc:Filter] => "id35"
$sqlWhereAddOgcFilter = '';
$ogcFilter = V::get('ogc:Filter', '', $params);
if (!empty($ogcFilter)) {
Lib::loadClass('ParseOgcFilter');
$parser = new ParseOgcFilter();
$parser->loadOgcFilter($ogcFilter);
$queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
$usedFields = $queryWhereBuilder->getUsedFields();
foreach ($usedFields as $fieldName) {
if (!$this->getFieldIdByName($fieldName)) throw new Exception("Not allowed PropertyName '{$fieldName}'");
}
$sqlWhereAddOgcFilter = $queryWhereBuilder->getQueryWhere('t');
if (!empty($sqlWhereAddOgcFilter)) $sqlWhereAddOgcFilter = " and {$sqlWhereAddOgcFilter}";
DBG::_('DBG_DS', '>1', "ogc:Filter parser", $parser, __CLASS__, __FUNCTION__, __LINE__);
DBG::_('DBG_DS', '>1', "ogc:Filter queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
DBG::_('DBG_DS', '>1', "ogc:Filter usedFields", $usedFields, __CLASS__, __FUNCTION__, __LINE__);
DBG::_('DBG_DS', '>1', "ogc:Filter sqlWhereAddOgcFilter", $sqlWhereAddOgcFilter, __CLASS__, __FUNCTION__, __LINE__);
}
$sqlTblName = FileStorage::getTableName();
$sqlUserLogin = User::getLogin();
$sqlWhere = "1=1";
// $sqlWhere .= " and t.`A_RECORD_CREATE_AUTHOR` = '{$sqlUserLogin}' ";// TODO: Acl to read file
$sqlWhere .= $sqlWhereAddOgcFilter;
if ($pk = V::get('primaryKey', 0, $params, 'int')) $sqlWhere .= " and t.`ID` = {$pk} ";
$rows = array_map(function($row) {
$wfsItem = array();
$wfsItem['id'] = $row['ID'];
$wfsItem['name'] = V::get('FILE_LABEL', $row['ID'], $row);
$wfsItem['size'] = $row['FILE_SIZE'];
$wfsItem['mimeType'] = $row['FILE_MIME_TYPE'];
$wfsItem['version'] = $row['FILE_VERSION'];
{// fetch file content
$objectFile = FileStorage::getFileById($row['ID']);// TODO: avoid sql in FileStorage::convertFromDBRow($row)
$wfsItem['content'] = ($objectFile['exists']) ? base64_encode(file_get_contents($objectFile['absolutePath'])) : null;
}
return $wfsItem;
}, DB::getPDO()->fetchAll("
select t.ID
, t.FILE_HASH
, t.FILE_LABEL
, t.FILE_TYPE
, t.FILE_MIME_TYPE
, t.FILE_MTIME
, t.FILE_SIZE
, t.FILE_VERSION
, t.A_STATUS
, t.A_RECORD_CREATE_DATE
, t.A_RECORD_CREATE_AUTHOR
, t.A_RECORD_UPDATE_DATE
, t.A_RECORD_UPDATE_AUTHOR
, t.A_ADM_COMPANY
, t.A_CLASSIFIED
, INET_NTOA(t.A_USER_IP) as IP
from `{$sqlTblName}` t
where {$sqlWhere}
order by ID DESC
limit {$sqlLimit} offset {$sqlOffset}
"));
$items = array();
foreach ($rows as $row) {
$items[$row['id']] = (object)$row;
}
return $items;
}
public function addItem($itemTodo) {
if (is_object($itemTodo)) {
$itemTodo = (array)$itemTodo;
}
if (!is_array($itemTodo)) throw new HttpException('Item is not array', 400);
if (empty($itemTodo)) {
DBG::_('DBG_DS', '>2', "Item patch is empty", null, __CLASS__, __FUNCTION__, __LINE__);
return 0;// nothing to insert
}
if (empty($itemTodo['content'])) throw new Exception("Empty file content");
$fileName = V::get('name', '', $itemTodo);
$binaryContent = base64_decode($itemTodo['content']);
return FileStorage::addFile($binaryContent, $fileName);
}
public function getGeomFieldType($fieldName) { return null; }
public function getPrimaryKeyField() { return 'id'; }
public function getAttributesFromZasoby() {
$attributes = array();// fldName => [ 'id_zasob' => int, 'label' => str, 'description' => str ]
// if ($acl->hasFieldPerm($idZasob, 'W')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
// if ($acl->hasFieldPerm($idZasob, 'C')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_create", "true");
// if (!$acl->hasFieldPerm($idZasob, 'R')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
return $attributes;
}
public function getXsdFieldType($fieldName) {
switch ($fieldName) {
case 'id': return 'xsd:integer';
case 'name': return 'xsd:string';
case 'size': return 'xsd:integer';
case 'mimeType': return 'xsd:string';
case 'version': return 'xsd:integer';
case 'content': return 'xsd:base64Binary';
default: throw new HttpException("Error field not exists '{$fieldName}'", 404);
}
}
}