TableAcl.php 42 KB

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