Beispiel #1
0
 private function editForm()
 {
     global $CORE;
     echo '<h2>' . l('Modify Template') . '</h2>';
     $map_name = req('show');
     if (!$map_name) {
         throw new NagVisException(l('Map name missing'));
     }
     if (count($CORE->getAvailableMaps('/^' . preg_quote($map_name) . '$/')) == 0) {
         throw new NagVisException(l('A map with this name does not exists'));
     }
     $MAPCFG = new GlobalMapCfg($map_name);
     $MAPCFG->readMapConfig(!ONLY_GLOBAL, false, false);
     $name = submitted('edit') ? post('name') : null;
     // Check if the template exists
     // Read map config but don't resolve templates and don't use the cache
     if ($name) {
         if (count($MAPCFG->getTemplateNames('/^' . $name . '$/')) == 0) {
             throw new FieldInputError('name', l('A template with this name does not exist.'));
         }
         $templates = $MAPCFG->getDefinitions('template');
         $obj_id = $MAPCFG->getTemplateIdByName($name);
         $options = array();
         foreach ($templates[$obj_id] as $key => $val) {
             if ($key != 'type' && $key != 'object_id' && $key != 'name') {
                 $options[$key] = $val;
             }
         }
         $num_options = max(post('num_options'), count($options));
     }
     if (is_action() && post('mode') == 'edit') {
         try {
             // Get all options from the POST vars
             $save_options = array('name' => $name);
             $options = array();
             for ($i = 0; $i < $num_options; $i++) {
                 $key = post('key_' . $i);
                 $val = post('val_' . $i);
                 if ($key !== '' && $val !== '') {
                     $save_options[$key] = $val;
                     $options[$key] = $val;
                 }
             }
             $MAPCFG->updateElement($obj_id, $save_options, true);
             success(l('The template has been modified.'));
         } 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;
     js_form_start('edit');
     hidden('mode', 'edit');
     echo '<table class="mytable">';
     echo '<tr><td class="tdlabel">' . l('Name') . '</td>';
     echo '<td class="tdfield">';
     $choices = array('' => l('Please choose'));
     foreach (array_keys($MAPCFG->getTemplateNames()) as $tmpl_name) {
         $choices[$tmpl_name] = $tmpl_name;
     }
     select('name', $choices, '', 'updateForm(this.form)');
     echo '</td></tr>';
     if ($name) {
         hidden('num_options', $num_options);
         if (post('mode') == 'edit' && post('_add_option')) {
             $num_options += 1;
         }
         echo '<tr><td>' . l('Key') . '</td><td>' . l('Value') . '</td></tr>';
         for ($i = 0; $i < $num_options; $i++) {
             if ($i < count($options)) {
                 $keys = array_keys($options);
                 $_POST['key_' . $i] = $keys[$i];
                 $values = array_values($options);
                 $_POST['val_' . $i] = $values[$i];
             } else {
                 unset($_POST['key_' . $i]);
                 unset($_POST['val_' . $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('Modify'));
     form_end();
 }
Beispiel #2
0
 private function deleteForm()
 {
     global $CORE;
     echo '<h2>' . l('Delete Shape') . '</h2>';
     if (is_action() && post('mode') == 'delete') {
         try {
             $name = post('name');
             if (!$name) {
                 throw new FieldInputError('name', l('Please choose a shape'));
             }
             $shapes = $CORE->getAvailableShapes();
             if (!isset($shapes[$name])) {
                 throw new FieldInputError('name', l('The shape does not exist.'));
             }
             // Check whether or not the shape is in use
             $using = array();
             foreach ($CORE->getAvailableMaps() as $map) {
                 $MAPCFG = new GlobalMapCfg($map);
                 try {
                     $MAPCFG->readMapConfig();
                 } catch (Exception $e) {
                     continue;
                     // don't fail on broken map configs
                 }
                 foreach ($MAPCFG->getDefinitions('shape') as $key => $obj) {
                     if (isset($obj['icon']) && $obj['icon'] == $name) {
                         $using[] = $MAPCFG->getName();
                     }
                 }
             }
             if ($using) {
                 throw new FieldInputError('name', l('Unable to delete this shape, because it is ' . 'currently used by these maps: [M].', array('M' => implode(',', $using))));
             }
             $path = path('sys', '', 'shapes', $name);
             if ($path !== '') {
                 unlink($path);
             }
             success(l('The shape has been deleted.'));
             //reload(null, 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('delete_shape');
     hidden('mode', 'delete');
     echo '<table class="mytable">';
     echo '<tr><td class="tdlabel">' . l('Shape') . '</td>';
     echo '<td class="tdfield">';
     $shapes = array('' => l('Choose a shape'));
     foreach ($CORE->getAvailableShapes() as $name) {
         $shapes[$name] = $name;
     }
     select('name', $shapes);
     echo '</td></tr>';
     echo '</table>';
     submit(l('Delete'));
     form_end();
 }
Beispiel #3
0
 private function doRename($name, $new_name)
 {
     global $CORE, $AUTHORISATION;
     $files = array();
     // loop all map configs to replace mapname in all map configs
     foreach ($CORE->getAvailableMaps() as $mapName) {
         try {
             $MAPCFG1 = new GlobalMapCfg($mapName);
             $MAPCFG1->readMapConfig();
             $i = 0;
             // loop definitions of type map
             foreach ($MAPCFG1->getDefinitions('map') as $key => $obj) {
                 // check if old map name is linked...
                 if ($obj['map_name'] == $name) {
                     $MAPCFG1->setValue('map', $i, 'map_name', $new_name);
                     $MAPCFG1->writeElement('map', $i);
                 }
                 $i++;
             }
         } catch (Exception $e) {
             // Do nothing. Siletly pass config errors here...
         }
     }
     // And also remove the permission
     $AUTHORISATION->renameMapPermissions($name, $new_name);
     // rename config file
     rename(cfg('paths', 'mapcfg') . $name . '.cfg', cfg('paths', 'mapcfg') . $new_name . '.cfg');
 }