Skip to content

Commit

Permalink
Merge pull request #2804 from Leantime/domain/client-refactor
Browse files Browse the repository at this point in the history
Client module refactor
  • Loading branch information
marcelfolaron authored Nov 25, 2024
2 parents aa3c144 + 5f725fb commit 48c7b1d
Show file tree
Hide file tree
Showing 27 changed files with 435 additions and 259 deletions.
10 changes: 5 additions & 5 deletions app/Domain/Api/Controllers/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Leantime\Core\Controller\Controller;
use Leantime\Domain\Auth\Models\Roles;
use Leantime\Domain\Auth\Services\Auth;
use Leantime\Domain\Clients\Repositories\Clients as ClientRepository;
use Leantime\Domain\Clients\Services\Clients as ClientService;
use Leantime\Domain\Projects\Repositories\Projects as ProjectRepository;
use Leantime\Domain\Users\Repositories\Users as UserRepository;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -20,7 +20,7 @@ class ApiKey extends Controller

private UserRepository $userRepo;

private ClientRepository $clientsRepo;
private ClientService $clientService;

/**
* init - initialize private variables
Expand All @@ -30,13 +30,13 @@ class ApiKey extends Controller
*
* @throws BindingResolutionException
*/
public function init(ProjectRepository $projectsRepo, UserRepository $userRepo, ClientRepository $clientsRepo): void
public function init(ProjectRepository $projectsRepo, UserRepository $userRepo, ClientService $clientService): void
{
self::dispatchEvent('api_key_init', $this);

$this->projectsRepo = $projectsRepo;
$this->userRepo = $userRepo;
$this->clientsRepo = $clientsRepo;
$this->clientService = $clientService;
}

/**
Expand Down Expand Up @@ -130,7 +130,7 @@ public function run(): Response
// Assign vars
$this->tpl->assign('allProjects', $this->projectsRepo->getAll());
$this->tpl->assign('roles', Roles::getRoles());
$this->tpl->assign('clients', $this->clientsRepo->getAll());
$this->tpl->assign('clients', $this->clientService->getAll());

// Sensitive Form, generate form tokens
$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyz';
Expand Down
51 changes: 39 additions & 12 deletions app/Domain/Clients/Controllers/DelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,73 @@
use Leantime\Core\Controller\Frontcontroller;
use Leantime\Domain\Auth\Models\Roles;
use Leantime\Domain\Auth\Services\Auth;
use Leantime\Domain\Clients\Repositories\Clients as ClientRepository;
use Leantime\Domain\Clients\Services\Clients as ClientService;
use Symfony\Component\HttpFoundation\Response;

class DelClient extends Controller
{
private ClientRepository $clientRepo;
private ClientService $clientService;

/**
* init - initialize private variables
*/
public function init()
public function init(
ClientService $clientService
)
{
$this->clientRepo = app()->make(ClientRepository::class);
$this->clientService = $clientService;
}


