AntAclBase.php 8.6 KB

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