Provide better error reporting and error checking when updating a network
[www-register-wizard.git] / application / controllers / register.php
index a6d3350..f3b4075 100644 (file)
@@ -11,7 +11,7 @@ include 'plc_header.php';
 
 // Common functions
 require_once 'plc_functions.php';
-require_once 'plc_sorts.php';
+//require_once 'plc_sorts.php';
 include 'plc_objects.php';
 
 
@@ -55,7 +55,7 @@ class Register extends Controller {
                $data = array();
                if ($this->validation->run() == TRUE)
                {
-                   if ($this->validation->model != "none-selected" )
+                       if ($this->validation->model != "none-selected" )
                        {
                                /* b/c the submit is valid, it doesn't matter if pcu_register is set */
                                $this->pcu_id = $this->add_pcu($data);
@@ -117,11 +117,12 @@ class Register extends Controller {
                global $api, $plc;
                // get sites depending on role and sites associated.
                if( $person->isAdmin() ) {
-                       $site_info= $api->GetSites( NULL, array( "name", "site_id", "login_base" ) );
+                       $site_info= $api->GetSites(array('peer_id' => NULL,'-SORT'=>'name'), 
+                                                                               array( "name", "site_id", "login_base" ) );
                } else {
                        $site_info= $api->GetSites( $person->getSites(), array( "name", "site_id", "login_base" ) );
                }
-               sort_sites( $site_info );
+               //sort_sites( $site_info );
                return $site_info;
        }
 
@@ -359,10 +360,16 @@ class Register extends Controller {
                        $optional_vals= array( "hostname"=>$hostname, "model"=>$model );
 
                        $site_id= $data['site_id'];
-                       $node_id= $api->AddNode( intval( $site_id ), $optional_vals );
-                       if( $node_id <= 0 ) {
-                               $data['error'] = $api->error();
-                               print $data['error'];
+                       // Try to get node in case this is from an error:
+                       $nodes = $api->GetNodes($optional_vals);
+                       if ( count($nodes) > 0 ) {
+                               $node_id= $nodes[0]['node_id'];
+                       } else {
+                               $node_id= $api->AddNode( intval( $site_id ), $optional_vals );
+                               if( $node_id <= 0 ) {
+                                       $data['error'] = $api->error();
+                                       print $data['error'];
+                               }
                        }
 
                        // now, try to add the network.
@@ -384,24 +391,39 @@ class Register extends Controller {
                                }
                        }
 
-                       $nodenetwork_id= $api->AddNodeNetwork( $node_id, $optional_vals);
-                       if( $nodenetwork_id <= 0 ) {
+                       $interface_id= $api->AddInterface( $node_id, $optional_vals);
+                       if( $interface_id <= 0 ) {
                                $data['error'] = $api->error();
                                print $data['error'];
                        }
 
-                       $success = $api->AddNodeToPCU( $node_id, $data['pcu_id'], 1);
-                       if( !isset($success) || $success <= 0 ) {
-                               $data['error'] = $api->error();
-                               print $data['error'];
+                       $pcus = $api->GetPCUs(array('pcu_id' => $data['pcu_id']));
+                       if ( count($pcus) > 0 )
+                       {
+                               $pcu = $pcus[0];
+                               # if $node_id in $pcu['node_ids']
+                               if ( ! in_array( $node_id , $pcu['node_ids'] ) )
+                               {
+                                   $success = $api->AddNodeToPCU( $node_id, $data['pcu_id'], 1);
+                                   if( !isset($success) || $success <= 0 ) {
+                                           $data['error'] = $api->error();
+                                           print $data['error'];
+                                   }
+                                  
+                               }
                        }
+                       $data['interface_id'] = $interface_id;
+                       $data['node_id'] = $node_id;
+                       if ( isset($errors) ) { $data['errors'] = $errors; }
+                       return $node_id;
+
+               } else {
+                   $data['error'] = $errors[0];
+                       return 0;
                }
 
 
-               $data['node_id'] = $node_id;
-               if ( isset($errors) ) { $data['errors'] = $errors; }
                
-               return $node_id;
        }
 
        function get_stage3_data($person, $data=NULL)
@@ -420,7 +442,12 @@ class Register extends Controller {
        {
                global $api, $plc;
                $plc_node_list = $api->GetNodes(array('site_id' => intval($site_id) ));
-               return PlcObject::constructList('Node', $plc_node_list);
+               $ret = array();
+               foreach ($plc_node_list as $plc_node)
+               {
+                       $ret[] = new Node($plc_node, True);
+               }
+               return $ret;
        }
 
        function getsite($site_id)
@@ -474,14 +501,20 @@ class Register extends Controller {
                        print $this->validation->error_string . "<br>";
                }
 
-
                $data['node'] = $this->getnode($data['node_id']);
-               $api_pcus = $api->GetPCUs($data['node']->pcu_ids);
-               $pcu = $api_pcus[0];
+               if ( sizeof($data['node']->pcu_ids) == 0)
+               {
+                       $data['pcu_assigned'] = False;
+                       $data['pcu_port'] = -1;
+               } else {
+                       $data['pcu_assigned'] = True;
+                       $api_pcus = $api->GetPCUs($data['node']->pcu_ids);
+                       $pcu = $api_pcus[0];
+                       # NOTE: find index of node id, then pull out that index of
+                       $index = array_search($data['node_id'], $pcu['node_ids']);
+                       $data['pcu_port'] = $pcu['ports'][$index];
+               }
 
-               # NOTE: find index of node id, then pull out that index of
-               $index = array_search($data['node_id'], $pcu['node_ids']);
-               $data['pcu_port'] = $pcu['ports'][$index];
                $data['stage'] = 4.5;
                #$data = $this->get_stage4_data($person, $data);
                $this->load->view('header', $data);
@@ -597,7 +630,7 @@ class Register extends Controller {
                $api_node_list = $api->GetNodes($node_id);
                if ( count($api_node_list) > 0 )
                {
-                       $node_obj = new Node($api_node_list[0]);
+                       $node_obj = new Node($api_node_list[0], True);
                } else {
                        print "broken!!!";
                        exit (1);
@@ -667,7 +700,7 @@ class Register extends Controller {
                        if ( count($optional_vals) > 0 )
                        {
                                print_r($optional_vals);
-                               $ret = $api->UpdateNodeNetwork( $node_obj->nodenetwork_id, $optional_vals);
+                               $ret = $api->UpdateInterface( $node_obj->interface_id, $optional_vals);
                                if( $ret <= 0 ) {
                                        $data['error'] = $api->error();
                                        print $data['error'];
@@ -689,6 +722,7 @@ class Register extends Controller {
                }
                $data['node'] = $this->getnode($data['node_id']);
                $data['site'] = $this->getsite($data['site_id']);
+               /*print "SITENAME: " . $data['site']['login_base'] . "<BR>";*/
                return $data;
        }
 
@@ -709,7 +743,7 @@ class Register extends Controller {
                $plc_node_list = $api->GetNodes(array('node_id' => intval($node_id) ));
                if ( count($plc_node_list) > 0 )
                {
-                       return new Node($plc_node_list[0]);
+                       return new Node($plc_node_list[0], True);
                } else {
                        return NULL;
                }