TableAcl.php 57 KB

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