TableAcl.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. <?php
  2. /**
  3. * $_SESSION['TableAcl_cache'][$tableID] = array(
  4. * [db] => DB zasob ID
  5. * [name] => Table name
  6. * [opis] => Table opis
  7. * [fields] => array(
  8. * [$fieldID] => array(
  9. * [name] => name
  10. * [perms] => perms (FORM_TREAT)
  11. * [opis] => opis
  12. * )
  13. * )
  14. * [types] => array(
  15. * [$fieldID] => array(
  16. * [type] => type
  17. * [null] => bool
  18. * [default] => default value
  19. * )
  20. * )
  21. * );
  22. */
  23. class TableAcl {
  24. private $_zasobID = '';
  25. private $_db = '';
  26. private $_name = '';
  27. private $_label = '';
  28. private $_opis = '';
  29. private $_fields = array();
  30. private $_types = array();
  31. private $_virtualFieldsIdList = array();
  32. public function __construct($zasobID) {
  33. $this->_zasobID = $zasobID;
  34. }
  35. public function getID() {
  36. return $this->_zasobID;
  37. }
  38. public function setName($name) {
  39. $this->_name = $name;
  40. }
  41. public function setNameByTableId($tableID) {
  42. //used for init without knowing table name
  43. $sql="select `DESC` from CRM_LISTA_ZASOBOW where ID=".$tableID." and `TYPE`='TABELA'";
  44. $res=DB::query($sql);
  45. $res_=DB::fetch($res);
  46. //DEBUG_S(-3,'setNameByTableId',$res_,__FILE__,__FUNCTION__,__LINE__);
  47. self::setName($res->DESC);
  48. }
  49. public function getName() {
  50. return $this->_name;
  51. }
  52. public function setOpis($opis) {
  53. $this->_opis = $opis;
  54. }
  55. public function getOpis() {
  56. return $this->_opis;
  57. }
  58. public function setLabel($label) {
  59. $this->_label = $label;
  60. }
  61. public function getLabel() {
  62. return $this->_label;
  63. }
  64. public function getRawLabel($posLimit = 20) {
  65. $label = $this->_label;
  66. if (empty($label) && !empty($this->_opis)) {
  67. $label = $this->_opis;
  68. if (mb_strlen($this->_opis) > $posLimit) {
  69. $pos = strpos($this->_opis, ' - ');
  70. if ($pos > $posLimit || $pos < 5) {
  71. $pos = $posLimit;
  72. $label = mb_substr($this->_opis, 0, $posLimit, 'utf-8') . '...';
  73. } else {
  74. $label = mb_substr($this->_opis, 0, $pos, 'utf-8');
  75. }
  76. }
  77. }
  78. if (empty($label)) {
  79. $label = $this->_name;
  80. }
  81. return $label;
  82. }
  83. public function getShortLabel($posLimit = 20) {
  84. $shortLabel = $this->getRawLabel($posLimit);
  85. $opis = $this->_opis;
  86. $shortLabel = '<span title="' . htmlspecialchars($opis) . '">' . $shortLabel . '</span>';
  87. return $shortLabel;
  88. }
  89. public function getLongLabel($posLimit = 30) {
  90. $longLabel = $this->getRawLabel($posLimit);
  91. $opis = $this->_opis;
  92. if ($longLabel != $this->_name) {
  93. $longLabel .= ' <em>' . $this->_name . '</em>';
  94. }
  95. $longLabel = '<span title="' . htmlspecialchars($opis) . '">' . $longLabel . '</span>';
  96. return $longLabel;
  97. }
  98. public function setDB($db) {
  99. $this->_db = $db;
  100. }
  101. public function getDB() {
  102. return $this->_db;
  103. }
  104. public function addField($fieldID, $name, $opis, $sort_prio, $label = '') {
  105. $field = array();
  106. $field['name'] = $name;
  107. $field['perms'] = '';
  108. $field['opis'] = $opis;
  109. $field['sort_prio'] = $sort_prio;
  110. $field['label'] = $label;
  111. $this->_fields[$fieldID] = $field;
  112. }
  113. public function getTableDbId($tableID) {
  114. return $this->_db;
  115. }
  116. public function getField($fieldID) {
  117. return $this->_fields[$fieldID];
  118. }
  119. public function hasField($fieldID) {
  120. return array_key_exists($fieldID, $this->_fields);
  121. }
  122. public function removeField($fieldID) {
  123. if (array_key_exists($fieldID, $this->_fields)) {
  124. unset($this->_fields[$fieldID]);
  125. }
  126. }
  127. public function getFields() {
  128. return $this->_fields;
  129. }
  130. public function setFieldPerms($fieldID, $perms) {
  131. if (array_key_exists($fieldID, $this->_fields)) {
  132. $this->_fields[$fieldID]['perms'] .= $perms;
  133. }
  134. }
  135. public function getFieldPerms($fieldID) {
  136. if (array_key_exists($fieldID, $this->_fields)) {
  137. $perms = V::get('perms', '', $this->_fields[$fieldID]);
  138. if ($perms) {
  139. return implode(',', array_unique(str_split($perms)));
  140. }
  141. }
  142. return '';
  143. }
  144. public function hasFieldPerm($fieldID, $perm) {
  145. if (array_key_exists($fieldID, $this->_fields)) {
  146. if (false !== strpos($this->_fields[$fieldID]['perms'], $perm)) {
  147. return true;
  148. }
  149. return false;
  150. }
  151. return false;
  152. }
  153. public function getFieldIdByName($fieldName) {
  154. $fieldID = 0;
  155. if (empty($fieldName)) {
  156. return;
  157. }
  158. foreach ($this->_fields as $kID => $vField) {
  159. if ($vField['name'] == $fieldName) {
  160. $fieldID = $kID;
  161. }
  162. }
  163. return $fieldID;
  164. }
  165. public function hasEditPerms() {
  166. foreach ($this->_fields as $kFldID => $vFld) {
  167. if ($this->hasFieldPerm($kFldID, 'W')) return true;
  168. if ($this->hasFieldPerm($kFldID, 'C')) return true;
  169. if ($this->hasFieldPerm($kFldID, 'S')) return true;
  170. }
  171. return false;
  172. }
  173. public function hasSuperAccessPerms() {
  174. foreach ($this->_fields as $kFldID => $vFld) {
  175. if ($this->hasFieldPerm($kFldID, 'S')) {
  176. return true;
  177. }
  178. else if ($this->hasFieldPerm($kFldID, 'V')) {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. public function hasPermSuperWrite() {
  185. foreach ($this->_fields as $kFldID => $vFld) {
  186. if ($this->hasFieldPerm($kFldID, 'S')) {
  187. return true;
  188. }
  189. }
  190. return false;
  191. }
  192. /**
  193. *
  194. */
  195. public function canWriteRecord($record) {
  196. $dbgArr = array();
  197. $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : '';
  198. $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : '';
  199. $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : '';
  200. $dbgArr['user_groups'] = User::getLdapGroupsNames();
  201. 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>';}
  202. if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
  203. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - is record owner</p>';}
  204. return true;
  205. }
  206. if ($dbgArr['record_write']) {
  207. if (in_array($dbgArr['record_write'], $dbgArr['user_groups'])) {
  208. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - has group write</p>';}
  209. return true;
  210. }
  211. } else {
  212. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - group write not set</p>';}
  213. return true;
  214. }
  215. return false;
  216. }
  217. public function canReadRecord($record) {
  218. $dbgArr = array();
  219. $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : '';
  220. $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : '';
  221. $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : '';
  222. $dbgArr['user_groups'] = User::getLdapGroupsNames();
  223. 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>';}
  224. if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
  225. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - is record owner</p>';}
  226. return true;
  227. }
  228. if ($dbgArr['record_read']) {
  229. if (in_array($dbgArr['record_read'], $dbgArr['user_groups'])) {
  230. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - has group read</p>';}
  231. return true;
  232. }
  233. } else {
  234. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - group read not set</p>';}
  235. return true;
  236. }
  237. return false;
  238. }
  239. /**
  240. * @param $taskPerm - 'C', 'W', 'R'
  241. */
  242. public function isAllowed($fieldID, $taskPerm, $record = null) {
  243. if (!in_array($taskPerm, array('C', 'W', 'R'))) {
  244. return false;
  245. }
  246. $adminFields = array();
  247. $adminFields[] = 'ID';
  248. $adminFields[] = 'A_RECORD_CREATE_DATE';
  249. $adminFields[] = 'A_RECORD_CREATE_AUTHOR';
  250. $adminFields[] = 'A_RECORD_UPDATE_DATE';
  251. $adminFields[] = 'A_RECORD_UPDATE_AUTHOR';
  252. $fieldName = $this->_fields[$fieldID]['name'];
  253. if ($taskPerm == 'R' && in_array($fieldName, $adminFields)) {
  254. return true;
  255. }
  256. // check perm: allow 'RS', 'WS' - can R/W field even if cant read record
  257. // check 'O' - can read field even if cant read field but can read record
  258. 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.')'
  259. ,'taskPerm'=>$taskPerm
  260. ,'canReadRecord'=>'"'.$this->canReadRecord($record).'"'
  261. ,'hasFieldPerm(O) || canWriteRecord'=>'"'.$this->hasFieldPerm($fieldID, 'O').'" || "'.$this->canReadRecord($record).'"'
  262. ,'hasFieldPerm(S)'=>'"'.$this->hasFieldPerm($fieldID, 'S').'"'
  263. ,'hasFieldPerm(V)'=>'"'.$this->hasFieldPerm($fieldID, 'V').'"'
  264. ));echo'</pre>'; }
  265. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  266. if ($taskPerm == 'R' && $this->hasFieldPerm($fieldID, 'V')) {
  267. return true;
  268. } else if ($taskPerm == 'R'
  269. && $record
  270. && $this->hasFieldPerm($fieldID, 'O')
  271. && ($this->canReadRecord($record) || $this->canWriteRecord($record))
  272. ) {
  273. return true;// 'WO' or 'CO'
  274. }
  275. return false;
  276. }
  277. // check 'R' - require can read record, or V - Super View
  278. if ($taskPerm == 'R') {
  279. if ($this->canReadRecord($record) || $this->hasFieldPerm($fieldID, 'V')) {
  280. return true;
  281. } else {
  282. return false;
  283. }
  284. }
  285. // 'C' and 'W' require colType
  286. $colType = $this->getFieldTypeById($fieldID);
  287. if (!$colType) {
  288. return false;
  289. }
  290. if ($taskPerm == 'W') {
  291. if ($record) {
  292. 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') . '))';}
  293. return ($this->canWriteRecord($record)|| $this->hasFieldPerm($fieldID, 'S'));
  294. }
  295. }
  296. return true;
  297. }
  298. /**
  299. * @param $taskPerm - 'C', 'W'
  300. */
  301. public function showFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  302. $out = '';
  303. if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
  304. if ($taskPerm == 'R') {
  305. $out .= 'Brak uprawnień do odczytu';
  306. }
  307. else if ($taskPerm == 'W') {
  308. $out .= 'Brak uprawnień do zapisu';
  309. } else {
  310. $out .= 'Brak uprawnień do tego pola (' . $taskPerm . ')';
  311. }
  312. return $out;
  313. }
  314. $colName = $this->_fields[$fieldID]['name'];
  315. if ($colName == 'ID') {
  316. return $out;
  317. }
  318. $colType = $this->getFieldTypeById($fieldID);
  319. if (!$colType) {
  320. $out .= 'Error - unknown type';
  321. return $out;
  322. }
  323. Lib::loadClass('Typespecial');
  324. $typeSpecial = Typespecial::getInstance($fieldID, $colName);
  325. $html = new stdClass();
  326. $html->_params = array();
  327. $html->tag = 'input';
  328. $html->cnt = '';
  329. $html->attrs = array();
  330. $html->attrs['id'] = $fName;
  331. $html->attrs['name'] = $fName;
  332. $html->attrs['type'] = 'text';
  333. $html->attrs['value'] = htmlspecialchars($fValue);
  334. if (isset($params['tabindex'])) {
  335. $html->attrs['tabindex'] = $params['tabindex'];
  336. }
  337. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  338. $html->attrs['disabled'] = 'disabled';
  339. }
  340. $maxGrid = V::get('maxGrid', 10, $params);
  341. if (substr($colType['type'], 0, 3) == 'int'
  342. || substr($colType['type'], 0, 7) == 'tinyint'
  343. || substr($colType['type'], 0, 8) == 'smallint'
  344. || substr($colType['type'], 0, 6) == 'bigint'
  345. ) {
  346. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  347. $html->attrs['type'] = 'number';
  348. $html->attrs['class'][] = 'input-small';
  349. }
  350. else if (substr($colType['type'], 0, 6) == 'double') {
  351. $html->attrs['type'] = 'text';
  352. $html->attrs['class'][] = 'input-small';
  353. }
  354. else if (substr($colType['type'], 0, 7) == 'decimal') {
  355. $html->attrs['type'] = 'text';
  356. $html->attrs['class'][] = 'input-small';
  357. }
  358. else if (substr($colType['type'], 0, 7) == 'varchar'
  359. || substr($colType['type'], 0, 4) == 'char'
  360. ) {
  361. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  362. $html->attrs['type'] = 'text';
  363. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  364. if ($maxLength > 0) {
  365. $html->attrs['maxlength'] = $maxLength;
  366. }
  367. $valLength = strlen($fValue);
  368. if (isset($params['widthClass'])) {
  369. if ($params['widthClass'] == 'inside-modal') {
  370. $html->attrs['style'] = 'width:98%;';
  371. } else {
  372. $html->attrs['style'] = 'width:98%;';
  373. }
  374. } else {
  375. /*
  376. if ($maxLength < 11) {
  377. $html->attrs['class'][] = 'span2';
  378. } else if ($maxLength < 31) {
  379. $html->attrs['class'][] = 'span5';
  380. } else if ($maxLength < 51) {
  381. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  382. } else if ($maxLength < 101) {
  383. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  384. } else {
  385. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  386. }
  387. */
  388. }
  389. }
  390. else if (substr($colType['type'], 0, 4) == 'date') {
  391. $testDatePicker = true;
  392. if ($testDatePicker) {
  393. $html->attrs['type'] = 'text';
  394. $html->_params[] = 'date';
  395. if (substr($colType['type'], 0, 8) == 'datetime') {
  396. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  397. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  398. $html->attrs['maxlength'] = 19;
  399. } else {
  400. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  401. $html->attrs['maxlength'] = 10;
  402. }
  403. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  404. $html->attrs['value'] = '';
  405. }
  406. } else {
  407. $html->attrs['type'] = 'date';
  408. }
  409. }
  410. else if ($colType['type'] == 'time') {
  411. $testDatePicker = true;
  412. if ($testDatePicker) {
  413. $html->attrs['type'] = 'text';
  414. $html->_params[] = 'time';
  415. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  416. $html->attrs['data-format'] = 'hh:mm:ss';
  417. $html->attrs['maxlength'] = 8;
  418. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  419. $html->attrs['value'] = '';
  420. }
  421. } else {
  422. $html->attrs['type'] = 'time';
  423. }
  424. }
  425. else if (substr($colType['type'], 0, 4) == 'enum') {
  426. unset($html->attrs['type']);
  427. unset($html->attrs['value']);
  428. $html->tag = 'select';
  429. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  430. $selValue = $fValue;
  431. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  432. if ($taskPerm == 'C') {
  433. $selValue = $colType['default'];
  434. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  435. $selValue = $colType['default'];
  436. }
  437. }
  438. $html->cnt .= '<option value="">' . "" . '</option>';
  439. if (!empty($selValue) && !in_array($selValue, $values)) {
  440. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  441. }
  442. foreach ($values as $val) {
  443. $sel = ($selValue == $val)? ' selected="selected"' : '';
  444. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  445. }
  446. }
  447. else if (substr($colType['type'], 0, 4) == 'text'
  448. || substr($colType['type'], 0, 8) == 'tinytext'
  449. || substr($colType['type'], 0, 10) == 'mediumtext'
  450. || substr($colType['type'], 0, 8) == 'longtext'
  451. ) {
  452. $html->tag = 'textarea';
  453. $html->cnt = htmlspecialchars($fValue);
  454. if (isset($params['widthClass'])) {
  455. if ($params['widthClass'] == 'inside-modal') {
  456. $html->attrs['style'] = 'width:98%;';
  457. } else {
  458. $html->attrs['style'] = 'width:98%;';
  459. }
  460. } else {
  461. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  462. }
  463. $html->attrs['rows'] = '3';
  464. unset($html->attrs['type']);
  465. unset($html->attrs['value']);
  466. }
  467. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  468. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  469. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  470. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  471. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  472. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  473. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  474. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  475. else {
  476. return 'unknown Type "'.$colType['type'].'"';
  477. }
  478. $html->attrs['class'][] = 'form-control';
  479. $attrsOut = array();
  480. foreach ($html->attrs as $k => $v) {
  481. if (is_array($v)) $v = implode(' ', $v);
  482. $attrsOut[] = "{$k}=\"{$v}\"";
  483. }
  484. if (in_array($html->tag, array('select', 'textarea'))) {
  485. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  486. $out .= $html->cnt;
  487. $out .= '</' . $html->tag . '>';
  488. } else {
  489. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  490. }
  491. if (in_array('date', $html->_params)) {
  492. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  493. <span class="glyphicon glyphicon-calendar"></span>
  494. </span>
  495. </div>';
  496. }
  497. else if (in_array('time', $html->_params)) {
  498. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  499. <span class="glyphicon glyphicon-time"></span>
  500. </span>
  501. </div>';
  502. }
  503. if (true == V::get('appendBack', '', $params)
  504. && !in_array('date', $html->_params)
  505. && !in_array('time', $html->_params)
  506. ) {
  507. if ($html->tag == 'input' && $taskPerm == 'W') {
  508. $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>';
  509. }
  510. }
  511. if ($typeSpecial) {
  512. $tsParams = array();
  513. $tsValue = V::get('typespecialValue', '', $params);
  514. if (!empty($tsValue)) {
  515. $tsParams['typespecialValue'] = $tsValue;
  516. }
  517. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  518. }
  519. return $out;
  520. }
  521. /**
  522. * List table ids by database
  523. *
  524. *
  525. */
  526. public static function GetTablesByDbId($db) {
  527. DEBUG_S(3,'TableAcl_cache',$_SESSION['TableAcl_cache'],__FILE__,__FUNCTION__,__LINE__);
  528. static $_cache;
  529. $return=array();
  530. if (!$_cache) $_cache = array();
  531. if (!empty($_SESSION['TableAcl_cache'])) {
  532. foreach($_SESSION['TableAcl_cache'] as $tableID=>$obj) {
  533. //if($obj->db==$db)
  534. $return[$obj['name']]=$tableID;
  535. }
  536. return $return;
  537. }
  538. return null;
  539. }
  540. /**
  541. * Get column object. Not initialize
  542. * @returns object - column instance if exists else null
  543. *
  544. * static
  545. */
  546. public static function getInstance($idTable) {
  547. static $_cache;
  548. if (!$_cache) $_cache = array();
  549. if (array_key_exists($idTable, $_cache)) {
  550. return $_cache[$idTable];
  551. }
  552. if (!empty($_SESSION['TableAcl_cache'][$idTable])) {
  553. $tableAcl = new TableAcl($idTable);
  554. $tableAcl->fromArray($_SESSION['TableAcl_cache'][$idTable]);
  555. $_cache[$idTable] = $tableAcl;
  556. return $_cache[$idTable];
  557. }
  558. return null;
  559. }
  560. public static function buildInstance($idTable, $tableConfig) {
  561. static $_cache;
  562. if (!$_cache) $_cache = array();
  563. if (array_key_exists($idTable, $_cache)) {
  564. return $_cache[$idTable];
  565. }
  566. if (empty($tableConfig)) {
  567. throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
  568. Lib::loadClass('ProcesHelper');
  569. $zasobObj = ProcesHelper::getZasobTableInfo($idTable);
  570. if (!$zasobObj) {
  571. return null;// TODO: throw new Exception("Zasob TABELA ID={$idTable} nie istnieje");
  572. }
  573. $tableConfig['db'] = $zasobObj->P__ID;
  574. $tableConfig['name'] = $zasobObj->DESC;
  575. $tableConfig['label'] = $zasobObj->DESC_PL;
  576. $tableConfig['opis'] = $zasobObj->OPIS;
  577. $userAcl = User::getAcl();
  578. $userPermsForTable = $userAcl->getPermsForTable($idTable);
  579. if (!$userPermsForTable) {
  580. return null;// TODO: throw new Exception("Brak uprawnień do pól Tabeli nr {$idTable} '{$zasobObj->DESC}'");
  581. }
  582. echo'<pre>$userPermsForTable('.$idTable.') ';print_r($userPermsForTable);echo'</pre>';
  583. if(0){// TODO: from UserAcl big query
  584. $foundTbls[$r->ZASOB_PARENT_ID]->addField($r->ID_ZASOB, $r->ZASOB_DESC, $r->ZASOB_OPIS, $r->z__SORT_PRIO, $r->ZASOB_DESC_PL);
  585. $foundTbls[$r->ZASOB_PARENT_ID]->setFieldPerms($r->ID_ZASOB, $r->FORM_TREAT);
  586. $tableConfig['fields'];// $this->_fields
  587. $tableConfig['virtualFieldsIdList'];// $this->_virtualFieldsIdList
  588. //$tableConfig['types'];// $this->_types
  589. }
  590. }
  591. if (empty($tableConfig)) {
  592. throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
  593. }
  594. $obj = new TableAcl($idTable);
  595. $obj->fromArray($tableConfig);
  596. $obj->save();
  597. $_cache[$idTable] = $obj;
  598. return $_cache[$idTable];
  599. }
  600. public function init($force = false) {
  601. if (empty($this->_fields)) {
  602. $this->_types = array();// clear _types @see $this->isInitialized
  603. $userAcl = User::getAcl();
  604. $fieldsConfig = $userAcl->getPermsForTable($this->_zasobID);
  605. DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__ );
  606. $this->initFieldsFromConfig($fieldsConfig);
  607. //DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $this, __CLASS__, __FUNCTION__, __LINE__ );
  608. }
  609. if ($this->isInitialized() && $force == false) {
  610. return;
  611. }
  612. $ds = $this->getDataSource();
  613. $this->_types = $ds->getFieldTypes();
  614. uasort($this->_fields, array($this, 'sortFieldsCallback'));
  615. $this->_fixDateFields();
  616. $this->_sortEnumFields();
  617. $this->_fixProjectType();
  618. $fieldIds = array_keys($this->_fields);
  619. Lib::loadClass('Typespecial');
  620. $vColsIdList = Typespecial::initFields($fieldIds);
  621. if (!empty($vColsIdList)) {
  622. $this->_virtualFieldsIdList = $vColsIdList;
  623. }
  624. $this->save();
  625. }
  626. public function initFieldsFromConfig($fieldsConfig) {
  627. foreach ($fieldsConfig as $idField => $vFieldConfig) {
  628. if ((int)$idField <= 0) {
  629. DBG::_('DBG_SCH', '1', "BUG key must be integer - skipping '{$idField}'", $vFieldConfig, __CLASS__, __FUNCTION__, __LINE__ );
  630. trigger_error("BUG " . __CLASS__ . "->" . __FUNCTION__ . "(\$fieldsConfig) key must be integer - skipping '{$idField}'", E_USER_NOTICE);
  631. continue;
  632. }
  633. //echo'<pre>INIT::$permField('.$vFieldConfig->ID_CELL.') hasFld('.$this->hasField($vFieldConfig->ID_CELL).') ';echo'</pre>';
  634. if (!$this->hasField($vFieldConfig['ID_CELL'])) {
  635. //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>';
  636. $this->addField($vFieldConfig['ID_CELL'], $vFieldConfig['CELL_NAME'], $vFieldConfig['CELL_DESC'], $vFieldConfig['SORT_PRIO'], $vFieldConfig['CELL_LABEL']);
  637. }
  638. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') hasFld('.$this->hasField($vFieldConfig['ID_CELL']).') ';echo'</pre>';
  639. if (!isset($vFieldConfig['FORM_TREAT'])) {// TODO: convert to legacy perms
  640. $vFieldConfig['FORM_TREAT'] = '';
  641. if ($vFieldConfig['PERM_R'] > 0) $vFieldConfig['FORM_TREAT'] .= 'R';
  642. if ($vFieldConfig['PERM_W'] > 0) $vFieldConfig['FORM_TREAT'] .= 'W';
  643. if ($vFieldConfig['PERM_X'] > 0) $vFieldConfig['FORM_TREAT'] .= 'X';
  644. if ($vFieldConfig['PERM_C'] > 0) $vFieldConfig['FORM_TREAT'] .= 'C';
  645. if ($vFieldConfig['PERM_S'] > 0) $vFieldConfig['FORM_TREAT'] .= 'S';
  646. if ($vFieldConfig['PERM_O'] > 0) $vFieldConfig['FORM_TREAT'] .= 'O';
  647. if ($vFieldConfig['PERM_V'] > 0) $vFieldConfig['FORM_TREAT'] .= 'V';
  648. if ($vFieldConfig['PERM_E'] > 0) $vFieldConfig['FORM_TREAT'] .= 'E';
  649. }
  650. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') ';print_r($vFieldConfig);echo'</pre>';
  651. if (!empty($vFieldConfig['FORM_TREAT'])) {
  652. $this->setFieldPerms($vFieldConfig['ID_CELL'], $vFieldConfig['FORM_TREAT']);
  653. }
  654. }
  655. }
  656. private function _fixProjectType() {
  657. $tblName = $this->getName();
  658. $fldName = 'M_DIST_TYPE';
  659. if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
  660. foreach ($this->_fields as $kFldId => $vFld) {
  661. if ($vFld['name'] == $fldName) {
  662. $sqlTypes = array();
  663. if (!empty($this->_types[$fldName])) {
  664. if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
  665. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
  666. }
  667. }
  668. if (!empty($sqlTypes)) {
  669. $allowedTypes = array();
  670. $db = DB::getDB();
  671. $sql = "select z.DESC
  672. from `CRM_LISTA_ZASOBOW` as z
  673. where z.`A_STATUS`='NORMAL'
  674. and z.`PARENT_ID`={$kFldId}
  675. order by z.`DESC` asc
  676. ";
  677. $res = $db->query($sql);
  678. while ($r = $db->fetch($res)) {
  679. if (in_array($r->DESC, $sqlTypes)) {
  680. $allowedTypes[] = $r->DESC;
  681. }
  682. }
  683. sort($allowedTypes);
  684. if (!empty($allowedTypes)) {
  685. $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
  686. }
  687. }
  688. }
  689. }
  690. }
  691. }
  692. private function _sortEnumFields() {
  693. foreach ($this->_fields as $kFldId => $vFld) {
  694. $type = $this->getFieldTypeById($kFldId);
  695. if (!empty($type['type'])) {
  696. if (substr($type['type'], 0, 4) == 'enum') {
  697. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  698. if (!empty($sqlTypes)) {
  699. sort($sqlTypes);
  700. $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
  701. }
  702. }
  703. }
  704. }
  705. }
  706. private function _fixDateFields() {
  707. foreach ($this->_types as $kFldName => $vType) {
  708. if ($kFldName == 'L_APPOITMENT_DATE') {
  709. $this->_types[$kFldName]['type'] = 'datetime';
  710. } else if ($kFldName == 'A_PROBLEM_DATE') {
  711. $this->_types[$kFldName]['type'] = 'datetime';
  712. }
  713. }
  714. }
  715. public function getUniqueKeys() {// TODO: RM NOT USED?
  716. $sqlKeys = array();
  717. $dbID = $this->getDB();
  718. $tblName = $this->getName();
  719. $db = DB::getDB($dbID);
  720. if (!$db) {
  721. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  722. }
  723. $sql = "SHOW KEYS FROM `{$tblName}`";
  724. $res = $db->query($sql);
  725. while ($r = $db->fetch($res)) {
  726. if ($r->Non_unique == '0') {
  727. $sqlKeys[$r->Column_name] = true;
  728. }
  729. }
  730. $sqlKeys = array_keys($sqlKeys);
  731. return $sqlKeys;
  732. }
  733. public function sortFieldsCallback($a, $b) {
  734. if ($a['name'] == 'ID') {
  735. return -1;
  736. }
  737. else if ($b['name'] == 'ID') {
  738. return 1;
  739. }
  740. else if ($a['sort_prio'] < $b['sort_prio']) {
  741. return -1;
  742. }
  743. else if ($a['sort_prio'] > $b['sort_prio']) {
  744. return 1;
  745. }
  746. else {
  747. return 0;
  748. }
  749. }
  750. public function isInitialized() {
  751. return (!empty($this->_types));
  752. }
  753. /**
  754. * Save data in session cache.
  755. */
  756. function save() {
  757. $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
  758. }
  759. public function getFieldTypeById($fieldID) {
  760. if (!array_key_exists($fieldID, $this->_fields)) {
  761. return null;
  762. }
  763. $colName = $this->_fields[$fieldID]['name'];
  764. if (!array_key_exists($colName, $this->_types)) {
  765. return null;
  766. }
  767. return $this->_types[$colName];
  768. }
  769. public function getFieldType($colName) {
  770. if (!array_key_exists($colName, $this->_types)) {
  771. return null;
  772. }
  773. return $this->_types[$colName];
  774. }
  775. public function hasFieldType($colName) {
  776. if (array_key_exists($colName, $this->_types)) {
  777. return true;
  778. }
  779. return false;
  780. }
  781. public function getVisibleFieldList() {
  782. $cols = array();
  783. $id = 0;
  784. foreach ($this->_fields as $kFieldID => $vField) {
  785. if ($vField['name'] == 'ID') {
  786. $id = $kFieldID;
  787. }
  788. }
  789. $cols[$id] = 'ID';
  790. foreach ($this->_fields as $kFieldID => $vField) {
  791. if ($vField['name'] == 'ID') {
  792. continue;
  793. }
  794. $cols[$kFieldID] = $vField['name'];
  795. }
  796. return $cols;
  797. }
  798. public function getExportFieldList() {
  799. $cols = array();
  800. $realFlds = $this->getRealFieldList();
  801. foreach ($realFlds as $vFieldName) {
  802. $fldId = $this->getFieldIdByName($vFieldName);
  803. if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
  804. $cols[] = $vFieldName;
  805. }
  806. }
  807. return $cols;
  808. }
  809. /**
  810. * List of real fields in database.
  811. */
  812. public function getRealFieldList() {
  813. $cols = array();
  814. $cols[] = 'ID';
  815. foreach ($this->_fields as $kFieldID => $vField) {
  816. if ($vField['name'] == 'ID') {
  817. continue;
  818. }
  819. if (array_key_exists($vField['name'], $this->_types)) {
  820. $cols[] = $vField['name'];
  821. }
  822. }
  823. return $cols;
  824. }
  825. public function getVirtualFieldList() {
  826. $cols = array();
  827. foreach ($this->_fields as $kFieldID => $vField) {
  828. if ($vField['name'] == 'ID') {
  829. continue;
  830. }
  831. if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
  832. $cols[$kFieldID] = $vField['name'];
  833. }
  834. else if (!array_key_exists($vField['name'], $this->_types)) {
  835. $cols[$kFieldID] = $vField['name'];
  836. }
  837. }
  838. return $cols;
  839. }
  840. public function getFieldLabel($fieldID) {
  841. if (array_key_exists($fieldID, $this->_fields)) {
  842. if (!empty($this->_fields[$fieldID]['label'])) {
  843. return $this->_fields[$fieldID]['label'];
  844. }
  845. }
  846. return null;
  847. }
  848. public function getFieldOpis($fieldID) {
  849. if (array_key_exists($fieldID, $this->_fields)) {
  850. if (!empty($this->_fields[$fieldID]['opis'])) {
  851. return $this->_fields[$fieldID]['opis'];
  852. }
  853. }
  854. return null;
  855. }
  856. public function getTypes() {
  857. return $this->_types;
  858. }
  859. public function fixEmptyValueFromUser($fieldID) {
  860. $value = '';
  861. $type = $this->getFieldTypeById($fieldID);
  862. if ($type) {
  863. if ($type['type'] == 'date') {
  864. $value = $type['default'];
  865. }
  866. if (substr($type['type'], 0, 3) == 'int'
  867. || substr($type['type'], 0, 7) == 'tinyint'
  868. || substr($type['type'], 0, 8) == 'smallint'
  869. || substr($type['type'], 0, 6) == 'bigint'
  870. ) {
  871. $value = intval($type['default']);
  872. }
  873. // fix bug when field is unique and is null allowed: change empty string to null
  874. if ($type['null']) {
  875. $value = 'NULL';
  876. }
  877. // fix bug when field is enum and is set to '0': for php '0' is empty
  878. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  879. if (false !== strpos($type['type'], "''")) {
  880. // enum('', '1','2')
  881. $value = '';
  882. } else if (false !== strpos($type['type'], "'0'")) {
  883. // enum('0', '1','2')
  884. $value = '0';
  885. } else {
  886. $value = $type['default'];
  887. }
  888. }
  889. }
  890. return $value;
  891. }
  892. public function fromArray($arr) {
  893. $this->_db = $arr['db'];
  894. $this->_name = $arr['name'];
  895. $this->_label = $arr['label'];
  896. $this->_opis = $arr['opis'];
  897. $this->_fields = V::get('fields', array(), $arr);
  898. $this->_virtualFieldsIdList = V::get('virtualFieldsIdList', array(), $arr);
  899. $this->_types = V::get('types', array(), $arr);
  900. }
  901. public function toArray() {
  902. $arr = array();
  903. $arr['db'] = $this->_db;
  904. $arr['name'] = $this->_name;
  905. $arr['label'] = $this->_label;
  906. $arr['opis'] = $this->_opis;
  907. $arr['fields'] = $this->_fields;
  908. $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
  909. $arr['types'] = $this->_types;
  910. return $arr;
  911. }
  912. public function convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f') {
  913. $item = array();
  914. $fields = $this->getFields();
  915. foreach ($fields as $kID => $vField) {
  916. $vFieldName = $vField['name'];
  917. if (array_key_exists("f{$kID}", $args)) {
  918. $value = $args["f{$kID}"];
  919. if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
  920. $value = $this->fixEmptyValueFromUser($kID);
  921. }
  922. $item[$vFieldName] = $value;
  923. }
  924. }
  925. return $item;
  926. }
  927. public function getItem($id) {
  928. $ds = $this->getDataSource();
  929. return $ds->getItem($id);
  930. }
  931. public function getItems($params) {
  932. $ds = $this->getDataSource();
  933. return $ds->getItems($params);
  934. }
  935. public function getTotal($params) {
  936. $ds = $this->getDataSource();
  937. return $ds->getTotal($params);
  938. }
  939. public function getColDefault($fieldName) {
  940. $ds = $this->getDataSource();
  941. return $ds->getColDefault($fieldName);
  942. }
  943. public function getSpecialFilters() {
  944. $ds = $this->getDataSource();
  945. return $ds->getSpecialFilters();
  946. }
  947. public function getGeomFields() {
  948. $ds = $this->getDataSource();
  949. return $ds->getGeomFields();
  950. }
  951. public function isGeomField($fldName) {
  952. $ds = $this->getDataSource();
  953. return $ds->isGeomField($fldName);
  954. }
  955. public function getHistItems($id) {
  956. $ds = $this->getDataSource();
  957. return $ds->getHistItems($id);
  958. }
  959. public function addItem($itemTodo) {
  960. if (is_object($itemTodo)) {
  961. $itemTodo = (array)$itemTodo;
  962. } else if (!is_array($itemTodo)) {
  963. throw new HttpException('Item is not array', 400);
  964. }
  965. if (empty($itemTodo)) {
  966. //throw new Exception('Item patch is empty');
  967. return 0;// nothing to insert
  968. }
  969. $ds = $this->getDataSource();
  970. // from convertObjectFromUserInput
  971. $item = array();
  972. $fields = $this->getFields();
  973. foreach ($fields as $kID => $vField) {
  974. $vFieldName = $vField['name'];
  975. if (!$this->isAllowed($kID, 'C')) {
  976. continue;
  977. }
  978. if (isset($itemTodo[$vFieldName])) {
  979. $value = $itemTodo[$vFieldName];
  980. if (empty($value) && strlen($value) == 0) {// fix bug in input type date and value="0000-00-00"
  981. $value = $this->fixEmptyValueFromUser($kID);
  982. }
  983. $item[$vFieldName] = $value;
  984. }
  985. }
  986. if (empty($item)) {
  987. throw new Exception("Nothing to add");
  988. }
  989. {// add DefaultAclGroup if no create perms ('C')
  990. $defaultAclGroup = User::getDefaultAclGroup();
  991. if ($defaultAclGroup) {
  992. foreach ($fields as $kID => $vField) {
  993. $vFieldName = $vField['name'];
  994. if (!$this->isAllowed($kID, 'C')) {
  995. if ($vFieldName == 'A_ADM_COMPANY') {
  996. $item[$vFieldName] = $defaultAclGroup;
  997. }
  998. else if ($vFieldName == 'A_CLASSIFIED') {
  999. $item[$vFieldName] = $defaultAclGroup;
  1000. }
  1001. }
  1002. }
  1003. }
  1004. }
  1005. return $ds->addItem($item);
  1006. }
  1007. /**
  1008. * @param array $itemPatch
  1009. */
  1010. public function updateItem($itemPatch) {
  1011. if (is_object($itemPatch)) {
  1012. $itemPatch = (array)$itemPatch;
  1013. } else if (!is_array($itemPatch)) {
  1014. throw new HttpException('Item patch is not array', 400);
  1015. }
  1016. if (empty($itemPatch)) {
  1017. //throw new Exception('Item patch is empty');
  1018. return 0;// nothing to change
  1019. }
  1020. $ds = $this->getDataSource();
  1021. $primaryKeyField = $ds->getPrimaryKeyField();
  1022. if (empty($itemPatch[$primaryKeyField])) {
  1023. throw new HttpException("Item Primary Key not set!", 400);
  1024. }
  1025. $primaryKey = $itemPatch[$primaryKeyField];
  1026. $itemOld = $this->getItem($primaryKey);
  1027. if (!$itemOld) {
  1028. throw new HttpException("Item not exists!", 404);
  1029. }
  1030. if (!$this->canWriteRecord($itemOld) && !$this->hasPermSuperWrite()) {
  1031. throw new HttpException("Brak dostępu do rekordu", 403);
  1032. }
  1033. // $itemPatch from user input to $itemPatchChecked
  1034. $itemPatchChecked = array();
  1035. $fields = $this->getFields();
  1036. foreach ($fields as $kID => $vField) {
  1037. $vFieldName = $vField['name'];
  1038. if (!$this->isAllowed($kID, 'W', $itemOld)) {
  1039. continue;
  1040. }
  1041. if (isset($itemPatch[$vFieldName])) {
  1042. if (!$this->isAllowed($kID, 'R', $itemOld) && '*****' == $itemPatch[$vFieldName]) {
  1043. // default value for perms 'W' without 'R' is '*****'
  1044. }
  1045. else {
  1046. $value = $itemPatch[$vFieldName];
  1047. if (empty($itemPatch[$vFieldName]) && strlen($itemPatch[$vFieldName]) == 0) {// fix bug in input type date and value="0000-00-00"
  1048. $value = $this->fixEmptyValueFromUser($kID);
  1049. }
  1050. if ($value != $itemOld->$vFieldName) {
  1051. $itemPatchChecked[$vFieldName] = $value;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. if (empty($itemPatchChecked)) {
  1057. //throw new HttpException("Item Primary Key not set!", 400);
  1058. return 0;// nothing to change
  1059. }
  1060. $itemPatchChecked[$primaryKeyField] = $primaryKey;
  1061. $affected = $ds->updateItem($itemPatchChecked);
  1062. return $affected;
  1063. }
  1064. public function createItemCopy($item) {
  1065. $ds = $this->getDataSource();
  1066. $types = $this->getTypes();
  1067. $uniqKeys = $ds->getUniqueKeys();// TODO: getUniqueFields
  1068. $primaryKeyField = $ds->getPrimaryKeyField();
  1069. $itemCopy = new stdClass();
  1070. foreach ($types as $kName => $vType) {
  1071. if ($kName == $primaryKeyField) {
  1072. continue;
  1073. } else if (in_array($kName, array('A_RECORD_UPDATE_AUTHOR','A_RECORD_UPDATE_DATE'))) {
  1074. continue;
  1075. }
  1076. $value = V::get($kName, '', $item);
  1077. if (in_array($kName, $uniqKeys)) {
  1078. $value .= '?';
  1079. }
  1080. if ($ds->isGeomField($kName)) {
  1081. $value = "GeomFromText('{$value}')";
  1082. }
  1083. $itemCopy->{$kName} = $value;
  1084. }
  1085. return $itemCopy;
  1086. }
  1087. public function getExportDataSource($cols = array()) {
  1088. $exportFieldList = $this->getExportFieldList();
  1089. if (!empty($cols)) {
  1090. $fltrExportFlds = array();
  1091. foreach ($exportFieldList as $fldName) {
  1092. if (in_array($fldName, $cols)) {
  1093. $fltrExportFlds[] = $fldName;
  1094. }
  1095. }
  1096. $exportFieldList = $fltrExportFlds;
  1097. }
  1098. $dataSource = $this->_getDataSource($exportFieldList);
  1099. return $dataSource;
  1100. }
  1101. public function getDataSource() {
  1102. $realFieldList = $this->getRealFieldList();
  1103. $dataSource = $this->_getDataSource($realFieldList);
  1104. $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
  1105. $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
  1106. $dataSource->setFieldOwner('L_APPOITMENT_USER', $this->hasFieldType('L_APPOITMENT_USER'));
  1107. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  1108. foreach ($adminFields as $vAdmFld) {
  1109. if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
  1110. $dataSource->addCol($vAdmFld);
  1111. }
  1112. }
  1113. return $dataSource;
  1114. }
  1115. private function _getDataSource($cols) {
  1116. Lib::loadClass('DataSourceFactory');
  1117. $dsConfig = array();
  1118. $dsConfig['source_id'] = $this->getDB();
  1119. $dsConfig['object_name'] = $this->getName();
  1120. $dsConfig['fields'] = $cols;
  1121. $dsConfig['field_types'] = $this->getTypes();
  1122. $dsConfig['fields_virtual'] = $this->getVirtualFieldList();
  1123. $dsConfig['acl_fltr_allowed'] = !$this->hasSuperAccessPerms();
  1124. return DataSourceFactory::buildFromZasobInfo($dsConfig);
  1125. }
  1126. public function getPrimaryKeyField() {
  1127. $ds = $this->getDataSource();
  1128. return $ds->getPrimaryKeyField();
  1129. }
  1130. }