<?php
namespace CoreCommon\Controllers;
use CoreCommon\Entities\AdvertisementMediaEntity;
use CoreCommon\Libraries\CustomFileSystem;
use CoreCommon\Models\AdvertisementMediaModel;
use CodeIgniter\HTTP\ResponseInterface;
class AdvertisementMedia extends ApiController
{
/**
* index function
* @method : GET
*/
public function index()
{
$advertisementMediaModel = new AdvertisementMediaModel();
$result = $advertisementMediaModel->select($this->urlQueryParam->getFiled())
->whereIn($this->urlQueryParam->extractQueryKey('in'), $this->urlQueryParam->extractQueryValue('in'))
->whereNotIn($this->urlQueryParam->extractQueryKey('nin'), $this->urlQueryParam->extractQueryValue('nin'))
->orWhereIn($this->urlQueryParam->extractQueryKey('oin'), $this->urlQueryParam->extractQueryValue('oin'))
->orWhereNotIn($this->urlQueryParam->extractQueryKey('onin'), $this->urlQueryParam->extractQueryValue('onin'))
->where($this->urlQueryParam->extractQueryArray('whr'))
->orWhere($this->urlQueryParam->extractQueryArray('owr'))
->like($this->urlQueryParam->extractQueryArray('lik'))
->orLike($this->urlQueryParam->extractQueryArray('olk'))
->orderBy($this->urlQueryParam->getSort(), $this->urlQueryParam->getOrder())
->paginate($this->urlQueryParam->getLimit(), 'default', $this->urlQueryParam->getPage(), $this->urlQueryParam->getOffset());
return $this->respond([
'data' => $result,
'pager' => $advertisementMediaModel->pager->getDetails()
], ResponseInterface::HTTP_OK, lang('Common.api.receive'));
}
/**
* show function
* @method : GET with params ID
*/
public function show($id = null)
{
$advertisementMediaModel = new AdvertisementMediaModel();
return $this->respond([
'data' => $advertisementMediaModel->where('id', $id)->paginate(1, 'default'),
'pager' => $advertisementMediaModel->pager->getDetails()
], ResponseInterface::HTTP_OK, lang('Common.api.receive'));
}
/**
* create function
* @method : POST
*/
public function create()
{
$advertisementMediaModel = new AdvertisementMediaModel();
$customConfig = new \CoreCommon\Config\CoreCommonConfig();
$imageService = \CodeIgniter\Config\Services::image();
$advertisementMediaEntity = new AdvertisementMediaEntity();
if ($this->request->getPost()) {
$rules = [
'image' => 'uploaded[image]|max_size[image,4096]|ext_in[image,png,jpg,mp4,gif,webp]',
];
if (!$this->validate($rules)) {
return $this->respond([
'error' => $this->validator->getErrors(),
'success' => false
], ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Common.api.validation'));
}
$advertisementMediaEntity->advertisement_id = $this->request->getPost('advertisement_id');
if (isset($_FILES['image'])) {
foreach ($this->request->getFileMultiple('image') as $avatar) {
$avatar->move($customConfig->uploadDirectory . '/advertisement', time() . '.' . $avatar->getClientExtension());
$advertisementMediaEntity->path = $avatar->getName();
$advertisementMediaEntity->editPath($avatar->getExtension() != 'mp4');
if (!$advertisementMediaModel->save($advertisementMediaEntity)) {
return $this->respond([
'error' => $advertisementMediaModel->errors(),
'success' => false,
], ResponseInterface::HTTP_BAD_REQUEST, lang('Common.api.reject'));
}
}
}
return $this->respond([
'data' => ''
], ResponseInterface::HTTP_CREATED, lang('Common.api.save'));
}
}
/**
* update function
* @method : PUT or PATCH
*/
public function update($id = null)
{
$advertisementMediaModel = new AdvertisementMediaModel();
$customConfig = new \CoreCommon\Config\CoreCommonConfig();
$imageService = \CodeIgniter\Config\Services::image();
$handy = new CustomFileSystem();
$advertisementMediaEntity = new AdvertisementMediaEntity();
$adsMedia = null;
if ($this->request->getPost()) {
$rules = [
'image' => 'uploaded[image]|max_size[image,4096]|ext_in[image,png,webp,jpeg,jpg,mp4,gif]',
];
if (!$this->validate($rules)) {
return $this->respond([
'error' => $this->validator->getErrors(),
'success' => false,
], ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Common.api.validation') );
}
$adsMedia = $advertisementMediaModel->where('id', $id)->first();
if (is_null($adsMedia)) {
return $this->respond([
'error' => $this->validator->getErrors(),
'success' => false
], ResponseInterface::HTTP_NOT_FOUND, lang('Common.api.exist'));
}
$advertisementMediaEntity->id = $id;
$advertisementMediaEntity->advertisement_id = $adsMedia->advertisement_id;
if (isset($_FILES['image'])) {
foreach ($this->request->getFileMultiple('image') as $avatar) {
if ($avatar->getExtension() == 'mp4')
$avatar->move($customConfig->uploadDirectory . '/advertisement/video', time() . '.' . $avatar->getClientExtension());
else
$avatar->move($customConfig->uploadDirectory . '/advertisement/image', time() . '.' . $avatar->getClientExtension());
$advertisementMediaEntity->path = $avatar->getName();
$advertisementMediaEntity->editPath($avatar->getExtension() != 'mp4');
if (!$advertisementMediaModel->update($id, $advertisementMediaEntity)) {
return $this->respond([
'error' => $advertisementMediaModel->errors(),
'success' => false,
], ResponseInterface::HTTP_BAD_REQUEST, lang('Common.api.reject'));
}
}
}
$handy->removeSingleFile(ROOTPATH . $adsMedia->path);
return $this->respond([
'data' => array(['id' => $id,
'advertisement_id' => $adsMedia->advertisement_id,
'path' => $advertisementMediaEntity->path])
], ResponseInterface::HTTP_OK, lang('Common.api.update'));
}
}
/**
* edit function
* @method : DELETE with params ID
*/
public function delete($id = null)
{
$advertisementMediaModel = new AdvertisementMediaModel();
$handy = new CustomFileSystem();
$id = ($id == 0 ? 0 : $id);
if ($id == 0) {
$isExist = $advertisementMediaModel->where(['advertisement_id' => $this->urlQueryParam->getForeignKey()])->
findAll();
$target = array('advertisement_id' => $this->urlQueryParam->getForeignKey());
} else {
$isExist = $advertisementMediaModel->where(['id' => $id])->findAll();
$target = array('id' => $id);
}
if ($isExist) {
$advertisementMediaModel->where($target)->delete();
foreach ($isExist as $path) {
$handy->removeSingleFile(ROOTPATH . $path->path);
}
}
return $this->respond([
], ResponseInterface::HTTP_OK, lang('Common.api.remove'));
}
}