TableAcl.php 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  1. <?php
  2. Lib::loadClass('Core_AclBase');
  3. Lib::loadClass('SchemaException');
  4. Lib::loadClass('Typespecial');
  5. Lib::loadClass('ProcesHelper');
  6. Lib::loadClass('Schema_TableFactory');
  7. Lib::loadClass('DataSourceFactory');
  8. Lib::loadClass('SchemaFactory');
  9. /**
  10. * $_SESSION['TableAcl_cache'][$tableID] = array(
  11. * [db] => DB zasob ID
  12. * [name] => Table name
  13. * [opis] => Table opis
  14. * [fields] => array(
  15. * [$fieldID] => array(
  16. * [name] => name
  17. * [perms] => perms (FORM_TREAT)
  18. * [opis] => opis
  19. * )
  20. * )
  21. * [types] => array(
  22. * [$fieldID] => array(
  23. * [type] => type
  24. * [null] => bool
  25. * [default] => default value
  26. * )
  27. * )
  28. * );
  29. */
  30. class TableAcl extends Core_AclBase {
  31. public $_rootTableName = null;
  32. public $_zasobID = '';
  33. public $_db = '';
  34. public $_name = '';
  35. public $_label = '';
  36. public $_opis = '';
  37. public $_executed_fieldsInit = null;
  38. public $_fields = array();
  39. public $_types = array();
  40. public $_virtualFieldsIdList = array();
  41. public $_schemaLoaded = false;
  42. public $_primaryKeyField = null;
  43. public $_sourceNamespace = null;
  44. public function __construct($zasobID) { $this->_zasobID = $zasobID; }
  45. public function getNamespace() { return $this->_sourceNamespace ? $this->_sourceNamespace . "/" . $this->getName() : 'default_db/' . $this->getName(); }
  46. public function getRootNamespace() { return $this->getNamespace(); }
  47. public function getSourceName() { return $this->_sourceNamespace ? $this->_sourceNamespace : 'default_db'; }
  48. public function getName() { return $this->_name; }
  49. public function getRootTableName() {
  50. if (empty($this->_name)) return null;// throw new Exception("Table name not defined");
  51. if ($this->_rootTableName) return $this->_rootTableName;
  52. if (!$this->_db) return null;// throw new Exception("Database not defined in table {$this->_zasobID}");
  53. if (DB::getPDO($this->_db)->getZasobId() === DB::getPDO()->getZasobId()) {
  54. $dbName = DB::getPDO($this->_db)->getDatabaseName();
  55. $this->_rootTableName = DB::getPDO()->fetchValue("
  56. select t.TABLE_NAME
  57. from `information_schema`.`TABLES` t
  58. where t.TABLE_SCHEMA = '{$dbName}'
  59. and t.TABLE_NAME LIKE '{$this->_name}'
  60. ");
  61. if (!$this->_rootTableName) return null;// throw new Exception("Table '{$this->_name}' not exists!");
  62. } else {
  63. $this->_rootTableName = $this->_name; // TODO: fix for Postgres
  64. }
  65. DBG::log("TableAcl({$this->_zasobID})->getRootTableName (\$this->_name='{$this->_name}', \$this->_rootTableName='{$this->_rootTableName}')");
  66. if ($this->_rootTableName != $this->_name) {
  67. if ($this->_zasobID) {
  68. $affected = DB::getPDO()->update('CRM_LISTA_ZASOBOW', 'ID', $this->_zasobID, [
  69. 'DESC' => $this->_rootTableName
  70. ]);
  71. DBG::log("TableAcl({$this->_zasobID})->getRootTableName fixed zasob table name (\$affected={$affected})");
  72. }
  73. $this->_name = $this->_rootTableName;
  74. $this->save();
  75. }
  76. return $this->_rootTableName;
  77. }
  78. public function getID() { return $this->_zasobID; }
  79. public function getSqlPrimaryKeyField() {
  80. return $this->getPrimaryKeyField();
  81. } // TODO: read from root object schema (_rootTableName)
  82. public function setName($name) {// TODO: used only by setNameByTableId
  83. DBG::log("setName('{$name}')");
  84. $this->_name = $name;
  85. }
  86. public function setOpis($opis) { $this->_opis = $opis; }
  87. public function getOpis() { return $this->_opis; }
  88. public function setLabel($label) { $this->_label = $label; }
  89. public function getLabel() { return $this->_label; }
  90. public function setNameByTableId($tableID) {
  91. //used for init without knowing table name
  92. $db = DB::getDB();
  93. $sql = "select `DESC` from CRM_LISTA_ZASOBOW where ID={$tableID} and `TYPE`='TABELA'";
  94. $res = $db->query($sql);
  95. $res_ = $db->fetch($res);
  96. //DEBUG_S(-3,'setNameByTableId',$res_,__FILE__,__FUNCTION__,__LINE__);
  97. self::setName($res->DESC);
  98. }
  99. public function getRawOpis() {
  100. $opis = $this->_opis;
  101. if (!$opis) return $this->getRawLabel(100);
  102. $opis = strip_tags($opis);
  103. return $opis;
  104. }
  105. public function getRawLabel($posLimit = 20) { // TODO: moved to P5Menu
  106. $label = $this->_label;
  107. if (empty($label) && !empty($this->_opis)) {
  108. $label = $this->_opis;
  109. if (mb_strlen($this->_opis) > $posLimit) {
  110. $pos = strpos($this->_opis, ' - ');
  111. if ($pos > $posLimit || $pos < 5) {
  112. $pos = $posLimit;
  113. $label = mb_substr($this->_opis, 0, $posLimit, 'utf-8') . '...';
  114. } else {
  115. $label = mb_substr($this->_opis, 0, $pos, 'utf-8');
  116. }
  117. }
  118. }
  119. if (empty($label)) {
  120. $label = $this->_name;
  121. }
  122. return $label;
  123. }
  124. public function getShortLabel($posLimit = 20) {
  125. $shortLabel = $this->getRawLabel($posLimit);
  126. $opis = $this->_opis;
  127. $shortLabel = '<span title="' . htmlspecialchars($opis) . '">' . $shortLabel . '</span>';
  128. return $shortLabel;
  129. }
  130. public function getLongLabel($posLimit = 30) { // TODO: RMME
  131. $longLabel = $this->getRawLabel($posLimit);
  132. $opis = $this->_opis;
  133. if ($longLabel != $this->_name) {
  134. $longLabel .= ' <em>' . $this->_name . '</em>';
  135. }
  136. $longLabel = '<span title="' . htmlspecialchars($opis) . '">' . $longLabel . '</span>';
  137. return $longLabel;
  138. }
  139. public function getLongRawLabel($posLimit = 30) {
  140. $longLabel = $this->getRawLabel($posLimit);
  141. $opis = $this->_opis;
  142. if ($longLabel != $this->_name) {
  143. $longLabel .= ' ' . $this->_name;
  144. }
  145. return $longLabel;
  146. }
  147. public function setDB($db) {
  148. $this->_db = $db;
  149. }
  150. public function getDB() {
  151. return $this->_db;
  152. }
  153. public function addField($fieldID, $name, $opis, $sort_prio, $label = '') {
  154. $field = array();
  155. $field['name'] = $name;
  156. $field['perms'] = '';
  157. $field['opis'] = $opis;
  158. $field['sort_prio'] = $sort_prio;
  159. $field['label'] = $label;
  160. $this->_fields[$fieldID] = $field;
  161. }
  162. public function getTableDbId($tableID) {
  163. return $this->_db;
  164. }
  165. public function getField($fieldID) {
  166. return $this->getFields()[$fieldID];
  167. }
  168. public function hasFieldById($fieldID) {
  169. return array_key_exists($fieldID, $this->getFields());
  170. }
  171. public function getFields() {
  172. $this->fieldsInit();
  173. $types = $this->_types;
  174. return array_map(function ($field) use ($types) {
  175. return array_merge($field, [
  176. 'fieldNamespace' => $field['name'],
  177. 'isLocal' => array_key_exists($field['name'], $types),
  178. ]);
  179. }, $this->_fields);
  180. }
  181. public function setFieldPerms($fieldID, $perms) {
  182. if (array_key_exists($fieldID, $this->getFields())) {
  183. $this->_fields[$fieldID]['perms'] .= $perms;
  184. }
  185. }
  186. public function getFieldPerms($fieldID) {
  187. if (array_key_exists($fieldID, $this->getFields())) {
  188. $perms = V::get('perms', '', $this->_fields[$fieldID]);
  189. if ($perms) {
  190. return implode(',', array_unique(str_split($perms)));
  191. }
  192. }
  193. return '';
  194. }
  195. public function hasFieldPerm($fieldID, $perm) {// TODO: legacy
  196. if (array_key_exists($fieldID, $this->getFields())) {
  197. if (false !== strpos($this->_fields[$fieldID]['perms'], $perm)) {
  198. return true;
  199. }
  200. return false;
  201. }
  202. return false;
  203. }
  204. public function getFieldIdByName($fieldName) {
  205. $fieldID = 0;
  206. if (empty($fieldName)) {
  207. return;
  208. }
  209. foreach ($this->getFields() as $kID => $vField) {
  210. if ($vField['name'] == $fieldName) {
  211. $fieldID = $kID;
  212. }
  213. }
  214. return $fieldID;
  215. }
  216. public function hasEditPerms() {
  217. foreach ($this->getFields() as $kFldID => $vFld) {
  218. if ($this->hasFieldPerm($kFldID, 'W')) return true;
  219. if ($this->hasFieldPerm($kFldID, 'C')) return true;
  220. if ($this->hasFieldPerm($kFldID, 'S')) return true;
  221. }
  222. return false;
  223. }
  224. public function hasSuperAccessPerms() {
  225. foreach ($this->getFields() as $kFldID => $vFld) {
  226. if ($this->hasFieldPerm($kFldID, 'S')) {
  227. return true;
  228. }
  229. else if ($this->hasFieldPerm($kFldID, 'V')) {
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. public function hasPermSuperWrite() {
  236. foreach ($this->getFields() as $kFldID => $vFld) {
  237. if ($this->hasFieldPerm($kFldID, 'S')) {
  238. return true;
  239. }
  240. }
  241. return false;
  242. }
  243. /**
  244. * @param $taskPerm - 'C', 'W', 'R'
  245. */
  246. public function isAllowed($fieldID, $taskPerm, $record = null) {// TODO: legacy - replace with canWriteField, canReadField, canWriteObjectField, canReadObjectField, canCreateField
  247. if (!in_array($taskPerm, array('C', 'W', 'R'))) {
  248. return false;
  249. }
  250. $adminFields = array();
  251. $adminFields[] = 'ID';
  252. $adminFields[] = 'A_RECORD_CREATE_DATE';
  253. $adminFields[] = 'A_RECORD_CREATE_AUTHOR';
  254. $adminFields[] = 'A_RECORD_UPDATE_DATE';
  255. $adminFields[] = 'A_RECORD_UPDATE_AUTHOR';
  256. $fieldName = $this->getFields()[$fieldID]['name'];
  257. if ($taskPerm == 'R' && in_array($fieldName, $adminFields)) {
  258. return true;
  259. }
  260. // check perm: allow 'RS', 'WS' - can R/W field even if cant read record
  261. // check 'O' - can read field even if cant read field but can read record
  262. if(V::get('DBG_ACL', '', $_REQUEST) > 1){ echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;"> (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r(array('Field'=>$fieldID.'('.$fieldName.')'
  263. ,'taskPerm'=>$taskPerm
  264. ,'fieldPerms'=>V::get('perms', null, V::get($fieldID, null, $this->_fields))
  265. ,'canReadRecord'=>'"'.$this->canReadRecord($record).'"'
  266. ,'hasFieldPerm(O) || canWriteRecord'=>'"'.$this->hasFieldPerm($fieldID, 'O').'" || "'.$this->canReadRecord($record).'"'
  267. ,'hasFieldPerm(S)'=>'"'.$this->hasFieldPerm($fieldID, 'S').'"'
  268. ,'hasFieldPerm(V)'=>'"'.$this->hasFieldPerm($fieldID, 'V').'"'
  269. ));echo'</pre>'; }
  270. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  271. if ($taskPerm == 'R' && $this->hasFieldPerm($fieldID, 'V')) {
  272. return true;
  273. } else if ($taskPerm == 'R'
  274. && $record
  275. && $this->hasFieldPerm($fieldID, 'O')
  276. && ($this->canReadRecord($record) || $this->canWriteRecord($record))
  277. ) {
  278. return true;// 'WO' or 'CO'
  279. }
  280. return false;
  281. }
  282. // check 'R' - require can read record, or V - Super View
  283. if ($taskPerm == 'R') {
  284. if ($this->canReadRecord($record) || $this->hasFieldPerm($fieldID, 'V')) {
  285. return true;
  286. } else {
  287. return false;
  288. }
  289. }
  290. // 'C' and 'W' require colType
  291. $colType = $this->getFieldTypeById($fieldID);
  292. if (!$colType) {
  293. return false;
  294. }
  295. if ($taskPerm == 'W') {
  296. if ($record) {
  297. if(V::get('DBG_ACL', '', $_REQUEST) > 1){echo '(Field: '.$fieldID.', canWriteRecord: ' . $this->canWriteRecord($record) . ' || (hasFieldPerm(S): ' . $this->hasFieldPerm($fieldID, 'S') . ' && hasFieldPerm(W): ' . $this->hasFieldPerm($fieldID, 'W') . '))';}
  298. return ($this->canWriteRecord($record)|| $this->hasFieldPerm($fieldID, 'S'));
  299. }
  300. }
  301. return true;
  302. }
  303. public function canCreateField($fieldName) {
  304. $idZasob = $this->getFieldIdByName($fieldName);
  305. return ($idZasob && $this->hasFieldPerm($idZasob, $taskPerm = 'C'));
  306. }
  307. public function canReadField($fieldName) {
  308. $idZasob = $this->getFieldIdByName($fieldName);
  309. // DBG::log($this->_fields[$idZasob], 'array', "canReadField({$fieldName})");
  310. if (!$idZasob) return false;
  311. if ('ID' == $fieldName) return true;
  312. if ('A_RECORD_CREATE_DATE' == $fieldName) return true;
  313. if ('A_RECORD_CREATE_AUTHOR' == $fieldName) return true;
  314. if ('A_RECORD_UPDATE_DATE' == $fieldName) return true;
  315. if ('A_RECORD_UPDATE_AUTHOR' == $fieldName) return true;
  316. if ($this->hasFieldPerm($idZasob, $taskPerm = 'R')) return true;
  317. if ($this->hasFieldPerm($idZasob, $taskPerm = 'V')) return true;
  318. if ($this->hasFieldPerm($idZasob, $taskPerm = 'O')) return true; // TODO: has 'O' ? @see canReadObjectField
  319. return false;
  320. }
  321. public function canReadObjectField($fieldName, $record) {
  322. // if (!$this->canReadField($fieldName)) return false;
  323. if ('ID' == $fieldName) return true;
  324. if ('A_RECORD_CREATE_DATE' == $fieldName) return true;
  325. if ('A_RECORD_CREATE_AUTHOR' == $fieldName) return true;
  326. if ('A_RECORD_UPDATE_DATE' == $fieldName) return true;
  327. if ('A_RECORD_UPDATE_AUTHOR' == $fieldName) return true;
  328. if (!$record) {
  329. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">canReadObjectField('.$fieldName.', $record) - record not set - return false (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): '.var_export($record, true).'</pre>';}
  330. return false;
  331. }
  332. $idZasob = $this->getFieldIdByName($fieldName);
  333. if (!$idZasob) {
  334. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">canReadObjectField('.$fieldName.', $record) - $idZasob not set - return false (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): '.var_export($record, true).'</pre>';}
  335. return false;
  336. }
  337. if (!$this->hasFieldPerm($idZasob, $taskPerm = 'R')) {
  338. if ($this->hasFieldPerm($idZasob, 'V')) return true;
  339. return ($this->hasFieldPerm($idZasob, 'O') && ($this->canReadRecord($record) || $this->canWriteRecord($record)));// 'WO' or 'CO'
  340. }
  341. return ($this->canReadRecord($record) || $this->hasFieldPerm($idZasob, 'V'));// 'R' require
  342. }
  343. public function canWriteField($fieldName) {
  344. $idZasob = $this->getFieldIdByName($fieldName);
  345. if (!$idZasob) return false;
  346. return $this->hasFieldPerm($idZasob, $taskPerm = 'W');
  347. }
  348. public function canWriteObjectField($fieldName, $record) {
  349. if (!$this->canWriteField($fieldName)) return false;
  350. $idZasob = $this->getFieldIdByName($fieldName);
  351. if (!$idZasob) return false;
  352. if(V::get('DBG_ACL', '', $_REQUEST) > 1){echo '(Field: "'.$fieldName.'" ['.$idZasob.'], canWriteRecord: ' . $this->canWriteRecord($record) . ' || (hasFieldPerm(S): ' . $this->hasFieldPerm($idZasob, 'S') . ' && hasFieldPerm(W): ' . $this->hasFieldPerm($idZasob, 'W') . '))';}
  353. return ($this->canWriteRecord($record) || $this->hasFieldPerm($idZasob, 'S'));
  354. }
  355. /**
  356. * @param $taskPerm - 'C', 'W'
  357. */
  358. public function getFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  359. $out = '';
  360. if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
  361. if ($taskPerm == 'R') throw new Exception("Brak uprawnień do odczytu");
  362. else if ($taskPerm == 'W') throw new Exception("Brak uprawnień do zapisu");
  363. else throw new Exception("Brak uprawnień do tego pola ({$taskPerm})");
  364. }
  365. $colName = $this->getFields()[$fieldID]['name'];
  366. if ($colName == 'ID') return;
  367. $colType = $this->getFieldTypeById($fieldID);
  368. if (!$colType) throw new Exception("Error - unknown type");
  369. $item = array();
  370. $item['htmlType'] = '';
  371. $item['value'] = $fValue;
  372. $item['canRead'] = $this->isAllowed($fieldID, 'R', $record);
  373. $item['canWrite'] = $this->isAllowed($fieldID, 'W', $record);
  374. $item['rawColType'] = $colType;
  375. return $item;
  376. $html = new stdClass();
  377. $html->_params = array();
  378. $html->tag = 'input';
  379. $html->cnt = '';
  380. $html->attrs = array();
  381. $html->attrs['id'] = $fName;
  382. $html->attrs['name'] = $fName;
  383. $html->attrs['type'] = 'text';
  384. $html->attrs['value'] = htmlspecialchars($fValue);
  385. if (isset($params['tabindex'])) {
  386. $html->attrs['tabindex'] = $params['tabindex'];
  387. }
  388. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  389. $html->attrs['disabled'] = 'disabled';
  390. }
  391. $maxGrid = V::get('maxGrid', 10, $params);
  392. if (substr($colType['type'], 0, 3) == 'int'
  393. || substr($colType['type'], 0, 7) == 'tinyint'
  394. || substr($colType['type'], 0, 8) == 'smallint'
  395. || substr($colType['type'], 0, 6) == 'bigint'
  396. ) {
  397. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  398. $html->attrs['type'] = 'number';
  399. $html->attrs['class'][] = 'input-small';
  400. }
  401. else if (substr($colType['type'], 0, 6) == 'double') {
  402. $html->attrs['type'] = 'text';
  403. $html->attrs['class'][] = 'input-small';
  404. }
  405. else if (substr($colType['type'], 0, 7) == 'decimal') {
  406. $html->attrs['type'] = 'text';
  407. $html->attrs['class'][] = 'input-small';
  408. }
  409. else if (substr($colType['type'], 0, 7) == 'varchar'
  410. || substr($colType['type'], 0, 4) == 'char'
  411. ) {
  412. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  413. $html->attrs['type'] = 'text';
  414. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  415. if ($maxLength > 0) {
  416. $html->attrs['maxlength'] = $maxLength;
  417. }
  418. $valLength = strlen($fValue);
  419. if (isset($params['widthClass'])) {
  420. if ($params['widthClass'] == 'inside-modal') {
  421. $html->attrs['style'] = 'width:98%;';
  422. } else {
  423. $html->attrs['style'] = 'width:98%;';
  424. }
  425. } else {
  426. /*
  427. if ($maxLength < 11) {
  428. $html->attrs['class'][] = 'span2';
  429. } else if ($maxLength < 31) {
  430. $html->attrs['class'][] = 'span5';
  431. } else if ($maxLength < 51) {
  432. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  433. } else if ($maxLength < 101) {
  434. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  435. } else {
  436. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  437. }
  438. */
  439. }
  440. if ($maxLength > 255) {// Fix for long varchar - use textarea
  441. $html->tag = 'textarea';
  442. $html->cnt = htmlspecialchars($fValue);
  443. $html->attrs['rows'] = '3';
  444. unset($html->attrs['type']);
  445. unset($html->attrs['value']);
  446. }
  447. }
  448. else if (substr($colType['type'], 0, 4) == 'date') {
  449. $testDatePicker = true;
  450. if ($testDatePicker) {
  451. $html->attrs['type'] = 'text';
  452. $html->_params[] = 'date';
  453. if (substr($colType['type'], 0, 8) == 'datetime') {
  454. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  455. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  456. $html->attrs['maxlength'] = 19;
  457. } else {
  458. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  459. $html->attrs['maxlength'] = 10;
  460. }
  461. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  462. $html->attrs['value'] = '';
  463. }
  464. } else {
  465. $html->attrs['type'] = 'date';
  466. }
  467. }
  468. else if ($colType['type'] == 'time') {
  469. $testDatePicker = true;
  470. if ($testDatePicker) {
  471. $html->attrs['type'] = 'text';
  472. $html->_params[] = 'time';
  473. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  474. $html->attrs['data-format'] = 'hh:mm:ss';
  475. $html->attrs['maxlength'] = 8;
  476. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  477. $html->attrs['value'] = '';
  478. }
  479. } else {
  480. $html->attrs['type'] = 'time';
  481. }
  482. }
  483. else if ($colType['type'] == 'timestamp') {
  484. $testDatePicker = true;
  485. if ($testDatePicker) {
  486. $html->attrs['type'] = 'text';
  487. $html->_params[] = 'date';
  488. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  489. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  490. $html->attrs['maxlength'] = 19;
  491. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  492. $html->attrs['value'] = '';
  493. }
  494. } else {
  495. $html->attrs['type'] = 'date';
  496. }
  497. }
  498. else if (substr($colType['type'], 0, 4) == 'enum') {
  499. unset($html->attrs['type']);
  500. unset($html->attrs['value']);
  501. $html->tag = 'select';
  502. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  503. $selValue = $fValue;
  504. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  505. if ($taskPerm == 'C') {
  506. $selValue = $colType['default'];
  507. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  508. $selValue = $colType['default'];
  509. }
  510. }
  511. $html->cnt .= '<option value="">' . "" . '</option>';
  512. if (!empty($selValue) && !in_array($selValue, $values)) {
  513. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  514. }
  515. foreach ($values as $val) {
  516. $sel = ($selValue == $val)? ' selected="selected"' : '';
  517. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  518. }
  519. }
  520. else if (substr($colType['type'], 0, 4) == 'text'
  521. || substr($colType['type'], 0, 8) == 'tinytext'
  522. || substr($colType['type'], 0, 10) == 'mediumtext'
  523. || substr($colType['type'], 0, 8) == 'longtext'
  524. ) {
  525. $html->tag = 'textarea';
  526. $html->cnt = htmlspecialchars($fValue);
  527. if (isset($params['widthClass'])) {
  528. if ($params['widthClass'] == 'inside-modal') {
  529. $html->attrs['style'] = 'width:98%;';
  530. } else {
  531. $html->attrs['style'] = 'width:98%;';
  532. }
  533. } else {
  534. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  535. }
  536. $html->attrs['rows'] = '3';
  537. unset($html->attrs['type']);
  538. unset($html->attrs['value']);
  539. }
  540. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  541. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  542. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  543. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  544. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  545. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  546. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  547. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  548. else throw new Exception("unknown Type '{$colType['type']}'");
  549. $html->attrs['class'][] = 'form-control';
  550. $attrsOut = array();
  551. foreach ($html->attrs as $k => $v) {
  552. if (is_array($v)) $v = implode(' ', $v);
  553. $attrsOut[] = "{$k}=\"{$v}\"";
  554. }
  555. if (in_array($html->tag, array('select', 'textarea'))) {
  556. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  557. $out .= $html->cnt;
  558. $out .= '</' . $html->tag . '>';
  559. } else {
  560. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  561. }
  562. if (in_array('date', $html->_params)) {
  563. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  564. <span class="glyphicon glyphicon-calendar"></span>
  565. </span>
  566. </div>';
  567. }
  568. else if (in_array('time', $html->_params)) {
  569. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  570. <span class="glyphicon glyphicon-time"></span>
  571. </span>
  572. </div>';
  573. }
  574. if (true == V::get('appendBack', '', $params)
  575. && !in_array('date', $html->_params)
  576. && !in_array('time', $html->_params)
  577. ) {
  578. if ($html->tag == 'input' && $taskPerm == 'W') {
  579. $out = '<div class="input-group show-last-value">' . $out . '<span class="input-group-addon button-appendBack" title="' . htmlspecialchars($fValue) . '"><span class="glyphicon glyphicon-arrow-left"></span></span></div>';
  580. }
  581. }
  582. $typeSpecial = Typespecial::getInstance($fieldID, $colName);
  583. if ($typeSpecial) {
  584. throw new Exception("TODO: TypeSpecial");
  585. $tsParams = array();
  586. $tsValue = V::get('typespecialValue', '', $params);
  587. if (!empty($tsValue)) {
  588. $tsParams['typespecialValue'] = $tsValue;
  589. }
  590. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  591. }
  592. return $out;
  593. }
  594. /**
  595. * @param $taskPerm - 'C', 'W'
  596. */
  597. public function showFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  598. $out = '';
  599. if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
  600. if ($taskPerm == 'R') {
  601. $out .= 'Brak uprawnień do odczytu';
  602. }
  603. else if ($taskPerm == 'W') {
  604. $out .= 'Brak uprawnień do zapisu';
  605. } else {
  606. $out .= 'Brak uprawnień do tego pola (' . $taskPerm . ')';
  607. }
  608. return $out;
  609. }
  610. $colName = $this->getFields()[$fieldID]['name'];
  611. if ($colName == 'ID') {
  612. return $out;
  613. }
  614. $colType = $this->getFieldTypeById($fieldID);
  615. if (!$colType) {
  616. $out .= 'Error - unknown type';
  617. return $out;
  618. }
  619. $typeSpecial = Typespecial::getInstance($fieldID, $colName);
  620. $html = new stdClass();
  621. $html->_params = array();
  622. $html->tag = 'input';
  623. $html->cnt = '';
  624. $html->attrs = array();
  625. $html->attrs['id'] = $fName;
  626. $html->attrs['name'] = $fName;
  627. $html->attrs['type'] = 'text';
  628. $html->attrs['value'] = htmlspecialchars($fValue);
  629. if (isset($params['tabindex'])) {
  630. $html->attrs['tabindex'] = $params['tabindex'];
  631. }
  632. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  633. $html->attrs['disabled'] = 'disabled';
  634. }
  635. $maxGrid = V::get('maxGrid', 10, $params);
  636. if (substr($colType['type'], 0, 3) == 'int'
  637. || substr($colType['type'], 0, 7) == 'tinyint'
  638. || substr($colType['type'], 0, 8) == 'smallint'
  639. || substr($colType['type'], 0, 6) == 'bigint'
  640. ) {
  641. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  642. $html->attrs['type'] = 'number';
  643. $html->attrs['class'][] = 'input-small';
  644. }
  645. else if (substr($colType['type'], 0, 6) == 'double') {
  646. $html->attrs['type'] = 'text';
  647. $html->attrs['class'][] = 'input-small';
  648. }
  649. else if (substr($colType['type'], 0, 6) == 'float') {
  650. $html->attrs['type'] = 'text';
  651. $html->attrs['class'][] = 'input-small';
  652. }
  653. else if (substr($colType['type'], 0, 7) == 'decimal') {
  654. $html->attrs['type'] = 'text';
  655. $html->attrs['class'][] = 'input-small';
  656. }
  657. else if (substr($colType['type'], 0, 7) == 'varchar'
  658. || substr($colType['type'], 0, 4) == 'char'
  659. ) {
  660. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  661. $html->attrs['type'] = 'text';
  662. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  663. if ($maxLength > 0) {
  664. $html->attrs['maxlength'] = $maxLength;
  665. }
  666. $valLength = strlen($fValue);
  667. if (isset($params['widthClass'])) {
  668. if ($params['widthClass'] == 'inside-modal') {
  669. $html->attrs['style'] = 'width:98%;';
  670. } else {
  671. $html->attrs['style'] = 'width:98%;';
  672. }
  673. } else {
  674. /*
  675. if ($maxLength < 11) {
  676. $html->attrs['class'][] = 'span2';
  677. } else if ($maxLength < 31) {
  678. $html->attrs['class'][] = 'span5';
  679. } else if ($maxLength < 51) {
  680. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  681. } else if ($maxLength < 101) {
  682. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  683. } else {
  684. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  685. }
  686. */
  687. }
  688. if ($maxLength > 255) {// Fix for long varchar - use textarea
  689. $html->tag = 'textarea';
  690. $html->cnt = htmlspecialchars($fValue);
  691. $html->attrs['rows'] = '3';
  692. unset($html->attrs['type']);
  693. unset($html->attrs['value']);
  694. }
  695. }
  696. else if (substr($colType['type'], 0, 4) == 'date') {
  697. $testDatePicker = true;
  698. if ($testDatePicker) {
  699. $html->attrs['type'] = 'text';
  700. $html->_params[] = 'date';
  701. if (substr($colType['type'], 0, 8) == 'datetime') {
  702. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  703. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  704. $html->attrs['maxlength'] = 19;
  705. } else {
  706. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  707. $html->attrs['maxlength'] = 10;
  708. }
  709. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  710. $html->attrs['value'] = '';
  711. }
  712. } else {
  713. $html->attrs['type'] = 'date';
  714. }
  715. }
  716. else if ($colType['type'] == 'time') {
  717. $testDatePicker = true;
  718. if ($testDatePicker) {
  719. $html->attrs['type'] = 'text';
  720. $html->_params[] = 'time';
  721. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  722. $html->attrs['data-format'] = 'hh:mm:ss';
  723. $html->attrs['maxlength'] = 8;
  724. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  725. $html->attrs['value'] = '';
  726. }
  727. } else {
  728. $html->attrs['type'] = 'time';
  729. }
  730. }
  731. else if ($colType['type'] == 'timestamp') {
  732. $testDatePicker = true;
  733. if ($testDatePicker) {
  734. $html->attrs['type'] = 'text';
  735. $html->_params[] = 'date';
  736. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  737. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  738. $html->attrs['maxlength'] = 19;
  739. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  740. $html->attrs['value'] = '';
  741. }
  742. } else {
  743. $html->attrs['type'] = 'date';
  744. }
  745. }
  746. else if (substr($colType['type'], 0, 4) == 'enum') {
  747. unset($html->attrs['type']);
  748. unset($html->attrs['value']);
  749. $html->tag = 'select';
  750. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  751. $selValue = $fValue;
  752. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  753. if ($taskPerm == 'C') {
  754. $selValue = $colType['default'];
  755. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  756. $selValue = $colType['default'];
  757. }
  758. }
  759. $html->cnt .= '<option value="">' . "" . '</option>';
  760. if (!empty($selValue) && !in_array($selValue, $values)) {
  761. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  762. }
  763. foreach ($values as $val) {
  764. $sel = ($selValue == $val)? ' selected="selected"' : '';
  765. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  766. }
  767. }
  768. else if (substr($colType['type'], 0, 4) == 'text'
  769. || substr($colType['type'], 0, 8) == 'tinytext'
  770. || substr($colType['type'], 0, 10) == 'mediumtext'
  771. || substr($colType['type'], 0, 8) == 'longtext'
  772. ) {
  773. $html->tag = 'textarea';
  774. $html->cnt = htmlspecialchars($fValue);
  775. if (isset($params['widthClass'])) {
  776. if ($params['widthClass'] == 'inside-modal') {
  777. $html->attrs['style'] = 'width:98%;';
  778. } else {
  779. $html->attrs['style'] = 'width:98%;';
  780. }
  781. } else {
  782. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  783. }
  784. $html->attrs['rows'] = '3';
  785. unset($html->attrs['type']);
  786. unset($html->attrs['value']);
  787. }
  788. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  789. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  790. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  791. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  792. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  793. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  794. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  795. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  796. else {
  797. return 'unknown Type "'.$colType['type'].'"';
  798. }
  799. $html->attrs['class'][] = 'form-control';
  800. $attrsOut = array();
  801. foreach ($html->attrs as $k => $v) {
  802. if (is_array($v)) $v = implode(' ', $v);
  803. $attrsOut[] = "{$k}=\"{$v}\"";
  804. }
  805. if (in_array($html->tag, array('select', 'textarea'))) {
  806. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  807. $out .= $html->cnt;
  808. $out .= '</' . $html->tag . '>';
  809. } else {
  810. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  811. }
  812. if (in_array('date', $html->_params)) {
  813. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  814. <span class="glyphicon glyphicon-calendar"></span>
  815. </span>
  816. </div>';
  817. }
  818. else if (in_array('time', $html->_params)) {
  819. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  820. <span class="glyphicon glyphicon-time"></span>
  821. </span>
  822. </div>';
  823. }
  824. if (true == V::get('appendBack', '', $params)
  825. && !in_array('date', $html->_params)
  826. && !in_array('time', $html->_params)
  827. ) {
  828. if ($html->tag == 'input' && $taskPerm == 'W') {
  829. $out = '<div class="input-group show-last-value">' . $out . '<span class="input-group-addon button-appendBack" title="' . htmlspecialchars($fValue) . '"><span class="glyphicon glyphicon-arrow-left"></span></span></div>';
  830. }
  831. }
  832. if ($typeSpecial) {
  833. $tsParams = array();
  834. $tsValue = V::get('typespecialValue', '', $params);
  835. if (!empty($tsValue)) {
  836. $tsParams['typespecialValue'] = $tsValue;
  837. }
  838. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  839. }
  840. return $out;
  841. }
  842. /**
  843. * List table ids by database
  844. *
  845. *
  846. */
  847. public static function GetTablesByDbId($db) {
  848. DEBUG_S(3,'TableAcl_cache',$_SESSION['TableAcl_cache'],__FILE__,__FUNCTION__,__LINE__);
  849. static $_cache;
  850. $return=array();
  851. if (!$_cache) $_cache = array();
  852. if (!empty($_SESSION['TableAcl_cache'])) {
  853. foreach($_SESSION['TableAcl_cache'] as $tableID=>$obj) {
  854. //if($obj->db==$db)
  855. $return[$obj['name']]=$tableID;
  856. }
  857. return $return;
  858. }
  859. return null;
  860. }
  861. /**
  862. * Get column object. Not initialize
  863. * @returns object - column instance if exists else null
  864. *
  865. * static
  866. */
  867. public static function getInstance($idTable) {
  868. static $_cache;
  869. if (!$_cache) $_cache = array();
  870. if (array_key_exists($idTable, $_cache)) {
  871. return $_cache[$idTable];
  872. }
  873. if (!empty($_SESSION['TableAcl_cache'][$idTable])) {
  874. $tableAcl = new TableAcl($idTable);
  875. $tableAcl->fromArray($_SESSION['TableAcl_cache'][$idTable]);
  876. $_cache[$idTable] = $tableAcl;
  877. return $_cache[$idTable];
  878. }
  879. if (!empty($_SESSION['AntAcl_cache'][$idTable])) {
  880. try {
  881. $objectList = SchemaFactory::loadDefaultObject('SystemObject')->getItems([
  882. 'propertyName' => "*,field",
  883. 'f_idZasob' => "={$idTable}",
  884. ]);
  885. if (empty($objectList)) throw new Exception("Acl [{$idTable}] not exists in SystemObject"); // TODO: update Storage object list?
  886. $objItem = reset($objectList);
  887. // DBG::log($objItem, 'array', "DBG objItem({$idTable})");
  888. switch ($objItem['_type']) {
  889. // case 'TableAcl': // TODO: TEST - to replace TableAcl by AntAcl or use object with namespace + '/tableName'?
  890. case 'AntAcl': {
  891. if (!$objItem['isObjectActive']) {
  892. if (!$objItem['hasStruct']) throw new Exception("namespace has no structure '{$namespace}'");
  893. if (!$objItem['isStructInstalled']) throw new Exception("namespace structure not installed '{$namespace}'");
  894. throw new Exception("namespace is not activated '{$namespace}'");
  895. }
  896. Lib::loadClass('AntAclBase');
  897. $_cache[$idTable] = AntAclBase::buildInstance($objItem['idZasob'], $objItem);
  898. return $_cache[$idTable];
  899. }
  900. case 'StorageAcl': {
  901. if (!$objItem['isObjectActive']) {
  902. if (!$objItem['hasStruct']) throw new Exception("namespace has no structure '{$namespace}'");
  903. if (!$objItem['isStructInstalled']) throw new Exception("namespace structure not installed '{$namespace}'");
  904. throw new Exception("namespace is not activated '{$namespace}'");
  905. }
  906. Lib::loadClass('StorageAclBase');
  907. $_cache[$idTable] = StorageAclBase::buildInstance($objItem['idZasob'], $objItem);
  908. return $_cache[$idTable];
  909. }
  910. default: throw new Exception("Not Implemented acl type '{$objItem['_type']}'");
  911. }
  912. } catch (Exception $e) {
  913. DBG::log($e);
  914. }
  915. }
  916. return null;
  917. }
  918. public static function buildInstance($idTable, $tableConfig) { // TODO: move outside - app session cache?
  919. static $_cache;
  920. if (!$_cache) $_cache = array();
  921. if (array_key_exists($idTable, $_cache)) {
  922. return $_cache[$idTable];
  923. }
  924. if (empty($tableConfig)) throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
  925. // if (empty($tableConfig)) {
  926. // $zasobObj = ProcesHelper::getZasobTableInfo($idTable);
  927. // if (!$zasobObj) {
  928. // return null;// TODO: throw new Exception("Zasob TABELA ID={$idTable} nie istnieje");
  929. // }
  930. // $tableConfig['db'] = $zasobObj->P__ID;
  931. // $tableConfig['name'] = $zasobObj->DESC;
  932. // $tableConfig['label'] = $zasobObj->DESC_PL;
  933. // $tableConfig['opis'] = $zasobObj->OPIS;
  934. //
  935. // $userAcl = User::getAcl();
  936. // $userPermsForTable = $userAcl->getPermsForTable($idTable);
  937. // if (!$userPermsForTable) {
  938. // return null;// TODO: throw new Exception("Brak uprawnień do pól Tabeli nr {$idTable} '{$zasobObj->DESC}'");
  939. // }
  940. // echo'<pre>$userPermsForTable('.$idTable.') ';print_r($userPermsForTable);echo'</pre>';
  941. // if(0){// TODO: from UserAcl big query
  942. // $foundTbls[$r->ZASOB_PARENT_ID]->addField($r->ID_ZASOB, $r->ZASOB_DESC, $r->ZASOB_OPIS, $r->z__SORT_PRIO, $r->ZASOB_DESC_PL);
  943. // $foundTbls[$r->ZASOB_PARENT_ID]->setFieldPerms($r->ID_ZASOB, $r->FORM_TREAT);
  944. // $tableConfig['fields'];// $this->getFields()
  945. // $tableConfig['virtualFieldsIdList'];// $this->_virtualFieldsIdList
  946. // //$tableConfig['types'];// $this->_types
  947. // }
  948. // }
  949. if (empty($tableConfig)) throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
  950. $type = (false !== strpos($tableConfig['name'], '/')) ? 'AntAcl' : 'TableAcl'; // TODO: fix naive check this // $type = DB::getPDO()->fetchValue(" select o._type from `CRM_#CACHE_ACL_OBJECT` o where o.idZasob = '{$idTable}' limit 1 ");
  951. if ('AntAcl' === $type) {
  952. // DBG::log("AntAcl save config in TableAcl::buildInstance [{$idTable}] '{$tableConfig['name']}'");
  953. $_SESSION['AntAcl_cache'][$idTable] = true;
  954. } else { // TableAcl
  955. $obj = new TableAcl($idTable);
  956. $obj->fromArray($tableConfig);
  957. $obj->getRootTableName();
  958. $obj->save();
  959. }
  960. $_cache[$idTable] = $obj;
  961. return $_cache[$idTable];
  962. }
  963. public function loadSchema() {
  964. if ($this->_schemaLoaded) return (bool)$this->_schemaClass;
  965. $srvName = $_SERVER['SERVER_NAME'];
  966. $this->_schemaClass = Schema_TableFactory::build($this->_name, $this->_db, $srvName);
  967. $this->_schemaLoaded = true;
  968. return (bool)$this->_schemaClass;
  969. }
  970. public function getLocalFieldList() {
  971. $this->getFields();
  972. return array_keys($this->_types);
  973. }
  974. public function isLocalField($fieldName) {
  975. $this->getFields();
  976. return array_key_exists($fieldName, $this->_types);
  977. }
  978. public function init($force = false) {
  979. // DBG::log("DBG INIT - init ({$this->_zasobID}, {$this->_rootTableName})");
  980. }
  981. public function fieldsInit($force = false) {
  982. $this->loadSchema();
  983. // DBG::log("TableAcl({$this->_rootTableName}) fieldsInit() isDone: '{$this->_executed_fieldsInit}'");
  984. if (null !== $this->_executed_fieldsInit) return;
  985. $this->_executed_fieldsInit = true;
  986. DBG::log("DBG INIT - fieldsInit ({$this->_zasobID}, {$this->_rootTableName})");
  987. if (empty($this->_fields)) {
  988. $this->_types = array();// clear _types @see $this->isInitialized
  989. $userAcl = User::getAcl();
  990. $fieldsConfig = $userAcl->getPermsForTable($this->_zasobID);
  991. // DBG::log($fieldsConfig, 'array', "INIT({$this->_zasobID})::initFieldsFromConfig \$fieldsConfig");
  992. $this->initFieldsFromConfig($fieldsConfig);
  993. //DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $this, __CLASS__, __FUNCTION__, __LINE__ );
  994. }
  995. if ($this->isInitialized() && $force == false) {
  996. return;
  997. }
  998. DBG::simpleLog('auth', "TableAcl::init(name: '{$this->_name}')");
  999. // DBG::log(['$this'=>$this, 'name'=>$this->getName()], 'array', "INIT({$this->_zasobID}):: \$this");
  1000. $ds = $this->getDataSource();
  1001. // DBG::log($ds, 'array', "INIT({$this->_zasobID})::getDataSource \$ds");
  1002. $this->_types = (is_object($this->_schemaClass) && method_exists($this->_schemaClass, 'getLegacyFieldTypes'))
  1003. ? $this->_schemaClass->getLegacyFieldTypes()
  1004. : $ds->getFieldTypes();
  1005. // DBG::log($this->_types, 'array', "INIT({$this->_zasobID})::getFieldTypes \$this->_types");
  1006. uasort($this->_fields, array($this, 'sortFieldsCallback'));
  1007. $this->_fixTypes();
  1008. // DBG::log($this->_types, 'array', "INIT({$this->_zasobID})::after fixTypes \$this->_types");
  1009. $this->save();
  1010. }
  1011. public function _fixTypes() {
  1012. $this->_fixDateFields();
  1013. $this->_sortEnumFields();
  1014. $this->_fixProjectType();
  1015. if ($this->loadSchema()) $this->_types = $this->_schemaClass->fixTypes($this->_types);
  1016. //DBG::_(true, true, "this->_types", $this->_types, __CLASS__, __FUNCTION__, __LINE__);
  1017. // fixGeomTypesFromSimpleType
  1018. foreach ($this->_types as $fieldName => $field) {
  1019. // [the_geom] => Array(
  1020. // [type] => geometry
  1021. // [null] => 1
  1022. // [default] =>
  1023. // [simpleType] => linestring
  1024. if ('geometry' == $field['type']) {
  1025. if (!empty($field['simpleType'])) {
  1026. switch ($field['simpleType']) {
  1027. case 'gml:LineStringPropertyType': $this->_types[ $fieldName ][ 'type' ] = 'linestring'; break;
  1028. }
  1029. }
  1030. $dbName = DB::getPDO()->getDatabaseName();
  1031. $tableName = $this->getName();
  1032. $geomType = DB::getPDO()->fetchValue("
  1033. select DATA_TYPE
  1034. from `information_schema`.`COLUMNS`
  1035. where TABLE_SCHEMA='{$dbName}'
  1036. and TABLE_NAME='{$tableName}'
  1037. and COLUMN_NAME='{$fieldName}'
  1038. ");
  1039. if (!empty($geomType)) $this->_types[ $fieldName ][ 'type' ] = $geomType;
  1040. }
  1041. }
  1042. {// TODO: replace this by $this->_schemaClass (from DataSourceFactory::build(...))
  1043. $fieldIds = array_keys($this->_fields);
  1044. $vColsIdList = Typespecial::initFields($fieldIds);
  1045. if (!empty($vColsIdList)) {
  1046. $this->_virtualFieldsIdList = $vColsIdList;
  1047. }
  1048. }
  1049. }
  1050. public function initFieldsFromConfig($fieldsConfig) {
  1051. foreach ($fieldsConfig as $idField => $vFieldConfig) {
  1052. if ((int)$idField <= 0) {
  1053. DBG::_('DBG_SCH', '1', "BUG key must be integer - skipping '{$idField}'", $vFieldConfig, __CLASS__, __FUNCTION__, __LINE__ );
  1054. trigger_error("BUG " . __CLASS__ . "->" . __FUNCTION__ . "(\$fieldsConfig) key must be integer - skipping '{$idField}'", E_USER_NOTICE);
  1055. continue;
  1056. }
  1057. //echo'<pre>INIT::$permField('.$vFieldConfig->ID_CELL.') hasFld('.$this->hasFieldById($vFieldConfig->ID_CELL).') ';echo'</pre>';
  1058. if (!$this->hasFieldById($vFieldConfig['ID_CELL'])) {
  1059. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') addFld('.$vFieldConfig['ID_CELL'] . ', ' . $vFieldConfig['CELL_NAME'] . ', ' . $vFieldConfig['CELL_DESC'] . ', ' . $vFieldConfig['SORT_PRIO'] . ', ' . $vFieldConfig['CELL_LABEL'].') ';echo'</pre>';
  1060. $this->addField($vFieldConfig['ID_CELL'], $vFieldConfig['CELL_NAME'], $vFieldConfig['CELL_DESC'], $vFieldConfig['SORT_PRIO'], $vFieldConfig['CELL_LABEL']);
  1061. }
  1062. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') hasFld('.$this->hasFieldById($vFieldConfig['ID_CELL']).') ';echo'</pre>';
  1063. if (!isset($vFieldConfig['FORM_TREAT'])) {// TODO: convert to legacy perms
  1064. $vFieldConfig['FORM_TREAT'] = '';
  1065. if ($vFieldConfig['PERM_R'] > 0) $vFieldConfig['FORM_TREAT'] .= 'R';
  1066. if ($vFieldConfig['PERM_W'] > 0) $vFieldConfig['FORM_TREAT'] .= 'W';
  1067. if ($vFieldConfig['PERM_X'] > 0) $vFieldConfig['FORM_TREAT'] .= 'X';
  1068. if ($vFieldConfig['PERM_C'] > 0) $vFieldConfig['FORM_TREAT'] .= 'C';
  1069. if ($vFieldConfig['PERM_S'] > 0) $vFieldConfig['FORM_TREAT'] .= 'S';
  1070. if ($vFieldConfig['PERM_O'] > 0) $vFieldConfig['FORM_TREAT'] .= 'O';
  1071. if ($vFieldConfig['PERM_V'] > 0) $vFieldConfig['FORM_TREAT'] .= 'V';
  1072. if ($vFieldConfig['PERM_E'] > 0) $vFieldConfig['FORM_TREAT'] .= 'E';
  1073. }
  1074. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') ';print_r($vFieldConfig);echo'</pre>';
  1075. if (!empty($vFieldConfig['FORM_TREAT'])) {
  1076. $this->setFieldPerms($vFieldConfig['ID_CELL'], $vFieldConfig['FORM_TREAT']);
  1077. }
  1078. }
  1079. }
  1080. private function _fixProjectType() {
  1081. $tblName = $this->getName();
  1082. $fldName = 'M_DIST_TYPE';
  1083. if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
  1084. foreach ($this->_fields as $kFldId => $vFld) {
  1085. if ($vFld['name'] == $fldName) {
  1086. $sqlTypes = array();
  1087. if (!empty($this->_types[$fldName])) {
  1088. if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
  1089. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
  1090. }
  1091. }
  1092. if (!empty($sqlTypes)) {
  1093. $allowedTypes = array();
  1094. $db = DB::getDB();
  1095. $sql = "select z.DESC
  1096. from `CRM_LISTA_ZASOBOW` as z
  1097. where z.`A_STATUS`='NORMAL'
  1098. and z.`PARENT_ID`={$kFldId}
  1099. order by z.`DESC` asc
  1100. ";
  1101. $res = $db->query($sql);
  1102. while ($r = $db->fetch($res)) {
  1103. if (in_array($r->DESC, $sqlTypes)) {
  1104. $allowedTypes[] = $r->DESC;
  1105. }
  1106. }
  1107. sort($allowedTypes);
  1108. if (!empty($allowedTypes)) {
  1109. $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
  1110. }
  1111. }
  1112. }
  1113. }
  1114. }
  1115. }
  1116. private function _sortEnumFields() {
  1117. foreach ($this->_fields as $kFldId => $vFld) {
  1118. $type = $this->getFieldTypeById($kFldId);
  1119. if (!empty($type['type'])) {
  1120. if (substr($type['type'], 0, 4) == 'enum') {
  1121. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  1122. if (!empty($sqlTypes)) {
  1123. sort($sqlTypes);
  1124. $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
  1125. }
  1126. }
  1127. }
  1128. }
  1129. }
  1130. private function _fixDateFields() {
  1131. foreach ($this->_types as $kFldName => $vType) {
  1132. if ($kFldName == 'L_APPOITMENT_DATE') {
  1133. $this->_types[$kFldName]['type'] = 'datetime';
  1134. } else if ($kFldName == 'A_PROBLEM_DATE') {
  1135. $this->_types[$kFldName]['type'] = 'datetime';
  1136. }
  1137. }
  1138. }
  1139. public function getUniqueKeys() {// TODO: RM NOT USED?
  1140. $sqlKeys = array();
  1141. $dbID = $this->getDB();
  1142. $tblName = $this->getName();
  1143. $db = DB::getDB($dbID);
  1144. if (!$db) {
  1145. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  1146. }
  1147. $sql = "SHOW KEYS FROM `{$tblName}`";
  1148. $res = $db->query($sql);
  1149. while ($r = $db->fetch($res)) {
  1150. if ($r->Non_unique == '0') {
  1151. $sqlKeys[$r->Column_name] = true;
  1152. }
  1153. }
  1154. $sqlKeys = array_keys($sqlKeys);
  1155. return $sqlKeys;
  1156. }
  1157. public function sortFieldsCallback($a, $b) {
  1158. if ($a['name'] == 'ID') {
  1159. return -1;
  1160. }
  1161. else if ($b['name'] == 'ID') {
  1162. return 1;
  1163. }
  1164. else if ($a['sort_prio'] < $b['sort_prio']) {
  1165. return -1;
  1166. }
  1167. else if ($a['sort_prio'] > $b['sort_prio']) {
  1168. return 1;
  1169. }
  1170. else {
  1171. return 0;
  1172. }
  1173. }
  1174. public function isInitialized() {
  1175. return (!empty($this->_types));
  1176. }
  1177. /**
  1178. * Save data in session cache.
  1179. */
  1180. function save() {
  1181. $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
  1182. // DBG::log("save(name='{$_SESSION['TableAcl_cache'][$this->_zasobID]['name']}')");
  1183. // DBG::simpleLog('auth', "TableAcl::save(name: '{$this->_name}')");
  1184. }
  1185. public function getFieldTypeById($fieldID) {
  1186. if (!array_key_exists($fieldID, $this->getFields())) return null;
  1187. $fieldName = $this->_fields[$fieldID]['name'];
  1188. if (!array_key_exists($fieldName, $this->_types)) return null;
  1189. return $this->_types[$fieldName];
  1190. }
  1191. public function getFieldType($fieldName) {
  1192. if (!array_key_exists($fieldName, $this->_types)) return null;
  1193. return $this->_types[$fieldName];
  1194. }
  1195. public function hasFieldType($fieldName) {
  1196. return (array_key_exists($fieldName, $this->_types));
  1197. }
  1198. public function getVisibleFieldList() { return $this->getVisibleFieldListByIdZasob(); }
  1199. public function getVisibleFieldListByIdZasob() {
  1200. $primaryKeyField = $this->getSqlPrimaryKeyField();
  1201. $cols = array();
  1202. $id = 0;
  1203. foreach ($this->getFields() as $kFieldID => $vField) {
  1204. if ($vField['name'] == $primaryKeyField) {
  1205. $id = $kFieldID;
  1206. }
  1207. }
  1208. $cols[$id] = $primaryKeyField;
  1209. foreach ($this->getFields() as $kFieldID => $vField) {
  1210. if ($vField['name'] == $primaryKeyField) {
  1211. continue;
  1212. }
  1213. $cols[$kFieldID] = $vField['name'];
  1214. }
  1215. return $cols;
  1216. }
  1217. public function getExportFieldList() {
  1218. $cols = array();
  1219. $realFlds = $this->getRealFieldList();
  1220. foreach ($realFlds as $vFieldName) {
  1221. $fldId = $this->getFieldIdByName($vFieldName);
  1222. if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
  1223. $cols[] = $vFieldName;
  1224. }
  1225. }
  1226. return $cols;
  1227. }
  1228. /**
  1229. * List of real fields in database.
  1230. */
  1231. public function getRealFieldList() {
  1232. $cols = array();
  1233. $cols[] = 'ID';
  1234. foreach ($this->getFields() as $kFieldID => $vField) {
  1235. if ($vField['name'] == 'ID') {
  1236. continue;
  1237. }
  1238. if (array_key_exists($vField['name'], $this->_types)) {
  1239. $cols[] = $vField['name'];
  1240. }
  1241. }
  1242. return $cols;
  1243. }
  1244. public function getFieldListByIdZasob() {
  1245. $cols = array();
  1246. foreach ($this->getFields() as $idField => $fld) {
  1247. if ($fld['name'] == 'ID') {
  1248. $cols[$idField] = 'ID';
  1249. }
  1250. }
  1251. foreach ($this->getFields() as $idField => $fld) {
  1252. if ($fld['name'] == 'ID') continue;
  1253. $cols[$idField] = $fld['name'];
  1254. }
  1255. return $cols;
  1256. }
  1257. public function getRealFieldListByIdZasob() {
  1258. $cols = array();
  1259. foreach ($this->getFields() as $idField => $fld) {
  1260. if ($fld['name'] == 'ID') {
  1261. $cols[$idField] = 'ID';
  1262. }
  1263. }
  1264. foreach ($this->getFields() as $idField => $fld) {
  1265. if ($fld['name'] == 'ID') continue;
  1266. if (array_key_exists($fld['name'], $this->_types)) {
  1267. $cols[$idField] = $fld['name'];
  1268. }
  1269. }
  1270. return $cols;
  1271. }
  1272. public function getVirtualFieldListByIdZasob() {
  1273. $cols = array();
  1274. foreach ($this->getFields() as $kFieldID => $vField) {
  1275. if ($vField['name'] == 'ID') {
  1276. continue;
  1277. }
  1278. if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
  1279. $cols[$kFieldID] = $vField['name'];
  1280. }
  1281. else if (!array_key_exists($vField['name'], $this->_types)) {
  1282. $cols[$kFieldID] = $vField['name'];
  1283. }
  1284. }
  1285. return $cols;
  1286. }
  1287. public function getFieldLabel($fieldName) {
  1288. $idField = $this->getFieldIdByName($fieldName);
  1289. if (array_key_exists($idField, $this->getFields())) {
  1290. if (!empty($this->_fields[$idField]['label'])) {
  1291. return $this->_fields[$idField]['label'];
  1292. }
  1293. }
  1294. return null;
  1295. }
  1296. public function getFieldOpis($fieldName) {
  1297. $idField = $this->getFieldIdByName($fieldName);
  1298. if (array_key_exists($idField, $this->getFields())) {
  1299. if (!empty($this->_fields[$idField]['opis'])) {
  1300. return $this->_fields[$idField]['opis'];
  1301. }
  1302. }
  1303. return null;
  1304. }
  1305. public function getTypes() {
  1306. return $this->_types;
  1307. }
  1308. public function fixEmptyValueFromUser($fieldID) {
  1309. $value = '';
  1310. $type = $this->getFieldTypeById($fieldID);
  1311. if ($type) {
  1312. if ($type['type'] == 'date') {
  1313. $value = $type['default'];
  1314. }
  1315. if (substr($type['type'], 0, 3) == 'int'
  1316. || substr($type['type'], 0, 7) == 'tinyint'
  1317. || substr($type['type'], 0, 8) == 'smallint'
  1318. || substr($type['type'], 0, 6) == 'bigint'
  1319. ) {
  1320. $value = intval($type['default']);
  1321. }
  1322. // fix bug when field is unique and is null allowed: change empty string to null
  1323. if ($type['null']) {
  1324. $value = 'NULL';
  1325. }
  1326. // fix bug when field is enum and is set to '0': for php '0' is empty
  1327. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  1328. if (false !== strpos($type['type'], "''")) {
  1329. // enum('', '1','2')
  1330. $value = '';
  1331. } else if (false !== strpos($type['type'], "'0'")) {
  1332. // enum('0', '1','2')
  1333. $value = '0';
  1334. } else {
  1335. $value = $type['default'];
  1336. }
  1337. }
  1338. }
  1339. return $value;
  1340. }
  1341. public function fromArray($arr) {
  1342. // DBG::log($arr, 'array', "fromArray(name='{$arr['name']}')");
  1343. $this->_db = $arr['db'];
  1344. $this->_name = $arr['name'];
  1345. $this->_rootTableName = V::get('_rootTableName', null, $arr);
  1346. $this->_label = $arr['label'];
  1347. $this->_opis = $arr['opis'];
  1348. $this->_executed_fieldsInit = null; // V::get('_executed_fieldsInit', null, $arr);
  1349. $this->_fields = V::get('fields', array(), $arr);
  1350. $this->_virtualFieldsIdList = V::get('virtualFieldsIdList', array(), $arr);
  1351. $this->_types = V::get('types', array(), $arr);
  1352. }
  1353. public function toArray() {
  1354. $arr = array();
  1355. $arr['db'] = $this->_db;
  1356. $arr['name'] = $this->_name;
  1357. $arr['_rootTableName'] = $this->_rootTableName;
  1358. $arr['label'] = $this->_label;
  1359. $arr['opis'] = $this->_opis;
  1360. // if ($this->_executed_fieldsInit) $arr['_executed_fieldsInit'] = $this->_executed_fieldsInit;
  1361. $arr['fields'] = $this->_fields;
  1362. $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
  1363. $arr['types'] = $this->_types;
  1364. return $arr;
  1365. }
  1366. public function convertObjectFromUserInput($userItem, $type = 'array_by_id', $prefix = 'f') {// TODO: rename / Legacy
  1367. $item = $this->parseUserItem($userItem, $type = 'array_by_id', $prefix = 'f');
  1368. foreach ($item as $fieldName => $value) {
  1369. $item[$fieldName] = $this->validateAndFixField($fieldName, $value);
  1370. }
  1371. DBG::log(['userItem' => $userItem, 'item' => $item], '', "after parseUserItem, validateAndFixField");
  1372. return $item;
  1373. }
  1374. public function parseUserItem($userItem, $type = 'array_by_id', $prefix = 'f') {
  1375. $item = [];
  1376. foreach ($this->getFieldsMap() as $userKey => $fieldName) {
  1377. if (!array_key_exists("f{$userKey}", $userItem)) continue;
  1378. $item[$fieldName] = $userItem["f{$userKey}"];
  1379. }
  1380. return $item;
  1381. }
  1382. public function getFieldsMap() {// [ $userKey => $fieldName ] // TODO: $this->_fieldsMap
  1383. $fieldsMap = [];
  1384. foreach ($this->getFields() as $id => $field) $fieldsMap[ $id ] = $field['name'];
  1385. return $fieldsMap;
  1386. }
  1387. public function validateAndFixField($fieldName, $value) {
  1388. if (empty($value) && 0 === strlen($value)) {// TODO: fixEmptyValueFromUser
  1389. return $this->fixFieldEmptyValue($fieldName);
  1390. }
  1391. $xsdType = $this->getXsdFieldType($fieldName);
  1392. switch ($xsdType) {
  1393. case 'xsd:decimal': {
  1394. return str_replace([',', ' '], ['.', ''], $value);
  1395. } break;
  1396. case 'p5:price': {
  1397. return V::convert($value, 'price');
  1398. } break;
  1399. }
  1400. return $value;
  1401. }
  1402. public function fixFieldEmptyValue($fieldName) {// TODO: legacy
  1403. $value = '';
  1404. $type = $this->getFieldType($fieldName);
  1405. if (!$type) return '';
  1406. if ('date' == $type['type']) {
  1407. $value = $type['default'];
  1408. }
  1409. if (substr($type['type'], 0, 3) == 'int'
  1410. || substr($type['type'], 0, 7) == 'tinyint'
  1411. || substr($type['type'], 0, 8) == 'smallint'
  1412. || substr($type['type'], 0, 6) == 'bigint'
  1413. ) {
  1414. $value = intval($type['default']);
  1415. }
  1416. // fix bug when field is unique and is null allowed: change empty string to null
  1417. if ($type['null']) {
  1418. $value = 'NULL';
  1419. }
  1420. // fix bug when field is enum and is set to '0': for php '0' is empty
  1421. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  1422. // if (false !== strpos($type['type'], "''")) {
  1423. // // enum('', '1','2')
  1424. // $value = '';
  1425. // } else if (false !== strpos($type['type'], "'0'")) {
  1426. // // enum('0', '1','2')
  1427. // $value = '0';
  1428. // } else {
  1429. $value = $type['default'];
  1430. // }
  1431. }
  1432. return $value;
  1433. }
  1434. public function buildQuery($params = []) {
  1435. Lib::loadClass('AclQueryFeatures');
  1436. return new AclQueryFeatures($this, $params);
  1437. }
  1438. public function getItem($primaryKey, $params = []) {
  1439. $ds = $this->getDataSource();
  1440. return $ds->getItem($primaryKey);
  1441. }
  1442. public function prepareQuery($params = array()) {
  1443. DBG::log($params, 'array', 'TableAcl->prepareQuery $params');
  1444. $ds = $this->getDataSource();
  1445. $filtrIsInstance = []; // TODO: read instances from $params
  1446. $filtrIsNotInstance = [];
  1447. Lib::loadClass('ACL');
  1448. return ACL::query($this)
  1449. ->isInstance($filtrIsInstance)
  1450. ->isNotInstance($filtrIsNotInstance)
  1451. ->whereRaw($ds->_parseSqlWhere($params));
  1452. }
  1453. public function getItems($params = array()) {
  1454. $orderBy = V::get('order_by', '', $params);
  1455. $orderDir = V::get('order_dir', '', $params);
  1456. $sortBy = (!empty($orderBy))
  1457. ? ( !empty($orderDir) ? "{$orderBy} {$orderDir}" : $orderBy )
  1458. : '';
  1459. $limit = V::get('limit', 10, $params, 'int');
  1460. $offset = V::get('limitstart', 0, $params, 'int');
  1461. $ds = $this->getDataSource();
  1462. return $this->prepareQuery($params)
  1463. ->select([
  1464. 'rawSelect' => $ds->_getSqlCols()
  1465. ])
  1466. ->select(!empty($params['@instances']) ? '@instances' : '')
  1467. ->limit($limit)
  1468. ->offset($offset)
  1469. ->orderBy($sortBy)
  1470. ->fetchAll();
  1471. }
  1472. public function getTotal($params = array()) {
  1473. return $this->prepareQuery($params)
  1474. ->select([ 'rawSelect' => "count(1) as cnt" ])
  1475. ->fetchValue();
  1476. }
  1477. public function getFieldDefaultValue($fieldName) { // TODO: get dafault value from xsd file - TODO: p5:default attribute
  1478. $ds = $this->getDataSource();
  1479. return $ds->getColDefault($fieldName);
  1480. }
  1481. public function getColDefault($fieldName) {
  1482. return $this->getFieldDefaultValue($fieldName);
  1483. }
  1484. public function getSpecialFilters() {
  1485. $ds = $this->getDataSource();
  1486. return $ds->getSpecialFilters();
  1487. }
  1488. public function isGeomField($fldName) {
  1489. return ('the_geom' === $fldName);
  1490. // $ds = $this->getDataSource();
  1491. // return $ds->isGeomField($fldName);
  1492. }
  1493. public function getGeomFieldType($fieldName) {
  1494. if (empty($this->_types)) $this->fieldsInit($force = true);
  1495. $dbGeomType = $this->getFieldType($fieldName);
  1496. $dbGeomType = (!empty($dbGeomType['type']))? $dbGeomType['type'] : '';
  1497. $geomType = strtolower($dbGeomType);
  1498. return $geomType;
  1499. }
  1500. public function getSqlTableFrom() {
  1501. if ($this->loadSchema() && method_exists($this->_schemaClass, 'getSqlTableFrom')) {
  1502. return $this->_schemaClass->getSqlTableFrom();
  1503. }
  1504. return DB::getPDO( $this->getDB() )->tableNameQuote( $this->getRootTableName() );
  1505. }
  1506. public function getHistItems($primaryKey) {
  1507. if ($this->loadSchema() && method_exists($this->_schemaClass, 'hasGetHistItems') && $this->_schemaClass->hasGetHistItems()) {
  1508. return $this->_schemaClass->getHistItems($primaryKey);
  1509. }
  1510. $ds = $this->getDataSource();
  1511. return $ds->getHistItems($primaryKey);
  1512. }
  1513. public function getHistItem($id, $idHist) {
  1514. $ds = $this->getDataSource();
  1515. $histItems = $ds->getHistItems($id, ['ID' => $idHist]);
  1516. if (empty($histItems)) return null;
  1517. return reset($histItems);
  1518. }
  1519. public function addItem($itemTodo) {
  1520. if (is_object($itemTodo)) {
  1521. $itemTodo = (array)$itemTodo;
  1522. } else if (!is_array($itemTodo)) {
  1523. throw new HttpException('Item is not array', 400);
  1524. }
  1525. $ds = $this->getDataSource();
  1526. // from convertObjectFromUserInput - fixEmptyValueFromUser
  1527. $item = array();
  1528. $fields = $this->getFields();
  1529. foreach ($fields as $kID => $vField) {
  1530. $vFieldName = $vField['name'];
  1531. if (!$this->isAllowed($kID, 'C')) {
  1532. continue;
  1533. }
  1534. if (isset($itemTodo[$vFieldName])) {
  1535. $value = $itemTodo[$vFieldName];
  1536. if (empty($value) && strlen($value) == 0) {// fix bug in input type date and value="0000-00-00"
  1537. $value = $this->fixEmptyValueFromUser($kID);
  1538. }
  1539. $item[$vFieldName] = $value;
  1540. }
  1541. }
  1542. // add empty record is allowed - empty($item)
  1543. {// add DefaultAclGroup if no create perms ('C')
  1544. $defaultAclGroup = User::getDefaultAclGroup();
  1545. if ($defaultAclGroup) {
  1546. $permFields = array('A_ADM_COMPANY', 'A_CLASSIFIED');
  1547. foreach ($permFields as $permFldName) {
  1548. if (array_key_exists($permFldName, $this->_types)) {
  1549. $permFldId = $this->getFieldIdByName($permFldName);
  1550. if (0 == $permFldId || !$this->isAllowed($permFldId, 'C')) {
  1551. $item[$permFldName] = $defaultAclGroup;
  1552. }
  1553. }
  1554. }
  1555. }
  1556. }
  1557. DBG::_('DBG_DS', '>2', "addItem", $item, __CLASS__, __FUNCTION__, __LINE__);
  1558. return $ds->addItem($item);
  1559. }
  1560. /**
  1561. * @param array $itemPatch
  1562. */
  1563. public function updateItem($itemPatch) {
  1564. if (is_object($itemPatch)) {
  1565. $itemPatch = (array)$itemPatch;
  1566. } else if (!is_array($itemPatch)) {
  1567. throw new HttpException('Item patch is not array', 400);
  1568. }
  1569. if (empty($itemPatch)) {
  1570. //throw new Exception('Item patch is empty');
  1571. return 0;// nothing to change
  1572. }
  1573. $primaryKeyField = $this->getPrimaryKeyField();
  1574. if (empty($itemPatch[$primaryKeyField])) throw new HttpException("Item Primary Key not set!", 400);
  1575. $primaryKey = $itemPatch[$primaryKeyField];
  1576. $itemOld = $this->getItem($primaryKey);
  1577. if (!$itemOld) throw new HttpException("Item not exists!", 404);
  1578. if (!$this->canWriteRecord($itemOld) && !$this->hasPermSuperWrite()) throw new HttpException("Brak dostępu do rekordu", 403);
  1579. // $itemPatch from user input to $itemPatchChecked
  1580. $itemPatchChecked = array();
  1581. $fields = $this->getFields();
  1582. foreach ($fields as $kID => $vField) {
  1583. $vFieldName = $vField['name'];
  1584. if (!$this->isAllowed($kID, 'W', $itemOld)) {
  1585. continue;
  1586. }
  1587. if (array_key_exists($vFieldName, $itemPatch)) {
  1588. if (!$this->isAllowed($kID, 'R', $itemOld) && '*****' == $itemPatch[$vFieldName]) {
  1589. // default value for perms 'W' without 'R' is '*****'
  1590. }
  1591. else {
  1592. $value = $itemPatch[$vFieldName];
  1593. if (empty($itemPatch[$vFieldName]) && strlen($itemPatch[$vFieldName]) == 0) {// fix bug in input type date and value="0000-00-00"
  1594. $value = $this->fixEmptyValueFromUser($kID);
  1595. }
  1596. if ($value != $itemOld->$vFieldName) {
  1597. $itemPatchChecked[$vFieldName] = $value;
  1598. }
  1599. }
  1600. }
  1601. }
  1602. if (empty($itemPatchChecked)) {
  1603. //throw new HttpException("Item Primary Key not set!", 400);
  1604. return 0;// nothing to change
  1605. }
  1606. $itemPatchChecked[$primaryKeyField] = $primaryKey;
  1607. if ($this->loadSchema() && method_exists($this->_schemaClass, 'hasUpdateItem') && $this->_schemaClass->hasUpdateItem()) {
  1608. $affected = $this->_schemaClass->updateItem($itemPatchChecked);
  1609. } else {
  1610. $ds = $this->getDataSource();
  1611. $affected = $ds->updateItem($itemPatchChecked);
  1612. }
  1613. return $affected;
  1614. }
  1615. public function createItemCopy($item) {
  1616. $ds = $this->getDataSource();
  1617. $types = $this->getTypes();
  1618. $uniqKeys = $ds->getUniqueKeys();// TODO: getUniqueFields
  1619. $primaryKeyField = $ds->getPrimaryKeyField();
  1620. $itemCopy = array();
  1621. foreach ($types as $kName => $vType) {
  1622. if ($kName == $primaryKeyField) continue;
  1623. if (in_array($kName, array('A_RECORD_UPDATE_AUTHOR','A_RECORD_UPDATE_DATE'))) continue;
  1624. $value = V::get($kName, '', $item);
  1625. if (in_array($kName, $uniqKeys)) {
  1626. $value .= '?';
  1627. }
  1628. if ($ds->isGeomField($kName) && !empty($value)) {
  1629. $value = "GeomFromText('{$value}')";
  1630. }
  1631. $itemCopy[$kName] = $value;
  1632. }
  1633. return $itemCopy;
  1634. }
  1635. public function getDataSource() {
  1636. $realFieldList = $this->getRealFieldList();
  1637. $dataSource = $this->_getDataSource($realFieldList);
  1638. $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
  1639. $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
  1640. $dataSource->setFieldOwner('L_APPOITMENT_USER', $this->hasFieldType('L_APPOITMENT_USER'));
  1641. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  1642. foreach ($adminFields as $vAdmFld) {
  1643. if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
  1644. $dataSource->addCol($vAdmFld);
  1645. }
  1646. }
  1647. return $dataSource;
  1648. }
  1649. private function _getDataSource($cols) {
  1650. $dsConfig = array();
  1651. $dsConfig['source_id'] = $this->getDB();
  1652. $dsConfig['object_name'] = $this->getRootTableName();
  1653. $dsConfig['fields'] = $cols;
  1654. $dsConfig['field_types'] = $this->getTypes();
  1655. $dsConfig['fields_virtual'] = $this->getVirtualFieldListByIdZasob();
  1656. $dsConfig['acl_fltr_allowed'] = !$this->hasSuperAccessPerms(); // filtr Access is visible only if user dont have supe access perms. If has then see all rows
  1657. // DBG::log($dsConfig, 'array', "DBG:DataSourceFactory::buildFromZasobInfo(\$dsConfig)");
  1658. return DataSourceFactory::buildFromZasobInfo($dsConfig);
  1659. }
  1660. public function getPrimaryKeyField() {
  1661. if ($this->_primaryKeyField) return $this->_primaryKeyField;
  1662. if ($this->loadSchema()) {
  1663. $pkField = $this->_schemaClass->getPrimaryKeyField();
  1664. if ($pkField) {
  1665. $this->_primaryKeyField = $pkField;
  1666. return $this->_primaryKeyField;
  1667. }
  1668. }
  1669. $ds = $this->getDataSource();
  1670. $this->_primaryKeyField = $ds->getPrimaryKeyField();
  1671. return $this->_primaryKeyField;
  1672. }
  1673. public function isIntegerField($fldName) {
  1674. $type = $this->getFieldType($fldName);
  1675. if (!$type) return false;
  1676. if (substr($type['type'], 0, 3) == 'int'
  1677. || substr($type['type'], 0, 7) == 'tinyint'
  1678. || substr($type['type'], 0, 8) == 'smallint'
  1679. || substr($type['type'], 0, 9) == 'mediumint'
  1680. || substr($type['type'], 0, 6) == 'bigint'
  1681. ) {
  1682. return true;
  1683. }
  1684. return false;
  1685. }
  1686. public function isDecimalField($fldName) {
  1687. $type = $this->getFieldType($fldName);
  1688. if (!$type) return false;
  1689. if (substr($type['type'], 0, 7) == 'decimal'
  1690. || substr($type['type'], 0, 7) == 'numeric'
  1691. || substr($type['type'], 0, 6) == 'double'
  1692. || substr($type['type'], 0, 5) == 'float'
  1693. || substr($type['type'], 0, 4) == 'real'
  1694. ) {
  1695. return true;
  1696. }
  1697. return false;
  1698. }
  1699. public function isDateField($fldName) {
  1700. $type = $this->getFieldType($fldName);
  1701. if (!$type) return false;
  1702. if (substr($type['type'], 0, 4) == 'date' && substr($type['type'], 0, 8) != 'datetime') {
  1703. return true;
  1704. }
  1705. return false;
  1706. }
  1707. public function isDateTimeField($fldName) {
  1708. $type = $this->getFieldType($fldName);
  1709. if (!$type) return false;
  1710. if (substr($type['type'], 0, 8) == 'datetime') {
  1711. return true;
  1712. }
  1713. return false;
  1714. }
  1715. public function isTimeField($fldName) {
  1716. $type = $this->getFieldType($fldName);
  1717. if (!$type) return false;
  1718. return ('time' === $type['type']);
  1719. }
  1720. public function isStringField($fldName) {
  1721. $type = $this->getFieldType($fldName);
  1722. if (!$type) return false;
  1723. if (substr($type['type'], 0, 7) == 'varchar'
  1724. || substr($colType['type'], 0, 4) == 'char'
  1725. ) {
  1726. return true;
  1727. }
  1728. return false;
  1729. }
  1730. public function isTextField($fldName) {
  1731. $type = $this->getFieldType($fldName);
  1732. if (!$type) return false;
  1733. if (substr($type['type'], 0, 4) == 'text'
  1734. || substr($type['type'], 0, 8) == 'tinytext'
  1735. || substr($type['type'], 0, 10) == 'mediumtext'
  1736. || substr($type['type'], 0, 8) == 'longtext'
  1737. ) {
  1738. return true;
  1739. }
  1740. return false;
  1741. }
  1742. public function isBinaryField($fldName) {
  1743. $type = $this->getFieldType($fldName);
  1744. if (!$type) return false;
  1745. if (substr($type['type'], 0, 4) == 'blob'
  1746. || substr($type['type'], 0, 8) == 'tinyblob'
  1747. || substr($type['type'], 0, 10) == 'mediumblob'
  1748. || substr($type['type'], 0, 8) == 'longblob'
  1749. ) {
  1750. return true;
  1751. }
  1752. return false;
  1753. }
  1754. public function isEnumerationField($fldName) {
  1755. $type = $this->getFieldType($fldName);
  1756. if (!$type) return false;
  1757. if (substr($type['type'], 0, 4) == 'enum') {
  1758. return true;
  1759. }
  1760. return false;
  1761. }
  1762. public function getEnumerations($fldName) {
  1763. $type = $this->getFieldType($fldName);
  1764. if (!$type) return [];
  1765. if (!$this->isEnumerationField($fldName)) return [];
  1766. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  1767. $enum = array();
  1768. foreach ($values as $val) {
  1769. $enum[$val] = $val;
  1770. }
  1771. return $enum;
  1772. }
  1773. public function getAttributesFromZasoby() {
  1774. $attributes = array();
  1775. $db = DB::getDB();
  1776. $sql = "
  1777. select `ID` as `id_zasob`
  1778. , `DESC` as `field_name`
  1779. , `DESC_PL` as `label`
  1780. , `OPIS` as `description`
  1781. from `CRM_LISTA_ZASOBOW`
  1782. where `PARENT_ID`={$this->_zasobID}
  1783. and `TYPE`='KOMORKA'
  1784. ";
  1785. $res = $db->query($sql);
  1786. while ($r = $db->fetch($res)) {
  1787. $fldAttr = array();
  1788. $fldAttr['id_zasob'] = $r->id_zasob;
  1789. if (!empty($r->label)) $fldAttr['label'] = $r->label;
  1790. if (!empty($r->description)) $fldAttr['description'] = $r->description;
  1791. $attributes[$r->field_name] = $fldAttr;
  1792. }
  1793. // fetch field values
  1794. $sql = "
  1795. select z.`ID` as `id_zasob`
  1796. , z.`DESC` as `field_name`
  1797. , v.`DESC` as `value`
  1798. , v.`DESC_PL` as `label`
  1799. -- , v.`OPIS` as `description`
  1800. from `CRM_LISTA_ZASOBOW` z
  1801. join `CRM_LISTA_ZASOBOW` v on(v.`PARENT_ID` = z.`ID`)
  1802. where z.`PARENT_ID`={$this->_zasobID}
  1803. and z.`TYPE`='KOMORKA'
  1804. ";
  1805. $res = $db->query($sql);
  1806. while ($r = $db->fetch($res)) {
  1807. if (!empty($r->value) && !empty($r->label)) {
  1808. $valuesMap = V::get('valuesMap', array(), $attributes[$r->field_name]);
  1809. $valuesMap[$r->value] = $r->label;
  1810. $attributes[$r->field_name]['valuesMap'] = $valuesMap;
  1811. }
  1812. }
  1813. return $attributes;
  1814. }
  1815. public function getXsdFieldType($fieldName) {// @returns string - xsd type, TODO: throw Exception when field not found
  1816. static $_xsdStruct = [];
  1817. if (array_key_exists($fieldName, $_xsdStruct)) return $_xsdStruct[$fieldName];
  1818. $_xsdStruct[$fieldName] = $this->_getXsdFieldType($fieldName);
  1819. return $_xsdStruct[$fieldName];
  1820. }
  1821. public function _getXsdFieldType($fieldName) {// @returns string - xsd type, TODO: throw Exception when field not found
  1822. if ($this->loadSchema()) {
  1823. $schemaType = $this->_schemaClass->getType($fieldName);
  1824. if ($schemaType) return $schemaType;
  1825. }
  1826. $fldType = 'xsd:string';
  1827. if ('A_RECORD_UPDATE_DATE' == $fieldName) return 'xsd:string';
  1828. if ('A_RECORD_CREATE_DATE' == $fieldName) return 'xsd:string';
  1829. if ($this->isIntegerField($fieldName)) return 'xsd:integer';
  1830. else if ($this->isDecimalField($fieldName)) return 'xsd:decimal';
  1831. else if ($this->isDateTimeField($fieldName)) return 'xsd:dateTime';
  1832. else if ($this->isTimeField($fieldName)) return 'xsd:time';
  1833. else if ($this->isDateField($fieldName)) return 'xsd:date';
  1834. else if ($this->isTextField($fieldName)) return 'p5:text';
  1835. else if ($this->isGeomField($fieldName)) {
  1836. //$fldType = 'gml:GeometryPropertyType';
  1837. $geomType = $this->getGeomFieldType($fieldName);
  1838. // DBG::log("DBG Geom field type ({$fieldName}) type({$geomType})");
  1839. if ('polygon' == $geomType) return 'gml:PolygonPropertyType';
  1840. else if ('point' == $geomType) return 'gml:PointPropertyType';
  1841. else if ('linestring' == $geomType) return 'gml:LineStringPropertyType';
  1842. else return 'gml:GeometryPropertyType';
  1843. }
  1844. else if ($this->isEnumerationField($fieldName)) return 'p5:enum';
  1845. else if ($this->isBinaryField($fieldName)) return 'xsd:base64Binary';
  1846. return $fldType;
  1847. }
  1848. public function getXsdFieldParam($fieldName, $paramKey) {
  1849. return ($this->loadSchema())
  1850. ? $this->_schemaClass->getFieldParam($fieldName, $paramKey)
  1851. : $this->_getXsdFieldParam($fieldName, $paramKey)
  1852. ;
  1853. }
  1854. public function _getXsdFieldParam($fieldName, $paramKey) {
  1855. switch ($paramKey) {
  1856. case 'maxLength': {
  1857. $colType = $this->getFieldType($fieldName);
  1858. if (!$colType) return null;
  1859. return (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  1860. }
  1861. }
  1862. return null;
  1863. }
  1864. public function getXsdTypes() { // @returns [ fieldName => xsdType, ... ]
  1865. $xsdTypes = [];
  1866. foreach ($this->getFields() as $field) {
  1867. $fieldName = $field['name'];
  1868. $xsdTypes[ $fieldName ] = $this->getXsdFieldType( $fieldName );
  1869. }
  1870. return $xsdTypes;
  1871. }
  1872. public function getFieldsWithXsdTypes() {
  1873. $xsdTypes = [];
  1874. foreach ($this->getFields() as $idZasob => $field) {
  1875. $xsdTypes[ $field['name'] ] = [
  1876. 'name' => $field['name'],
  1877. 'perms' => $field['perms'],
  1878. 'idZasob' => $idZasob,
  1879. 'xsdType' => $this->getXsdFieldType($field['name']),
  1880. ];
  1881. }
  1882. return $xsdTypes;
  1883. }
  1884. public function getSqlFieldName($childName) {
  1885. return $childName;
  1886. }
  1887. public function hasField($fieldName) {// TODO: fix use Core_AclFields or ACL::getObjectFields($namespace) - use cache - one place to store structure
  1888. return array_key_exists($fieldName, $this->_types);
  1889. }
  1890. public function hasWriteGroupField() { return $this->hasField('A_ADM_COMPANY'); }
  1891. public function hasReadGroupField() { return $this->hasField('A_CLASSIFIED'); }
  1892. public function hasOwnerField() { return $this->hasField('L_APPOITMENT_USER'); }
  1893. }