Code

Updated release part detection
[gosa.git] / gosa-plugins / goto-ng / admin / newConfigManagement / class_newConfigManagement.inc
index 321ef0bb35dd218d8b08f741560e785d22b2a630..2b92346270e932a952ff6b6fead2b49751747f86 100644 (file)
@@ -36,32 +36,32 @@ class newConfigManagement extends plugin
         $items['root']['name'] = '/';
         $items['root']['description'] = _('Root');
 
+        // Define distribution paramters.
+        $dOpt1 = array('description' => _('Name'), 'default' => '', 'value' => '', 'required' => true,
+                'type' => 'string', 'display' => _('Name'));
+        $dOpt2 = array('description' => _('Distribution type'), 'default' => 'deb', 'value' => 'deb', 'required' => true,
+                'type' => 'combobox', 'display' => _('Distribution type'), 'values' => array("deb" => 'deb', "rpm" => 'rpm'));
+        $dOpt3 = array('description' => _('Mirror Url'), 'default' => '', 'value' => '', 'required' => false,
+                'type' => 'string', 'display' => _('Mirror Url'));
+        $dOpt4 = array('description' => _('Method'), 'default' => 'puppet', 'value' => 'puppet', 'required' => false,
+                'type' => 'combobox', 'display' => _('Installation method'), 'values'=>array('puppet'=>_('Puppet')));
+
+        // Define release parameters
+        $rOpt1 = array('description' => _('Name'), 'default' => '', 'value' => '', 'required' => true,
+                'type' => 'string', 'display' => _('Name'));
+
         $items['Distribution']['container'] = array('Release');
         $items['Distribution']['name'] = 'Distribution';
         $items['Distribution']['description'] = _('Distribution');
-        $items['Distribution']['options']['name']['description'] = _("Name");
-        $items['Distribution']['options']['name']['default'] = "";
-        $items['Distribution']['options']['name']['value'] = "";
-        $items['Distribution']['options']['name']['required'] = true;
-        $items['Distribution']['options']['name']['type'] = 'string';
-        $items['Distribution']['options']['name']['display'] = _('Name');
-        $items['Distribution']['options']['type']['description'] = _("Distribution type");
-        $items['Distribution']['options']['type']['default'] = "deb";
-        $items['Distribution']['options']['type']['value'] = "deb";
-        $items['Distribution']['options']['type']['values'] = array("deb" => 'deb', "rpm" => 'rpm');
-        $items['Distribution']['options']['type']['required'] = true;
-        $items['Distribution']['options']['type']['type'] = 'combobox';
-        $items['Distribution']['options']['type']['display'] = _('Distribution type');
+        $items['Distribution']['options']['name'] = $dOpt1;
+        $items['Distribution']['options']['mirror'] = $dOpt3;
+        $items['Distribution']['options']['installation_type'] = $dOpt2;
+        $items['Distribution']['options']['installation_method'] = $dOpt4;
 
         $items['Release']['container'] = array('Release', '__CFG_ITEMS__');
         $items['Release']['name'] = 'Release';
         $items['Release']['description'] = _('Release');
-        $items['Release']['options']['name']['description'] = _("Name");
-        $items['Release']['options']['name']['default'] = "";
-        $items['Release']['options']['name']['value'] = "";
-        $items['Release']['options']['name']['required'] = true;
-        $items['Release']['options']['name']['type'] = 'string';
-        $items['Release']['options']['name']['display'] = _('Name');
+        $items['Release']['options']['name'] = $rOpt1;
 
         $this->installationMethods = array();
         $this->installationMethods['root']['description'] = _('root');
@@ -152,10 +152,28 @@ class newConfigManagement extends plugin
         }else{
             foreach($res as $dist){
                 $this->dataModel->addItem('Distribution','/root', $dist['name'], $dist);
+
                 if(isset($dist['releases'])){
-                    foreach($dist['releases'] as $release){
+
+                    // Sort releases by name length
+                    $sort = array();
+                    foreach($dist['releases'] as $id => $release){
+                        $sort[strlen($release['name']) . $release['name']]  = $id;
+                    }
+                    uksort($sort, "strnatcasecmp");   
+
+                    // Append release tags
+                    foreach($sort as $id){
+                        $release = $dist['releases'][$id];
+                        $names = preg_split("/\//", $release['name']);
+    
+                        $rPath = "";
                         $distPath = "/root/{$dist['name']}";
-                        $this->dataModel->addItem('Release',$distPath, $release['name'], $release);
+                        foreach($names as $rName){
+                            $rPath .= '/'.$rName;
+                            $this->dataModel->addItem('Release',$distPath, $rName, $release);
+                            $distPath .= $rPath;
+                        }
                     }
                 }
             }
@@ -208,10 +226,13 @@ class newConfigManagement extends plugin
 
                 // Request all config items for the selected release via rpc.
                 $rpc = $this->config->getRpcHandle();
-                $res = $rpc->listConfigItems($data['name']);
+                $releasePath = $this->getReleasePart($path);
+                $res = $rpc->listConfigItems($releasePath);
                 if(!$rpc->success()){
                     msg_dialog::display(_("Error"),sprintf(_("Failed to load distributions: %s"),$rpc->get_error()),ERROR_DIALOG);
                 }else{
+            
+                    if(!$res) return;
 
                     // Sort entries by path length 
                     $sLen = array();
@@ -464,6 +485,16 @@ class newConfigManagement extends plugin
         }
     }
 
+    function getUsedNamesForPath($path)
+    {
+        $item = $this->dataModel->getItemByPath($path);
+        $names = array();
+        foreach($item['children'] as $path => $data){
+            $names[] = $data['name'];
+        }
+        return($names);
+    }
+
 
     /*! \brief      Edits a selected list item.
      */
@@ -545,31 +576,110 @@ class newConfigManagement extends plugin
         }
         return(NULL);
     }
