TableAcl.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. $html->tag = 'textarea';
  420. $html->cnt = htmlspecialchars($fValue);
  421. if (isset($params['widthClass'])) {
  422. if ($params['widthClass'] == 'inside-modal') {
  423. $html->attrs['style'] = 'width:98%;';
  424. } else {
  425. $html->attrs['style'] = 'width:98%;';
  426. }
  427. } else {
  428. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  429. }
  430. $html->attrs['rows'] = '3';
  431. unset($html->attrs['type']);
  432. unset($html->attrs['value']);
  433. }
  434. else if ('polygon' == $colType['type']) {
  435. return '...';
  436. }
  437. else {
  438. echo'unknown Type "'.$colType['type'].'"';
  439. }
  440. $attrsOut = array();
  441. foreach ($html->attrs as $k => $v) {
  442. if (is_array($v)) $v = implode(' ', $v);
  443. $attrsOut[] = "{$k}=\"{$v}\"";
  444. }
  445. if (in_array($html->tag, array('select', 'textarea'))) {
  446. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  447. $out .= $html->cnt;
  448. $out .= '</' . $html->tag . '>';
  449. } else {
  450. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  451. }
  452. if (in_array('date', $html->_params)) {
  453. $out = '<div class="input-append">' . $out . '<span class="add-on">
  454. <i data-time-icon="icon-time" data-date-icon="icon-calendar" class="icon-calendar"></i>
  455. </span>
  456. </div>';
  457. }
  458. if (true == V::get('appendBack', '', $params)) {
  459. if ($html->tag == 'input' && $taskPerm == 'W') {
  460. $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>';
  461. }
  462. }
  463. if ($typeSpecial) {
  464. $tsParams = array();
  465. $tsValue = V::get('typespecialValue', '', $params);
  466. if (!empty($tsValue)) {
  467. $tsParams['typespecialValue'] = $tsValue;
  468. }
  469. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  470. }
  471. return $out;
  472. }
  473. /**
  474. * Get column object. Not initialize
  475. * @returns object - column instance if exists else null
  476. *
  477. * static
  478. */
  479. public static function getInstance($tableID) {
  480. static $_cache;
  481. if (!$_cache) $_cache = array();
  482. if (array_key_exists($tableID, $_cache)) {
  483. return $_cache[$tableID];
  484. }
  485. if (!empty($_SESSION['TableAcl_cache'][$tableID])) {
  486. $obj = new TableAcl($tableID);
  487. $obj->fromArray($_SESSION['TableAcl_cache'][$tableID]);
  488. $_cache[$tableID] = $obj;
  489. return $_cache[$tableID];
  490. }
  491. return null;
  492. }
  493. public function init($force = false) {
  494. if ($this->isInitialized() && $force == false) {
  495. return;
  496. }
  497. $dbID = $this->getDB();
  498. $tblName = $this->getName();
  499. $db = DB::getDB($dbID);
  500. if (!$db) {
  501. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  502. }
  503. $res = $db->query("show fields from `{$tblName}` ");
  504. while ($h = $db->fetch_row($res)) {
  505. $fieldName = $h[0];
  506. $fieldType = $h[1];
  507. $this->_types[$fieldName] = array('type'=>$h[1], 'null'=>('YES' == $h[2]), 'default'=>$h[4]);
  508. }
  509. uasort($this->_fields, array($this, 'sortFieldsCallback'));
  510. $this->_fixDateFields();
  511. $this->_sortEnumFields();
  512. $this->_fixProjectType();
  513. $fieldIds = array_keys($this->_fields);
  514. Lib::loadClass('Typespecial');
  515. $vColsIdList = Typespecial::initFields($fieldIds);
  516. if (!empty($vColsIdList)) {
  517. $this->_virtualFieldsIdList = $vColsIdList;
  518. }
  519. $this->save();
  520. }
  521. private function _fixProjectType() {
  522. $tblName = $this->getName();
  523. $fldName = 'M_DIST_TYPE';
  524. if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
  525. foreach ($this->_fields as $kFldId => $vFld) {
  526. if ($vFld['name'] == $fldName) {
  527. $sqlTypes = array();
  528. if (!empty($this->_types[$fldName])) {
  529. if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
  530. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
  531. }
  532. }
  533. if (!empty($sqlTypes)) {
  534. $allowedTypes = array();
  535. $db = DB::getDB();
  536. $sql = "select z.DESC
  537. from `CRM_LISTA_ZASOBOW` as z
  538. where z.`A_STATUS`='NORMAL'
  539. and z.`PARENT_ID`={$kFldId}
  540. order by z.`DESC` asc
  541. ";
  542. $res = $db->query($sql);
  543. while ($r = $db->fetch($res)) {
  544. if (in_array($r->DESC, $sqlTypes)) {
  545. $allowedTypes[] = $r->DESC;
  546. }
  547. }
  548. sort($allowedTypes);
  549. if (!empty($allowedTypes)) {
  550. $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
  551. }
  552. }
  553. }
  554. }
  555. }
  556. }
  557. private function _sortEnumFields() {
  558. foreach ($this->_fields as $kFldId => $vFld) {
  559. $type = $this->getFieldTypeById($kFldId);
  560. if (!empty($type['type'])) {
  561. if (substr($type['type'], 0, 4) == 'enum') {
  562. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  563. if (!empty($sqlTypes)) {
  564. sort($sqlTypes);
  565. $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
  566. }
  567. }
  568. }
  569. }
  570. }
  571. private function _fixDateFields() {
  572. foreach ($this->_types as $kFldName => $vType) {
  573. if ($kFldName == 'L_APPOITMENT_DATE') {
  574. $this->_types[$kFldName]['type'] = 'datetime';
  575. } else if ($kFldName == 'A_PROBLEM_DATE') {
  576. $this->_types[$kFldName]['type'] = 'datetime';
  577. }
  578. }
  579. }
  580. public function getUniqueKeys() {
  581. $sqlKeys = array();
  582. $dbID = $this->getDB();
  583. $tblName = $this->getName();
  584. $db = DB::getDB($dbID);
  585. if (!$db) {
  586. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  587. }
  588. $sql = "SHOW KEYS FROM `{$tblName}`";
  589. $res = $db->query($sql);
  590. while ($r = $db->fetch($res)) {
  591. if ($r->Non_unique == '0') {
  592. $sqlKeys[$r->Column_name] = true;
  593. }
  594. }
  595. $sqlKeys = array_keys($sqlKeys);
  596. return $sqlKeys;
  597. }
  598. public function sortFieldsCallback($a, $b) {
  599. if ($a['name'] == 'ID') {
  600. return -1;
  601. }
  602. else if ($b['name'] == 'ID') {
  603. return 1;
  604. }
  605. else if ($a['sort_prio'] < $b['sort_prio']) {
  606. return -1;
  607. }
  608. else if ($a['sort_prio'] > $b['sort_prio']) {
  609. return 1;
  610. }
  611. else {
  612. return 0;
  613. }
  614. }
  615. public function isInitialized() {
  616. return (!empty($this->_types));
  617. }
  618. /**
  619. * Save data in session cache.
  620. */
  621. function save() {
  622. $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
  623. }
  624. public function getFieldTypeById($fieldID) {
  625. if (!array_key_exists($fieldID, $this->_fields)) {
  626. return null;
  627. }
  628. $colName = $this->_fields[$fieldID]['name'];
  629. if (!array_key_exists($colName, $this->_types)) {
  630. return null;
  631. }
  632. return $this->_types[$colName];
  633. }
  634. public function getFieldType($colName) {
  635. if (!array_key_exists($colName, $this->_types)) {
  636. return null;
  637. }
  638. return $this->_types[$colName];
  639. }
  640. public function hasFieldType($colName) {
  641. if (array_key_exists($colName, $this->_types)) {
  642. return true;
  643. }
  644. return false;
  645. }
  646. public function getVisibleFieldList() {
  647. $cols = array();
  648. $id = 0;
  649. foreach ($this->_fields as $kFieldID => $vField) {
  650. if ($vField['name'] == 'ID') {
  651. $id = $kFieldID;
  652. }
  653. }
  654. $cols[$id] = 'ID';
  655. foreach ($this->_fields as $kFieldID => $vField) {
  656. if ($vField['name'] == 'ID') {
  657. continue;
  658. }
  659. $cols[$kFieldID] = $vField['name'];
  660. }
  661. return $cols;
  662. }
  663. /**
  664. * List of real fields in database.
  665. */
  666. public function getRealFieldList() {
  667. $cols = array();
  668. $cols[] = 'ID';
  669. foreach ($this->_fields as $kFieldID => $vField) {
  670. if ($vField['name'] == 'ID') {
  671. continue;
  672. }
  673. if (array_key_exists($vField['name'], $this->_types)) {
  674. $cols[] = $vField['name'];
  675. }
  676. }
  677. return $cols;
  678. }
  679. public function getVirtualFieldList() {
  680. $cols = array();
  681. foreach ($this->_fields as $kFieldID => $vField) {
  682. if ($vField['name'] == 'ID') {
  683. continue;
  684. }
  685. if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
  686. $cols[$kFieldID] = $vField['name'];
  687. }
  688. else if (!array_key_exists($vField['name'], $this->_types)) {
  689. $cols[$kFieldID] = $vField['name'];
  690. }
  691. }
  692. return $cols;
  693. }
  694. public function getFieldLabel($fieldID) {
  695. if (array_key_exists($fieldID, $this->_fields)) {
  696. if (!empty($this->_fields[$fieldID]['label'])) {
  697. return $this->_fields[$fieldID]['label'];
  698. }
  699. }
  700. return null;
  701. }
  702. public function getTypes() {
  703. return $this->_types;
  704. }
  705. public function fixEmptyValueFromUser($fieldID) {
  706. $value = '';
  707. $type = $this->getFieldTypeById($fieldID);
  708. if ($type) {
  709. if ($type['type'] == 'date') {
  710. $value = $type['default'];
  711. }
  712. // fix bug when field is unique and is null allowed: change empty string to null
  713. if ($type['null']) {
  714. $value = 'NULL';
  715. }
  716. // fix bug when field is enum and is set to '0': for php '0' is empty
  717. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  718. if (false !== strpos($type['type'], "''")) {
  719. // enum('', '1','2')
  720. $value = '';
  721. } else if (false !== strpos($type['type'], "'0'")) {
  722. // enum('0', '1','2')
  723. $value = '0';
  724. } else {
  725. $value = $type['default'];
  726. }
  727. }
  728. }
  729. return $value;
  730. }
  731. public function fromArray($arr) {
  732. $this->_db = $arr['db'];
  733. $this->_name = $arr['name'];
  734. $this->_label = $arr['label'];
  735. $this->_opis = $arr['opis'];
  736. $this->_fields = $arr['fields'];
  737. $this->_virtualFieldsIdList = $arr['virtualFieldsIdList'];
  738. $this->_types = $arr['types'];
  739. }
  740. public function toArray() {
  741. $arr = array();
  742. $arr['db'] = $this->_db;
  743. $arr['name'] = $this->_name;
  744. $arr['label'] = $this->_label;
  745. $arr['opis'] = $this->_opis;
  746. $arr['fields'] = $this->_fields;
  747. $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
  748. $arr['types'] = $this->_types;
  749. return $arr;
  750. }
  751. public function getDataSource() {
  752. Lib::loadClass('Data_Source');
  753. $dataSource = new Data_Source($this->getDB());
  754. $dataSource->setTable($this->getName());
  755. $realFieldList = $this->getRealFieldList();
  756. $dataSource->setCols($realFieldList);
  757. $dataSource->setColTypes($this->getTypes());
  758. $dataSource->setVirtualCols($this->getVirtualFieldList());
  759. $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
  760. $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
  761. $dataSource->setAccessFltrAllowed(!$this->hasSuperAccessPerms());
  762. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  763. foreach ($adminFields as $vAdmFld) {
  764. if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
  765. $dataSource->addCol($vAdmFld);
  766. }
  767. }
  768. return $dataSource;
  769. }
  770. }