예제 #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();
 }