100 lines
2.7 KiB
PHP
100 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Projects;
|
|
|
|
use App\Controllers\BaseController;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
|
|
class OffersController extends BaseController
|
|
{
|
|
public function initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
|
|
if (!defined('AUTHENTICATED') || AUTHENTICATED === false) {
|
|
die('NO ACCESS');
|
|
}
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
// Default action
|
|
}
|
|
|
|
public function getAllOffers(): ResponseInterface
|
|
{
|
|
$projectID = $this->cleanInput('projectID');
|
|
$archive = $this->cleanInput('archive');
|
|
$search = $this->cleanInput('search');
|
|
$order = $this->cleanInput('order');
|
|
$direction = $this->cleanInput('direction');
|
|
|
|
$offers = \Offer::getAllOffers($projectID, $archive, $search, $order, $direction);
|
|
|
|
return $this->json($offers);
|
|
}
|
|
|
|
public function getOffersWithoutProject(): ResponseInterface
|
|
{
|
|
$offersWithoutProject = \Offer::getOffersWithoutProject();
|
|
|
|
return $this->json($offersWithoutProject);
|
|
}
|
|
|
|
public function saveOffer(): ResponseInterface
|
|
{
|
|
$offerValidation = new \OfferValidation();
|
|
|
|
if (!$offerValidation->getValidationResult()) {
|
|
return $this->json([
|
|
'success' => false,
|
|
'messages' => $offerValidation->getErrorMessages()
|
|
]);
|
|
}
|
|
|
|
$offer = $this->request->getPost();
|
|
|
|
$offerID = $offer['id'];
|
|
unset($offer['id']);
|
|
|
|
$offerID = \Offer::saveOffer($offerID, $offer);
|
|
|
|
$createProject = $this->checkCreateproject($offerID);
|
|
|
|
return $this->json(['success' => true, 'offerID' => $offerID, 'createProject' => $createProject]);
|
|
}
|
|
|
|
public function archiveOffer(int $offerID, string $status): ResponseInterface
|
|
{
|
|
\Offer::whereId($offerID)->update(['archive' => $status]);
|
|
return $this->json(['success' => true]);
|
|
}
|
|
|
|
public function removeOffer(int $offerID): ResponseInterface
|
|
{
|
|
$removeOffer = new \RemoveOffer($offerID);
|
|
|
|
if ($removeOffer->getRemoveAllowed() === false) {
|
|
return $this->json(['result' => false]);
|
|
}
|
|
|
|
\Offer::removeOffer($offerID);
|
|
|
|
return $this->json(['result' => true]);
|
|
}
|
|
|
|
private function checkCreateproject(int $offerID): bool
|
|
{
|
|
$offer = \Offer::find($offerID);
|
|
|
|
if (is_int($offer->project_id)) {
|
|
return false;
|
|
}
|
|
if (is_null($offer->accepteddate)) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|