TableAcl.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 getName() {
  42. return $this->_name;
  43. }
  44. public function setOpis($opis) {
  45. $this->_opis = $opis;
  46. }
  47. public function getOpis() {
  48. return $this->_opis;
  49. }
  50. public function setLabel($label) {
  51. $this->_label = $label;
  52. }
  53. public function getLabel() {
  54. return $this->_label;
  55. }
  56. public function getRawLabel($posLimit = 20) {
  57. $label = $this->_label;
  58. if (empty($label) && !empty($this->_opis)) {
  59. $label = $this->_opis;
  60. if (mb_strlen($this->_opis) > $posLimit) {
  61. $pos = strpos($this->_opis, ' - ');
  62. if ($pos > $posLimit || $pos < 5) {
  63. $pos = $posLimit;
  64. $label = mb_substr($this->_opis, 0, $posLimit, 'utf-8') . '...';
  65. } else {
  66. $label = mb_substr($this->_opis, 0, $pos, 'utf-8');
  67. }
  68. }
  69. }
  70. if (empty($label)) {
  71. $label = $this->_name;
  72. }
  73. return $label;
  74. }
  75. public function getShortLabel($posLimit = 20) {
  76. $shortLabel = $this->getRawLabel($posLimit);
  77. $opis = $this->_opis;
  78. $shortLabel = '<span title="' . htmlspecialchars($opis) . '">' . $shortLabel . '</span>';
  79. return $shortLabel;
  80. }
  81. public function getLongLabel($posLimit = 30) {
  82. $longLabel = $this->getRawLabel($posLimit);
  83. $opis = $this->_opis;
  84. if ($longLabel != $this->_name) {
  85. $longLabel .= ' <em>' . $this->_name . '</em>';
  86. }
  87. $longLabel = '<span title="' . htmlspecialchars($opis) . '">' . $longLabel . '</span>';
  88. return $longLabel;
  89. }
  90. public function setDB($db) {
  91. $this->_db = $db;
  92. }
  93. public function getDB() {
  94. return $this->_db;
  95. }
  96. public function addField($fieldID, $name, $opis, $sort_prio, $label = '') {
  97. $field = array();
  98. $field['name'] = $name;
  99. $field['perms'] = '';
  100. $field['opis'] = $opis;
  101. $field['sort_prio'] = $sort_prio;
  102. $field['label'] = $label;
  103. $this->_fields[$fieldID] = $field;
  104. }
  105. public function getField($fieldID) {
  106. return $this->_fields[$fieldID];
  107. }
  108. public function hasField($fieldID) {
  109. return array_key_exists($fieldID, $this->_fields);
  110. }
  111. public function removeField($fieldID) {
  112. if (array_key_exists($fieldID, $this->_fields)) {
  113. unset($this->_fields[$fieldID]);
  114. }
  115. }
  116. public function getFields() {
  117. return $this->_fields;
  118. }
  119. public function setFieldPerms($fieldID, $perms) {
  120. if (array_key_exists($fieldID, $this->_fields)) {
  121. $this->_fields[$fieldID]['perms'] .= $perms;
  122. }
  123. }
  124. public function getFieldPerms($fieldID) {
  125. if (array_key_exists($fieldID, $this->_fields)) {
  126. $perms = V::get('perms', '', $this->_fields[$fieldID]);
  127. if ($perms) {
  128. return implode(',', array_unique(str_split($perms)));
  129. }
  130. }
  131. return '';
  132. }
  133. public function hasFieldPerm($fieldID, $perm) {
  134. if (array_key_exists($fieldID, $this->_fields)) {
  135. if (false !== strpos($this->_fields[$fieldID]['perms'], $perm)) {
  136. return true;
  137. }
  138. return false;
  139. }
  140. return false;
  141. }
  142. public function getFieldIdByName($fieldName) {
  143. $fieldID = 0;
  144. if (empty($fieldName)) {
  145. return;
  146. }
  147. foreach ($this->_fields as $kID => $vField) {
  148. if ($vField['name'] == $fieldName) {
  149. $fieldID = $kID;
  150. }
  151. }
  152. return $fieldID;
  153. }
  154. public function hasSuperAccessPerms() {
  155. foreach ($this->_fields as $kFldID => $vFld) {
  156. if ($this->hasFieldPerm($kFldID, 'S')) {
  157. return true;
  158. }
  159. else if ($this->hasFieldPerm($kFldID, 'V')) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. public function hasPermSuperWrite() {
  166. foreach ($this->_fields as $kFldID => $vFld) {
  167. if ($this->hasFieldPerm($kFldID, 'S')) {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. /**
  174. *
  175. */
  176. public function canWriteRecord($record) {
  177. $dbgArr = array();
  178. $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : '';
  179. $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : '';
  180. $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : '';
  181. $dbgArr['user_groups'] = User::getLdapGroupsNames();
  182. 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>';}
  183. if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
  184. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - is record owner</p>';}
  185. return true;
  186. }
  187. if ($dbgArr['record_write']) {
  188. if (in_array($dbgArr['record_write'], $dbgArr['user_groups'])) {
  189. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - has group write</p>';}
  190. return true;
  191. }
  192. } else {
  193. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - group write not set</p>';}
  194. return true;
  195. }
  196. return false;
  197. }
  198. public function canReadRecord($record) {
  199. $dbgArr = array();
  200. $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : '';
  201. $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : '';
  202. $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : '';
  203. $dbgArr['user_groups'] = User::getLdapGroupsNames();
  204. 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>';}
  205. if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
  206. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - is record owner</p>';}
  207. return true;
  208. }
  209. if ($dbgArr['record_read']) {
  210. if (in_array($dbgArr['record_read'], $dbgArr['user_groups'])) {
  211. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - has group read</p>';}
  212. return true;
  213. }
  214. } else {
  215. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - group read not set</p>';}
  216. return true;
  217. }
  218. return false;
  219. }
  220. /**
  221. * @param $taskPerm - 'C', 'W', 'R'
  222. */
  223. public function isAllowed($fieldID, $taskPerm, $record = null) {
  224. if (!in_array($taskPerm, array('C', 'W', 'R'))) {
  225. return false;
  226. }
  227. $adminFields = array();
  228. $adminFields[] = 'ID';
  229. $adminFields[] = 'A_RECORD_CREATE_DATE';
  230. $adminFields[] = 'A_RECORD_CREATE_AUTHOR';
  231. $adminFields[] = 'A_RECORD_UPDATE_DATE';
  232. $adminFields[] = 'A_RECORD_UPDATE_AUTHOR';
  233. $fieldName = $this->_fields[$fieldID]['name'];
  234. if ($taskPerm == 'R' && in_array($fieldName, $adminFields)) {
  235. return true;
  236. }
  237. // check perm: allow 'RS', 'WS' - can R/W field even if cant read record
  238. // check 'O' - can read field even if cant read field but can read record
  239. 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.')'
  240. ,'taskPerm'=>$taskPerm
  241. ,'canReadRecord'=>'"'.$this->canReadRecord($record).'"'
  242. ,'hasFieldPerm(O) || canWriteRecord'=>'"'.$this->hasFieldPerm($fieldID, 'O').'" || "'.$this->canReadRecord($record).'"'
  243. ,'hasFieldPerm(S)'=>'"'.$this->hasFieldPerm($fieldID, 'S').'"'
  244. ,'hasFieldPerm(V)'=>'"'.$this->hasFieldPerm($fieldID, 'V').'"'
  245. ));echo'</pre>'; }
  246. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  247. if ($taskPerm == 'R' && $this->hasFieldPerm($fieldID, 'V')) {
  248. return true;
  249. } else if ($taskPerm == 'R'
  250. && $record
  251. && $this->hasFieldPerm($fieldID, 'O')
  252. && ($this->canReadRecord($record) || $this->canWriteRecord($record))
  253. ) {
  254. return true;// 'WO' or 'CO'
  255. }
  256. return false;
  257. }
  258. // check 'R' - require can read record, or V - Super View
  259. if ($taskPerm == 'R') {
  260. if ($this->canReadRecord($record) || $this->hasFieldPerm($fieldID, 'V')) {
  261. return true;
  262. } else {
  263. return false;
  264. }
  265. }
  266. // 'C' and 'W' require colType
  267. $colType = $this->getFieldTypeById($fieldID);
  268. if (!$colType) {
  269. return false;
  270. }
  271. if ($taskPerm == 'W') {
  272. if ($record) {
  273. 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') . '))';}
  274. return ($this->canWriteRecord($record)|| $this->hasFieldPerm($fieldID, 'S'));
  275. }
  276. }
  277. return true;
  278. }
  279. /**
  280. * @param $taskPerm - 'C', 'W'
  281. */
  282. public function showFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  283. $out = '';
  284. if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
  285. if ($taskPerm == 'R') {
  286. $out .= 'Brak uprawnień do odczytu';
  287. }
  288. else if ($taskPerm == 'W') {
  289. $out .= 'Brak uprawnień do zapisu';
  290. } else {
  291. $out .= 'Brak uprawnień do tego pola (' . $taskPerm . ')';
  292. }
  293. return $out;
  294. }
  295. $colName = $this->_fields[$fieldID]['name'];
  296. if ($colName == 'ID') {
  297. return $out;
  298. }
  299. $colType = $this->getFieldTypeById($fieldID);
  300. if (!$colType) {
  301. $out .= 'Error - unknown type';
  302. return $out;
  303. }
  304. Lib::loadClass('Typespecial');
  305. $typeSpecial = Typespecial::getInstance($fieldID, $colName);
  306. $html = new stdClass();
  307. $html->_params = array();
  308. $html->tag = 'input';
  309. $html->cnt = '';
  310. $html->attrs = array();
  311. $html->attrs['id'] = $fName;
  312. $html->attrs['name'] = $fName;
  313. $html->attrs['type'] = 'text';
  314. $html->attrs['value'] = htmlspecialchars($fValue);
  315. if (isset($params['tabindex'])) {
  316. $html->attrs['tabindex'] = $params['tabindex'];
  317. }
  318. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  319. $html->attrs['disabled'] = 'disabled';
  320. }
  321. $maxGrid = V::get('maxGrid', 10, $params);
  322. /* Form input Relative sizing:
  323. <input class="input-mini" type="text" placeholder=".input-mini">
  324. <input class="input-small" type="text" placeholder=".input-small">
  325. <input class="input-medium" type="text" placeholder=".input-medium">
  326. <input class="input-large" type="text" placeholder=".input-large">
  327. <input class="input-xlarge" type="text" placeholder=".input-xlarge">
  328. <input class="input-xxlarge" type="text" placeholder=".input-xxlarge">
  329. */
  330. if (substr($colType['type'], 0, 3) == 'int'
  331. || substr($colType['type'], 0, 7) == 'tinyint'
  332. || substr($colType['type'], 0, 8) == 'smallint'
  333. ) {
  334. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  335. $html->attrs['type'] = 'number';
  336. $html->attrs['class'] []= 'input-small';
  337. }
  338. else if (substr($colType['type'], 0, 6) == 'double') {
  339. $html->attrs['type'] = 'text';
  340. $html->attrs['class'] []= 'input-small';
  341. }
  342. else if (substr($colType['type'], 0, 7) == 'decimal') {
  343. $html->attrs['type'] = 'text';
  344. $html->attrs['class'] []= 'input-small';
  345. }
  346. else if (substr($colType['type'], 0, 7) == 'varchar'
  347. || substr($colType['type'], 0, 4) == 'char'
  348. ) {
  349. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  350. $html->attrs['type'] = 'text';
  351. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  352. if ($maxLength > 0) {
  353. $html->attrs['maxlength'] = $maxLength;
  354. }
  355. $valLength = strlen($fValue);
  356. if (isset($params['widthClass'])) {
  357. if ($params['widthClass'] == 'inside-modal') {
  358. $html->attrs['style'] = 'width:98%;';
  359. } else {
  360. $html->attrs['style'] = 'width:98%;';
  361. }
  362. } else {
  363. if ($maxLength < 11) {
  364. $html->attrs['class'] []= 'span2';
  365. } else if ($maxLength < 31) {
  366. $html->attrs['class'] []= 'span5';
  367. } else if ($maxLength < 51) {
  368. $html->attrs['class'] []= (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  369. } else if ($maxLength < 101) {
  370. $html->attrs['class'] []= (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  371. } else {
  372. $html->attrs['class'] []= (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  373. }
  374. }
  375. }
  376. else if (substr($colType['type'], 0, 4) == 'date') {
  377. $testDatePicker = true;
  378. if ($testDatePicker) {
  379. $html->attrs['type'] = 'text';
  380. $html->_params[] = 'date';
  381. if (substr($colType['type'], 0, 8) == 'datetime') {
  382. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  383. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  384. $html->attrs['maxlength'] = 19;
  385. } else {
  386. $html->attrs['class'][] = 'se_type-date';// datepicker';
  387. $html->attrs['maxlength'] = 10;
  388. }
  389. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  390. $html->attrs['value'] = '';
  391. }
  392. } else {
  393. $html->attrs['type'] = 'date';
  394. }
  395. }
  396. else if (substr($colType['type'], 0, 4) == 'enum') {
  397. unset($html->attrs['type']);
  398. unset($html->attrs['value']);
  399. $html->tag = 'select';
  400. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  401. $selValue = $fValue;
  402. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  403. if ($taskPerm == 'C') {
  404. $selValue = $colType['default'];
  405. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  406. $selValue = $colType['default'];
  407. }
  408. }
  409. $html->cnt .= '<option value="">' . "" . '</option>';
  410. if (!empty($selValue) && !in_array($selValue, $values)) {
  411. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  412. }
  413. foreach ($values as $val) {
  414. $sel = ($selValue == $val)? ' selected="selected"' : '';
  415. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  416. }
  417. }
  418. else if (substr($colType['type'], 0, 4) == 'text'
  419. || substr($colType['type'], 0, 8) == 'tinytext'
  420. || substr($colType['type'], 0, 10) == 'mediumtext'
  421. || substr($colType['type'], 0, 8) == 'longtext'
  422. ) {
  423. $html->tag = 'textarea';
  424. $html->cnt = htmlspecialchars($fValue);
  425. if (isset($params['widthClass'])) {
  426. if ($params['widthClass'] == 'inside-modal') {
  427. $html->attrs['style'] = 'width:98%;';
  428. } else {
  429. $html->attrs['style'] = 'width:98%;';
  430. }
  431. } else {
  432. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  433. }
  434. $html->attrs['rows'] = '3';
  435. unset($html->attrs['type']);
  436. unset($html->attrs['value']);
  437. }
  438. else if ('polygon' == $colType['type']) {
  439. return '...';
  440. }
  441. else {
  442. echo'unknown Type "'.$colType['type'].'"';
  443. }
  444. $attrsOut = array();
  445. foreach ($html->attrs as $k => $v) {
  446. if (is_array($v)) $v = implode(' ', $v);
  447. $attrsOut[] = "{$k}=\"{$v}\"";
  448. }
  449. if (in_array($html->tag, array('select', 'textarea'))) {
  450. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  451. $out .= $html->cnt;
  452. $out .= '</' . $html->tag . '>';
  453. } else {
  454. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  455. }
  456. if (in_array('date', $html->_params)) {
  457. $out = '<div class="input-append">' . $out . '<span class="add-on">
  458. <i data-time-icon="icon-time" data-date-icon="icon-calendar" class="icon-calendar"></i>
  459. </span>
  460. </div>';
  461. }
  462. if (true == V::get('appendBack', '', $params)) {
  463. if ($html->tag == 'input' && $taskPerm == 'W') {
  464. $out = '<div class="input-append show-last-value">' . $out . '<button class="btn btn-appendBack" type="button" title="' . htmlspecialchars($fValue) . '"><i class="icon-arrow-left"></i> Cofnij</button></div>';
  465. }
  466. }
  467. if ($typeSpecial) {
  468. $tsParams = array();
  469. $tsValue = V::get('typespecialValue', '', $params);
  470. if (!empty($tsValue)) {
  471. $tsParams['typespecialValue'] = $tsValue;
  472. }
  473. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  474. }
  475. return $out;
  476. }
  477. /**
  478. * Get column object. Not initialize
  479. * @returns object - column instance if exists else null
  480. *
  481. * static
  482. */
  483. public static function getInstance($tableID) {
  484. static $_cache;
  485. if (!$_cache) $_cache = array();
  486. if (array_key_exists($tableID, $_cache)) {
  487. return $_cache[$tableID];
  488. }
  489. if (!empty($_SESSION['TableAcl_cache'][$tableID])) {
  490. $obj = new TableAcl($tableID);
  491. $obj->fromArray($_SESSION['TableAcl_cache'][$tableID]);
  492. $_cache[$tableID] = $obj;
  493. return $_cache[$tableID];
  494. }
  495. return null;
  496. }
  497. public function init($force = false) {
  498. if ($this->isInitialized() && $force == false) {
  499. return;
  500. }
  501. $dbID = $this->getDB();
  502. $tblName = $this->getName();
  503. $db = DB::getDB($dbID);
  504. if (!$db) {
  505. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  506. }
  507. $res = $db->query("show fields from `{$tblName}` ");
  508. while ($h = $db->fetch_row($res)) {
  509. $fieldName = $h[0];
  510. $fieldType = $h[1];
  511. $this->_types[$fieldName] = array('type'=>$h[1], 'null'=>('YES' == $h[2]), 'default'=>$h[4]);
  512. }
  513. uasort($this->_fields, array($this, 'sortFieldsCallback'));
  514. $this->_fixDateFields();
  515. $this->_sortEnumFields();
  516. $this->_fixProjectType();
  517. $fieldIds = array_keys($this->_fields);
  518. Lib::loadClass('Typespecial');
  519. $vColsIdList = Typespecial::initFields($fieldIds);
  520. if (!empty($vColsIdList)) {
  521. $this->_virtualFieldsIdList = $vColsIdList;
  522. }
  523. $this->save();
  524. }
  525. private function _fixProjectType() {
  526. $tblName = $this->getName();
  527. $fldName = 'M_DIST_TYPE';
  528. if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
  529. foreach ($this->_fields as $kFldId => $vFld) {
  530. if ($vFld['name'] == $fldName) {
  531. $sqlTypes = array();
  532. if (!empty($this->_types[$fldName])) {
  533. if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
  534. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
  535. }
  536. }
  537. if (!empty($sqlTypes)) {
  538. $allowedTypes = array();
  539. $db = DB::getDB();
  540. $sql = "select z.DESC
  541. from `CRM_LISTA_ZASOBOW` as z
  542. where z.`A_STATUS`='NORMAL'
  543. and z.`PARENT_ID`={$kFldId}
  544. order by z.`DESC` asc
  545. ";
  546. $res = $db->query($sql);
  547. while ($r = $db->fetch($res)) {
  548. if (in_array($r->DESC, $sqlTypes)) {
  549. $allowedTypes[] = $r->DESC;
  550. }
  551. }
  552. sort($allowedTypes);
  553. if (!empty($allowedTypes)) {
  554. $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
  555. }
  556. }
  557. }
  558. }
  559. }
  560. }
  561. private function _sortEnumFields() {
  562. foreach ($this->_fields as $kFldId => $vFld) {
  563. $type = $this->getFieldTypeById($kFldId);
  564. if (!empty($type['type'])) {
  565. if (substr($type['type'], 0, 4) == 'enum') {
  566. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  567. if (!empty($sqlTypes)) {
  568. sort($sqlTypes);
  569. $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
  570. }
  571. }
  572. }
  573. }
  574. }
  575. private function _fixDateFields() {
  576. foreach ($this->_types as $kFldName => $vType) {
  577. if ($kFldName == 'L_APPOITMENT_DATE') {
  578. $this->_types[$kFldName]['type'] = 'datetime';
  579. } else if ($kFldName == 'A_PROBLEM_DATE') {
  580. $this->_types[$kFldName]['type'] = 'datetime';
  581. }
  582. }
  583. }
  584. public function getUniqueKeys() {
  585. $sqlKeys = array();
  586. $dbID = $this->getDB();
  587. $tblName = $this->getName();
  588. $db = DB::getDB($dbID);
  589. if (!$db) {
  590. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  591. }
  592. $sql = "SHOW KEYS FROM `{$tblName}`";
  593. $res = $db->query($sql);
  594. while ($r = $db->fetch($res)) {
  595. if ($r->Non_unique == '0') {
  596. $sqlKeys[$r->Column_name] = true;
  597. }
  598. }
  599. $sqlKeys = array_keys($sqlKeys);
  600. return $sqlKeys;
  601. }
  602. public function sortFieldsCallback($a, $b) {
  603. if ($a['name'] == 'ID') {
  604. return -1;
  605. }
  606. else if ($b['name'] == 'ID') {
  607. return 1;
  608. }
  609. else if ($a['sort_prio'] < $b['sort_prio']) {
  610. return -1;
  611. }
  612. else if ($a['sort_prio'] > $b['sort_prio']) {
  613. return 1;
  614. }
  615. else {
  616. return 0;
  617. }
  618. }
  619. public function isInitialized() {
  620. return (!empty($this->_types));
  621. }
  622. /**
  623. * Save data in session cache.
  624. */
  625. function save() {
  626. $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
  627. }
  628. public function getFieldTypeById($fieldID) {
  629. if (!array_key_exists($fieldID, $this->_fields)) {
  630. return null;
  631. }
  632. $colName = $this->_fields[$fieldID]['name'];
  633. if (!array_key_exists($colName, $this->_types)) {
  634. return null;
  635. }
  636. return $this->_types[$colName];
  637. }
  638. public function getFieldType($colName) {
  639. if (!array_key_exists($colName, $this->_types)) {
  640. return null;
  641. }
  642. return $this->_types[$colName];
  643. }
  644. public function hasFieldType($colName) {
  645. if (array_key_exists($colName, $this->_types)) {
  646. return true;
  647. }
  648. return false;
  649. }
  650. public function getVisibleFieldList() {
  651. $cols = array();
  652. $id = 0;
  653. foreach ($this->_fields as $kFieldID => $vField) {
  654. if ($vField['name'] == 'ID') {
  655. $id = $kFieldID;
  656. }
  657. }
  658. $cols[$id] = 'ID';
  659. foreach ($this->_fields as $kFieldID => $vField) {
  660. if ($vField['name'] == 'ID') {
  661. continue;
  662. }
  663. $cols[$kFieldID] = $vField['name'];
  664. }
  665. return $cols;
  666. }
  667. public function getExportFieldList() {
  668. $cols = array();
  669. $realFlds = $this->getRealFieldList();
  670. foreach ($realFlds as $vFieldName) {
  671. $fldId = $this->getFieldIdByName($vFieldName);
  672. if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
  673. $cols[] = $vFieldName;
  674. }
  675. }
  676. return $cols;
  677. }
  678. /**
  679. * List of real fields in database.
  680. */
  681. public function getRealFieldList() {
  682. $cols = array();
  683. $cols[] = 'ID';
  684. foreach ($this->_fields as $kFieldID => $vField) {
  685. if ($vField['name'] == 'ID') {
  686. continue;
  687. }
  688. if (array_key_exists($vField['name'], $this->_types)) {
  689. $cols[] = $vField['name'];
  690. }
  691. }
  692. return $cols;
  693. }
  694. public function getVirtualFieldList() {
  695. $cols = array();
  696. foreach ($this->_fields as $kFieldID => $vField) {
  697. if ($vField['name'] == 'ID') {
  698. continue;
  699. }
  700. if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
  701. $cols[$kFieldID] = $vField['name'];
  702. }
  703. else if (!array_key_exists($vField['name'], $this->_types)) {
  704. $cols[$kFieldID] = $vField['name'];
  705. }
  706. }
  707. return $cols;
  708. }
  709. public function getFieldLabel($fieldID) {
  710. if (array_key_exists($fieldID, $this->_fields)) {
  711. if (!empty($this->_fields[$fieldID]['label'])) {
  712. return $this->_fields[$fieldID]['label'];
  713. }
  714. }
  715. return null;
  716. }
  717. public function getFieldOpis($fieldID) {
  718. if (array_key_exists($fieldID, $this->_fields)) {
  719. if (!empty($this->_fields[$fieldID]['opis'])) {
  720. return $this->_fields[$fieldID]['opis'];
  721. }
  722. }
  723. return null;
  724. }
  725. public function getTypes() {
  726. return $this->_types;
  727. }
  728. public function fixEmptyValueFromUser($fieldID) {
  729. $value = '';
  730. $type = $this->getFieldTypeById($fieldID);
  731. if ($type) {
  732. if ($type['type'] == 'date') {
  733. $value = $type['default'];
  734. }
  735. // fix bug when field is unique and is null allowed: change empty string to null
  736. if ($type['null']) {
  737. $value = 'NULL';
  738. }
  739. // fix bug when field is enum and is set to '0': for php '0' is empty
  740. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  741. if (false !== strpos($type['type'], "''")) {
  742. // enum('', '1','2')
  743. $value = '';
  744. } else if (false !== strpos($type['type'], "'0'")) {
  745. // enum('0', '1','2')
  746. $value = '0';
  747. } else {
  748. $value = $type['default'];
  749. }
  750. }
  751. }
  752. return $value;
  753. }
  754. public function fromArray($arr) {
  755. $this->_db = $arr['db'];
  756. $this->_name = $arr['name'];
  757. $this->_label = $arr['label'];
  758. $this->_opis = $arr['opis'];
  759. $this->_fields = $arr['fields'];
  760. $this->_virtualFieldsIdList = $arr['virtualFieldsIdList'];
  761. $this->_types = $arr['types'];
  762. }
  763. public function toArray() {
  764. $arr = array();
  765. $arr['db'] = $this->_db;
  766. $arr['name'] = $this->_name;
  767. $arr['label'] = $this->_label;
  768. $arr['opis'] = $this->_opis;
  769. $arr['fields'] = $this->_fields;
  770. $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
  771. $arr['types'] = $this->_types;
  772. return $arr;
  773. }
  774. public function getExportDataSource($cols = array()) {
  775. $exportFieldList = $this->getExportFieldList();
  776. if (!empty($cols)) {
  777. $fltrExportFlds = array();
  778. foreach ($exportFieldList as $fldName) {
  779. if (in_array($fldName, $cols)) {
  780. $fltrExportFlds[] = $fldName;
  781. }
  782. }
  783. $exportFieldList = $fltrExportFlds;
  784. }
  785. $dataSource = $this->_getDataSource($exportFieldList);
  786. return $dataSource;
  787. }
  788. public function getDataSource() {
  789. $realFieldList = $this->getRealFieldList();
  790. $dataSource = $this->_getDataSource($realFieldList);
  791. $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
  792. $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
  793. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  794. foreach ($adminFields as $vAdmFld) {
  795. if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
  796. $dataSource->addCol($vAdmFld);
  797. }
  798. }
  799. return $dataSource;
  800. }
  801. private function _getDataSource($cols) {
  802. Lib::loadClass('Data_Source');
  803. $dataSource = new Data_Source($this->getDB());
  804. $dataSource->setTable($this->getName());
  805. $dataSource->setCols($cols);
  806. $dataSource->setColTypes($this->getTypes());
  807. $dataSource->setVirtualCols($this->getVirtualFieldList());
  808. $dataSource->setAccessFltrAllowed(!$this->hasSuperAccessPerms());
  809. return $dataSource;
  810. }
  811. }