TableAcl.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  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. $this->save();
  890. }
  891. public function _fixTypes() {
  892. $this->_fixDateFields();
  893. $this->_sortEnumFields();
  894. $this->_fixProjectType();
  895. if ($this->_schemaClass) $this->_types = $this->_schemaClass->fixTypes($this->_types);
  896. //DBG::_(true, true, "this->_types", $this->_types, __CLASS__, __FUNCTION__, __LINE__);
  897. $fieldIds = array_keys($this->_fields);
  898. Lib::loadClass('Typespecial');
  899. $vColsIdList = Typespecial::initFields($fieldIds);
  900. if (!empty($vColsIdList)) {
  901. $this->_virtualFieldsIdList = $vColsIdList;
  902. }
  903. }
  904. public function initFieldsFromConfig($fieldsConfig) {
  905. foreach ($fieldsConfig as $idField => $vFieldConfig) {
  906. if ((int)$idField <= 0) {
  907. DBG::_('DBG_SCH', '1', "BUG key must be integer - skipping '{$idField}'", $vFieldConfig, __CLASS__, __FUNCTION__, __LINE__ );
  908. trigger_error("BUG " . __CLASS__ . "->" . __FUNCTION__ . "(\$fieldsConfig) key must be integer - skipping '{$idField}'", E_USER_NOTICE);
  909. continue;
  910. }
  911. //echo'<pre>INIT::$permField('.$vFieldConfig->ID_CELL.') hasFld('.$this->hasField($vFieldConfig->ID_CELL).') ';echo'</pre>';
  912. if (!$this->hasField($vFieldConfig['ID_CELL'])) {
  913. //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>';
  914. $this->addField($vFieldConfig['ID_CELL'], $vFieldConfig['CELL_NAME'], $vFieldConfig['CELL_DESC'], $vFieldConfig['SORT_PRIO'], $vFieldConfig['CELL_LABEL']);
  915. }
  916. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') hasFld('.$this->hasField($vFieldConfig['ID_CELL']).') ';echo'</pre>';
  917. if (!isset($vFieldConfig['FORM_TREAT'])) {// TODO: convert to legacy perms
  918. $vFieldConfig['FORM_TREAT'] = '';
  919. if ($vFieldConfig['PERM_R'] > 0) $vFieldConfig['FORM_TREAT'] .= 'R';
  920. if ($vFieldConfig['PERM_W'] > 0) $vFieldConfig['FORM_TREAT'] .= 'W';
  921. if ($vFieldConfig['PERM_X'] > 0) $vFieldConfig['FORM_TREAT'] .= 'X';
  922. if ($vFieldConfig['PERM_C'] > 0) $vFieldConfig['FORM_TREAT'] .= 'C';
  923. if ($vFieldConfig['PERM_S'] > 0) $vFieldConfig['FORM_TREAT'] .= 'S';
  924. if ($vFieldConfig['PERM_O'] > 0) $vFieldConfig['FORM_TREAT'] .= 'O';
  925. if ($vFieldConfig['PERM_V'] > 0) $vFieldConfig['FORM_TREAT'] .= 'V';
  926. if ($vFieldConfig['PERM_E'] > 0) $vFieldConfig['FORM_TREAT'] .= 'E';
  927. }
  928. //echo'<pre>INIT::$permField('.$vFieldConfig['ID_CELL'].') ';print_r($vFieldConfig);echo'</pre>';
  929. if (!empty($vFieldConfig['FORM_TREAT'])) {
  930. $this->setFieldPerms($vFieldConfig['ID_CELL'], $vFieldConfig['FORM_TREAT']);
  931. }
  932. }
  933. }
  934. private function _fixProjectType() {
  935. $tblName = $this->getName();
  936. $fldName = 'M_DIST_TYPE';
  937. if ($tblName == 'IN7_MK_BAZA_DYSTRYBUCJI') {
  938. foreach ($this->_fields as $kFldId => $vFld) {
  939. if ($vFld['name'] == $fldName) {
  940. $sqlTypes = array();
  941. if (!empty($this->_types[$fldName])) {
  942. if (substr($this->_types[$fldName]['type'], 0, 4) == 'enum') {
  943. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($this->_types[$fldName]['type'], 5)));
  944. }
  945. }
  946. if (!empty($sqlTypes)) {
  947. $allowedTypes = array();
  948. $db = DB::getDB();
  949. $sql = "select z.DESC
  950. from `CRM_LISTA_ZASOBOW` as z
  951. where z.`A_STATUS`='NORMAL'
  952. and z.`PARENT_ID`={$kFldId}
  953. order by z.`DESC` asc
  954. ";
  955. $res = $db->query($sql);
  956. while ($r = $db->fetch($res)) {
  957. if (in_array($r->DESC, $sqlTypes)) {
  958. $allowedTypes[] = $r->DESC;
  959. }
  960. }
  961. sort($allowedTypes);
  962. if (!empty($allowedTypes)) {
  963. $this->_types[$fldName]['type'] = "enum('" . implode("','", $allowedTypes) . "')";
  964. }
  965. }
  966. }
  967. }
  968. }
  969. }
  970. private function _sortEnumFields() {
  971. foreach ($this->_fields as $kFldId => $vFld) {
  972. $type = $this->getFieldTypeById($kFldId);
  973. if (!empty($type['type'])) {
  974. if (substr($type['type'], 0, 4) == 'enum') {
  975. $sqlTypes = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  976. if (!empty($sqlTypes)) {
  977. sort($sqlTypes);
  978. $this->_types[$vFld['name']]['type'] = "enum('" . implode("','", $sqlTypes) . "')";
  979. }
  980. }
  981. }
  982. }
  983. }
  984. private function _fixDateFields() {
  985. foreach ($this->_types as $kFldName => $vType) {
  986. if ($kFldName == 'L_APPOITMENT_DATE') {
  987. $this->_types[$kFldName]['type'] = 'datetime';
  988. } else if ($kFldName == 'A_PROBLEM_DATE') {
  989. $this->_types[$kFldName]['type'] = 'datetime';
  990. }
  991. }
  992. }
  993. public function getUniqueKeys() {// TODO: RM NOT USED?
  994. $sqlKeys = array();
  995. $dbID = $this->getDB();
  996. $tblName = $this->getName();
  997. $db = DB::getDB($dbID);
  998. if (!$db) {
  999. die('Error - Brak konfiguracji dla bazy danych ID=' . $dbID);
  1000. }
  1001. $sql = "SHOW KEYS FROM `{$tblName}`";
  1002. $res = $db->query($sql);
  1003. while ($r = $db->fetch($res)) {
  1004. if ($r->Non_unique == '0') {
  1005. $sqlKeys[$r->Column_name] = true;
  1006. }
  1007. }
  1008. $sqlKeys = array_keys($sqlKeys);
  1009. return $sqlKeys;
  1010. }
  1011. public function sortFieldsCallback($a, $b) {
  1012. if ($a['name'] == 'ID') {
  1013. return -1;
  1014. }
  1015. else if ($b['name'] == 'ID') {
  1016. return 1;
  1017. }
  1018. else if ($a['sort_prio'] < $b['sort_prio']) {
  1019. return -1;
  1020. }
  1021. else if ($a['sort_prio'] > $b['sort_prio']) {
  1022. return 1;
  1023. }
  1024. else {
  1025. return 0;
  1026. }
  1027. }
  1028. public function isInitialized() {
  1029. return (!empty($this->_types));
  1030. }
  1031. /**
  1032. * Save data in session cache.
  1033. */
  1034. function save() {
  1035. $_SESSION['TableAcl_cache'][$this->_zasobID] = $this->toArray();
  1036. }
  1037. public function getFieldTypeById($fieldID) {
  1038. if (!array_key_exists($fieldID, $this->_fields)) {
  1039. return null;
  1040. }
  1041. $colName = $this->_fields[$fieldID]['name'];
  1042. if (!array_key_exists($colName, $this->_types)) {
  1043. return null;
  1044. }
  1045. return $this->_types[$colName];
  1046. }
  1047. public function getFieldType($colName) {
  1048. if (!array_key_exists($colName, $this->_types)) {
  1049. return null;
  1050. }
  1051. return $this->_types[$colName];
  1052. }
  1053. public function hasFieldType($colName) {
  1054. if (array_key_exists($colName, $this->_types)) {
  1055. return true;
  1056. }
  1057. return false;
  1058. }
  1059. public function getVisibleFieldList() { return $this->getVisibleFieldListByIdZasob(); }
  1060. public function getVisibleFieldListByIdZasob() {
  1061. $cols = array();
  1062. $id = 0;
  1063. foreach ($this->_fields as $kFieldID => $vField) {
  1064. if ($vField['name'] == 'ID') {
  1065. $id = $kFieldID;
  1066. }
  1067. }
  1068. $cols[$id] = 'ID';
  1069. foreach ($this->_fields as $kFieldID => $vField) {
  1070. if ($vField['name'] == 'ID') {
  1071. continue;
  1072. }
  1073. $cols[$kFieldID] = $vField['name'];
  1074. }
  1075. return $cols;
  1076. }
  1077. public function getExportFieldList() {
  1078. $cols = array();
  1079. $realFlds = $this->getRealFieldList();
  1080. foreach ($realFlds as $vFieldName) {
  1081. $fldId = $this->getFieldIdByName($vFieldName);
  1082. if ($fldId > 0 && $this->hasFieldPerm($fldId, 'E')) {
  1083. $cols[] = $vFieldName;
  1084. }
  1085. }
  1086. return $cols;
  1087. }
  1088. /**
  1089. * List of real fields in database.
  1090. */
  1091. public function getRealFieldList() {
  1092. $cols = array();
  1093. $cols[] = 'ID';
  1094. foreach ($this->_fields as $kFieldID => $vField) {
  1095. if ($vField['name'] == 'ID') {
  1096. continue;
  1097. }
  1098. if (array_key_exists($vField['name'], $this->_types)) {
  1099. $cols[] = $vField['name'];
  1100. }
  1101. }
  1102. return $cols;
  1103. }
  1104. public function getFieldListByIdZasob() {
  1105. $cols = array();
  1106. foreach ($this->_fields as $idField => $fld) {
  1107. if ($fld['name'] == 'ID') {
  1108. $cols[$idField] = 'ID';
  1109. }
  1110. }
  1111. foreach ($this->_fields as $idField => $fld) {
  1112. if ($fld['name'] == 'ID') continue;
  1113. $cols[$idField] = $fld['name'];
  1114. }
  1115. return $cols;
  1116. }
  1117. public function getRealFieldListByIdZasob() {
  1118. $cols = array();
  1119. foreach ($this->_fields as $idField => $fld) {
  1120. if ($fld['name'] == 'ID') {
  1121. $cols[$idField] = 'ID';
  1122. }
  1123. }
  1124. foreach ($this->_fields as $idField => $fld) {
  1125. if ($fld['name'] == 'ID') continue;
  1126. if (array_key_exists($fld['name'], $this->_types)) {
  1127. $cols[$idField] = $fld['name'];
  1128. }
  1129. }
  1130. return $cols;
  1131. }
  1132. public function getVirtualFieldListByIdZasob() {
  1133. $cols = array();
  1134. foreach ($this->_fields as $kFieldID => $vField) {
  1135. if ($vField['name'] == 'ID') {
  1136. continue;
  1137. }
  1138. if (in_array($kFieldID, $this->_virtualFieldsIdList)) {
  1139. $cols[$kFieldID] = $vField['name'];
  1140. }
  1141. else if (!array_key_exists($vField['name'], $this->_types)) {
  1142. $cols[$kFieldID] = $vField['name'];
  1143. }
  1144. }
  1145. return $cols;
  1146. }
  1147. public function getFieldLabel($fieldName) {
  1148. $idField = $this->getFieldIdByName($fieldName);
  1149. if (array_key_exists($idField, $this->_fields)) {
  1150. if (!empty($this->_fields[$idField]['label'])) {
  1151. return $this->_fields[$idField]['label'];
  1152. }
  1153. }
  1154. return null;
  1155. }
  1156. public function getFieldOpis($fieldName) {
  1157. $idField = $this->getFieldIdByName($fieldName);
  1158. if (array_key_exists($idField, $this->_fields)) {
  1159. if (!empty($this->_fields[$idField]['opis'])) {
  1160. return $this->_fields[$idField]['opis'];
  1161. }
  1162. }
  1163. return null;
  1164. }
  1165. public function getTypes() {
  1166. return $this->_types;
  1167. }
  1168. public function fixEmptyValueFromUser($fieldID) {
  1169. $value = '';
  1170. $type = $this->getFieldTypeById($fieldID);
  1171. if ($type) {
  1172. if ($type['type'] == 'date') {
  1173. $value = $type['default'];
  1174. }
  1175. if (substr($type['type'], 0, 3) == 'int'
  1176. || substr($type['type'], 0, 7) == 'tinyint'
  1177. || substr($type['type'], 0, 8) == 'smallint'
  1178. || substr($type['type'], 0, 6) == 'bigint'
  1179. ) {
  1180. $value = intval($type['default']);
  1181. }
  1182. // fix bug when field is unique and is null allowed: change empty string to null
  1183. if ($type['null']) {
  1184. $value = 'NULL';
  1185. }
  1186. // fix bug when field is enum and is set to '0': for php '0' is empty
  1187. if (substr($type['type'], 0, 4) == 'enum') {// && $args["f{$fieldID}"] === '0') {
  1188. if (false !== strpos($type['type'], "''")) {
  1189. // enum('', '1','2')
  1190. $value = '';
  1191. } else if (false !== strpos($type['type'], "'0'")) {
  1192. // enum('0', '1','2')
  1193. $value = '0';
  1194. } else {
  1195. $value = $type['default'];
  1196. }
  1197. }
  1198. }
  1199. return $value;
  1200. }
  1201. public function fromArray($arr) {
  1202. $this->_db = $arr['db'];
  1203. $this->_name = $arr['name'];
  1204. $this->_label = $arr['label'];
  1205. $this->_opis = $arr['opis'];
  1206. $this->_fields = V::get('fields', array(), $arr);
  1207. $this->_virtualFieldsIdList = V::get('virtualFieldsIdList', array(), $arr);
  1208. $this->_types = V::get('types', array(), $arr);
  1209. }
  1210. public function toArray() {
  1211. $arr = array();
  1212. $arr['db'] = $this->_db;
  1213. $arr['name'] = $this->_name;
  1214. $arr['label'] = $this->_label;
  1215. $arr['opis'] = $this->_opis;
  1216. $arr['fields'] = $this->_fields;
  1217. $arr['virtualFieldsIdList'] = $this->_virtualFieldsIdList;
  1218. $arr['types'] = $this->_types;
  1219. return $arr;
  1220. }
  1221. public function convertObjectFromUserInput($args, $type = 'array_by_id', $prefix = 'f') {
  1222. $item = array();
  1223. $fields = $this->getFields();
  1224. foreach ($fields as $kID => $vField) {
  1225. $vFieldName = $vField['name'];
  1226. if (array_key_exists("f{$kID}", $args)) {
  1227. $value = $args["f{$kID}"];
  1228. if (empty($args["f{$kID}"]) && strlen($args["f{$kID}"]) == 0) {// fix bug in input type date and value="0000-00-00"
  1229. $value = $this->fixEmptyValueFromUser($kID);
  1230. }
  1231. $item[$vFieldName] = $value;
  1232. }
  1233. }
  1234. return $item;
  1235. }
  1236. public function getItem($primaryKey) {
  1237. $ds = $this->getDataSource();
  1238. return $ds->getItem($primaryKey);
  1239. }
  1240. public function getItems($params = array()) {// TODO: use ParseOgcQuery
  1241. $ds = $this->getDataSource();
  1242. return $ds->getItems($params);
  1243. }
  1244. public function getTotal($params = array()) {// TODO: use ParseOgcQuery
  1245. $ds = $this->getDataSource();
  1246. return $ds->getTotal($params);
  1247. }
  1248. public function getColDefault($fieldName) {
  1249. $ds = $this->getDataSource();
  1250. return $ds->getColDefault($fieldName);
  1251. }
  1252. public function getSpecialFilters() {
  1253. $ds = $this->getDataSource();
  1254. return $ds->getSpecialFilters();
  1255. }
  1256. public function getGeomFields() {
  1257. $ds = $this->getDataSource();
  1258. return $ds->getGeomFields();
  1259. }
  1260. public function isGeomField($fldName) {
  1261. $ds = $this->getDataSource();
  1262. return $ds->isGeomField($fldName);
  1263. }
  1264. public function getGeomFieldType($fldName) {
  1265. $dbGeomType = $this->getFieldType($fldName);
  1266. $dbGeomType = (!empty($dbGeomType['type']))? $dbGeomType['type'] : '';
  1267. $geomType = strtolower($dbGeomType);
  1268. return $geomType;
  1269. }
  1270. public function getHistItems($id) {
  1271. $ds = $this->getDataSource();
  1272. return $ds->getHistItems($id);
  1273. }
  1274. public function getHistItem($id, $idHist) {
  1275. $ds = $this->getDataSource();
  1276. $histItems = $ds->getHistItems($id, ['ID' => $idHist]);
  1277. if (empty($histItems)) return null;
  1278. return reset($histItems);
  1279. }
  1280. public function addItem($itemTodo) {
  1281. if (is_object($itemTodo)) {
  1282. $itemTodo = (array)$itemTodo;
  1283. } else if (!is_array($itemTodo)) {
  1284. throw new HttpException('Item is not array', 400);
  1285. }
  1286. if (empty($itemTodo)) {
  1287. //throw new Exception('Item patch is empty');
  1288. DBG::_('DBG_DS', '>2', "Item patch is empty", null, __CLASS__, __FUNCTION__, __LINE__);
  1289. return 0;// nothing to insert
  1290. }
  1291. $ds = $this->getDataSource();
  1292. // from convertObjectFromUserInput
  1293. $item = array();
  1294. $fields = $this->getFields();
  1295. foreach ($fields as $kID => $vField) {
  1296. $vFieldName = $vField['name'];
  1297. if (!$this->isAllowed($kID, 'C')) {
  1298. continue;
  1299. }
  1300. if (isset($itemTodo[$vFieldName])) {
  1301. $value = $itemTodo[$vFieldName];
  1302. if (empty($value) && strlen($value) == 0) {// fix bug in input type date and value="0000-00-00"
  1303. $value = $this->fixEmptyValueFromUser($kID);
  1304. }
  1305. $item[$vFieldName] = $value;
  1306. }
  1307. }
  1308. if (empty($item)) {
  1309. throw new Exception("Nothing to add");
  1310. }
  1311. {// add DefaultAclGroup if no create perms ('C')
  1312. $defaultAclGroup = User::getDefaultAclGroup();
  1313. if ($defaultAclGroup) {
  1314. $permFields = array('A_ADM_COMPANY', 'A_CLASSIFIED');
  1315. foreach ($permFields as $permFldName) {
  1316. if (array_key_exists($permFldName, $this->_types)) {
  1317. $permFldId = $this->getFieldIdByName($permFldName);
  1318. if (0 == $permFldId || !$this->isAllowed($permFldId, 'C')) {
  1319. $item[$permFldName] = $defaultAclGroup;
  1320. }
  1321. }
  1322. }
  1323. }
  1324. }
  1325. DBG::_('DBG_DS', '>2', "addItem", $item, __CLASS__, __FUNCTION__, __LINE__);
  1326. return $ds->addItem($item);
  1327. }
  1328. /**
  1329. * @param array $itemPatch
  1330. */
  1331. public function updateItem($itemPatch) {
  1332. if (is_object($itemPatch)) {
  1333. $itemPatch = (array)$itemPatch;
  1334. } else if (!is_array($itemPatch)) {
  1335. throw new HttpException('Item patch is not array', 400);
  1336. }
  1337. if (empty($itemPatch)) {
  1338. //throw new Exception('Item patch is empty');
  1339. return 0;// nothing to change
  1340. }
  1341. $ds = $this->getDataSource();
  1342. $primaryKeyField = $ds->getPrimaryKeyField();
  1343. if (empty($itemPatch[$primaryKeyField])) {
  1344. throw new HttpException("Item Primary Key not set!", 400);
  1345. }
  1346. $primaryKey = $itemPatch[$primaryKeyField];
  1347. $itemOld = $this->getItem($primaryKey);
  1348. if (!$itemOld) {
  1349. throw new HttpException("Item not exists!", 404);
  1350. }
  1351. if (!$this->canWriteRecord($itemOld) && !$this->hasPermSuperWrite()) {
  1352. throw new HttpException("Brak dostępu do rekordu", 403);
  1353. }
  1354. // $itemPatch from user input to $itemPatchChecked
  1355. $itemPatchChecked = array();
  1356. $fields = $this->getFields();
  1357. foreach ($fields as $kID => $vField) {
  1358. $vFieldName = $vField['name'];
  1359. if (!$this->isAllowed($kID, 'W', $itemOld)) {
  1360. continue;
  1361. }
  1362. if (array_key_exists($vFieldName, $itemPatch)) {
  1363. if (!$this->isAllowed($kID, 'R', $itemOld) && '*****' == $itemPatch[$vFieldName]) {
  1364. // default value for perms 'W' without 'R' is '*****'
  1365. }
  1366. else {
  1367. $value = $itemPatch[$vFieldName];
  1368. if (empty($itemPatch[$vFieldName]) && strlen($itemPatch[$vFieldName]) == 0) {// fix bug in input type date and value="0000-00-00"
  1369. $value = $this->fixEmptyValueFromUser($kID);
  1370. }
  1371. if ($value != $itemOld->$vFieldName) {
  1372. $itemPatchChecked[$vFieldName] = $value;
  1373. }
  1374. }
  1375. }
  1376. }
  1377. if (empty($itemPatchChecked)) {
  1378. //throw new HttpException("Item Primary Key not set!", 400);
  1379. return 0;// nothing to change
  1380. }
  1381. $itemPatchChecked[$primaryKeyField] = $primaryKey;
  1382. $affected = $ds->updateItem($itemPatchChecked);
  1383. return $affected;
  1384. }
  1385. public function createItemCopy($item) {
  1386. $ds = $this->getDataSource();
  1387. $types = $this->getTypes();
  1388. $uniqKeys = $ds->getUniqueKeys();// TODO: getUniqueFields
  1389. $primaryKeyField = $ds->getPrimaryKeyField();
  1390. $itemCopy = new stdClass();
  1391. foreach ($types as $kName => $vType) {
  1392. if ($kName == $primaryKeyField) {
  1393. continue;
  1394. } else if (in_array($kName, array('A_RECORD_UPDATE_AUTHOR','A_RECORD_UPDATE_DATE'))) {
  1395. continue;
  1396. }
  1397. $value = V::get($kName, '', $item);
  1398. if (in_array($kName, $uniqKeys)) {
  1399. $value .= '?';
  1400. }
  1401. if ($ds->isGeomField($kName) && !empty($value)) {
  1402. $value = "GeomFromText('{$value}')";
  1403. }
  1404. $itemCopy->{$kName} = $value;
  1405. }
  1406. return $itemCopy;
  1407. }
  1408. public function getExportDataSource($cols = array()) {
  1409. $exportFieldList = $this->getExportFieldList();
  1410. if (!empty($cols)) {
  1411. $fltrExportFlds = array();
  1412. foreach ($exportFieldList as $fldName) {
  1413. if (in_array($fldName, $cols)) {
  1414. $fltrExportFlds[] = $fldName;
  1415. }
  1416. }
  1417. $exportFieldList = $fltrExportFlds;
  1418. }
  1419. $dataSource = $this->_getDataSource($exportFieldList);
  1420. return $dataSource;
  1421. }
  1422. public function getDataSource() {
  1423. $realFieldList = $this->getRealFieldList();
  1424. $dataSource = $this->_getDataSource($realFieldList);
  1425. $dataSource->setFieldGroupWrite('A_ADM_COMPANY', $this->hasFieldType('A_ADM_COMPANY'));
  1426. $dataSource->setFieldGroupRead('A_CLASSIFIED', $this->hasFieldType('A_CLASSIFIED'));
  1427. $dataSource->setFieldOwner('L_APPOITMENT_USER', $this->hasFieldType('L_APPOITMENT_USER'));
  1428. $adminFields = array('A_RECORD_CREATE_DATE', 'A_RECORD_CREATE_AUTHOR', 'A_RECORD_UPDATE_DATE', 'A_RECORD_UPDATE_AUTHOR');
  1429. foreach ($adminFields as $vAdmFld) {
  1430. if (!in_array($vAdmFld, $realFieldList) && $this->hasFieldType($vAdmFld)) {
  1431. $dataSource->addCol($vAdmFld);
  1432. }
  1433. }
  1434. return $dataSource;
  1435. }
  1436. private function _getDataSource($cols) {
  1437. Lib::loadClass('DataSourceFactory');
  1438. $dsConfig = array();
  1439. $dsConfig['source_id'] = $this->getDB();
  1440. $dsConfig['object_name'] = $this->getName();
  1441. $dsConfig['fields'] = $cols;
  1442. $dsConfig['field_types'] = $this->getTypes();
  1443. $dsConfig['fields_virtual'] = $this->getVirtualFieldListByIdZasob();
  1444. $dsConfig['acl_fltr_allowed'] = !$this->hasSuperAccessPerms();
  1445. return DataSourceFactory::buildFromZasobInfo($dsConfig);
  1446. }
  1447. public function getPrimaryKeyField() {
  1448. $ds = $this->getDataSource();
  1449. return $ds->getPrimaryKeyField();
  1450. }
  1451. public function isIntegerField($fldName) {
  1452. $type = $this->getFieldType($fldName);
  1453. if (!$type) return false;
  1454. if (substr($type['type'], 0, 3) == 'int'
  1455. || substr($type['type'], 0, 7) == 'tinyint'
  1456. || substr($type['type'], 0, 8) == 'smallint'
  1457. || substr($type['type'], 0, 9) == 'mediumint'
  1458. || substr($type['type'], 0, 6) == 'bigint'
  1459. ) {
  1460. return true;
  1461. }
  1462. return false;
  1463. }
  1464. public function isDecimalField($fldName) {
  1465. $type = $this->getFieldType($fldName);
  1466. if (!$type) return false;
  1467. if (substr($type['type'], 0, 7) == 'decimal'
  1468. || substr($type['type'], 0, 7) == 'numeric'
  1469. || substr($type['type'], 0, 6) == 'double'
  1470. || substr($type['type'], 0, 5) == 'float'
  1471. || substr($type['type'], 0, 4) == 'real'
  1472. ) {
  1473. return true;
  1474. }
  1475. return false;
  1476. }
  1477. public function isDateField($fldName) {
  1478. $type = $this->getFieldType($fldName);
  1479. if (!$type) return false;
  1480. if (substr($type['type'], 0, 4) == 'date' && substr($type['type'], 0, 8) != 'datetime') {
  1481. return true;
  1482. }
  1483. return false;
  1484. }
  1485. public function isDateTimeField($fldName) {
  1486. $type = $this->getFieldType($fldName);
  1487. if (!$type) return false;
  1488. if (substr($type['type'], 0, 8) == 'datetime') {
  1489. return true;
  1490. }
  1491. return false;
  1492. }
  1493. public function isStringField($fldName) {
  1494. $type = $this->getFieldType($fldName);
  1495. if (!$type) return false;
  1496. if (substr($type['type'], 0, 7) == 'varchar'
  1497. || substr($colType['type'], 0, 4) == 'char'
  1498. ) {
  1499. return true;
  1500. }
  1501. return false;
  1502. }
  1503. public function isTextField($fldName) {
  1504. $type = $this->getFieldType($fldName);
  1505. if (!$type) return false;
  1506. if (substr($type['type'], 0, 4) == 'text'
  1507. || substr($type['type'], 0, 8) == 'tinytext'
  1508. || substr($type['type'], 0, 10) == 'mediumtext'
  1509. || substr($type['type'], 0, 8) == 'longtext'
  1510. ) {
  1511. return true;
  1512. }
  1513. return false;
  1514. }
  1515. public function isBinaryField($fldName) {
  1516. $type = $this->getFieldType($fldName);
  1517. if (!$type) return false;
  1518. if (substr($type['type'], 0, 4) == 'blob'
  1519. || substr($type['type'], 0, 8) == 'tinyblob'
  1520. || substr($type['type'], 0, 10) == 'mediumblob'
  1521. || substr($type['type'], 0, 8) == 'longblob'
  1522. ) {
  1523. return true;
  1524. }
  1525. return false;
  1526. }
  1527. public function isEnumerationField($fldName) {
  1528. $type = $this->getFieldType($fldName);
  1529. if (!$type) return false;
  1530. if (substr($type['type'], 0, 4) == 'enum') {
  1531. return true;
  1532. }
  1533. return false;
  1534. }
  1535. public function getEnumerations($fldName) {
  1536. $enum = array();
  1537. $type = $this->getFieldType($fldName);
  1538. if (!$type) return $enum;
  1539. if (!$this->isEnumerationField($fldName)) return $enum;
  1540. $values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($type['type'], 5)));
  1541. foreach ($values as $val) {
  1542. $enum[$val] = $val;
  1543. }
  1544. return $enum;
  1545. }
  1546. public function getAttributesFromZasoby() {
  1547. $attributes = array();
  1548. $db = DB::getDB();
  1549. $sql = "
  1550. select `ID` as `id_zasob`
  1551. , `DESC` as `field_name`
  1552. , `DESC_PL` as `label`
  1553. , `OPIS` as `description`
  1554. from `CRM_LISTA_ZASOBOW`
  1555. where `PARENT_ID`={$this->_zasobID}
  1556. and `TYPE`='KOMORKA'
  1557. ";
  1558. $res = $db->query($sql);
  1559. while ($r = $db->fetch($res)) {
  1560. $fldAttr = array();
  1561. $fldAttr['id_zasob'] = $r->id_zasob;
  1562. if (!empty($r->label)) $fldAttr['label'] = $r->label;
  1563. if (!empty($r->description)) $fldAttr['description'] = $r->description;
  1564. $attributes[$r->field_name] = $fldAttr;
  1565. }
  1566. // fetch field values
  1567. $sql = "
  1568. select z.`ID` as `id_zasob`
  1569. , z.`DESC` as `field_name`
  1570. , v.`DESC` as `value`
  1571. , v.`DESC_PL` as `label`
  1572. -- , v.`OPIS` as `description`
  1573. from `CRM_LISTA_ZASOBOW` z
  1574. join `CRM_LISTA_ZASOBOW` v on(v.`PARENT_ID` = z.`ID`)
  1575. where z.`PARENT_ID`={$this->_zasobID}
  1576. and z.`TYPE`='KOMORKA'
  1577. ";
  1578. $res = $db->query($sql);
  1579. while ($r = $db->fetch($res)) {
  1580. if (!empty($r->value) && !empty($r->label)) {
  1581. $valuesMap = V::get('valuesMap', array(), $attributes[$r->field_name]);
  1582. $valuesMap[$r->value] = $r->label;
  1583. $attributes[$r->field_name]['valuesMap'] = $valuesMap;
  1584. }
  1585. }
  1586. return $attributes;
  1587. }
  1588. public function getXsdFieldType($fldName) {// @returns string - xsd type, TODO: throw Exception when field not found
  1589. $fldType = 'xsd:string';
  1590. if ('A_RECORD_UPDATE_DATE' == $fldName) return 'xsd:string';
  1591. if ('A_RECORD_CREATE_DATE' == $fldName) return 'xsd:string';
  1592. if ($this->isIntegerField($fldName)) return 'xsd:integer';
  1593. else if ($this->isDecimalField($fldName)) return 'xsd:decimal';
  1594. else if ($this->isDateField($fldName)) return 'xsd:date';
  1595. else if ($this->isDateTimeField($fldName)) return 'xsd:dateTime';
  1596. else if ($this->isGeomField($fldName)) {
  1597. //$fldType = 'gml:GeometryPropertyType';
  1598. $geomType = $this->getGeomFieldType($fldName);
  1599. if ('polygon' == $geomType) return 'gml:PolygonPropertyType';
  1600. else if ('point' == $geomType) return 'gml:PointPropertyType';
  1601. else if ('linestring' == $geomType) return 'gml:LineStringPropertyType';
  1602. else return 'gml:GeometryPropertyType';
  1603. }
  1604. else if ($this->isEnumerationField($fldName)) return 'xsd:string';
  1605. else if ($this->isBinaryField($fldName)) return 'xsd:base64Binary';
  1606. return $fldType;
  1607. }
  1608. }