TableAcl.php 40 KB

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