TypespecialVariable.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. <?php
  2. Lib::loadClass('TypespecialBase');
  3. Lib::loadClass('SqlQueryWhereBuilder');
  4. class TypespecialVariable extends TypespecialBase {
  5. private $fldID;
  6. private $fldName;
  7. public function __construct($fldID, $fldName) {
  8. $this->fldID = $fldID;
  9. $this->fldName = $fldName;
  10. }
  11. public static function getInstance($fldID, $fldName) {
  12. switch ($fldName) {
  13. case 'A_ADM_COMPANY':
  14. case 'A_CLASSIFIED':
  15. case 'K_OD_KOGO':
  16. case 'OD_KOGO_ADRES':
  17. case 'M_DISTRIBUTOR':
  18. case '__CONNECTIONS':
  19. case '__NESTED_GROUPS':
  20. case '__USER_GROUPS':
  21. case '__TELBOXES':
  22. case '__TELBOXES_NAME':
  23. case '__DEALS_SALES_TELBOXES_NAME':
  24. case '__ZALICZKI_NAJEMCOW__NAJEMCA':
  25. case '__ZASOB':
  26. case '__PROCES':
  27. case '__COMPANIES':
  28. case 'DEFAULT_ACL_GROUP':
  29. case 'VERSION_GIT':
  30. case '__USER_ID':
  31. case '__USER_LOGIN':
  32. case 'A_EXEC_GROUP':
  33. // case 'L_APPOITMENT_USER':
  34. return new TypespecialVariable($fldID, $fldName);
  35. break;
  36. }
  37. return null;
  38. }
  39. /**
  40. * @returns [{id:int, param_out:str, exports:[]}]
  41. */
  42. public function getValuesWithExports($query, $params = array()) {
  43. $items = $this->_getValues($query, false, $params);
  44. return $items;
  45. }
  46. public function getValuesByIds($tblId, $ids) {
  47. return null;
  48. }
  49. public function getEditSelectedValuesByIds($tblId, $id, $fieldName, $fieldValue) {
  50. $items = $this->_getValues($fieldValue, true);
  51. $values = array();
  52. foreach ($items as $vItem) {
  53. $values[$vItem->param_out] = $vItem->param_out;
  54. }
  55. return $values;
  56. }
  57. public function showFormItem($tblID, $fName, $selValue = '', $params = array(), $record = null) {
  58. $out = '';
  59. $jsonAllowCreate = 'true';
  60. switch ($this->fldName) {
  61. case 'L_APPOITMENT_USER':
  62. case 'A_ADM_COMPANY':
  63. case 'A_CLASSIFIED':
  64. case 'DEFAULT_ACL_GROUP':
  65. case 'VERSION_GIT':
  66. case '__USER_ID':
  67. case '__USER_LOGIN':
  68. $jsonAllowCreate = 'false';
  69. break;
  70. default:
  71. $jsonAllowCreate = 'true';
  72. }
  73. if (isset($params['allowCreate'])) {
  74. $jsonAllowCreate = ($params['allowCreate'])? 'true' : 'false';
  75. }
  76. $jsonPreload = 'false';
  77. switch ($this->fldName) {
  78. case 'VERSION_GIT':
  79. //$jsonPreload = 'true';
  80. break;
  81. default:
  82. $jsonPreload = 'false';
  83. }
  84. $tsValue = V::get('typespecialValue', '', $params);
  85. if (!empty($selValue) && !empty($tsValue)) {
  86. $tsValue = "{$selValue}: {$tsValue}";
  87. }
  88. $optionsJson = 'null';
  89. $out .= '<div class="typepsecial">';
  90. $outPlaceholder = '';
  91. if ('' != ($paramPlaceholder = V::get('placeholder', '', $params))) {
  92. $outPlaceholder = ' placeholder="' . $paramPlaceholder . '"';
  93. }
  94. $out .= '<select id="ts-' . $fName . '"' . $outPlaceholder . '>';
  95. if (!empty($selValue)) {
  96. $tsVal = (!empty($tsValue))? $tsValue : $selValue;
  97. //$out .= '<option value="' . $selValue . '" selected="selected" typespecial="'.$tsVal.'">' . $tsVal . '</option>';
  98. $out .= '<option value="' . $selValue . '" selected="selected">' . $tsVal . '</option>';
  99. $optionsJson = new stdClass();
  100. $optionsJson->id = $selValue;
  101. $optionsJson->name = ($tsValue)? $tsValue : $selValue;
  102. $optionsJson = array($optionsJson);
  103. $optionsJson = json_encode($optionsJson);
  104. }
  105. $out .= '</select>';
  106. $out .= '</div>';
  107. $ajaxDataUrlBase = "index-ajax.php?_cls=TableAjax&_zasobID={$tblID}&_task=TYPESPECIAL&fldID={$this->fldID}";
  108. $ajaxDataUrlBase = V::get('ajaxDataUrlBase', $ajaxDataUrlBase, $params);
  109. $frmFldName = V::get('formFieldName', $fName, $params);
  110. $out .= '<script>' . "
  111. jQuery(document).ready(function(){
  112. var fldNode=jQuery('#{$fName}'), tsNode=jQuery('#ts-{$fName}');
  113. if (!fldNode && !tsNode) {
  114. return;
  115. }
  116. fldNode.attr('name', '');
  117. fldNode.hide();
  118. if (fldNode.parent().hasClass('show-last-value')) {
  119. fldNode.parent().hide();
  120. }
  121. tsNode.attr('name', '{$frmFldName}');
  122. tsNode.attr('tabindex', fldNode.attr('tabindex'));
  123. tsNode.selectize({
  124. theme: 'typespecial',
  125. valueField: 'id',
  126. labelField: 'id',
  127. searchField: 'name',
  128. sortField: 'name',
  129. create: {$jsonAllowCreate},
  130. delimiter: ';',
  131. dataAttr: 'typespecial',
  132. preload: {$jsonPreload},
  133. options: {$optionsJson},
  134. render: {
  135. item: function(item, escape) {
  136. //console.log('item', item);
  137. //var name = formatName(item);
  138. return '<div>' +
  139. '<span class=\"name\">' + escape(item.name || item.id) + '</span>' +
  140. '</div>';
  141. },
  142. option: function(item, escape) {
  143. //console.log('--- render.option item(',item,') this:',this);
  144. return '<div>' +
  145. '<span class=\"title\">' +
  146. escape(item.name || item.id) +
  147. '</span>' +
  148. '</div>';
  149. }
  150. },
  151. onItemAdd: function(value, item) {// Invoked when an item is selected.
  152. var curSel = this.options[value] || null, fieldExport;
  153. //console.log('--- onItemAdd val(',value,'/',item.data('name'),'/',item,') this:curSel:',curSel);
  154. if (curSel && curSel.exports) {
  155. for (var i in curSel.exports) {
  156. fieldExport = jQuery('#ts-f' + i);
  157. if (fieldExport.length) {
  158. //console.log('--- onItemAdd fieldExport.selectize(',fieldExport.selectize,')', fieldExport);
  159. if (fieldExport.get(0).selectize) {
  160. fieldExport.get(0).selectize.addOption({id:curSel.exports[i], name:curSel.exports[i]});
  161. fieldExport.get(0).selectize.setValue(curSel.exports[i]);
  162. } else {
  163. //jQuery('#f' + i).val(curSel.exports[i]);
  164. }
  165. } else {
  166. //console.log('--- onItemAdd jQuery(#f' + i + ').val(', curSel.exports[i], '): fieldExport.is(input:',fieldExport.is('input'),'/select:',fieldExport.is('select'),')', fieldExport);
  167. fieldExport = jQuery('#f' + i);
  168. if (fieldExport.is('input')) {
  169. jQuery('#f' + i).val(curSel.exports[i]);
  170. } else if (fieldExport.is('select')) {
  171. //TODO: add option and select //jQuery('#f' + i).val(curSel.exports[i]);
  172. }
  173. }
  174. }
  175. }
  176. },
  177. score: function(search) {
  178. var score = this.getScoreFunction(search);
  179. return function(item) {
  180. //console.log('score:item:', item, ', score:', score(item));
  181. if (search && search == item.id) {
  182. return 1;
  183. } else {
  184. return score(item);// score(item) * (1 + Math.min(item.watchers / 100, 1));
  185. }
  186. };
  187. },
  188. load: function(query, callback) {
  189. if (!query.length) return callback();
  190. $.ajax({
  191. url: '{$ajaxDataUrlBase}',
  192. data: 'q=' + encodeURIComponent(query),
  193. type: 'POST',
  194. error: function() {
  195. callback();
  196. },
  197. success: function(res) {
  198. for (var i in res) {
  199. if (!res[i].name || res[i].id != res[i].name) {
  200. res[i].name = res[i].id + ': ' + res[i].name;
  201. }
  202. }
  203. callback(res);
  204. }
  205. });
  206. }
  207. });
  208. });
  209. " . '</script>';
  210. return $out;
  211. }
  212. /**
  213. * @params $query - query string
  214. * @params $strict - search only equal value
  215. * @returns [{id:int, param_out:str, exports:[]}]
  216. */
  217. private function _getValues($query, $strict = false, $params = array()) {
  218. $values = array();
  219. switch ($this->fldName) {
  220. case 'A_ADM_COMPANY':
  221. case 'A_CLASSIFIED':
  222. case 'A_EXEC_GROUP':
  223. Lib::loadClass('UsersHelper');
  224. $userName = User::getLogin();
  225. $userLdapGroups = UsersHelper::getLDAPGroupByUserName($userName);
  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 'DEFAULT_ACL_GROUP':
  255. Lib::loadClass('UsersHelper');
  256. $userLdapGroups = UsersHelper::getLDAPGroupsAll();
  257. 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>';}
  258. if (!empty($userLdapGroups)) {
  259. foreach ($userLdapGroups as $kID => $vLdapGroup) {
  260. $allowGroup = false;
  261. //$allowGroup = $vLdapGroup->gidNumber > 1000;
  262. if ('workgroup' == $vLdapGroup->cn) {
  263. $allowGroup = true;
  264. } else {
  265. $cnTest = str_replace('-', '_', $vLdapGroup->cn);
  266. $cnTest = explode('_', $cnTest);
  267. $cnTest = $cnTest[0];
  268. if (is_numeric($cnTest)) {
  269. $allowGroup = true;
  270. }
  271. }
  272. if ($allowGroup) {
  273. if ($strict) {
  274. if (!empty($query) && $query == $vLdapGroup->cn) {
  275. $values[$vLdapGroup->cn] = (object)array('id'=>$vLdapGroup->cn, 'param_out'=>$vLdapGroup->cn);
  276. }
  277. } else {
  278. $values[$vLdapGroup->cn] = (object)array('id'=>$vLdapGroup->cn, 'param_out'=>$vLdapGroup->cn);
  279. //$values[$vLdapGroup->cn] = $vLdapGroup->cn;
  280. }
  281. }
  282. }
  283. }
  284. break;
  285. case 'K_OD_KOGO':
  286. $db = DB::getDB();
  287. $OD_KOGO_ADRES_ID = 0;
  288. $sql = "select z2.`ID`
  289. from `CRM_LISTA_ZASOBOW` as z
  290. join `CRM_LISTA_ZASOBOW` as z2 on (z2.`PARENT_ID`=z.`PARENT_ID`)
  291. where
  292. z.`ID`='{$this->fldID}'
  293. and z2.`DESC`='OD_KOGO_ADRES'
  294. ";
  295. 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>';}
  296. $res = $db->query($sql);
  297. while ($r = $db->fetch($res)) {
  298. $OD_KOGO_ADRES_ID = $r->ID;
  299. }
  300. $sqlLimit = 20;
  301. $query = trim($query, ' %');
  302. $query = $db->_($query);
  303. $sqlSearch = "k.`K_OD_KOGO` like '%{$query}%'";
  304. if ($strict) {
  305. if (!empty($query)) {
  306. $sqlLimit = 1;
  307. $sqlSearch = "k.`K_OD_KOGO`='{$query}'";
  308. } else {
  309. return $values;
  310. }
  311. }
  312. $sql = "select k.`K_OD_KOGO`, k.`OD_KOGO_ADRES`
  313. from `IN7_DZIENNIK_KORESP` as k
  314. where {$sqlSearch}
  315. group by k.`K_OD_KOGO`
  316. limit {$sqlLimit}
  317. ";
  318. 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>';}
  319. $res = $db->query($sql);
  320. while ($r = $db->fetch($res)) {
  321. $values[] = (object)array('id'=>$r->K_OD_KOGO, 'param_out'=>$r->K_OD_KOGO, 'exports'=>array($OD_KOGO_ADRES_ID => $r->OD_KOGO_ADRES));
  322. }
  323. 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>';}
  324. break;
  325. case 'OD_KOGO_ADRES':
  326. $db = DB::getDB();
  327. $query = trim($query, ' %');
  328. $query = $db->_($query);
  329. $sqlLimit = 20;
  330. $sqlSearch = "k.`OD_KOGO_ADRES` like '%{$query}%'";
  331. if ($strict) {
  332. if (!empty($query)) {
  333. $sqlLimit = 1;
  334. $sqlSearch = "k.`OD_KOGO_ADRES`='{$query}'";
  335. } else {
  336. return $values;
  337. }
  338. }
  339. $sql = "select k.`OD_KOGO_ADRES`
  340. from `IN7_DZIENNIK_KORESP` as k
  341. where {$sqlSearch}
  342. group by k.`OD_KOGO_ADRES`
  343. limit {$sqlLimit}
  344. ";
  345. 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>';}
  346. $res = $db->query($sql);
  347. while ($r = $db->fetch($res)) {
  348. $values[] = (object)array('id'=>$r->OD_KOGO_ADRES, 'param_out'=>$r->OD_KOGO_ADRES);
  349. }
  350. 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>';}
  351. break;
  352. case 'M_DISTRIBUTOR':
  353. $db = DB::getDB();
  354. $query = trim($query, ' %');
  355. $query = $db->_($query);
  356. $sqlLimit = 20;
  357. $sqlSearch = "p.`M_DISTRIBUTOR` like '%{$query}%'";
  358. if ($strict) {
  359. if (!empty($query)) {
  360. $sqlLimit = 1;
  361. $sqlSearch = "p.`M_DISTRIBUTOR`='{$query}'";
  362. } else {
  363. return $values;
  364. }
  365. }
  366. $sql = "select p.`M_DISTRIBUTOR`
  367. from `IN7_MK_BAZA_DYSTRYBUCJI` as p
  368. where p.`M_DISTRIBUTOR` like '%{$query}%'
  369. group by p.`M_DISTRIBUTOR`
  370. limit {$sqlLimit}
  371. ";
  372. 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>';}
  373. $res = $db->query($sql);
  374. while ($r = $db->fetch($res)) {
  375. $values[] = (object)array('id'=>$r->M_DISTRIBUTOR, 'param_out'=>$r->M_DISTRIBUTOR);
  376. }
  377. 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>';}
  378. break;
  379. case '__NESTED_GROUPS': {
  380. $db = DB::getDB();
  381. $query = trim($query, ' %');
  382. $query = $db->_($query);
  383. $sqlLimit = 20;
  384. $sql = "select z.`ID`, z.`DESC`, z.`TYPE`
  385. from `CRM_LISTA_ZASOBOW` as z
  386. where z.`A_STATUS` in('NORMAL', 'WAITING')
  387. and z.`TYPE` in('STANOWISKO', 'PODMIOT', 'DZIAL')
  388. and (z.`DESC` like '%{$query}%' or z.`ID` like '%{$query}%')
  389. group by z.`DESC`
  390. limit {$sqlLimit}
  391. ";
  392. 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>';}
  393. $res = $db->query($sql);
  394. while ($r = $db->fetch($res)) {
  395. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->TYPE . ' ' . $r->DESC);
  396. }
  397. 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>';}
  398. break;
  399. }
  400. case '__USER_GROUPS': {
  401. $db = DB::getDB();
  402. $query = trim($query, ' %');
  403. $query = $db->_($query);
  404. $sqlLimit = 20;
  405. $sql = "select z.`ID`, z.`DESC`, z.`TYPE`
  406. from `CRM_LISTA_ZASOBOW` as z
  407. where z.`A_STATUS` in('NORMAL', 'WAITING')
  408. and z.`TYPE` in('STANOWISKO', 'PODMIOT', 'DZIAL')
  409. and (z.`DESC` like '%{$query}%' or z.`ID` like '%{$query}%')
  410. group by z.`DESC`
  411. limit {$sqlLimit}
  412. ";
  413. 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>';}
  414. $res = $db->query($sql);
  415. while ($r = $db->fetch($res)) {
  416. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->TYPE . ' ' . $r->DESC);
  417. }
  418. 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>';}
  419. break;
  420. }
  421. case '__TELBOXES': {
  422. $db = DB::getDB();
  423. $query = trim($query, ' %');
  424. $query = $db->_($query);
  425. $sqlLimit = 20;
  426. $sql = "select tx.`ID`, tx.`T_TELBOX_NAME`, tx.`T_TELBOX_TYPE`
  427. from `TELBOXES` as tx
  428. where
  429. tx.`A_STATUS`!='DELETED'
  430. and (tx.`ID` like '%{$query}%' or tx.`T_TELBOX_NAME` like '%{$query}%')
  431. order by tx.`T_TELBOX_NAME`
  432. limit {$sqlLimit}
  433. ";
  434. 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>';}
  435. $res = $db->query($sql);
  436. while ($r = $db->fetch($res)) {
  437. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->T_TELBOX_NAME . ' ' . $r->T_TELBOX_TYPE);
  438. }
  439. 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>';}
  440. break;
  441. }
  442. case '__TELBOXES_NAME': {
  443. $db = DB::getDB();
  444. $query = trim($query, ' %');
  445. $query = $db->_($query);
  446. $sqlLimit = 20;
  447. $sql = "select tx.`ID`, tx.`T_TELBOX_NAME`, tx.`T_TELBOX_TYPE`
  448. from `TELBOXES` as tx
  449. where
  450. tx.`A_STATUS`!='DELETED'
  451. and (tx.`ID` like '%{$query}%' or tx.`T_TELBOX_NAME` like '%{$query}%')
  452. order by tx.`T_TELBOX_NAME`
  453. limit {$sqlLimit}
  454. ";
  455. 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>';}
  456. $res = $db->query($sql);
  457. while ($r = $db->fetch($res)) {
  458. $values[] = (object)array('id'=>$r->T_TELBOX_NAME, 'param_out'=>$r->T_TELBOX_NAME . ' ' . $r->T_TELBOX_TYPE);
  459. }
  460. 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>';}
  461. break;
  462. }
  463. case '__DEALS_SALES_TELBOXES_NAME': {
  464. $db = DB::getDB();
  465. $query = trim($query, ' %');
  466. $query = $db->_($query);
  467. $sqlLimit = 20;
  468. $sql = "select x.`id`, x.`label`
  469. from (
  470. select d.`T_TELBOX_NEIGHBOUR_IN` as `id`
  471. , d.`marka` as `label`
  472. from `DEALS_SALES` d
  473. where d.`A_STATUS`!='DELETED'
  474. group by d.`T_TELBOX_NEIGHBOUR_IN`, d.`marka`
  475. ) as x
  476. where x.`id` like '%{$query}%' or x.`label` like '%{$query}%'
  477. order by x.`label`
  478. limit {$sqlLimit}
  479. ";
  480. 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>';}
  481. $res = $db->query($sql);
  482. while ($r = $db->fetch($res)) {
  483. $values[] = (object)array('id'=>$r->id . ': '. $r->label, 'param_out'=>'');
  484. }
  485. 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>';}
  486. break;
  487. }
  488. case '__ZALICZKI_NAJEMCOW__NAJEMCA': {
  489. $pdo = DB::getPDO();
  490. $query = trim($query, ' %');
  491. $sqlLimit = 20;
  492. $sql = "select x.`id`, x.`label`
  493. from (
  494. select d.`NAJEMCA` as `id`
  495. , d.`NAJEMCA` as `label`
  496. from `ZALICZKI_NAJEMCOW` d
  497. where d.`A_STATUS`!='DELETED'
  498. and d.`NAJEMCA` like :query_like
  499. group by d.`NAJEMCA`
  500. ) as x
  501. order by x.`label`
  502. limit {$sqlLimit}
  503. ";
  504. DBG::_('DBG_TS', '>2', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
  505. $sth = $pdo->prepare($sql);
  506. $sth->bindValue(':query_like', "%{$query}%", PDO::PARAM_STR);
  507. $sth->execute();
  508. $list = $sth->fetchAll();
  509. foreach ($list as $item) {
  510. $values[] = (object)array('id'=>$item['id'], 'param_out'=>'');
  511. }
  512. DBG::_('DBG_TS', '>2', "values", $values, __CLASS__, __FUNCTION__, __LINE__);
  513. break;
  514. }
  515. case '__ZASOB': {
  516. $db = DB::getDB();
  517. $sqlQuery = new stdClass();
  518. $sqlQuery->limit = 20;
  519. $sqlQuery->orderBy = "";
  520. $sqlQuery->selectLabel = "concat(z.`TYPE`, ' ', z.`DESC`)";
  521. $sqlQuery->selectAddBestFit = "";
  522. $sqlQuery->whereAddQueryByWords = "";
  523. $sqlWhereAddType = "";
  524. if (!empty($params['zasob_type_in'])) {
  525. if (is_scalar($params['zasob_type_in'])) $params['zasob_type_in'] = array($params['zasob_type_in']);
  526. $sqlWhereAddType = " and z.`TYPE` IN('" . implode("','", $params['zasob_type_in']) . "') ";
  527. if (in_array('KOMORKA', $params['zasob_type_in'])) {
  528. $sqlQuery->selectLabel = "concat(z.`TYPE`, ' ', z.`DESC` , ' (', (select zp.`DESC` from `CRM_LISTA_ZASOBOW` as zp where zp.`ID`=z.`PARENT_ID` limit 1), ')')";
  529. }
  530. }
  531. $query = trim($query, ' %');
  532. $sqlQuery->_queryByWords = array();
  533. if (is_numeric($query)) {
  534. $sqlQueryNum = intval($query);
  535. $sqlQuery->_queryByWords[] = "z.`ID` like '%{$sqlQueryNum}%'";
  536. $sqlQuery->selectAddBestFit = "
  537. , IF (z.`ID`='{$sqlQueryNum}', 1000,
  538. IF (z.`ID` like '{$sqlQueryNum}%', 900, 100)
  539. ) as _bestFit
  540. ";
  541. $sqlQuery->orderBy = "order by _bestFit DESC";
  542. } else {
  543. $queryWhereBuilder = new SqlQueryWhereBuilder();
  544. $searchWords = $queryWhereBuilder->splitQueryToWords($query);
  545. DBG::_('DBG_TS', '>2', "SqlQueryWhereBuilder->splitQueryToWords({$query})", $searchWords, __CLASS__, __FUNCTION__, __LINE__);
  546. $sqlWords = array();
  547. if (!empty($searchWords)) {
  548. foreach ($searchWords as $word) {
  549. if (is_numeric($word)) {
  550. $sqlWord = intval($word);
  551. $sqlQuery->_queryByWords[] = " ( z.`ID` like '%{$sqlWord}%' or z.`DESC` like '%{$sqlWord}%' ) ";
  552. } else {
  553. $sqlWord = $db->_($word);
  554. $sqlQuery->_queryByWords[] = "z.`DESC` like '%{$sqlWord}%'";
  555. }
  556. }
  557. }
  558. }
  559. if (!empty($sqlQuery->_queryByWords)) {
  560. $sqlQuery->whereAddQueryByWords = " and (" . implode(" and ", $sqlQuery->_queryByWords) . ")";
  561. }
  562. $sql = "select z.`ID`
  563. , {$sqlQuery->selectLabel} as `LABEL`
  564. {$sqlQuery->selectAddBestFit}
  565. from `CRM_LISTA_ZASOBOW` as z
  566. where z.`A_STATUS` in('NORMAL', 'WAITING')
  567. {$sqlQuery->whereAddQueryByWords}
  568. {$sqlWhereAddType}
  569. {$sqlQuery->orderBy}
  570. limit {$sqlQuery->limit}
  571. ";
  572. DBG::_('DBG_TS', '>2', "sql", $sql, __CLASS__, __FUNCTION__, __LINE__);
  573. $res = $db->query($sql);
  574. while ($r = $db->fetch($res)) {
  575. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->LABEL);
  576. }
  577. DBG::_('DBG_TS', '>2', "values", $values, __CLASS__, __FUNCTION__, __LINE__);
  578. break;
  579. }
  580. case '__PROCES': {
  581. $db = DB::getDB();
  582. $query = trim($query, ' %');
  583. $query = $db->_($query);
  584. $sqlLimit = 20;
  585. $sqlSelectLabel = "concat(z.`TYPE`, ' ', z.`DESC`)";
  586. $sqlWhereAdd = "";
  587. if (is_numeric($query)) {
  588. $sql = "select z.`ID`
  589. , {$sqlSelectLabel} as `LABEL`
  590. , IF (z.`ID`='{$query}', 1000,
  591. IF (z.`ID` like '{$query}%', 900, 100)
  592. ) as _bestFit
  593. from `CRM_PROCES` as z
  594. where z.`A_STATUS` in('NORMAL', 'WAITING')
  595. and (z.`DESC` like '%{$query}%' or z.`ID` like '%{$query}%')
  596. {$sqlWhereAdd}
  597. order by _bestFit DESC
  598. limit {$sqlLimit}
  599. ";
  600. } else {
  601. $sql = "select z.`ID`
  602. , {$sqlSelectLabel} as `LABEL`
  603. from `CRM_PROCES` as z
  604. where z.`A_STATUS` in('NORMAL', 'WAITING')
  605. and (z.`DESC` like '%{$query}%' or z.`ID` like '%{$query}%')
  606. {$sqlWhereAdd}
  607. limit {$sqlLimit}
  608. ";
  609. }
  610. 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>';}
  611. $res = $db->query($sql);
  612. while ($r = $db->fetch($res)) {
  613. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->LABEL);
  614. }
  615. 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>';}
  616. break;
  617. }
  618. case '__COMPANIES': {
  619. $db = DB::getDB();
  620. $query = trim($query, ' %');
  621. $query = $db->_($query);
  622. $sqlLimit = 20;
  623. $sql = "select c.`ID`, c.`P_NAME`, c.`P_NIP`
  624. from `COMPANIES` as c
  625. where c.`A_STATUS` in('NORMAL', 'WAITING')
  626. and (c.`P_NAME` like '%{$query}%' or c.`P_NIP` like '%{$query}%' or c.`ID` like '%{$query}%')
  627. limit {$sqlLimit}
  628. ";
  629. 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>';}
  630. $res = $db->query($sql);
  631. while ($r = $db->fetch($res)) {
  632. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->P_NAME . ' ' . $r->P_NIP);
  633. }
  634. 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>';}
  635. break;
  636. }
  637. case 'VERSION_GIT': {
  638. $gitPath = '/Library/Server/Web/Data/Sites/SE-production-git/';
  639. $versions = array();
  640. if (file_exists($gitPath)) {
  641. $cmd = "cd {$gitPath} && git tag -l| sort -r -n -t. -k1,1 -k2,2 -k3,3 -k4,4|head -10";
  642. $cmdOut = null; $cmdRet = null;
  643. exec($cmd, $cmdOut, $cmdRet);
  644. if ($cmdRet == 0) {
  645. if (!empty($cmdOut)) {
  646. foreach ($cmdOut as $tag) {
  647. array_unshift($versions, $tag);
  648. }
  649. }
  650. }
  651. }
  652. foreach ($versions as $version) {
  653. if (!empty($query)) {
  654. if ($strict) {
  655. if ($version != $query) {
  656. continue;
  657. }
  658. }
  659. else {
  660. if (false === strpos($version, $query)) {
  661. continue;
  662. }
  663. }
  664. }
  665. $values[] = (object)array('id'=>$version, 'param_out'=>$version);
  666. }
  667. }
  668. case '__USER_ID': {
  669. $db = DB::getDB();
  670. $query = trim($query, ' %');
  671. $query = $db->_($query);
  672. $sqlLimit = 20;
  673. $sqlSelectLabel = array();
  674. $sqlSelectLabel[] = 'u.`ADM_NAME`';
  675. $sqlSelectLabel[] = "' ('";
  676. $sqlSelectLabel[] = 'u.`ADM_ACCOUNT`';
  677. $sqlSelectLabel[] = "', '";
  678. $sqlSelectLabel[] = 'u.`EMAIL`';
  679. $sqlSelectLabel[] = "', '";
  680. $sqlSelectLabel[] = 'u.`ADM_PHONE`';
  681. $sqlSelectLabel[] = "')'";
  682. $sqlSelectLabel = "concat(" . implode(", ", $sqlSelectLabel) . ")";
  683. $sql = "select u.`ID`
  684. , {$sqlSelectLabel} as `LABEL`
  685. from `ADMIN_USERS` as u
  686. where u.`A_STATUS` in('NORMAL', 'WAITING','MONITOR','WARNING')
  687. and u.`EMPLOYEE_TYPE` in('Pracownik','Partner')
  688. and (u.`ID` like '%{$query}%'
  689. or u.`ADM_ACCOUNT` like '%{$query}%'
  690. or u.`ADM_NAME` like '%{$query}%'
  691. or u.`ADM_PHONE` like '%{$query}%'
  692. or u.`EMAIL` like '%{$query}%'
  693. )
  694. limit {$sqlLimit}
  695. ";
  696. 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>';}
  697. $res = $db->query($sql);
  698. while ($r = $db->fetch($res)) {
  699. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->LABEL);
  700. }
  701. 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>';}
  702. break;
  703. }
  704. case '__USER_LOGIN': {
  705. $db = DB::getDB();
  706. $query = trim($query, ' %');
  707. $query = $db->_($query);
  708. $sqlLimit = 20;
  709. $sqlSelectLabel = array();
  710. $sqlSelectLabel[] = 'u.`ADM_NAME`';
  711. $sqlSelectLabel[] = "' ('";
  712. $sqlSelectLabel[] = 'u.`ADM_ACCOUNT`';
  713. $sqlSelectLabel[] = "', '";
  714. $sqlSelectLabel[] = 'u.`EMAIL`';
  715. $sqlSelectLabel[] = "', '";
  716. $sqlSelectLabel[] = 'u.`ADM_PHONE`';
  717. $sqlSelectLabel[] = "')'";
  718. $sqlSelectLabel = "concat(" . implode(", ", $sqlSelectLabel) . ")";
  719. $sql = "select u.`ADM_ACCOUNT` as ID
  720. , {$sqlSelectLabel} as `LABEL`
  721. from `ADMIN_USERS` as u
  722. where u.`A_STATUS` in('NORMAL', 'WAITING','MONITOR','WARNING')
  723. and u.`EMPLOYEE_TYPE` in('Pracownik','Partner')
  724. and (u.`ID` like '%{$query}%'
  725. or u.`ADM_ACCOUNT` like '%{$query}%'
  726. or u.`ADM_NAME` like '%{$query}%'
  727. or u.`ADM_PHONE` like '%{$query}%'
  728. or u.`EMAIL` like '%{$query}%'
  729. )
  730. limit {$sqlLimit}
  731. ";
  732. 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>';}
  733. $res = $db->query($sql);
  734. while ($r = $db->fetch($res)) {
  735. $values[] = (object)array('id'=>$r->ID, 'param_out'=>$r->LABEL);
  736. }
  737. 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>';}
  738. return $values;
  739. }
  740. case 'L_APPOITMENT_USER': {
  741. $query = trim($query, ' %');
  742. $acl = User::getAcl()->getObjectAcl('default_db', 'ADMIN_USERS');
  743. $queryFeatures = $acl->buildQuery([
  744. (!empty($query))
  745. ? [null, 'or', [
  746. (is_numeric($query)) ? ['ID', 'like', "{$query}%"] : null,
  747. ['ADM_ACCOUNT', 'like', "%{$query}%"],
  748. ['ADM_NAME', 'like', "%{$query}%"],
  749. ['ADM_PHONE', 'like', "%{$query}%"],
  750. ['EMAIL', 'like', "%{$query}%"],
  751. ] ]
  752. : null,
  753. 'f_A_STATUS' => '=NORMAL',
  754. 'f_ADM_TECH_WORKER' => '!NO',
  755. 'limit' => 20
  756. ]);
  757. return array_map(function ($item) {
  758. return (object)[
  759. 'id' => $item['ID'],
  760. 'param_out' => "{$item['ADM_NAME']} ({$item['ADM_ACCOUNT']})"
  761. ];
  762. }, $queryFeatures->getItems());
  763. } break;
  764. default:
  765. }
  766. return $values;
  767. }
  768. /**
  769. * @returns object
  770. * {
  771. * ext_tbl_id: "1466",
  772. * ext_row_id: "2975",
  773. * id: "2975",
  774. * tbl_label: "Zasoby",
  775. * link_type_id: "5",
  776. * link_type: "NestedGroups",
  777. * },
  778. */
  779. public function getReturnData($tblId, $rowId, $fieldName) {
  780. $retData = new stdClass();
  781. $retData->items = array();
  782. $values = array();
  783. switch ($this->fldName) {
  784. case '__CONNECTIONS': {
  785. $linkTypes = array();
  786. $tblLabels = array();
  787. $db = DB::getDB();
  788. $sqlLimit = 21;
  789. $sql = "select l.*
  790. from `ITEM_LINKS` as l
  791. where l.`A_STATUS` in('NORMAL', 'WAITING')
  792. and (
  793. (l.`TABLE_1_ZASOB_ID`={$tblId} and l.`TABLE_1_ID`={$rowId})
  794. or
  795. (l.`TABLE_2_ZASOB_ID`={$tblId} and l.`TABLE_2_ID`={$rowId})
  796. )
  797. and l.`LINKS_TYPE_ID`>1
  798. limit {$sqlLimit}
  799. ";
  800. 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>';}
  801. $res = $db->query($sql);
  802. while ($r = $db->fetch($res)) {
  803. $connObj = new stdClass();
  804. if ($r->TABLE_1_ZASOB_ID == $tblId && $r->TABLE_1_ID == $rowId) {
  805. $connObj->ext_tbl_id = $r->TABLE_2_ZASOB_ID;
  806. $connObj->ext_row_id = $r->TABLE_2_ID;
  807. $connObj->id = $r->TABLE_2_ID;
  808. $connObj->label = "{$r->TABLE_2_ZASOB_ID}({$r->TABLE_2_ID})";
  809. }
  810. else if ($r->TABLE_2_ZASOB_ID == $tblId && $r->TABLE_2_ID == $rowId) {
  811. $connObj->ext_tbl_id = $r->TABLE_1_ZASOB_ID;
  812. $connObj->ext_row_id = $r->TABLE_1_ID;
  813. $connObj->id = $r->TABLE_1_ID;
  814. $connObj->label = "{$r->TABLE_1_ZASOB_ID}({$r->TABLE_1_ID})";
  815. }
  816. if ($connObj) {
  817. $connObj->link_type_id = $r->LINKS_TYPE_ID;
  818. $linkTypes[$connObj->link_type_id] = '';
  819. $tblLabels[$connObj->ext_tbl_id] = '';
  820. $retData->items[] = $connObj;
  821. }
  822. }
  823. if (!empty($linkTypes)) {
  824. $sql = "select lt.`ID`, lt.`NAME`
  825. from `ITEM_LINK_TYPES` as lt
  826. where lt.`ID` in (" . implode(", ", array_keys($linkTypes)) . ")
  827. ";
  828. $res = $db->query($sql);
  829. while ($r = $db->fetch($res)) {
  830. $linkTypes[$r->ID] = $r->NAME;
  831. }
  832. }
  833. if (!empty($tblLabels)) {
  834. $sql = "select z.`ID`, z.`DESC`, z.`DESC_PL`, z.`OPIS`
  835. from `CRM_LISTA_ZASOBOW` as z
  836. where z.`ID` in (" . implode(", ", array_keys($tblLabels)) . ")
  837. ";
  838. $res = $db->query($sql);
  839. while ($r = $db->fetch($res)) {
  840. if (!empty($r->DESC_PL)) {
  841. $tblLabels[$r->ID] = $r->DESC_PL;
  842. } else if (!empty($r->OPIS)) {
  843. $tblLabels[$r->ID] = V::strShortUtf8($r->OPIS, 20);
  844. } else {
  845. $tblLabels[$r->ID] = $r->DESC;
  846. }
  847. }
  848. }
  849. foreach ($retData->items as $k => $connObj) {
  850. $connObj->link_type = V::get($connObj->link_type_id, $connObj->link_type_id, $linkTypes);
  851. $connObj->tbl_label = V::get($connObj->ext_tbl_id, $connObj->ext_tbl_id, $tblLabels);
  852. }
  853. 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>';}
  854. return $retData;
  855. break;
  856. }
  857. default:
  858. }
  859. return null;
  860. }
  861. }