api.custmaps.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. <?php
  2. /**
  3. * Custom users maps class
  4. */
  5. class CustomMaps {
  6. protected $allMaps = array();
  7. protected $allItems = array();
  8. protected $ymapsCfg = array();
  9. protected $altCfg = array();
  10. protected $itemTypes = array();
  11. protected $center = '';
  12. protected $zoom = '';
  13. const UPLOAD_PATH = 'exports/';
  14. const EX_NO_MAP_ID = 'NOT_EXISTING_MAP_ID';
  15. const EX_NO_ITM_ID = 'NOT_EXISTING_ITEM_ID';
  16. const EX_NO_FILE = 'NOT_EXISTING_FILE';
  17. const EX_WRONG_EXT = 'WRONG_FILE_EXTENSION';
  18. const EX_WRONG_KML = 'WRONG_KML_FILE_FORMAT';
  19. public function __construct() {
  20. $this->loadYmapsConfig();
  21. $this->loadAlterConfig();
  22. $this->setDefaults();
  23. $this->setItemTypes();
  24. $this->loadMaps();
  25. $this->loadItems();
  26. }
  27. /**
  28. * Loads system-wide ymaps config into private config storage
  29. *
  30. * @global object $ubillingConfig
  31. */
  32. protected function loadYmapsConfig() {
  33. global $ubillingConfig;
  34. $this->ymapsCfg = $ubillingConfig->getYmaps();
  35. }
  36. /**
  37. * Loads system-wide alter config into private config storage
  38. *
  39. * @global object $ubillingConfig
  40. */
  41. protected function loadAlterConfig() {
  42. global $ubillingConfig;
  43. $this->altCfg = $ubillingConfig->getAlter();
  44. }
  45. /**
  46. * Loads existing custom maps into private data property
  47. *
  48. * @return void
  49. */
  50. protected function loadMaps() {
  51. $query = "SELECT * from `custmaps` ORDER by `id` ASC";
  52. $all = simple_queryall($query);
  53. if (!empty($all)) {
  54. foreach ($all as $io => $each) {
  55. $this->allMaps[$each['id']] = $each;
  56. }
  57. }
  58. }
  59. /**
  60. * Sets default map center and zoom
  61. *
  62. * @return void
  63. */
  64. protected function setDefaults() {
  65. $this->center = $this->ymapsCfg['CENTER'];
  66. $this->zoom = $this->ymapsCfg['ZOOM'];
  67. }
  68. /**
  69. * Sets available item types into private data property
  70. *
  71. * @return void
  72. */
  73. protected function setItemTypes() {
  74. $this->itemTypes = array(
  75. 'pillar' => __('Pillar'),
  76. 'sump' => __('Sump'),
  77. 'coupling' => __('Coupling'),
  78. 'node' => __('Node'),
  79. 'box' => __('Box'),
  80. 'amplifier' => __('Amplifier'),
  81. 'optrec' => __('Optical reciever')
  82. );
  83. }
  84. /**
  85. * Returns item type localized name
  86. *
  87. * @param string $type
  88. * @return string
  89. */
  90. protected function itemGetTypeName($type) {
  91. $result = '';
  92. if (isset($this->itemTypes[$type])) {
  93. $result = $this->itemTypes[$type];
  94. } else {
  95. $result.=__('Unknown');
  96. }
  97. return ($result);
  98. }
  99. /**
  100. * Loads all existing custom maps items into private data property
  101. *
  102. * @return void
  103. */
  104. protected function loadItems() {
  105. $query = "SELECT * from `custmapsitems`";
  106. $all = simple_queryall($query);
  107. if (!empty($all)) {
  108. foreach ($all as $io => $each) {
  109. $this->allItems[$each['id']] = $each;
  110. }
  111. }
  112. }
  113. /**
  114. * Sets override map center
  115. *
  116. * @param string $coords
  117. */
  118. public function setCenter($coords) {
  119. $this->center = $coords;
  120. }
  121. /**
  122. * Sets map override zoom
  123. *
  124. * @param int $zoom
  125. */
  126. public function setZoom($zoom) {
  127. $this->zoom = $zoom;
  128. }
  129. /**
  130. * Filters some layer from current
  131. *
  132. * @param string $layers
  133. * @param string $filter
  134. * @return string
  135. */
  136. protected function filterLayers($layers, $filter) {
  137. $result = str_replace($filter, '', $layers);
  138. return ($result);
  139. }
  140. /**
  141. * Returns map controls
  142. *
  143. * @return string
  144. */
  145. protected function mapControls() {
  146. $result = '';
  147. $result.=wf_BackLink('?module=custmaps');
  148. if (wf_CheckGet(array('showmap'))) {
  149. $mapId = $_GET['showmap'];
  150. if (cfr('CUSTMAPEDIT')) {
  151. $result.=wf_Link('?module=custmaps&showmap=' . $mapId . '&mapedit=true', wf_img('skins/ymaps/edit.png') . ' ' . __('Edit'), false, 'ubButton');
  152. }
  153. //custom layers
  154. if (wf_CheckGet(array('cl'))) {
  155. $custLayers = $_GET['cl'];
  156. } else {
  157. $custLayers = '';
  158. }
  159. //system layers
  160. if (wf_CheckGet(array('layers'))) {
  161. $curLayers = $_GET['layers'];
  162. } else {
  163. $curLayers = '';
  164. }
  165. $result.=wf_Link('?module=custmaps&showitems=' . $mapId, wf_img('skins/icon_table.png') . ' ' . __('Objects'), false, 'ubButton');
  166. $result.=wf_delimiter();
  167. $result.=wf_Link('?module=custmaps&showmap=' . $mapId, wf_img('skins/icon_cleanup.png') . ' ' . $this->mapGetName($mapId), false, 'ubButton');
  168. foreach ($this->allMaps as $cmapId => $cmapData) {
  169. if ($cmapId != $mapId) {
  170. $result.=wf_Link('?module=custmaps&showmap=' . $mapId . '&layers=' . $curLayers . '&cl=' . $cmapId . 'z' . $this->filterLayers($custLayers, $cmapId . 'z'), wf_img('skins/swmapsmall.png') . ' ' . $this->mapGetName($cmapId), false, 'ubButton');
  171. }
  172. }
  173. $result.=wf_Link('?module=custmaps&showmap=' . $mapId . '&layers=bs' . $this->filterLayers($curLayers, 'bs') . '&cl=' . $custLayers, wf_img('skins/ymaps/build.png') . ' ' . __('Builds map'), false, 'ubButton');
  174. $result.=wf_Link('?module=custmaps&showmap=' . $mapId . '&layers=sw' . $this->filterLayers($curLayers, 'sw') . '&cl=' . $custLayers, wf_img('skins/ymaps/network.png') . ' ' . __('Switches map'), false, 'ubButton');
  175. $result.=wf_Link('?module=custmaps&showmap=' . $mapId . '&layers=ul' . $this->filterLayers($curLayers, 'ul') . '&cl=' . $custLayers, wf_img('skins/ymaps/uplinks.png') . ' ' . __('Show links'), false, 'ubButton');
  176. }
  177. $result.=wf_delimiter();
  178. return ($result);
  179. }
  180. /**
  181. * Returns empty map container
  182. *
  183. * @return string
  184. */
  185. protected function mapContainer() {
  186. $container = generic_MapContainer('100%', '800px', 'custmap');
  187. return ($container);
  188. }
  189. /**
  190. * Returns existing maps list view
  191. *
  192. * @return string
  193. */
  194. public function renderMapList() {
  195. $messages = new UbillingMessageHelper();
  196. $result = $this->mapListControls();
  197. $cells = wf_TableCell(__('ID'));
  198. $cells.= wf_TableCell(__('Name'));
  199. $cells.= wf_TableCell(__('Actions'));
  200. $rows = wf_TableRow($cells, 'row1');
  201. if (!empty($this->allMaps)) {
  202. foreach ($this->allMaps as $io => $each) {
  203. $cells = wf_TableCell($each['id']);
  204. $nameLink = wf_Link('?module=custmaps&showmap=' . $each['id'], $each['name'], false);
  205. $cells.= wf_TableCell($nameLink);
  206. $actLinks = '';
  207. if (cfr('CUSTMAPEDIT')) {
  208. $actLinks.= wf_JSAlertStyled('?module=custmaps&deletemap=' . $each['id'], web_delete_icon(), $messages->getDeleteAlert()) . ' ';
  209. $actLinks.= wf_modalAuto(web_edit_icon(), __('Edit'), $this->mapEditForm($each['id']));
  210. }
  211. $actLinks.= wf_Link('?module=custmaps&showmap=' . $each['id'], wf_img('skins/icon_search_small.gif', __('Show')), false);
  212. $actLinks.= wf_Link('?module=custmaps&showitems=' . $each['id'], wf_img('skins/icon_table.png', __('Objects')), false);
  213. $cells.= wf_TableCell($actLinks);
  214. $rows.= wf_TableRow($cells, 'row3');
  215. }
  216. }
  217. $result.= wf_TableBody($rows, '100%', '0', 'sortable');
  218. return ($result);
  219. }
  220. /**
  221. * Returns item edit form
  222. *
  223. * @param int $itemid
  224. * @return string
  225. */
  226. public function itemEditForm($itemid) {
  227. $itemid = vf($itemid, 3);
  228. $result = '';
  229. if (isset($this->allItems[$itemid])) {
  230. $result.= wf_BackLink('?module=custmaps&showitems=' . $this->allItems[$itemid]['mapid']);
  231. $result.= wf_delimiter();
  232. $inputs = wf_HiddenInput('edititemid', $itemid);
  233. $inputs.= wf_Selector('edititemtype', $this->itemTypes, __('Type'), $this->allItems[$itemid]['type'], true);
  234. $inputs.= wf_TextInput('edititemgeo', __('Geo location'), $this->allItems[$itemid]['geo'], true, '20', 'geo');
  235. $inputs.= wf_TextInput('edititemname', __('Name'), $this->allItems[$itemid]['name'], true, '20');
  236. $inputs.= wf_TextInput('edititemlocation', __('Location'), $this->allItems[$itemid]['location'], true, '20');
  237. $inputs.= wf_Submit(__('Save'));
  238. $result.= wf_Form('', 'POST', $inputs, 'glamour');
  239. } else {
  240. throw new Exception(self::EX_NO_ITM_ID);
  241. }
  242. return ($result);
  243. }
  244. /**
  245. * Changes existing item properties in database
  246. *
  247. * @param string $itemid
  248. * @param string $type
  249. * @param string $geo
  250. * @param string $name
  251. * @param string $location
  252. * @throws Exception
  253. */
  254. public function itemEdit($itemid, $type, $geo, $name, $location) {
  255. $itemid = vf($itemid, 3);
  256. if (isset($this->allItems[$itemid])) {
  257. simple_update_field('custmapsitems', 'name', $name, "WHERE `id`='" . $itemid . "'");
  258. simple_update_field('custmapsitems', 'type', $type, "WHERE `id`='" . $itemid . "'");
  259. simple_update_field('custmapsitems', 'geo', $geo, "WHERE `id`='" . $itemid . "'");
  260. simple_update_field('custmapsitems', 'location', $location, "WHERE `id`='" . $itemid . "'");
  261. log_register('CUSTMAPS EDIT ITEM [' . $itemid . ']');
  262. } else {
  263. throw new Exception(self::EX_NO_ITM_ID);
  264. }
  265. }
  266. /**
  267. * Returns items import form
  268. *
  269. * @return string
  270. */
  271. protected function itemsImportForm() {
  272. $inputs = wf_tag('form', false, 'glamour', 'action="" enctype="multipart/form-data" method="POST"');
  273. $inputs.= wf_tag('input', false, '', 'type="file" name="itemsUploadFile"');
  274. $inputs.= wf_tag('br');
  275. $inputs.= wf_Selector('itemsUploadTypes', $this->itemTypes, __('Type'), '', true);
  276. $inputs.= wf_Submit(__('Upload'));
  277. $inputs.= wf_tag('form', true);
  278. $result = $inputs;
  279. return ($result);
  280. }
  281. /**
  282. * Catches file upload
  283. *
  284. * @return string
  285. */
  286. public function catchFileUpload() {
  287. $result = '';
  288. $allowedExtensions = array("kml", "txt");
  289. $fileAccepted = true;
  290. foreach ($_FILES as $file) {
  291. if ($file['tmp_name'] > '') {
  292. if (@!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) {
  293. $fileAccepted = false;
  294. }
  295. }
  296. }
  297. if ($fileAccepted) {
  298. $newFilename = zb_rand_string(10) . '_custmap.kml';
  299. $newSavePath = self::UPLOAD_PATH . $newFilename;
  300. move_uploaded_file($_FILES['itemsUploadFile']['tmp_name'], $newSavePath);
  301. if (file_exists($newSavePath)) {
  302. $uploadResult = wf_tag('span', false, 'alert_success') . __('Upload complete') . wf_tag('span', true);
  303. $result = $newFilename;
  304. } else {
  305. $uploadResult = wf_tag('span', false, 'alert_error') . __('Upload failed') . wf_tag('span', true);
  306. }
  307. } else {
  308. $uploadResult = wf_tag('span', false, 'alert_error') . __('Upload failed') . ': ' . self::EX_WRONG_EXT . wf_tag('span', true);
  309. }
  310. show_window('', $uploadResult);
  311. if ($result) {
  312. $this->itemsImportKml($newFilename, $_GET['showitems'], $_POST['itemsUploadTypes']);
  313. }
  314. return ($result);
  315. }
  316. /**
  317. * Extract placemarks to import
  318. *
  319. * @param array $data
  320. * @return array
  321. */
  322. protected function kmlExtractPlacemarks($data) {
  323. $result = array();
  324. $i = 0;
  325. if (!empty($data)) {
  326. foreach ($data as $io => $each) {
  327. if (isset($each['Point'])) {
  328. @$result[$i]['name'] = trim($each['name']);
  329. $coordsRaw = trim($each['Point']['coordinates']);
  330. $coordsRaw = explode(',', $coordsRaw);
  331. $result[$i]['geo'] = $coordsRaw[1] . ', ' . $coordsRaw[0];
  332. $i++;
  333. }
  334. }
  335. }
  336. return ($result);
  337. }
  338. /**
  339. * Performs import of uploaded KML file
  340. *
  341. * @param string $filename
  342. */
  343. protected function itemsImportKml($filename, $mapId, $type) {
  344. $mapId = vf($mapId, 3);
  345. $type = vf($type);
  346. $toImport = array();
  347. $importCount = 0;
  348. if (file_exists(self::UPLOAD_PATH . '/' . $filename)) {
  349. $rawData = file_get_contents(self::UPLOAD_PATH . '/' . $filename);
  350. if (!empty($rawData)) {
  351. $rawData = zb_xml2array($rawData);
  352. if (isset($this->allMaps[$mapId])) {
  353. if (!empty($rawData)) {
  354. if (isset($rawData['kml'])) {
  355. if (isset($rawData['kml']['Document'])) {
  356. $importDocument = $rawData['kml']['Document'];
  357. if (!empty($importDocument)) {
  358. //turbo GPS 3 broken format
  359. foreach ($importDocument as $io => $each) {
  360. if ($io == 'Placemark') {
  361. $toImport = $each;
  362. } else {
  363. //natural google earth format
  364. if (is_array($each)) {
  365. foreach ($each as $ia => $deeper) {
  366. if ($ia == 'Placemark') {
  367. $toImport = $deeper;
  368. }
  369. }
  370. }
  371. }
  372. //extracting placemarks
  373. if (!empty($toImport)) {
  374. $placemarks = $this->kmlExtractPlacemarks($toImport);
  375. if (!empty($placemarks)) {
  376. foreach ($placemarks as $ix => $importPm) {
  377. $this->itemCreate($mapId, $type, $importPm['geo'], $importPm['name'], '');
  378. $importCount++;
  379. }
  380. show_info(__('Objects') . ': ' . $importCount);
  381. show_window('', wf_Link('?module=custmaps&showitems=' . $mapId, wf_img('skins/refresh.gif') . ' ' . __('Renew'), false, 'ubButton'));
  382. }
  383. }
  384. }
  385. }
  386. } else {
  387. show_error(self::EX_WRONG_KML);
  388. }
  389. } else {
  390. show_error(self::EX_WRONG_KML);
  391. }
  392. } else {
  393. show_warning(__('Empty file') . ' ' . self::EX_WRONG_KML);
  394. }
  395. } else {
  396. show_error(self::EX_NO_MAP_ID);
  397. }
  398. } else {
  399. show_warning(__('Empty file') . ' (.kml)');
  400. }
  401. } else {
  402. show_error(self::EX_NO_FILE);
  403. }
  404. }
  405. /**
  406. * Returns existing map items list datatables container
  407. *
  408. * @param int $mapid
  409. *
  410. * @return string
  411. */
  412. public function renderItemsListFast($mapid) {
  413. $mapid = vf($mapid, 3);
  414. $opts = '"order": [[ 0, "desc" ]]';
  415. $columns = array('ID', 'Type', 'Geo location', 'Name', 'Location', 'Actions');
  416. $result = '';
  417. $result.= wf_BackLink('?module=custmaps');
  418. if (cfr('CUSTMAPEDIT')) {
  419. $result.= wf_modalAuto(wf_img('skins/photostorage_upload.png') . ' ' . __('Upload file from HDD'), __('Upload') . ' KML', $this->itemsImportForm(), 'ubButton');
  420. }
  421. $result.=wf_Link('?module=custmaps&showitems=' . $mapid . '&duplicates=true', wf_img('skins/duplicate_icon.gif') . ' ' . __('Show duplicates'), true, 'ubButton');
  422. $result.= wf_delimiter();
  423. $result.=wf_JqDtLoader($columns, '?module=custmaps&ajax=true&showitems=' . $mapid, false, 'Objects', '100', $opts);
  424. return ($result);
  425. }
  426. /**
  427. * Renders custom map items list JSON data
  428. *
  429. * @param int $mapid
  430. *
  431. * @return void
  432. */
  433. public function renderItemsListJsonData($mapid) {
  434. $mapid = vf($mapid, 3);
  435. $json = new wf_JqDtHelper();
  436. $messages = new UbillingMessageHelper();
  437. if ($this->altCfg['ADCOMMENTS_ENABLED']) {
  438. $adcomments = new ADcomments('CUSTMAPITEMS');
  439. $adc = true;
  440. } else {
  441. $adc = false;
  442. }
  443. if (!empty($this->allItems)) {
  444. foreach ($this->allItems as $io => $each) {
  445. $indicator = ($adc) ? $adcomments->getCommentsIndicator($each['id']) : '';
  446. if ($each['mapid'] == $mapid) {
  447. $actLinks = '';
  448. if (cfr('CUSTMAPEDIT')) {
  449. $actLinks.= wf_JSAlertStyled('?module=custmaps&deleteitem=' . $each['id'], web_delete_icon(), $messages->getDeleteAlert()) . ' ';
  450. }
  451. $actLinks.= wf_JSAlertStyled('?module=custmaps&edititem=' . $each['id'], web_edit_icon(), $messages->getEditAlert()) . ' ';
  452. $actLinks.= wf_Link('?module=custmaps&showmap=' . $each['mapid'] . '&locateitem=' . $each['geo'] . '&zoom=' . $this->ymapsCfg['FINDING_ZOOM'], wf_img('skins/icon_search_small.gif', __('Find on map')), false) . ' ';
  453. $actLinks.=$indicator;
  454. $data[] = $each['id'];
  455. $data[] = $this->itemGetTypeName($each['type']);
  456. $data[] = $each['geo'];
  457. $data[] = $each['name'];
  458. $data[] = $each['location'];
  459. $data[] = $actLinks;
  460. $json->addRow($data);
  461. unset($data);
  462. }
  463. }
  464. }
  465. $json->getJson();
  466. }
  467. /**
  468. * Returns list of duplicate coords/name items for some existing map
  469. *
  470. * @param int $mapid
  471. * @return string
  472. */
  473. public function renderItemDuplicateList($mapid) {
  474. $mapid = vf($mapid, 3);
  475. $result = '';
  476. $messages = new UbillingMessageHelper();
  477. $itemsCount = 0;
  478. $filterArray = array();
  479. if ($this->altCfg['ADCOMMENTS_ENABLED']) {
  480. $adcomments = new ADcomments('CUSTMAPITEMS');
  481. $adc = true;
  482. } else {
  483. $adc = false;
  484. }
  485. //counting unique geo coords
  486. if (!empty($this->allItems)) {
  487. foreach ($this->allItems as $ia => $eachItem) {
  488. if ($eachItem['mapid'] == $mapid) {
  489. if (isset($filterArray[$eachItem['geo']])) {
  490. $filterArray[$eachItem['geo']] ++;
  491. } else {
  492. $filterArray[$eachItem['geo']] = 1;
  493. }
  494. }
  495. }
  496. }
  497. $result.= wf_BackLink('?module=custmaps&showitems=' . $mapid);
  498. $result.= wf_delimiter();
  499. $cells = wf_TableCell(__('ID'));
  500. $cells.= wf_TableCell(__('Type'));
  501. $cells.= wf_TableCell(__('Geo location'));
  502. $cells.= wf_TableCell(__('Name'));
  503. $cells.= wf_TableCell(__('Location'));
  504. $cells.= wf_TableCell(__('Actions'));
  505. $rows = wf_TableRow($cells, 'row1');
  506. if (!empty($this->allItems)) {
  507. foreach ($this->allItems as $io => $each) {
  508. $indicator = ($adc) ? $adcomments->getCommentsIndicator($each['id']) : '';
  509. if ($each['mapid'] == $mapid) {
  510. if (isset($filterArray[$each['geo']])) {
  511. if ($filterArray[$each['geo']] > 1) {
  512. $cells = wf_TableCell($each['id']);
  513. $cells.= wf_TableCell($this->itemGetTypeName($each['type']));
  514. $cells.= wf_TableCell($each['geo']);
  515. $cells.= wf_TableCell($each['name']);
  516. $cells.= wf_TableCell($each['location']);
  517. $actLinks = '';
  518. if (cfr('CUSTMAPEDIT')) {
  519. $actLinks.= wf_JSAlertStyled('?module=custmaps&deleteitem=' . $each['id'], web_delete_icon(), $messages->getDeleteAlert()) . ' ';
  520. }
  521. $actLinks.= wf_JSAlertStyled('?module=custmaps&edititem=' . $each['id'], web_edit_icon(), $messages->getEditAlert()) . ' ';
  522. $actLinks.= wf_Link('?module=custmaps&showmap=' . $each['mapid'] . '&locateitem=' . $each['geo'] . '&zoom=' . $this->ymapsCfg['FINDING_ZOOM'], wf_img('skins/icon_search_small.gif', __('Find on map')), false) . ' ';
  523. $actLinks.=$indicator;
  524. $cells.= wf_TableCell($actLinks);
  525. $rows.= wf_TableRow($cells, 'row3');
  526. $itemsCount++;
  527. }
  528. }
  529. }
  530. }
  531. }
  532. $result.= wf_TableBody($rows, '100%', '0', 'sortable');
  533. $result.= __('Total') . ': ' . $itemsCount;
  534. return ($result);
  535. return ($result);
  536. }
  537. /**
  538. * Deletes item from database by its ID
  539. *
  540. * @param int $itemid
  541. *
  542. * @return int
  543. */
  544. public function itemDelete($itemid) {
  545. $itemid = vf($itemid, 3);
  546. $result = '';
  547. if (isset($this->allItems[$itemid])) {
  548. $result = $this->allItems[$itemid]['mapid'];
  549. $query = "DELETE from `custmapsitems` WHERE `id`='" . $itemid . "';";
  550. nr_query($query);
  551. log_register('CUSTMAPS DELETE ITEM ID [' . $itemid . ']');
  552. } else {
  553. throw new Exception(self::EX_NO_ITM_ID);
  554. }
  555. return ($result);
  556. }
  557. /**
  558. * Returns map creation form
  559. *
  560. * @return string
  561. */
  562. protected function mapCreateForm() {
  563. $inputs = wf_TextInput('newmapname', __('Name'), '', true, '30');
  564. $inputs.= wf_Submit(__('Create'));
  565. $result = wf_Form('', 'POST', $inputs, 'glamour');
  566. return ($result);
  567. }
  568. /**
  569. * Returns custom map editing form
  570. *
  571. * @param int $id
  572. *
  573. * @return string
  574. */
  575. protected function mapEditForm($id) {
  576. $inputs = wf_TextInput('editmapname', __('Name'), $this->allMaps[$id]['name'], true, '30');
  577. $inputs.= wf_HiddenInput('editmapid', $id);
  578. $inputs.= wf_Submit(__('Create'));
  579. $result = wf_Form('', 'POST', $inputs, 'glamour');
  580. return ($result);
  581. }
  582. /**
  583. * Returns map list controls panel
  584. *
  585. * @return string
  586. */
  587. protected function mapListControls() {
  588. $result = wf_modalAuto(wf_img('skins/add_icon.png') . ' ' . __('Create'), __('Create new map'), $this->mapCreateForm(), 'ubButton');
  589. $result.= wf_delimiter();
  590. return ($result);
  591. }
  592. /**
  593. * Creates new custom map in database
  594. *
  595. * @param string $name
  596. */
  597. public function mapCreate($name) {
  598. $nameFiltered = mysql_real_escape_string($name);
  599. $query = "INSERT INTO `custmaps` (`id`, `name`) VALUES (NULL, '" . $nameFiltered . "'); ";
  600. nr_query($query);
  601. $newId = simple_get_lastid('custmaps');
  602. log_register('CUSTMAPS CREATE MAP `' . $name . '` ID [' . $newId . ']');
  603. }
  604. /**
  605. * Deletes existing custom map by its ID
  606. *
  607. * @param int $id
  608. */
  609. public function mapDelete($id) {
  610. $id = vf($id, 3);
  611. if (isset($this->allMaps[$id])) {
  612. $query = "DELETE from `custmaps` WHERE `id`='" . $id . "';";
  613. nr_query($query);
  614. log_register('CUSTMAPS DELETE MAP [' . $id . ']');
  615. $query = "DELETE from `custmapsitems` WHERE `id`='" . $id . "';";
  616. nr_query($query);
  617. log_register('CUSTMAPS FLUSH ITEMS [' . $id . ']');
  618. } else {
  619. throw new Exception(self::EX_NO_MAP_ID);
  620. }
  621. }
  622. /**
  623. * Changes existing custom map name in database
  624. *
  625. * @paramint $id
  626. * @param string $name
  627. * @throws Exception
  628. */
  629. public function mapEdit($id, $name) {
  630. $id = vf($id, 3);
  631. if (isset($this->allMaps[$id])) {
  632. simple_update_field('custmaps', 'name', $name, "WHERE `id`='" . $id . "'");
  633. log_register('CUSTMAPS EDIT MAP [' . $id . '] SET `' . $name . '`');
  634. } else {
  635. throw new Exception(self::EX_NO_MAP_ID);
  636. }
  637. }
  638. /**
  639. * Returns existing custom map name by its Id
  640. *
  641. * @param int $id
  642. * @return string
  643. */
  644. public function mapGetName($id) {
  645. $id = vf($id, 3);
  646. return ($this->allMaps[$id]['name']);
  647. }
  648. /**
  649. * Returns icon for some item type
  650. *
  651. * @param string $type
  652. *
  653. * @return string
  654. */
  655. protected function itemGetIcon($type) {
  656. switch ($type) {
  657. case 'pillar':
  658. $result = 'twirl#greenIcon';
  659. break;
  660. case 'sump':
  661. $result = 'twirl#brownIcon';
  662. break;
  663. case 'coupling':
  664. $result = 'twirl#yellowIcon';
  665. break;
  666. case 'node':
  667. $result = 'twirl#orangeIcon';
  668. break;
  669. case 'box':
  670. $result = 'twirl#greyIcon';
  671. break;
  672. case 'amplifier':
  673. $result = 'twirl#pinkDotIcon';
  674. break;
  675. case 'optrec':
  676. $result = 'twirl#nightDotIcon';
  677. break;
  678. default :
  679. $result = 'twirl#lightblueIcon';
  680. break;
  681. }
  682. return ($result);
  683. }
  684. /**
  685. * Returns list of map placemarks
  686. *
  687. * @param int $id
  688. *
  689. * @return string
  690. */
  691. public function mapGetPlacemarks($id) {
  692. $id = vf($id, 3);
  693. $result = '';
  694. if (!empty($this->allItems)) {
  695. foreach ($this->allItems as $io => $each) {
  696. if (($each['mapid'] == $id) AND ( !empty($each['geo']))) {
  697. $icon = $this->itemGetIcon($each['type']);
  698. $content = $this->itemGetTypeName($each['type']) . ': ' . $each['name'];
  699. $controls = wf_Link('?module=custmaps&edititem=' . $each['id'], web_edit_icon(), false);
  700. $controls = str_replace("'", '`', $controls);
  701. $controls = str_replace("\n", '', $controls);
  702. $result.=$this->mapAddMark($each['geo'], $each['location'], $content, $controls, $icon, '');
  703. }
  704. }
  705. }
  706. return ($result);
  707. }
  708. /**
  709. * Returns item location form
  710. *
  711. * @return string
  712. */
  713. protected function itemLocationForm() {
  714. $result = wf_Selector('newitemtype', $this->itemTypes, __('Type'), '', true);
  715. $result.= wf_TextInput('newitemname', __('Name'), '', true, 20);
  716. $result.= wf_TextInput('newitemlocation', __('Location'), '', true, 20);
  717. $result.= wf_Submit(__('Create'));
  718. return ($result);
  719. }
  720. /**
  721. * Creates new map item in database
  722. *
  723. * @param int $mapid
  724. * @param string $type
  725. * @param string $geo
  726. * @param string $name
  727. * @param string $location
  728. */
  729. public function itemCreate($mapid, $type, $geo, $name, $location) {
  730. $mapid = vf($mapid, 3);
  731. $type = mysql_real_escape_string($type);
  732. $geo = mysql_real_escape_string($geo);
  733. $nameFiltered = mysql_real_escape_string($name);
  734. $location = mysql_real_escape_string($location);
  735. if (isset($this->allMaps[$mapid])) {
  736. $query = "INSERT INTO `custmapsitems` (`id`, `mapid`, `type`, `geo`, `name`, `location`) "
  737. . "VALUES (NULL, '" . $mapid . "', '" . $type . "', '" . $geo . "', '" . $nameFiltered . "', '" . $location . "');";
  738. nr_query($query);
  739. $newId = simple_get_lastid('custmapsitems');
  740. log_register('CUSTMAPS CREATE ITEM `' . $name . '` ID [' . $newId . ']');
  741. } else {
  742. throw new Exception(self::EX_NO_MAP_ID);
  743. }
  744. }
  745. /**
  746. * Returns map mark
  747. *
  748. * @param string $coords - map coordinates
  749. * @param string $title - ballon title
  750. * @param string $content - ballon content
  751. * @param string $footer - ballon footer content
  752. * @param string $icon - YM icon class
  753. * @param string $iconlabel - icon label string
  754. *
  755. * @return string
  756. */
  757. protected function mapAddMark($coords, $title = '', $content = '', $footer = '', $icon = 'twirl#lightblueIcon', $iconlabel = '') {
  758. return (generic_mapAddMark($coords, $title, $content, $footer, $icon, $this->ymapsCfg['CANVAS_RENDER']));
  759. }
  760. /**
  761. * Returns map circle
  762. *
  763. * @param $coords - map coordinates
  764. * @param $radius - circle radius in meters
  765. *
  766. * @return string
  767. *
  768. */
  769. public function mapAddCircle($coords, $radius, $content = '', $hint = '') {
  770. return (generic_MapAddCircle($coords, $radius, $content, $hint));
  771. }
  772. /**
  773. * Returns initialized JS map
  774. *
  775. * @param string $placemarks
  776. * @param string $editor
  777. * @return string
  778. */
  779. public function mapInit($placemarks, $editor = '') {
  780. $result = $this->mapControls();
  781. $result.= $this->mapContainer();
  782. $result.= generic_MapInit($this->center, $this->zoom, $this->ymapsCfg['TYPE'], $placemarks, $editor, $this->ymapsCfg['LANG'], 'custmap');
  783. return ($result);
  784. }
  785. /**
  786. * Return geo coordinates locator with embedded form
  787. *
  788. * @return string
  789. */
  790. public function mapLocationEditor() {
  791. $title = wf_tag('b') . __('Place coordinates') . wf_tag('b', true);
  792. $data = $this->itemLocationForm();
  793. $result = generic_MapEditor('newitemgeo', $title, $data);
  794. return ($result);
  795. }
  796. }
  797. ?>