more tweaks for reservable nodes, and other bugfixes
[www-register-wizard.git] / application / controllers / register.php
index a6d3350..f2c6c0c 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';
 
 
@@ -37,25 +37,25 @@ class Register extends Controller {
                $this->validation->set_error_delimiters('<span class="error">', '</span>');
 
                $rules['model']         = "trim|required";
-               $rules['hostname']  = "trim|required";
+               $rules['hostname']      = "trim|required";
                $rules['ip']            = "trim|required|valid_ip";
-               $rules['username']  = "trim";
-               $rules['password']  = "trim|required";
+               $rules['username']      = "trim";
+               $rules['password']      = "trim|required";
                $rules['notes']         = "trim";
                $this->validation->set_rules($rules);
 
-               $fields['model']  = "Model";
-               $fields['hostname']  = "Hostname";
+               $fields['model']        = "Model";
+               $fields['hostname']     = "Hostname";
                $fields['ip']           = "IP Address";
-               $fields['username']  = "Username";
-               $fields['password']  = "Password";
+               $fields['username']     = "Username";
+               $fields['password']     = "Password";
                $this->validation->set_fields($fields);
 
                $person = new Person($plc->person);
                $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);
@@ -75,12 +75,12 @@ class Register extends Controller {
                /* add pcu, get pcu info */
                $site_id = intval( $_REQUEST['site_id'] );
                $fields= array( 'protocol'=>    '',
-                                               'model'=>               $_REQUEST['model'], 
-                                               'hostname'=>    $this->validation->hostname,
-                                               'ip'=>                  $this->validation->ip,
-                                               'username'=>    $this->validation->username, 
-                                               'password'=>    $this->validation->password, 
-                                               'notes'=>               $_REQUEST['notes'], );
+                               'model'=>       $_REQUEST['model'], 
+                               'hostname'=>    $this->validation->hostname,
+                               'ip'=>          $this->validation->ip,
+                               'username'=>    $this->validation->username, 
+                               'password'=>    $this->validation->password, 
+                               'notes'=>       $_REQUEST['notes'], );
                $pcu_id= $api->AddPCU( $site_id, $fields );
 
                if( $pcu_id == 0 ) {
@@ -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;
        }
 
