TableAcl.php 51 KB

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