2024-07-16 09:51:47 +00:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
SPDX-FileCopyrightText: © 2024 Millions Missing FRANCE <info@millionsmissing.fr>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Modules\MMFCustomersGroups\Providers;
|
|
|
|
|
2024-07-18 14:18:10 +00:00
|
|
|
use Eventy;
|
|
|
|
use View;
|
|
|
|
|
2024-07-18 16:22:16 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2024-07-16 10:06:59 +00:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
|
2024-07-18 16:22:16 +00:00
|
|
|
use Modules\MMFCustomersGroups\Entities\CustomersGroup;
|
2024-07-18 14:18:10 +00:00
|
|
|
use Modules\MMFCustomersGroups\Entities\Mailbox;
|
|
|
|
|
2024-07-16 09:51:47 +00:00
|
|
|
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();
|
2024-07-16 14:54:04 +00:00
|
|
|
$this->registerViews();
|
2024-07-27 16:27:10 +00:00
|
|
|
$this->registerTranslations();
|
2024-07-16 10:04:08 +00:00
|
|
|
$this->loadMigrationsFrom(__DIR__ . '/../Database/Migrations');
|
2024-07-16 14:54:04 +00:00
|
|
|
$this->hooks();
|
2024-07-16 09:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 [];
|
|
|
|
}
|
2024-07-16 14:54:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
|
|
|
|
2024-07-27 16:27:10 +00:00
|
|
|
/**
|
|
|
|
* Register translations.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function registerTranslations() {
|
|
|
|
$this->loadTranslationsFrom(__DIR__ .'/../Resources/lang', 'mmfcustomersgroups');
|
|
|
|
}
|
|
|
|
|
2024-07-16 14:54:04 +00:00
|
|
|
/**
|
|
|
|
* Module hooks.
|
|
|
|
*/
|
|
|
|
public function hooks() {
|
|
|
|
// Add a menu entry to manage the customer groups.
|
2024-07-18 14:18:10 +00:00
|
|
|
Eventy::addAction('menu.manage.after_mailboxes', function() {
|
|
|
|
echo View::make('mmfcustomersgroups::partials/menu', [])->render();
|
2024-07-16 14:54:04 +00:00
|
|
|
});
|
2024-07-18 14:18:10 +00:00
|
|
|
|
|
|
|
// 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;
|
2024-07-30 09:55:22 +00:00
|
|
|
// Do not add the groups selection space if no group is set.
|
|
|
|
if ( $groups->isEmpty() ) return;
|
2024-07-18 14:18:10 +00:00
|
|
|
echo View::make(
|
|
|
|
'mmfcustomersgroups::conversations/partials/groups-selection',
|
|
|
|
[
|
|
|
|
'groups' => $groups,
|
|
|
|
],
|
|
|
|
)->render();
|
|
|
|
}, 20, 3);
|
2024-07-18 16:22:16 +00:00
|
|
|
|
|
|
|
// Update the list of recipients if some groups have been selected.
|
|
|
|
Eventy::addAction('conversation.send_reply_save', function($conversation, $request) {
|
2024-07-27 16:20:12 +00:00
|
|
|
$groups_cc = $request->groups_cc;
|
|
|
|
$groups_bcc = $request->groups_bcc;
|
2024-07-18 16:22:16 +00:00
|
|
|
// Return early if no group has been selected.
|
2024-07-27 16:20:12 +00:00
|
|
|
if ( empty($groups_cc) && empty($groups_bcc) ) return;
|
|
|
|
|
|
|
|
// Cc - Get the list of e-mails included in the selected groups.
|
|
|
|
if ( !empty($groups_cc) ) {
|
|
|
|
$emails = new Collection;
|
|
|
|
foreach ( $groups_cc 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bcc - Get the list of e-mails included in the selected groups.
|
|
|
|
if ( !empty($groups_bcc) ) {
|
|
|
|
$emails = new Collection;
|
|
|
|
foreach ( $groups_bcc as $group_id ) {
|
|
|
|
$group = CustomersGroup::find($group_id);
|
|
|
|
$emails = $emails->concat($group->emails());
|
|
|
|
}
|
|
|
|
$emails = $emails->unique('email')->pluck('email');
|
|
|
|
// Update the list of Bcc emails.
|
|
|
|
$bcc = array_unique(array_merge(
|
|
|
|
$conversation->getBccArray(),
|
|
|
|
$emails->toArray()
|
|
|
|
));
|
|
|
|
$conversation->setBcc($bcc);
|
2024-07-18 16:22:16 +00:00
|
|
|
}
|
2024-07-27 16:20:12 +00:00
|
|
|
|
|
|
|
// TODO: Check if this explicit save() call is required.
|
2024-07-19 16:52:11 +00:00
|
|
|
$conversation->save();
|
2024-07-18 16:22:16 +00:00
|
|
|
}, 20, 2);
|
2024-07-16 14:54:04 +00:00
|
|
|
}
|
2024-07-16 09:51:47 +00:00
|
|
|
}
|