D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
tattooscyy
/
test
/
app
/
Http
/
Controllers
/
Manager
/
Filename :
PromotionController.php
back
Copy
<?php namespace App\Http\Controllers\Manager; use DateTime; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Auth; use App; use Mail; use App\Saloon; use App\Promotion; use App\PromotionEmail; use App\Contract; use \App\Mail\PromotionEmailSender; class PromotionController extends Controller { public function __construct() { set_time_limit(0); } public function getIndex(Request $request) { $saloon = $request->saloon; $id = (int)$saloon->id; $promotions_emails = PromotionEmail::orderBy('id', 'DESC'); if ($request->date_from) { $date_f = DateTime::createFromFormat('Y-m-d', $request->date_from)->format('y-m-d'); $promotions_emails = $promotions_emails->where('putdate', '>=', $date_f); } else { $promotions_emails = $promotions_emails->where('putdate', '>=', date('y-m-d')); } if ($request->date_to) { $date_t = DateTime::createFromFormat('Y-m-d', $request->date_to)->format('y-m-d'); $promotions_emails = $promotions_emails->where('putdate', '<=', $date_t); } else { $promotions_emails = $promotions_emails->where('putdate', '<=', date('y-m-d')); } if ($request->cleared) { $promotions_emails = $promotions_emails->where('cleared', 1); } else { $promotions_emails = $promotions_emails->whereNull('cleared'); } $promotions = Promotion::orderBy('id', 'DESC')->paginate('30'); $saloons = Saloon::all(); $promotions_emails = $promotions_emails->get(); $promotion_email_last = PromotionEmail::where('status', 'contract')->orderBy('id', 'DESC')->first(); if (!$promotion_email_last) { $promotion_email_last = new PromotionEmail; } return view('manager.promotions', compact('saloons', 'promotions', 'id', 'promotions_emails', 'promotion_email_last')); } public function postIndex(Request $request) { //dd($_POST); //$saloon_id = (int)$_POST['saloon_id']; //$pic = \App::make('\App\Libs\Imag')->url($dat, public_path() . $pp, $name); $obj = new Promotion; $obj->thema = $_POST['thema']; $obj->body = $_POST['body']; $obj->saloon_id = 1; $obj->user_id = Auth::user()->id; $obj->save(); $promotion_emails = PromotionEmail::whereIn('id', $_POST['promotions'])->get(); /* $arr_type = []; if (isset($_POST['type_piercing']) && $_POST['type_piercing'] == 'Piercing') { $arr_type[] = 'piercing'; } else { //echo "Do not Need wheelchair access."; } if (isset($_POST['type_tatoo']) && $_POST['type_tatoo'] == 'Tattoo') { $arr_type[] = 'tattoo'; } else { //echo "Do not Need wheelchair access."; } $contrs = Contract::whereIn('type', $arr_type)->where('saloon_id', $saloon_id)->orderBy('id', 'DESC')->get(); foreach($contrs as $one){ $to_name = $one->name; $to_email = $one->email; dump($one); } dd(); */ $count = 0; foreach ($promotion_emails as $promotion_email) { $to_name = $promotion_email->name; $to_email = $promotion_email->email; $thema = $obj->thema; $user_name = ''; if ($promotion_email->name) { $user_name = $promotion_email->name; } else { if ($promotion_email->contract) { $user_name = $promotion_email->contract->name; } } $body = ' <p>Bonjour ' . $user_name . ', </p> <p>' . $obj->body . '</p> <p>_________________ </p> <p></p> <sup><a href="https://tattooscalculator.com/unsubscribe?dec=' . encrypt($promotion_email->email) . '">Se désabonner</a></sup> '; $headers = "From: American Body Art <info@tattooscalculator.com> \r\n"; $headers .= "Reply-To: noreply@tattooscalculator.com\r\n"; $headers .= "MIME-Version: 1.0\r\n"; $headers .= "Content-Type: text/html; charset=UTF-8\r\n"; mail($to_email, $thema, $body, $headers); //Mail::to($to_email)->send(new PromotionEmailSender($promotion_email, $obj)); $promotion_email->update(['promotion_id' => $obj->id]); $count++; } $obj->update(['counts' => $count]); return redirect()->back(); } public function getEmailFromContracts(Request $request) { if ($request->contract_date_from) { $date_from = DateTime::createFromFormat('Y-m-d', $request->contract_date_from)->format('Y-m-d'); } else { $date_from = '1970-01-01'; } if ($request->contract_date_to) { $date_to = DateTime::createFromFormat('Y-m-d', $request->contract_date_to)->format('Y-m-d'); } else { $date_to = date('Y-m-d'); } $contracts = Contract::where('created_at', '>=', $date_from)->where('created_at', '<=', $date_to)->orderBy('id', 'DESC')->get(); foreach ($contracts as $contract) { if (filter_var($contract->email, FILTER_VALIDATE_EMAIL)) { $promotion_email = PromotionEmail::where('email', $contract->email); if ($promotion_email->count() > 0) { $promotion_email->update(['status' => 'contract', 'cleared' => null, 'contract_id' => $contract->id, 'putdate' => date('y-m-d')]); } else { $promotion_email_new = new PromotionEmail; $promotion_email_new->email = $contract->email; $promotion_email_new->putdate = date('y-m-d'); $promotion_email_new->contract_id = $contract->id; $promotion_email_new->status = 'contract'; $promotion_email_new->save(); } } } return redirect()->back(); } public function postEmailFromCsv(Request $request) { $path = $request->file('csv')->getRealPath(); $csv_data = array_map('str_getcsv', file($path)); foreach ($csv_data as $row) { if ($row[0]) { if (filter_var($row[0], FILTER_VALIDATE_EMAIL)) { $promotion_email = PromotionEmail::where('email', $row[0])->first(); if (isset($promotion_email)) { if ($promotion_email->count() > 0) { $tt = isset($row[1]) ? $row[1] : null; if ($tt != null) { $promotion_email->name = $tt; } $promotion_email->status = 'csv'; $promotion_email->cleared = null; $promotion_email->putdate = date('y-m-d'); $promotion_email->update(); } else { $promotion_email_new = new PromotionEmail; $promotion_email_new->email = $row[0]; $promotion_email_new->putdate = date('y-m-d'); $promotion_email_new->status = 'csv'; $promotion_email_new->cleared = null; $promotion_email_new->name = isset($row[1]) ? $row[1] : null; $promotion_email_new->save(); } } else { $promotion_email_new = new PromotionEmail; $promotion_email_new->email = $row[0]; $promotion_email_new->putdate = date('y-m-d'); $promotion_email_new->status = 'csv'; $promotion_email_new->cleared = null; $promotion_email_new->name = isset($row[1]) ? $row[1] : null; $promotion_email_new->save(); } } } } return redirect()->back(); } public function getUnsubscribe() { return view('unsubscribe'); } public function postUnsubscribe(Request $request) { $email = decrypt(request()->dec); PromotionEmail::where('email', $email)->update(['promo' => 0]); return redirect('/'); } public function getClear() { $promotions_emails = PromotionEmail::where('putdate', date('y-m-d'))->get(); foreach ($promotions_emails as $promotion_email) { $promotion_email->update(['cleared' => 1]); } return redirect('/manager/promotions'); } }