Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
/**
* @brief Contact Controller
* @author dev@maarch.org
* @ingroup core
*/
namespace Core\Controllers;
use Core\Models\ContactModel;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Respect\Validation\Validator;
class ContactController
{
public function create(RequestInterface $request, ResponseInterface $response)
{
$data = $request->getParams();
$check = Validator::notEmpty()->validate($data['firstname']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['lastname']);
$check = $check && Validator::intVal()->notEmpty()->validate($data['contactType']);
$check = $check && Validator::intVal()->notEmpty()->validate($data['contactPurposeId']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['isCorporatePerson']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['email']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
if (empty($data['userId'])) {
$data['userId'] = 'superadmin';
}
if (empty($data['entityId'])) {
$data['entityId'] = 'SUPERADMIN';
}
if ($data['isCorporatePerson'] != 'Y') {
$data['isCorporatePerson'] = 'N';
} else {
$data['addressFirstname'] = $data['firstname'];
$data['addressLastname'] = $data['lastname'];
unset($data['firstname'], $data['lastname']);
if (empty($data['isPrivate'])) {
$data['isPrivate'] = 'N';
} elseif ($data['isPrivate'] != 'N') {
$data['isPrivate'] = 'Y';
}
$contact = ContactModel::getByEmail(['email' => $data['email'], 'select' => ['contacts_v2.contact_id', 'contact_addresses.id']]);
if (!empty($contact['id'])) {
return $response->withJson(['contactId' => $contact['contact_id'], 'addressId' => $contact['id']]);
}
$contactId = ContactModel::create($data);
$data['contactId'] = $contactId;
$addressId = ContactModel::createAddress($data);
if (empty($contactId) || empty($addressId)) {
return $response->withStatus(500)->withJson(['errors' => '[ContactController create] Contact creation has failed']);
}
return $response->withJson(['contactId' => $contactId, 'addressId' => $addressId]);
}
}