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