AntAclBase.php 10 KB

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