TypespecialVariable.php 29 KB

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