TableAcl.php 58 KB

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