AntAclBase.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. Lib::loadClass('ACL');
  3. Lib::loadClass('Core_AclBase');
  4. /**
  5. * SE/schema/ant-object/default_db.{rootTableName}/{name}/build.xml
  6. */
  7. class AntAclBase extends Core_AclBase {
  8. public function __construct($zasobID = 0) {
  9. $this->_zasobID = $zasobID;
  10. $this->_name = '';
  11. $this->_namespace = '';
  12. $this->_rootTableName = '';
  13. $this->_db = 0; // database id zasobu
  14. $this->_rootNamespace = '';
  15. $this->_primaryKey = '';
  16. $this->_fields = [];
  17. }
  18. public function getName() { return $this->_name; }
  19. public function getNamespace() { return $this->_namespace; }
  20. public function getRootNamespace() { return $this->_rootNamespace; }
  21. public function getSourceName() { return 'default_db'; } // TODO: ?
  22. public function getRootTableName() { return $this->_rootTableName; }
  23. public function getPrimaryKeyField() { return $this->_primaryKey; }
  24. public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  25. public function getVirtualFieldListByIdZasob() { return []; }
  26. // public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); } // TODO: get visible fields
  27. public function getVisibleFieldListByIdZasob() {
  28. $fields = $this->getRealFieldListByIdZasob();
  29. $pkField = $this->getPrimaryKeyField();
  30. $cols = array();
  31. foreach ($fields as $kFieldID => $fieldName) {
  32. if ($pkField === $fieldName) {
  33. $id = $kFieldID;
  34. break;
  35. }
  36. }
  37. $cols[$id] = 'ID';
  38. foreach ($fields as $kFieldID => $fieldName) {
  39. if ($pkField === $fieldName) continue;
  40. $cols[$kFieldID] = $fieldName;
  41. }
  42. return $cols;
  43. }
  44. public function getRealFieldListByIdZasob() {
  45. $cols = array();
  46. $fakeZasobId = 1000000;
  47. foreach ($this->getFields() as $field) {
  48. $idZasobField = ($field['idZasob']) ? $field['idZasob'] : $fakeZasobId++;
  49. $cols[$idZasobField] = $field['fieldNamespace'];
  50. }
  51. return $cols;
  52. }
  53. public function getFieldType($fieldName) { return null; }
  54. // try {
  55. // throw new Exception("TODO: AntAclBase::getFieldType({$fieldName})");
  56. // } catch (Exception $e) {
  57. // DBG::log($e);
  58. // }
  59. // $field = $this->_getField($fieldName);
  60. // return $field['xsdType'];
  61. // }
  62. public function getXsdFieldType($fieldName) {
  63. $field = $this->_getField($fieldName);
  64. return $field['xsdType'];
  65. }
  66. public function getXsdMaxOccurs($fieldName) {
  67. $field = $this->_getField($fieldName);
  68. return $field['maxOccurs'];
  69. }
  70. public function getXsdMinOccurs($fieldName) {
  71. $field = $this->_getField($fieldName);
  72. return $field['minOccurs'];
  73. }
  74. public function getAttributesFromZasoby() {
  75. return [];// TODO: ...
  76. }
  77. public function getXsdFieldParam($fieldName, $paramKey) { // TODO: fetch from db
  78. return null;
  79. }
  80. // public function getXsdFieldParam($fieldName, $paramKey) { // TableAcl
  81. // return ($this->_schemaClass)
  82. // ? $this->_schemaClass->getFieldParam($fieldName, $paramKey)
  83. // : null
  84. // ;
  85. // }
  86. // public function getXsdFieldParam($fieldName, $paramKey) { // SimpleSchema
  87. // if (empty($this->_simpleSchema['root'][$fieldName])) return null;
  88. // if (empty($this->_simpleSchema['root'][$fieldName]['@@params'])) return null;
  89. // if (empty($this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey])) return null;
  90. // return $this->_simpleSchema['root'][$fieldName]['@@params'][$paramKey];
  91. // }
  92. public function isGeomField($fieldName) {
  93. return ('the_geom' === $fieldName); // TODO: check by xsdType
  94. }
  95. public function isEnumerationField($fieldName) {
  96. return false; // TODO: ...
  97. }
  98. public function canWriteField($fieldName) {
  99. return false; // TODO: return $this->getPerms($fieldName)->canWrite()
  100. }
  101. public function canCreateField($fieldName) {
  102. return false; // TODO: return $this->getPerms($fieldName)->canCreate()
  103. }
  104. public function canReadField($fieldName) {
  105. return true; // TODO: return $this->getPerms($fieldName)->canRead()
  106. }
  107. public function canReadObjectField($fieldName, $object) {
  108. return true; // TODO: return $this->getPerms($fieldName, $object)->canRead()
  109. }
  110. public function canWriteObjectField($fieldName, $record) {
  111. return false; // TODO: return $this->getPerms($fieldName, $object)->canWrite()
  112. }
  113. public function getFields() { // TODO: conflict return structure with TableAcl
  114. if (empty($this->_fields)) {
  115. // TODO: fetch fields from DB
  116. // Lib::loadClass('SchemaFactory');
  117. // $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
  118. // $item = $objectStorage->getItem($namespace, [
  119. // 'propertyName' => '*,field'
  120. // ]);
  121. }
  122. return $this->_fields;
  123. }
  124. public function _getField($fieldName) {
  125. foreach ($this->getFields() as $field) {
  126. if ($fieldName === $field['fieldNamespace']) return $field;
  127. }
  128. throw new Exception("Field not found '{$this->_namespace}/{$fieldName}'");
  129. }
  130. public function getSqlPrimaryKeyField() { return 'ID'; } // TODO: read from root object schema (_rootTableName)
  131. public function getTotal($params = []) {
  132. DBG::log($params, 'array', "AntAclBase::getTotal params");
  133. $idInstance = ACL::getInstanceId($this->_namespace);
  134. $instanceTable = ACL::getInstanceTable($this->_namespace);
  135. $sqlPrimaryKey = $this->getSqlPrimaryKeyField();
  136. return DB::getPDO()->fetchValue(" -- getTotal({$this->_namespace})
  137. select count(1)
  138. from `{$this->_rootTableName}` t
  139. join `{$instanceTable}` i on(i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance})
  140. ");
  141. }
  142. public function getItems($params = []) {
  143. DBG::log($params, 'array', "AntAclBase::getItems params");
  144. // $sql->limit = V::get('limit', 10, $params, 'int');
  145. // $sql->offset = V::get('limitstart', 0, $params, 'int');
  146. $limit = V::get('limit', 0, $params, 'int');
  147. $limit = ($limit < 0) ? 0 : $limit;
  148. $offset = V::get('limitstart', 0, $params, 'int');
  149. $offset = ($offset < 0) ? 0 : $offset;
  150. $sqlLimit = ($limit > 0)
  151. ? "limit {$limit} offset {$offset}"
  152. : '';
  153. $idInstance = ACL::getInstanceId($this->_namespace);
  154. $instanceTable = ACL::getInstanceTable($this->_namespace);
  155. $sqlPrimaryKey = $this->getSqlPrimaryKeyField();
  156. {
  157. $filtrIsInstance = [$this->_namespace];
  158. $filtrIsNotInstance = [];
  159. if (!empty($params['f_is_instance'])) $filtrIsInstance = $params['f_is_instance'];
  160. if (!empty($params['f_is_not_instance'])) $filtrIsNotInstance = $params['f_is_not_instance'];
  161. }
  162. return ACL::query($this)
  163. ->isInstance($filtrIsInstance)
  164. ->isNotInstance($filtrIsNotInstance)
  165. ->select('*') // TODO: fields
  166. ->select(!empty($params['@instances']) ? '@instances' : '')
  167. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance}" ])
  168. ->limit($limit)
  169. ->offset($offset)
  170. ->execute();
  171. }
  172. public static function buildInstance($idZasob, $conf = []) {
  173. static $_cache;
  174. if (!$_cache) $_cache = array();
  175. if (array_key_exists($idZasob, $_cache)) {
  176. return $_cache[$idZasob];
  177. }
  178. if (empty($conf)) {
  179. throw new Exception("Brak danych konfiguracyjnych do obiektu ant nr {$idZasob}");
  180. // TODO: fetch conf by $idZasob (or find $namespace first)
  181. // Lib::loadClass('SchemaFactory');
  182. // $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
  183. // $item = $objectStorage->getItem($namespace, [
  184. // 'propertyName' => '*,field'
  185. // ]);
  186. }
  187. DBG::log($conf, 'array', 'AntAclBase::buildInstance $conf');
  188. $acl = new AntAclBase($idZasob);
  189. $acl->_name = $conf['name'];
  190. $acl->_rootTableName = $conf['_rootTableName'];
  191. $acl->_db = $conf['idDatabase'];
  192. $acl->_namespace = $conf['namespace'];
  193. $acl->_rootNamespace = str_replace('__x3A__', '/', $conf['nsPrefix']);
  194. $acl->_fields = $conf['field']; // TODO: lazyLoading - use getFields() in all functions - TODO: use ACL::getObjectFields
  195. $acl->_primaryKey = 'ID'; // $conf['primaryKey'];
  196. $_cache[$idZasob] = $acl;
  197. return $_cache[$idZasob];
  198. }
  199. public function buildQuery($params = array()) {
  200. Lib::loadClass('AclQueryFeatures');
  201. return new AclQueryFeatures($this, $params, $legacyMode = false);
  202. }
  203. public function getInstanceList() {
  204. $rootTableName = $this->_rootTableName;
  205. return array_map(function ($row) use ($rootTableName) {
  206. return $row['name'];
  207. }, SchemaFactory::loadDefaultObject('SystemObject')->getItems([
  208. 'propertyName' => 'name', // TODO: SystemObject fix propertyName
  209. 'f__rootTableName' => "={$rootTableName}",
  210. 'f__type' => "=AntAcl",
  211. 'f_isObjectActive' => "=1",
  212. ])
  213. );
  214. }
  215. public function getLocalFieldList() {
  216. return array_map(function ($field) {
  217. return $field['fieldNamespace'];
  218. }, array_filter($this->getFields(), function ($field) {
  219. return ($field['isLocal']);
  220. }));
  221. }
  222. public function isLocalField($fieldName) {
  223. return $this->_getField($fieldName)['isLocal'];
  224. }
  225. }