TableAcl.php 57 KB

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