/**
* run - display template and edit data
* get - display template
*/
public function run()
public function get($params):Response
{
// dd("In get function");
Auth::authOrRedirect([Roles::$owner, Roles::$admin], true);

//Only admins
if (Auth::userIsAtLeast(Roles::$admin)) {
if (isset($_GET['id']) === true) {
$id = (int) ($_GET['id']);
if (isset($params['id']) === true) {
$id = (int) ($params['id']);

$this->tpl->assign('client', $this->clientService->get($id));

return $this->tpl->display('clients.delClient');
} else {
return $this->tpl->display('errors.error403', responseCode: 403);
}
} else {
return $this->tpl->display('errors.error403', responseCode: 403);
}
}

if ($this->clientRepo->hasTickets($id) === true) {

/**
* post - display template and delete client
*/
public function post($params):Response
{
Auth::authOrRedirect([Roles::$owner, Roles::$admin], true);

//Only admins
if (Auth::userIsAtLeast(Roles::$admin)) {
if (isset($params['id']) === true) {
$id = (int) ($params['id']);

if ($this->clientService->hasTickets($id) === true) {
$this->tpl->setNotification($this->language->__('notification.client_has_todos'), 'error');
} else {
if (isset($_POST['del']) === true) {
$this->clientRepo->deleteClient($id);
$this->clientService->delete($id);

$this->tpl->setNotification($this->language->__('notification.client_deleted'), 'success');

return Frontcontroller::redirect(BASE_URL.'/clients/showAll');
}
}

$this->tpl->assign('client', $this->clientRepo->getClient($id));

return $this->tpl->display('clients.delClient');
} else {
return $this->tpl->display('errors.error403', responseCode: 403);
Expand Down
75 changes: 37 additions & 38 deletions app/Domain/Clients/Controllers/NewClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,62 +10,61 @@
use Leantime\Core\Controller\Frontcontroller;
use Leantime\Domain\Auth\Models\Roles;
use Leantime\Domain\Auth\Services\Auth;
use Leantime\Domain\Clients\Repositories\Clients as ClientRepository;
use Leantime\Domain\Users\Repositories\Users as UserRepository;
use Leantime\Domain\Clients\Models\Clients as ClientModel;
use Leantime\Domain\Clients\Services\Clients as ClientService;
use Symfony\Component\HttpFoundation\Response;

class NewClient extends Controller
{
private ClientRepository $clientRepo;

private UserRepository $user;
private ClientService $clientService;

/**
* init - initialize private variables
*/
public function init(ClientRepository $clientRepo, UserRepository $user)
public function init(
ClientService $clientService
){
$this->clientService = $clientService;
}

/**
* get - display template and provide empty values object
*/
public function get(): Response
{
Auth::authOrRedirect([Roles::$owner, Roles::$admin], true);

//Only admins
if (Auth::userIsAtLeast(Roles::$admin)) {

$this->clientRepo = $clientRepo;
$this->user = $user;
$values = app() -> make(ClientModel::class);

$this->tpl->assign('values', $values);

return $this->tpl->display('clients.newClient');
} else {
return $this->tpl->display('errors.error403', responseCode: 403);
}
}

/**
* run - display template and edit data
* post - display template and save data
*/
public function run()
public function post($params): Response
{
Auth::authOrRedirect([Roles::$owner, Roles::$admin], true);

//Only admins
if (Auth::userIsAtLeast(Roles::$admin)) {
$values = [
'name' => '',
'street' => '',
'zip' => '',
'city' => '',
'state' => '',
'country' => '',
'phone' => '',
'internet' => '',
'email' => '',
];

if (isset($_POST['save']) === true) {
$values = [
'name' => ($_POST['name']),
'street' => ($_POST['street']),
'zip' => ($_POST['zip']),
'city' => ($_POST['city']),
'state' => ($_POST['state']),
'country' => ($_POST['country']),
'phone' => ($_POST['phone']),
'internet' => ($_POST['internet']),
'email' => ($_POST['email']),
];

if ($values['name'] !== '') {
if ($this->clientRepo->isClient($values) !== true) {
$id = $this->clientRepo->addClient($values);

$values = null;

if (isset($params['save']) === true) {

$values = app() -> make(ClientModel::class, ['attributes' => $params]);
if ($values->name !== '') {
if ($this->clientService->isClient($values) !== true) {
$id = $this->clientService->create($values);
$this->tpl->setNotification($this->language->__('notification.client_added_successfully'), 'success', 'new_client');

return Frontcontroller::redirect(BASE_URL.'/clients/showClient/'.$id);
Expand Down
18 changes: 10 additions & 8 deletions app/Domain/Clients/Controllers/ShowAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
use Leantime\Core\Controller\Controller;
use Leantime\Domain\Auth\Models\Roles;
use Leantime\Domain\Auth\Services\Auth;
use Leantime\Domain\Clients\Repositories\Clients as ClientRepository;
use Leantime\Domain\Clients\Services\Clients as ClientService;
use Symfony\Component\HttpFoundation\Response;

class ShowAll extends Controller
{
private ClientRepository $clientRepo;
private ClientService $clientService;

/**
* init - initialize private variables
*/
public function init(ClientRepository $clientRepo)
public function init(ClientService $clientService)
{

$this->clientRepo = $clientRepo;
$this->clientService = $clientService;
}

/**
* run - display template and edit data
* get - display template and edit data
*/
public function run()
public function get():Response
{

Auth::authOrRedirect([Roles::$owner, Roles::$admin], true);
Expand All @@ -36,7 +36,9 @@ public function run()
$this->tpl->assign('admin', true);
}

$this->tpl->assign('allClients', $this->clientRepo->getAll());
$clients = $this->clientService->getAll();

$this->tpl->assign('allClients', $clients);

return $this->tpl->display('clients.showAll');
}
Expand Down
Loading

0 comments on commit 48c7b1d

Please sign in to comment.