Skip to content

Commit

Permalink
Factorize clone requests; add some tests (glpi-project#7624)
Browse files Browse the repository at this point in the history
* Factorize clone requests; add some tests

Remove all overrides (thanks to @cedric-anne!)
Get from existing data
Add a method to retrieve field name
  • Loading branch information
trasher authored Jul 9, 2020
1 parent 26dd1f1 commit 8fe965c
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 204 deletions.
4 changes: 0 additions & 4 deletions inc/calendar.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,8 +654,4 @@ function updateDurationCache($calendars_id) {
static function getDayNumberInWeek($date) {
return (int)date('w', $date);
}

public function getOverrideFieldName(): string {
return 'calendars_id';
}
}
20 changes: 0 additions & 20 deletions inc/calendar_holiday.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,6 @@ class Calendar_Holiday extends CommonDBRelation {
static public $checkItem_2_Rights = self::DONT_CHECK_ITEM_RIGHTS;


/**
* get the request results to get items associated to the given one (defined by $itemtype and $items_id)
*
* @param string $itemtype the type of the item we want the resulting items to be associated to
* @param string $items_id the name of the item we want the resulting items to be associated to
*
* @return array the items associated to the given one (empty if none was found)
**/
static function getItemsAssociationRequest($itemtype, $items_id) {
global $DB;

return $DB->request([
'SELECT' => 'id',
'FROM' => static::getTable(),
'WHERE' => [
static::$items_id_1 => $items_id
]
]);
}

/**
* @since 0.84
**/
Expand Down
20 changes: 0 additions & 20 deletions inc/calendarsegment.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,6 @@ function prepareInputForAdd($input) {
return parent::prepareInputForAdd($input);
}

/**
* get the request results to get items associated to the given one (defined by $itemtype and $items_id)
*
* @param string $itemtype the type of the item we want the resulting items to be associated to
* @param string $items_id the name of the item we want the resulting items to be associated to
*
* @return array the items associated to the given one (empty if none was found)
**/
static function getItemsAssociationRequest($itemtype, $items_id) {
global $DB;

return $DB->request([
'SELECT' => 'id',
'FROM' => static::getTable(),
'WHERE' => [
static::$items_id => $items_id
]
]);
}

/**
* Duplicate all segments from a calendar to his clone
*
Expand Down
35 changes: 12 additions & 23 deletions inc/commondbchild.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,6 @@ static function getSQLCriteriaToSearchForItem($itemtype, $items_id) {
return null;
}

/**
* get the request results to get items associated to the given one (defined by $itemtype and $items_id)
*
* @since 9.5
*
* @param string $itemtype the type of the item we want the resulting items to be associated to
* @param string $items_id the name of the item we want the resulting items to be associated to
*
* @return array the items associated to the given one (empty if none was found)
**/
static function getItemsAssociationRequest($itemtype, $items_id) {
global $DB;

return $DB->request([
'SELECT' => 'id',
'FROM' => static::getTable(),
'WHERE' => [
static::$itemtype => $itemtype,
static::$items_id => $items_id
]
]);
}


/**
* @since 0.84
Expand Down Expand Up @@ -882,4 +859,16 @@ function affectChild($id, $items_id = 0, $itemtype = '') {

return $this->update($input);
}

public static final function getItemField($itemtype): string {
if (isset(static::$items_id) && getItemtypeForForeignKeyField(static::$items_id) == $itemtype) {
return static::$items_id;
}

if (isset (static::$itemtype) && preg_match('/^itemtype/', static::$itemtype)) {
return static::$items_id;
}

throw new \RuntimeException('Cannot guess ');
}
}
19 changes: 10 additions & 9 deletions inc/commondbconnexity.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,18 @@ static function getItemsAssociatedTo($itemtype, $items_id) {
*/
static function getItemsAssociationRequest($itemtype, $items_id) {
global $DB;

return $DB->request([
'SELECT' => 'id',
'FROM' => static::getTable(),
'WHERE' => [
'itemtype' => $itemtype,
'items_id' => $items_id
]
]);
return $DB->request(static::getSQLCriteriaToSearchForItem($itemtype, $items_id));
}

/**
* Get item field name for cloning
*
* @param string $itemtype Item type
*
* @return string
*/
abstract public static function getItemField($itemtype): string;

/**
* get associated item (defined by $itemtype and $items_id)
*
Expand Down
22 changes: 22 additions & 0 deletions inc/commondbrelation.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1830,4 +1830,26 @@ static function countForMainItem(CommonDBTM $item, $extra_types_where = []) {
}
return $nb;
}

public static final function getItemField($itemtype): string {
if (isset(static::$items_id_1) && getItemtypeForForeignKeyField(static::$items_id_1) == $itemtype) {
return static::$items_id_1;
}
if (isset(static::$items_id_2) && getItemtypeForForeignKeyField(static::$items_id_2) == $itemtype) {
return static::$items_id_2;
}

if (isset(static::$itemtype_1) && isset(static::$itemtype_2) && preg_match('/^itemtype/', static::$itemtype_1) && preg_match('/^itemtype/', static::$itemtype_2)) {
throw new \RuntimeException('Bad relation (' . $itemtype . ', ' .static::class . ', ' . static::$itemtype_1 . ', ' . static::$itemtype_2 . ')');
}

if (isset(static::$itemtype_1) && preg_match('/^itemtype/', static::$itemtype_1)) {
return static::$items_id_1;
}
if (isset(static::$itemtype_2) && preg_match('/^itemtype/', static::$itemtype_2)) {
return static::$items_id_2;
}

throw new \RuntimeException('Cannot guess ');
}
}
4 changes: 0 additions & 4 deletions inc/contract.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1795,8 +1795,4 @@ static function getMassiveActionsForItemtype(array &$actions, $itemtype, $is_del
static function getIcon() {
return "fas fa-file-signature";
}

public function getOverrideFieldName(): string {
return 'contracts_id';
}
}
29 changes: 0 additions & 29 deletions inc/contract_item.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,33 +696,4 @@ static function getRelationMassiveActionsSpecificities() {

return $specificities;
}

/**
* get the request results to get items associated to the given one (defined by $itemtype and $items_id)
*
* @param string $itemtype the type of the item we want the resulting items to be associated to
* @param string $items_id the name of the item we want the resulting items to be associated to
*
* @return array the items associated to the given one (empty if none was found)
**/
static function getItemsAssociationRequest($itemtype, $items_id) {
global $DB;
$where = [];
if ($itemtype == 'Contract') {
$where = [
'contracts_id' => $items_id
];
} else {
$where = [
'itemtype' => $itemtype,
'items_id' => $items_id
];
}

return $DB->request([
'SELECT' => 'id',
'FROM' => static::getTable(),
'WHERE' => $where
]);
}
}
20 changes: 0 additions & 20 deletions inc/contract_supplier.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,24 +343,4 @@ static function showForContract(Contract $contract) {
}
echo "</div>";
}

