TableAcl.php 57 KB

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