TableAcl.php 33 KB

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