TypespecialVariable.php 32 KB

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