TableAcl.php 40 KB

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