TableAcl.php 52 KB

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