Skip to content

Commit 662826a

Browse files
author
Jose Ganora
committed
Merge branch 'develop' into develop-IFRC
2 parents e1cd4f3 + a07ce4d commit 662826a

File tree

9 files changed

+111
-20
lines changed

9 files changed

+111
-20
lines changed

app/Classes/Feeds/WhatNowFeed.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Classes\Serializers\CustomDataSerializer;
1010
use App\Classes\Transformers\WhatNowEntityTransformer;
1111
use App\Models\Organisation;
12+
use App\Models\Region;
1213
use App\Models\WhatNowEntity;
1314

1415
class WhatNowFeed implements JsonFeedInterface
@@ -18,6 +19,11 @@ class WhatNowFeed implements JsonFeedInterface
1819
*/
1920
protected $language;
2021

22+
/**
23+
* @var Region
24+
*/
25+
protected $region;
26+
2127
/**
2228
* @var Organisation
2329
*/
@@ -85,14 +91,23 @@ public function setOrganisation(Organisation $org)
8591
}
8692

8793
/**
88-
* @param string $lang
94+
* @param null $region
8995
* @return $this
9096
*/
91-
public function setLanguage($lang = 'en_US')
97+
public function setRegion(Region $region = null)
9298
{
93-
// @todo validate locale
94-
$this->language = $lang;
99+
$this->region = $region;
100+
return $this;
101+
}
95102

103+
/**
104+
* @param string $lang
105+
* @return $this
106+
*/
107+
public function setLanguage($lang = null)
108+
{
109+
$this->language = $lang ? substr($lang, 0, 2) : null;
110+
$this->transformer->setLang($this->language);
96111
return $this;
97112
}
98113

@@ -118,7 +133,8 @@ public function loadData()
118133
$data = $this->whatNowRepo->findItemsForOrgId(
119134
$this->organisation->id,
120135
$this->language,
121-
$this->filterEventTypes
136+
$this->filterEventTypes,
137+
$this->region->id ?? null
122138
);
123139

