NetworkPort::switchInstantiationType PHP Method

switchInstantiationType() public method

Change the instantion type of a NetworkPort : check validity of the new type of instantiation and that it is not equal to current ones. Update the NetworkPort and delete the previous instantiation. It is up to the caller to create the new instantiation !
public switchInstantiationType ( $new_instantiation_type ) : false
$new_instantiation_type the name of the new instaniation type
return false on error, true if the previous instantiation is not available (ie.: invalid instantiation type) or the object of the previous instantiation.
    function switchInstantiationType($new_instantiation_type)
    {
        // First, check if the new instantiation is a valid one ...
        if (!in_array($new_instantiation_type, self::getNetworkPortInstantiations())) {
            return false;
        }
        // Load the previous instantiation
        $previousInstantiation = $this->getInstantiation();
        // If the previous instantiation is the same than the new one: nothing to do !
        if ($previousInstantiation !== false && $previousInstantiation->getType() == $new_instantiation_type) {
            return $previousInstantiation;
        }
        // We update the current NetworkPort
        $input = $this->fields;
        $input['instantiation_type'] = $new_instantiation_type;
        $this->update($input);
        // Then, we delete the previous instantiation
        if ($previousInstantiation !== false) {
            $previousInstantiation->delete($previousInstantiation->fields);
            return $previousInstantiation;
        }
        return true;
    }

Usage Example

 /**
  * @since version 0.85
  *
  * @see CommonDBTM::processMassiveActionsForOneItemtype()
  **/
 static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids)
 {
     global $DB;
     switch ($ma->getAction()) {
         case 'transform_to':
             $input = $ma->getInput();
             if (isset($input["transform_to"]) && !empty($input["transform_to"])) {
                 $networkport = new NetworkPort();
                 foreach ($ids as $id) {
                     if ($networkport->canEdit($id) && $item->can($id, DELETE)) {
                         if (empty($networkport->fields['instantiation_type'])) {
                             if ($networkport->switchInstantiationType($input['transform_to']) !== false) {
                                 $instantiation = $networkport->getInstantiation();
                                 $input2 = $item->fields;
                                 $input2['networkports_id'] = $input2['id'];
                                 unset($input2['id']);
                                 if ($instantiation->add($input2)) {
                                     $item->delete(array('id' => $id));
                                     $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
                                 } else {
                                     $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                                     $ma->addMessage($networkport->getErrorMessage(ERROR_ON_ACTION));
                                 }
                             } else {
                                 $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                                 $ma->addMessage($networkport->getErrorMessage(ERROR_ON_ACTION));
                             }
                         } else {
                             $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                             $ma->addMessage($networkport->getErrorMessage(ERROR_ON_ACTION));
                         }
                     } else {
                         $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
                         $ma->addMessage($networkport->getErrorMessage(ERROR_RIGHT));
                     }
                 }
             } else {
                 $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
             }
             return;
     }
     parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
 }
All Usage Examples Of NetworkPort::switchInstantiationType