TableAcl.php 54 KB

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