TableAcl.php 51 KB

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