TypespecialVariable.php 28 KB

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