Skip to content

Commit

Permalink
#6 Group project notifications (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
pato1 committed Jul 20, 2020
1 parent d6d4ad1 commit 4176703
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 111 deletions.
141 changes: 50 additions & 91 deletions app/Http/Controllers/EIAController.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,123 +85,80 @@ public function storeForm(FormBuilder $formBuilder, Request $request)

$existingwatcher = \App\Watcher::where('email', $watcher->email)->where('search', $watcher->search)->first();

$notifycount = 0;
// create new watcher only if it already does not exist
if (!isset($existingwatcher)) {
$watcher->save();

$projects = \App\Project::with('regions')->with('districts')->with('localities')->with('companies.company')->with('documents')->where('updated_at', '>=', \Carbon\Carbon::now()->subDays(7))->get();
$notifications_queue = [];
foreach ($projects as $project) {
$notify = 0;
$project->url = str_replace('/print', '', $project->url); // remove /print from URL
$project->url = str_replace('.sk/eia/', '.sk/sk/eia/', $project->url); // add /sk/ to URL
foreach ($project->regions->pluck('name')->toArray() as $region) {
if (stripos($region, $watcher->search) !== false) {
$notify = 1;
}
}
foreach ($project->districts->pluck('name')->toArray() as $district) {
if (stripos($district, $watcher->search) !== false) {
$notify = 1;
}
}
foreach ($project->localities->pluck('name')->toArray() as $locality) {
if (stripos($locality, $watcher->search) !== false) {
$notify = 1;
}
}
if ($notify) {

$project_locations = array_merge(
$project->regions->pluck('name')->toArray(),
$project->districts->pluck('name')->toArray(),
$project->localities->pluck('name')->toArray()
);

foreach ($project_locations as $project_location) {
if (stripos($project_location, $watcher->search) === false) continue;

$hash = sha1($watcher->id . $watcher->search . $watcher->created_at);
$project->setAttribute('unsubscribelinkloc', route('unsubscribe', [$watcher->email, $hash, $watcher->id]));
Mail::to($watcher->email)->send(new ProjectNotification($project));
Log::info('Notifying ' . $watcher->email . ': ' . $project->name);
$notifycount++;
$notifications_queue[] = $project;
break;
}
}
}

$notifications = collect($notifications_queue);
$message = 'Budeme vám zasielať upozornenia na ' . $watcher->email . ' pre lokalitu: ' . $watcher->search . '.';
if ($notifycount > 0) {
$message .= ' Tiež sme vám poslali upozornenia na ' . $notifycount;
if ($notifycount == 1) {
$message .= ' projekt';
}

if ($notifycount >= 2 and $notifycount <= 4) {
$message .= ' projekty';
}

if ($notifycount >= 5) {
$message .= ' projektov';
}
if ($notifications->count() > 0) {
Mail::to($watcher->email)->send(new ProjectNotification($notifications));
Log::info('Notifying ' . $watcher->email . ': ' . $notifications->map(function ($project) { return $project->name; })->implode(', '));

$message .= ' Tiež sme vám poslali upozornenia na ' . $notifications->count();
$message .= ProjectNotification::formatPlural($notifications->count());
$message .= ' EIA, ktoré boli pridané za posledných 7 dní a vyhovujú vašej požiadavke.';
}

return redirect()->route('index')->with('message', $message);
}

public function sendNotifications($project_id)
private function sendNotifications($project_ids)
{
$project = \App\Project::with('regions')->with('districts')->with('localities')->with('companies.company')->with('documents')->find($project_id);
$watchers = \App\Watcher::get();
$notified_emails = [];
$project->url = str_replace('/print', '', $project->url); // remove /print from URL
$project->url = str_replace('.sk/eia/', '.sk/sk/eia/', $project->url); // add /sk/ to URL
foreach ($watchers as $watcher) {
$notify = 0;
foreach ($project->regions->pluck('name')->toArray() as $region) {
if (stripos($region, $watcher->search) !== false) {
$notify = 1;
}
}
foreach ($project->districts->pluck('name')->toArray() as $district) {
if (stripos($district, $watcher->search) !== false) {
$notify = 1;
}
}
foreach ($project->localities->pluck('name')->toArray() as $locality) {
if (stripos($locality, $watcher->search) !== false) {
$notify = 1;
$notifications_queue = [];
foreach ($project_ids as $project_id) {
$project = \App\Project::with('regions')->with('districts')->with('localities')->with('companies.company')->with('documents')->find($project_id);
$project->url = str_replace('/print', '', $project->url);
$project->url = str_replace('.sk/eia/', '.sk/sk/eia/', $project->url);

$project_locations = array_merge(
$project->regions->pluck('name')->toArray(),
$project->districts->pluck('name')->toArray(),
$project->localities->pluck('name')->toArray()
);

foreach ($watchers as $watcher) {
foreach ($project_locations as $project_location) {
if (stripos($project_location, $watcher->search) === false) continue;
if (isset($notifications_queue[$watcher->email][$project->id])) continue; // Only notify once for project per email
$hash = sha1($watcher->id . $watcher->search . $watcher->created_at);
$project->setAttribute('unsubscribelinkloc', route('unsubscribe', [$watcher->email, $hash, $watcher->id]));
$notifications_queue[$watcher->email][$project->id] = $project;
break; // Stop searching localities as watcher was already fulfilled
}
}
if ($notify and in_array($watcher->email, $notified_emails) === false) {
$hash = sha1($watcher->id . $watcher->search . $watcher->created_at);
$project->setAttribute('unsubscribelinkloc', route('unsubscribe', [$watcher->email, $hash, $watcher->id]));
Mail::to($watcher->email)->send(new ProjectNotification($project));
Log::info('Notifying ' . $watcher->email . ': ' . $project->name);
$notified_emails[] = $watcher->email;
}
}

foreach ($notifications_queue as $email => $projects) {
$notifications = collect($projects);
Mail::to($email)->send(new ProjectNotification($notifications));
Log::info('Notifying ' . $email . ': ' . $notifications->map(function ($project) { return $project->name; })->implode(', '));
};
}
/*
public function debugProject(Request $request)
{
while (1) {
$project = \App\Project::with('regions')->with('districts')->with('localities')->with('companies.company')->with('institutions.institution')->with('stakeholders.stakeholder')->with('documents')->find($request->id);
print_r($project);
flush();
}
return dd($project);
}
*/
/*
public function refreshFiles() {
$crawler=Goutte::request('GET', 'http://www.enviroportal.sk/sk/eia/print');
$crawler->filter('tr:not(.head)')->each(function ($line) use (&$i, &$found) {
$url='http://www.enviroportal.sk'.str_replace('/sk/','/',$line->filter('a')->attr('href')).'/print';
$detail=Goutte::request('GET', $url);
$o=0;
$detail->filter('a')->each(function ($node) use (&$i, &$found, &$o) {
if (strpos($node->attr('href'), 'eia/dokument')!==FALSE) {
$found[$i]['doc'][$o]['name']=trim($node->text());
$found[$i]['doc'][$o]['url']=trim($node->attr('href'));
$o++;
}
});
}
}
*/

public function retrieveData($searchparams = '')
{
// search[country]=1
Expand Down Expand Up @@ -473,6 +430,7 @@ public function retrieveData($searchparams = '')

//print_r($found); exit;

$notified_project_ids = [];
// process retrieved data
foreach ($found as $item) {
$project = \App\Project::where('url', $item['url'])->first();
Expand Down Expand Up @@ -655,8 +613,9 @@ public function retrieveData($searchparams = '')
}
}

$this->sendNotifications($project->id);
$notified_project_ids[] = $project->id;
}
$this->sendNotifications($notified_project_ids);
}

private function updateContentTypes()
Expand Down
28 changes: 24 additions & 4 deletions app/Mail/ProjectNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,36 @@
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Collection;

class ProjectNotification extends Mailable
{
use Queueable, SerializesModels;

public static function formatPlural($count)
{
if ($count == 1) {
return ' projekt';
}

if ($count >= 2 and $count <= 4) {
return ' projekty';
}

if ($count >= 5) {
return ' projektov';
}
}


/**
* Create a new message instance.
*
* @return void
* @param Collection $projects
*/
public function __construct($project)
public function __construct(Collection $projects)
{
$this->project = $project;
$this->projects = $projects;
}

/**
Expand All @@ -28,6 +45,9 @@ public function __construct($project)
*/
public function build()
{
return $this->view('email.notification')->subject('EIA: '.$this->project->name)->with('project',$this->project);
$subject = $this->projects->count() === 1
? 'EIA: ' . $this->projects->first()->name
: 'EIA: ' . $this->projects->count() . ' ' . self::formatPlural($this->projects->count());
return $this->view('email.notification')->subject($subject)->with('projects', $this->projects);
}
}
13 changes: 0 additions & 13 deletions resources/views/email/footer.blade.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
@section('footer')
</td>
</tr>
</table>
</td>
</tr>

<!-- END MAIN CONTENT AREA -->
</table>

<!-- START FOOTER -->
<div class="footer">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="content-block">
<p><strong>Tento email dostávate, lebo ste sa prihlásili na odber upozornení na webe <a href="https://eia.cyklokoalicia.sk">Sleduj EIA</a>.</strong></p>
<p><a href="{{ $project->unsubscribelinkloc }}" title="Zrušenie odberu upozornení pre danú lokalitu">Odhlásenie z odberu upozornení pre túto lokalitu</a>.</p>
</td>
</tr>
<tr>
<td class="content-block powered-by">
<small>Službu poskytuje Cyklokoalícia. <strong>Ak chcete, aby sme službu prevádzkovali aj naďalej, podporte nás sumou 10+€ na účet: SK9683300000002700175046 (variabilný symbol 0314 alebo poznámka EIA).</strong></small><br/>
Expand Down
22 changes: 21 additions & 1 deletion resources/views/email/notification.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@include('email.header')
<!-- START CENTERED WHITE CONTAINER -->
@foreach($projects as $project)
<span class="preheader"><strong>Nové EIA: {{ $project->name }}</strong><br /><br />
Okres:
@foreach ($project->districts as $district)
Expand Down Expand Up @@ -77,4 +77,24 @@
</li>
@endforeach
</ul>

</td>
</tr>
</table>
</td>
</tr>

<!-- END MAIN CONTENT AREA -->
</table>
<div class="footer">
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td class="content-block">
<p><strong>Tento email dostávate, lebo ste sa prihlásili na odber upozornení na webe <a href="https://eia.cyklokoalicia.sk">Sleduj EIA</a>.</strong></p>
<p><a href="{{ $project->unsubscribelinkloc }}" title="Zrušenie odberu upozornení pre danú lokalitu">Odhlásenie z odberu upozornení pre túto lokalitu</a>.</p>
</td>
</tr>
</table>
</div>
@endforeach
@include('email.footer')
2 changes: 0 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@

Route::get('cron/get', ['uses' => 'EIAController@get', 'as' => 'get']);
Route::get('cron/update', ['uses' => 'EIAController@updateFiles', 'as' => 'update']);

// Route::get('debug/project/{id}', ['uses' => 'EIAController@debugProject', 'as' => 'debug.project']);

0 comments on commit 4176703

Please sign in to comment.