AntAclBase.php 10 KB

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