namespace App;
use App\Mail\BatchMail;
+use Arr;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use \Illuminate\Database\Eloquent\Relations;
{
protected $guarded = [];
protected $softDeletes = false;
+
+
use BelongsToPdfFile;
'sent_to'
];
+ /**
+ * @param $data
+ * @return EmailBatch
+ */
+ public static function createFromRequest($data): EmailBatch
+ {
+ $batch = new self;
+
+ $recipientSlug = Arr::get($data, 'recipient_group.slug');
+ $prefix = $recipientSlug === 'admin' ?
+ '** TEST ** ' :
+ '';
+
+ $batch->subject = $prefix . Arr::get($data, 'email.subject', '');
+ $batch->content = [
+ 'body' => Arr::get($data, 'email.content'),
+ 'link' => Arr::get($data, 'email.link'),
+ 'html' => Arr::get($data, 'email.html'),
+ 'type' => Arr::get($data, 'email.type', 0) //0: newsletter, 1: PDF
+
+ ];
+ $batch->file_id = Arr::get($data, 'file.id');
+ $batch->sent_to = [];
+
+ $batch->save();
+
+
+ return $batch;
+
+ }
+
+ /**
+ * @param $data
+ * @return EmailBatch|null
+ */
+ public static function createAttachmentMailFromRequest($data): ?EmailBatch
+ {
+ $fileId = Arr::get($data, 'file.id');
+ /** @var PdfFile $file */
+
+ if($fileId === null) {
+ return null;
+ }
+
+ $file = PdfFile::find($fileId);
+
+ if(!$file || ! $file->collection->sends_pdf) {
+ return null;
+ }
+
+ $batch = new self;
+ $recipientSlug = Arr::get($data, 'recipient_group.slug');
+ $prefix = $recipientSlug === 'admin' ?
+ '** TEST ** [Version PDF]' :
+ '[Version PDF] ';
+
+ $batch->subject = $prefix . Arr::get($data, 'email.subject', '');
+ $batch->file_id = $fileId;
+
+ $batch->content = [
+ 'body' => "Bonjour, ci-joint la version PDF de {$file->title}",
+ 'type' => 0
+
+ ];
+ $batch->sent_to = [];
+
+ $batch->save();
+
+ return $batch;
+
+ }
+
/**
* @return Relations\HasMany
*/
$this->content['body'],
$this->subject,
$this->content['link'] ?? null,
- optional($this->file)->coverUrl
+ optional($this->file)->coverUrl,
+ $this->getView()
))->render();
}
+ /**
+ * @return int
+ */
+ public function getView()
+ {
+ if($this->content['type'] == 1) {
+ return BatchMail::MAIL_TYPE_PDF_FILE;
+ }
+ return BatchMail::MAIL_TYPE_NEWSLETTER;
+ }
/**
* @return string
*/
* @property string $slug
* @property string $name
* @property string $short_name
+ * @property bool $sends_pdf
* @method static FileCollection findOrFail($id) FileCollection
*/
class FileCollection extends Model
public $timestamps = false;
+ protected $guarded = [];
+
public function files()
{
return $this->hasMany(PdfFile::class, 'collection_id');
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
+use PharIo\Manifest\Email;
class PublishController extends Controller
*/
public function publish(Request $request)
{
- $batch = new EmailBatch();
$data = $request->input('data');
- //0: newsletter, 1: PDF
- $type = Arr::get($data, 'email.type', 0);
- $recipientGroup = Arr::get($data, 'recipient_group');
- $recipientSlug = Arr::get($data, 'recipient_group.slug');
- $prefix = $recipientSlug === 'admin' ?
- '** TEST ** ' :
- '';
-
- $batch->subject = $prefix . Arr::get($data, 'email.subject', '');
- $batch->content = [
- 'body' => Arr::get($data, 'email.content'),
- 'link' => Arr::get($data, 'email.link'),
- 'html' => Arr::get($data, 'email.html')
- ];
- $batch->file_id = Arr::get($data, 'file.id');
- $batch->sent_to = [];
- $batch->save();
+ $recipientGroup = Arr::get($data, 'recipient_group');
- $builder = $recipientGroup === null ?
+ /** @var Builder $usersBuilder */
+ $usersBuilder = $recipientGroup === null ?
User::receivesEmails() :
PublishController::sendGroups()[$recipientGroup['slug']]['builder'];
- $processEmailBatch = new ProcessEmailBatch($batch, $builder);
- dispatch($processEmailBatch->withFile());
+ $userReceivesPdfBuilder = clone $usersBuilder;
+
+ $batch = EmailBatch::createFromRequest($data);
+ $processEmailBatch = new ProcessEmailBatch($batch, $usersBuilder);
+ dispatch($processEmailBatch);
+
+ if($pdfBatch = EmailBatch::createAttachmentMailFromRequest($data)) {
+ $userReceivesPdfBuilder->receivesPdf();
+ $pdfProcessEmailBatch = new ProcessEmailBatch($pdfBatch, $userReceivesPdfBuilder);
+ dispatch($pdfProcessEmailBatch->withFile());
+
+ }
return ['data' => $batch->id];
}
$request->input('subject'),
$request->input('link'),
$request->input('image'),
+ BatchMail::MAIL_TYPE_PDF_FILE
))->render();
}
$this->users = $usersBuilder->get();
- $this->html = $this->batch->content['html'];
$this->replaceVariables();
}
- $view = $this->html ?? $this->batch->renderFromContent();
-
+ $view = $this->batch->render();
$params = [
* @param string $subject
* @param string|null $link
* @param null $image
+ * @param int $type
*/
- public function __construct($content, $subject, $link = null, $image = null)
+ public function __construct($content, $subject, $link = null, $image = null, $type = self::MAIL_TYPE_NEWSLETTER)
{
//
$this->subject = $subject;
$this->image = $image;
- $this->emailView = self::getView(self::MAIL_TYPE_PDF_FILE);
+ $this->emailView = self::getView($type);
$this->content = $this->emailView === 'batch-pdf' ?
(Html2Markdown::convert($content)) :
}
}
+
+
/**
* @param $id
* @return string
public function calculate(NovaRequest $request)
{
return $this->result([
- 'Compte activé' => User::registeredUser()->registrationComplete()->count(),
- 'Compte non activé' => User::registeredUser()->registrationIncomplete()->count(),
+ 'Compte activé' => User::subscriber()->registrationComplete()->count(),
+ 'Compte non activé' => User::subscriber()->registrationIncomplete()->count(),
]);
}
*/
public function calculate(NovaRequest $request)
{
- return $this->result(User::registeredUser()->count());
+ return $this->result(User::subscriber()->count());
}
public function calculate(NovaRequest $request)
{
return $this->result([
- 'Abonnés (groupe)' => $g = User::hasOrgSubscription()->count(),
- 'Abonnés (indiv)' => $i = User::hasIndSubscription()->count(),
+ 'Abonnés' => $g = User::subscriber()->count(),
"Période d'essai" => $t = User::isOnTrial()->count(),
"Découverte" => $d = User::isOnDiscovery()->count(),
- "Reste" => User::count() - $i - $g - $t -$d
+ "Reste" => User::count() - $g - $t -$d
]);
}
/** @var PdfFile $pdfFile */
- $pdfFile = self::create([
- 'ref' => $ref,
- 'collection_id' => $collection_id,
+ $pdfFile = self::query()->updateOrCreate([
'slug' => Str::slug("{$collection->slug}_{$ref}"),
+ 'published' => 0,
+ 'collection_id' => $collection_id,
+ ],
+ [
+ 'ref' => $ref,
'title' => "{$collection->short_name} - $ref",
'headlines' => $headlines,
- 'published' => 0,
]);
*/
public function getMailableUrl(User $user): string
{
- if($user->receives_pdf) {
- return $this->getDownloadUrl();
- }
+// if($user->receives_pdf) {
+// return $this->getDownloadUrl();
+// }
return ($user->type === User::TYPE_DISCOVER && !$user->reg_complete) ?
$this->getSignedUrl($user) :
$this->getUrlWithToken($user);
$builder->where('is_free', true);
}
+
+
/**
* @return string
*/
'badge' => 'success',
'label' => 'Abonnement actif (indep)'
],
+ 'subscribed' => [
+ 'badge' => 'success',
+ 'label' => 'Abonnement actif'
+ ],
'trial' => [
'badge' => 'info',
'label' => "Période d'essai"
*/
public function hasValidSubscription(): bool
{
- return $this->orgIsSubscribed() || $this->subscribed();
+ return $this->type === self::TYPE_SUBSCRIBER;
}
/**
/**
* @param Builder $builder
+ * @deprecated
*/
public function scopeHasActiveSubscription(Builder $builder): void
{
- $builder->hasOrgSubscription()->orWhere->hasIndSubscription();
+ $builder->subscriber();
}
/**
/**
* @param Builder $builder
*/
- public function scopeRegisteredUser(Builder $builder): void
+ public function scopeSubscriber(Builder $builder): void
{
$builder->where('type', self::TYPE_SUBSCRIBER);
}
}
+ /**
+ * @param Builder $builder
+ */
+ public function scopeReceivesPdf(Builder $builder): void
+ {
+ $builder->where('receives_pdf', 1);
+ }
+
switch($this->type) {
case self::TYPE_SUBSCRIBER:
- if($this->orgIsSubscribed) {
- $id = 'org_subscribed';
- }
- if($this->subscribed()) {
- $id = 'ind_subscribed';
- }
- if($this->onTrial()) {
- $id = 'trial';
- }
+ $id = 'subscribed';
break;
case self::TYPE_PROSPECT:
--- /dev/null
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class AddSendsPdfToCollection extends Migration
+{
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::table('collections', function (Blueprint $table) {
+ $table->boolean('sends_pdf')->default(0);
+ });
+ \App\FileCollection::query()->first()->update(['sends_pdf' => true]);
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('collections', function (Blueprint $table) {
+ $table->boolean('sends_pdf')->default(0);
+ });
+ }
+}
</td>
</tr>
</table>
+<br>
{!! $content !!}