unioil-mobile-api/app/Http/Controllers/API/PhotoSliderController.php

266 lines
8.8 KiB
PHP

<?php
namespace App\Http\Controllers\API;
use App\Libraries\S3;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Contracts\PhotoSliderResourceInterface;
use App\Contracts\AdminActionLogsInterface;
use App\Http\Resources\PhotoSliderResource;
use App\Libraries\ParameterHelper;
use App\Helpers\CurrentUserHelper;
use App\Helpers\StringHelper;
use App\Helpers\HttpStatusCode;
use App\Http\Requests\PhotoSliderFormValidation;
class PhotoSliderController extends Controller
{
const MODULE = 'PHOTOSLIDER';
public $photoSlider;
protected $format;
protected $module;
protected $model;
protected $admin_logs;
public function __construct(PhotoSliderResourceInterface $photoSlider, HttpStatusCode $httpStatusCode,AdminActionLogsInterface $admin_logs)
{
$this->photoSlider = $photoSlider;
$this->format = $httpStatusCode;
$this->admin_logs = $admin_logs;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$params = [
'search' => ($request->has('_search') ? $request->get('_search') : NULL),
'page_size' => ($request->has('page_size') ? $request->get('page_size') : 10),
'page' => ($request->has('page') ? $request->get('page') : 10),
'sorting' => ParameterHelper::prepareSortingParameter($request)
];
$list = $this->photoSlider->listing($params);
if(count($list))
{
$additionals = $this->format->success("Success",[],false);
$data = PhotoSliderResource::collection($list)->additional($additionals);
return $data->response()->setStatusCode(200);
}
else
{
return $this->format->success("No records found",[]);
}
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(PhotoSliderFormValidation $request)
{
$id = $this->photoSlider->store($request);
if($id)
{
$this->admin_logs->log($id,self::MODULE,'STORE');
return $this->format->created('Photo Slider has been added');
}
else
{
return $this->format->notFound();
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($uuid)
{
$details = $this->photoSlider->getByField('photoslider_uuid',$uuid);
if($details->count())
{
$additionals = $this->format->success("Success",[],false);
$data = (new PhotoSliderResource($details[0]))->additional($additionals);
return $data->response()->setStatusCode(200);
}
else
return $this->format->notFound();
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(PhotoSliderFormValidation $request, $uuid)
{
$old_data = $this->photoSlider->getByField('photoslider_uuid',$uuid);
if($old_data->count())
{
if($this->photoSlider->update($request,$uuid))
{
$this->admin_logs->log($old_data[0]['photoslider_id'],self::MODULE,'UPDATE');
return $this->format->success("Photo Slider has been updated");
}
else
{
return $this->format->notFound();
}
}
return $this->format->notFound();
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($uuid)
{
$details = $this->photoSlider->getByField('photoslider_uuid',$uuid);
if($details->count())
{
$details = $details[0];
if($this->photoSlider->delete($uuid))
{
$this->admin_logs->log($details['photoslider_id'],self::MODULE,'DELETE');
return $this->format->success("Photo Slider Successfully Deleted");
}
}
return $this->format->notFound();
}
public function batch_delete(Request $request)
{
$uuid = $request->has('photoslider_uuid') ? $request->get('photoslider_uuid') : null;
if($uuid)
{
$details = $this->photoSlider->getDetailsWhereIn('photoslider_uuid',$uuid);
$id = array();
if($this->photoSlider->delete($uuid))
{
foreach ($details as $key => $value)
{
$this->admin_logs->log($value['photoslider_id'],self::MODULE,'DELETE');
}
return $this->format->success("Photo Slider Successfully Deleted");
}
else
{
return $this->format->badRequest('Something went wrong');
}
}
else
{
$data['photoslider_uuid'] = 'photoslider_uuid is required';
return $this->format->unprocessableEntity("Submit at least one item",$data);
}
}
public function check_count()
{
if($this->photoSlider->count() < 10)
return $this->format->success('User can add');
else
return $this->format->badRequest('Photo slider cannot be more than 10');
}
public function get_photo_slider(Request $request)
{
$data = $this->photoSlider->getPhotoSlider();
$currentUser = CurrentUserHelper::get_member_by_lcard($request->input('lcard_uuid'));
$return = [];
foreach ($data as $key => $value){
$return[$key]['photoslider_id'] = $value['photoslider_id'];
$return[$key]['photoslider_uuid'] = $value['photoslider_uuid'];
$return[$key]['title'] = $value['title'];
$return[$key]['description'] = $value['description'];
$return[$key]['image'] = S3::public_path($value['image']);
$return[$key]['date_start'] = $value['date_start'];
$return[$key]['date_end'] = $value['date_end'];
$return[$key]['is_toppromotion'] = $value['is_toppromotion'];
$return[$key]['is_gps'] = $value['is_gps'];
$return[$key]['promo_type'] = $value['promo_type'];
$return[$key]['promotion_id'] = $value['promotion_id'];
$promotion_uuid = 0;
if ( strtotime(date('Y-m-d H:i:s')) >= strtotime($value['promotion_date_start']) ){
if(strtotime(date('Y-m-d H:i:s')) <= strtotime($value['promotion_date_end'])){
$promotion_uuid = $value['promotion_uuid'];
}
}
$return[$key]['promotion_uuid'] = $promotion_uuid;
$return[$key]['promotion_date_start'] = $value['promotion_date_start'];
$return[$key]['promotion_date_end'] = $value['promotion_date_end'];
$return[$key]['promo_details'] = [
"title"=> $promotion_uuid=="0" ? "" : $value['promotion_title'],
"description"=>$promotion_uuid=="0" ? "" : $value['promotion_description'],
"image"=>$promotion_uuid=="0" ? "" : S3::public_path($value['promotion_image']),
];
$return[$key]['promo_details'] = $value['promo_type'] != 3 ? $return[$key]['promo_details'] : ["title"=> "","description"=>"","image"=>""];
if ($currentUser){
$now = Carbon::now();
$birthday = $now->year."-".$currentUser->birthdate->month."-".$currentUser->birthdate->day." 00:00:00";
if($value['promo_type'] == "3"){
if (strtotime($value['promotion_date_start']) <= strtotime($birthday) && strtotime($value['promotion_date_end']) >= strtotime($birthday) ){
$return[$key]['promo_details'] = [
"title"=> $promotion_uuid=="0" ? "" : $value['promotion_title'],
"description"=>$promotion_uuid=="0" ? "" : $value['promotion_description'],
"image"=>$promotion_uuid=="0" ? "" : S3::public_path($value['promotion_image']),
];
}
}
}
}
if (count($data) == 0 ) {
return $this->format->mobile_success("Data is Empty");
}
return $this->format->mobile_success('Success',$return);
}
}