Back | Home
الـ Path الحالي: /home/picotech/domains/instantly.picotech.app/public_html/public/uploads/./././../.././app/Jobs
الملفات الموجودة في هذا الـ Path:
.
..
AfterGenerate.php
CampaignCreateChunk.php
CampaignCreateJob.php
CampaignCreateSuccessJob.php
ChunkImport.php
ChunkImportFail.php
ListBuilderQueue.php
مشاهدة ملف: CampaignCreateChunk.php
<?php
namespace App\Jobs;
use App\Models\Campaign;
use App\Models\Contact;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class CampaignCreateChunk implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $daily_sent_limit;
private $send_speed;
private $campaign_running_date;
private $user;
private $offset;
private $take_count = 5000;
private $message;
private $to_email;
private $from_email;
private $email_queue;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($message, $dailySentLimit, $campaignRunningDate, $sendSpeed, $user, $offset)
{
$this->message = $message;
$this->daily_sent_limit = $dailySentLimit;
$this->campaign_running_date = $campaignRunningDate;
$this->send_speed = $sendSpeed;
$this->user = $user;
$this->offset = $offset;
$numberArray = json_decode($message->emails);
$this->to_email = $numberArray->to ?? [];
$this->from_email = $numberArray->from ?? [];
$this->email_queue = [];
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$toEmails = array_slice($this->to_email, $this->offset, ($this->daily_sent_limit));
if ($toEmails) {
$generatedToEmails = [];
$contacts = Contact::where('customer_id', $this->user->id)->whereIn('email', $toEmails)->orderBy('email')->get()->unique('email');
foreach ($contacts as $contact) {
// $templates = $this->message->campaign_tempate()->first();
$templates = $this->message->body;
// dd($templates);
$templateValue=self::generateTemplate($contact,$templates,$this->message->campaign_id);
if (!in_array($contact->email, $generatedToEmails)) {
$this->email_queue[] = [
'domain_id' => $this->message->domain_id,
'message_id' => $this->message->id,
'campaign_id' => $this->message->campaign_id,
'from' => $this->from_email,
'to' => $contact->email,
'schedule_datetime' => null,
'body' => null,
'template' =>json_encode($templateValue),
'reply_to'=>$this->message->reply_to,
'from_name'=>$this->message->from_name,
'subject'=>$this->message->subject,
'created_at' => now(),
'updated_at' => now(),
];
$generatedToEmails[] = $contact->email;
}
}
foreach (array_chunk($this->email_queue, $this->take_count) as $key => $daily_email_queues) {
$final_email_queue = [];
foreach ($daily_email_queues as $queue) {
$final_email_queue[] = [
'random_ref_key' => Str::random(),
'domain_id' => $queue['domain_id'],
'message_id' => $queue['message_id'],
'campaign_id' => $queue['campaign_id'],
'from' => $queue['from'],
'to' => $queue['to'],
'schedule_datetime' => $this->campaign_running_date->addMinutes(ceil($this->send_speed))->toDateTimeString(),
'body' => $queue['body'],
'template' => $queue['template'],
'reply_to' => $queue['reply_to'],
'from_name' => $queue['from_name'],
'subject' => $queue['subject'],
'created_at' => now(),
'updated_at' => now(),
'type'=> 'sent',
];
}
if ($final_email_queue) {
$this->user->email_queues()->createMany($final_email_queue);
$this->user->message_logs()->createMany($final_email_queue);
}
}
}
}
public function failed(\Exception $exception)
{
Campaign::where('id',$this->message->campaign_id)->update(['import_fail_message'=>substr($exception->getMessage(), 0, 191)]);
}
public static function generateTemplate($contact,$templates,$campaign_id){
$campaign_description=$templates;
if ($contact->first_name) {
$campaign_description = str_replace('{first_name}', $contact->first_name, $campaign_description);
} else {
$campaign_description = str_replace('{first_name}', ' ', $campaign_description);
}
if ($contact->last_name) {
$campaign_description = str_replace('{last_name}', $contact->last_name, $campaign_description);
} else {
$campaign_description = str_replace('{last_name}', ' ', $campaign_description);
}
if ($contact->address) {
$campaign_description = str_replace('{address}', $contact->address, $campaign_description);
} else {
$campaign_description = str_replace('{address}', ' ', $campaign_description);
}
if ($contact->city) {
$campaign_description = str_replace('{city}', $contact->city, $campaign_description);
} else {
$campaign_description = str_replace('{city}', ' ', $campaign_description);
}
if ($contact->state) {
$campaign_description = str_replace('{state}', $contact->state, $campaign_description);
} else {
$campaign_description = str_replace('{state}', ' ', $campaign_description);
}
if ($contact->zip_code) {
$campaign_description = str_replace('{zip_code}', $contact->zip_code, $campaign_description);
} else {
$campaign_description = str_replace('{zip_code}', ' ', $campaign_description);
}
if ($contact->email) {
$campaign_description = str_replace('{email}', $contact->email, $campaign_description);
$unsub_link=route('unsubscribe',['email'=>$contact->email,'campaign'=>$campaign_id]);
} else {
$campaign_description = str_replace('{email}', ' ', $campaign_description);
$unsub_link='';
}
$templateValue = [];
$templateValue['campaign_title'] = '';
$templateValue['campaign_description'] = $campaign_description;
$templateValue['unsubscribe_link'] = $unsub_link;
return $templateValue;
}
}