/**
  * Link inventory object to another one
  *
  * @param $params    the input parameters
  * @param $protocol  the commonication protocol used
  *
  **/
 static function methodLinkObjects($params, $protocol)
 {
     global $CFG_GLPI;
     if (isset($params['help'])) {
         return array('fields' => 'array, mandatory', 'help' => 'bool, optional');
     }
     if (!Session::getLoginUserID()) {
         return self::Error($protocol, WEBSERVICES_ERROR_NOTAUTHENTICATED);
     }
     //Must be superadmin to use this method
     if (!Session::haveRight('config', UPDATE)) {
         return self::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED);
     }
     if (!isset($params['fields']) || empty($params['fields']) || !is_array($params['fields'])) {
         return self::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'fields');
     }
     if (!isset($_SESSION["glpi_currenttime"])) {
         $_SESSION["glpi_currenttime"] = date("Y-m-d H:i:s");
     }
     $resp = array();
     $errors = array();
     foreach ($params['fields'] as $links) {
         if (!in_array($links['from_item']['itemtype'], array('Computer')) && !preg_match("/Device/", $links['from_item']['itemtype'])) {
             $errors[] = self::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, '', self::getDisplayError());
         }
         switch ($links['from_item']['itemtype']) {
             case 'Computer':
                 // Direct connections
                 if (in_array($links['to_item']['itemtype'], array('Monitor', 'Peripheral', 'Phone', 'Printer'))) {
                     $comp_item = new Computer_Item();
                     $data = array();
                     $data['items_id'] = $links['to_item']['id'];
                     $data['computers_id'] = $links['from_item']['id'];
                     $data['itemtype'] = $links['to_item']['itemtype'];
                     if (!$comp_item->can(-1, UPDATE, $data)) {
                         $errors[] = self::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, '', self::getDisplayError());
                     } else {
                         if ($comp_item->add($data)) {
                             $resp['Computer'][$data['computers_id']] = self::methodGetObject(array('itemtype' => 'Computer', 'id' => $data['computers_id'], 'with_printer' => 1, 'with_monitor' => 1, 'with_phone' => 1, 'with_peripheral' => 1), $protocol);
                         } else {
                             $errors[] = self::Error($protocol, WEBSERVICES_ERROR_FAILED, '', self::getDisplayError());
                         }
                     }
                 }
                 // Device connection
                 if (preg_match("/Device/", $links['to_item']['itemtype'])) {
                     $comp_device = new Computer_Device();
                     $links_field = getPlural(strtolower($links['to_item']['itemtype'])) . "_id";
                     $data = array();
                     $data['computers_id'] = $links['from_item']['id'];
                     $data[$links_field] = $links['to_item']['id'];
                     $data['itemtype'] = $links['to_item']['itemtype'];
                     if (!isset($links['to_item']['quantity']) || !is_numeric($links['to_item']['quantity'])) {
                         $quantity = 1;
                     } else {
                         $quantity = $links['to_item']['quantity'];
                     }
                     if (isset($links['to_item']['specificity'])) {
                         if (!is_numeric($links['to_item']['specificity'])) {
                             $errors[] = self::Error($protocol, WEBSERVICES_ERROR_MISSINGPARAMETER, '', 'specificity');
                         } else {
                             $data['specificity'] = $links['to_item']['specificity'];
                         }
                     }
                     $linked = false;
                     for ($i = 0; $i < $quantity; $i++) {
                         if (!$comp_device->can(-1, UPDATE, $data)) {
                             $errors[] = self::Error($protocol, WEBSERVICES_ERROR_NOTALLOWED, '', self::getDisplayError());
                         } else {
                             if ($comp_device->add($data)) {
                                 $linked = true;
                             }
                         }
                     }
                     if ($linked) {
                         $resp['Computer'][$data['computers_id']] = self::methodGetObject(array('itemtype' => 'Computer', 'id' => $data['computers_id']), $protocol);
                     } else {
                         $errors[] = self::Error($protocol, WEBSERVICES_ERROR_FAILED, '', self::getDisplayError());
                     }
                 }
                 //other link object
                 break;
                 //itemtype
         }
     }
     if (count($errors)) {
         $resp = array($resp, $errors);
     }
     return $resp;
 }
Пример #2
0
             if ($val == 1) {
                 $input = array('computers_id' => $key, 'itemtype' => $_POST["itemtype"], 'items_id' => $_POST["connect_item"]);
                 if ($conn->can(-1, 'w', $input)) {
                     $conn->add($input);
                 }
             }
         }
     }
     break;
 case "connect":
     if (isset($_POST["connect_item"]) && $_POST["connect_item"]) {
         $conn = new Computer_Item();
         foreach ($_POST["item"] as $key => $val) {
             if ($val == 1) {
                 $input = array('computers_id' => $_POST["connect_item"], 'itemtype' => $_POST["itemtype"], 'items_id' => $key);
                 if ($conn->can(-1, 'w', $input)) {
                     $conn->add($input);
                 }
             }
         }
     }
     break;
 case "disconnect":
     $conn = new Computer_Item();
     foreach ($_POST["item"] as $key => $val) {
         if ($val == 1) {
             if ($item->getFromDB($key)) {
                 $conn->disconnectForItem($item);
             }
         }
     }