@@ -187,12 +188,12 @@ class Register extends Controller {
                /* add pcu, get pcu info */
                $pcu_id = intval($this->validation->pcu_id);
                $fields= array( 'protocol'=>    '',
-                                               'model'=>               $_REQUEST['model'], 
-                                               'hostname'=>    $this->validation->hostname,
-                                               'ip'=>                  $this->validation->ip,
-                                               'username'=>    $this->validation->username, 
-                                               'password'=>    $this->validation->password, 
-                                               'notes'=>               $_REQUEST['notes'], );
+                               'model'=>       $_REQUEST['model'], 
+                               'hostname'=>    $this->validation->hostname,
+                               'ip'=>          $this->validation->ip,
+                               'username'=>    $this->validation->username, 
+                               'password'=>    $this->validation->password, 
+                               'notes'=>       $_REQUEST['notes'], );
                $ret = $api->UpdatePCU( $pcu_id, $fields );
 
                if( $ret != 1 ) {
@@ -243,27 +244,29 @@ class Register extends Controller {
                $data['site_id'] = intval($site_id);
 
                if ( isset($_REQUEST['pcu_proceed']) ) {
-                       $rules['hostname']  = "";
+                       $rules['hostname']      = "";
+                       $rules['node_type']     = 'regular';
                        $rules['model']         = "";
-                       $rules['method']        = "";
+                       $rules['method']        = "dhcp";
                        $rules['ip']            = "";
-                       $rules['netmask']  = "";
-                       $rules['network']  = "";
-                       $rules['gateway']  = "";
-                       $rules['broadcast']  = "";
-                       $rules['dns1']  = "";
-                       $rules['dns2']  = "";
+                       $rules['netmask']       = "";
+                       $rules['network']       = "";
+                       $rules['gateway']       = "";
+                       $rules['broadcast']     = "";
+                       $rules['dns1']          = "";
+                       $rules['dns2']          = "";
                        $this->validation->set_rules($rules);
-                       $fields['hostname']  = "Hostname";
+                       $fields['hostname']     = "Hostname";
+                       $fields['node_type']    = "Node Type";
                        $fields['model']        = "Model";
                        $fields['method']       = "Method";
                        $fields['ip']           = "IP Address";
-                       $fields['netmask']              = "Netmask Address";
-                       $fields['network']              = "Network Address";
-                       $fields['gateway']              = "Gateway Address";
+                       $fields['netmask']      = "Netmask Address";
+                       $fields['network']      = "Network Address";
+                       $fields['gateway']      = "Gateway Address";
                        $fields['broadcast']    = "Broadcast Address";
-                       $fields['dns1']                 = "Primary DNS Address";
-                       $fields['dns2']                 = "Secondary DNS Address";
+                       $fields['dns1']         = "Primary DNS Address";
+                       $fields['dns2']         = "Secondary DNS Address";
                        $this->validation->set_fields($fields);
 
                        $result = $this->validation->run();
@@ -272,7 +275,8 @@ class Register extends Controller {
                        $this->disp_errors = False;
                        print $this->validation->error_string . "<br>";
                } else {
-                       $rules['hostname']  = "trim|required";
+                       $rules['hostname']      = "trim|required";
+                       $rules['node_type']     = "required";
                        $rules['model']         = "trim|required";
                        $rules['method']        = "required";
                        $rules['ip']            = "trim|required|valid_ip";
@@ -294,16 +298,17 @@ class Register extends Controller {
                        }
                        $this->validation->set_rules($rules);
 
-                       $fields['hostname']  = "Hostname";
+                       $fields['hostname']     = "Hostname";
+                       $fields['node_type']    = "Node Type";
                        $fields['model']        = "Model";
                        $fields['method']       = "Method";
                        $fields['ip']           = "IP Address";
-                       $fields['netmask']              = "Netmask Address";
-                       $fields['network']              = "Network Address";
-                       $fields['gateway']              = "Gateway Address";
+                       $fields['netmask']      = "Netmask Address";
+                       $fields['network']      = "Network Address";
+                       $fields['gateway']      = "Gateway Address";
                        $fields['broadcast']    = "Broadcast Address";
-                       $fields['dns1']                 = "Primary DNS Address";
-                       $fields['dns2']                 = "Secondary DNS Address";
+                       $fields['dns1']         = "Primary DNS Address";
+                       $fields['dns2']         = "Secondary DNS Address";
                        $this->validation->set_fields($fields);
 
                        if ($this->validation->run() == TRUE)
@@ -324,7 +329,8 @@ class Register extends Controller {
        {
                global $api, $plc;
                $hostname = trim($_REQUEST['hostname']);
-               $model= trim($_REQUEST['model']);
+               $node_type = trim($_REQUEST['node_type']);
+               $model = trim($_REQUEST['model']);
                $method = trim($_REQUEST['method']);
                $ip = trim($_REQUEST['ip']);
                if ( $method == 'static' )
@@ -353,16 +359,21 @@ class Register extends Controller {
                        }
                }
 
-               if( !isset($errors) || count($errors) == 0 )
-               {
+               if( !isset($errors) || count($errors) == 0 ) {
                        // add new node and its network
-                       $optional_vals= array( "hostname"=>$hostname, "model"=>$model );
+                       $optional_vals= array( 'hostname'=>$hostname, 'node_type'=>$node_type, '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 +395,37 @@ 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;
 
-               $data['node_id'] = $node_id;
-               if ( isset($errors) ) { $data['errors'] = $errors; }
+               } else {
+                   $data['error'] = $errors[0];
+                       return 0;
+               }
                
-               return $node_id;
        }
 
        function get_stage3_data($person, $data=NULL)
@@ -420,7 +444,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 +503,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);
@@ -500,16 +535,17 @@ class Register extends Controller {
                $person = new Person($plc->person);
 
                $fields['hostname']     = "Hostname";
+               $fields['node_type']    = "Node Type";
                $fields['model']        = "Model";
                $fields['method']       = "Method";
                $fields['ip']           = "IP Address";
                $fields['netmask']      = "Netmask Address";
                $fields['network']      = "Network Address";
                $fields['gateway']      = "Gateway Address";
-               $fields['broadcast']= "Broadcast Address";
+               $fields['broadcast']    = "Broadcast Address";
                $fields['dns1']         = "Primary DNS Address";
                $fields['dns2']         = "Secondary DNS Address";
-               $fields['node_id']  = "NODE id";
+               $fields['node_id']      = "NODE id";
                $this->validation->set_fields($fields);
 
                $data = array();
@@ -519,8 +555,9 @@ class Register extends Controller {
                if ( isset($_REQUEST['node_choose']) ) {
                        $rules['node_id']       = "required|intval";
                        $rules['hostname']      = "";
+                       $rules['node_type']     = "regular";
                        $rules['model']         = "";
-                       $rules['method']        = "";
+                       $rules['method']        = "dhcp";
                        $rules['ip']            = "";
                        $rules['netmask']       = "";
                        $rules['network']       = "";
@@ -536,12 +573,12 @@ class Register extends Controller {
                        $this->disp_errors = False;
                        print $this->validation->error_string . "<br>";
                } else {
-                       $rules['hostname']  = "trim|required";
+                       $rules['hostname']      = "trim|required";
+                       $rules['node_type']     = 'required';
                        $rules['model']         = "trim|required";
                        $rules['method']        = "required";
                        $rules['ip']            = "trim|required|valid_ip";
-                       if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' )
-                       {
+                       if ( isset ($_REQUEST['method']) && $_REQUEST['method'] == 'static' ) {
                                $rules['netmask']  = "trim|valid_ip";
                                $rules['network']  = "trim|valid_ip";
                                $rules['gateway']  = "trim|valid_ip";
@@ -568,8 +605,7 @@ class Register extends Controller {
                }
                $data['node_id'] = intval($this->validation->node_id);
                $data['stage'] = 4;
-               if ( $this->checknodeid($data['node_id']) )
-               {
+               if ( $this->checknodeid($data['node_id']) ) {
                        $data = $this->get_stage4_data($person, $data);
                        $this->load->view('header', $data);
                        $this->load->view('debug', $data);
@@ -585,19 +621,19 @@ class Register extends Controller {
                # TODO: RECODE To update values instead of adding them...
                global $api, $plc;
                $hostname = trim($_REQUEST['hostname']);
-               $model= trim($_REQUEST['model']);
+               $node_type = trim($_REQUEST['node_type']);
+               $model = trim($_REQUEST['model']);
                $node_id = intval($this->validation->node_id);
-               $ret = $api->UpdateNode( $node_id, array('hostname' => $hostname, 
-                                                                                 'model' => $model));
+               $optional_vals = array('hostname' => $hostname, 'model' => $model, 'node_type' => $node_type );
+               $ret = $api->UpdateNode( $node_id, $optional_vals);
                if( $ret <= 0 ) {
                        $data['error'] = $api->error();
                        print $data['error'];
                }
 
                $api_node_list = $api->GetNodes($node_id);
-               if ( count($api_node_list) > 0 )
-               {
-                       $node_obj = new Node($api_node_list[0]);
+               if ( count($api_node_list) > 0 ) {
+                       $node_obj = new Node($api_node_list[0], True);
                } else {
                        print "broken!!!";
                        exit (1);
@@ -618,11 +654,11 @@ class Register extends Controller {
 
                // used to generate error strings for static fields only
                $static_fields= array();
-               $static_fields['netmask']= "Netmask address";
-               $static_fields['network']= "Network address";
-               $static_fields['gateway']= "Gateway address";
-               $static_fields['broadcast']= "Broadcast address";
-               $static_fields['dns1']= "Primary DNS address";
+               $static_fields['netmask']       = "Netmask address";
+               $static_fields['network']       = "Network address";
+               $static_fields['gateway']       = "Gateway address";
+               $static_fields['broadcast']     = "Broadcast address";
+               $static_fields['dns1']          = "Primary DNS address";
 
                if ( $method == 'static' )
                {
@@ -666,8 +702,8 @@ class Register extends Controller {
 
                        if ( count($optional_vals) > 0 )
                        {
-                               print_r($optional_vals);
-                               $ret = $api->UpdateNodeNetwork( $node_obj->nodenetwork_id, $optional_vals);
+                         // print_r($optional_vals);
+                               $ret = $api->UpdateInterface( $node_obj->interface_id, $optional_vals);
                                if( $ret <= 0 ) {
                                        $data['error'] = $api->error();
                                        print $data['error'];
@@ -689,6 +725,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 +746,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;
                }