示例#1
0
 public function listing($page, $limit)
 {
     $response = array('data' => array(), 'paginator' => '');
     if (!empty($limit)) {
         $emails = Email::paginate($limit);
     } else {
         $emails = Email::where('id', '>', '0')->get();
     }
     if (!empty($emails)) {
         foreach ($emails as $key => $email) {
             $response['data'][] = $this->get($email->id, false);
         }
     }
     if (!empty($limit)) {
         $response = Utility::paginator($response, $emails, $limit);
     }
     return $response;
 }
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     // Find email that are ready to be started
     $emails = Email::where('started', 0)->whereNotNull('scheduled_for')->where('scheduled_for', '<=', DB::RAW('NOW()'))->get();
     // set every email as started
     foreach ($emails as $email) {
         $email->started = 1;
         $email->save();
     }
     foreach ($emails as $email) {
         // Generate email list
         // To add more email list edit this file and `App/Models/Email.php
         $list = [];
         $students = [];
         $newcomers = [];
         switch ($email->list) {
             case Email::STUPRELISTE:
                 $list['*****@*****.**'] = ['name' => 'STUPRE-liste', 'user' => null];
                 break;
             case Email::VOLUNTEERS:
                 $students = Student::where('volunteer', 1)->get();
                 break;
             case Email::CE_VALIDATED:
                 $students = Student::where('ce', 1)->whereNotNull('team_id')->where('team_accepted', 1)->get();
                 break;
             case Email::REFERRALS_VALIDATED:
                 $students = Student::where('referral', 1)->where('referral_validated', 1)->get();
                 break;
             case Email::REFERRALS_INCOMPLETE:
                 $students = Student::where('referral', 1)->where('referral_validated', 0)->where(function ($query) {
                     $query->where('phone', '')->orWhereNull('phone')->orWhere('email', '')->orWhereNull('email')->orWhere('referral_text', '')->orWhereNull('referral_text');
                 })->get();
                 break;
             case Email::REFERRALS_VALIDATED_BRANCH:
                 $students = Student::where('referral', 1)->where('referral_validated', 1)->where('branch', '<>', 'tc')->get();
                 break;
             case Email::REFERRALS_VALIDATED_TC:
                 $students = Student::where('referral', 1)->where('referral_validated', 1)->where('branch', '=', 'tc')->get();
                 break;
             case Email::ORGA:
                 $students = Student::where('orga', 1)->get();
                 break;
             case Email::ADMIN:
                 $students = Student::where('admin', 100)->get();
                 break;
             case Email::NEWCOMERS_ALL:
                 $newcomers = Newcomer::all();
                 break;
             case Email::NEWCOMERS_ALL_TC:
                 $newcomers = Newcomer::where('branch', 'TC')->get();
                 break;
             case Email::NEWCOMERS_ALL_BRANCH:
                 $newcomers = Newcomer::where('branch', '<>', 'TC')->where('branch', '<>', 'MP')->get();
                 break;
             case Email::NEWCOMERS_ALL_MASTER:
                 $newcomers = Newcomer::where('branch', 'MP')->get();
                 break;
             case Email::NEWCOMERS_FILLED:
                 $newcomers = Newcomer::where('email', '<>', '')->whereNotNull('email')->get();
                 break;
             case Email::NEWCOMERS_FILLED_TC:
                 $newcomers = Newcomer::where('branch', 'TC')->where('email', '<>', '')->whereNotNull('email')->get();
                 break;
             case Email::NEWCOMERS_FILLED_BRANCH:
                 $newcomers = Newcomer::where('branch', '<>', 'TC')->where('branch', '<>', 'MP')->where('email', '<>', '')->whereNotNull('email')->get();
                 break;
             case Email::NEWCOMERS_FILLED_MASTER:
                 $newcomers = Newcomer::where('branch', 'MP')->where('email', '<>', '')->whereNotNull('email')->get();
                 break;
             default:
                 echo 'Error : Unknown email list id';
                 break;
         }
         // Select email to put in the list
         foreach ($students as $student) {
             $list[$student->email] = ['name' => $student->first_name . ' ' . $student->last_name, 'user' => $student];
         }
         foreach ($newcomers as $newcomer) {
             if (!empty($newcomer->email)) {
                 $list[$newcomer->email] = ['name' => $newcomer->first_name . ' ' . $newcomer->last_name, 'user' => $newcomer];
             } elseif (!empty($newcomer->registration_email)) {
                 $list[$newcomer->registration_email] = ['name' => $newcomer->first_name . ' ' . $newcomer->last_name, 'user' => $newcomer];
             }
         }
         // Set count in db
         $email->done = 0;
         $email->total = count($list);
         $email->save();
         // Send emails
         $delay = 0;
         foreach ($list as $dest => $val) {
             $this->currentUser = $val['user'];
             $view = $email->template;
             if ($email->is_plaintext) {
                 $view = preg_replace_callback('/{{([A-Z0-9_]+)}}/i', array($this, 'replaceCallback'), $view);
                 $view = nl2br(e($view));
             } else {
                 $view = preg_replace_callback('/{{([A-Z0-9_]+)}}/i', array($this, 'replaceCallback'), $view);
             }
             $job = new SendEmail($email, $view, $dest, $val['name']);
             $job->delay($delay);
             $this->dispatch($job);
             $delay += 5;
         }
     }
 }