AntAclBase.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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->_rootNamespace = '';
  14. $this->_primaryKey = '';
  15. $this->_fields = [];
  16. }
  17. public function getName() { return $this->_name; }
  18. public function getNamespace() { return $this->_namespace; }
  19. public function getRootNamespace() { return $this->_rootNamespace; }
  20. public function getSourceName() { return 'default_db'; } // TODO: ?
  21. public function getRootTableName() { return $this->_rootTableName; }
  22. public function getPrimaryKeyField() { return $this->_primaryKey; }
  23. public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
  24. public function getVirtualFieldListByIdZasob() { return []; }
  25. public function getVisibleFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); } // TODO: get visible fields
  26. public function getRealFieldListByIdZasob() {
  27. $cols = array();
  28. $fakeZasobId = 1000000;
  29. foreach ($this->getFields() as $field) {
  30. $idZasobField = ($field['idZasob']) ? $field['idZasob'] : $fakeZasobId++;
  31. $cols[$idZasobField] = $field['fieldNamespace'];
  32. }
  33. return $cols;
  34. }
  35. public function getFieldType($fieldName) { return null; }
  36. // try {
  37. // throw new Exception("TODO: AntAclBase::getFieldType({$fieldName})");
  38. // } catch (Exception $e) {
  39. // DBG::log($e);
  40. // }
  41. // $field = $this->_getField($fieldName);
  42. // return $field['xsdType'];
  43. // }
  44. public function getXsdFieldType($fieldName) {
  45. $field = $this->_getField($fieldName);
  46. return $field['xsdType'];
  47. }
  48. public function getXsdMaxOccurs($fieldName) {
  49. $field = $this->_getField($fieldName);
  50. return $field['maxOccurs'];
  51. }
  52. public function getXsdMinOccurs($fieldName) {
  53. $field = $this->_getField($fieldName);
  54. return $field['minOccurs'];
  55. }
  56. public function getAttributesFromZasoby() {
  57. return [];// TODO: ...
  58. }
  59. public function isGeomField($fieldName) {
  60. return ('the_geom' === $fieldName); // TODO: ...
  61. }
  62. public function isEnumerationField($fieldName) {
  63. return false; // TODO: ...
  64. }
  65. public function canWriteField($fieldName) {
  66. return false; // TODO: return $this->getPerms($fieldName)->canWrite()
  67. }
  68. public function canCreateField($fieldName) {
  69. return false; // TODO: return $this->getPerms($fieldName)->canCreate()
  70. }
  71. public function canReadField($fieldName) {
  72. return true; // TODO: return $this->getPerms($fieldName)->canRead()
  73. }
  74. public function canReadObjectField($fieldName, $object) {
  75. return true; // TODO: return $this->getPerms($fieldName, $object)->canRead()
  76. }
  77. public function getFields() {
  78. if (empty($this->_fields)) {
  79. // TODO: fetch fields from DB
  80. // Lib::loadClass('SchemaFactory');
  81. // $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
  82. // $item = $objectStorage->getItem($namespace, [
  83. // 'propertyName' => '*,field'
  84. // ]);
  85. }
  86. return $this->_fields;
  87. }
  88. public function _getField($fieldName) {
  89. foreach ($this->getFields() as $field) {
  90. if ($fieldName === $field['fieldNamespace']) return $field;
  91. }
  92. throw new Exception("Field not found '{$this->_namespace}/{$fieldName}'");
  93. }
  94. public function getSqlPrimaryKeyField() { return 'ID'; } // TODO: read from root object schema (_rootTableName)
  95. public function getTotal($params = []) {
  96. DBG::log($params, 'array', "AntAclBase::getTotal params");
  97. $idInstance = ACL::getInstanceId($this->_namespace);
  98. $instanceTable = ACL::getInstanceTable($this->_namespace);
  99. $sqlPrimaryKey = $this->getSqlPrimaryKeyField();
  100. return DB::getPDO()->fetchValue(" -- getTotal({$this->_namespace})
  101. select count(1)
  102. from `{$this->_rootTableName}` t
  103. join `{$instanceTable}` i on(i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance})
  104. ");
  105. }
  106. public function getItems($params = []) {
  107. DBG::log($params, 'array', "AntAclBase::getItems params");
  108. // $sql->limit = V::get('limit', 10, $params, 'int');
  109. // $sql->offset = V::get('limitstart', 0, $params, 'int');
  110. $limit = V::get('limit', 0, $params, 'int');
  111. $limit = ($limit < 0) ? 0 : $limit;
  112. $offset = V::get('limitstart', 0, $params, 'int');
  113. $offset = ($offset < 0) ? 0 : $offset;
  114. $sqlLimit = ($limit > 0)
  115. ? "limit {$limit} offset {$offset}"
  116. : '';
  117. $idInstance = ACL::getInstanceId($this->_namespace);
  118. $instanceTable = ACL::getInstanceTable($this->_namespace);
  119. $sqlPrimaryKey = $this->getSqlPrimaryKeyField();
  120. {
  121. $filtrIsInstance = [$this->_namespace];
  122. $filtrIsNotInstance = [];
  123. if (!empty($params['f_is_instance'])) $filtrIsInstance = $params['f_is_instance'];
  124. if (!empty($params['f_is_not_instance'])) $filtrIsNotInstance = $params['f_is_not_instance'];
  125. }
  126. return ACL::query($this)
  127. ->isInstance($filtrIsInstance)
  128. ->isNotInstance($filtrIsNotInstance)
  129. ->select('*') // TODO: fields
  130. ->select(!empty($params['@instances']) ? '@instances' : '')
  131. // ->join($instanceTable, 'i', [ 'rawJoin' => "i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance}" ])
  132. ->limit($limit)
  133. ->offset($offset)
  134. ->execute();
  135. return DB::getPDO()->fetchAll(" -- getItems({$this->_namespace})
  136. select t.*
  137. from `{$this->_rootTableName}` t
  138. join `{$instanceTable}` i on(i.pk = t.{$sqlPrimaryKey} and i.idInstance = {$idInstance})
  139. ");
  140. }
  141. public static function buildInstance($idZasob, $conf = []) {
  142. static $_cache;
  143. if (!$_cache) $_cache = array();
  144. if (array_key_exists($idZasob, $_cache)) {
  145. return $_cache[$idZasob];
  146. }
  147. if (empty($conf)) {
  148. throw new Exception("Brak danych konfiguracyjnych do obiektu ant nr {$idZasob}");
  149. // TODO: fetch conf by $idZasob (or find $namespace first)
  150. // Lib::loadClass('SchemaFactory');
  151. // $objectStorage = SchemaFactory::loadDefaultObject('SystemObject');
  152. // $item = $objectStorage->getItem($namespace, [
  153. // 'propertyName' => '*,field'
  154. // ]);
  155. }
  156. DBG::log($conf, 'array', 'AntAclBase::buildInstance $conf');
  157. $acl = new AntAclBase($idZasob);
  158. $acl->_name = $conf['name'];
  159. $acl->_rootTableName = $conf['_rootTableName'];
  160. $acl->_namespace = $conf['namespace'];
  161. $acl->_rootNamespace = str_replace('__x3A__', '/', $conf['nsPrefix']);
  162. $acl->_fields = $conf['field']; // TODO: lazyLoading - use getFields() in all functions
  163. $acl->_primaryKey = 'ID'; // $conf['primaryKey'];
  164. // 'idZasob' => '25872',
  165. // 'idDatabase' => '36',
  166. // 'namespace' => 'default_db/CRM_PROCES/CRM_PROCES',
  167. // '_rootTableName' => 'CRM_PROCES',
  168. // '_type' => 'AntAcl',
  169. // 'isActive' => '1',
  170. // 'isStructInstalled' => '1',
  171. // 'description' => '',
  172. // 'name' => 'CRM_PROCES',
  173. // 'nsPrefix' => 'default_db__x3A__CRM_PROCES',
  174. // 'typeName' => 'default_db__x3A__CRM_PROCES:CRM_PROCES',
  175. // 'reinstallLink' => 'https://biuro.biall-net.pl/dev-pl/se-master/index.php?_route=Storage&_task=objectReinstall&namespace=default_db/CRM_PROCES/CRM_PROCES',
  176. // 'field' => [
  177. // 0 => [
  178. // 'namespace' => 'default_db/CRM_PROCES/CRM_PROCES/ID',
  179. // 'fieldNamespace' => 'ID',
  180. // 'idZasob' => NULL,
  181. // 'idDatabase' => '36',
  182. // '_rootTableName' => 'CRM_PROCES',
  183. // 'objectNamespace' => 'default_db/CRM_PROCES/CRM_PROCES',
  184. // 'xsdType' => 'xsd:int',
  185. // 'xsdRestrictions' => '[]',
  186. // 'appInfo' => '[]',
  187. // 'minOccurs' => '0',
  188. // 'maxOccurs' => '1',
  189. // 'isActive' => '1',
  190. // 'description' => '',
  191. // ),
  192. $_cache[$idZasob] = $acl;
  193. return $_cache[$idZasob];
  194. }
  195. }