TableAcl.php 52 KB

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