TableAcl.php 56 KB

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