/**
* get the request results to get items associated to the given one (defined by $itemtype and $items_id)
*
* @param string $itemtype the type of the item we want the resulting items to be associated to
* @param string $items_id the name of the item we want the resulting items to be associated to
*
* @return array the items associated to the given one (empty if none was found)
**/
static function getItemsAssociationRequest($itemtype, $items_id) {
global $DB;

return $DB->request([
'SELECT' => 'id',
'FROM' => static::getTable(),
'WHERE' => [
static::$items_id_1 => $items_id
]
]);
}
}
19 changes: 0 additions & 19 deletions inc/contractcost.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,25 +179,6 @@ function rawSearchOptions() {
return $tab;
}

/**
* get the request results to get items associated to the given one (defined by $itemtype and $items_id)
*
* @param string $itemtype the type of the item we want the resulting items to be associated to
* @param string $items_id the name of the item we want the resulting items to be associated to
*
* @return array the items associated to the given one (empty if none was found)
**/
static function getItemsAssociationRequest($itemtype, $items_id) {
global $DB;

return $DB->request([
'SELECT' => 'id',
'FROM' => static::getTable(),
'WHERE' => [
static::$items_id => $items_id
]
]);
}

/**
* Duplicate all costs from a contract template to its clone
Expand Down
11 changes: 1 addition & 10 deletions inc/features/clonable.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,10 @@ trait Clonable {
*/
abstract public function getCloneRelations() :array;

/**
* Get override field name
*
* @return string
*/
public function getOverrideFieldName(): string {
return 'items_id';
}

public function post_clone($source, $history) {
parent::post_clone($source, $history);

$clone_relations = $this->getCloneRelations();
$override_input[$this->getOverrideFieldName()] = $this->getID();
foreach ($clone_relations as $classname) {
if (!is_a($classname, CommonDBConnexity::class, true)) {
Toolbox::logWarning(
Expand All @@ -75,6 +65,7 @@ public function post_clone($source, $history) {
continue;
}

$override_input[$classname::getItemField($this->getType())] = $this->getID();
$relation_items = $classname::getItemsAssociatedTo($this->getType(), $source->getID());
foreach ($relation_items as $relation_item) {
$relation_item->clone($override_input, $history);
Expand Down
20 changes: 0 additions & 20 deletions inc/projectcost.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,26 +190,6 @@ function rawSearchOptions() {
return $tab;
}

/**
* get the request results to get items associated to the given one (defined by $itemtype and $items_id)
*
* @param string $itemtype the type of the item we want the resulting items to be associated to
* @param string $items_id the name of the item we want the resulting items to be associated to
*
* @return array the items associated to the given one (empty if none was found)
**/
static function getItemsAssociationRequest($itemtype, $items_id) {
global $DB;

return $DB->request([
'SELECT' => 'id',
'FROM' => static::getTable(),
'WHERE' => [
static::$items_id => $items_id
]
]);
}


/**
* Duplicate all costs from a project template to its clone
Expand Down
25 changes: 6 additions & 19 deletions inc/projecttask.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,25 +155,6 @@ function cleanDBonPurge() {
parent::cleanDBonPurge();
}

/**
* get the request results to get items associated to the given one (defined by $itemtype and $items_id)
*
* @param string $itemtype the type of the item we want the resulting items to be associated to
* @param string $items_id the name of the item we want the resulting items to be associated to
*
* @return array the items associated to the given one (empty if none was found)
**/
static function getItemsAssociationRequest($itemtype, $items_id) {
global $DB;

return $DB->request([
'SELECT' => 'id',
'FROM' => static::getTable(),
'WHERE' => [
static::$items_id => $items_id
]
]);
}

/**
* Duplicate all tasks from a project template to his clone
Expand Down Expand Up @@ -2075,4 +2056,10 @@ public function getInputFromVCalendar(VCalendar $vcalendar) {

return $input;
}


public function prepareInputForClone($input) {
$input['uuid'] = \Ramsey\Uuid\Uuid::uuid4();
return parent::prepareInputForClone($input);
}
}
Loading

0 comments on commit 8fe965c

Please sign in to comment.