AntAclBase.php 9.6 KB

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