/**
  * @warning this is not valid if $itemtype_1 == $itemtype_2 !
  *
  * @since version 0.85
  *
  * @see CommonDBTM::processMassiveActionsForOneItemtype()
  **/
 static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids)
 {
     global $DB;
     $action = $ma->getAction();
     $input = $ma->getInput();
     $specificities = static::getRelationMassiveActionsSpecificities();
     // First, get normalized action : add or remove
     if (in_array($action, $specificities['normalized']['add'])) {
         $normalized_action = 'add';
     } else {
         if (in_array($action, $specificities['normalized']['remove'])) {
             $normalized_action = 'remove';
         } else {
             // If we cannot get normalized action, then, its not for this method !
             parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
             return;
         }
     }
     $link = new static();
     // Get the default 'input' entries from the relation
     $input2 = static::getRelationInputForProcessingOfMassiveActions($action, $item, $ids, $input);
     // complete input2 with the right fields from input and define the peer with this information
     foreach (array(static::$itemtype_1, static::$items_id_1) as $field) {
         if (isset($input['peer_' . $field])) {
             $input2[$field] = $input['peer_' . $field];
             $item_number = 2;
         }
     }
     foreach (array(static::$itemtype_2, static::$items_id_2) as $field) {
         if (isset($input['peer_' . $field])) {
             $input2[$field] = $input['peer_' . $field];
             $item_number = 1;
         }
     }
     // If the fields provided by showMassiveActionsSubForm are not valid then quit !
     if (!isset($item_number)) {
         $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
         $ma->addMessage($link->getErrorMessage(ERROR_NOT_FOUND));
         return;
     }
     if ($item_number == 1) {
         $itemtype = static::$itemtype_1;
         $items_id = static::$items_id_1;
         $peertype = static::$itemtype_2;
         $peers_id = static::$items_id_2;
     } else {
         $itemtype = static::$itemtype_2;
         $items_id = static::$items_id_2;
         $peertype = static::$itemtype_1;
         $peers_id = static::$items_id_1;
     }
     if (preg_match('/^itemtype/', $itemtype)) {
         $input2[$itemtype] = $item->getType();
     }
     // Get the peer from the $input2 and the name of its fields
     $peer = static::getItemFromArray($peertype, $peers_id, $input2, true, true, true);
     // $peer not valid => not in DB or try to remove all at once !
     if ($peer === false || $peer->isNewItem()) {
         if (isset($input2[$peers_id]) && $input2[$peers_id] > 0) {
             $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
             if ($peer instanceof CommonDBTM) {
                 $ma->addMessage($peer->getErrorMessage(ERROR_NOT_FOUND));
             } else {
                 $ma->addMessage($link->getErrorMessage(ERROR_NOT_FOUND));
             }
             return;
         }
         if (!$specificities['can_remove_all_at_once'] && !$specificities['only_remove_all_at_once']) {
             return false;
         }
         $peer = false;
     }
     // Make a link between $item_1, $item_2 and $item and $peer. Thus, we will be able to update
     // $item without having to care about the number of the item
     if ($item_number == 1) {
         $item_1 =& $item;
         $item_2 =& $peer;
     } else {
         $item_1 =& $peer;
         $item_2 =& $item;
     }
     switch ($normalized_action) {
         case 'add':
             // remove all at once only available for remove !
             if (!$peer) {
                 $ma->itemDone($item->getType(), $ids, MassiveAction::ACTION_KO);
                 $ma->addMessage($link->getErrorMessage(ERROR_ON_ACTION));
                 return;
             }
             foreach ($ids as $key) {
                 if (!$item->getFromDB($key)) {
                     $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
                     $ma->addMessage($item->getErrorMessage(ERROR_NOT_FOUND));
                     continue;
                 }
                 $input2[$items_id] = $item->getID();
                 // If 'can_link_several_times', then, we add the elements !
                 if ($specificities['can_link_several_times']) {
                     if ($link->can(-1, CREATE, $input2)) {
                         if ($link->add($input2)) {
                             $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK);
                         } else {
                             $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
                             $ma->addMessage($link->getErrorMessage(ERROR_ON_ACTION));
                         }
                     } else {
                         $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_NORIGHT);
                         $ma->addMessage($link->getErrorMessage(ERROR_RIGHT));
                     }
                 } else {
                     $link->getEmpty();
                     if (!$link->getFromDBForItems($item_1, $item_2)) {
                         if ($specificities['check_both_items_if_same_type'] && $item_1->getType() == $item_2->getType()) {
                             $link->getFromDBForItems($item_2, $item_1);
                         }
                     }
                     if (!$link->isNewItem()) {
                         if (!$specificities['update_if_different']) {
                             $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
                             $ma->addMessage($link->getErrorMessage(ERROR_ALREADY_DEFINED));
                             continue;
                         }
                         $input2[static::getIndexName()] = $link->getID();
                         if ($link->can($link->getID(), UPDATE, $input2)) {
                             if ($link->update($input2)) {
                                 $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK);
                             } else {
                                 $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
                                 $ma->addMessage($link->getErrorMessage(ERROR_ON_ACTION));
                             }
                         } else {
                             $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_NORIGHT);
                             $ma->addMessage($link->getErrorMessage(ERROR_RIGHT));
                         }
                         // if index defined, then cannot not add any other link due to index unicity
                         unset($input2[static::getIndexName()]);
                     } else {
                         if ($link->can(-1, CREATE, $input2)) {
                             if ($link->add($input2)) {
                                 $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK);
                             } else {
                                 $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
                                 $ma->addMessage($link->getErrorMessage(ERROR_ON_ACTION));
                             }
                         } else {
                             $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_NORIGHT);
                             $ma->addMessage($link->getErrorMessage(ERROR_RIGHT));
                         }
                     }
                 }
             }
             return;
         case 'remove':
             foreach ($ids as $key) {
                 // First, get the query to find all occurences of the link item<=>key
                 if (!$peer) {
                     $query = static::getSQLRequestToSearchForItem($item->getType(), $key);
                 } else {
                     if (!$item->getFromDB($key)) {
                         $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
                         $ma->addMessage($item->getErrorMessage(ERROR_NOT_FOUND));
                         continue;
                     }
                     $query = 'SELECT `' . static::getIndexName() . '`
                         FROM `' . static::getTable() . '`';
                     $WHERE = array();
                     if (preg_match('/^itemtype/', static::$itemtype_1)) {
                         $WHERE[] = " `" . static::$itemtype_1 . "` = '" . $item_1->getType() . "'";
                     }
                     $WHERE[] = " `" . static::$items_id_1 . "` = '" . $item_1->getID() . "'";
                     if (preg_match('/^itemtype/', static::$itemtype_2)) {
                         $WHERE[] = " `" . static::$itemtype_2 . "` = '" . $item_2->getType() . "'";
                     }
                     $WHERE[] = " `" . static::$items_id_2 . "` = '" . $item_2->getID() . "'";
                     $query .= 'WHERE (' . implode(' AND ', $WHERE) . ')';
                     if ($specificities['check_both_items_if_same_type'] && $item_1->getType() == $item_2->getType()) {
                         $WHERE = array();
                         if (preg_match('/^itemtype/', static::$itemtype_1)) {
                             $WHERE[] = " `" . static::$itemtype_1 . "` = '" . $item_2->getType() . "'";
                         }
                         $WHERE[] = " `" . static::$items_id_1 . "` = '" . $item_2->getID() . "'";
                         if (preg_match('/^itemtype/', static::$itemtype_2)) {
                             $WHERE[] = " `" . static::$itemtype_2 . "` = '" . $item_2->getType() . "'";
                         }
                         $WHERE[] = " `" . static::$items_id_2 . "` = '" . $item_2->getID() . "'";
                         $query .= ' OR (' . implode(' AND ', $WHERE) . ')';
                     }
                 }
                 $request = $DB->request($query);
                 $number_results = $request->numrows();
                 if ($number_results == 0) {
                     $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
                     $ma->addMessage($link->getErrorMessage(ERROR_NOT_FOUND));
                     continue;
                 }
                 $ok = 0;
                 $ko = 0;
                 $noright = 0;
                 foreach ($request as $line) {
                     if ($link->can($line[static::getIndexName()], DELETE)) {
                         if ($link->delete(array('id' => $line[static::getIndexName()]))) {
                             $ok++;
                         } else {
                             $ko++;
                             $ma->addMessage($link->getErrorMessage(ERROR_ON_ACTION));
                         }
                     } else {
                         $noright++;
                         $ma->addMessage($link->getErrorMessage(ERROR_RIGHT));
                     }
                 }
                 if ($ok == $number_results) {
                     $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_OK);
                 } else {
                     if ($noright > 0) {
                         $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_NORIGHT);
                     } else {
                         if ($ko > 0) {
                             $ma->itemDone($item->getType(), $key, MassiveAction::ACTION_KO);
                         }
                     }
                 }
             }
             return;
     }
     parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
 }
