TableAcl.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695
  1. <?php
  2. /**
  3. * $_SESSION['TableAcl_cache'][$tableID] = array(
  4. * [db] => DB zasob ID
  5. * [name] => Table name
  6. * [opis] => Table opis
  7. * [fields] => array(
  8. * [$fieldID] => array(
  9. * [name] => name
  10. * [perms] => perms (FORM_TREAT)
  11. * [opis] => opis
  12. * )
  13. * )
  14. * [types] => array(
  15. * [$fieldID] => array(
  16. * [type] => type
  17. * [null] => bool
  18. * [default] => default value
  19. * )
  20. * )
  21. * );
  22. */
  23. class TableAcl {
  24. private $_zasobID = '';
  25. private $_db = '';
  26. private $_name = '';
  27. private $_label = '';
  28. private $_opis = '';
  29. private $_fields = array();
  30. private $_types = array();
  31. private $_virtualFieldsIdList = array();
  32. private $_schemaLoaded = false;
  33. public function __construct($zasobID) {
  34. $this->_zasobID = $zasobID;
  35. }
  36. public function getID() {
  37. return $this->_zasobID;
  38. }
  39. public function setName($name) {
  40. $this->_name = $name;
  41. }
  42. public function setNameByTableId($tableID) {
  43. //used for init without knowing table name
  44. $db = DB::getDB();
  45. $sql = "select `DESC` from CRM_LISTA_ZASOBOW where ID={$tableID} and `TYPE`='TABELA'";
  46. $res = $db->query($sql);
  47. $res_ = $db->fetch($res);
  48. //DEBUG_S(-3,'setNameByTableId',$res_,__FILE__,__FUNCTION__,__LINE__);
  49. self::setName($res->DESC);
  50. }
  51. public function getName() {
  52. return $this->_name;
  53. }
  54. public function setOpis($opis) {
  55. $this->_opis = $opis;
  56. }
  57. public function getOpis() {
  58. return $this->_opis;
  59. }
  60. public function setLabel($label) {
  61. $this->_label = $label;
  62. }
  63. public function getLabel() {
  64. return $this->_label;
  65. }
  66. public function getRawLabel($posLimit = 20) {
  67. $label = $this->_label;
  68. if (empty($label) && !empty($this->_opis)) {
  69. $label = $this->_opis;
  70. if (mb_strlen($this->_opis) > $posLimit) {
  71. $pos = strpos($this->_opis, ' - ');
  72. if ($pos > $posLimit || $pos < 5) {
  73. $pos = $posLimit;
  74. $label = mb_substr($this->_opis, 0, $posLimit, 'utf-8') . '...';
  75. } else {
  76. $label = mb_substr($this->_opis, 0, $pos, 'utf-8');
  77. }
  78. }
  79. }
  80. if (empty($label)) {
  81. $label = $this->_name;
  82. }
  83. return $label;
  84. }
  85. public function getShortLabel($posLimit = 20) {
  86. $shortLabel = $this->getRawLabel($posLimit);
  87. $opis = $this->_opis;
  88. $shortLabel = '<span title="' . htmlspecialchars($opis) . '">' . $shortLabel . '</span>';
  89. return $shortLabel;
  90. }
  91. public function getLongLabel($posLimit = 30) {
  92. $longLabel = $this->getRawLabel($posLimit);
  93. $opis = $this->_opis;
  94. if ($longLabel != $this->_name) {
  95. $longLabel .= ' <em>' . $this->_name . '</em>';
  96. }
  97. $longLabel = '<span title="' . htmlspecialchars($opis) . '">' . $longLabel . '</span>';
  98. return $longLabel;
  99. }
  100. public function setDB($db) {
  101. $this->_db = $db;
  102. }
  103. public function getDB() {
  104. return $this->_db;
  105. }
  106. public function addField($fieldID, $name, $opis, $sort_prio, $label = '') {
  107. $field = array();
  108. $field['name'] = $name;
  109. $field['perms'] = '';
  110. $field['opis'] = $opis;
  111. $field['sort_prio'] = $sort_prio;
  112. $field['label'] = $label;
  113. $this->_fields[$fieldID] = $field;
  114. }
  115. public function getTableDbId($tableID) {
  116. return $this->_db;
  117. }
  118. public function getField($fieldID) {
  119. return $this->_fields[$fieldID];
  120. }
  121. public function hasField($fieldID) {
  122. return array_key_exists($fieldID, $this->_fields);
  123. }
  124. public function removeField($fieldID) {
  125. if (array_key_exists($fieldID, $this->_fields)) {
  126. unset($this->_fields[$fieldID]);
  127. }
  128. }
  129. public function getFields() {
  130. return $this->_fields;
  131. }
  132. public function setFieldPerms($fieldID, $perms) {
  133. if (array_key_exists($fieldID, $this->_fields)) {
  134. $this->_fields[$fieldID]['perms'] .= $perms;
  135. }
  136. }
  137. public function getFieldPerms($fieldID) {
  138. if (array_key_exists($fieldID, $this->_fields)) {
  139. $perms = V::get('perms', '', $this->_fields[$fieldID]);
  140. if ($perms) {
  141. return implode(',', array_unique(str_split($perms)));
  142. }
  143. }
  144. return '';
  145. }
  146. public function hasFieldPerm($fieldID, $perm) {
  147. if (array_key_exists($fieldID, $this->_fields)) {
  148. if (false !== strpos($this->_fields[$fieldID]['perms'], $perm)) {
  149. return true;
  150. }
  151. return false;
  152. }
  153. return false;
  154. }
  155. public function getFieldIdByName($fieldName) {
  156. $fieldID = 0;
  157. if (empty($fieldName)) {
  158. return;
  159. }
  160. foreach ($this->_fields as $kID => $vField) {
  161. if ($vField['name'] == $fieldName) {
  162. $fieldID = $kID;
  163. }
  164. }
  165. return $fieldID;
  166. }
  167. public function hasEditPerms() {
  168. foreach ($this->_fields as $kFldID => $vFld) {
  169. if ($this->hasFieldPerm($kFldID, 'W')) return true;
  170. if ($this->hasFieldPerm($kFldID, 'C')) return true;
  171. if ($this->hasFieldPerm($kFldID, 'S')) return true;
  172. }
  173. return false;
  174. }
  175. public function hasCreatePerms() {
  176. foreach ($this->_fields as $kFldID => $vFld) {
  177. if ($this->hasFieldPerm($kFldID, 'C')) return true;
  178. }
  179. return false;
  180. }
  181. public function hasSuperAccessPerms() {
  182. foreach ($this->_fields as $kFldID => $vFld) {
  183. if ($this->hasFieldPerm($kFldID, 'S')) {
  184. return true;
  185. }
  186. else if ($this->hasFieldPerm($kFldID, 'V')) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. public function hasPermSuperWrite() {
  193. foreach ($this->_fields as $kFldID => $vFld) {
  194. if ($this->hasFieldPerm($kFldID, 'S')) {
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. /**
  201. *
  202. */
  203. public function canWriteRecord($record) {
  204. $dbgArr = array();
  205. $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : '';
  206. $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : '';
  207. $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : '';
  208. $dbgArr['user_groups'] = User::getLdapGroupsNames();
  209. 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>';}
  210. if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
  211. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - is record owner</p>';}
  212. return true;
  213. }
  214. if ($dbgArr['record_write']) {
  215. if (in_array($dbgArr['record_write'], $dbgArr['user_groups'])) {
  216. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - has group write</p>';}
  217. return true;
  218. }
  219. } else {
  220. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - group write not set</p>';}
  221. return true;
  222. }
  223. return false;
  224. }
  225. public function canReadRecord($record) {
  226. $dbgArr = array();
  227. $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : '';
  228. $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : '';
  229. $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : '';
  230. $dbgArr['user_groups'] = User::getLdapGroupsNames();
  231. 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>';}
  232. if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
  233. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - is record owner</p>';}
  234. return true;
  235. }
  236. if ($dbgArr['record_read']) {
  237. if (in_array($dbgArr['record_read'], $dbgArr['user_groups'])) {
  238. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - has group read</p>';}
  239. return true;
  240. }
  241. } else {
  242. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - group read not set</p>';}
  243. return true;
  244. }
  245. return false;
  246. }
  247. /**
  248. * @param $taskPerm - 'C', 'W', 'R'
  249. */
  250. public function isAllowed($fieldID, $taskPerm, $record = null) {
  251. if (!in_array($taskPerm, array('C', 'W', 'R'))) {
  252. return false;
  253. }
  254. $adminFields = array();
  255. $adminFields[] = 'ID';
  256. $adminFields[] = 'A_RECORD_CREATE_DATE';
  257. $adminFields[] = 'A_RECORD_CREATE_AUTHOR';
  258. $adminFields[] = 'A_RECORD_UPDATE_DATE';
  259. $adminFields[] = 'A_RECORD_UPDATE_AUTHOR';
  260. $fieldName = $this->_fields[$fieldID]['name'];
  261. if ($taskPerm == 'R' && in_array($fieldName, $adminFields)) {
  262. return true;
  263. }
  264. // check perm: allow 'RS', 'WS' - can R/W field even if cant read record
  265. // check 'O' - can read field even if cant read field but can read record
  266. 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.')'
  267. ,'taskPerm'=>$taskPerm
  268. ,'fieldPerms'=>V::get('perms', null, V::get($fieldID, null, $this->_fields))
  269. ,'canReadRecord'=>'"'.$this->canReadRecord($record).'"'
  270. ,'hasFieldPerm(O) || canWriteRecord'=>'"'.$this->hasFieldPerm($fieldID, 'O').'" || "'.$this->canReadRecord($record).'"'
  271. ,'hasFieldPerm(S)'=>'"'.$this->hasFieldPerm($fieldID, 'S').'"'
  272. ,'hasFieldPerm(V)'=>'"'.$this->hasFieldPerm($fieldID, 'V').'"'
  273. ));echo'</pre>'; }
  274. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  275. if ($taskPerm == 'R' && $this->hasFieldPerm($fieldID, 'V')) {
  276. return true;
  277. } else if ($taskPerm == 'R'
  278. && $record
  279. && $this->hasFieldPerm($fieldID, 'O')
  280. && ($this->canReadRecord($record) || $this->canWriteRecord($record))
  281. ) {
  282. return true;// 'WO' or 'CO'
  283. }
  284. return false;
  285. }
  286. // check 'R' - require can read record, or V - Super View
  287. if ($taskPerm == 'R') {
  288. if ($this->canReadRecord($record) || $this->hasFieldPerm($fieldID, 'V')) {
  289. return true;
  290. } else {
  291. return false;
  292. }
  293. }
  294. // 'C' and 'W' require colType
  295. $colType = $this->getFieldTypeById($fieldID);
  296. if (!$colType) {
  297. return false;
  298. }
  299. if ($taskPerm == 'W') {
  300. if ($record) {
  301. 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') . '))';}
  302. return ($this->canWriteRecord($record)|| $this->hasFieldPerm($fieldID, 'S'));
  303. }
  304. }
  305. return true;
  306. }
  307. /**
  308. * @param $taskPerm - 'C', 'W'
  309. */
  310. public function getFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  311. $out = '';
  312. if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
  313. if ($taskPerm == 'R') throw new Exception("Brak uprawnień do odczytu");
  314. else if ($taskPerm == 'W') throw new Exception("Brak uprawnień do zapisu");
  315. else throw new Exception("Brak uprawnień do tego pola ({$taskPerm})");
  316. }
  317. $colName = $this->_fields[$fieldID]['name'];
  318. if ($colName == 'ID') return;
  319. $colType = $this->getFieldTypeById($fieldID);
  320. if (!$colType) throw new Exception("Error - unknown type");
  321. $item = array();
  322. $item['htmlType'] = '';
  323. $item['rawColType'] = $colType;
  324. return $item;
  325. $html = new stdClass();
  326. $html->_params = array();
  327. $html->tag = 'input';
  328. $html->cnt = '';
  329. $html->attrs = array();
  330. $html->attrs['id'] = $fName;
  331. $html->attrs['name'] = $fName;
  332. $html->attrs['type'] = 'text';
  333. $html->attrs['value'] = htmlspecialchars($fValue);
  334. if (isset($params['tabindex'])) {
  335. $html->attrs['tabindex'] = $params['tabindex'];
  336. }
  337. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  338. $html->attrs['disabled'] = 'disabled';
  339. }
  340. $maxGrid = V::get('maxGrid', 10, $params);
  341. if (substr($colType['type'], 0, 3) == 'int'
  342. || substr($colType['type'], 0, 7) == 'tinyint'
  343. || substr($colType['type'], 0, 8) == 'smallint'
  344. || substr($colType['type'], 0, 6) == 'bigint'
  345. ) {
  346. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  347. $html->attrs['type'] = 'number';
  348. $html->attrs['class'][] = 'input-small';
  349. }
  350. else if (substr($colType['type'], 0, 6) == 'double') {
  351. $html->attrs['type'] = 'text';
  352. $html->attrs['class'][] = 'input-small';
  353. }
  354. else if (substr($colType['type'], 0, 7) == 'decimal') {
  355. $html->attrs['type'] = 'text';
  356. $html->attrs['class'][] = 'input-small';
  357. }
  358. else if (substr($colType['type'], 0, 7) == 'varchar'
  359. || substr($colType['type'], 0, 4) == 'char'
  360. ) {
  361. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  362. $html->attrs['type'] = 'text';
  363. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  364. if ($maxLength > 0) {
  365. $html->attrs['maxlength'] = $maxLength;
  366. }
  367. $valLength = strlen($fValue);
  368. if (isset($params['widthClass'])) {
  369. if ($params['widthClass'] == 'inside-modal') {
  370. $html->attrs['style'] = 'width:98%;';
  371. } else {
  372. $html->attrs['style'] = 'width:98%;';
  373. }
  374. } else {
  375. /*
  376. if ($maxLength < 11) {
  377. $html->attrs['class'][] = 'span2';
  378. } else if ($maxLength < 31) {
  379. $html->attrs['class'][] = 'span5';
  380. } else if ($maxLength < 51) {
  381. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  382. } else if ($maxLength < 101) {
  383. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  384. } else {
  385. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  386. }
  387. */
  388. }
  389. if ($maxLength > 255) {// Fix for long varchar - use textarea
  390. $html->tag = 'textarea';
  391. $html->cnt = htmlspecialchars($fValue);
  392. $html->attrs['rows'] = '3';
  393. unset($html->attrs['type']);
  394. unset($html->attrs['value']);
  395. }
  396. }
  397. else if (substr($colType['type'], 0, 4) == 'date') {
  398. $testDatePicker = true;
  399. if ($testDatePicker) {
  400. $html->attrs['type'] = 'text';
  401. $html->_params[] = 'date';
  402. if (substr($colType['type'], 0, 8) == 'datetime') {
  403. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  404. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  405. $html->attrs['maxlength'] = 19;
  406. } else {
  407. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  408. $html->attrs['maxlength'] = 10;
  409. }
  410. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  411. $html->attrs['value'] = '';
  412. }
  413. } else {
  414. $html->attrs['type'] = 'date';
  415. }
  416. }
  417. else if ($colType['type'] == 'time') {
  418. $testDatePicker = true;
  419. if ($testDatePicker) {
  420. $html->attrs['type'] = 'text';
  421. $html->_params[] = 'time';
  422. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  423. $html->attrs['data-format'] = 'hh:mm:ss';
  424. $html->attrs['maxlength'] = 8;
  425. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  426. $html->attrs['value'] = '';
  427. }
  428. } else {
  429. $html->attrs['type'] = 'time';
  430. }
  431. }
  432. else if (substr($colType['type'], 0, 4) == 'enum') {
  433. unset($html->attrs['type']);
  434. unset($html->attrs['value']);
  435. $html->tag = 'select';
  436. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  437. $selValue = $fValue;
  438. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  439. if ($taskPerm == 'C') {
  440. $selValue = $colType['default'];
  441. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  442. $selValue = $colType['default'];
  443. }
  444. }
  445. $html->cnt .= '<option value="">' . "" . '</option>';
  446. if (!empty($selValue) && !in_array($selValue, $values)) {
  447. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  448. }
  449. foreach ($values as $val) {
  450. $sel = ($selValue == $val)? ' selected="selected"' : '';
  451. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  452. }
  453. }
  454. else if (substr($colType['type'], 0, 4) == 'text'
  455. || substr($colType['type'], 0, 8) == 'tinytext'
  456. || substr($colType['type'], 0, 10) == 'mediumtext'
  457. || substr($colType['type'], 0, 8) == 'longtext'
  458. ) {
  459. $html->tag = 'textarea';
  460. $html->cnt = htmlspecialchars($fValue);
  461. if (isset($params['widthClass'])) {
  462. if ($params['widthClass'] == 'inside-modal') {
  463. $html->attrs['style'] = 'width:98%;';
  464. } else {
  465. $html->attrs['style'] = 'width:98%;';
  466. }
  467. } else {
  468. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  469. }
  470. $html->attrs['rows'] = '3';
  471. unset($html->attrs['type']);
  472. unset($html->attrs['value']);
  473. }
  474. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  475. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  476. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  477. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  478. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  479. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  480. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  481. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  482. else throw new Exception("unknown Type '{$colType['type']}'");
  483. $html->attrs['class'][] = 'form-control';
  484. $attrsOut = array();
  485. foreach ($html->attrs as $k => $v) {
  486. if (is_array($v)) $v = implode(' ', $v);
  487. $attrsOut[] = "{$k}=\"{$v}\"";
  488. }
  489. if (in_array($html->tag, array('select', 'textarea'))) {
  490. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  491. $out .= $html->cnt;
  492. $out .= '</' . $html->tag . '>';
  493. } else {
  494. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  495. }
  496. if (in_array('date', $html->_params)) {
  497. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  498. <span class="glyphicon glyphicon-calendar"></span>
  499. </span>
  500. </div>';
  501. }
  502. else if (in_array('time', $html->_params)) {
  503. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  504. <span class="glyphicon glyphicon-time"></span>
  505. </span>
  506. </div>';
  507. }
  508. if (true == V::get('appendBack', '', $params)
  509. && !in_array('date', $html->_params)
  510. && !in_array('time', $html->_params)
  511. ) {
  512. if ($html->tag == 'input' && $taskPerm == 'W') {
  513. $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>';
  514. }
  515. }
  516. Lib::loadClass('Typespecial');
  517. $typeSpecial = Typespecial::getInstance($fieldID, $colName);
  518. if ($typeSpecial) {
  519. throw new Exception("TODO: TypeSpecial");
  520. $tsParams = array();
  521. $tsValue = V::get('typespecialValue', '', $params);
  522. if (!empty($tsValue)) {
  523. $tsParams['typespecialValue'] = $tsValue;
  524. }
  525. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  526. }
  527. return $out;
  528. }
  529. /**
  530. * @param $taskPerm - 'C', 'W'
  531. */
  532. public function showFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  533. $out = '';
  534. if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
  535. if ($taskPerm == 'R') {
  536. $out .= 'Brak uprawnień do odczytu';
  537. }
  538. else if ($taskPerm == 'W') {
  539. $out .= 'Brak uprawnień do zapisu';
  540. } else {
  541. $out .= 'Brak uprawnień do tego pola (' . $taskPerm . ')';
  542. }
  543. return $out;
  544. }
  545. $colName = $this->_fields[$fieldID]['name'];
  546. if ($colName == 'ID') {
  547. return $out;
  548. }
  549. $colType = $this->getFieldTypeById($fieldID);
  550. if (!$colType) {
  551. $out .= 'Error - unknown type';
  552. return $out;
  553. }
  554. Lib::loadClass('Typespecial');
  555. $typeSpecial = Typespecial::getInstance($fieldID, $colName);
  556. $html = new stdClass();
  557. $html->_params = array();
  558. $html->tag = 'input';
  559. $html->cnt = '';
  560. $html->attrs = array();
  561. $html->attrs['id'] = $fName;
  562. $html->attrs['name'] = $fName;
  563. $html->attrs['type'] = 'text';
  564. $html->attrs['value'] = htmlspecialchars($fValue);
  565. if (isset($params['tabindex'])) {
  566. $html->attrs['tabindex'] = $params['tabindex'];
  567. }
  568. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  569. $html->attrs['disabled'] = 'disabled';
  570. }
  571. $maxGrid = V::get('maxGrid', 10, $params);
  572. if (substr($colType['type'], 0, 3) == 'int'
  573. || substr($colType['type'], 0, 7) == 'tinyint'
  574. || substr($colType['type'], 0, 8) == 'smallint'
  575. || substr($colType['type'], 0, 6) == 'bigint'
  576. ) {
  577. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  578. $html->attrs['type'] = 'number';
  579. $html->attrs['class'][] = 'input-small';
  580. }
  581. else if (substr($colType['type'], 0, 6) == 'double') {
  582. $html->attrs['type'] = 'text';
  583. $html->attrs['class'][] = 'input-small';
  584. }
  585. else if (substr($colType['type'], 0, 7) == 'decimal') {
  586. $html->attrs['type'] = 'text';
  587. $html->attrs['class'][] = 'input-small';
  588. }
  589. else if (substr($colType['type'], 0, 7) == 'varchar'
  590. || substr($colType['type'], 0, 4) == 'char'
  591. ) {
  592. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  593. $html->attrs['type'] = 'text';
  594. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  595. if ($maxLength > 0) {
  596. $html->attrs['maxlength'] = $maxLength;
  597. }
  598. $valLength = strlen($fValue);
  599. if (isset($params['widthClass'])) {
  600. if ($params['widthClass'] == 'inside-modal') {
  601. $html->attrs['style'] = 'width:98%;';
  602. } else {
  603. $html->attrs['style'] = 'width:98%;';
  604. }
  605. } else {
  606. /*
  607. if ($maxLength < 11) {
  608. $html->attrs['class'][] = 'span2';
  609. } else if ($maxLength < 31) {
  610. $html->attrs['class'][] = 'span5';
  611. } else if ($maxLength < 51) {
  612. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  613. } else if ($maxLength < 101) {
  614. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  615. } else {
  616. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  617. }
  618. */
  619. }
  620. if ($maxLength > 255) {// Fix for long varchar - use textarea
  621. $html->tag = 'textarea';
  622. $html->cnt = htmlspecialchars($fValue);
  623. $html->attrs['rows'] = '3';
  624. unset($html->attrs['type']);
  625. unset($html->attrs['value']);
  626. }
  627. }
  628. else if (substr($colType['type'], 0, 4) == 'date') {
  629. $testDatePicker = true;
  630. if ($testDatePicker) {
  631. $html->attrs['type'] = 'text';
  632. $html->_params[] = 'date';
  633. if (substr($colType['type'], 0, 8) == 'datetime') {
  634. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  635. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  636. $html->attrs['maxlength'] = 19;
  637. } else {
  638. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  639. $html->attrs['maxlength'] = 10;
  640. }
  641. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  642. $html->attrs['value'] = '';
  643. }
  644. } else {
  645. $html->attrs['type'] = 'date';
  646. }
  647. }
  648. else if ($colType['type'] == 'time') {
  649. $testDatePicker = true;
  650. if ($testDatePicker) {
  651. $html->attrs['type'] = 'text';
  652. $html->_params[] = 'time';
  653. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  654. $html->attrs['data-format'] = 'hh:mm:ss';
  655. $html->attrs['maxlength'] = 8;
  656. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  657. $html->attrs['value'] = '';
  658. }
  659. } else {
  660. $html->attrs['type'] = 'time';
  661. }
  662. }
  663. else if (substr($colType['type'], 0, 4) == 'enum') {
  664. unset($html->attrs['type']);
  665. unset($html->attrs['value']);
  666. $html->tag = 'select';
  667. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  668. $selValue = $fValue;
  669. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  670. if ($taskPerm == 'C') {
  671. $selValue = $colType['default'];
  672. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  673. $selValue = $colType['default'];
  674. }
  675. }
  676. $html->cnt .= '<option value="">' . "" . '</option>';
  677. if (!empty($selValue) && !in_array($selValue, $values)) {
  678. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  679. }
  680. foreach ($values as $val) {
  681. $sel = ($selValue == $val)? ' selected="selected"' : '';
  682. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  683. }
  684. }
  685. else if (substr($colType['type'], 0, 4) == 'text'
  686. || substr($colType['type'], 0, 8) == 'tinytext'
  687. || substr($colType['type'], 0, 10) == 'mediumtext'
  688. || substr($colType['type'], 0, 8) == 'longtext'
  689. ) {
  690. $html->tag = 'textarea';
  691. $html->cnt = htmlspecialchars($fValue);
  692. if (isset($params['widthClass'])) {
  693. if ($params['widthClass'] == 'inside-modal') {
  694. $html->attrs['style'] = 'width:98%;';
  695. } else {
  696. $html->attrs['style'] = 'width:98%;';
  697. }
  698. } else {
  699. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  700. }
  701. $html->attrs['rows'] = '3';
  702. unset($html->attrs['type']);
  703. unset($html->attrs['value']);
  704. }
  705. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  706. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  707. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  708. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  709. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  710. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  711. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  712. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  713. else {
  714. return 'unknown Type "'.$colType['type'].'"';
  715. }
  716. $html->attrs['class'][] = 'form-control';
  717. $attrsOut = array();
  718. foreach ($html->attrs as $k => $v) {
  719. if (is_array($v)) $v = implode(' ', $v);
  720. $attrsOut[] = "{$k}=\"{$v}\"";
  721. }
  722. if (in_array($html->tag, array('select', 'textarea'))) {
  723. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  724. $out .= $html->cnt;
  725. $out .= '</' . $html->tag . '>';
  726. } else {
  727. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  728. }
  729. if (in_array('date', $html->_params)) {
  730. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  731. <span class="glyphicon glyphicon-calendar"></span>
  732. </span>
  733. </div>';
  734. }
  735. else if (in_array('time', $html->_params)) {
  736. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  737. <span class="glyphicon glyphicon-time"></span>
  738. </span>
  739. </div>';
  740. }
  741. if (true == V::get('appendBack', '', $params)
  742. && !in_array('date', $html->_params)
  743. && !in_array('time', $html->_params)
  744. ) {
  745. if ($html->tag == 'input' && $taskPerm == 'W') {
  746. $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>';
  747. }
  748. }
  749. if ($typeSpecial) {
  750. $tsParams = array();
  751. $tsValue = V::get('typespecialValue', '', $params);
  752. if (!empty($tsValue)) {
  753. $tsParams['typespecialValue'] = $tsValue;
  754. }
  755. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  756. }
  757. return $out;
  758. }
  759. /**
  760. * List table ids by database
  761. *
  762. *
  763. */
  764. public static function GetTablesByDbId($db) {
  765. DEBUG_S(3,'TableAcl_cache',$_SESSION['TableAcl_cache'],__FILE__,__FUNCTION__,__LINE__);
  766. static $_cache;
  767. $return=array();
  768. if (!$_cache) $_cache = array();
  769. if (!empty($_SESSION['TableAcl_cache'])) {
  770. foreach($_SESSION['TableAcl_cache'] as $tableID=>$obj) {
  771. //if($obj->db==$db)
  772. $return[$obj['name']]=$tableID;
  773. }
  774. return $return;
  775. }
  776. return null;
  777. }
  778. /**
  779. * Get column object. Not initialize
  780. * @returns object - column instance if exists else null
  781. *
  782. * static
  783. */
  784. public static function getInstance($idTable) {
  785. static $_cache;
  786. if (!$_cache) $_cache = array();
  787. if (array_key_exists($idTable, $_cache)) {
  788. return $_cache[$idTable];
  789. }
  790. if (!empty($_SESSION['TableAcl_cache'][$idTable])) {
  791. $tableAcl = new TableAcl($idTable);
  792. $tableAcl->fromArray($_SESSION['TableAcl_cache'][$idTable]);
  793. $_cache[$idTable] = $tableAcl;
  794. return $_cache[$idTable];
  795. }
  796. return null;
  797. }
  798. public static function buildInstance($idTable, $tableConfig) {
  799. static $_cache;
  800. if (!$_cache) $_cache = array();
  801. if (array_key_exists($idTable, $_cache)) {
  802. return $_cache[$idTable];
  803. }
  804. if (empty($tableConfig)) {
  805. throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
  806. Lib::loadClass('ProcesHelper');
  807. $zasobObj = ProcesHelper::getZasobTableInfo($idTable);
  808. if (!$zasobObj) {
  809. return null;// TODO: throw new Exception("Zasob TABELA ID={$idTable} nie istnieje");
  810. }
  811. $tableConfig['db'] = $zasobObj->P__ID;
  812. $tableConfig['name'] = $zasobObj->DESC;
  813. $tableConfig['label'] = $zasobObj->DESC_PL;
  814. $tableConfig['opis'] = $zasobObj->OPIS;
  815. $userAcl = User::getAcl();
  816. $userPermsForTable = $userAcl->getPermsForTable($idTable);
  817. if (!$userPermsForTable) {
  818. return null;// TODO: throw new Exception("Brak uprawnień do pól Tabeli nr {$idTable} '{$zasobObj->DESC}'");
  819. }
  820. echo'<pre>$userPermsForTable('.$idTable.') ';print_r($userPermsForTable);echo'</pre>';
  821. if(0){// TODO: from UserAcl big query
  822. $foundTbls[$r->ZASOB_PARENT_ID]->addField($r->ID_ZASOB, $r->ZASOB_DESC, $r->ZASOB_OPIS, $r->z__SORT_PRIO, $r->ZASOB_DESC_PL);
  823. $foundTbls[$r->ZASOB_PARENT_ID]->setFieldPerms($r->ID_ZASOB, $r->FORM_TREAT);
  824. $tableConfig['fields'];// $this->_fields
  825. $tableConfig['virtualFieldsIdList'];// $this->_virtualFieldsIdList
  826. //$tableConfig['types'];// $this->_types
  827. }
  828. }
  829. if (empty($tableConfig)) {
  830. throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
  831. }
  832. $obj = new TableAcl($idTable);
  833. $obj->fromArray($tableConfig);
  834. $obj->save();
  835. $_cache[$idTable] = $obj;
  836. return $_cache[$idTable];
  837. }
  838. public function loadSchema() {
  839. if ($this->_schemaLoaded) return;
  840. $srvName = $_SERVER['SERVER_NAME'];
  841. try {
  842. $this->_schemaClass = Schema_TableFactory::build($this->_name, $this->_db, $srvName);
  843. } catch (Exception $e) {
  844. DBG::_('DBG_SCH', '>1', "Exception in Schema_TableFactory::build", $e->getMessage(), __CLASS__, __FUNCTION__, __LINE__);
  845. // TODO: hide only not found, else re-throw
  846. }
  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. }