TableAcl.php 58 KB

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