Esempio n. 2
0
 /**
  * We can add several single CommonDBChild to a given Item. In such case, we display a "+"
  * button and the fields already entered.
  * This method display the fields
  *
  * @since version 0.84
  *
  * @todo study if we cannot use these methods for the user emails
  * @see showAddChildButtonForItemForm()
  *
  * @param $item         CommonDBTM object the item on which to add the current CommenDBChild
  * @param $field_name   the name of the HTML field inside Item's form
  * @param $canedit      (default NULL) NULL to use default behaviour
  *
  * @return nothing (display only)
  **/
 static function showChildsForItemForm(CommonDBTM $item, $field_name, $canedit = NULL)
 {
     global $DB, $CFG_GLPI;
     $items_id = $item->getID();
     if (is_null($canedit)) {
         if ($item->isNewItem()) {
             if (!$item->canCreate()) {
                 return false;
             }
             $canedit = $item->canUpdate();
         } else {
             if (!$item->can($items_id, 'r')) {
                 return false;
             }
             $canedit = $item->can($items_id, "w");
         }
     }
     $lower_name = strtolower(get_called_class());
     $div_id = "add_" . $lower_name . "_to_" . $item->getType() . "_" . $items_id;
     // To be sure not to load bad datas from this table
     if ($items_id == 0) {
         $items_id = -99;
     }
     $query = "SELECT *\n                FROM `" . static::getTable() . "`\n                WHERE `" . static::$items_id . "` = '" . $item->getID() . "'";
     if (preg_match('/^itemtype/', static::$itemtype)) {
         $query .= " AND `itemtype` = '" . $item->getType() . "'";
     }
     $current_item = new static();
     if ($current_item->maybeDeleted()) {
         $query .= " AND `is_deleted` = '0'";
     }
     $count = 0;
     foreach ($DB->request($query) as $data) {
         $current_item->fields = $data;
         if ($count) {
             echo '<br>';
         }
         $count++;
         $current_item->showChildForItemForm($canedit, $field_name . "[" . $current_item->getID() . "]");
     }
     if ($canedit) {
         echo "<div id='{$div_id}'>";
         // No Child display field
         if ($count == 0) {
             $current_item->getEmpty();
             $current_item->showChildForItemForm($canedit, $field_name . "[-100]");
         }
         echo "</div>";
     }
 }