TypespecialVariable.php 28 KB

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