TableAcl.php 52 KB

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