TableAcl.php 40 KB

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