TableAcl.php 44 KB

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