TableAcl.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810
  1. <?php
  2. Lib::loadClass('Core_AclBase');
  3. Lib::loadClass('SchemaException');
  4. /**
  5. * $_SESSION['TableAcl_cache'][$tableID] = array(
  6. * [db] => DB zasob ID
  7. * [name] => Table name
  8. * [opis] => Table opis
  9. * [fields] => array(
  10. * [$fieldID] => array(
  11. * [name] => name
  12. * [perms] => perms (FORM_TREAT)
  13. * [opis] => opis
  14. * )
  15. * )
  16. * [types] => array(
  17. * [$fieldID] => array(
  18. * [type] => type
  19. * [null] => bool
  20. * [default] => default value
  21. * )
  22. * )
  23. * );
  24. */
  25. class TableAcl extends Core_AclBase {
  26. private $_zasobID = '';
  27. private $_db = '';
  28. private $_name = '';
  29. private $_label = '';
  30. private $_opis = '';
  31. private $_fields = array();
  32. private $_types = array();
  33. private $_virtualFieldsIdList = array();
  34. private $_schemaLoaded = false;
  35. public function __construct($zasobID) { $this->_zasobID = $zasobID; }
  36. public function getSourceName() { return 'default_db'; }
  37. public function getName() { return $this->_name; }
  38. public function getRootTableName() { return $this->_name; }
  39. public function getID() { return $this->_zasobID; }
  40. public function setName($name) { $this->_name = $name; }
  41. public function setOpis($opis) { $this->_opis = $opis; }
  42. public function getOpis() { return $this->_opis; }
  43. public function setLabel($label) { $this->_label = $label; }
  44. public function getLabel() { return $this->_label; }
  45. public function setNameByTableId($tableID) {
  46. //used for init without knowing table name
  47. $db = DB::getDB();
  48. $sql = "select `DESC` from CRM_LISTA_ZASOBOW where ID={$tableID} and `TYPE`='TABELA'";
  49. $res = $db->query($sql);
  50. $res_ = $db->fetch($res);
  51. //DEBUG_S(-3,'setNameByTableId',$res_,__FILE__,__FUNCTION__,__LINE__);
  52. self::setName($res->DESC);
  53. }
  54. public function getRawOpis() {
  55. $opis = $this->_opis;
  56. if (!$opis) return $this->getRawLabel(100);
  57. $opis = strip_tags($opis);
  58. return $opis;
  59. }
  60. public function getRawLabel($posLimit = 20) {
  61. $label = $this->_label;
  62. if (empty($label) && !empty($this->_opis)) {
  63. $label = $this->_opis;
  64. if (mb_strlen($this->_opis) > $posLimit) {
  65. $pos = strpos($this->_opis, ' - ');
  66. if ($pos > $posLimit || $pos < 5) {
  67. $pos = $posLimit;
  68. $label = mb_substr($this->_opis, 0, $posLimit, 'utf-8') . '...';
  69. } else {
  70. $label = mb_substr($this->_opis, 0, $pos, 'utf-8');
  71. }
  72. }
  73. }
  74. if (empty($label)) {
  75. $label = $this->_name;
  76. }
  77. return $label;
  78. }
  79. public function getShortLabel($posLimit = 20) {
  80. $shortLabel = $this->getRawLabel($posLimit);
  81. $opis = $this->_opis;
  82. $shortLabel = '<span title="' . htmlspecialchars($opis) . '">' . $shortLabel . '</span>';
  83. return $shortLabel;
  84. }
  85. public function getLongLabel($posLimit = 30) {
  86. $longLabel = $this->getRawLabel($posLimit);
  87. $opis = $this->_opis;
  88. if ($longLabel != $this->_name) {
  89. $longLabel .= ' <em>' . $this->_name . '</em>';
  90. }
  91. $longLabel = '<span title="' . htmlspecialchars($opis) . '">' . $longLabel . '</span>';
  92. return $longLabel;
  93. }
  94. public function getLongRawLabel($posLimit = 30) {
  95. $longLabel = $this->getRawLabel($posLimit);
  96. $opis = $this->_opis;
  97. if ($longLabel != $this->_name) {
  98. $longLabel .= ' ' . $this->_name;
  99. }
  100. return $longLabel;
  101. }
  102. public function setDB($db) {
  103. $this->_db = $db;
  104. }
  105. public function getDB() {
  106. return $this->_db;
  107. }
  108. public function addField($fieldID, $name, $opis, $sort_prio, $label = '') {
  109. $field = array();
  110. $field['name'] = $name;
  111. $field['perms'] = '';
  112. $field['opis'] = $opis;
  113. $field['sort_prio'] = $sort_prio;
  114. $field['label'] = $label;
  115. $this->_fields[$fieldID] = $field;
  116. }
  117. public function getTableDbId($tableID) {
  118. return $this->_db;
  119. }
  120. public function getField($fieldID) {
  121. return $this->_fields[$fieldID];
  122. }
  123. public function hasField($fieldID) {
  124. return array_key_exists($fieldID, $this->_fields);
  125. }
  126. public function removeField($fieldID) {
  127. if (array_key_exists($fieldID, $this->_fields)) {
  128. unset($this->_fields[$fieldID]);
  129. }
  130. }
  131. public function getFields() {
  132. return $this->_fields;
  133. }
  134. public function setFieldPerms($fieldID, $perms) {
  135. if (array_key_exists($fieldID, $this->_fields)) {
  136. $this->_fields[$fieldID]['perms'] .= $perms;
  137. }
  138. }
  139. public function getFieldPerms($fieldID) {
  140. if (array_key_exists($fieldID, $this->_fields)) {
  141. $perms = V::get('perms', '', $this->_fields[$fieldID]);
  142. if ($perms) {
  143. return implode(',', array_unique(str_split($perms)));
  144. }
  145. }
  146. return '';
  147. }
  148. public function hasFieldPerm($fieldID, $perm) {// TODO: legacy
  149. if (array_key_exists($fieldID, $this->_fields)) {
  150. if (false !== strpos($this->_fields[$fieldID]['perms'], $perm)) {
  151. return true;
  152. }
  153. return false;
  154. }
  155. return false;
  156. }
  157. public function getFieldIdByName($fieldName) {
  158. $fieldID = 0;
  159. if (empty($fieldName)) {
  160. return;
  161. }
  162. foreach ($this->_fields as $kID => $vField) {
  163. if ($vField['name'] == $fieldName) {
  164. $fieldID = $kID;
  165. }
  166. }
  167. return $fieldID;
  168. }
  169. public function hasEditPerms() {
  170. foreach ($this->_fields as $kFldID => $vFld) {
  171. if ($this->hasFieldPerm($kFldID, 'W')) return true;
  172. if ($this->hasFieldPerm($kFldID, 'C')) return true;
  173. if ($this->hasFieldPerm($kFldID, 'S')) return true;
  174. }
  175. return false;
  176. }
  177. public function hasCreatePerms() {
  178. foreach ($this->_fields as $kFldID => $vFld) {
  179. if ($this->hasFieldPerm($kFldID, 'C')) return true;
  180. }
  181. return false;
  182. }
  183. public function hasSuperAccessPerms() {
  184. foreach ($this->_fields as $kFldID => $vFld) {
  185. if ($this->hasFieldPerm($kFldID, 'S')) {
  186. return true;
  187. }
  188. else if ($this->hasFieldPerm($kFldID, 'V')) {
  189. return true;
  190. }
  191. }
  192. return false;
  193. }
  194. public function hasPermSuperWrite() {
  195. foreach ($this->_fields as $kFldID => $vFld) {
  196. if ($this->hasFieldPerm($kFldID, 'S')) {
  197. return true;
  198. }
  199. }
  200. return false;
  201. }
  202. /**
  203. *
  204. */
  205. public function canWriteRecord($record) {
  206. $dbgArr = array();
  207. $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : '';
  208. $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : '';
  209. $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : '';
  210. $dbgArr['user_groups'] = User::getLdapGroupsNames();
  211. 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>';}
  212. if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
  213. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - is record owner</p>';}
  214. return true;
  215. }
  216. if ($dbgArr['record_write']) {
  217. if (in_array($dbgArr['record_write'], $dbgArr['user_groups'])) {
  218. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - has group write</p>';}
  219. return true;
  220. }
  221. } else {
  222. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - group write not set</p>';}
  223. return true;
  224. }
  225. return false;
  226. }
  227. public function canReadRecord($record) {
  228. $dbgArr = array();
  229. $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : '';
  230. $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : '';
  231. $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : '';
  232. $dbgArr['user_groups'] = User::getLdapGroupsNames();
  233. 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>';}
  234. if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
  235. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - is record owner</p>';}
  236. return true;
  237. }
  238. if ($dbgArr['record_read']) {
  239. if (in_array($dbgArr['record_read'], $dbgArr['user_groups'])) {
  240. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - has group read</p>';}
  241. return true;
  242. }
  243. } else {
  244. if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo '<p>true - group read not set</p>';}
  245. return true;
  246. }
  247. return false;
  248. }
  249. /**
  250. * @param $taskPerm - 'C', 'W', 'R'
  251. */
  252. public function isAllowed($fieldID, $taskPerm, $record = null) {// TODO: legacy - replace with canWriteField, canReadField, canWriteObjectField, canReadObjectField, canCreateField
  253. if (!in_array($taskPerm, array('C', 'W', 'R'))) {
  254. return false;
  255. }
  256. $adminFields = array();
  257. $adminFields[] = 'ID';
  258. $adminFields[] = 'A_RECORD_CREATE_DATE';
  259. $adminFields[] = 'A_RECORD_CREATE_AUTHOR';
  260. $adminFields[] = 'A_RECORD_UPDATE_DATE';
  261. $adminFields[] = 'A_RECORD_UPDATE_AUTHOR';
  262. $fieldName = $this->_fields[$fieldID]['name'];
  263. if ($taskPerm == 'R' && in_array($fieldName, $adminFields)) {
  264. return true;
  265. }
  266. // check perm: allow 'RS', 'WS' - can R/W field even if cant read record
  267. // check 'O' - can read field even if cant read field but can read record
  268. 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.')'
  269. ,'taskPerm'=>$taskPerm
  270. ,'fieldPerms'=>V::get('perms', null, V::get($fieldID, null, $this->_fields))
  271. ,'canReadRecord'=>'"'.$this->canReadRecord($record).'"'
  272. ,'hasFieldPerm(O) || canWriteRecord'=>'"'.$this->hasFieldPerm($fieldID, 'O').'" || "'.$this->canReadRecord($record).'"'
  273. ,'hasFieldPerm(S)'=>'"'.$this->hasFieldPerm($fieldID, 'S').'"'
  274. ,'hasFieldPerm(V)'=>'"'.$this->hasFieldPerm($fieldID, 'V').'"'
  275. ));echo'</pre>'; }
  276. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  277. if ($taskPerm == 'R' && $this->hasFieldPerm($fieldID, 'V')) {
  278. return true;
  279. } else if ($taskPerm == 'R'
  280. && $record
  281. && $this->hasFieldPerm($fieldID, 'O')
  282. && ($this->canReadRecord($record) || $this->canWriteRecord($record))
  283. ) {
  284. return true;// 'WO' or 'CO'
  285. }
  286. return false;
  287. }
  288. // check 'R' - require can read record, or V - Super View
  289. if ($taskPerm == 'R') {
  290. if ($this->canReadRecord($record) || $this->hasFieldPerm($fieldID, 'V')) {
  291. return true;
  292. } else {
  293. return false;
  294. }
  295. }
  296. // 'C' and 'W' require colType
  297. $colType = $this->getFieldTypeById($fieldID);
  298. if (!$colType) {
  299. return false;
  300. }
  301. if ($taskPerm == 'W') {
  302. if ($record) {
  303. 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') . '))';}
  304. return ($this->canWriteRecord($record)|| $this->hasFieldPerm($fieldID, 'S'));
  305. }
  306. }
  307. return true;
  308. }
  309. public function canCreateField($fieldName) {
  310. $idZasob = $this->getFieldIdByName($fieldName);
  311. return ($idZasob && $this->hasFieldPerm($idZasob, $taskPerm = 'C'));
  312. }
  313. public function canReadField($fieldName) {
  314. $idZasob = $this->getFieldIdByName($fieldName);
  315. if (!$idZasob) return false;
  316. if ('ID' == $fieldName) return true;
  317. if ('A_RECORD_CREATE_DATE' == $fieldName) return true;
  318. if ('A_RECORD_CREATE_AUTHOR' == $fieldName) return true;
  319. if ('A_RECORD_UPDATE_DATE' == $fieldName) return true;
  320. if ('A_RECORD_UPDATE_AUTHOR' == $fieldName) return true;
  321. if (!$this->hasFieldPerm($idZasob, $taskPerm = 'R')) {
  322. if ($this->hasFieldPerm($idZasob, 'V')) return true;
  323. }
  324. return true;
  325. }
  326. public function canReadObjectField($fieldName, $record) {
  327. if (!$this->canReadField($fieldName)) return false;
  328. if (!$record) return false;
  329. $idZasob = $this->getFieldIdByName($fieldName);
  330. if (!$idZasob) return false;
  331. if (!$this->hasFieldPerm($idZasob, $taskPerm = 'R')) {
  332. if ($this->hasFieldPerm($idZasob, 'V')) return true;
  333. return ($this->hasFieldPerm($idZasob, 'O') && ($this->canReadRecord($record) || $this->canWriteRecord($record)));// 'WO' or 'CO'
  334. }
  335. return ($this->canReadRecord($record) || $this->hasFieldPerm($idZasob, 'V'));// 'R' require
  336. }
  337. public function canWriteField($fieldName) {
  338. $idZasob = $this->getFieldIdByName($fieldName);
  339. if (!$idZasob) return false;
  340. return $this->hasFieldPerm($idZasob, $taskPerm = 'W');
  341. }
  342. public function canWriteObjectField($fieldName, $record) {
  343. if (!$this->canWriteField($fieldName)) return false;
  344. $idZasob = $this->getFieldIdByName($fieldName);
  345. if (!$idZasob) return false;
  346. if(V::get('DBG_ACL', '', $_REQUEST) > 1){echo '(Field: "'.$fieldName.'" ['.$idZasob.'], canWriteRecord: ' . $this->canWriteRecord($record) . ' || (hasFieldPerm(S): ' . $this->hasFieldPerm($idZasob, 'S') . ' && hasFieldPerm(W): ' . $this->hasFieldPerm($idZasob, 'W') . '))';}
  347. return ($this->canWriteRecord($record) || $this->hasFieldPerm($idZasob, 'S'));
  348. }
  349. /**
  350. * @param $taskPerm - 'C', 'W'
  351. */
  352. public function getFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  353. $out = '';
  354. if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
  355. if ($taskPerm == 'R') throw new Exception("Brak uprawnień do odczytu");
  356. else if ($taskPerm == 'W') throw new Exception("Brak uprawnień do zapisu");
  357. else throw new Exception("Brak uprawnień do tego pola ({$taskPerm})");
  358. }
  359. $colName = $this->_fields[$fieldID]['name'];
  360. if ($colName == 'ID') return;
  361. $colType = $this->getFieldTypeById($fieldID);
  362. if (!$colType) throw new Exception("Error - unknown type");
  363. $item = array();
  364. $item['htmlType'] = '';
  365. $item['value'] = $fValue;
  366. $item['canRead'] = $this->isAllowed($fieldID, 'R', $record);
  367. $item['canWrite'] = $this->isAllowed($fieldID, 'W', $record);
  368. $item['rawColType'] = $colType;
  369. return $item;
  370. $html = new stdClass();
  371. $html->_params = array();
  372. $html->tag = 'input';
  373. $html->cnt = '';
  374. $html->attrs = array();
  375. $html->attrs['id'] = $fName;
  376. $html->attrs['name'] = $fName;
  377. $html->attrs['type'] = 'text';
  378. $html->attrs['value'] = htmlspecialchars($fValue);
  379. if (isset($params['tabindex'])) {
  380. $html->attrs['tabindex'] = $params['tabindex'];
  381. }
  382. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  383. $html->attrs['disabled'] = 'disabled';
  384. }
  385. $maxGrid = V::get('maxGrid', 10, $params);
  386. if (substr($colType['type'], 0, 3) == 'int'
  387. || substr($colType['type'], 0, 7) == 'tinyint'
  388. || substr($colType['type'], 0, 8) == 'smallint'
  389. || substr($colType['type'], 0, 6) == 'bigint'
  390. ) {
  391. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  392. $html->attrs['type'] = 'number';
  393. $html->attrs['class'][] = 'input-small';
  394. }
  395. else if (substr($colType['type'], 0, 6) == 'double') {
  396. $html->attrs['type'] = 'text';
  397. $html->attrs['class'][] = 'input-small';
  398. }
  399. else if (substr($colType['type'], 0, 7) == 'decimal') {
  400. $html->attrs['type'] = 'text';
  401. $html->attrs['class'][] = 'input-small';
  402. }
  403. else if (substr($colType['type'], 0, 7) == 'varchar'
  404. || substr($colType['type'], 0, 4) == 'char'
  405. ) {
  406. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  407. $html->attrs['type'] = 'text';
  408. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  409. if ($maxLength > 0) {
  410. $html->attrs['maxlength'] = $maxLength;
  411. }
  412. $valLength = strlen($fValue);
  413. if (isset($params['widthClass'])) {
  414. if ($params['widthClass'] == 'inside-modal') {
  415. $html->attrs['style'] = 'width:98%;';
  416. } else {
  417. $html->attrs['style'] = 'width:98%;';
  418. }
  419. } else {
  420. /*
  421. if ($maxLength < 11) {
  422. $html->attrs['class'][] = 'span2';
  423. } else if ($maxLength < 31) {
  424. $html->attrs['class'][] = 'span5';
  425. } else if ($maxLength < 51) {
  426. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  427. } else if ($maxLength < 101) {
  428. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  429. } else {
  430. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  431. }
  432. */
  433. }
  434. if ($maxLength > 255) {// Fix for long varchar - use textarea
  435. $html->tag = 'textarea';
  436. $html->cnt = htmlspecialchars($fValue);
  437. $html->attrs['rows'] = '3';
  438. unset($html->attrs['type']);
  439. unset($html->attrs['value']);
  440. }
  441. }
  442. else if (substr($colType['type'], 0, 4) == 'date') {
  443. $testDatePicker = true;
  444. if ($testDatePicker) {
  445. $html->attrs['type'] = 'text';
  446. $html->_params[] = 'date';
  447. if (substr($colType['type'], 0, 8) == 'datetime') {
  448. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  449. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  450. $html->attrs['maxlength'] = 19;
  451. } else {
  452. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  453. $html->attrs['maxlength'] = 10;
  454. }
  455. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  456. $html->attrs['value'] = '';
  457. }
  458. } else {
  459. $html->attrs['type'] = 'date';
  460. }
  461. }
  462. else if ($colType['type'] == 'time') {
  463. $testDatePicker = true;
  464. if ($testDatePicker) {
  465. $html->attrs['type'] = 'text';
  466. $html->_params[] = 'time';
  467. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  468. $html->attrs['data-format'] = 'hh:mm:ss';
  469. $html->attrs['maxlength'] = 8;
  470. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  471. $html->attrs['value'] = '';
  472. }
  473. } else {
  474. $html->attrs['type'] = 'time';
  475. }
  476. }
  477. else if ($colType['type'] == 'timestamp') {
  478. $testDatePicker = true;
  479. if ($testDatePicker) {
  480. $html->attrs['type'] = 'text';
  481. $html->_params[] = 'date';
  482. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  483. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  484. $html->attrs['maxlength'] = 19;
  485. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  486. $html->attrs['value'] = '';
  487. }
  488. } else {
  489. $html->attrs['type'] = 'date';
  490. }
  491. }
  492. else if (substr($colType['type'], 0, 4) == 'enum') {
  493. unset($html->attrs['type']);
  494. unset($html->attrs['value']);
  495. $html->tag = 'select';
  496. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  497. $selValue = $fValue;
  498. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  499. if ($taskPerm == 'C') {
  500. $selValue = $colType['default'];
  501. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  502. $selValue = $colType['default'];
  503. }
  504. }
  505. $html->cnt .= '<option value="">' . "" . '</option>';
  506. if (!empty($selValue) && !in_array($selValue, $values)) {
  507. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  508. }
  509. foreach ($values as $val) {
  510. $sel = ($selValue == $val)? ' selected="selected"' : '';
  511. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  512. }
  513. }
  514. else if (substr($colType['type'], 0, 4) == 'text'
  515. || substr($colType['type'], 0, 8) == 'tinytext'
  516. || substr($colType['type'], 0, 10) == 'mediumtext'
  517. || substr($colType['type'], 0, 8) == 'longtext'
  518. ) {
  519. $html->tag = 'textarea';
  520. $html->cnt = htmlspecialchars($fValue);
  521. if (isset($params['widthClass'])) {
  522. if ($params['widthClass'] == 'inside-modal') {
  523. $html->attrs['style'] = 'width:98%;';
  524. } else {
  525. $html->attrs['style'] = 'width:98%;';
  526. }
  527. } else {
  528. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  529. }
  530. $html->attrs['rows'] = '3';
  531. unset($html->attrs['type']);
  532. unset($html->attrs['value']);
  533. }
  534. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  535. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  536. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  537. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  538. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  539. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  540. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  541. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  542. else throw new Exception("unknown Type '{$colType['type']}'");
  543. $html->attrs['class'][] = 'form-control';
  544. $attrsOut = array();
  545. foreach ($html->attrs as $k => $v) {
  546. if (is_array($v)) $v = implode(' ', $v);
  547. $attrsOut[] = "{$k}=\"{$v}\"";
  548. }
  549. if (in_array($html->tag, array('select', 'textarea'))) {
  550. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  551. $out .= $html->cnt;
  552. $out .= '</' . $html->tag . '>';
  553. } else {
  554. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  555. }
  556. if (in_array('date', $html->_params)) {
  557. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  558. <span class="glyphicon glyphicon-calendar"></span>
  559. </span>
  560. </div>';
  561. }
  562. else if (in_array('time', $html->_params)) {
  563. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  564. <span class="glyphicon glyphicon-time"></span>
  565. </span>
  566. </div>';
  567. }
  568. if (true == V::get('appendBack', '', $params)
  569. && !in_array('date', $html->_params)
  570. && !in_array('time', $html->_params)
  571. ) {
  572. if ($html->tag == 'input' && $taskPerm == 'W') {
  573. $out = '<div class="input-group show-last-value">' . $out . '<span class="input-group-addon button-appendBack" title="' . htmlspecialchars($fValue) . '"><span class="glyphicon glyphicon-arrow-left"></span></span></div>';
  574. }
  575. }
  576. Lib::loadClass('Typespecial');
  577. $typeSpecial = Typespecial::getInstance($fieldID, $colName);
  578. if ($typeSpecial) {
  579. throw new Exception("TODO: TypeSpecial");
  580. $tsParams = array();
  581. $tsValue = V::get('typespecialValue', '', $params);
  582. if (!empty($tsValue)) {
  583. $tsParams['typespecialValue'] = $tsValue;
  584. }
  585. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  586. }
  587. return $out;
  588. }
  589. /**
  590. * @param $taskPerm - 'C', 'W'
  591. */
  592. public function showFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  593. $out = '';
  594. if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
  595. if ($taskPerm == 'R') {
  596. $out .= 'Brak uprawnień do odczytu';
  597. }
  598. else if ($taskPerm == 'W') {
  599. $out .= 'Brak uprawnień do zapisu';
  600. } else {
  601. $out .= 'Brak uprawnień do tego pola (' . $taskPerm . ')';
  602. }
  603. return $out;
  604. }
  605. $colName = $this->_fields[$fieldID]['name'];
  606. if ($colName == 'ID') {
  607. return $out;
  608. }
  609. $colType = $this->getFieldTypeById($fieldID);
  610. if (!$colType) {
  611. $out .= 'Error - unknown type';
  612. return $out;
  613. }
  614. Lib::loadClass('Typespecial');
  615. $typeSpecial = Typespecial::getInstance($fieldID, $colName);
  616. $html = new stdClass();
  617. $html->_params = array();
  618. $html->tag = 'input';
  619. $html->cnt = '';
  620. $html->attrs = array();
  621. $html->attrs['id'] = $fName;
  622. $html->attrs['name'] = $fName;
  623. $html->attrs['type'] = 'text';
  624. $html->attrs['value'] = htmlspecialchars($fValue);
  625. if (isset($params['tabindex'])) {
  626. $html->attrs['tabindex'] = $params['tabindex'];
  627. }
  628. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  629. $html->attrs['disabled'] = 'disabled';
  630. }
  631. $maxGrid = V::get('maxGrid', 10, $params);
  632. if (substr($colType['type'], 0, 3) == 'int'
  633. || substr($colType['type'], 0, 7) == 'tinyint'
  634. || substr($colType['type'], 0, 8) == 'smallint'
  635. || substr($colType['type'], 0, 6) == 'bigint'
  636. ) {
  637. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  638. $html->attrs['type'] = 'number';
  639. $html->attrs['class'][] = 'input-small';
  640. }
  641. else if (substr($colType['type'], 0, 6) == 'double') {
  642. $html->attrs['type'] = 'text';
  643. $html->attrs['class'][] = 'input-small';
  644. }
  645. else if (substr($colType['type'], 0, 7) == 'decimal') {
  646. $html->attrs['type'] = 'text';
  647. $html->attrs['class'][] = 'input-small';
  648. }
  649. else if (substr($colType['type'], 0, 7) == 'varchar'
  650. || substr($colType['type'], 0, 4) == 'char'
  651. ) {
  652. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  653. $html->attrs['type'] = 'text';
  654. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  655. if ($maxLength > 0) {
  656. $html->attrs['maxlength'] = $maxLength;
  657. }
  658. $valLength = strlen($fValue);
  659. if (isset($params['widthClass'])) {
  660. if ($params['widthClass'] == 'inside-modal') {
  661. $html->attrs['style'] = 'width:98%;';
  662. } else {
  663. $html->attrs['style'] = 'width:98%;';
  664. }
  665. } else {
  666. /*
  667. if ($maxLength < 11) {
  668. $html->attrs['class'][] = 'span2';
  669. } else if ($maxLength < 31) {
  670. $html->attrs['class'][] = 'span5';
  671. } else if ($maxLength < 51) {
  672. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  673. } else if ($maxLength < 101) {
  674. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  675. } else {
  676. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  677. }
  678. */
  679. }
  680. if ($maxLength > 255) {// Fix for long varchar - use textarea
  681. $html->tag = 'textarea';
  682. $html->cnt = htmlspecialchars($fValue);
  683. $html->attrs['rows'] = '3';
  684. unset($html->attrs['type']);
  685. unset($html->attrs['value']);
  686. }
  687. }
  688. else if (substr($colType['type'], 0, 4) == 'date') {
  689. $testDatePicker = true;
  690. if ($testDatePicker) {
  691. $html->attrs['type'] = 'text';
  692. $html->_params[] = 'date';
  693. if (substr($colType['type'], 0, 8) == 'datetime') {
  694. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  695. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  696. $html->attrs['maxlength'] = 19;
  697. } else {
  698. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  699. $html->attrs['maxlength'] = 10;
  700. }
  701. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  702. $html->attrs['value'] = '';
  703. }
  704. } else {
  705. $html->attrs['type'] = 'date';
  706. }
  707. }
  708. else if ($colType['type'] == 'time') {
  709. $testDatePicker = true;
  710. if ($testDatePicker) {
  711. $html->attrs['type'] = 'text';
  712. $html->_params[] = 'time';
  713. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  714. $html->attrs['data-format'] = 'hh:mm:ss';
  715. $html->attrs['maxlength'] = 8;
  716. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  717. $html->attrs['value'] = '';
  718. }
  719. } else {
  720. $html->attrs['type'] = 'time';
  721. }
  722. }
  723. else if ($colType['type'] == 'timestamp') {
  724. $testDatePicker = true;
  725. if ($testDatePicker) {
  726. $html->attrs['type'] = 'text';
  727. $html->_params[] = 'date';
  728. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  729. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  730. $html->attrs['maxlength'] = 19;
  731. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  732. $html->attrs['value'] = '';
  733. }
  734. } else {
  735. $html->attrs['type'] = 'date';
  736. }
  737. }
  738. else if (substr($colType['type'], 0, 4) == 'enum') {
  739. unset($html->attrs['type']);
  740. unset($html->attrs['value']);
  741. $html->tag = 'select';
  742. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  743. $selValue = $fValue;
  744. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  745. if ($taskPerm == 'C') {
  746. $selValue = $colType['default'];
  747. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  748. $selValue = $colType['default'];
  749. }
  750. }
  751. $html->cnt .= '<option value="">' . "" . '</option>';
  752. if (!empty($selValue) && !in_array($selValue, $values)) {
  753. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  754. }
  755. foreach ($values as $val) {
  756. $sel = ($selValue == $val)? ' selected="selected"' : '';
  757. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  758. }
  759. }
  760. else if (substr($colType['type'], 0, 4) == 'text'
  761. || substr($colType['type'], 0, 8) == 'tinytext'
  762. || substr($colType['type'], 0, 10) == 'mediumtext'
  763. || substr($colType['type'], 0, 8) == 'longtext'
  764. ) {
  765. $html->tag = 'textarea';
  766. $html->cnt = htmlspecialchars($fValue);
  767. if (isset($params['widthClass'])) {
  768. if ($params['widthClass'] == 'inside-modal') {
  769. $html->attrs['style'] = 'width:98%;';
  770. } else {
  771. $html->attrs['style'] = 'width:98%;';
  772. }
  773. } else {
  774. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  775. }
  776. $html->attrs['rows'] = '3';
  777. unset($html->attrs['type']);
  778. unset($html->attrs['value']);
  779. }
  780. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  781. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  782. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  783. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  784. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  785. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  786. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  787. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  788. else {
  789. return 'unknown Type "'.$colType['type'].'"';
  790. }
  791. $html->attrs['class'][] = 'form-control';
  792. $attrsOut = array();
  793. foreach ($html->attrs as $k => $v) {
  794. if (is_array($v)) $v = implode(' ', $v);
  795. $attrsOut[] = "{$k}=\"{$v}\"";
  796. }
  797. if (in_array($html->tag, array('select', 'textarea'))) {
  798. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  799. $out .= $html->cnt;
  800. $out .= '</' . $html->tag . '>';
  801. } else {
  802. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  803. }
  804. if (in_array('date', $html->_params)) {
  805. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  806. <span class="glyphicon glyphicon-calendar"></span>
  807. </span>
  808. </div>';
  809. }
  810. else if (in_array('time', $html->_params)) {
  811. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  812. <span class="glyphicon glyphicon-time"></span>
  813. </span>
  814. </div>';
  815. }
  816. if (true == V::get('appendBack', '', $params)
  817. && !in_array('date', $html->_params)
  818. && !in_array('time', $html->_params)
  819. ) {
  820. if ($html->tag == 'input' && $taskPerm == 'W') {
  821. $out = '<div class="input-group show-last-value">' . $out . '<span class="input-group-addon button-appendBack" title="' . htmlspecialchars($fValue) . '"><span class="glyphicon glyphicon-arrow-left"></span></span></div>';
  822. }
  823. }
  824. if ($typeSpecial) {
  825. $tsParams = array();
  826. $tsValue = V::get('typespecialValue', '', $params);
  827. if (!empty($tsValue)) {
  828. $tsParams['typespecialValue'] = $tsValue;
  829. }
  830. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  831. }
  832. return $out;
  833. }
  834. /**
  835. * List table ids by database
  836. *
  837. *
  838. */
  839. public static function GetTablesByDbId($db) {
  840. DEBUG_S(3,'TableAcl_cache',$_SESSION['TableAcl_cache'],__FILE__,__FUNCTION__,__LINE__);
  841. static $_cache;
  842. $return=array();
  843. if (!$_cache) $_cache = array();
  844. if (!empty($_SESSION['TableAcl_cache'])) {
  845. foreach($_SESSION['TableAcl_cache'] as $tableID=>$obj) {
  846. //if($obj->db==$db)
  847. $return[$obj['name']]=$tableID;
  848. }
  849. return $return;
  850. }
  851. return null;
  852. }
  853. /**
  854. * Get column object. Not initialize
  855. * @returns object - column instance if exists else null
  856. *
  857. * static
  858. */
  859. public static function getInstance($idTable) {
  860. static $_cache;
  861. if (!$_cache) $_cache = array();
  862. if (array_key_exists($idTable, $_cache)) {
  863. return $_cache[$idTable];
  864. }
  865. if (!empty($_SESSION['TableAcl_cache'][$idTable])) {
  866. $tableAcl = new TableAcl($idTable);
  867. $tableAcl->fromArray($_SESSION['TableAcl_cache'][$idTable]);
  868. $_cache[$idTable] = $tableAcl;
  869. return $_cache[$idTable];
  870. }
  871. return null;
  872. }
  873. public static function buildInstance($idTable, $tableConfig) {
  874. static $_cache;
  875. if (!$_cache) $_cache = array();
  876. if (array_key_exists($idTable, $_cache)) {
  877. return $_cache[$idTable];
  878. }
  879. if (empty($tableConfig)) {
  880. throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
  881. Lib::loadClass('ProcesHelper');
  882. $zasobObj = ProcesHelper::getZasobTableInfo($idTable);
  883. if (!$zasobObj) {
  884. return null;// TODO: throw new Exception("Zasob TABELA ID={$idTable} nie istnieje");
  885. }
  886. $tableConfig['db'] = $zasobObj->P__ID;
  887. $tableConfig['name'] = $zasobObj->DESC;
  888. $tableConfig['label'] = $zasobObj->DESC_PL;
  889. $tableConfig['opis'] = $zasobObj->OPIS;
  890. $userAcl = User::getAcl();
  891. $userPermsForTable = $userAcl->getPermsForTable($idTable);
  892. if (!$userPermsForTable) {
  893. return null;// TODO: throw new Exception("Brak uprawnień do pól Tabeli nr {$idTable} '{$zasobObj->DESC}'");
  894. }
  895. echo'<pre>$userPermsForTable('.$idTable.') ';print_r($userPermsForTable);echo'</pre>';
  896. if(0){// TODO: from UserAcl big query
  897. $foundTbls[$r->ZASOB_PARENT_ID]->addField($r->ID_ZASOB, $r->ZASOB_DESC, $r->ZASOB_OPIS, $r->z__SORT_PRIO, $r->ZASOB_DESC_PL);
  898. $foundTbls[$r->ZASOB_PARENT_ID]->setFieldPerms($r->ID_ZASOB, $r->FORM_TREAT);
  899. $tableConfig['fields'];// $this->_fields
  900. $tableConfig['virtualFieldsIdList'];// $this->_virtualFieldsIdList
  901. //$tableConfig['types'];// $this->_types
  902. }
  903. }
  904. if (empty($tableConfig)) {
  905. throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
  906. }
  907. $obj = new TableAcl($idTable);
  908. $obj->fromArray($tableConfig);
  909. $obj->save();
  910. $_cache[$idTable] = $obj;
  911. return $_cache[$idTable];
  912. }
  913. public function loadSchema() {
  914. if ($this->_schemaLoaded) return;
  915. $srvName = $_SERVER['SERVER_NAME'];
  916. Lib::loadClass('Schema_TableFactory');
  917. $this->_schemaClass = Schema_TableFactory::build($this->_name, $this->_db, $srvName);
  918. $this->_schemaLoaded = true;
  919. }
  920. public function init($force = false) {
  921. $this->loadSchema();
  922. if (empty($this->_fields)) {
  923. $this->_types = array();// clear _types @see $this->isInitialized
  924. $userAcl = User::getAcl();
  925. $fieldsConfig = $userAcl->getPermsForTable($this->_zasobID);
  926. DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__ );
  927. $this->initFieldsFromConfig($fieldsConfig);
  928. //DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $this, __CLASS__, __FUNCTION__, __LINE__ );
  929. }
  930. if ($this->isInitialized() && $force == false) {
  931. return;
  932. }
  933. $ds = $this->getDataSource();
  934. $this->_types = $ds->getFieldTypes();
  935. uasort($this->_fields, array($this, 'sortFieldsCallback'));
  936. $this->_fixTypes();
  937. $this->save();
  938. }
  939. public function _fixTypes() {
  940. $this->_fixDateFields();
  941. $this->_sortEnumFields();
  942. $this->_fixProjectType();
  943. if ($this->_schemaClass) $this->_types = $this->_schemaClass->fixTypes($this->_types);
  944. //DBG::_(true, true, "this->_types", $this->_types, __CLASS__, __FUNCTION__, __LINE__);
  945. $fieldIds = array_keys($this->_fields);
  946. Lib::loadClass('Typespecial');
  947. $vColsIdList = Typespecial::initFields($fieldIds);
  948. if (!empty($vColsIdList)) {
  949. $this->_virtualFieldsIdList = $vColsIdList;
  950. }
  951. }
  952. public function initFieldsFromConfig($fieldsConfig) {
  953. foreach ($fieldsConfig as $idField => $vFieldConfig) {
  954. if ((int)$idField <= 0) {
  955. DBG::_('DBG_SCH', '1', "BUG key must be integer - skipping '{$idField}'", $vFieldConfig, __CLASS__, __FUNCTION__, __LINE__ );
  956. trigger_error("BUG " . __CLASS__ . "->" . __FUNCTION__ . "(\$fieldsConfig) key must be integer - skipping '{$idField}'", E_USER_NOTICE);
  957. continue;
  958. }
  959. //echo'<pre>INIT::$permField('.$vFieldConfig->ID_CELL.') hasFld('.$this->hasField($vFieldConfig->ID_CELL).') ';echo'</pre>';
  960. if (!$this->hasField($vFieldConfig['ID_CELL'])) {
  961. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') addFld('.$vFieldConfig['ID_CELL'] . ', ' . $vFieldConfig['CELL_NAME'] . ', ' . $vFieldConfig['CELL_DESC'] . ', ' . $vFieldConfig['SORT_PRIO'] . ', ' . $vFieldConfig['CELL_LABEL'].') ';echo'</pre>';
  962. $this->addField($vFieldConfig['ID_CELL'], $vFieldConfig['CELL_NAME'], $vFieldConfig['CELL_DESC'], $vFieldConfig['SORT_PRIO'], $vFieldConfig['CELL_LABEL']);
  963. }
  964. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') hasFld('.$this->hasField($vFieldConfig['ID_CELL']).') ';echo'</pre>';
  965. if (!isset($vFieldConfig['FORM_TREAT'])) {// TODO: convert to legacy perms
  966. $vFieldConfig['FORM_TREAT'] = '';
  967. if ($vFieldConfig['PERM_R'] > 0) $vFieldConfig['FORM_TREAT'] .= 'R';
  968. if ($vFieldConfig['PERM_W'] > 0) $vFieldConfig['FORM_TREAT'] .= 'W';
  969. if ($vFieldConfig['PERM_X'] > 0) $vFieldConfig['FORM_TREAT'] .= 'X';
  970. if ($vFieldConfig['PERM_C'] > 0) $vFieldConfig['FORM_TREAT'] .= 'C';
  971. if ($vFieldConfig['PERM_S'] > 0) $vFieldConfig['FORM_TREAT'] .= 'S';
  972. if ($vFieldConfig['PERM_O'] > 0) $vFieldConfig['FORM_TREAT'] .= 'O';
  973. if ($vFieldConfig['PERM_V'] > 0) $vFieldConfig['FORM_TREAT'] .= 'V';
  974. if ($vFieldConfig['PERM_E'] > 0) $vFieldConfig['FORM_TREAT'] .= 'E';
  975. }
  976. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') ';print_r($vFieldConfig);echo'</pre>';
  977. if (!empty($vFieldConfig['FORM_TREAT'])) {
  978. $this->setFieldPerms($vFieldConfig['ID_CELL'], $vFieldConfig['FORM_TREAT']);
  979. }
  980. }
  981. }
  982. private function _fixProjectType() {
  983. $tblName = $this->getName();
  984. $fldName = 'M_DIST_TYPE';
  985. if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
  986. foreach ($this->_fields as $kFldId => $vFld) {
  987. if ($vFld['name'] == $fldName) {
  988. $sqlTypes = array();
  989. if (!empty($this->_types[$fldName])) {
  990. if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
  991. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
  992. }
  993. }
  994. if (!empty($sqlTypes)) {
  995. $allowedTypes = array();
  996. $db = DB::getDB();
  997. $sql = "select z.DESC
  998. from `CRM_LISTA_ZASOBOW` as z
  999. where z.`A_STATUS`='NORMAL'
  1000. and z.`PARENT_ID`={$kFldId}
  1001. order by z.`DESC` asc
  1002. ";
  1003. $res = $db->query($sql);
  1004. while ($r = $db->fetch($res)) {
  1005. if (in_array($r->DESC, $sqlTypes)) {
  1006. $allowedTypes[] = $r->DESC;
  1007. }
  1008. }
  1009. sort($allowedTypes);
  1010. if (!empty($allowedTypes)) {
  1011. $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
  1012. }
  1013. }
  1014. }
  1015. }
  1016. }
  1017. }
  1018. private function _sortEnumFields() {
  1019. foreach ($this->_fields as $kFldId => $vFld) {
  1020. $type = $this->getFieldTypeById($kFldId);
  1021. if (!empty($type['type'])) {
  1022. if (substr($type['type'], 0, 4) == 'enum') {
  1023. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  1024. if (!empty($sqlTypes)) {
  1025. sort($sqlTypes);
  1026. $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
  1027. }
  1028. }
  1029. }
  1030. }
  1031. }
  1032. private function _fixDateFields() {
  1033. foreach ($this->_types as $kFldName => $vType) {
  1034. if ($kFldName == 'L_APPOITMENT_DATE') {
  1035. $this->_types[$kFldName]['type'] = 'datetime';
  1036. } else if ($kFldName == 'A_PROBLEM_DATE') {
  1037. $this->_types[$kFldName]['type'] = 'datetime';
  1038. }
  1039. }
  1040. }
  1041. public function getUniqueKeys() {// TODO: RM NOT USED?
  1042. $sqlKeys = array();
  1043. $dbID = $this->getDB();
  1044. $tblName = $this->getName();
  1045. $db = DB::getDB($dbID);
  1046. if (!$db) {
  1047. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  1048. }
  1049. $sql = "SHOW KEYS FROM `{$tblName}`";
  1050. $res = $db->query($sql);
  1051. while ($r = $db->fetch($res)) {
  1052. if ($r->Non_unique == '0') {
  1053. $sqlKeys[$r->Column_name] = true;
  1054. }
  1055. }
  1056. $sqlKeys = array_keys($sqlKeys);
  1057. return $sqlKeys;
  1058. }
  1059. public function sortFieldsCallback($a, $b) {
  1060. if ($a['name'] == 'ID') {
  1061. return -1;
  1062. }
  1063. else if ($b['name'] == 'ID') {
  1064. return 1;
  1065. }
  1066. else if ($a['sort_prio'] < $b['sort_prio']) {
  1067. return -1;
  1068. }
  1069. else if ($a['sort_prio'] > $b['sort_prio']) {
  1070. return 1;
  1071. }
  1072. else {
  1073. return 0;
  1074. }
  1075. }
  1076. public function isInitialized() {
  1077. return (!empty($this->_types));
  1078. }
  1079. /**
  1080. * Save data in session cache.
  1081. */
  1082. function save() {
  1083. $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
  1084. }
  1085. public function getFieldTypeById($fieldID) {
  1086. if (!array_key_exists($fieldID, $this->_fields)) {
  1087. return null;
  1088. }
  1089. $colName = $this->_fields[$fieldID]['name'];
  1090. if (!array_key_exists($colName, $this->_types)) {
  1091. return null;
  1092. }
  1093. return $this->_types[$colName];
  1094. }
  1095. public function getFieldType($colName) {
  1096. if (!array_key_exists($colName, $this->_types)) {
  1097. return null;
  1098. }
  1099. return $this->_types[$colName];
  1100. }
  1101. public function hasFieldType($colName) {
  1102. if (array_key_exists($colName, $this->_types)) {
  1103. return true;
  1104. }
  1105. return false;
  1106. }
  1107. public function getVisibleFieldList() {
  1108. $cols = array();
  1109. $id = 0;
  1110. foreach ($this->_fields as $kFieldID => $vField) {
  1111. if ($vField['name'] == 'ID') {
  1112. $id = $kFieldID;
  1113. }
  1114. }
  1115. $cols[$id] = 'ID';
  1116. foreach ($this->_fields as $kFieldID => $vField) {
  1117. if ($vField['name'] == 'ID') {
  1118. continue;
  1119. }
  1120. $cols[$kFieldID] = $vField['name'];
  1121. }
  1122. return $cols;
  1123. }
  1124. public function getExportFieldList() {
  1125. $cols = array();
  1126. $realFlds = $this->getRealFieldList();
  1127. foreach ($realFlds as $vFieldName) {
  1128. $fldId = $this->getFieldIdByName($vFieldName);
  1129. if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
  1130. $cols[] = $vFieldName;
  1131. }
  1132. }
  1133. return $cols;
  1134. }
  1135. /**
  1136. * List of real fields in database.
  1137. */
  1138. public function getRealFieldList() {
  1139. $cols = array();
  1140. $cols[] = 'ID';
  1141. foreach ($this->_fields as $kFieldID => $vField) {
  1142. if ($vField['name'] == 'ID') {
  1143. continue;
  1144. }
  1145. if (array_key_exists($vField['name'], $this->_types)) {
  1146. $cols[] = $vField['name'];
  1147. }
  1148. }
  1149. return $cols;
  1150. }
  1151. public function getRealFieldListByIdZasob() {
  1152. $cols = array();
  1153. foreach ($this->_fields as $idField => $fld) {
  1154. if ($fld['name'] == 'ID') {
  1155. $cols[$idField] = 'ID';
  1156. }
  1157. }
  1158. foreach ($this->_fields as $idField => $fld) {
  1159. if ($fld['name'] == 'ID') continue;
  1160. if (array_key_exists($fld['name'], $this->_types)) {
  1161. $cols[$idField] = $fld['name'];
  1162. }
  1163. }
  1164. return $cols;
  1165. }
  1166. public function getVirtualFieldList() {
  1167. $cols = array();
  1168. foreach ($this->_fields as $kFieldID => $vField) {
  1169. if ($vField['name'] == 'ID') {
  1170. continue;
  1171. }
  1172. if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
  1173. $cols[$kFieldID] = $vField['name'];
  1174. }
  1175. else if (!array_key_exists($vField['name'], $this->_types)) {
  1176. $cols[$kFieldID] = $vField['name'];
  1177. }
  1178. }
  1179. return $cols;
  1180. }
  1181. public function getFieldLabel($fieldID) {
  1182. if (array_key_exists($fieldID, $this->_fields)) {
  1183. if (!empty($this->_fields[$fieldID]['label'])) {
  1184. return $this->_fields[$fieldID]['label'];
  1185. }
  1186. }
  1187. return null;
  1188. }
  1189. public function getFieldOpis($fieldID) {
  1190. if (array_key_exists($fieldID, $this->_fields)) {
  1191. if (!empty($this->_fields[$fieldID]['opis'])) {
  1192. return $this->_fields[$fieldID]['opis'];
  1193. }
  1194. }
  1195. return null;
  1196. }
  1197. public function getTypes() {
  1198. return $this->_types;
  1199. }
  1200. public function fixEmptyValueFromUser($fieldID) {
  1201. $value = '';
  1202. $type = $this->getFieldTypeById($fieldID);
  1203. if ($type) {
  1204. if ($type['type'] == 'date') {
  1205. $value = $type['default'];
  1206. }
  1207. if (substr($type['type'], 0, 3) == 'int'
  1208. || substr($type['type'], 0, 7) == 'tinyint'
  1209. || substr($type['type'], 0, 8) == 'smallint'
  1210. || substr($type['type'], 0, 6) == 'bigint'
  1211. ) {
  1212. $value = intval($type['default']);
  1213. }
  1214. // fix bug when field is unique and is null allowed: change empty string to null
  1215. if ($type['null']) {
  1216. $value = 'NULL';
  1217. }
  1218. // fix bug when field is enum and is set to '0': for php '0' is empty
  1219. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  1220. if (false !== strpos($type['type'], "''")) {
  1221. // enum('', '1','2')
  1222. $value = '';
  1223. } else if (false !== strpos($type['type'], "'0'")) {
  1224. // enum('0', '1','2')
  1225. $value = '0';
  1226. } else {
  1227. $value = $type['default'];
  1228. }
  1229. }
  1230. }
  1231. return $value;
  1232. }
  1233. public function fromArray($arr) {
  1234. $this->_db = $arr['db'];
  1235. $this->_name = $arr['name'];
  1236. $this->_label = $arr['label'];
  1237. $this->_opis = $arr['opis'];
  1238. $this->_fields = V::get('fields', array(), $arr);
  1239. $this->_virtualFieldsIdList = V::get('virtualFieldsIdList', array(), $arr);
  1240. $this->_types = V::get('types', array(), $arr);
  1241. }
  1242. public function toArray() {
  1243. $arr = array();
  1244. $arr['db'] = $this->_db;
  1245. $arr['name'] = $this->_name;
  1246. $arr['label'] = $this->_label;
  1247. $arr['opis'] = $this->_opis;
  1248. $arr['fields'] = $this->_fields;
  1249. $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
  1250. $arr['types'] = $this->_types;
  1251. return $arr;
  1252. }
  1253. public function convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f') {
  1254. $item = array();
  1255. $fields = $this->getFields();
  1256. foreach ($fields as $kID => $vField) {
  1257. $vFieldName = $vField['name'];
  1258. if (array_key_exists("f{$kID}", $args)) {
  1259. $value = $args["f{$kID}"];
  1260. if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
  1261. $value = $this->fixEmptyValueFromUser($kID);
  1262. }
  1263. $item[$vFieldName] = $value;
  1264. }
  1265. }
  1266. return $item;
  1267. }
  1268. public function getItem($id) {
  1269. $ds = $this->getDataSource();
  1270. return $ds->getItem($id);
  1271. }
  1272. public function getItems($params = array()) {
  1273. $ds = $this->getDataSource();
  1274. return $ds->getItems($params);
  1275. }
  1276. public function getTotal($params) {
  1277. $ds = $this->getDataSource();
  1278. return $ds->getTotal($params);
  1279. }
  1280. public function getColDefault($fieldName) {
  1281. $ds = $this->getDataSource();
  1282. return $ds->getColDefault($fieldName);
  1283. }
  1284. public function getSpecialFilters() {
  1285. $ds = $this->getDataSource();
  1286. return $ds->getSpecialFilters();
  1287. }
  1288. public function getGeomFields() {
  1289. $ds = $this->getDataSource();
  1290. return $ds->getGeomFields();
  1291. }
  1292. public function isGeomField($fldName) {
  1293. $ds = $this->getDataSource();
  1294. return $ds->isGeomField($fldName);
  1295. }
  1296. public function getGeomFieldType($fldName) {
  1297. $dbGeomType = $this->getFieldType($fldName);
  1298. $dbGeomType = (!empty($dbGeomType['type']))? $dbGeomType['type'] : '';
  1299. $geomType = strtolower($dbGeomType);
  1300. return $geomType;
  1301. }
  1302. public function getHistItems($id) {
  1303. $ds = $this->getDataSource();
  1304. return $ds->getHistItems($id);
  1305. }
  1306. public function addItem($itemTodo) {
  1307. if (is_object($itemTodo)) {
  1308. $itemTodo = (array)$itemTodo;
  1309. } else if (!is_array($itemTodo)) {
  1310. throw new HttpException('Item is not array', 400);
  1311. }
  1312. if (empty($itemTodo)) {
  1313. //throw new Exception('Item patch is empty');
  1314. DBG::_('DBG_DS', '>2', "Item patch is empty", null, __CLASS__, __FUNCTION__, __LINE__);
  1315. return 0;// nothing to insert
  1316. }
  1317. $ds = $this->getDataSource();
  1318. // from convertObjectFromUserInput
  1319. $item = array();
  1320. $fields = $this->getFields();
  1321. foreach ($fields as $kID => $vField) {
  1322. $vFieldName = $vField['name'];
  1323. if (!$this->isAllowed($kID, 'C')) {
  1324. continue;
  1325. }
  1326. if (isset($itemTodo[$vFieldName])) {
  1327. $value = $itemTodo[$vFieldName];
  1328. if (empty($value) && strlen($value) == 0) {// fix bug in input type date and value="0000-00-00"
  1329. $value = $this->fixEmptyValueFromUser($kID);
  1330. }
  1331. $item[$vFieldName] = $value;
  1332. }
  1333. }
  1334. if (empty($item)) {
  1335. throw new Exception("Nothing to add");
  1336. }
  1337. {// add DefaultAclGroup if no create perms ('C')
  1338. $defaultAclGroup = User::getDefaultAclGroup();
  1339. if ($defaultAclGroup) {
  1340. $permFields = array('A_ADM_COMPANY', 'A_CLASSIFIED');
  1341. foreach ($permFields as $permFldName) {
  1342. if (array_key_exists($permFldName, $this->_types)) {
  1343. $permFldId = $this->getFieldIdByName($permFldName);
  1344. if (0 == $permFldId || !$this->isAllowed($permFldId, 'C')) {
  1345. $item[$permFldName] = $defaultAclGroup;
  1346. }
  1347. }
  1348. }
  1349. }
  1350. }
  1351. DBG::_('DBG_DS', '>2', "addItem", $item, __CLASS__, __FUNCTION__, __LINE__);
  1352. return $ds->addItem($item);
  1353. }
  1354. /**
  1355. * @param array $itemPatch
  1356. */
  1357. public function updateItem($itemPatch) {
  1358. if (is_object($itemPatch)) {
  1359. $itemPatch = (array)$itemPatch;
  1360. } else if (!is_array($itemPatch)) {
  1361. throw new HttpException('Item patch is not array', 400);
  1362. }
  1363. if (empty($itemPatch)) {
  1364. //throw new Exception('Item patch is empty');
  1365. return 0;// nothing to change
  1366. }
  1367. $ds = $this->getDataSource();
  1368. $primaryKeyField = $ds->getPrimaryKeyField();
  1369. if (empty($itemPatch[$primaryKeyField])) {
  1370. throw new HttpException("Item Primary Key not set!", 400);
  1371. }
  1372. $primaryKey = $itemPatch[$primaryKeyField];
  1373. $itemOld = $this->getItem($primaryKey);
  1374. if (!$itemOld) {
  1375. throw new HttpException("Item not exists!", 404);
  1376. }
  1377. if (!$this->canWriteRecord($itemOld) && !$this->hasPermSuperWrite()) {
  1378. throw new HttpException("Brak dostępu do rekordu", 403);
  1379. }
  1380. // $itemPatch from user input to $itemPatchChecked
  1381. $itemPatchChecked = array();
  1382. $fields = $this->getFields();
  1383. foreach ($fields as $kID => $vField) {
  1384. $vFieldName = $vField['name'];
  1385. if (!$this->isAllowed($kID, 'W', $itemOld)) {
  1386. continue;
  1387. }
  1388. if (array_key_exists($vFieldName, $itemPatch)) {
  1389. if (!$this->isAllowed($kID, 'R', $itemOld) && '*****' == $itemPatch[$vFieldName]) {
  1390. // default value for perms 'W' without 'R' is '*****'
  1391. }
  1392. else {
  1393. $value = $itemPatch[$vFieldName];
  1394. if (empty($itemPatch[$vFieldName]) && strlen($itemPatch[$vFieldName]) == 0) {// fix bug in input type date and value="0000-00-00"
  1395. $value = $this->fixEmptyValueFromUser($kID);
  1396. }
  1397. if ($value != $itemOld->$vFieldName) {
  1398. $itemPatchChecked[$vFieldName] = $value;
  1399. }
  1400. }
  1401. }
  1402. }
  1403. if (empty($itemPatchChecked)) {
  1404. //throw new HttpException("Item Primary Key not set!", 400);
  1405. return 0;// nothing to change
  1406. }
  1407. $itemPatchChecked[$primaryKeyField] = $primaryKey;
  1408. $affected = $ds->updateItem($itemPatchChecked);
  1409. return $affected;
  1410. }
  1411. public function createItemCopy($item) {
  1412. $ds = $this->getDataSource();
  1413. $types = $this->getTypes();
  1414. $uniqKeys = $ds->getUniqueKeys();// TODO: getUniqueFields
  1415. $primaryKeyField = $ds->getPrimaryKeyField();
  1416. $itemCopy = new stdClass();
  1417. foreach ($types as $kName => $vType) {
  1418. if ($kName == $primaryKeyField) {
  1419. continue;
  1420. } else if (in_array($kName, array('A_RECORD_UPDATE_AUTHOR','A_RECORD_UPDATE_DATE'))) {
  1421. continue;
  1422. }
  1423. $value = V::get($kName, '', $item);
  1424. if (in_array($kName, $uniqKeys)) {
  1425. $value .= '?';
  1426. }
  1427. if ($ds->isGeomField($kName) && !empty($value)) {
  1428. $value = "GeomFromText('{$value}')";
  1429. }
  1430. $itemCopy->{$kName} = $value;
  1431. }
  1432. return $itemCopy;
  1433. }
  1434. public function getExportDataSource($cols = array()) {
  1435. $exportFieldList = $this->getExportFieldList();
  1436. if (!empty($cols)) {
  1437. $fltrExportFlds = array();
  1438. foreach ($exportFieldList as $fldName) {
  1439. if (in_array($fldName, $cols)) {
  1440. $fltrExportFlds[] = $fldName;
  1441. }
  1442. }
  1443. $exportFieldList = $fltrExportFlds;
  1444. }
  1445. $dataSource = $this->_getDataSource($exportFieldList);
  1446. return $dataSource;
  1447. }
  1448. public function getDataSource() {
  1449. $realFieldList = $this->getRealFieldList();
  1450. $dataSource = $this->_getDataSource($realFieldList);
  1451. $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
  1452. $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
  1453. $dataSource->setFieldOwner('L_APPOITMENT_USER', $this->hasFieldType('L_APPOITMENT_USER'));
  1454. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  1455. foreach ($adminFields as $vAdmFld) {
  1456. if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
  1457. $dataSource->addCol($vAdmFld);
  1458. }
  1459. }
  1460. return $dataSource;
  1461. }
  1462. private function _getDataSource($cols) {
  1463. Lib::loadClass('DataSourceFactory');
  1464. $dsConfig = array();
  1465. $dsConfig['source_id'] = $this->getDB();
  1466. $dsConfig['object_name'] = $this->getName();
  1467. $dsConfig['fields'] = $cols;
  1468. $dsConfig['field_types'] = $this->getTypes();
  1469. $dsConfig['fields_virtual'] = $this->getVirtualFieldList();
  1470. $dsConfig['acl_fltr_allowed'] = !$this->hasSuperAccessPerms();
  1471. return DataSourceFactory::buildFromZasobInfo($dsConfig);
  1472. }
  1473. public function getPrimaryKeyField() {
  1474. $ds = $this->getDataSource();
  1475. return $ds->getPrimaryKeyField();
  1476. }
  1477. public function isIntegerField($fldName) {
  1478. $type = $this->getFieldType($fldName);
  1479. if (!$type) return false;
  1480. if (substr($type['type'], 0, 3) == 'int'
  1481. || substr($type['type'], 0, 7) == 'tinyint'
  1482. || substr($type['type'], 0, 8) == 'smallint'
  1483. || substr($type['type'], 0, 9) == 'mediumint'
  1484. || substr($type['type'], 0, 6) == 'bigint'
  1485. ) {
  1486. return true;
  1487. }
  1488. return false;
  1489. }
  1490. public function isDecimalField($fldName) {
  1491. $type = $this->getFieldType($fldName);
  1492. if (!$type) return false;
  1493. if (substr($type['type'], 0, 7) == 'decimal'
  1494. || substr($type['type'], 0, 7) == 'numeric'
  1495. || substr($type['type'], 0, 6) == 'double'
  1496. || substr($type['type'], 0, 5) == 'float'
  1497. || substr($type['type'], 0, 4) == 'real'
  1498. ) {
  1499. return true;
  1500. }
  1501. return false;
  1502. }
  1503. public function isDateField($fldName) {
  1504. $type = $this->getFieldType($fldName);
  1505. if (!$type) return false;
  1506. if (substr($type['type'], 0, 4) == 'date' && substr($type['type'], 0, 8) != 'datetime') {
  1507. return true;
  1508. }
  1509. return false;
  1510. }
  1511. public function isDateTimeField($fldName) {
  1512. $type = $this->getFieldType($fldName);
  1513. if (!$type) return false;
  1514. if (substr($type['type'], 0, 8) == 'datetime') {
  1515. return true;
  1516. }
  1517. return false;
  1518. }
  1519. public function isStringField($fldName) {
  1520. $type = $this->getFieldType($fldName);
  1521. if (!$type) return false;
  1522. if (substr($type['type'], 0, 7) == 'varchar'
  1523. || substr($colType['type'], 0, 4) == 'char'
  1524. ) {
  1525. return true;
  1526. }
  1527. return false;
  1528. }
  1529. public function isTextField($fldName) {
  1530. $type = $this->getFieldType($fldName);
  1531. if (!$type) return false;
  1532. if (substr($type['type'], 0, 4) == 'text'
  1533. || substr($type['type'], 0, 8) == 'tinytext'
  1534. || substr($type['type'], 0, 10) == 'mediumtext'
  1535. || substr($type['type'], 0, 8) == 'longtext'
  1536. ) {
  1537. return true;
  1538. }
  1539. return false;
  1540. }
  1541. public function isBinaryField($fldName) {
  1542. $type = $this->getFieldType($fldName);
  1543. if (!$type) return false;
  1544. if (substr($type['type'], 0, 4) == 'blob'
  1545. || substr($type['type'], 0, 8) == 'tinyblob'
  1546. || substr($type['type'], 0, 10) == 'mediumblob'
  1547. || substr($type['type'], 0, 8) == 'longblob'
  1548. ) {
  1549. return true;
  1550. }
  1551. return false;
  1552. }
  1553. public function isEnumerationField($fldName) {
  1554. $type = $this->getFieldType($fldName);
  1555. if (!$type) return false;
  1556. if (substr($type['type'], 0, 4) == 'enum') {
  1557. return true;
  1558. }
  1559. return false;
  1560. }
  1561. public function getEnumerations($fldName) {
  1562. $enum = array();
  1563. $type = $this->getFieldType($fldName);
  1564. if (!$type) return $enum;
  1565. if (!$this->isEnumerationField($fldName)) return $enum;
  1566. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  1567. foreach ($values as $val) {
  1568. $enum[$val] = $val;
  1569. }
  1570. return $enum;
  1571. }
  1572. public function getAttributesFromZasoby() {
  1573. $attributes = array();
  1574. $db = DB::getDB();
  1575. $sql = "
  1576. select `ID` as `id_zasob`
  1577. , `DESC` as `field_name`
  1578. , `DESC_PL` as `label`
  1579. , `OPIS` as `description`
  1580. from `CRM_LISTA_ZASOBOW`
  1581. where `PARENT_ID`={$this->_zasobID}
  1582. and `TYPE`='KOMORKA'
  1583. ";
  1584. $res = $db->query($sql);
  1585. while ($r = $db->fetch($res)) {
  1586. $fldAttr = array();
  1587. $fldAttr['id_zasob'] = $r->id_zasob;
  1588. if (!empty($r->label)) $fldAttr['label'] = $r->label;
  1589. if (!empty($r->description)) $fldAttr['description'] = $r->description;
  1590. $attributes[$r->field_name] = $fldAttr;
  1591. }
  1592. // fetch field values
  1593. $sql = "
  1594. select z.`ID` as `id_zasob`
  1595. , z.`DESC` as `field_name`
  1596. , v.`DESC` as `value`
  1597. , v.`DESC_PL` as `label`
  1598. -- , v.`OPIS` as `description`
  1599. from `CRM_LISTA_ZASOBOW` z
  1600. join `CRM_LISTA_ZASOBOW` v on(v.`PARENT_ID` = z.`ID`)
  1601. where z.`PARENT_ID`={$this->_zasobID}
  1602. and z.`TYPE`='KOMORKA'
  1603. ";
  1604. $res = $db->query($sql);
  1605. while ($r = $db->fetch($res)) {
  1606. if (!empty($r->value) && !empty($r->label)) {
  1607. $valuesMap = V::get('valuesMap', array(), $attributes[$r->field_name]);
  1608. $valuesMap[$r->value] = $r->label;
  1609. $attributes[$r->field_name]['valuesMap'] = $valuesMap;
  1610. }
  1611. }
  1612. return $attributes;
  1613. }
  1614. public function getXsdFieldType($fldName) {// @returns string - xsd type, TODO: throw Exception when field not found
  1615. $fldType = 'xsd:string';
  1616. if ($this->isIntegerField($fldName)) return 'xsd:integer';
  1617. else if ($this->isDecimalField($fldName)) return 'xsd:decimal';
  1618. else if ($this->isDateField($fldName)) return 'xsd:date';
  1619. else if ($this->isDateTimeField($fldName)) return 'xsd:dateTime';
  1620. else if ($this->isGeomField($fldName)) {
  1621. //$fldType = 'gml:GeometryPropertyType';
  1622. $geomType = $this->getGeomFieldType($fldName);
  1623. if ('polygon' == $geomType) return 'gml:PolygonPropertyType';
  1624. else if ('point' == $geomType) return 'gml:PointPropertyType';
  1625. else if ('linestring' == $geomType) return 'gml:LineStringPropertyType';
  1626. else return 'gml:GeometryPropertyType';
  1627. }
  1628. else if ($this->isEnumerationField($fldName)) return 'xsd:string';
  1629. else if ($this->isBinaryField($fldName)) return 'xsd:base64Binary';
  1630. return $fldType;
  1631. }
  1632. }