TableAcl.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. $dbID = $this->getDB();
  557. $tblName = $this->getName();
  558. $db = DB::getDB($dbID);
  559. if (!$db) {
  560. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  561. }
  562. $res = $db->query("show fields from `{$tblName}` ");
  563. while ($h = $db->fetch_row($res)) {
  564. $fieldName = $h[0];
  565. $fieldType = $h[1];
  566. $this->_types[$fieldName] = array('type'=>$h[1], 'null'=>('YES' == $h[2]), 'default'=>$h[4]);
  567. }
  568. uasort($this->_fields, array($this, 'sortFieldsCallback'));
  569. $this->_fixDateFields();
  570. $this->_sortEnumFields();
  571. $this->_fixProjectType();
  572. $fieldIds = array_keys($this->_fields);
  573. Lib::loadClass('Typespecial');
  574. $vColsIdList = Typespecial::initFields($fieldIds);
  575. if (!empty($vColsIdList)) {
  576. $this->_virtualFieldsIdList = $vColsIdList;
  577. }
  578. $this->save();
  579. }
  580. private function _fixProjectType() {
  581. $tblName = $this->getName();
  582. $fldName = 'M_DIST_TYPE';
  583. if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
  584. foreach ($this->_fields as $kFldId => $vFld) {
  585. if ($vFld['name'] == $fldName) {
  586. $sqlTypes = array();
  587. if (!empty($this->_types[$fldName])) {
  588. if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
  589. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
  590. }
  591. }
  592. if (!empty($sqlTypes)) {
  593. $allowedTypes = array();
  594. $db = DB::getDB();
  595. $sql = "select z.DESC
  596. from `CRM_LISTA_ZASOBOW` as z
  597. where z.`A_STATUS`='NORMAL'
  598. and z.`PARENT_ID`={$kFldId}
  599. order by z.`DESC` asc
  600. ";
  601. $res = $db->query($sql);
  602. while ($r = $db->fetch($res)) {
  603. if (in_array($r->DESC, $sqlTypes)) {
  604. $allowedTypes[] = $r->DESC;
  605. }
  606. }
  607. sort($allowedTypes);
  608. if (!empty($allowedTypes)) {
  609. $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
  610. }
  611. }
  612. }
  613. }
  614. }
  615. }
  616. private function _sortEnumFields() {
  617. foreach ($this->_fields as $kFldId => $vFld) {
  618. $type = $this->getFieldTypeById($kFldId);
  619. if (!empty($type['type'])) {
  620. if (substr($type['type'], 0, 4) == 'enum') {
  621. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  622. if (!empty($sqlTypes)) {
  623. sort($sqlTypes);
  624. $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
  625. }
  626. }
  627. }
  628. }
  629. }
  630. private function _fixDateFields() {
  631. foreach ($this->_types as $kFldName => $vType) {
  632. if ($kFldName == 'L_APPOITMENT_DATE') {
  633. $this->_types[$kFldName]['type'] = 'datetime';
  634. } else if ($kFldName == 'A_PROBLEM_DATE') {
  635. $this->_types[$kFldName]['type'] = 'datetime';
  636. }
  637. }
  638. }
  639. public function getUniqueKeys() {
  640. $sqlKeys = array();
  641. $dbID = $this->getDB();
  642. $tblName = $this->getName();
  643. $db = DB::getDB($dbID);
  644. if (!$db) {
  645. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  646. }
  647. $sql = "SHOW KEYS FROM `{$tblName}`";
  648. $res = $db->query($sql);
  649. while ($r = $db->fetch($res)) {
  650. if ($r->Non_unique == '0') {
  651. $sqlKeys[$r->Column_name] = true;
  652. }
  653. }
  654. $sqlKeys = array_keys($sqlKeys);
  655. return $sqlKeys;
  656. }
  657. public function sortFieldsCallback($a, $b) {
  658. if ($a['name'] == 'ID') {
  659. return -1;
  660. }
  661. else if ($b['name'] == 'ID') {
  662. return 1;
  663. }
  664. else if ($a['sort_prio'] < $b['sort_prio']) {
  665. return -1;
  666. }
  667. else if ($a['sort_prio'] > $b['sort_prio']) {
  668. return 1;
  669. }
  670. else {
  671. return 0;
  672. }
  673. }
  674. public function isInitialized() {
  675. return (!empty($this->_types));
  676. }
  677. /**
  678. * Save data in session cache.
  679. */
  680. function save() {
  681. $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
  682. }
  683. public function getFieldTypeById($fieldID) {
  684. if (!array_key_exists($fieldID, $this->_fields)) {
  685. return null;
  686. }
  687. $colName = $this->_fields[$fieldID]['name'];
  688. if (!array_key_exists($colName, $this->_types)) {
  689. return null;
  690. }
  691. return $this->_types[$colName];
  692. }
  693. public function getFieldType($colName) {
  694. if (!array_key_exists($colName, $this->_types)) {
  695. return null;
  696. }
  697. return $this->_types[$colName];
  698. }
  699. public function hasFieldType($colName) {
  700. if (array_key_exists($colName, $this->_types)) {
  701. return true;
  702. }
  703. return false;
  704. }
  705. public function getVisibleFieldList() {
  706. $cols = array();
  707. $id = 0;
  708. foreach ($this->_fields as $kFieldID => $vField) {
  709. if ($vField['name'] == 'ID') {
  710. $id = $kFieldID;
  711. }
  712. }
  713. $cols[$id] = 'ID';
  714. foreach ($this->_fields as $kFieldID => $vField) {
  715. if ($vField['name'] == 'ID') {
  716. continue;
  717. }
  718. $cols[$kFieldID] = $vField['name'];
  719. }
  720. return $cols;
  721. }
  722. public function getExportFieldList() {
  723. $cols = array();
  724. $realFlds = $this->getRealFieldList();
  725. foreach ($realFlds as $vFieldName) {
  726. $fldId = $this->getFieldIdByName($vFieldName);
  727. if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
  728. $cols[] = $vFieldName;
  729. }
  730. }
  731. return $cols;
  732. }
  733. /**
  734. * List of real fields in database.
  735. */
  736. public function getRealFieldList() {
  737. $cols = array();
  738. $cols[] = 'ID';
  739. foreach ($this->_fields as $kFieldID => $vField) {
  740. if ($vField['name'] == 'ID') {
  741. continue;
  742. }
  743. if (array_key_exists($vField['name'], $this->_types)) {
  744. $cols[] = $vField['name'];
  745. }
  746. }
  747. return $cols;
  748. }
  749. public function getVirtualFieldList() {
  750. $cols = array();
  751. foreach ($this->_fields as $kFieldID => $vField) {
  752. if ($vField['name'] == 'ID') {
  753. continue;
  754. }
  755. if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
  756. $cols[$kFieldID] = $vField['name'];
  757. }
  758. else if (!array_key_exists($vField['name'], $this->_types)) {
  759. $cols[$kFieldID] = $vField['name'];
  760. }
  761. }
  762. return $cols;
  763. }
  764. public function getFieldLabel($fieldID) {
  765. if (array_key_exists($fieldID, $this->_fields)) {
  766. if (!empty($this->_fields[$fieldID]['label'])) {
  767. return $this->_fields[$fieldID]['label'];
  768. }
  769. }
  770. return null;
  771. }
  772. public function getFieldOpis($fieldID) {
  773. if (array_key_exists($fieldID, $this->_fields)) {
  774. if (!empty($this->_fields[$fieldID]['opis'])) {
  775. return $this->_fields[$fieldID]['opis'];
  776. }
  777. }
  778. return null;
  779. }
  780. public function getTypes() {
  781. return $this->_types;
  782. }
  783. public function fixEmptyValueFromUser($fieldID) {
  784. $value = '';
  785. $type = $this->getFieldTypeById($fieldID);
  786. if ($type) {
  787. if ($type['type'] == 'date') {
  788. $value = $type['default'];
  789. }
  790. if (substr($type['type'], 0, 3) == 'int'
  791. || substr($type['type'], 0, 7) == 'tinyint'
  792. || substr($type['type'], 0, 8) == 'smallint'
  793. || substr($type['type'], 0, 6) == 'bigint'
  794. ) {
  795. $value = intval($type['default']);
  796. }
  797. // fix bug when field is unique and is null allowed: change empty string to null
  798. if ($type['null']) {
  799. $value = 'NULL';
  800. }
  801. // fix bug when field is enum and is set to '0': for php '0' is empty
  802. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  803. if (false !== strpos($type['type'], "''")) {
  804. // enum('', '1','2')
  805. $value = '';
  806. } else if (false !== strpos($type['type'], "'0'")) {
  807. // enum('0', '1','2')
  808. $value = '0';
  809. } else {
  810. $value = $type['default'];
  811. }
  812. }
  813. }
  814. return $value;
  815. }
  816. public function fromArray($arr) {
  817. $this->_db = $arr['db'];
  818. $this->_name = $arr['name'];
  819. $this->_label = $arr['label'];
  820. $this->_opis = $arr['opis'];
  821. $this->_fields = $arr['fields'];
  822. $this->_virtualFieldsIdList = $arr['virtualFieldsIdList'];
  823. $this->_types = $arr['types'];
  824. }
  825. public function toArray() {
  826. $arr = array();
  827. $arr['db'] = $this->_db;
  828. $arr['name'] = $this->_name;
  829. $arr['label'] = $this->_label;
  830. $arr['opis'] = $this->_opis;
  831. $arr['fields'] = $this->_fields;
  832. $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
  833. $arr['types'] = $this->_types;
  834. return $arr;
  835. }
  836. public function convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f') {
  837. $obj = new stdClass();
  838. $fields = $this->getFields();
  839. foreach ($fields as $kID => $vField) {
  840. if (!$this->isAllowed($kID, 'C')) {
  841. continue;
  842. }
  843. if (array_key_exists("f{$kID}", $args)) {
  844. $obj->{$vField['name']} = $args["f{$kID}"];
  845. if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
  846. $obj->{$vField['name']} = $this->fixEmptyValueFromUser($kID);
  847. }
  848. }
  849. }
  850. {// add DefaultAclGroup if no create perms ('C')
  851. $defaultAclGroup = User::getDefaultAclGroup();
  852. if ($defaultAclGroup) {
  853. foreach ($fields as $kID => $vField) {
  854. if (!$this->isAllowed($kID, 'C')) {
  855. if ($vField['name'] == 'A_ADM_COMPANY') {
  856. $obj->{$vField['name']} = $defaultAclGroup;
  857. }
  858. else if ($vField['name'] == 'A_CLASSIFIED') {
  859. $obj->{$vField['name']} = $defaultAclGroup;
  860. }
  861. }
  862. }
  863. }
  864. }
  865. return $obj;
  866. }
  867. public function getItem($id) {
  868. $ds = $this->getDataSource();
  869. return $ds->getItem($id);
  870. }
  871. public function getItems($params) {
  872. $ds = $this->getDataSource();
  873. return $ds->getItems($params);
  874. }
  875. public function getTotal($params) {
  876. $ds = $this->getDataSource();
  877. return $ds->getTotal($params);
  878. }
  879. public function getColDefault($fieldName) {
  880. $ds = $this->getDataSource();
  881. return $ds->getColDefault($fieldName);
  882. }
  883. public function getSpecialFilters() {
  884. $ds = $this->getDataSource();
  885. return $ds->getSpecialFilters();
  886. }
  887. public function getGeomFields() {
  888. $ds = $this->getDataSource();
  889. return $ds->getGeomFields();
  890. }
  891. public function isGeomField($fldName) {
  892. $ds = $this->getDataSource();
  893. return $ds->isGeomField($fldName);
  894. }
  895. public function getHistItems($id) {
  896. $ds = $this->getDataSource();
  897. return $ds->getHistItems($id);
  898. }
  899. public function addItem($item) {
  900. $ds = $this->getDataSource();
  901. return $ds->addItem($item);
  902. }
  903. public function updateItem($itemPatch) {
  904. $ds = $this->getDataSource();
  905. $affected = $ds->updateItem($itemPatch);
  906. if ($affected < 0) {
  907. $dsErrors = $ds->getDbErrors();
  908. $dsErrors = "Wystąpiły błędy!\n" . implode("\n", $dsErrors);
  909. throw new Exception($dsErrors);
  910. }
  911. return $affected;
  912. }
  913. public function createItemCopy($item) {
  914. $ds = $this->getDataSource();
  915. $types = $this->getTypes();
  916. $uniqKeys = $this->getUniqueKeys();// TODO: getUniqueFields
  917. $primaryKeyField = 'ID';// TODO: getPrimaryField
  918. $itemCopy = new stdClass();
  919. foreach ($types as $kName => $vType) {
  920. if ($kName == $primaryKeyField) {
  921. continue;
  922. } else if (in_array($kName, array('A_RECORD_UPDATE_AUTHOR','A_RECORD_UPDATE_DATE'))) {
  923. continue;
  924. }
  925. $value = V::get($kName, '', $item);
  926. if (in_array($kName, $uniqKeys)) {
  927. $value .= '?';
  928. }
  929. if ($ds->isGeomField($kName)) {
  930. $value = "GeomFromText('{$value}')";
  931. }
  932. $itemCopy->{$kName} = $value;
  933. }
  934. return $itemCopy;
  935. }
  936. public function getExportDataSource($cols = array()) {
  937. $exportFieldList = $this->getExportFieldList();
  938. if (!empty($cols)) {
  939. $fltrExportFlds = array();
  940. foreach ($exportFieldList as $fldName) {
  941. if (in_array($fldName, $cols)) {
  942. $fltrExportFlds[] = $fldName;
  943. }
  944. }
  945. $exportFieldList = $fltrExportFlds;
  946. }
  947. $dataSource = $this->_getDataSource($exportFieldList);
  948. return $dataSource;
  949. }
  950. public function getDataSource() {
  951. $realFieldList = $this->getRealFieldList();
  952. $dataSource = $this->_getDataSource($realFieldList);
  953. $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
  954. $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
  955. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  956. foreach ($adminFields as $vAdmFld) {
  957. if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
  958. $dataSource->addCol($vAdmFld);
  959. }
  960. }
  961. return $dataSource;
  962. }
  963. private function _getDataSource($cols) {
  964. Lib::loadClass('Data_Source');
  965. $dataSource = new Data_Source($this->getDB());
  966. $dataSource->setTable($this->getName());
  967. $dataSource->setCols($cols);
  968. $dataSource->setColTypes($this->getTypes());
  969. $dataSource->setVirtualCols($this->getVirtualFieldList());
  970. $dataSource->setAccessFltrAllowed(!$this->hasSuperAccessPerms());
  971. return $dataSource;
  972. }
  973. }