Skip to content

Commit c19a831

Browse files
committed
fix(refactor): condense join logic
This incidentally fixed a typo in the join expression for assets.
1 parent 0e039b8 commit c19a831

File tree

1 file changed

+22
-63
lines changed

1 file changed

+22
-63
lines changed

Module.php

Lines changed: 22 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,7 @@ public function filterByTeam(Event $event)
913913
$event->getParam('request')->getOperation() === 'search' &&
914914
$this->getServiceLocator()->get('Omeka\Status')->isSiteRequest()
915915
) {
916+
916917
//get the id for the current site
917918
$site_slug = $this->getServiceLocator()->get('Omeka\Status')->getRouteMatch()->getParam('site-slug');
918919
$site_id = $em->getRepository('Omeka\Entity\Site')->findOneBy(['slug' => $site_slug])->getId();
@@ -961,60 +962,43 @@ public function filterByTeam(Event $event)
961962
return;
962963
}
963964

964-
///If this is a case where someone is adding something and can choose which team to add it to, take that into
965-
/// consideration and add it to that team. Otherwise, conduct the query filtering based on the current team
966-
/// This turned out to be vital to making public facing browse and search work
967-
if (isset($query['team_id'])) {
968-
$team_id = (int) $query['team_id'];
969-
$qb->leftJoin('Teams\Entity\TeamResource', 'tr_ti', Expr\Join::WITH, $alias . '.id = tr_ti.resource')
970-
->andWhere('tr_ti.team = :team_id')
971-
->setParameter('team_id', $team_id)
972-
;
973-
return;
974-
} else {
975-
$team_id = $this->getTeamContext($query, $event);
976-
}
977965

966+
$team_id = $this->getTeamContext($query, $event);
978967
if ($team_id === [0]) {
979968
return;
980969
}
981970
if (is_array($team_id)) {
982971
if ($entityClass == \Omeka\Entity\Site::class) {
983-
$team_alias = 'ts';
984-
if (!$this->getUser()) {
985-
return ;
986-
} else {
987-
//TODO get the team_id's associated with the site and then do an orWhere()/orX()
988-
//Leaving this todo for now, but this should be covered by item-site now
989-
$qb->leftJoin('Teams\Entity\TeamSite', $team_alias, Expr\Join::WITH, "{$alias}.id = {$team_alias}.site")
990-
->andWhere("$team_alias.team = :team_id")
991-
->setParameter('team_id', $team_id[0]);
992-
}
972+
$teamAlias = 'ts';
973+
$joinCol = 'site';
974+
$teamsEntityClass = \Teams\Entity\TeamSite::class;
993975
} elseif ($entityClass == \Omeka\Entity\ResourceTemplate::class) {
994-
$team_alias = 'trt';
995-
$qb->leftJoin('Teams\Entity\TeamResourceTemplate', $team_alias, Expr\Join::WITH, "{$alias}.id = {$team_alias}.resource_template")
996-
->andWhere($team_alias . '.team = :team_id')
997-
->setParameter('team_id', $team_id[0]);
976+
$teamAlias = 'trt';
977+
$joinCol = 'resource_template';
978+
$teamsEntityClass = \Teams\Entity\TeamResourceTemplate::class;
998979
} elseif ($entityClass == \Omeka\Entity\User::class) {
999-
return;
980+
$teamAlias = 'tu';
981+
$joinCol = 'user';
982+
$teamsEntityClass = \Teams\Entity\TeamUser::class;
1000983
} elseif ($entityClass == \Omeka\Entity\Vocabulary::class) {
1001984
return;
1002985
} elseif ($entityClass == \Omeka\Entity\Asset::class) {
1003-
$team_alias = 'ta';
1004-
$qb->leftJoin('Teams\Entity\TeamAsset', $team_alias, Expr\Join::WITH, "{$alias}.id = {$team_alias}.asset'")
1005-
->andWhere("{$team_alias}.team = :team_id")
1006-
->setParameter('team_id', $team_id[0]);
986+
$teamAlias = 'ta';
987+
$joinCol = 'asset';
988+
$teamsEntityClass = \Teams\Entity\TeamAsset::class;
1007989
} else {
1008-
$team_alias = 'tr';
1009-
$qb->leftJoin('Teams\Entity\TeamResource', $team_alias, Expr\Join::WITH, "{$alias}.id = {$team_alias}.resource")
1010-
->andWhere("{$team_alias}.team = :team_id")
1011-
->setParameter('team_id', $team_id[0]);
990+
$teamAlias = 'tr';
991+
$teamsEntityClass = \Teams\Entity\TeamResource::class;
992+
$joinCol = 'resource';
1012993
}
994+
$qb->leftJoin($teamsEntityClass, $teamAlias, Expr\Join::WITH, "{$alias}.id = {$teamAlias}.{$joinCol}")
995+
->andWhere($teamAlias . '.team = :team_id')
996+
->setParameter('team_id', $team_id[0]);
1013997
if (count($team_id) > 1) {
1014998
$orX = $qb->expr()->orX();
1015999
$i = 0;
10161000
foreach ($team_id as $value) {
1017-
$orX->add($qb->expr()->eq("{$team_alias}.team", ':name' . $i));
1001+
$orX->add($qb->expr()->eq("{$teamAlias}.team", ':name' . $i));
10181002
$qb->setParameter('name' . $i, $value);
10191003
$i++;
10201004
}
@@ -1039,25 +1023,6 @@ public function getOrphans(Event $event)
10391023
}
10401024
}
10411025

1042-
//Handle Users
1043-
public function filterByTeamUser(Event $event)
1044-
{
1045-
$query = $event->getParam('request')->getContent();
1046-
if (isset($query['bypass_team_filter']) && $query['bypass_team_filter']) {
1047-
return;
1048-
}
1049-
$qb = $event->getParam('queryBuilder');
1050-
$alias = 'omeka_root';
1051-
if (array_key_exists('team_id', $query) && is_int($query['team_id'])) {
1052-
$team = $query['team_id'];
1053-
} else {
1054-
$team = $this->getTeamContext($query, $event);
1055-
}
1056-
$qb->leftJoin('Teams\Entity\TeamUser', 'tu', Expr\Join::WITH, $alias . '.id = tu.user')
1057-
->andWhere('tu.team = :team_id')
1058-
->setParameter('team_id', $team);
1059-
}
1060-
10611026
/**
10621027
* Adds user's teams to the user view page
10631028
*
@@ -2639,14 +2604,14 @@ public function attachListeners(SharedEventManagerInterface $sharedEventManager)
26392604
SiteAdapter::class,
26402605
ResourceTemplateAdapter::class,
26412606
AssetAdapter::class,
2607+
UserAdapter::class,
26422608

26432609
];
26442610
foreach ($adapters as $adapter):
26452611

26462612
// Add the group filter to the search.
26472613
$sharedEventManager->attach(
26482614
$adapter,
2649-
// '*',
26502615
'api.search.query',
26512616
[$this, 'filterByTeam']
26522617
);
@@ -2659,12 +2624,6 @@ public function attachListeners(SharedEventManagerInterface $sharedEventManager)
26592624
[$this, 'getOrphans']
26602625
);
26612626

2662-
$sharedEventManager->attach(
2663-
UserAdapter::class,
2664-
'api.search.query',
2665-
[$this, 'filterByTeamUser']
2666-
);
2667-
26682627
$sharedEventManager->attach(
26692628
'*',
26702629
'api.find.post',

0 commit comments

Comments
 (0)