124140
if ($data instanceof Collection) {

app/Classes/Repositories/RegionRepository.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
<?php
2-
32
namespace App\Classes\Repositories;
43

4+
use App\Models\Region;
5+
6+
57
class RegionRepository implements RegionRepositoryInterface
68
{
9+
/**
10+
* @var Region
11+
*/
12+
protected $regModel;
13+
14+
/**
15+
* @param Region $regionModel
16+
*/
17+
public function __construct(Region $regModel)
18+
{
19+
$this->regModel = $regModel;
20+
}
21+
22+
/**
23+
* @param array $attributes
24+
* @return static
25+
*/
26+
public function newInstance(array $attributes = [])
27+
{
28+
return $this->regModel->newInstance($attributes);
29+
}
30+
31+
32+
public function findBySlug($orgId, $slug)
33+
{
34+
$slug = strtolower($slug);
35+
$slug = str_replace(' ', '-', $slug);
36+
return $this->regModel->where('slug', $slug)->where('organisation_id', $orgId)->first();
37+
}
738

839
public function mapTranslationInput($regionId, $language_code, $data)
940
{

app/Classes/Repositories/RegionRepositoryInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,10 @@
44

55
interface RegionRepositoryInterface
66
{
7+
/**
8+
* @param $regionName
9+
* @return mixed
10+
*/
11+
public function findBySlug($orgId, $slug);
712

813
}

app/Classes/Repositories/WhatNowRepository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,18 @@ public function destroy($id)
148148
* @param null $lang
149149
* @param array $eventTypes
150150
*/
151-
public function findItemsForOrgId($orgId, $lang = null, array $eventTypes = [])
151+
public function findItemsForOrgId($orgId, $lang = null, array $eventTypes = [], $regId = null)
152152
{
153153
$query = $this->whatNowModel->where('org_id', $orgId);
154154

155155
if (count($eventTypes)) {
156156
$query->whereIn('event_type', $eventTypes);
157157
}
158158

159+
if ($regId) {
160+
$query->where('region_id', $regId);
161+
}
162+
159163
return $query->get();
160164
}
161165

app/Classes/Repositories/WhatNowRepositoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface WhatNowRepositoryInterface extends RepositoryInterface
99
* @param null $lang
1010
* @param array $eventTypes
1111
*/
12-
public function findItemsForOrgId($orgId, $lang = null, array $eventTypes = []);
12+
public function findItemsForOrgId($orgId, $lang = null, array $eventTypes = [], $regionName = null);
1313

1414
/**
1515
* @param $orgId

app/Classes/Repositories/WhatNowTranslationRepository.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,23 @@ public function getLatestTranslations($id)
132132
* @param $id
133133
* @return \Illuminate\Database\Eloquent\Collection
134134
*/
135-
public function getLatestPublishedTranslations($id)
135+
public function getLatestPublishedTranslations($id, $lang = null)
136136
{
137137
$model = $this->whatNowTranslationModel;
138138

139-
return $model::fromQuery(DB::raw('SELECT wet1.*
140-
FROM whatnow_entity_translations wet1
141-
LEFT JOIN whatnow_entity_translations wet2 ON wet1.entity_id = wet2.entity_id AND wet1.language_code = wet2.language_code AND wet1.published_at < wet2.published_at
142-
WHERE wet2.published_at IS NULL AND wet1.entity_id = :entityId AND wet1.published_at IS NOT NULL'), ['entityId' => $id]);
139+
$query = 'SELECT wet1.*
140+
FROM whatnow_entity_translations wet1
141+
LEFT JOIN whatnow_entity_translations wet2 ON wet1.entity_id = wet2.entity_id AND wet1.language_code = wet2.language_code AND wet1.published_at < wet2.published_at
142+
WHERE wet2.published_at IS NULL AND wet1.entity_id = :entityId AND wet1.published_at IS NOT NULL';
143+
144+
$bindings = ['entityId' => $id];
145+
146+
if ($lang !== null) {
147+
$query .= ' AND wet1.language_code = :lang';
148+
$bindings['lang'] = $lang;
149+
}
150+
151+
return $model::fromQuery(DB::raw($query), $bindings);
143152
}
144153

145154
/**

app/Classes/Repositories/WhatNowTranslationRepositoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function getLatestTranslations($id);
2323
* @param $id
2424
* @return \Illuminate\Database\Eloquent\Collection
2525
*/
26-
public function getLatestPublishedTranslations($id);
26+
public function getLatestPublishedTranslations($id, $lang = null);
2727

2828
/**
2929
* @param array $ids

app/Classes/Transformers/WhatNowEntityTransformer.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class WhatNowEntityTransformer extends TransformerAbstract
1414
* @var bool
1515
*/
1616
private $unpublished = false;
17+
private $lang = null;
1718

1819
/**
1920
* @var bool
@@ -37,9 +38,18 @@ function __construct(WhatNowTranslationRepositoryInterface $repo, $configuration
3738
$this->castDateToBoolean = $configuration['castDateToBoolean'];
3839
}
3940

41+
if(isset($configuration['lang'])) {
42+
$this->lang = $configuration['lang'];
43+
}
44+
4045
$this->wnTransRepo = $repo;
4146
}
4247

48+
public function setLang($lang)
49+
{
50+
$this->lang = $lang;
51+
}
52+
4353
/**
4454
* Turn this item object into a generic array
4555
*
@@ -80,7 +90,7 @@ public function transform(WhatNowEntity $model)
8090
if ($this->unpublished) {
8191
$translations = $this->wnTransRepo->getLatestTranslations($model->id);
8292
} else {
83-
$translations = $this->wnTransRepo->getLatestPublishedTranslations($model->id);
93+
$translations = $this->wnTransRepo->getLatestPublishedTranslations($model->id, $this->lang);
8494
}
8595

8696
$defaultStages = [];

app/Http/Controllers/WhatNowController.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Classes\Feeds\WhatNowFeed;
66
use App\Classes\Repositories\OrganisationRepositoryInterface;
7+
use App\Classes\Repositories\RegionRepositoryInterface;
78
use App\Classes\Repositories\WhatNowRepositoryInterface;
89
use App\Classes\Repositories\WhatNowTranslationRepositoryInterface;
910
use App\Classes\Serializers\CustomDataSerializer;
@@ -27,6 +28,11 @@ class WhatNowController extends Controller
2728
*/
2829
protected $orgRepo;
2930

31+
/**
32+
* @var RegionRepositoryInterface
33+
*/
34+
protected $regionRepo;
35+
3036
/**
3137
* @var WhatNowRepositoryInterface
3238
*/
@@ -58,12 +64,14 @@ class WhatNowController extends Controller
5864
*/
5965
public function __construct(
6066
OrganisationRepositoryInterface $orgRepo,
67+
RegionRepositoryInterface $regionRepo,
6168
WhatNowRepositoryInterface $wnRepo,
6269
WhatNowTranslationRepositoryInterface $wnTransRepo,
6370
Request $request,
6471
Manager $manager
6572
) {
6673
$this->orgRepo = $orgRepo;
74+
$this->regionRepo = $regionRepo;
6775
$this->wnRepo = $wnRepo;
6876
$this->wnTransRepo = $wnTransRepo;
6977
$this->request = $request;
@@ -177,18 +185,26 @@ public function getFeed(WhatNowFeed $feed, $code)
177185

178186
return response(null, 404);
179187
}
180-
181188
$feed->setOrganisation($org);
182189

183-
// @todo lang filter
184-
/*$langParam = $this->request->query('language', null);
190+
$regName = $this->request->query('region', null);
191+
if ($regName) {
192+
try {
193+
$reg = $this->regionRepo->findBySlug($org->id, $regName);
194+
$feed->setRegion($reg);
195+
} catch (\Exception $e) {
196+
Log::error('Region not found', ['message' => $e->getMessage()]);
197+
}
198+
}
199+
200+
$langParam = $this->request->query('language', null);
185201
$langHeader = $this->request->header('Accept-Language', null);
186202

187203
if ($langParam) {
188-
$feed->setLanguage($this->request->query('language'));
204+
$feed->setLanguage($langParam);
189205
} elseif ($langHeader) {
190206
$feed->setLanguage(locale_accept_from_http($langHeader));
191-
}*/
207+
}
192208

193209
$feed->setEventTypeFilter($this->request->query('eventType', null));
194210
$feed->loadData();

0 commit comments

Comments
 (0)