TableAcl.php 51 KB

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