238 lines
8.9 KiB
PHP
238 lines
8.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
use App\Enums\ArticleCategory;
|
|
use App\Enums\NotificationType;
|
|
use App\Events\NewNotification;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreArticle;
|
|
use App\Http\Requests\UpdateArticle;
|
|
use Illuminate\Support\Str;
|
|
use App\Models\Articles;
|
|
use App\Models\SurveysAnswers;
|
|
use App\Traits\UpdateModelStatus;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use League\Csv\Writer;
|
|
|
|
class ArticlesController extends Controller
|
|
{
|
|
use UpdateModelStatus;
|
|
|
|
public function index()
|
|
{
|
|
$articles = Articles::paginate();
|
|
return view('bo.articles.index', compact('articles'));
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
return view('bo.articles.create');
|
|
}
|
|
|
|
public function store(StoreArticle $request)
|
|
{
|
|
|
|
$imageName = Str::slug($request->title) . '-le-gaulois-' . date('d-m-Y') . '.' . $request->picture->extension();
|
|
$request->picture->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m')), $imageName);
|
|
$imageName_r = Str::slug(
|
|
$request->title
|
|
) . '-le-gaulois-' . date('d-m-Y') . '_r.' . $request->picture_r->extension();
|
|
$request->picture_r->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m')), $imageName_r);
|
|
|
|
$article = new Articles($request->validated());
|
|
$article->picture = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName;
|
|
$article->picture_r = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName_r;
|
|
$article->saveOrFail();
|
|
if ($request->type == ArticleCategory::sondage()) {
|
|
foreach ($request->answers as $articlenswer) {
|
|
if (!empty($articlenswer)) {
|
|
$article->answers()->create([
|
|
'answer' => $articlenswer,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
if ($request->notification === 1) {
|
|
event(new NewNotification(NotificationType::info(), route('club.infos.child', [
|
|
'articleTag' => $request->type->value,
|
|
'publishedArticle' => $request->slug
|
|
]), $article));
|
|
return redirect()->route('bo.articles.edit', $article->id)
|
|
->with('success', 'Notification envoyée, Enregistré !');
|
|
}
|
|
|
|
return redirect()->route('bo.articles.edit', $article->id);
|
|
}
|
|
|
|
public function edit(Articles $article)
|
|
{
|
|
return view('bo.articles.edit', ['article' => $article]);
|
|
}
|
|
|
|
public function update(UpdateArticle $request, Articles $article)
|
|
{
|
|
$article->fill($request->validated());
|
|
$picture = $request->picture;
|
|
$picture_r = $request->picture_r;
|
|
|
|
if ($picture !== null && $picture_r !== null) {
|
|
$request->validate([
|
|
'picture' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=340,height=340',
|
|
'picture_r' => 'image|mimes:jpeg,png,jpg|max:2048|dimensions:width=780,height=508',
|
|
]);
|
|
} elseif ($picture !== null && $picture_r === null) {
|
|
$request->validate([
|
|
'picture' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=340,height=340'
|
|
]);
|
|
} elseif ($picture === null && $picture_r !== null) {
|
|
$request->validate([
|
|
'picture_r' => 'required|image|mimes:jpeg,png,jpg|max:2048|dimensions:width=780,height=508'
|
|
]);
|
|
}
|
|
|
|
if ($picture !== null) {
|
|
if (file_exists(public_path() . '/' . $article->picture)) {
|
|
unlink(public_path() . '/' . $article->picture);
|
|
}
|
|
$imageName = $request->slug . '.' . $picture->extension();
|
|
$picture->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m') . '/'), $imageName);
|
|
}
|
|
if ($picture_r !== null) {
|
|
if (file_exists(public_path() . '/' . $article->picture_r)) {
|
|
unlink(public_path() . '/' . $article->picture_r);
|
|
}
|
|
$imageName_r = $request->slug . '_r.' . $picture_r->extension();
|
|
$picture_r->move(public_path('uploads/images/articles/' . date('Y') . '/' . date('m') . '/'), $imageName_r);
|
|
}
|
|
|
|
if ($picture !== null) {
|
|
$article->picture = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName;
|
|
}
|
|
if ($picture_r !== null) {
|
|
$article->picture_r = 'uploads/images/articles/' . date('Y') . '/' . date('m') . '/' . $imageName_r;
|
|
}
|
|
if ($request->type == ArticleCategory::sondage()) {
|
|
// Sync or delete Old answers
|
|
foreach ($article->answers as $answer) {
|
|
if (array_key_exists($answer->id, $request->answers)) {
|
|
$answer->answer = $request->answers[$answer->id];
|
|
$answer->saveOrFail();
|
|
} else {
|
|
$answer->delete();
|
|
}
|
|
}
|
|
// Save New answers
|
|
foreach ($request->answers as $key => $formAnswer) {
|
|
if (\strpos($key, '_') !== false) {
|
|
$answer = new SurveysAnswers();
|
|
$answer->answer = $formAnswer;
|
|
$answer->article()->associate($article);
|
|
$answer->saveOrFail();
|
|
}
|
|
}
|
|
} else {
|
|
// remove Sondage elements
|
|
$article->question = null;
|
|
$article->multiple_response = null;
|
|
$article->stop_poll = null;
|
|
$article->answers()->delete();
|
|
}//end if
|
|
$article->saveOrFail();
|
|
|
|
if ($article->type == ArticleCategory::actu() && $article->status === 1) {
|
|
$articlectus = Articles::where([['status', 1], ['type', ArticleCategory::actu()]])
|
|
->orderBy('created_at', 'DESC')->limit(3)->get();
|
|
Cache::put('ArticlesGlobal', $articlectus);
|
|
}
|
|
|
|
if ($request->notification === 1) {
|
|
event(
|
|
new NewNotification(
|
|
NotificationType::info(),
|
|
route('club.infos.child', [
|
|
'articleTag' => $article->type->value,
|
|
'publishedArticle' => $article->slug
|
|
]),
|
|
$article
|
|
)
|
|
);
|
|
return redirect()->route('bo.articles.edit', $article->id)
|
|
->with('success', 'Notification envoyée, Enregistré !');
|
|
}
|
|
|
|
return redirect()->route('bo.articles.edit', $article->id)
|
|
->with('success', 'Enregistré !');
|
|
}
|
|
|
|
public function destroy(Articles $article)
|
|
{
|
|
if (file_exists(public_path() . '/' . $article->picture)) {
|
|
unlink(public_path() . '/' . $article->picture);
|
|
}
|
|
if (file_exists(public_path() . '/' . $article->picture_r)) {
|
|
unlink(public_path() . '/' . $article->picture_r);
|
|
}
|
|
|
|
$article->notifiable()->delete();
|
|
$article->delete();
|
|
|
|
return redirect()->route('bo.articles.index')
|
|
->with('warning', 'Le contenu à bien été supprimé');
|
|
}
|
|
|
|
public function exportSondage(Articles $article)
|
|
{
|
|
if (!$article->type->equals(ArticleCategory::sondage())) {
|
|
return response()->redirect('bo.articles.edit', $article);
|
|
}
|
|
|
|
$anwsers = $article->answers->load('customers');
|
|
|
|
$header = '"customer_id";"crm_id";' . implode(';', $anwsers->map(function ($answer) {
|
|
return "\"$answer->answer\"";
|
|
})->all());
|
|
|
|
$w = Writer::createFromString($header . PHP_EOL);
|
|
$w->setDelimiter(';');
|
|
$w->setEnclosure('"');
|
|
$w->setOutputBOM(Writer::BOM_UTF8);
|
|
|
|
$tmpl = [
|
|
0 => '0',
|
|
1 => '0',
|
|
];
|
|
foreach ($anwsers as $k => $answer) {
|
|
$tmpl[$k + 2] = '0';
|
|
}
|
|
$res = [];
|
|
foreach ($anwsers as $k => $answer) {
|
|
$answer->customers->map(function ($customer) use ($answer, $k, &$res) {
|
|
if (!array_key_exists($customer->id, $res)) {
|
|
$res[$customer->id] = [
|
|
'customer' => $customer,
|
|
'answers' => []
|
|
];
|
|
}
|
|
$res[$customer->id]['answers'][$k + 2] = 1;
|
|
});
|
|
}
|
|
|
|
foreach ($res as $customerIntel) {
|
|
$w->insertOne(array_replace(
|
|
$tmpl,
|
|
[
|
|
$customerIntel['customer']->id,
|
|
$customerIntel['customer']->crm_id,
|
|
],
|
|
$customerIntel['answers']
|
|
));
|
|
}
|
|
return response((string)$w, 200, [
|
|
'Content-Encoding' => 'none',
|
|
'Content-Type' => 'text/csv; charset=UTF-8',
|
|
'Content-Disposition' => "attachment; filename=\"Résultats Sondage $article->title.csv\"",
|
|
'Content-Description' => 'File Transfer',
|
|
]);
|
|
}
|
|
}
|