-   
+  
+    
+    /*! \brief  Extracts the distribution path out of a path.
+     *          e.g. /root/debian/squeeze/test/module -> /root/debian
+     */
+    function getDistributionPath($fullPath)
+    {
+        $fullPath.='/';
+        while(preg_match("/\//", $fullPath)){
+            $fullPath = preg_replace("/\/[^\/]*$/","", $fullPath);
+            $item = $this->dataModel->getItemByPath($fullPath);
+            if($item['type'] == 'Distribution'){
+                return($fullPath);
+            }
+        }
+        return(NULL);
+    }
+
+    
+    /*! \brief  Extracts the release-part out of a path.
+     *          e.g. /root/debian/squeeze/test/module -> squeeze/test
+     */
+    function getReleasePart($path)
+    {
+        $rPath = $this->getReleasePath($path);
+        $dPath = $this->getDistributionPath($path);
+        return(preg_replace("/^".preg_quote($dPath, '/')."\/?/", "", $rPath));
+    }
  
     function saveItemChanges()
     {
+        // Save template engine modifications and validate values.
+        $this->TemplateEngine->save_object();
+        $msgs = $this->TemplateEngine->check();
+
+        // Get values to be saved
+        $values = array();
+        foreach($this->TemplateEngine->getWidgets() as $w){
+            $values[$w->getName()] = $w->getValue();
+        }
+           
+        // No input error were found, now check that we do not use the same name twice.
+        if(!count($msgs)){
+            $usedNames = $this->getUsedNamesForPath($this->selectedContainer);
+
+            // Allow the item to keep its name.
+            if($this->currentObject != NULL && isset($this->currentObject['values']['name'])){
+                $usedNames = array_remove_entries(array($this->currentObject['values']['name']), $usedNames);
+            }
+            if(in_array($values['name'],$usedNames)){
+                $msgs[] = msgPool::duplicated(_("Name"));
+            }
+        }
+
+        // Display errors
+        if(count($msgs)){
+            msg_dialog::displayChecks($msgs);
+            return;
+        }
+
+        // Get the item type to be saved
         $item = $this->currentObject;
         $type = $this->TemplateEngine->getItemType();
         if($this->cfgItemMap[$type] == 'root'){
-            echo "{$type} Cannot be saved yet";
-            $this->listing->clearDialogObject();
-            return;
-        }
 
-        if($this->cfgItemMap[$type] != 'root'){
+            // We've to create a new distribution
+            if($type == 'Distribution'){
+                $name = $values['name'];
+                $itype = $values['installation_type'];
+                $imethod = $values['installation_method'];
+                $mirror = $values['mirror'];
 
-            // Save template engine modifications
-            $this->TemplateEngine->save_object();
-            $release = preg_replace("/^.*\//","", $this->getReleasePath($this->selectedContainer));
+                // Initiate the rpc request.
+                $rpc = $this->config->getRpcHandle();
+                $res = $rpc->createDistribution($name, $itype, array('mirror'=>$mirror, 'install_method' => $imethod));
+                if(!$rpc->success()){
+                    msg_dialog::display(_("Error"), sprintf(_("Failed to save distributions: %s"), $rpc->get_error()),ERROR_DIALOG);
+                    return(NULL);
+                }else{
 
-            // Get values to be saved
-            $values = array();
-            foreach($this->TemplateEngine->getWidgets() as $w){
-                $values[$w->getName()] = $w->getValue();
+                    // We've successfully added the item, now add it to the tree.
+                    $this->dataModel->addItem($type, $this->selectedContainer, $values['name'],array(), '-' );
+
+                    // Finally - close the dialog. 
+                    $this->listing->clearDialogObject();
+                }
+                
+
+                $this->listing->clearDialogObject();
+
+            }else{
+
+                echo "{$type} Cannot be saved yet";
+                $this->listing->clearDialogObject();
+                return;
             }
-           
+        }
+
+        // Save a CONFIG-ITEM object.
+        if($this->cfgItemMap[$type] != 'root'){
+
             // Get paths 
+            $release = preg_replace("/^.*\//","", $this->getReleasePath($this->selectedContainer));
             $newPath = $this->selectedContainer."/".$values['name'];
             $newItemPath = $this->getItemPath($this->selectedContainer)."/".$values['name'];
             if($item){
@@ -595,7 +705,7 @@ class newConfigManagement extends plugin
                     $this->listing->clearDialogObject();
                 }
             }else{
-    
+
                 // Write the modifications back to the server.
                 $rpc = $this->config->getRpcHandle();
                 $res = $rpc->setConfigItem($release, $oldItemPath, $type, $values);
@@ -605,13 +715,10 @@ class newConfigManagement extends plugin
                 }else{
             
                     // Update the data model
-                    $item['values'] = $values;
-                    
-                    $this->dataModel->setItemValues($item['path'], $item['values']);
-                    $this->dataModel->moveItem($oldPath, $newPath);
-
-                    print_a($this);
-
+                    $this->dataModel->setItemValues($oldPath, $values);
+                    if($oldPath != $newPath){
+                        $this->dataModel->moveItem($oldPath, $newPath);
+                    }
                     $this->listing->clearDialogObject();
                 }
             }