public function parse($orig_name) { global $CORE; ob_start(); if (is_action()) { try { $name = post('name'); if (!$name) { throw new FieldInputError('name', l('Please provide a map name.')); } if (!preg_match(MATCH_MAP_NAME, $name)) { throw new FieldInputError('name', l('This is not a valid map name (need to match [M])', array('M' => MATCH_MAP_NAME))); } if (count($CORE->getAvailableMaps('/^' . $name . '$/')) > 0) { throw new FieldInputError('name', l('A map with this name already exists.')); } // Read the old config $MAPCFG = new GlobalMapCfg($orig_name); $MAPCFG->readMapConfig(); // Create a new map config $NEW = new GlobalMapCfg($name); $NEW->createMapConfig(); foreach ($MAPCFG->getMapObjects() as $object_id => $cfg) { $NEW->addElement($cfg['type'], $cfg, $perm = true, $object_id); } success(l('The map has been created.')); reload(cfg('paths', 'htmlbase') . '/frontend/nagvis-js/index.php?mod=Map&show=' . $name, 1); } catch (FieldInputError $e) { form_error($e->field, $e->msg); } catch (NagVisException $e) { form_error(null, $e->message()); } catch (Exception $e) { if (isset($e->msg)) { form_error(null, $e->msg); } else { throw $e; } } } echo $this->error; echo '<div class="simple_form">' . N; js_form_start('to_new_map'); input('name'); submit(l('Save')); focus('name'); // Keep the view parameters the users has set $params = ltrim(req('view_params'), '&'); if ($params) { $parts = explode('&', $params); foreach ($parts as $part) { list($key, $val) = explode('=', $part); hidden($key, $val); } } form_end(); echo '</div>' . N; return ob_get_clean(); }
private function createForm() { global $CORE; echo '<h2>' . l('Create Template') . '</h2>'; if (submitted('create')) { $num_options = post('num_options', 1); } else { $num_options = 1; } $map_name = req('show'); if (!$map_name) { throw new FieldInputError(null, l('Map name missing')); } if (count($CORE->getAvailableMaps('/^' . preg_quote($map_name) . '$/')) == 0) { throw new FieldInputError(null, l('A map with this name does not exists')); } $MAPCFG = new GlobalMapCfg($map_name); $MAPCFG->readMapConfig(!ONLY_GLOBAL, false, false); if (is_action() && post('mode') == 'create') { try { $name = post('name'); if (!preg_match(MATCH_STRING_NO_SPACE, $name)) { throw new FieldInputError('name', l('You need to configure an unique name.')); } // Check if the template already exists // Read map config but don't resolve templates and don't use the cache if (count($MAPCFG->getTemplateNames('/^' . $name . '$/')) > 0) { throw new FieldInputError('name', l('A template with this name already exists.')); } // Get all options from the POST vars $options = array('name' => $name); for ($i = 0; $i < $num_options; $i++) { $key = post('key_' . $i); $val = post('val_' . $i); if ($key !== '' && $val !== '') { $options[$key] = $val; } } // append a new object definition to the map configuration $MAPCFG->addElement('template', $options, true); success(l('The template has been created.')); } catch (FieldInputError $e) { form_error($e->field, $e->msg); } catch (Exception $e) { if (isset($e->msg)) { form_error(null, $e->msg); } else { throw $e; } } } echo $this->error; js_form_start('create'); hidden('mode', 'create'); hidden('num_options', $num_options); echo '<table class="mytable">'; echo '<tr><td class="tdlabel">' . l('Name') . '</td>'; echo '<td class="tdfield">'; input('name'); echo '</td></tr>'; if (post('mode') == 'create' && post('_add_option')) { $num_options += 1; } echo '<tr><td>' . l('Key') . '</td><td>' . l('Value') . '</td></tr>'; for ($i = 0; $i < $num_options; $i++) { echo '<tr><td class="tdlabel">'; input('key_' . $i); echo '</td><td class="tdfield">'; input('val_' . $i); echo '</td></tr>'; } echo '<tr><td></td><td class="tdfield">'; button('_add_option', l('Add Option'), 'addOption(this.form)'); echo '</td></tr>'; echo '</table>'; submit(l('Create')); form_end(); }
private function createForm() { global $CORE; echo '<h2>' . l('Create Map') . '</h2>'; $map_types = array('map' => l('Regular map'), 'worldmap' => l('Geographical map (interactive)'), 'geomap' => l('Geographical map (non interactive)'), 'automap' => l('Automap based on parent/child relations'), 'dynmap' => l('Dynamic map')); if (is_action() && post('mode') == 'create') { try { $name = post('name'); if (!$name) { throw new FieldInputError('name', l('You need to provide a unique map name (ID) for the map')); } if (count($CORE->getAvailableMaps('/^' . preg_quote($name) . '$/')) > 0) { throw new FieldInputError('name', l('A map with this name already exists')); } if (!preg_match(MATCH_MAP_NAME, $name)) { throw new FieldInputError('name', l('This is not a valid map name (need to match [M])', array('M' => MATCH_MAP_NAME))); } $type = post('type'); if (!isset($map_types[$type])) { throw new FieldInputError('type', l('You provided an invalid type')); } $alias = post('alias'); if ($alias && !preg_match(MATCH_STRING, $alias)) { throw new FieldInputError('alias', l('This is not a valid map alias (need to match [M])', array('M' => MATCH_STRING))); } $MAPCFG = new GlobalMapCfg($name); if (!$MAPCFG->createMapConfig()) { throw new NagVisException(l('Failed to create the map')); } $global = array(); if ($alias) { $global['alias'] = $alias; } if ($type != 'map') { $global['sources'] = array($type); } $MAPCFG->addElement('global', $global, false, 0); $MAPCFG->handleSources('init_map'); $MAPCFG->storeUpdateElement(0); success(l('The map has been created. Changing to the new map...')); reload('index.php?mod=Map&act=view&show=' . $name, 1); } catch (FieldInputError $e) { form_error($e->field, $e->msg); } catch (Exception $e) { if (isset($e->msg)) { form_error(null, $e->msg); } else { throw $e; } } } echo $this->error; js_form_start('create_map'); hidden('mode', 'create'); echo '<table class="mytable">'; echo '<tr><td class="tdlabel">' . l('ID (Internal Name)') . '</td>'; echo '<td class="tdfield">'; input('name'); echo '</td></tr>'; echo '<tr><td class="tdlabel">' . l('Alias') . '</td>'; echo '<td class="tdfield">'; input('alias'); echo '</td></tr>'; echo '<tr><td class="tdlabel">' . l('Map Type') . '</td>'; echo '<td class="tdfield">'; select('type', $map_types); echo '</td></tr>'; echo '</table>'; submit(l('Create')); form_end(); }