<?php
namespace Modules\Common\Controllers;
use Modules\Common\Entities\ChatPrivateMediaEntity;
use Modules\Common\Libraries\CustomFileSystem;
use Modules\Common\Models\ChatPrivateMediaModel;
use CodeIgniter\HTTP\ResponseInterface;
use Modules\Shared\Controllers\ApiController;
class ChatPrivateMedia extends ApiController
{
/**
* index function
* @method : GET
*/
public function index()
{
$chatPrivateMediaModel = new ChatPrivateMediaModel();
$result = $chatPrivateMediaModel->select($this->urlQueryParam->getFiled())
->orderBy($this->urlQueryParam->getSort(), $this->urlQueryParam->getOrder())
->paginate($this->urlQueryParam->getLimit(), 'default', $this->urlQueryParam->getPage(), $this->urlQueryParam->getOffset());
return $this->respond([
'data' => $result,
'pager' => $chatPrivateMediaModel->pager->getDetails()
], ResponseInterface::HTTP_OK, lang('Shared.api.receive'));
}
/**
* show function
* @method : GET with params ID
*/
public function show($id = null)
{
$chatPrivateMediaModel = new ChatPrivateMediaModel();
return $this->respond([
'data' => $chatPrivateMediaModel->where('id', $id)->paginate(1, 'default'),
'pager' => $chatPrivateMediaModel->pager->getDetails()
], ResponseInterface::HTTP_OK, lang('Shared.api.receive'));
}
/**
* create function
* @method : POST
*/
public function create()
{
$chatPrivateMediaModel = new ChatPrivateMediaModel();
$customConfig = new \Modules\Common\Config\ModuleCommonConfig();
$imageService = \CodeIgniter\Config\Services::image();
$chatPrivateMediaEntity = new ChatPrivateMediaEntity();
if ($this->request->getPost()) {
$rules = [
'image' => 'uploaded[image]|max_size[image,4096]|ext_in[image,png,webp,jpeg,jpg,gif]',
'chatPrivateId' => 'required'
];
if (!$this->validate($rules)) {
return $this->respond([
'error' => $this->validator->getErrors(),
'success' => false
], ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Shared.api.validation'));
}
$chatPrivateMediaEntity->chatPrivateId = $this->request->getPost('chatPrivateId');
if (isset($_FILES['image'])) {
foreach ($this->request->getFileMultiple('image') as $avatar) {
$avatar->move($customConfig->uploadDirectory . '/chat_private', time() . '.' . $avatar->getClientExtension());
$chatPrivateMediaEntity->path = $avatar->getName();
$chatPrivateMediaEntity->editPath();
if ($avatar->getClientExtension() != 'gif') {
$imageService->withFile(ROOTPATH . $chatPrivateMediaEntity->path)
->withResource()
->save(ROOTPATH . $chatPrivateMediaEntity->path, 90);
}
if (!$chatPrivateMediaModel->save($chatPrivateMediaEntity)) {
return $this->respond([
'error' => $chatPrivateMediaModel->errors(),
'success' => false,
], ResponseInterface::HTTP_BAD_REQUEST, lang('Shared.api.reject'));
}
}
}
return $this->respond([
'data' => ''
], ResponseInterface::HTTP_CREATED, lang('Shared.api.save'));
}
}
/**
* update function
* @method : PUT or PATCH
*/
public function update($id = null)
{
$chatPrivateMediaModel = new ChatPrivateMediaModel();
$customConfig = new \Modules\Common\Config\ModuleCommonConfig();
$imageService = \CodeIgniter\Config\Services::image();
$chatPrivateMediaEntity = new ChatPrivateMediaEntity();
$handy = new CustomFileSystem();
$chatRoomMedia = null;
if ($this->request->getPost()) {
$rules = [
'image' => 'uploaded[image]|max_size[image,4096]|ext_in[image,png,jpg,webp,gif]',
'chatPrivateId' => 'required'
];
if (!$this->validate($rules)) {
return $this->respond([
'error' => $this->validator->getErrors(),
'success' => false
], ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Shared.api.validation'));
}
$chatRoomMedia = $chatPrivateMediaModel->where('id', $id)->first();
if (is_null($chatRoomMedia)) {
return $this->respond([
'error' => $this->validator->getErrors(),
'success' => false
], ResponseInterface::HTTP_NOT_FOUND, lang('Shared.api.exist'));
}
$chatPrivateMediaEntity->chatPrivateId = $this->request->getPost('chatPrivateId');
$chatPrivateMediaEntity->id = $id;
if (isset($_FILES['image'])) {
foreach ($this->request->getFileMultiple('image') as $avatar) {
$avatar->move($customConfig->uploadDirectory . '/chat_private', time() . '.' . $avatar->getClientExtension());
$chatPrivateMediaEntity->path = $avatar->getName();
$chatPrivateMediaEntity->editPath();
if ($avatar->getClientExtension() != 'gif') {
$imageService->withFile(ROOTPATH . $chatPrivateMediaEntity->path)
->withResource()
->save(ROOTPATH . $chatPrivateMediaEntity->path, 90);
}
if (!$chatPrivateMediaModel->update($id, $chatPrivateMediaEntity)) {
return $this->respond([
'error' => $chatPrivateMediaModel->errors(),
'success' => false,
], ResponseInterface::HTTP_BAD_REQUEST, lang('Shared.api.reject'));
}
}
}
$handy->removeSingleFile(ROOTPATH . $chatRoomMedia->path);
return $this->respond([
'data' => array(['id' => $id,
'chatPrivateId' => $chatRoomMedia->chatPrivateId,
'path' => $chatPrivateMediaEntity->path])
], ResponseInterface::HTTP_OK, lang('Shared.api.update'));
}
}
/**
* edit function
* @method : DELETE with params ID
*/
public function delete($id = null)
{
$chatPrivateMediaModel = new ChatPrivateMediaModel();
$handy = new CustomFileSystem();
$id = ($id == 0 ? 0 : $id);
if ($id == 0) {
$isExist = $chatPrivateMediaModel->where(['chat_private_id' => $this->urlQueryParam->getForeignKey()])->
findAll();
$target = array('chat_private_id' => $this->urlQueryParam->getForeignKey());
} else {
$isExist = $chatPrivateMediaModel->where(['id' => $id])->findAll();
$target = array('id' => $id);
}
if ($isExist) {
$chatPrivateMediaModel->where($target)->delete();
foreach ($isExist as $path) {
$handy->removeSingleFile(ROOTPATH . $path->path);
}
}
return $this->respond([
], ResponseInterface::HTTP_OK, lang('Shared.api.remove'));
}
}