TableAcl.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  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. $permFields = $userAcl->getPermsForTable($this->_zasobID);
  605. //echo'<pre>INIT::$permFields('.$this->_zasobID.') fields('.count($this->_fields).') ';print_r($permFields);echo'</pre>';
  606. foreach ($permFields as $idField => $fld) {
  607. //echo'<pre>INIT::$permField('.$fld->ID_CELL.') hasFld('.$this->hasField($fld->ID_CELL).') ';echo'</pre>';
  608. if (!$this->hasField($fld->ID_CELL)) {
  609. //echo'<pre>INIT::$permField('.$fld->ID_CELL.') addFld('.$fld->ID_CELL . ', ' . $fld->CELL_NAME . ', ' . $fld->CELL_DESC . ', ' . $fld->SORT_PRIO . ', ' . $fld->CELL_LABEL.') ';echo'</pre>';
  610. $this->addField($fld->ID_CELL, $fld->CELL_NAME, $fld->CELL_DESC, $fld->SORT_PRIO, $fld->CELL_LABEL);
  611. }
  612. //echo'<pre>INIT::$permField('.$fld->ID_CELL.') hasFld('.$this->hasField($fld->ID_CELL).') ';echo'</pre>';
  613. {// TODO: convert to legacy perms
  614. $fld->FORM_TREAT = '';
  615. if ($fld->PERM_R > 0) $fld->FORM_TREAT .= 'R';
  616. if ($fld->PERM_W > 0) $fld->FORM_TREAT .= 'W';
  617. if ($fld->PERM_X > 0) $fld->FORM_TREAT .= 'X';
  618. if ($fld->PERM_C > 0) $fld->FORM_TREAT .= 'C';
  619. if ($fld->PERM_S > 0) $fld->FORM_TREAT .= 'S';
  620. if ($fld->PERM_O > 0) $fld->FORM_TREAT .= 'O';
  621. if ($fld->PERM_V > 0) $fld->FORM_TREAT .= 'V';
  622. if ($fld->PERM_E > 0) $fld->FORM_TREAT .= 'E';
  623. }
  624. //echo'<pre>INIT::$permField('.$fld->ID_CELL.') ';print_r($fld);echo'</pre>';
  625. if (!empty($fld->FORM_TREAT)) {
  626. $this->setFieldPerms($fld->ID_CELL, $fld->FORM_TREAT);
  627. }
  628. }
  629. //echo'<pre>INIT::$this('.$this->_zasobID.') fields('.count($this->_fields).') ';print_r($this);echo'</pre>';
  630. }
  631. if ($this->isInitialized() && $force == false) {
  632. return;
  633. }
  634. $ds = $this->getDataSource();
  635. $this->_types = $ds->getFieldTypes();
  636. uasort($this->_fields, array($this, 'sortFieldsCallback'));
  637. $this->_fixDateFields();
  638. $this->_sortEnumFields();
  639. $this->_fixProjectType();
  640. $fieldIds = array_keys($this->_fields);
  641. Lib::loadClass('Typespecial');
  642. $vColsIdList = Typespecial::initFields($fieldIds);
  643. if (!empty($vColsIdList)) {
  644. $this->_virtualFieldsIdList = $vColsIdList;
  645. }
  646. $this->save();
  647. }
  648. private function _fixProjectType() {
  649. $tblName = $this->getName();
  650. $fldName = 'M_DIST_TYPE';
  651. if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
  652. foreach ($this->_fields as $kFldId => $vFld) {
  653. if ($vFld['name'] == $fldName) {
  654. $sqlTypes = array();
  655. if (!empty($this->_types[$fldName])) {
  656. if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
  657. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
  658. }
  659. }
  660. if (!empty($sqlTypes)) {
  661. $allowedTypes = array();
  662. $db = DB::getDB();
  663. $sql = "select z.DESC
  664. from `CRM_LISTA_ZASOBOW` as z
  665. where z.`A_STATUS`='NORMAL'
  666. and z.`PARENT_ID`={$kFldId}
  667. order by z.`DESC` asc
  668. ";
  669. $res = $db->query($sql);
  670. while ($r = $db->fetch($res)) {
  671. if (in_array($r->DESC, $sqlTypes)) {
  672. $allowedTypes[] = $r->DESC;
  673. }
  674. }
  675. sort($allowedTypes);
  676. if (!empty($allowedTypes)) {
  677. $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
  678. }
  679. }
  680. }
  681. }
  682. }
  683. }
  684. private function _sortEnumFields() {
  685. foreach ($this->_fields as $kFldId => $vFld) {
  686. $type = $this->getFieldTypeById($kFldId);
  687. if (!empty($type['type'])) {
  688. if (substr($type['type'], 0, 4) == 'enum') {
  689. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  690. if (!empty($sqlTypes)) {
  691. sort($sqlTypes);
  692. $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
  693. }
  694. }
  695. }
  696. }
  697. }
  698. private function _fixDateFields() {
  699. foreach ($this->_types as $kFldName => $vType) {
  700. if ($kFldName == 'L_APPOITMENT_DATE') {
  701. $this->_types[$kFldName]['type'] = 'datetime';
  702. } else if ($kFldName == 'A_PROBLEM_DATE') {
  703. $this->_types[$kFldName]['type'] = 'datetime';
  704. }
  705. }
  706. }
  707. public function getUniqueKeys() {// TODO: RM NOT USED?
  708. $sqlKeys = array();
  709. $dbID = $this->getDB();
  710. $tblName = $this->getName();
  711. $db = DB::getDB($dbID);
  712. if (!$db) {
  713. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  714. }
  715. $sql = "SHOW KEYS FROM `{$tblName}`";
  716. $res = $db->query($sql);
  717. while ($r = $db->fetch($res)) {
  718. if ($r->Non_unique == '0') {
  719. $sqlKeys[$r->Column_name] = true;
  720. }
  721. }
  722. $sqlKeys = array_keys($sqlKeys);
  723. return $sqlKeys;
  724. }
  725. public function sortFieldsCallback($a, $b) {
  726. if ($a['name'] == 'ID') {
  727. return -1;
  728. }
  729. else if ($b['name'] == 'ID') {
  730. return 1;
  731. }
  732. else if ($a['sort_prio'] < $b['sort_prio']) {
  733. return -1;
  734. }
  735. else if ($a['sort_prio'] > $b['sort_prio']) {
  736. return 1;
  737. }
  738. else {
  739. return 0;
  740. }
  741. }
  742. public function isInitialized() {
  743. return (!empty($this->_types));
  744. }
  745. /**
  746. * Save data in session cache.
  747. */
  748. function save() {
  749. $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
  750. }
  751. public function getFieldTypeById($fieldID) {
  752. if (!array_key_exists($fieldID, $this->_fields)) {
  753. return null;
  754. }
  755. $colName = $this->_fields[$fieldID]['name'];
  756. if (!array_key_exists($colName, $this->_types)) {
  757. return null;
  758. }
  759. return $this->_types[$colName];
  760. }
  761. public function getFieldType($colName) {
  762. if (!array_key_exists($colName, $this->_types)) {
  763. return null;
  764. }
  765. return $this->_types[$colName];
  766. }
  767. public function hasFieldType($colName) {
  768. if (array_key_exists($colName, $this->_types)) {
  769. return true;
  770. }
  771. return false;
  772. }
  773. public function getVisibleFieldList() {
  774. $cols = array();
  775. $id = 0;
  776. foreach ($this->_fields as $kFieldID => $vField) {
  777. if ($vField['name'] == 'ID') {
  778. $id = $kFieldID;
  779. }
  780. }
  781. $cols[$id] = 'ID';
  782. foreach ($this->_fields as $kFieldID => $vField) {
  783. if ($vField['name'] == 'ID') {
  784. continue;
  785. }
  786. $cols[$kFieldID] = $vField['name'];
  787. }
  788. return $cols;
  789. }
  790. public function getExportFieldList() {
  791. $cols = array();
  792. $realFlds = $this->getRealFieldList();
  793. foreach ($realFlds as $vFieldName) {
  794. $fldId = $this->getFieldIdByName($vFieldName);
  795. if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
  796. $cols[] = $vFieldName;
  797. }
  798. }
  799. return $cols;
  800. }
  801. /**
  802. * List of real fields in database.
  803. */
  804. public function getRealFieldList() {
  805. $cols = array();
  806. $cols[] = 'ID';
  807. foreach ($this->_fields as $kFieldID => $vField) {
  808. if ($vField['name'] == 'ID') {
  809. continue;
  810. }
  811. if (array_key_exists($vField['name'], $this->_types)) {
  812. $cols[] = $vField['name'];
  813. }
  814. }
  815. return $cols;
  816. }
  817. public function getVirtualFieldList() {
  818. $cols = array();
  819. foreach ($this->_fields as $kFieldID => $vField) {
  820. if ($vField['name'] == 'ID') {
  821. continue;
  822. }
  823. if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
  824. $cols[$kFieldID] = $vField['name'];
  825. }
  826. else if (!array_key_exists($vField['name'], $this->_types)) {
  827. $cols[$kFieldID] = $vField['name'];
  828. }
  829. }
  830. return $cols;
  831. }
  832. public function getFieldLabel($fieldID) {
  833. if (array_key_exists($fieldID, $this->_fields)) {
  834. if (!empty($this->_fields[$fieldID]['label'])) {
  835. return $this->_fields[$fieldID]['label'];
  836. }
  837. }
  838. return null;
  839. }
  840. public function getFieldOpis($fieldID) {
  841. if (array_key_exists($fieldID, $this->_fields)) {
  842. if (!empty($this->_fields[$fieldID]['opis'])) {
  843. return $this->_fields[$fieldID]['opis'];
  844. }
  845. }
  846. return null;
  847. }
  848. public function getTypes() {
  849. return $this->_types;
  850. }
  851. public function fixEmptyValueFromUser($fieldID) {
  852. $value = '';
  853. $type = $this->getFieldTypeById($fieldID);
  854. if ($type) {
  855. if ($type['type'] == 'date') {
  856. $value = $type['default'];
  857. }
  858. if (substr($type['type'], 0, 3) == 'int'
  859. || substr($type['type'], 0, 7) == 'tinyint'
  860. || substr($type['type'], 0, 8) == 'smallint'
  861. || substr($type['type'], 0, 6) == 'bigint'
  862. ) {
  863. $value = intval($type['default']);
  864. }
  865. // fix bug when field is unique and is null allowed: change empty string to null
  866. if ($type['null']) {
  867. $value = 'NULL';
  868. }
  869. // fix bug when field is enum and is set to '0': for php '0' is empty
  870. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  871. if (false !== strpos($type['type'], "''")) {
  872. // enum('', '1','2')
  873. $value = '';
  874. } else if (false !== strpos($type['type'], "'0'")) {
  875. // enum('0', '1','2')
  876. $value = '0';
  877. } else {
  878. $value = $type['default'];
  879. }
  880. }
  881. }
  882. return $value;
  883. }
  884. public function fromArray($arr) {
  885. $this->_db = $arr['db'];
  886. $this->_name = $arr['name'];
  887. $this->_label = $arr['label'];
  888. $this->_opis = $arr['opis'];
  889. $this->_fields = V::get('fields', array(), $arr);
  890. $this->_virtualFieldsIdList = V::get('virtualFieldsIdList', array(), $arr);
  891. $this->_types = V::get('types', array(), $arr);
  892. }
  893. public function toArray() {
  894. $arr = array();
  895. $arr['db'] = $this->_db;
  896. $arr['name'] = $this->_name;
  897. $arr['label'] = $this->_label;
  898. $arr['opis'] = $this->_opis;
  899. $arr['fields'] = $this->_fields;
  900. $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
  901. $arr['types'] = $this->_types;
  902. return $arr;
  903. }
  904. public function convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f') {
  905. $item = array();
  906. $fields = $this->getFields();
  907. foreach ($fields as $kID => $vField) {
  908. $vFieldName = $vField['name'];
  909. if (array_key_exists("f{$kID}", $args)) {
  910. $value = $args["f{$kID}"];
  911. if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
  912. $value = $this->fixEmptyValueFromUser($kID);
  913. }
  914. $item[$vFieldName] = $value;
  915. }
  916. }
  917. return $item;
  918. }
  919. public function getItem($id) {
  920. $ds = $this->getDataSource();
  921. return $ds->getItem($id);
  922. }
  923. public function getItems($params) {
  924. $ds = $this->getDataSource();
  925. return $ds->getItems($params);
  926. }
  927. public function getTotal($params) {
  928. $ds = $this->getDataSource();
  929. return $ds->getTotal($params);
  930. }
  931. public function getColDefault($fieldName) {
  932. $ds = $this->getDataSource();
  933. return $ds->getColDefault($fieldName);
  934. }
  935. public function getSpecialFilters() {
  936. $ds = $this->getDataSource();
  937. return $ds->getSpecialFilters();
  938. }
  939. public function getGeomFields() {
  940. $ds = $this->getDataSource();
  941. return $ds->getGeomFields();
  942. }
  943. public function isGeomField($fldName) {
  944. $ds = $this->getDataSource();
  945. return $ds->isGeomField($fldName);
  946. }
  947. public function getHistItems($id) {
  948. $ds = $this->getDataSource();
  949. return $ds->getHistItems($id);
  950. }
  951. public function addItem($itemTodo) {
  952. if (is_object($itemTodo)) {
  953. $itemTodo = (array)$itemTodo;
  954. } else if (!is_array($itemTodo)) {
  955. throw new HttpException('Item is not array', 400);
  956. }
  957. if (empty($itemTodo)) {
  958. //throw new Exception('Item patch is empty');
  959. return 0;// nothing to insert
  960. }
  961. $ds = $this->getDataSource();
  962. // from convertObjectFromUserInput
  963. $item = array();
  964. $fields = $this->getFields();
  965. foreach ($fields as $kID => $vField) {
  966. $vFieldName = $vField['name'];
  967. if (!$this->isAllowed($kID, 'C')) {
  968. continue;
  969. }
  970. if (isset($itemTodo[$vFieldName])) {
  971. $value = $itemTodo[$vFieldName];
  972. if (empty($value) && strlen($value) == 0) {// fix bug in input type date and value="0000-00-00"
  973. $value = $this->fixEmptyValueFromUser($kID);
  974. }
  975. $item[$vFieldName] = $value;
  976. }
  977. }
  978. if (empty($item)) {
  979. throw new Exception("Nothing to add");
  980. }
  981. {// add DefaultAclGroup if no create perms ('C')
  982. $defaultAclGroup = User::getDefaultAclGroup();
  983. if ($defaultAclGroup) {
  984. foreach ($fields as $kID => $vField) {
  985. $vFieldName = $vField['name'];
  986. if (!$this->isAllowed($kID, 'C')) {
  987. if ($vFieldName == 'A_ADM_COMPANY') {
  988. $item[$vFieldName] = $defaultAclGroup;
  989. }
  990. else if ($vFieldName == 'A_CLASSIFIED') {
  991. $item[$vFieldName] = $defaultAclGroup;
  992. }
  993. }
  994. }
  995. }
  996. }
  997. return $ds->addItem($item);
  998. }
  999. /**
  1000. * @param array $itemPatch
  1001. */
  1002. public function updateItem($itemPatch) {
  1003. if (is_object($itemPatch)) {
  1004. $itemPatch = (array)$itemPatch;
  1005. } else if (!is_array($itemPatch)) {
  1006. throw new HttpException('Item patch is not array', 400);
  1007. }
  1008. if (empty($itemPatch)) {
  1009. //throw new Exception('Item patch is empty');
  1010. return 0;// nothing to change
  1011. }
  1012. $ds = $this->getDataSource();
  1013. $primaryKeyField = $ds->getPrimaryKeyField();
  1014. if (empty($itemPatch[$primaryKeyField])) {
  1015. throw new HttpException("Item Primary Key not set!", 400);
  1016. }
  1017. $primaryKey = $itemPatch[$primaryKeyField];
  1018. $itemOld = $this->getItem($primaryKey);
  1019. if (!$itemOld) {
  1020. throw new HttpException("Item not exists!", 404);
  1021. }
  1022. if (!$this->canWriteRecord($itemOld) && !$this->hasPermSuperWrite()) {
  1023. throw new HttpException("Brak dostępu do rekordu", 403);
  1024. }
  1025. // $itemPatch from user input to $itemPatchChecked
  1026. $itemPatchChecked = array();
  1027. $fields = $this->getFields();
  1028. foreach ($fields as $kID => $vField) {
  1029. $vFieldName = $vField['name'];
  1030. if (!$this->isAllowed($kID, 'W', $itemOld)) {
  1031. continue;
  1032. }
  1033. if (isset($itemPatch[$vFieldName])) {
  1034. if (!$this->isAllowed($kID, 'R', $itemOld) && '*****' == $itemPatch[$vFieldName]) {
  1035. // default value for perms 'W' without 'R' is '*****'
  1036. }
  1037. else {
  1038. $value = $itemPatch[$vFieldName];
  1039. if (empty($itemPatch[$vFieldName]) && strlen($itemPatch[$vFieldName]) == 0) {// fix bug in input type date and value="0000-00-00"
  1040. $value = $this->fixEmptyValueFromUser($kID);
  1041. }
  1042. if ($value != $itemOld->$vFieldName) {
  1043. $itemPatchChecked[$vFieldName] = $value;
  1044. }
  1045. }
  1046. }
  1047. }
  1048. if (empty($itemPatchChecked)) {
  1049. //throw new HttpException("Item Primary Key not set!", 400);
  1050. return 0;// nothing to change
  1051. }
  1052. $itemPatchChecked[$primaryKeyField] = $primaryKey;
  1053. $affected = $ds->updateItem($itemPatchChecked);
  1054. return $affected;
  1055. }
  1056. public function createItemCopy($item) {
  1057. $ds = $this->getDataSource();
  1058. $types = $this->getTypes();
  1059. $uniqKeys = $ds->getUniqueKeys();// TODO: getUniqueFields
  1060. $primaryKeyField = $ds->getPrimaryKeyField();
  1061. $itemCopy = new stdClass();
  1062. foreach ($types as $kName => $vType) {
  1063. if ($kName == $primaryKeyField) {
  1064. continue;
  1065. } else if (in_array($kName, array('A_RECORD_UPDATE_AUTHOR','A_RECORD_UPDATE_DATE'))) {
  1066. continue;
  1067. }
  1068. $value = V::get($kName, '', $item);
  1069. if (in_array($kName, $uniqKeys)) {
  1070. $value .= '?';
  1071. }
  1072. if ($ds->isGeomField($kName)) {
  1073. $value = "GeomFromText('{$value}')";
  1074. }
  1075. $itemCopy->{$kName} = $value;
  1076. }
  1077. return $itemCopy;
  1078. }
  1079. public function getExportDataSource($cols = array()) {
  1080. $exportFieldList = $this->getExportFieldList();
  1081. if (!empty($cols)) {
  1082. $fltrExportFlds = array();
  1083. foreach ($exportFieldList as $fldName) {
  1084. if (in_array($fldName, $cols)) {
  1085. $fltrExportFlds[] = $fldName;
  1086. }
  1087. }
  1088. $exportFieldList = $fltrExportFlds;
  1089. }
  1090. $dataSource = $this->_getDataSource($exportFieldList);
  1091. return $dataSource;
  1092. }
  1093. public function getDataSource() {
  1094. $realFieldList = $this->getRealFieldList();
  1095. $dataSource = $this->_getDataSource($realFieldList);
  1096. $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
  1097. $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
  1098. $dataSource->setFieldOwner('L_APPOITMENT_USER', $this->hasFieldType('L_APPOITMENT_USER'));
  1099. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  1100. foreach ($adminFields as $vAdmFld) {
  1101. if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
  1102. $dataSource->addCol($vAdmFld);
  1103. }
  1104. }
  1105. return $dataSource;
  1106. }
  1107. private function _getDataSource($cols) {
  1108. Lib::loadClass('DataSourceFactory');
  1109. $dsConfig = array();
  1110. $dsConfig['source_id'] = $this->getDB();
  1111. $dsConfig['object_name'] = $this->getName();
  1112. $dsConfig['fields'] = $cols;
  1113. $dsConfig['field_types'] = $this->getTypes();
  1114. $dsConfig['fields_virtual'] = $this->getVirtualFieldList();
  1115. $dsConfig['acl_fltr_allowed'] = !$this->hasSuperAccessPerms();
  1116. return DataSourceFactory::buildFromZasobInfo($dsConfig);
  1117. }
  1118. public function getPrimaryKeyField() {
  1119. $ds = $this->getDataSource();
  1120. return $ds->getPrimaryKeyField();
  1121. }
  1122. }