TableAcl.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. /* Form input Relative sizing:
  334. <input class="input-mini" type="text" placeholder=".input-mini">
  335. <input class="input-small" type="text" placeholder=".input-small">
  336. <input class="input-medium" type="text" placeholder=".input-medium">
  337. <input class="input-large" type="text" placeholder=".input-large">
  338. <input class="input-xlarge" type="text" placeholder=".input-xlarge">
  339. <input class="input-xxlarge" type="text" placeholder=".input-xxlarge">
  340. */
  341. if (substr($colType['type'], 0, 3) == 'int'
  342. || substr($colType['type'], 0, 7) == 'tinyint'
  343. || substr($colType['type'], 0, 8) == 'smallint'
  344. ) {
  345. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  346. $html->attrs['type'] = 'number';
  347. $html->attrs['class'][] = 'input-small';
  348. }
  349. else if (substr($colType['type'], 0, 6) == 'double') {
  350. $html->attrs['type'] = 'text';
  351. $html->attrs['class'][] = 'input-small';
  352. }
  353. else if (substr($colType['type'], 0, 7) == 'decimal') {
  354. $html->attrs['type'] = 'text';
  355. $html->attrs['class'][] = 'input-small';
  356. }
  357. else if (substr($colType['type'], 0, 7) == 'varchar'
  358. || substr($colType['type'], 0, 4) == 'char'
  359. ) {
  360. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  361. $html->attrs['type'] = 'text';
  362. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  363. if ($maxLength > 0) {
  364. $html->attrs['maxlength'] = $maxLength;
  365. }
  366. $valLength = strlen($fValue);
  367. if (isset($params['widthClass'])) {
  368. if ($params['widthClass'] == 'inside-modal') {
  369. $html->attrs['style'] = 'width:98%;';
  370. } else {
  371. $html->attrs['style'] = 'width:98%;';
  372. }
  373. } else {
  374. if ($maxLength < 11) {
  375. $html->attrs['class'][] = 'span2';
  376. } else if ($maxLength < 31) {
  377. $html->attrs['class'][] = 'span5';
  378. } else if ($maxLength < 51) {
  379. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  380. } else if ($maxLength < 101) {
  381. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  382. } else {
  383. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  384. }
  385. }
  386. }
  387. else if (substr($colType['type'], 0, 4) == 'date') {
  388. $testDatePicker = true;
  389. if ($testDatePicker) {
  390. $html->attrs['type'] = 'text';
  391. $html->_params[] = 'date';
  392. if (substr($colType['type'], 0, 8) == 'datetime') {
  393. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  394. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  395. $html->attrs['maxlength'] = 19;
  396. } else {
  397. $html->attrs['class'][] = 'se_type-date';// datepicker';
  398. $html->attrs['maxlength'] = 10;
  399. }
  400. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  401. $html->attrs['value'] = '';
  402. }
  403. } else {
  404. $html->attrs['type'] = 'date';
  405. }
  406. }
  407. else if (substr($colType['type'], 0, 4) == 'enum') {
  408. unset($html->attrs['type']);
  409. unset($html->attrs['value']);
  410. $html->tag = 'select';
  411. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  412. $selValue = $fValue;
  413. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  414. if ($taskPerm == 'C') {
  415. $selValue = $colType['default'];
  416. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  417. $selValue = $colType['default'];
  418. }
  419. }
  420. $html->cnt .= '<option value="">' . "" . '</option>';
  421. if (!empty($selValue) && !in_array($selValue, $values)) {
  422. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  423. }
  424. foreach ($values as $val) {
  425. $sel = ($selValue == $val)? ' selected="selected"' : '';
  426. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  427. }
  428. }
  429. else if (substr($colType['type'], 0, 4) == 'text'
  430. || substr($colType['type'], 0, 8) == 'tinytext'
  431. || substr($colType['type'], 0, 10) == 'mediumtext'
  432. || substr($colType['type'], 0, 8) == 'longtext'
  433. ) {
  434. $html->tag = 'textarea';
  435. $html->cnt = htmlspecialchars($fValue);
  436. if (isset($params['widthClass'])) {
  437. if ($params['widthClass'] == 'inside-modal') {
  438. $html->attrs['style'] = 'width:98%;';
  439. } else {
  440. $html->attrs['style'] = 'width:98%;';
  441. }
  442. } else {
  443. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  444. }
  445. $html->attrs['rows'] = '3';
  446. unset($html->attrs['type']);
  447. unset($html->attrs['value']);
  448. }
  449. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  450. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  451. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  452. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  453. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  454. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  455. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  456. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  457. else {
  458. return 'unknown Type "'.$colType['type'].'"';
  459. }
  460. $attrsOut = array();
  461. foreach ($html->attrs as $k => $v) {
  462. if (is_array($v)) $v = implode(' ', $v);
  463. $attrsOut[] = "{$k}=\"{$v}\"";
  464. }
  465. if (in_array($html->tag, array('select', 'textarea'))) {
  466. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  467. $out .= $html->cnt;
  468. $out .= '</' . $html->tag . '>';
  469. } else {
  470. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  471. }
  472. if (in_array('date', $html->_params)) {
  473. $out = '<div class="input-append">' . $out . '<span class="add-on">
  474. <i data-time-icon="icon-time" data-date-icon="icon-calendar" class="icon-calendar"></i>
  475. </span>
  476. </div>';
  477. }
  478. if (true == V::get('appendBack', '', $params)) {
  479. if ($html->tag == 'input' && $taskPerm == 'W') {
  480. $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>';
  481. }
  482. }
  483. if ($typeSpecial) {
  484. $tsParams = array();
  485. $tsValue = V::get('typespecialValue', '', $params);
  486. if (!empty($tsValue)) {
  487. $tsParams['typespecialValue'] = $tsValue;
  488. }
  489. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  490. }
  491. return $out;
  492. }
  493. /**
  494. * List table ids by database
  495. *
  496. *
  497. */
  498. public static function GetTablesByDbId($db) {
  499. DEBUG_S(3,'TableAcl_cache',$_SESSION['TableAcl_cache'],__FILE__,__FUNCTION__,__LINE__);
  500. static $_cache;
  501. $return=array();
  502. if (!$_cache) $_cache = array();
  503. if (!empty($_SESSION['TableAcl_cache'])) {
  504. foreach($_SESSION['TableAcl_cache'] as $tableID=>$obj) {
  505. //if($obj->db==$db)
  506. $return[$obj['name']]=$tableID;
  507. }
  508. return $return;
  509. }
  510. return null;
  511. }
  512. /**
  513. * Get column object. Not initialize
  514. * @returns object - column instance if exists else null
  515. *
  516. * static
  517. */
  518. public static function getInstance($tableID) {
  519. static $_cache;
  520. if (!$_cache) $_cache = array();
  521. if (array_key_exists($tableID, $_cache)) {
  522. return $_cache[$tableID];
  523. }
  524. if (!empty($_SESSION['TableAcl_cache'][$tableID])) {
  525. $obj = new TableAcl($tableID);
  526. $obj->fromArray($_SESSION['TableAcl_cache'][$tableID]);
  527. $_cache[$tableID] = $obj;
  528. return $_cache[$tableID];
  529. }
  530. return null;
  531. }
  532. public function init($force = false) {
  533. if ($this->isInitialized() && $force == false) {
  534. return;
  535. }
  536. $dbID = $this->getDB();
  537. $tblName = $this->getName();
  538. $db = DB::getDB($dbID);
  539. if (!$db) {
  540. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  541. }
  542. $res = $db->query("show fields from `{$tblName}` ");
  543. while ($h = $db->fetch_row($res)) {
  544. $fieldName = $h[0];
  545. $fieldType = $h[1];
  546. $this->_types[$fieldName] = array('type'=>$h[1], 'null'=>('YES' == $h[2]), 'default'=>$h[4]);
  547. }
  548. uasort($this->_fields, array($this, 'sortFieldsCallback'));
  549. $this->_fixDateFields();
  550. $this->_sortEnumFields();
  551. $this->_fixProjectType();
  552. $fieldIds = array_keys($this->_fields);
  553. Lib::loadClass('Typespecial');
  554. $vColsIdList = Typespecial::initFields($fieldIds);
  555. if (!empty($vColsIdList)) {
  556. $this->_virtualFieldsIdList = $vColsIdList;
  557. }
  558. $this->save();
  559. }
  560. private function _fixProjectType() {
  561. $tblName = $this->getName();
  562. $fldName = 'M_DIST_TYPE';
  563. if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
  564. foreach ($this->_fields as $kFldId => $vFld) {
  565. if ($vFld['name'] == $fldName) {
  566. $sqlTypes = array();
  567. if (!empty($this->_types[$fldName])) {
  568. if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
  569. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
  570. }
  571. }
  572. if (!empty($sqlTypes)) {
  573. $allowedTypes = array();
  574. $db = DB::getDB();
  575. $sql = "select z.DESC
  576. from `CRM_LISTA_ZASOBOW` as z
  577. where z.`A_STATUS`='NORMAL'
  578. and z.`PARENT_ID`={$kFldId}
  579. order by z.`DESC` asc
  580. ";
  581. $res = $db->query($sql);
  582. while ($r = $db->fetch($res)) {
  583. if (in_array($r->DESC, $sqlTypes)) {
  584. $allowedTypes[] = $r->DESC;
  585. }
  586. }
  587. sort($allowedTypes);
  588. if (!empty($allowedTypes)) {
  589. $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. private function _sortEnumFields() {
  597. foreach ($this->_fields as $kFldId => $vFld) {
  598. $type = $this->getFieldTypeById($kFldId);
  599. if (!empty($type['type'])) {
  600. if (substr($type['type'], 0, 4) == 'enum') {
  601. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  602. if (!empty($sqlTypes)) {
  603. sort($sqlTypes);
  604. $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
  605. }
  606. }
  607. }
  608. }
  609. }
  610. private function _fixDateFields() {
  611. foreach ($this->_types as $kFldName => $vType) {
  612. if ($kFldName == 'L_APPOITMENT_DATE') {
  613. $this->_types[$kFldName]['type'] = 'datetime';
  614. } else if ($kFldName == 'A_PROBLEM_DATE') {
  615. $this->_types[$kFldName]['type'] = 'datetime';
  616. }
  617. }
  618. }
  619. public function getUniqueKeys() {
  620. $sqlKeys = array();
  621. $dbID = $this->getDB();
  622. $tblName = $this->getName();
  623. $db = DB::getDB($dbID);
  624. if (!$db) {
  625. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  626. }
  627. $sql = "SHOW KEYS FROM `{$tblName}`";
  628. $res = $db->query($sql);
  629. while ($r = $db->fetch($res)) {
  630. if ($r->Non_unique == '0') {
  631. $sqlKeys[$r->Column_name] = true;
  632. }
  633. }
  634. $sqlKeys = array_keys($sqlKeys);
  635. return $sqlKeys;
  636. }
  637. public function sortFieldsCallback($a, $b) {
  638. if ($a['name'] == 'ID') {
  639. return -1;
  640. }
  641. else if ($b['name'] == 'ID') {
  642. return 1;
  643. }
  644. else if ($a['sort_prio'] < $b['sort_prio']) {
  645. return -1;
  646. }
  647. else if ($a['sort_prio'] > $b['sort_prio']) {
  648. return 1;
  649. }
  650. else {
  651. return 0;
  652. }
  653. }
  654. public function isInitialized() {
  655. return (!empty($this->_types));
  656. }
  657. /**
  658. * Save data in session cache.
  659. */
  660. function save() {
  661. $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
  662. }
  663. public function getFieldTypeById($fieldID) {
  664. if (!array_key_exists($fieldID, $this->_fields)) {
  665. return null;
  666. }
  667. $colName = $this->_fields[$fieldID]['name'];
  668. if (!array_key_exists($colName, $this->_types)) {
  669. return null;
  670. }
  671. return $this->_types[$colName];
  672. }
  673. public function getFieldType($colName) {
  674. if (!array_key_exists($colName, $this->_types)) {
  675. return null;
  676. }
  677. return $this->_types[$colName];
  678. }
  679. public function hasFieldType($colName) {
  680. if (array_key_exists($colName, $this->_types)) {
  681. return true;
  682. }
  683. return false;
  684. }
  685. public function getVisibleFieldList() {
  686. $cols = array();
  687. $id = 0;
  688. foreach ($this->_fields as $kFieldID => $vField) {
  689. if ($vField['name'] == 'ID') {
  690. $id = $kFieldID;
  691. }
  692. }
  693. $cols[$id] = 'ID';
  694. foreach ($this->_fields as $kFieldID => $vField) {
  695. if ($vField['name'] == 'ID') {
  696. continue;
  697. }
  698. $cols[$kFieldID] = $vField['name'];
  699. }
  700. return $cols;
  701. }
  702. public function getExportFieldList() {
  703. $cols = array();
  704. $realFlds = $this->getRealFieldList();
  705. foreach ($realFlds as $vFieldName) {
  706. $fldId = $this->getFieldIdByName($vFieldName);
  707. if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
  708. $cols[] = $vFieldName;
  709. }
  710. }
  711. return $cols;
  712. }
  713. /**
  714. * List of real fields in database.
  715. */
  716. public function getRealFieldList() {
  717. $cols = array();
  718. $cols[] = 'ID';
  719. foreach ($this->_fields as $kFieldID => $vField) {
  720. if ($vField['name'] == 'ID') {
  721. continue;
  722. }
  723. if (array_key_exists($vField['name'], $this->_types)) {
  724. $cols[] = $vField['name'];
  725. }
  726. }
  727. return $cols;
  728. }
  729. public function getVirtualFieldList() {
  730. $cols = array();
  731. foreach ($this->_fields as $kFieldID => $vField) {
  732. if ($vField['name'] == 'ID') {
  733. continue;
  734. }
  735. if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
  736. $cols[$kFieldID] = $vField['name'];
  737. }
  738. else if (!array_key_exists($vField['name'], $this->_types)) {
  739. $cols[$kFieldID] = $vField['name'];
  740. }
  741. }
  742. return $cols;
  743. }
  744. public function getFieldLabel($fieldID) {
  745. if (array_key_exists($fieldID, $this->_fields)) {
  746. if (!empty($this->_fields[$fieldID]['label'])) {
  747. return $this->_fields[$fieldID]['label'];
  748. }
  749. }
  750. return null;
  751. }
  752. public function getFieldOpis($fieldID) {
  753. if (array_key_exists($fieldID, $this->_fields)) {
  754. if (!empty($this->_fields[$fieldID]['opis'])) {
  755. return $this->_fields[$fieldID]['opis'];
  756. }
  757. }
  758. return null;
  759. }
  760. public function getTypes() {
  761. return $this->_types;
  762. }
  763. public function fixEmptyValueFromUser($fieldID) {
  764. $value = '';
  765. $type = $this->getFieldTypeById($fieldID);
  766. if ($type) {
  767. if ($type['type'] == 'date') {
  768. $value = $type['default'];
  769. }
  770. if (substr($type['type'], 0, 3) == 'int'
  771. || substr($type['type'], 0, 7) == 'tinyint'
  772. || substr($type['type'], 0, 8) == 'smallint'
  773. ) {
  774. $value = intval($type['default']);
  775. }
  776. // fix bug when field is unique and is null allowed: change empty string to null
  777. if ($type['null']) {
  778. $value = 'NULL';
  779. }
  780. // fix bug when field is enum and is set to '0': for php '0' is empty
  781. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  782. if (false !== strpos($type['type'], "''")) {
  783. // enum('', '1','2')
  784. $value = '';
  785. } else if (false !== strpos($type['type'], "'0'")) {
  786. // enum('0', '1','2')
  787. $value = '0';
  788. } else {
  789. $value = $type['default'];
  790. }
  791. }
  792. }
  793. return $value;
  794. }
  795. public function fromArray($arr) {
  796. $this->_db = $arr['db'];
  797. $this->_name = $arr['name'];
  798. $this->_label = $arr['label'];
  799. $this->_opis = $arr['opis'];
  800. $this->_fields = $arr['fields'];
  801. $this->_virtualFieldsIdList = $arr['virtualFieldsIdList'];
  802. $this->_types = $arr['types'];
  803. }
  804. public function toArray() {
  805. $arr = array();
  806. $arr['db'] = $this->_db;
  807. $arr['name'] = $this->_name;
  808. $arr['label'] = $this->_label;
  809. $arr['opis'] = $this->_opis;
  810. $arr['fields'] = $this->_fields;
  811. $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
  812. $arr['types'] = $this->_types;
  813. return $arr;
  814. }
  815. public function getExportDataSource($cols = array()) {
  816. $exportFieldList = $this->getExportFieldList();
  817. if (!empty($cols)) {
  818. $fltrExportFlds = array();
  819. foreach ($exportFieldList as $fldName) {
  820. if (in_array($fldName, $cols)) {
  821. $fltrExportFlds[] = $fldName;
  822. }
  823. }
  824. $exportFieldList = $fltrExportFlds;
  825. }
  826. $dataSource = $this->_getDataSource($exportFieldList);
  827. return $dataSource;
  828. }
  829. public function getDataSource() {
  830. $realFieldList = $this->getRealFieldList();
  831. $dataSource = $this->_getDataSource($realFieldList);
  832. $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
  833. $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
  834. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  835. foreach ($adminFields as $vAdmFld) {
  836. if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
  837. $dataSource->addCol($vAdmFld);
  838. }
  839. }
  840. return $dataSource;
  841. }
  842. private function _getDataSource($cols) {
  843. Lib::loadClass('Data_Source');
  844. $dataSource = new Data_Source($this->getDB());
  845. $dataSource->setTable($this->getName());
  846. $dataSource->setCols($cols);
  847. $dataSource->setColTypes($this->getTypes());
  848. $dataSource->setVirtualCols($this->getVirtualFieldList());
  849. $dataSource->setAccessFltrAllowed(!$this->hasSuperAccessPerms());
  850. return $dataSource;
  851. }
  852. }