TableAcl.php 40 KB

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