TypespecialVariable.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. <?php
  2. Lib::loadClass('TypespecialBase');
  3. class TypespecialVariable extends TypespecialBase {
  4. private $fldID;
  5. private $fldName;
  6. public function __construct($fldID, $fldName) {
  7. $this->fldID = $fldID;
  8. $this->fldName = $fldName;
  9. }
  10. public static function getInstance($fldID, $fldName) {
  11. switch ($fldName) {
  12. case 'A_ADM_COMPANY':
  13. case 'A_CLASSIFIED':
  14. case 'K_OD_KOGO':
  15. case 'OD_KOGO_ADRES':
  16. case 'M_DISTRIBUTOR':
  17. case '__CONNECTIONS':
  18. case '__NESTED_GROUPS':
  19. case '__USER_GROUPS':
  20. case '__TELBOXES':
  21. case '__TELBOXES_NAME':
  22. case '__ZASOB':
  23. case '__COMPANIES':
  24. case 'DEFAULT_ACL_GROUP':
  25. return new TypespecialVariable($fldID, $fldName);
  26. break;
  27. }
  28. return null;
  29. }
  30. /**
  31. * @returns [{id:int, param_out:str, exports:[]}]
  32. */
  33. public function getValuesWithExports($query) {
  34. $items = $this->_getValues($query);
  35. return $items;
  36. }
  37. public function getValuesByIds($tblId, $ids) {
  38. return null;
  39. }
  40. public function getEditSelectedValuesByIds($tblId, $id, $fieldName, $fieldValue) {
  41. $items = $this->_getValues($fieldValue, true);
  42. $values = array();
  43. foreach ($items as $vItem) {
  44. $values[$vItem->param_out] = $vItem->param_out;
  45. }
  46. return $values;
  47. }
  48. public function showFormItem($tblID, $fName, $selValue = '', $params = array(), $record = null) {
  49. $out = '';
  50. $jsonAllowCreate = 'true';
  51. switch ($this->fldName) {
  52. case 'A_ADM_COMPANY':
  53. case 'A_CLASSIFIED':
  54. case 'DEFAULT_ACL_GROUP':
  55. $jsonAllowCreate = 'false';
  56. break;
  57. default:
  58. $jsonAllowCreate = 'true';
  59. }
  60. if (isset($params['allowCreate'])) {
  61. $jsonAllowCreate = ($params['allowCreate'])? 'true' : 'false';
  62. }
  63. $tsValue = V::get('typespecialValue', '', $params);
  64. if (!empty($selValue) && !empty($tsValue)) {
  65. $tsValue = "{$selValue}: {$tsValue}";
  66. }
  67. $optionsJson = 'null';
  68. $out .= '<div class="typepsecial">';
  69. $out .= '<select id="ts-' . $fName . '">';
  70. if (!empty($selValue)) {
  71. $tsVal = (!empty($tsValue))? $tsValue : $selValue;
  72. //$out .= '<option value="' . $selValue . '" selected="selected" typespecial="'.$tsVal.'">' . $tsVal . '</option>';
  73. $out .= '<option value="' . $selValue . '" selected="selected">' . $tsVal . '</option>';
  74. $optionsJson = new stdClass();
  75. $optionsJson->id = $selValue;
  76. $optionsJson->name = ($tsValue)? $tsValue : $selValue;
  77. $optionsJson = array($optionsJson);
  78. $optionsJson = json_encode($optionsJson);
  79. }
  80. $out .= '</select>';
  81. $out .= '</div>';
  82. $ajaxDataUrlBase = "index-ajax.php?_cls=TableAjax&_zasobID={$tblID}&_task=TYPESPECIAL&fldID={$this->fldID}";
  83. $ajaxDataUrlBase = V::get('ajaxDataUrlBase', $ajaxDataUrlBase, $params);
  84. $out .= '<script>' . "
  85. (function(){
  86. var fldNode=jQuery('#{$fName}'), tsNode=jQuery('#ts-{$fName}');
  87. if (!fldNode && !tsNode) {
  88. return;
  89. }
  90. fldNode.attr('name', '');
  91. fldNode.hide();
  92. if (fldNode.parent().hasClass('show-last-value')) {
  93. fldNode.parent().hide();
  94. }
  95. tsNode.attr('name', '{$fName}');
  96. tsNode.attr('tabindex', fldNode.attr('tabindex'));
  97. tsNode.selectize({
  98. theme: 'typespecial',
  99. valueField: 'id',
  100. labelField: 'id',
  101. searchField: 'name',
  102. sortField: 'name',
  103. create: {$jsonAllowCreate},
  104. delimiter: ';',
  105. dataAttr: 'typespecial',
  106. // preload: true,
  107. options: {$optionsJson},
  108. render: {
  109. item: function(item, escape) {
  110. //console.log('item', item);
  111. //var name = formatName(item);
  112. return '<div>' +
  113. '<span class=\"name\">' + escape(item.name || item.id) + '</span>' +
  114. '</div>';
  115. },
  116. option: function(item, escape) {
  117. //console.log('--- render.option item(',item,') this:',this);
  118. return '<div>' +
  119. '<span class=\"title\">' +
  120. escape(item.name || item.id) +
  121. '</span>' +
  122. '</div>';
  123. }
  124. },
  125. onItemAdd: function(value, item) {// Invoked when an item is selected.
  126. var curSel = this.options[value] || null, fieldExport;
  127. //console.log('--- onItemAdd val(',value,'/',item.data('name'),'/',item,') this:curSel:',curSel);
  128. if (curSel && curSel.exports) {
  129. for (var i in curSel.exports) {
  130. fieldExport = jQuery('#ts-f' + i);
  131. if (fieldExport.length) {
  132. //console.log('--- onItemAdd fieldExport.selectize(',fieldExport.selectize,')', fieldExport);
  133. if (fieldExport.get(0).selectize) {
  134. fieldExport.get(0).selectize.addOption({id:curSel.exports[i], name:curSel.exports[i]});
  135. fieldExport.get(0).selectize.setValue(curSel.exports[i]);
  136. } else {
  137. //jQuery('#f' + i).val(curSel.exports[i]);
  138. }
  139. } else {
  140. //console.log('--- onItemAdd jQuery(#f' + i + ').val(', curSel.exports[i], '): fieldExport.is(input:',fieldExport.is('input'),'/select:',fieldExport.is('select'),')', fieldExport);
  141. fieldExport = jQuery('#f' + i);
  142. if (fieldExport.is('input')) {
  143. jQuery('#f' + i).val(curSel.exports[i]);
  144. } else if (fieldExport.is('select')) {
  145. //TODO: add option and select //jQuery('#f' + i).val(curSel.exports[i]);
  146. }
  147. }
  148. }
  149. }
  150. },
  151. score: function(search) {
  152. var score = this.getScoreFunction(search);
  153. return function(item) {
  154. //console.log('score:item:', item, ', score:', score(item));
  155. return score(item);// score(item) * (1 + Math.min(item.watchers / 100, 1));
  156. };
  157. },
  158. load: function(query, callback) {
  159. if (!query.length) return callback();
  160. $.ajax({
  161. url: '{$ajaxDataUrlBase}',
  162. data: 'q=' + encodeURIComponent(query),
  163. type: 'POST',
  164. error: function() {
  165. callback();
  166. },
  167. success: function(res) {
  168. for (var i in res) {
  169. if (!res[i].name || res[i].id != res[i].name) {
  170. res[i].name = res[i].id + ': ' + res[i].name;
  171. }
  172. }
  173. callback(res);
  174. }
  175. });
  176. }
  177. });
  178. })()
  179. " . '</script>';
  180. return $out;
  181. }
  182. /**
  183. * @params $query - query string
  184. * @params $strict - search only euqal value
  185. * @returns [{id:int, param_out:str, exports:[]}]
  186. */
  187. private function _getValues($query, $strict = false) {
  188. $values = array();
  189. switch ($this->fldName) {
  190. case 'A_ADM_COMPANY':
  191. case 'A_CLASSIFIED':
  192. Lib::loadClass('UsersHelper');
  193. $userName = User::getLogin();
  194. $userLdapGroups = UsersHelper::getLDAPGroupByUserName($userName);
  195. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">userLdapGroups (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($userLdapGroups);echo'</pre>';}
  196. if (!empty($userLdapGroups)) {
  197. foreach ($userLdapGroups as $kID => $vLdapGroup) {
  198. $allowGroup = false;
  199. //$allowGroup = $vLdapGroup->gidNumber > 1000;
  200. if ('workgroup' == $vLdapGroup->cn) {
  201. $allowGroup = true;
  202. } else {
  203. $cnTest = str_replace('-', '_', $vLdapGroup->cn);
  204. $cnTest = explode('_', $cnTest);
  205. $cnTest = $cnTest[0];
  206. if (is_numeric($cnTest)) {
  207. $allowGroup = true;
  208. }
  209. }
  210. if ($allowGroup) {
  211. if ($strict) {
  212. if (!empty($query) && $query == $vLdapGroup->cn) {
  213. $values[$vLdapGroup->cn] = (object)array('id'=>$vLdapGroup->cn, 'param_out'=>$vLdapGroup->cn);
  214. }
  215. } else {
  216. $values[$vLdapGroup->cn] = (object)array('id'=>$vLdapGroup->cn, 'param_out'=>$vLdapGroup->cn);
  217. //$values[$vLdapGroup->cn] = $vLdapGroup->cn;
  218. }
  219. }
  220. }
  221. }
  222. break;
  223. case 'DEFAULT_ACL_GROUP':
  224. Lib::loadClass('UsersHelper');
  225. $userLdapGroups = UsersHelper::getLDAPGroupsAll();
  226. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">userLdapGroups (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($userLdapGroups);echo'</pre>';}
  227. if (!empty($userLdapGroups)) {
  228. foreach ($userLdapGroups as $kID => $vLdapGroup) {
  229. $allowGroup = false;
  230. //$allowGroup = $vLdapGroup->gidNumber > 1000;
  231. if ('workgroup' == $vLdapGroup->cn) {
  232. $allowGroup = true;
  233. } else {
  234. $cnTest = str_replace('-', '_', $vLdapGroup->cn);
  235. $cnTest = explode('_', $cnTest);
  236. $cnTest = $cnTest[0];
  237. if (is_numeric($cnTest)) {
  238. $allowGroup = true;
  239. }
  240. }
  241. if ($allowGroup) {
  242. if ($strict) {
  243. if (!empty($query) && $query == $vLdapGroup->cn) {
  244. $values[$vLdapGroup->cn] = (object)array('id'=>$vLdapGroup->cn, 'param_out'=>$vLdapGroup->cn);
  245. }
  246. } else {
  247. $values[$vLdapGroup->cn] = (object)array('id'=>$vLdapGroup->cn, 'param_out'=>$vLdapGroup->cn);
  248. //$values[$vLdapGroup->cn] = $vLdapGroup->cn;
  249. }
  250. }
  251. }
  252. }
  253. break;
  254. case 'K_OD_KOGO':
  255. $db = DB::getDB();
  256. $OD_KOGO_ADRES_ID = 0;
  257. $sql = "select z2.`ID`
  258. from `CRM_LISTA_ZASOBOW` as z
  259. join `CRM_LISTA_ZASOBOW` as z2 on (z2.`PARENT_ID`=z.`PARENT_ID`)
  260. where
  261. z.`ID`='{$this->fldID}'
  262. and z2.`DESC`='OD_KOGO_ADRES'
  263. ";
  264. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  265. $res = $db->query($sql);
  266. while ($r = $db->fetch($res)) {
  267. $OD_KOGO_ADRES_ID = $r->ID;
  268. }
  269. $sqlLimit = 20;
  270. $query = trim($query, ' %');
  271. $query = $db->_($query);
  272. $sqlSearch = "k.`K_OD_KOGO` like '%{$query}%'";
  273. if ($strict) {
  274. if (!empty($query)) {
  275. $sqlLimit = 1;
  276. $sqlSearch = "k.`K_OD_KOGO`='{$query}'";
  277. } else {
  278. return $values;
  279. }
  280. }
  281. $sql = "select k.`K_OD_KOGO`, k.`OD_KOGO_ADRES`
  282. from `IN7_DZIENNIK_KORESP` as k
  283. where {$sqlSearch}
  284. group by k.`K_OD_KOGO`
  285. limit {$sqlLimit}
  286. ";
  287. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  288. $res = $db->query($sql);
  289. while ($r = $db->fetch($res)) {
  290. $values[] = (object)array('id'=>$r->K_OD_KOGO, 'param_out'=>$r->K_OD_KOGO, 'exports'=>array($OD_KOGO_ADRES_ID => $r->OD_KOGO_ADRES));
  291. }
  292. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">values (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($values);echo'</pre>';}
  293. break;
  294. case 'OD_KOGO_ADRES':
  295. $db = DB::getDB();
  296. $query = trim($query, ' %');
  297. $query = $db->_($query);
  298. $sqlLimit = 20;
  299. $sqlSearch = "k.`OD_KOGO_ADRES` like '%{$query}%'";
  300. if ($strict) {
  301. if (!empty($query)) {
  302. $sqlLimit = 1;
  303. $sqlSearch = "k.`OD_KOGO_ADRES`='{$query}'";
  304. } else {
  305. return $values;
  306. }
  307. }
  308. $sql = "select k.`OD_KOGO_ADRES`
  309. from `IN7_DZIENNIK_KORESP` as k
  310. where {$sqlSearch}
  311. group by k.`OD_KOGO_ADRES`
  312. limit {$sqlLimit}
  313. ";
  314. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  315. $res = $db->query($sql);
  316. while ($r = $db->fetch($res)) {
  317. $values[] = (object)array('id'=>$r->OD_KOGO_ADRES, 'param_out'=>$r->OD_KOGO_ADRES);
  318. }
  319. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">values (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($values);echo'</pre>';}
  320. break;
  321. case 'M_DISTRIBUTOR':
  322. $db = DB::getDB();
  323. $query = trim($query, ' %');
  324. $query = $db->_($query);
  325. $sqlLimit = 20;
  326. $sqlSearch = "p.`M_DISTRIBUTOR` like '%{$query}%'";
  327. if ($strict) {
  328. if (!empty($query)) {
  329. $sqlLimit = 1;
  330. $sqlSearch = "p.`M_DISTRIBUTOR`='{$query}'";
  331. } else {
  332. return $values;
  333. }
  334. }
  335. $sql = "select p.`M_DISTRIBUTOR`
  336. from `IN7_MK_BAZA_DYSTRYBUCJI` as p
  337. where p.`M_DISTRIBUTOR` like '%{$query}%'
  338. group by p.`M_DISTRIBUTOR`
  339. limit {$sqlLimit}
  340. ";
  341. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  342. $res = $db->query($sql);
  343. while ($r = $db->fetch($res)) {
  344. $values[] = (object)array('id'=>$r->M_DISTRIBUTOR, 'param_out'=>$r->M_DISTRIBUTOR);
  345. }
  346. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">values (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($values);echo'</pre>';}
  347. break;
  348. case '__NESTED_GROUPS': {
  349. $db = DB::getDB();
  350. $query = trim($query, ' %');
  351. $query = $db->_($query);
  352. $sqlLimit = 20;
  353. $sql = "select z.`ID`, z.`DESC`, z.`TYPE`
  354. from `CRM_LISTA_ZASOBOW` as z
  355. where z.`A_STATUS` in('NORMAL', 'WAITING')
  356. and z.`TYPE` in('STANOWISKO', 'PODMIOT', 'DZIAL')
  357. and (z.`DESC` like '%{$query}%' or z.`ID` like '%{$query}%')
  358. group by z.`DESC`
  359. limit {$sqlLimit}
  360. ";
  361. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  362. $res = $db->query($sql);
  363. while ($r = $db->fetch($res)) {
  364. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->TYPE . ' ' . $r->DESC);
  365. }
  366. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">values (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($values);echo'</pre>';}
  367. break;
  368. }
  369. case '__USER_GROUPS': {
  370. $db = DB::getDB();
  371. $query = trim($query, ' %');
  372. $query = $db->_($query);
  373. $sqlLimit = 20;
  374. $sql = "select z.`ID`, z.`DESC`, z.`TYPE`
  375. from `CRM_LISTA_ZASOBOW` as z
  376. where z.`A_STATUS` in('NORMAL', 'WAITING')
  377. and z.`TYPE` in('STANOWISKO', 'PODMIOT', 'DZIAL')
  378. and (z.`DESC` like '%{$query}%' or z.`ID` like '%{$query}%')
  379. group by z.`DESC`
  380. limit {$sqlLimit}
  381. ";
  382. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  383. $res = $db->query($sql);
  384. while ($r = $db->fetch($res)) {
  385. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->TYPE . ' ' . $r->DESC);
  386. }
  387. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">values (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($values);echo'</pre>';}
  388. break;
  389. }
  390. case '__TELBOXES': {
  391. $db = DB::getDB();
  392. $query = trim($query, ' %');
  393. $query = $db->_($query);
  394. $sqlLimit = 20;
  395. $sql = "select tx.`ID`, tx.`T_TELBOX_NAME`, tx.`T_TELBOX_TYPE`
  396. from `TELBOXES` as tx
  397. where
  398. tx.`A_STATUS`!='DELETED'
  399. and (tx.`ID` like '%{$query}%' or tx.`T_TELBOX_NAME` like '%{$query}%')
  400. order by tx.`T_TELBOX_NAME`
  401. limit {$sqlLimit}
  402. ";
  403. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  404. $res = $db->query($sql);
  405. while ($r = $db->fetch($res)) {
  406. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->T_TELBOX_NAME . ' ' . $r->T_TELBOX_TYPE);
  407. }
  408. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">values (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($values);echo'</pre>';}
  409. break;
  410. }
  411. case '__TELBOXES_NAME': {
  412. $db = DB::getDB();
  413. $query = trim($query, ' %');
  414. $query = $db->_($query);
  415. $sqlLimit = 20;
  416. $sql = "select tx.`ID`, tx.`T_TELBOX_NAME`, tx.`T_TELBOX_TYPE`
  417. from `TELBOXES` as tx
  418. where
  419. tx.`A_STATUS`!='DELETED'
  420. and (tx.`ID` like '%{$query}%' or tx.`T_TELBOX_NAME` like '%{$query}%')
  421. order by tx.`T_TELBOX_NAME`
  422. limit {$sqlLimit}
  423. ";
  424. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  425. $res = $db->query($sql);
  426. while ($r = $db->fetch($res)) {
  427. $values[] = (object)array('id'=>$r->T_TELBOX_NAME, 'param_out'=>$r->T_TELBOX_NAME . ' ' . $r->T_TELBOX_TYPE);
  428. }
  429. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">values (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($values);echo'</pre>';}
  430. break;
  431. }
  432. case '__ZASOB': {
  433. $db = DB::getDB();
  434. $query = trim($query, ' %');
  435. $query = $db->_($query);
  436. $sqlLimit = 20;
  437. $sql = "select z.`ID`, z.`DESC`, z.`TYPE`
  438. from `CRM_LISTA_ZASOBOW` as z
  439. where z.`A_STATUS` in('NORMAL', 'WAITING')
  440. and (z.`DESC` like '%{$query}%' or z.`ID` like '%{$query}%')
  441. -- group by z.`DESC`
  442. limit {$sqlLimit}
  443. ";
  444. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  445. $res = $db->query($sql);
  446. while ($r = $db->fetch($res)) {
  447. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->TYPE . ' ' . $r->DESC);
  448. }
  449. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">values (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($values);echo'</pre>';}
  450. break;
  451. }
  452. case '__COMPANIES': {
  453. $db = DB::getDB();
  454. $query = trim($query, ' %');
  455. $query = $db->_($query);
  456. $sqlLimit = 20;
  457. $sql = "select c.`ID`, c.`P_NAME`, c.`P_NIP`
  458. from `COMPANIES` as c
  459. where c.`A_STATUS` in('NORMAL', 'WAITING')
  460. and (c.`P_NAME` like '%{$query}%' or c.`P_NIP` like '%{$query}%' or c.`ID` like '%{$query}%')
  461. limit {$sqlLimit}
  462. ";
  463. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  464. $res = $db->query($sql);
  465. while ($r = $db->fetch($res)) {
  466. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->P_NAME . ' ' . $r->P_NIP);
  467. }
  468. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">values (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($values);echo'</pre>';}
  469. break;
  470. }
  471. default:
  472. }
  473. return $values;
  474. }
  475. /**
  476. * @returns object
  477. * {
  478. * ext_tbl_id: "1466",
  479. * ext_row_id: "2975",
  480. * id: "2975",
  481. * tbl_label: "Zasoby",
  482. * link_type_id: "5",
  483. * link_type: "NestedGroups",
  484. * },
  485. */
  486. public function getReturnData($tblId, $rowId, $fieldName) {
  487. $retData = new stdClass();
  488. $retData->items = array();
  489. $values = array();
  490. switch ($this->fldName) {
  491. case '__CONNECTIONS': {
  492. $linkTypes = array();
  493. $tblLabels = array();
  494. $db = DB::getDB();
  495. $sqlLimit = 21;
  496. $sql = "select l.*
  497. from `ITEM_LINKS` as l
  498. where l.`A_STATUS` in('NORMAL', 'WAITING')
  499. and (
  500. (l.`TABLE_1_ZASOB_ID`={$tblId} and l.`TABLE_1_ID`={$rowId})
  501. or
  502. (l.`TABLE_2_ZASOB_ID`={$tblId} and l.`TABLE_2_ID`={$rowId})
  503. )
  504. and l.`LINKS_TYPE_ID`>1
  505. limit {$sqlLimit}
  506. ";
  507. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  508. $res = $db->query($sql);
  509. while ($r = $db->fetch($res)) {
  510. $connObj = new stdClass();
  511. if ($r->TABLE_1_ZASOB_ID == $tblId && $r->TABLE_1_ID == $rowId) {
  512. $connObj->ext_tbl_id = $r->TABLE_2_ZASOB_ID;
  513. $connObj->ext_row_id = $r->TABLE_2_ID;
  514. $connObj->id = $r->TABLE_2_ID;
  515. $connObj->label = "{$r->TABLE_2_ZASOB_ID}({$r->TABLE_2_ID})";
  516. }
  517. else if ($r->TABLE_2_ZASOB_ID == $tblId && $r->TABLE_2_ID == $rowId) {
  518. $connObj->ext_tbl_id = $r->TABLE_1_ZASOB_ID;
  519. $connObj->ext_row_id = $r->TABLE_1_ID;
  520. $connObj->id = $r->TABLE_1_ID;
  521. $connObj->label = "{$r->TABLE_1_ZASOB_ID}({$r->TABLE_1_ID})";
  522. }
  523. if ($connObj) {
  524. $connObj->link_type_id = $r->LINKS_TYPE_ID;
  525. $linkTypes[$connObj->link_type_id] = '';
  526. $tblLabels[$connObj->ext_tbl_id] = '';
  527. $retData->items[] = $connObj;
  528. }
  529. }
  530. if (!empty($linkTypes)) {
  531. $sql = "select lt.`ID`, lt.`NAME`
  532. from `ITEM_LINK_TYPES` as lt
  533. where lt.`ID` in (" . implode(", ", array_keys($linkTypes)) . ")
  534. ";
  535. $res = $db->query($sql);
  536. while ($r = $db->fetch($res)) {
  537. $linkTypes[$r->ID] = $r->NAME;
  538. }
  539. }
  540. if (!empty($tblLabels)) {
  541. $sql = "select z.`ID`, z.`DESC`, z.`DESC_PL`, z.`OPIS`
  542. from `CRM_LISTA_ZASOBOW` as z
  543. where z.`ID` in (" . implode(", ", array_keys($tblLabels)) . ")
  544. ";
  545. $res = $db->query($sql);
  546. while ($r = $db->fetch($res)) {
  547. if (!empty($r->DESC_PL)) {
  548. $tblLabels[$r->ID] = $r->DESC_PL;
  549. } else if (!empty($r->OPIS)) {
  550. $tblLabels[$r->ID] = V::strShortUtf8($r->OPIS, 20);
  551. } else {
  552. $tblLabels[$r->ID] = $r->DESC;
  553. }
  554. }
  555. }
  556. foreach ($retData->items as $k => $connObj) {
  557. $connObj->link_type = V::get($connObj->link_type_id, $connObj->link_type_id, $linkTypes);
  558. $connObj->tbl_label = V::get($connObj->ext_tbl_id, $connObj->ext_tbl_id, $tblLabels);
  559. }
  560. if(V::get('DBG_TS', 0, $_GET) > 2){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">retData->items('.count($retData->items).') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($retData->items);echo'</pre>';}
  561. return $retData;
  562. break;
  563. }
  564. default:
  565. }
  566. return null;
  567. }
  568. }