1<?php
2/*****************************************************************************
3 *
4 * ViewToNewMap.php - Class for rendering the "to new map" dialog
5 *
6 * Copyright (c) 2004-2016 NagVis Project (Contact: info@nagvis.org)
7 *
8 * License:
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *****************************************************************************/
24
25class ViewToNewMap {
26    private $error = null;
27
28    public function parse($orig_name) {
29        global $CORE;
30
31        ob_start();
32
33        $view_params = array();
34        $params = ltrim(req('view_params'), '&');
35        if ($params) {
36            $parts = explode('&', $params);
37            foreach ($parts as $part) {
38                list($key, $val) = explode('=', $part);
39                $view_params[$key] = $val;
40            }
41        }
42
43        if (is_action()) {
44            try {
45                $name = post('name');
46                if (!$name)
47                    throw new FieldInputError('name', l('Please provide a map name.'));
48
49                if (!preg_match(MATCH_MAP_NAME, $name))
50                    throw new FieldInputError('name', l('This is not a valid map name (need to match [M])',
51                                                                    array('M' => MATCH_MAP_NAME)));
52
53                if (count($CORE->getAvailableMaps('/^'.$name.'$/')) > 0)
54                    throw new FieldInputError('name', l('A map with this name already exists.'));
55
56                if (!isset($view_params["worldmap_center"]))
57                    throw new FieldInputError('view_params', l('Please change your viewport before saving as new map.'));
58
59                if (!preg_match(MATCH_COORDS_MULTI, $view_params["worldmap_center"]))
60                    throw new FieldInputError('view_params', l('This is not a valid worldmap center'));
61
62                if (!isset($view_params["worldmap_zoom"]))
63                    throw new FieldInputError('view_params', l('Worldmap zoom parameter missing.'));
64
65                if (!preg_match(MATCH_INTEGER, $view_params["worldmap_zoom"]))
66                    throw new FieldInputError('view_params', l('This is not a valid worldmap zoom'));
67
68                // Read the old config
69                $MAPCFG = new GlobalMapCfg($orig_name);
70                $MAPCFG->readMapConfig();
71
72                // Create a new map config
73                $NEW = new GlobalMapCfg($name);
74                $NEW->createMapConfig();
75                foreach ($MAPCFG->getMapObjects() AS $object_id => $cfg) {
76                    $NEW->addElement($cfg['type'], $cfg, $perm = true, $object_id);
77                }
78
79                $NEW->setValue(0, "worldmap_center", $view_params["worldmap_center"]);
80                $NEW->setValue(0, "worldmap_zoom", $view_params["worldmap_zoom"]);
81                $NEW->storeUpdateElement(0);
82
83                success(l('The map has been created.'));
84                reload(cfg('paths','htmlbase').'/frontend/nagvis-js/index.php?mod=Map&show='.$name, 1);
85            } catch (FieldInputError $e) {
86                form_error($e->field, $e->msg);
87            } catch (NagVisException $e) {
88                form_error(null, $e->message());
89            } catch (Exception $e) {
90                if (isset($e->msg))
91                    form_error(null, $e->msg);
92                else
93                    throw $e;
94            }
95        }
96        echo $this->error;
97
98        echo '<div class="simple_form">'.N;
99        js_form_start('to_new_map');
100        input('name');
101        submit(l('Save'));
102        focus('name');
103
104        // Keep the view parameters the users has set
105        foreach ($view_params AS $key => $val) {
106            hidden($key, $val);
107        }
108
109        form_end();
110        echo '</div>'.N;
111
112        return ob_get_clean();
113    }
114}
115?>
116