TypespecialVariable.php 31 KB

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