public function ReservationConflicts::buildConflictQuery in MERCI (Manage Equipment Reservations, Checkout and Inventory) 8.2
Overrides ReservationConflictsInterface::buildConflictQuery
2 calls to ReservationConflicts::buildConflictQuery()
File
- src/
ReservationConflicts.php, line 340 - Contains \Drupal\merci\ReservationConflicts. Abstraction of the selection logic of an entity reference field.
Class
- ReservationConflicts
- A null implementation of EntityReference_SelectionHandler.
Namespace
Drupal\merciCode
public function buildConflictQuery($date, $item = NULL) {
$exclude_id = $this->entity
->id();
$entity_type = $this->entity
->getEntityTypeId();
$items = array();
if ($item) {
$items[] = $item->target_id;
}
else {
foreach ($this->entity
->get($this->item_field) as $delta => $item) {
$items[] = $item->target_id;
}
}
// Build the query.
// Entity type is the entity holding the date and item fields.
$query = \Drupal::entityQuery($entity_type);
if (count($items) == 1) {
$query
->condition($this->item_field, reset($items));
}
else {
$query
->condition($this->item_field, $items, 'IN');
}
// Ignore myself.
if ($exclude_id) {
$entity_type_id_key = $this->entity
->getEntityType()
->getKey('id');
$query
->condition($entity_type_id_key, $exclude_id, '!=');
}
$dates = array(
'value' => $date
->get('value')
->getValue(),
'end_value' => $date
->get('end_value')
->getValue(),
);
// start falls within another reservation.
// |-------------this-------------|
// |-------------conflict-------------------------|
// OR
// |-------------this-------------------------------|
// |-------------conflict-------------------------|
$and1 = $query
->andConditionGroup()
->condition($this->date_field . '.value', $dates['value'], '<=')
->condition($this->date_field . '.end_value', $dates['value'], '>=');
// end falls within another reservation.
// |-------------this-------------------------------|
// |-------------conflict-------------------------|
$and2 = $query
->andConditionGroup()
->condition($this->date_field . '.value', $dates['end_value'], '<=')
->condition($this->date_field . '.end_value', $dates['end_value'], '>=');
// start before another reservation.
// end after another reservation.
// |-------------------------this-------------------------------|
// |----------------conflict------------------|
$and3 = $query
->andConditionGroup()
->condition($this->date_field . '.value', $dates['value'], '>')
->condition($this->date_field . '.end_value', $dates['end_value'], '<');
$or = $query
->orConditionGroup()
->condition($and1)
->condition($and2)
->condition($and3);
$query
->condition($or);
$query
->sort($this->date_field . '.value');
// Add a generic entity access tag to the query.
$query
->addTag('merci_resource');
$query
->addMetaData('merci_reservable_handler', $this);
return $query;
}