NetworkPort::splitInputForElements PHP Method

splitInputForElements() public method

The form of the NetworkPort can contain the details of the NetworkPortInstantiation as well as NetworkName elements (if no more than one name is attached to this port). Feilds from both NetworkPortInstantiation and NetworkName must not be process by the NetworkPort::add or NetworkPort::update. But they must be kept for adding or updating these elements. This is done after creating or updating the current port. Otherwise, its ID may not be known (in case of new port). To keep the unused fields, we check each field key. If it is owned by NetworkPort (ie : exists inside the $this->fields array), then they remain inside $input. If they are prefix by "Networkname_", then they are added to $this->input_for_NetworkName. Else, they are for the instantiation and added to $this->input_for_instantiation. This method must be call before NetworkPort::add or NetworkPort::update in case of NetworkPort form. Otherwise, the entry of the database may contain wrong values.
See also: updateDependencies for the update
public splitInputForElements ( $input )
$input
    function splitInputForElements($input)
    {
        if (isset($this->input_for_instantiation) || isset($this->input_for_NetworkName) || isset($this->input_for_NetworkPortConnect) || !isset($input)) {
            return;
        }
        $this->input_for_instantiation = array();
        $this->input_for_NetworkName = array();
        $this->input_for_NetworkPortConnect = array();
        $clone = clone $this;
        $clone->getEmpty();
        foreach ($input as $field => $value) {
            if (array_key_exists($field, $clone->fields) || $field[0] == '_') {
                continue;
            }
            if (preg_match('/^NetworkName_/', $field)) {
                $networkName_field = preg_replace('/^NetworkName_/', '', $field);
                $this->input_for_NetworkName[$networkName_field] = $value;
            } else {
                if (preg_match('/^NetworkPortConnect_/', $field)) {
                    $networkName_field = preg_replace('/^NetworkPortConnect_/', '', $field);
                    $this->input_for_NetworkPortConnect[$networkName_field] = $value;
                } else {
                    $this->input_for_instantiation[$field] = $value;
                }
            }
            unset($input[$field]);
        }
        return $input;
    }

Usage Example

 /**
  * @test
  */
 public function prepareDB()
 {
     global $DB;
     $DB->connect();
     $entity = new Entity();
     $computer = new Computer();
     $pfAgent = new PluginFusioninventoryAgent();
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfIPRange = new PluginFusioninventoryIPRange();
     $networkEquipment = new NetworkEquipment();
     $networkPort = new NetworkPort();
     $printer = new Printer();
     $pfNetworkEquipment = new PluginFusioninventoryNetworkEquipment();
     // Create entities
     $_SESSION['glpiactive_entity'] = 0;
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`)\n         VALUES (1, 'ent1', 0, 'Entité racine > ent1', 2)");
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`)\n         VALUES (2, 'ent2', 0, 'Entité racine > ent2', 2)");
     $DB->query("INSERT INTO `glpi_entities`\n         (`id`, `name`, `entities_id`, `completename`, `level`)\n         VALUES (3, 'ent1.1', 1, 'Entité racine > ent1 > ent1.1', 3)");
     // Create computers + agents
     $input = array('entities_id' => 0, 'name' => 'computer1');
     $computers_id = $computer->add($input);
     $input = array('entities_id' => 0, 'name' => 'computer1', 'version' => '{"INVENTORY":"v2.3.11"}', 'device_id' => 'computer1', 'useragent' => 'FusionInventory-Agent_v2.3.11', 'computers_id' => $computers_id);
     $pfAgent->add($input);
     // Create Network Equipments
     $input = array('name' => 'sw0', 'entities_id' => 0);
     $networkEquipment->add($input);
     $input = array('entities_id' => 0, 'name' => 'management', 'items_id' => 1, 'itemtype' => 'NetworkEquipment', 'instantiation_type' => 'NetworkPortAggregate', 'NetworkName__ipaddresses' => array('-1' => '10.0.0.10'));
     $networkPort->splitInputForElements($input);
     $networkPort->add($input);
     $networkPort->updateDependencies(1);
     $input = array('networkequipments_id' => 1, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfNetworkEquipment->add($input);
     $input = array('name' => 'sw1', 'entities_id' => 1);
     $networkEquipment->add($input);
     $input = array('entities_id' => 1, 'name' => 'management', 'items_id' => 2, 'itemtype' => 'NetworkEquipment', 'instantiation_type' => 'NetworkPortAggregate', 'NetworkName__ipaddresses' => array('-1' => '10.0.0.11'));
     $networkPort->splitInputForElements($input);
     $networkPort->add($input);
     $networkPort->updateDependencies(1);
     $input = array('networkequipments_id' => 2, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfNetworkEquipment->add($input);
     $input = array('name' => 'sw2', 'entities_id' => 2);
     $networkEquipment->add($input);
     $input = array('entities_id' => 2, 'name' => 'management', 'items_id' => 3, 'itemtype' => 'NetworkEquipment', 'instantiation_type' => 'NetworkPortAggregate', 'NetworkName__ipaddresses' => array('-1' => '10.0.0.12'));
     $networkPort->splitInputForElements($input);
     $networkPort->add($input);
     $networkPort->updateDependencies(1);
     $input = array('networkequipments_id' => 3, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfNetworkEquipment->add($input);
     $input = array('name' => 'sw3/1.1', 'entities_id' => 3);
     $networkEquipment->add($input);
     $input = array('entities_id' => 3, 'name' => 'management', 'items_id' => 4, 'itemtype' => 'NetworkEquipment', 'instantiation_type' => 'NetworkPortAggregate', 'NetworkName__ipaddresses' => array('-1' => '10.0.0.21'));
     $networkPort->splitInputForElements($input);
     $networkPort->add($input);
     $networkPort->updateDependencies(1);
     $input = array('networkequipments_id' => 4, 'plugin_fusioninventory_configsecurities_id' => 2);
     $pfNetworkEquipment->add($input);
     // Create Printers
     // Add IPRange
     $input = array('entities_id' => 1, 'name' => 'Office', 'ip_start' => '10.0.0.1', 'ip_end' => '10.0.0.254');
     $ipranges_id = $pfIPRange->add($input);
     // Allow all agents to do network discovery
     $query = "UPDATE `glpi_plugin_fusioninventory_agentmodules` " . " SET `is_active`='1' " . " WHERE `modulename`='NETWORKINVENTORY'";
     $DB->query($query);
     // create task
     $input = array('entities_id' => 0, 'name' => 'network inventory', 'is_active' => 1);
     $tasks_id = $pfTask->add($input);
     // create taskjob
     $input = array('plugin_fusioninventory_tasks_id' => $tasks_id, 'entities_id' => 0, 'name' => 'inventory', 'method' => 'networkinventory', 'targets' => '[{"PluginFusioninventoryIPRange":"' . $ipranges_id . '"}]', 'actors' => '[{"PluginFusioninventoryAgent":"1"}]');
     $pfTaskjob->add($input);
 }
All Usage Examples Of NetworkPort::splitInputForElements