TableAcl.php 69 KB

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