Ejemplo n.º 1
0
 /**
  * @since version 0.85
  *
  * @see CommonDBTM::processMassiveActionsForOneItemtype()
  **/
 static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids)
 {
     switch ($ma->getAction()) {
         case 'add_item':
             $input = $ma->getInput();
             $item_ticket = new static();
             foreach ($ids as $id) {
                 if ($item->getFromDB($id) && !empty($input['items_id'])) {
                     $input['tickets_id'] = $id;
                     $input['itemtype'] = $input['item_itemtype'];
                     if ($item_ticket->can(-1, CREATE, $input)) {
                         $ok = true;
                         if (!$item_ticket->add($input)) {
                             $ok = false;
                         }
                         if ($ok) {
                             $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
                         } else {
                             $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                             $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
                         }
                     } else {
                         $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
                         $ma->addMessage($item->getErrorMessage(ERROR_RIGHT));
                     }
                 } else {
                     $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                     $ma->addMessage($item->getErrorMessage(ERROR_NOT_FOUND));
                 }
             }
             return;
         case 'delete_item':
             $input = $ma->getInput();
             $item_ticket = new static();
             foreach ($ids as $id) {
                 if ($item->getFromDB($id) && !empty($input['items_id'])) {
                     $item_found = $item_ticket->find("`tickets_id` = {$id} AND `itemtype` = '" . $input['item_itemtype'] . "' AND `items_id` = " . $input['items_id']);
                     if (!empty($item_found)) {
                         $item_founds_id = array_keys($item_found);
                         $input['id'] = $item_founds_id[0];
                         if ($item_ticket->can($input['id'], DELETE, $input)) {
                             $ok = true;
                             if (!$item_ticket->delete($input)) {
                                 $ok = false;
                             }
                             if ($ok) {
                                 $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
                             } else {
                                 $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                                 $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
                             }
                         } else {
                             $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
                             $ma->addMessage($item->getErrorMessage(ERROR_RIGHT));
                         }
                     } else {
                         $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                         $ma->addMessage($item->getErrorMessage(ERROR_NOT_FOUND));
                     }
                 } else {
                     $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                     $ma->addMessage($item->getErrorMessage(ERROR_NOT_FOUND));
                 }
             }
             return;
     }
     parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
 }
 /**
  * @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);
 }
Ejemplo n.º 3
0
 /**
  * @since version 0.85
  *
  * @see CommonDBTM::processMassiveActionsForOneItemtype()
  **/
 static function processMassiveActionsForOneItemtype(MassiveAction $ma, CommonDBTM $item, array $ids)
 {
     switch ($ma->getAction()) {
         case 'submit_validation':
             $input = $ma->getInput();
             $valid = new static();
             foreach ($ids as $id) {
                 if ($item->getFromDB($id)) {
                     $input2 = array(static::$items_id => $id, 'comment_submission' => $input['comment_submission']);
                     if ($valid->can(-1, CREATE, $input2)) {
                         $users = $input['users_id_validate'];
                         if (!is_array($users)) {
                             $users = array($users);
                         }
                         $ok = true;
                         foreach ($users as $user) {
                             $input2["users_id_validate"] = $user;
                             if (!$valid->add($input2)) {
                                 $ok = false;
                             }
                         }
                         if ($ok) {
                             $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
                         } else {
                             $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                             $ma->addMessage($item->getErrorMessage(ERROR_ON_ACTION));
                         }
                     } else {
                         $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_NORIGHT);
                         $ma->addMessage($item->getErrorMessage(ERROR_RIGHT));
                     }
                 } else {
                     $ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
                     $ma->addMessage($item->getErrorMessage(ERROR_NOT_FOUND));
                 }
             }
             return;
     }
     parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
 }