예제 #1
0
 /**
  * @since version 0.84
  **/
 function prepareInputForUpdate($input)
 {
     if (!is_array($input)) {
         return false;
     }
     // True if item changed
     if (!parent::checkAttachedItemChangesAllowed($input, array(static::$itemtype_1, static::$items_id_1, static::$itemtype_2, static::$items_id_2))) {
         return false;
     }
     return parent::addNeededInfoToInput($input);
 }
 /**
  * @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);
 }
예제 #3
0
 /**
  * Get the standard massive actions
  *
  * @param $item                   the item for which we want the massive actions
  * @param $is_deleted             massive action for deleted items ?   (default 0)
  * @param $checkitem              link item to check right              (default NULL)
  *
  * @return an array of massive actions or false if $item is not valid
  **/
 static function getAllMassiveActions($item, $is_deleted = 0, CommonDBTM $checkitem = NULL)
 {
     global $CFG_GLPI, $PLUGIN_HOOKS;
     // TODO: when maybe* will be static, when can completely switch to $itemtype !
     if (is_string($item)) {
         $itemtype = $item;
         if (!($item = getItemForItemtype($itemtype))) {
             return false;
         }
     } else {
         if ($item instanceof CommonDBTM) {
             $itemtype = $item->getType();
         } else {
             return false;
         }
     }
     if (!is_null($checkitem)) {
         $canupdate = $checkitem->canUpdate();
         $candelete = $checkitem->canDelete();
         $canpurge = $checkitem->canPurge();
     } else {
         $canupdate = $itemtype::canUpdate();
         $candelete = $itemtype::canDelete();
         $canpurge = $itemtype::canPurge();
     }
     $actions = array();
     $self_pref = __CLASS__ . self::CLASS_ACTION_SEPARATOR;
     if ($is_deleted) {
         if ($canpurge) {
             if (in_array($itemtype, Item_Devices::getConcernedItems())) {
                 $actions[$self_pref . 'purge_item_but_devices'] = _x('button', 'Delete permanently but keep devices');
                 $actions[$self_pref . 'purge'] = _x('button', 'Delete permanently and remove devices');
             } else {
                 $actions[$self_pref . 'purge'] = _x('button', 'Delete permanently');
             }
             $actions[$self_pref . 'restore'] = _x('button', 'Restore');
         }
     } else {
         if ($_SESSION['glpiactiveprofile']['interface'] == 'central' && ($canupdate || InfoCom::canApplyOn($itemtype) && Infocom::canUpdate())) {
             //TRANS: select action 'update' (before doing it)
             $actions[$self_pref . 'update'] = _x('button', 'Update');
         }
         Infocom::getMassiveActionsForItemtype($actions, $itemtype, $is_deleted, $checkitem);
         CommonDBConnexity::getMassiveActionsForItemtype($actions, $itemtype, $is_deleted, $checkitem);
         // do not take into account is_deleted if items may be dynamic
         if ($item->maybeDeleted() && !$item->useDeletedToLockIfDynamic()) {
             if ($candelete) {
                 $actions[$self_pref . 'delete'] = _x('button', 'Put in dustbin');
             }
         } else {
             if ($canpurge) {
                 $actions[$self_pref . 'purge'] = _x('button', 'Delete permanently');
                 if ($item instanceof CommonDropdown) {
                     $actions[$self_pref . 'purge_but_item_linked'] = _x('button', 'Delete permanently even if linked items');
                 }
             }
         }
         Document::getMassiveActionsForItemtype($actions, $itemtype, $is_deleted, $checkitem);
         Contract::getMassiveActionsForItemtype($actions, $itemtype, $is_deleted, $checkitem);
         // Specific actions
         $actions += $item->getSpecificMassiveActions($checkitem);
         // Plugin Specific actions
         if (isset($PLUGIN_HOOKS['use_massive_action'])) {
             foreach ($PLUGIN_HOOKS['use_massive_action'] as $plugin => $val) {
                 $plug_actions = Plugin::doOneHook($plugin, 'MassiveActions', $itemtype);
                 if (count($plug_actions)) {
                     $actions += $plug_actions;
                 }
             }
         }
     }
     Lock::getMassiveActionsForItemtype($actions, $itemtype, $is_deleted, $checkitem);
     // Manage forbidden actions : try complete action name or MassiveAction:action_name
     $forbidden_actions = $item->getForbiddenStandardMassiveAction();
     if (is_array($forbidden_actions) && count($forbidden_actions)) {
         foreach ($forbidden_actions as $actiontodel) {
             if (isset($actions[$actiontodel])) {
                 unset($actions[$actiontodel]);
             } else {
                 // Not found search adding MassiveAction prefix
                 $actiontodel = $self_pref . $actiontodel;
                 if (isset($actions[$actiontodel])) {
                     unset($actions[$actiontodel]);
                 }
             }
         }
     }
     return $actions;
 }