TableAcl.php 56 KB

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