TableAcl.php 51 KB

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