TableAcl.php 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  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 (substr($colType['type'], 0, 4) == 'enum') {
  478. unset($html->attrs['type']);
  479. unset($html->attrs['value']);
  480. $html->tag = 'select';
  481. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  482. $selValue = $fValue;
  483. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  484. if ($taskPerm == 'C') {
  485. $selValue = $colType['default'];
  486. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  487. $selValue = $colType['default'];
  488. }
  489. }
  490. $html->cnt .= '<option value="">' . "" . '</option>';
  491. if (!empty($selValue) && !in_array($selValue, $values)) {
  492. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  493. }
  494. foreach ($values as $val) {
  495. $sel = ($selValue == $val)? ' selected="selected"' : '';
  496. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  497. }
  498. }
  499. else if (substr($colType['type'], 0, 4) == 'text'
  500. || substr($colType['type'], 0, 8) == 'tinytext'
  501. || substr($colType['type'], 0, 10) == 'mediumtext'
  502. || substr($colType['type'], 0, 8) == 'longtext'
  503. ) {
  504. $html->tag = 'textarea';
  505. $html->cnt = htmlspecialchars($fValue);
  506. if (isset($params['widthClass'])) {
  507. if ($params['widthClass'] == 'inside-modal') {
  508. $html->attrs['style'] = 'width:98%;';
  509. } else {
  510. $html->attrs['style'] = 'width:98%;';
  511. }
  512. } else {
  513. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  514. }
  515. $html->attrs['rows'] = '3';
  516. unset($html->attrs['type']);
  517. unset($html->attrs['value']);
  518. }
  519. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  520. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  521. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  522. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  523. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  524. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  525. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  526. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  527. else throw new Exception("unknown Type '{$colType['type']}'");
  528. $html->attrs['class'][] = 'form-control';
  529. $attrsOut = array();
  530. foreach ($html->attrs as $k => $v) {
  531. if (is_array($v)) $v = implode(' ', $v);
  532. $attrsOut[] = "{$k}=\"{$v}\"";
  533. }
  534. if (in_array($html->tag, array('select', 'textarea'))) {
  535. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  536. $out .= $html->cnt;
  537. $out .= '</' . $html->tag . '>';
  538. } else {
  539. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  540. }
  541. if (in_array('date', $html->_params)) {
  542. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  543. <span class="glyphicon glyphicon-calendar"></span>
  544. </span>
  545. </div>';
  546. }
  547. else if (in_array('time', $html->_params)) {
  548. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  549. <span class="glyphicon glyphicon-time"></span>
  550. </span>
  551. </div>';
  552. }
  553. if (true == V::get('appendBack', '', $params)
  554. && !in_array('date', $html->_params)
  555. && !in_array('time', $html->_params)
  556. ) {
  557. if ($html->tag == 'input' && $taskPerm == 'W') {
  558. $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>';
  559. }
  560. }
  561. Lib::loadClass('Typespecial');
  562. $typeSpecial = Typespecial::getInstance($fieldID, $colName);
  563. if ($typeSpecial) {
  564. throw new Exception("TODO: TypeSpecial");
  565. $tsParams = array();
  566. $tsValue = V::get('typespecialValue', '', $params);
  567. if (!empty($tsValue)) {
  568. $tsParams['typespecialValue'] = $tsValue;
  569. }
  570. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  571. }
  572. return $out;
  573. }
  574. /**
  575. * @param $taskPerm - 'C', 'W'
  576. */
  577. public function showFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
  578. $out = '';
  579. if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
  580. if ($taskPerm == 'R') {
  581. $out .= 'Brak uprawnień do odczytu';
  582. }
  583. else if ($taskPerm == 'W') {
  584. $out .= 'Brak uprawnień do zapisu';
  585. } else {
  586. $out .= 'Brak uprawnień do tego pola (' . $taskPerm . ')';
  587. }
  588. return $out;
  589. }
  590. $colName = $this->_fields[$fieldID]['name'];
  591. if ($colName == 'ID') {
  592. return $out;
  593. }
  594. $colType = $this->getFieldTypeById($fieldID);
  595. if (!$colType) {
  596. $out .= 'Error - unknown type';
  597. return $out;
  598. }
  599. Lib::loadClass('Typespecial');
  600. $typeSpecial = Typespecial::getInstance($fieldID, $colName);
  601. $html = new stdClass();
  602. $html->_params = array();
  603. $html->tag = 'input';
  604. $html->cnt = '';
  605. $html->attrs = array();
  606. $html->attrs['id'] = $fName;
  607. $html->attrs['name'] = $fName;
  608. $html->attrs['type'] = 'text';
  609. $html->attrs['value'] = htmlspecialchars($fValue);
  610. if (isset($params['tabindex'])) {
  611. $html->attrs['tabindex'] = $params['tabindex'];
  612. }
  613. if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
  614. $html->attrs['disabled'] = 'disabled';
  615. }
  616. $maxGrid = V::get('maxGrid', 10, $params);
  617. if (substr($colType['type'], 0, 3) == 'int'
  618. || substr($colType['type'], 0, 7) == 'tinyint'
  619. || substr($colType['type'], 0, 8) == 'smallint'
  620. || substr($colType['type'], 0, 6) == 'bigint'
  621. ) {
  622. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
  623. $html->attrs['type'] = 'number';
  624. $html->attrs['class'][] = 'input-small';
  625. }
  626. else if (substr($colType['type'], 0, 6) == 'double') {
  627. $html->attrs['type'] = 'text';
  628. $html->attrs['class'][] = 'input-small';
  629. }
  630. else if (substr($colType['type'], 0, 7) == 'decimal') {
  631. $html->attrs['type'] = 'text';
  632. $html->attrs['class'][] = 'input-small';
  633. }
  634. else if (substr($colType['type'], 0, 7) == 'varchar'
  635. || substr($colType['type'], 0, 4) == 'char'
  636. ) {
  637. //$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
  638. $html->attrs['type'] = 'text';
  639. $maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
  640. if ($maxLength > 0) {
  641. $html->attrs['maxlength'] = $maxLength;
  642. }
  643. $valLength = strlen($fValue);
  644. if (isset($params['widthClass'])) {
  645. if ($params['widthClass'] == 'inside-modal') {
  646. $html->attrs['style'] = 'width:98%;';
  647. } else {
  648. $html->attrs['style'] = 'width:98%;';
  649. }
  650. } else {
  651. /*
  652. if ($maxLength < 11) {
  653. $html->attrs['class'][] = 'span2';
  654. } else if ($maxLength < 31) {
  655. $html->attrs['class'][] = 'span5';
  656. } else if ($maxLength < 51) {
  657. $html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  658. } else if ($maxLength < 101) {
  659. $html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
  660. } else {
  661. $html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
  662. }
  663. */
  664. }
  665. if ($maxLength > 255) {// Fix for long varchar - use textarea
  666. $html->tag = 'textarea';
  667. $html->cnt = htmlspecialchars($fValue);
  668. $html->attrs['rows'] = '3';
  669. unset($html->attrs['type']);
  670. unset($html->attrs['value']);
  671. }
  672. }
  673. else if (substr($colType['type'], 0, 4) == 'date') {
  674. $testDatePicker = true;
  675. if ($testDatePicker) {
  676. $html->attrs['type'] = 'text';
  677. $html->_params[] = 'date';
  678. if (substr($colType['type'], 0, 8) == 'datetime') {
  679. $html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
  680. $html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
  681. $html->attrs['maxlength'] = 19;
  682. } else {
  683. $html->attrs['class'][] = 'se_type-date';// datetimepicker';
  684. $html->attrs['maxlength'] = 10;
  685. }
  686. if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
  687. $html->attrs['value'] = '';
  688. }
  689. } else {
  690. $html->attrs['type'] = 'date';
  691. }
  692. }
  693. else if ($colType['type'] == 'time') {
  694. $testDatePicker = true;
  695. if ($testDatePicker) {
  696. $html->attrs['type'] = 'text';
  697. $html->_params[] = 'time';
  698. $html->attrs['class'][] = 'se_type-time';// datetimepicker';
  699. $html->attrs['data-format'] = 'hh:mm:ss';
  700. $html->attrs['maxlength'] = 8;
  701. if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
  702. $html->attrs['value'] = '';
  703. }
  704. } else {
  705. $html->attrs['type'] = 'time';
  706. }
  707. }
  708. else if (substr($colType['type'], 0, 4) == 'enum') {
  709. unset($html->attrs['type']);
  710. unset($html->attrs['value']);
  711. $html->tag = 'select';
  712. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
  713. $selValue = $fValue;
  714. if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
  715. if ($taskPerm == 'C') {
  716. $selValue = $colType['default'];
  717. } else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
  718. $selValue = $colType['default'];
  719. }
  720. }
  721. $html->cnt .= '<option value="">' . "" . '</option>';
  722. if (!empty($selValue) && !in_array($selValue, $values)) {
  723. $html->cnt .= '<option value="' . $selValue . '" selected="selected">' . $selValue . '</option>';
  724. }
  725. foreach ($values as $val) {
  726. $sel = ($selValue == $val)? ' selected="selected"' : '';
  727. $html->cnt .= '<option value="' . $val . '"' . $sel . '>' . $val . '</option>';
  728. }
  729. }
  730. else if (substr($colType['type'], 0, 4) == 'text'
  731. || substr($colType['type'], 0, 8) == 'tinytext'
  732. || substr($colType['type'], 0, 10) == 'mediumtext'
  733. || substr($colType['type'], 0, 8) == 'longtext'
  734. ) {
  735. $html->tag = 'textarea';
  736. $html->cnt = htmlspecialchars($fValue);
  737. if (isset($params['widthClass'])) {
  738. if ($params['widthClass'] == 'inside-modal') {
  739. $html->attrs['style'] = 'width:98%;';
  740. } else {
  741. $html->attrs['style'] = 'width:98%;';
  742. }
  743. } else {
  744. //$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
  745. }
  746. $html->attrs['rows'] = '3';
  747. unset($html->attrs['type']);
  748. unset($html->attrs['value']);
  749. }
  750. else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
  751. else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
  752. else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
  753. else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
  754. else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
  755. else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
  756. else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
  757. else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
  758. else {
  759. return 'unknown Type "'.$colType['type'].'"';
  760. }
  761. $html->attrs['class'][] = 'form-control';
  762. $attrsOut = array();
  763. foreach ($html->attrs as $k => $v) {
  764. if (is_array($v)) $v = implode(' ', $v);
  765. $attrsOut[] = "{$k}=\"{$v}\"";
  766. }
  767. if (in_array($html->tag, array('select', 'textarea'))) {
  768. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
  769. $out .= $html->cnt;
  770. $out .= '</' . $html->tag . '>';
  771. } else {
  772. $out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
  773. }
  774. if (in_array('date', $html->_params)) {
  775. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  776. <span class="glyphicon glyphicon-calendar"></span>
  777. </span>
  778. </div>';
  779. }
  780. else if (in_array('time', $html->_params)) {
  781. $out = '<div class="input-group">' . $out . '<span class="input-group-addon">
  782. <span class="glyphicon glyphicon-time"></span>
  783. </span>
  784. </div>';
  785. }
  786. if (true == V::get('appendBack', '', $params)
  787. && !in_array('date', $html->_params)
  788. && !in_array('time', $html->_params)
  789. ) {
  790. if ($html->tag == 'input' && $taskPerm == 'W') {
  791. $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>';
  792. }
  793. }
  794. if ($typeSpecial) {
  795. $tsParams = array();
  796. $tsValue = V::get('typespecialValue', '', $params);
  797. if (!empty($tsValue)) {
  798. $tsParams['typespecialValue'] = $tsValue;
  799. }
  800. $out .= ' ' . $typeSpecial->showFormItem($this->_zasobID, $fName, $fValue, $tsParams, $record);
  801. }
  802. return $out;
  803. }
  804. /**
  805. * List table ids by database
  806. *
  807. *
  808. */
  809. public static function GetTablesByDbId($db) {
  810. DEBUG_S(3,'TableAcl_cache',$_SESSION['TableAcl_cache'],__FILE__,__FUNCTION__,__LINE__);
  811. static $_cache;
  812. $return=array();
  813. if (!$_cache) $_cache = array();
  814. if (!empty($_SESSION['TableAcl_cache'])) {
  815. foreach($_SESSION['TableAcl_cache'] as $tableID=>$obj) {
  816. //if($obj->db==$db)
  817. $return[$obj['name']]=$tableID;
  818. }
  819. return $return;
  820. }
  821. return null;
  822. }
  823. /**
  824. * Get column object. Not initialize
  825. * @returns object - column instance if exists else null
  826. *
  827. * static
  828. */
  829. public static function getInstance($idTable) {
  830. static $_cache;
  831. if (!$_cache) $_cache = array();
  832. if (array_key_exists($idTable, $_cache)) {
  833. return $_cache[$idTable];
  834. }
  835. if (!empty($_SESSION['TableAcl_cache'][$idTable])) {
  836. $tableAcl = new TableAcl($idTable);
  837. $tableAcl->fromArray($_SESSION['TableAcl_cache'][$idTable]);
  838. $_cache[$idTable] = $tableAcl;
  839. return $_cache[$idTable];
  840. }
  841. return null;
  842. }
  843. public static function buildInstance($idTable, $tableConfig) {
  844. static $_cache;
  845. if (!$_cache) $_cache = array();
  846. if (array_key_exists($idTable, $_cache)) {
  847. return $_cache[$idTable];
  848. }
  849. if (empty($tableConfig)) {
  850. throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
  851. Lib::loadClass('ProcesHelper');
  852. $zasobObj = ProcesHelper::getZasobTableInfo($idTable);
  853. if (!$zasobObj) {
  854. return null;// TODO: throw new Exception("Zasob TABELA ID={$idTable} nie istnieje");
  855. }
  856. $tableConfig['db'] = $zasobObj->P__ID;
  857. $tableConfig['name'] = $zasobObj->DESC;
  858. $tableConfig['label'] = $zasobObj->DESC_PL;
  859. $tableConfig['opis'] = $zasobObj->OPIS;
  860. $userAcl = User::getAcl();
  861. $userPermsForTable = $userAcl->getPermsForTable($idTable);
  862. if (!$userPermsForTable) {
  863. return null;// TODO: throw new Exception("Brak uprawnień do pól Tabeli nr {$idTable} '{$zasobObj->DESC}'");
  864. }
  865. echo'<pre>$userPermsForTable('.$idTable.') ';print_r($userPermsForTable);echo'</pre>';
  866. if(0){// TODO: from UserAcl big query
  867. $foundTbls[$r->ZASOB_PARENT_ID]->addField($r->ID_ZASOB, $r->ZASOB_DESC, $r->ZASOB_OPIS, $r->z__SORT_PRIO, $r->ZASOB_DESC_PL);
  868. $foundTbls[$r->ZASOB_PARENT_ID]->setFieldPerms($r->ID_ZASOB, $r->FORM_TREAT);
  869. $tableConfig['fields'];// $this->_fields
  870. $tableConfig['virtualFieldsIdList'];// $this->_virtualFieldsIdList
  871. //$tableConfig['types'];// $this->_types
  872. }
  873. }
  874. if (empty($tableConfig)) {
  875. throw new Exception("Brak danych konfiguracyjnych do tabeli nr {$idTable} #TACL" . __LINE__);
  876. }
  877. $obj = new TableAcl($idTable);
  878. $obj->fromArray($tableConfig);
  879. $obj->save();
  880. $_cache[$idTable] = $obj;
  881. return $_cache[$idTable];
  882. }
  883. public function loadSchema() {
  884. if ($this->_schemaLoaded) return;
  885. $srvName = $_SERVER['SERVER_NAME'];
  886. Lib::loadClass('Schema_TableFactory');
  887. $this->_schemaClass = Schema_TableFactory::build($this->_name, $this->_db, $srvName);
  888. $this->_schemaLoaded = true;
  889. }
  890. public function init($force = false) {
  891. $this->loadSchema();
  892. if (empty($this->_fields)) {
  893. $this->_types = array();// clear _types @see $this->isInitialized
  894. $userAcl = User::getAcl();
  895. $fieldsConfig = $userAcl->getPermsForTable($this->_zasobID);
  896. DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__ );
  897. $this->initFieldsFromConfig($fieldsConfig);
  898. //DBG::_('DBG_SCH', '1', "INIT::\$fieldsConfig({$this->_zasobID}) fields(".count($this->_fields).")", $this, __CLASS__, __FUNCTION__, __LINE__ );
  899. }
  900. if ($this->isInitialized() && $force == false) {
  901. return;
  902. }
  903. $ds = $this->getDataSource();
  904. $this->_types = $ds->getFieldTypes();
  905. uasort($this->_fields, array($this, 'sortFieldsCallback'));
  906. $this->_fixTypes();
  907. $this->save();
  908. }
  909. public function _fixTypes() {
  910. $this->_fixDateFields();
  911. $this->_sortEnumFields();
  912. $this->_fixProjectType();
  913. if ($this->_schemaClass) $this->_types = $this->_schemaClass->fixTypes($this->_types);
  914. //DBG::_(true, true, "this->_types", $this->_types, __CLASS__, __FUNCTION__, __LINE__);
  915. $fieldIds = array_keys($this->_fields);
  916. Lib::loadClass('Typespecial');
  917. $vColsIdList = Typespecial::initFields($fieldIds);
  918. if (!empty($vColsIdList)) {
  919. $this->_virtualFieldsIdList = $vColsIdList;
  920. }
  921. }
  922. public function initFieldsFromConfig($fieldsConfig) {
  923. foreach ($fieldsConfig as $idField => $vFieldConfig) {
  924. if ((int)$idField <= 0) {
  925. DBG::_('DBG_SCH', '1', "BUG key must be integer - skipping '{$idField}'", $vFieldConfig, __CLASS__, __FUNCTION__, __LINE__ );
  926. trigger_error("BUG " . __CLASS__ . "->" . __FUNCTION__ . "(\$fieldsConfig) key must be integer - skipping '{$idField}'", E_USER_NOTICE);
  927. continue;
  928. }
  929. //echo'<pre>INIT::$permField('.$vFieldConfig->ID_CELL.') hasFld('.$this->hasField($vFieldConfig->ID_CELL).') ';echo'</pre>';
  930. if (!$this->hasField($vFieldConfig['ID_CELL'])) {
  931. //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>';
  932. $this->addField($vFieldConfig['ID_CELL'], $vFieldConfig['CELL_NAME'], $vFieldConfig['CELL_DESC'], $vFieldConfig['SORT_PRIO'], $vFieldConfig['CELL_LABEL']);
  933. }
  934. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') hasFld('.$this->hasField($vFieldConfig['ID_CELL']).') ';echo'</pre>';
  935. if (!isset($vFieldConfig['FORM_TREAT'])) {// TODO: convert to legacy perms
  936. $vFieldConfig['FORM_TREAT'] = '';
  937. if ($vFieldConfig['PERM_R'] > 0) $vFieldConfig['FORM_TREAT'] .= 'R';
  938. if ($vFieldConfig['PERM_W'] > 0) $vFieldConfig['FORM_TREAT'] .= 'W';
  939. if ($vFieldConfig['PERM_X'] > 0) $vFieldConfig['FORM_TREAT'] .= 'X';
  940. if ($vFieldConfig['PERM_C'] > 0) $vFieldConfig['FORM_TREAT'] .= 'C';
  941. if ($vFieldConfig['PERM_S'] > 0) $vFieldConfig['FORM_TREAT'] .= 'S';
  942. if ($vFieldConfig['PERM_O'] > 0) $vFieldConfig['FORM_TREAT'] .= 'O';
  943. if ($vFieldConfig['PERM_V'] > 0) $vFieldConfig['FORM_TREAT'] .= 'V';
  944. if ($vFieldConfig['PERM_E'] > 0) $vFieldConfig['FORM_TREAT'] .= 'E';
  945. }
  946. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') ';print_r($vFieldConfig);echo'</pre>';
  947. if (!empty($vFieldConfig['FORM_TREAT'])) {
  948. $this->setFieldPerms($vFieldConfig['ID_CELL'], $vFieldConfig['FORM_TREAT']);
  949. }
  950. }
  951. }
  952. private function _fixProjectType() {
  953. $tblName = $this->getName();
  954. $fldName = 'M_DIST_TYPE';
  955. if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
  956. foreach ($this->_fields as $kFldId => $vFld) {
  957. if ($vFld['name'] == $fldName) {
  958. $sqlTypes = array();
  959. if (!empty($this->_types[$fldName])) {
  960. if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
  961. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
  962. }
  963. }
  964. if (!empty($sqlTypes)) {
  965. $allowedTypes = array();
  966. $db = DB::getDB();
  967. $sql = "select z.DESC
  968. from `CRM_LISTA_ZASOBOW` as z
  969. where z.`A_STATUS`='NORMAL'
  970. and z.`PARENT_ID`={$kFldId}
  971. order by z.`DESC` asc
  972. ";
  973. $res = $db->query($sql);
  974. while ($r = $db->fetch($res)) {
  975. if (in_array($r->DESC, $sqlTypes)) {
  976. $allowedTypes[] = $r->DESC;
  977. }
  978. }
  979. sort($allowedTypes);
  980. if (!empty($allowedTypes)) {
  981. $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
  982. }
  983. }
  984. }
  985. }
  986. }
  987. }
  988. private function _sortEnumFields() {
  989. foreach ($this->_fields as $kFldId => $vFld) {
  990. $type = $this->getFieldTypeById($kFldId);
  991. if (!empty($type['type'])) {
  992. if (substr($type['type'], 0, 4) == 'enum') {
  993. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  994. if (!empty($sqlTypes)) {
  995. sort($sqlTypes);
  996. $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
  997. }
  998. }
  999. }
  1000. }
  1001. }
  1002. private function _fixDateFields() {
  1003. foreach ($this->_types as $kFldName => $vType) {
  1004. if ($kFldName == 'L_APPOITMENT_DATE') {
  1005. $this->_types[$kFldName]['type'] = 'datetime';
  1006. } else if ($kFldName == 'A_PROBLEM_DATE') {
  1007. $this->_types[$kFldName]['type'] = 'datetime';
  1008. }
  1009. }
  1010. }
  1011. public function getUniqueKeys() {// TODO: RM NOT USED?
  1012. $sqlKeys = array();
  1013. $dbID = $this->getDB();
  1014. $tblName = $this->getName();
  1015. $db = DB::getDB($dbID);
  1016. if (!$db) {
  1017. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  1018. }
  1019. $sql = "SHOW KEYS FROM `{$tblName}`";
  1020. $res = $db->query($sql);
  1021. while ($r = $db->fetch($res)) {
  1022. if ($r->Non_unique == '0') {
  1023. $sqlKeys[$r->Column_name] = true;
  1024. }
  1025. }
  1026. $sqlKeys = array_keys($sqlKeys);
  1027. return $sqlKeys;
  1028. }
  1029. public function sortFieldsCallback($a, $b) {
  1030. if ($a['name'] == 'ID') {
  1031. return -1;
  1032. }
  1033. else if ($b['name'] == 'ID') {
  1034. return 1;
  1035. }
  1036. else if ($a['sort_prio'] < $b['sort_prio']) {
  1037. return -1;
  1038. }
  1039. else if ($a['sort_prio'] > $b['sort_prio']) {
  1040. return 1;
  1041. }
  1042. else {
  1043. return 0;
  1044. }
  1045. }
  1046. public function isInitialized() {
  1047. return (!empty($this->_types));
  1048. }
  1049. /**
  1050. * Save data in session cache.
  1051. */
  1052. function save() {
  1053. $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
  1054. }
  1055. public function getFieldTypeById($fieldID) {
  1056. if (!array_key_exists($fieldID, $this->_fields)) {
  1057. return null;
  1058. }
  1059. $colName = $this->_fields[$fieldID]['name'];
  1060. if (!array_key_exists($colName, $this->_types)) {
  1061. return null;
  1062. }
  1063. return $this->_types[$colName];
  1064. }
  1065. public function getFieldType($colName) {
  1066. if (!array_key_exists($colName, $this->_types)) {
  1067. return null;
  1068. }
  1069. return $this->_types[$colName];
  1070. }
  1071. public function hasFieldType($colName) {
  1072. if (array_key_exists($colName, $this->_types)) {
  1073. return true;
  1074. }
  1075. return false;
  1076. }
  1077. public function getVisibleFieldList() {
  1078. $cols = array();
  1079. $id = 0;
  1080. foreach ($this->_fields as $kFieldID => $vField) {
  1081. if ($vField['name'] == 'ID') {
  1082. $id = $kFieldID;
  1083. }
  1084. }
  1085. $cols[$id] = 'ID';
  1086. foreach ($this->_fields as $kFieldID => $vField) {
  1087. if ($vField['name'] == 'ID') {
  1088. continue;
  1089. }
  1090. $cols[$kFieldID] = $vField['name'];
  1091. }
  1092. return $cols;
  1093. }
  1094. public function getExportFieldList() {
  1095. $cols = array();
  1096. $realFlds = $this->getRealFieldList();
  1097. foreach ($realFlds as $vFieldName) {
  1098. $fldId = $this->getFieldIdByName($vFieldName);
  1099. if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
  1100. $cols[] = $vFieldName;
  1101. }
  1102. }
  1103. return $cols;
  1104. }
  1105. /**
  1106. * List of real fields in database.
  1107. */
  1108. public function getRealFieldList() {
  1109. $cols = array();
  1110. $cols[] = 'ID';
  1111. foreach ($this->_fields as $kFieldID => $vField) {
  1112. if ($vField['name'] == 'ID') {
  1113. continue;
  1114. }
  1115. if (array_key_exists($vField['name'], $this->_types)) {
  1116. $cols[] = $vField['name'];
  1117. }
  1118. }
  1119. return $cols;
  1120. }
  1121. public function getRealFieldListByIdZasob() {
  1122. $cols = array();
  1123. foreach ($this->_fields as $idField => $fld) {
  1124. if ($fld['name'] == 'ID') {
  1125. $cols[$idField] = 'ID';
  1126. }
  1127. }
  1128. foreach ($this->_fields as $idField => $fld) {
  1129. if ($fld['name'] == 'ID') continue;
  1130. if (array_key_exists($fld['name'], $this->_types)) {
  1131. $cols[$idField] = $fld['name'];
  1132. }
  1133. }
  1134. return $cols;
  1135. }
  1136. public function getVirtualFieldList() {
  1137. $cols = array();
  1138. foreach ($this->_fields as $kFieldID => $vField) {
  1139. if ($vField['name'] == 'ID') {
  1140. continue;
  1141. }
  1142. if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
  1143. $cols[$kFieldID] = $vField['name'];
  1144. }
  1145. else if (!array_key_exists($vField['name'], $this->_types)) {
  1146. $cols[$kFieldID] = $vField['name'];
  1147. }
  1148. }
  1149. return $cols;
  1150. }
  1151. public function getFieldLabel($fieldID) {
  1152. if (array_key_exists($fieldID, $this->_fields)) {
  1153. if (!empty($this->_fields[$fieldID]['label'])) {
  1154. return $this->_fields[$fieldID]['label'];
  1155. }
  1156. }
  1157. return null;
  1158. }
  1159. public function getFieldOpis($fieldID) {
  1160. if (array_key_exists($fieldID, $this->_fields)) {
  1161. if (!empty($this->_fields[$fieldID]['opis'])) {
  1162. return $this->_fields[$fieldID]['opis'];
  1163. }
  1164. }
  1165. return null;
  1166. }
  1167. public function getTypes() {
  1168. return $this->_types;
  1169. }
  1170. public function fixEmptyValueFromUser($fieldID) {
  1171. $value = '';
  1172. $type = $this->getFieldTypeById($fieldID);
  1173. if ($type) {
  1174. if ($type['type'] == 'date') {
  1175. $value = $type['default'];
  1176. }
  1177. if (substr($type['type'], 0, 3) == 'int'
  1178. || substr($type['type'], 0, 7) == 'tinyint'
  1179. || substr($type['type'], 0, 8) == 'smallint'
  1180. || substr($type['type'], 0, 6) == 'bigint'
  1181. ) {
  1182. $value = intval($type['default']);
  1183. }
  1184. // fix bug when field is unique and is null allowed: change empty string to null
  1185. if ($type['null']) {
  1186. $value = 'NULL';
  1187. }
  1188. // fix bug when field is enum and is set to '0': for php '0' is empty
  1189. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  1190. if (false !== strpos($type['type'], "''")) {
  1191. // enum('', '1','2')
  1192. $value = '';
  1193. } else if (false !== strpos($type['type'], "'0'")) {
  1194. // enum('0', '1','2')
  1195. $value = '0';
  1196. } else {
  1197. $value = $type['default'];
  1198. }
  1199. }
  1200. }
  1201. return $value;
  1202. }
  1203. public function fromArray($arr) {
  1204. $this->_db = $arr['db'];
  1205. $this->_name = $arr['name'];
  1206. $this->_label = $arr['label'];
  1207. $this->_opis = $arr['opis'];
  1208. $this->_fields = V::get('fields', array(), $arr);
  1209. $this->_virtualFieldsIdList = V::get('virtualFieldsIdList', array(), $arr);
  1210. $this->_types = V::get('types', array(), $arr);
  1211. }
  1212. public function toArray() {
  1213. $arr = array();
  1214. $arr['db'] = $this->_db;
  1215. $arr['name'] = $this->_name;
  1216. $arr['label'] = $this->_label;
  1217. $arr['opis'] = $this->_opis;
  1218. $arr['fields'] = $this->_fields;
  1219. $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
  1220. $arr['types'] = $this->_types;
  1221. return $arr;
  1222. }
  1223. public function convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f') {
  1224. $item = array();
  1225. $fields = $this->getFields();
  1226. foreach ($fields as $kID => $vField) {
  1227. $vFieldName = $vField['name'];
  1228. if (array_key_exists("f{$kID}", $args)) {
  1229. $value = $args["f{$kID}"];
  1230. if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
  1231. $value = $this->fixEmptyValueFromUser($kID);
  1232. }
  1233. $item[$vFieldName] = $value;
  1234. }
  1235. }
  1236. return $item;
  1237. }
  1238. public function getItem($id) {
  1239. $ds = $this->getDataSource();
  1240. return $ds->getItem($id);
  1241. }
  1242. public function getItems($params = array()) {
  1243. $ds = $this->getDataSource();
  1244. return $ds->getItems($params);
  1245. }
  1246. public function getTotal($params) {
  1247. $ds = $this->getDataSource();
  1248. return $ds->getTotal($params);
  1249. }
  1250. public function getColDefault($fieldName) {
  1251. $ds = $this->getDataSource();
  1252. return $ds->getColDefault($fieldName);
  1253. }
  1254. public function getSpecialFilters() {
  1255. $ds = $this->getDataSource();
  1256. return $ds->getSpecialFilters();
  1257. }
  1258. public function getGeomFields() {
  1259. $ds = $this->getDataSource();
  1260. return $ds->getGeomFields();
  1261. }
  1262. public function isGeomField($fldName) {
  1263. $ds = $this->getDataSource();
  1264. return $ds->isGeomField($fldName);
  1265. }
  1266. public function getGeomFieldType($fldName) {
  1267. $dbGeomType = $this->getFieldType($fldName);
  1268. $dbGeomType = (!empty($dbGeomType['type']))? $dbGeomType['type'] : '';
  1269. $geomType = strtolower($dbGeomType);
  1270. return $geomType;
  1271. }
  1272. public function getHistItems($id) {
  1273. $ds = $this->getDataSource();
  1274. return $ds->getHistItems($id);
  1275. }
  1276. public function addItem($itemTodo) {
  1277. if (is_object($itemTodo)) {
  1278. $itemTodo = (array)$itemTodo;
  1279. } else if (!is_array($itemTodo)) {
  1280. throw new HttpException('Item is not array', 400);
  1281. }
  1282. if (empty($itemTodo)) {
  1283. //throw new Exception('Item patch is empty');
  1284. DBG::_('DBG_DS', '>2', "Item patch is empty", null, __CLASS__, __FUNCTION__, __LINE__);
  1285. return 0;// nothing to insert
  1286. }
  1287. $ds = $this->getDataSource();
  1288. // from convertObjectFromUserInput
  1289. $item = array();
  1290. $fields = $this->getFields();
  1291. foreach ($fields as $kID => $vField) {
  1292. $vFieldName = $vField['name'];
  1293. if (!$this->isAllowed($kID, 'C')) {
  1294. continue;
  1295. }
  1296. if (isset($itemTodo[$vFieldName])) {
  1297. $value = $itemTodo[$vFieldName];
  1298. if (empty($value) && strlen($value) == 0) {// fix bug in input type date and value="0000-00-00"
  1299. $value = $this->fixEmptyValueFromUser($kID);
  1300. }
  1301. $item[$vFieldName] = $value;
  1302. }
  1303. }
  1304. if (empty($item)) {
  1305. throw new Exception("Nothing to add");
  1306. }
  1307. {// add DefaultAclGroup if no create perms ('C')
  1308. $defaultAclGroup = User::getDefaultAclGroup();
  1309. if ($defaultAclGroup) {
  1310. $permFields = array('A_ADM_COMPANY', 'A_CLASSIFIED');
  1311. foreach ($permFields as $permFldName) {
  1312. if (array_key_exists($permFldName, $this->_types)) {
  1313. $permFldId = $this->getFieldIdByName($permFldName);
  1314. if (0 == $permFldId || !$this->isAllowed($permFldId, 'C')) {
  1315. $item[$permFldName] = $defaultAclGroup;
  1316. }
  1317. }
  1318. }
  1319. }
  1320. }
  1321. DBG::_('DBG_DS', '>2', "addItem", $item, __CLASS__, __FUNCTION__, __LINE__);
  1322. return $ds->addItem($item);
  1323. }
  1324. /**
  1325. * @param array $itemPatch
  1326. */
  1327. public function updateItem($itemPatch) {
  1328. if (is_object($itemPatch)) {
  1329. $itemPatch = (array)$itemPatch;
  1330. } else if (!is_array($itemPatch)) {
  1331. throw new HttpException('Item patch is not array', 400);
  1332. }
  1333. if (empty($itemPatch)) {
  1334. //throw new Exception('Item patch is empty');
  1335. return 0;// nothing to change
  1336. }
  1337. $ds = $this->getDataSource();
  1338. $primaryKeyField = $ds->getPrimaryKeyField();
  1339. if (empty($itemPatch[$primaryKeyField])) {
  1340. throw new HttpException("Item Primary Key not set!", 400);
  1341. }
  1342. $primaryKey = $itemPatch[$primaryKeyField];
  1343. $itemOld = $this->getItem($primaryKey);
  1344. if (!$itemOld) {
  1345. throw new HttpException("Item not exists!", 404);
  1346. }
  1347. if (!$this->canWriteRecord($itemOld) && !$this->hasPermSuperWrite()) {
  1348. throw new HttpException("Brak dostępu do rekordu", 403);
  1349. }
  1350. // $itemPatch from user input to $itemPatchChecked
  1351. $itemPatchChecked = array();
  1352. $fields = $this->getFields();
  1353. foreach ($fields as $kID => $vField) {
  1354. $vFieldName = $vField['name'];
  1355. if (!$this->isAllowed($kID, 'W', $itemOld)) {
  1356. continue;
  1357. }
  1358. if (array_key_exists($vFieldName, $itemPatch)) {
  1359. if (!$this->isAllowed($kID, 'R', $itemOld) && '*****' == $itemPatch[$vFieldName]) {
  1360. // default value for perms 'W' without 'R' is '*****'
  1361. }
  1362. else {
  1363. $value = $itemPatch[$vFieldName];
  1364. if (empty($itemPatch[$vFieldName]) && strlen($itemPatch[$vFieldName]) == 0) {// fix bug in input type date and value="0000-00-00"
  1365. $value = $this->fixEmptyValueFromUser($kID);
  1366. }
  1367. if ($value != $itemOld->$vFieldName) {
  1368. $itemPatchChecked[$vFieldName] = $value;
  1369. }
  1370. }
  1371. }
  1372. }
  1373. if (empty($itemPatchChecked)) {
  1374. //throw new HttpException("Item Primary Key not set!", 400);
  1375. return 0;// nothing to change
  1376. }
  1377. $itemPatchChecked[$primaryKeyField] = $primaryKey;
  1378. $affected = $ds->updateItem($itemPatchChecked);
  1379. return $affected;
  1380. }
  1381. public function createItemCopy($item) {
  1382. $ds = $this->getDataSource();
  1383. $types = $this->getTypes();
  1384. $uniqKeys = $ds->getUniqueKeys();// TODO: getUniqueFields
  1385. $primaryKeyField = $ds->getPrimaryKeyField();
  1386. $itemCopy = new stdClass();
  1387. foreach ($types as $kName => $vType) {
  1388. if ($kName == $primaryKeyField) {
  1389. continue;
  1390. } else if (in_array($kName, array('A_RECORD_UPDATE_AUTHOR','A_RECORD_UPDATE_DATE'))) {
  1391. continue;
  1392. }
  1393. $value = V::get($kName, '', $item);
  1394. if (in_array($kName, $uniqKeys)) {
  1395. $value .= '?';
  1396. }
  1397. if ($ds->isGeomField($kName) && !empty($value)) {
  1398. $value = "GeomFromText('{$value}')";
  1399. }
  1400. $itemCopy->{$kName} = $value;
  1401. }
  1402. return $itemCopy;
  1403. }
  1404. public function getExportDataSource($cols = array()) {
  1405. $exportFieldList = $this->getExportFieldList();
  1406. if (!empty($cols)) {
  1407. $fltrExportFlds = array();
  1408. foreach ($exportFieldList as $fldName) {
  1409. if (in_array($fldName, $cols)) {
  1410. $fltrExportFlds[] = $fldName;
  1411. }
  1412. }
  1413. $exportFieldList = $fltrExportFlds;
  1414. }
  1415. $dataSource = $this->_getDataSource($exportFieldList);
  1416. return $dataSource;
  1417. }
  1418. public function getDataSource() {
  1419. $realFieldList = $this->getRealFieldList();
  1420. $dataSource = $this->_getDataSource($realFieldList);
  1421. $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
  1422. $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
  1423. $dataSource->setFieldOwner('L_APPOITMENT_USER', $this->hasFieldType('L_APPOITMENT_USER'));
  1424. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  1425. foreach ($adminFields as $vAdmFld) {
  1426. if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
  1427. $dataSource->addCol($vAdmFld);
  1428. }
  1429. }
  1430. return $dataSource;
  1431. }
  1432. private function _getDataSource($cols) {
  1433. Lib::loadClass('DataSourceFactory');
  1434. $dsConfig = array();
  1435. $dsConfig['source_id'] = $this->getDB();
  1436. $dsConfig['object_name'] = $this->getName();
  1437. $dsConfig['fields'] = $cols;
  1438. $dsConfig['field_types'] = $this->getTypes();
  1439. $dsConfig['fields_virtual'] = $this->getVirtualFieldList();
  1440. $dsConfig['acl_fltr_allowed'] = !$this->hasSuperAccessPerms();
  1441. return DataSourceFactory::buildFromZasobInfo($dsConfig);
  1442. }
  1443. public function getPrimaryKeyField() {
  1444. $ds = $this->getDataSource();
  1445. return $ds->getPrimaryKeyField();
  1446. }
  1447. public function isIntegerField($fldName) {
  1448. $type = $this->getFieldType($fldName);
  1449. if (!$type) return false;
  1450. if (substr($type['type'], 0, 3) == 'int'
  1451. || substr($type['type'], 0, 7) == 'tinyint'
  1452. || substr($type['type'], 0, 8) == 'smallint'
  1453. || substr($type['type'], 0, 9) == 'mediumint'
  1454. || substr($type['type'], 0, 6) == 'bigint'
  1455. ) {
  1456. return true;
  1457. }
  1458. return false;
  1459. }
  1460. public function isDecimalField($fldName) {
  1461. $type = $this->getFieldType($fldName);
  1462. if (!$type) return false;
  1463. if (substr($type['type'], 0, 7) == 'decimal'
  1464. || substr($type['type'], 0, 7) == 'numeric'
  1465. || substr($type['type'], 0, 6) == 'double'
  1466. || substr($type['type'], 0, 5) == 'float'
  1467. || substr($type['type'], 0, 4) == 'real'
  1468. ) {
  1469. return true;
  1470. }
  1471. return false;
  1472. }
  1473. public function isDateField($fldName) {
  1474. $type = $this->getFieldType($fldName);
  1475. if (!$type) return false;
  1476. if (substr($type['type'], 0, 4) == 'date' && substr($type['type'], 0, 8) != 'datetime') {
  1477. return true;
  1478. }
  1479. return false;
  1480. }
  1481. public function isDateTimeField($fldName) {
  1482. $type = $this->getFieldType($fldName);
  1483. if (!$type) return false;
  1484. if (substr($type['type'], 0, 8) == 'datetime') {
  1485. return true;
  1486. }
  1487. return false;
  1488. }
  1489. public function isStringField($fldName) {
  1490. $type = $this->getFieldType($fldName);
  1491. if (!$type) return false;
  1492. if (substr($type['type'], 0, 7) == 'varchar'
  1493. || substr($colType['type'], 0, 4) == 'char'
  1494. ) {
  1495. return true;
  1496. }
  1497. return false;
  1498. }
  1499. public function isTextField($fldName) {
  1500. $type = $this->getFieldType($fldName);
  1501. if (!$type) return false;
  1502. if (substr($type['type'], 0, 4) == 'text'
  1503. || substr($type['type'], 0, 8) == 'tinytext'
  1504. || substr($type['type'], 0, 10) == 'mediumtext'
  1505. || substr($type['type'], 0, 8) == 'longtext'
  1506. ) {
  1507. return true;
  1508. }
  1509. return false;
  1510. }
  1511. public function isBinaryField($fldName) {
  1512. $type = $this->getFieldType($fldName);
  1513. if (!$type) return false;
  1514. if (substr($type['type'], 0, 4) == 'blob'
  1515. || substr($type['type'], 0, 8) == 'tinyblob'
  1516. || substr($type['type'], 0, 10) == 'mediumblob'
  1517. || substr($type['type'], 0, 8) == 'longblob'
  1518. ) {
  1519. return true;
  1520. }
  1521. return false;
  1522. }
  1523. public function isEnumerationField($fldName) {
  1524. $type = $this->getFieldType($fldName);
  1525. if (!$type) return false;
  1526. if (substr($type['type'], 0, 4) == 'enum') {
  1527. return true;
  1528. }
  1529. return false;
  1530. }
  1531. public function getEnumerations($fldName) {
  1532. $enum = array();
  1533. $type = $this->getFieldType($fldName);
  1534. if (!$type) return $enum;
  1535. if (!$this->isEnumerationField($fldName)) return $enum;
  1536. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  1537. foreach ($values as $val) {
  1538. $enum[$val] = $val;
  1539. }
  1540. return $enum;
  1541. }
  1542. public function getAttributesFromZasoby() {
  1543. $attributes = array();
  1544. $db = DB::getDB();
  1545. $sql = "
  1546. select `ID` as `id_zasob`
  1547. , `DESC` as `field_name`
  1548. , `DESC_PL` as `label`
  1549. , `OPIS` as `description`
  1550. from `CRM_LISTA_ZASOBOW`
  1551. where `PARENT_ID`={$this->_zasobID}
  1552. and `TYPE`='KOMORKA'
  1553. ";
  1554. $res = $db->query($sql);
  1555. while ($r = $db->fetch($res)) {
  1556. $fldAttr = array();
  1557. $fldAttr['id_zasob'] = $r->id_zasob;
  1558. if (!empty($r->label)) $fldAttr['label'] = $r->label;
  1559. if (!empty($r->description)) $fldAttr['description'] = $r->description;
  1560. $attributes[$r->field_name] = $fldAttr;
  1561. }
  1562. // fetch field values
  1563. $sql = "
  1564. select z.`ID` as `id_zasob`
  1565. , z.`DESC` as `field_name`
  1566. , v.`DESC` as `value`
  1567. , v.`DESC_PL` as `label`
  1568. -- , v.`OPIS` as `description`
  1569. from `CRM_LISTA_ZASOBOW` z
  1570. join `CRM_LISTA_ZASOBOW` v on(v.`PARENT_ID` = z.`ID`)
  1571. where z.`PARENT_ID`={$this->_zasobID}
  1572. and z.`TYPE`='KOMORKA'
  1573. ";
  1574. $res = $db->query($sql);
  1575. while ($r = $db->fetch($res)) {
  1576. if (!empty($r->value) && !empty($r->label)) {
  1577. $valuesMap = V::get('valuesMap', array(), $attributes[$r->field_name]);
  1578. $valuesMap[$r->value] = $r->label;
  1579. $attributes[$r->field_name]['valuesMap'] = $valuesMap;
  1580. }
  1581. }
  1582. return $attributes;
  1583. }
  1584. public function getXsdFieldType($fldName) {// @returns string - xsd type, TODO: throw Exception when field not found
  1585. $fldType = 'xsd:string';
  1586. if ($this->isIntegerField($fldName)) return 'xsd:integer';
  1587. else if ($this->isDecimalField($fldName)) return 'xsd:decimal';
  1588. else if ($this->isDateField($fldName)) return 'xsd:date';
  1589. else if ($this->isDateTimeField($fldName)) return 'xsd:dateTime';
  1590. else if ($this->isGeomField($fldName)) {
  1591. //$fldType = 'gml:GeometryPropertyType';
  1592. $geomType = $this->getGeomFieldType($fldName);
  1593. if ('polygon' == $geomType) return 'gml:PolygonPropertyType';
  1594. else if ('point' == $geomType) return 'gml:PointPropertyType';
  1595. else if ('linestring' == $geomType) return 'gml:LineStringPropertyType';
  1596. else return 'gml:GeometryPropertyType';
  1597. }
  1598. else if ($this->isEnumerationField($fldName)) return 'xsd:string';
  1599. else if ($this->isBinaryField($fldName)) return 'xsd:base64Binary';
  1600. return $fldType;
  1601. }
  1602. }