freescout-customers-groups/Providers/MMFCustomersGroupsServiceProvider.php

124 lines
3.2 KiB
PHP

<?php
/*
SPDX-License-Identifier: AGPL-3.0-only
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
*/
namespace Modules\MMFCustomersGroups\Providers;
use Eventy;
use View;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\ServiceProvider;
use Modules\MMFCustomersGroups\Entities\CustomersGroup;
use Modules\MMFCustomersGroups\Entities\Mailbox;
class MMFCustomersGroupsServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Boot the application events.
*
* @return void
*/
public function boot() {
$this->registerConfig();
$this->registerViews();
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
$this->hooks();
}
/**
* Register config.
*
* @return void
*/
protected function registerConfig() {
$this->publishes([
__DIR__.'/../Config/config.php' => config_path('mmfcustomersgroups.php'),
], 'config');
$this->mergeConfigFrom(
__DIR__.'/../Config/config.php', 'mmfcustomersgroups'
);
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides() {
return [];
}
/**
* Register views.
*
* @return void
*/
public function registerViews() {
$viewPath = resource_path('views/modules/mmfcustomersgroups');
$sourcePath = __DIR__.'/../Resources/views';
$this->publishes([
$sourcePath => $viewPath,
],'views');
$this->loadViewsFrom(array_merge(array_map(function ($path) {
return $path . '/modules/mmfcustomersgroups';
}, \Config::get('view.paths')), [$sourcePath]), 'mmfcustomersgroups');
}
/**
* Module hooks.
*/
public function hooks() {
// Add a menu entry to manage the customer groups.
Eventy::addAction('menu.manage.after_mailboxes', function() {
echo View::make('mmfcustomersgroups::partials/menu', [])->render();
});
// Add a list of customer groups to select from when writing a new conversation.
Eventy::addAction('conversation.create_form.before_subject', function($conversation, $mailbox, $thread) {
// We must fetch a fresh Mailbox instance,
// as the one that has been passed does not have a "groups" relationship.
$mailbox = Mailbox::find($mailbox->id);
$groups = $mailbox->groups;
echo View::make(
'mmfcustomersgroups::conversations/partials/groups-selection',
[
'groups' => $groups,
],
)->render();
}, 20, 3);
// Update the list of recipients if some groups have been selected.
Eventy::addAction('conversation.send_reply_save', function($conversation, $request) {
$groups = $request->groups;
// Return early if no group has been selected.
if ( empty($groups) ) return;
// Get the list of e-mails included in the selected groups.
$emails = new Collection;
foreach ( $groups as $group_id ) {
$group = CustomersGroup::find($group_id);
$emails = $emails->concat($group->emails());
}
$emails = $emails->unique('email')->pluck('email');
// Update the list of cc emails.
$cc = array_unique(array_merge(
$conversation->getCcArray(),
$emails->toArray()
));
$conversation->setCc($cc);
$conversation->save();
}, 20, 2);
}
}