TableAcl.php 52 KB

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