TableAcl.php 57 KB

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