Rev 1225 | Rev 14281 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Controller;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\ViewModel;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Library\Functions;
use LeadersLinked\Mapper\BehaviorMapper;
use LeadersLinked\Form\BehaviorForm;
use LeadersLinked\Model\Behavior;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
class BehaviorsController extends AbstractActionController {
/**
*
* @var AdapterInterface
*/
private $adapter;
/**
*
* @var AbstractAdapter
*/
private $cache;
/**
*
* @var LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @param AdapterInterface $adapter
* @param AbstractAdapter $cache
* @param LoggerInterface $logger
* @param array $config
*/
public function __construct($adapter, $cache, $logger, $config) {
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
}
public function indexAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
$request = $this->getRequest();
if ($request->isGet()) {
$headers = $request->getHeaders();
$isJson = false;
if ($headers->has('Accept')) {
$accept = $headers->get('Accept');
$prioritized = $accept->getPrioritized();
foreach ($prioritized as $key => $value) {
$raw = trim($value->getRaw());
if (!$isJson) {
$isJson = strpos($raw, 'json');
}
}
}
if ($isJson) {
$behaviorMapper = BehaviorMapper::getInstance($this->adapter);
$records = $currentCompany ?
$behaviorMapper->fetchAllCompanyId($currentCompany->id) :
$behaviorMapper->fetchAllByDefault();
$items = [];
foreach ($records as $record) {
$item = [
'uuid' => $record->uuid,
'description' => $record->description,
];
array_push($items, $item);
}
return new JsonModel([
'success' => true,
'data' => $items
]);
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
;
}
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
;
}
}
public function addAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
if ($request->isPost()) {
$form = new BehaviorForm();
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if ($form->isValid()) {
$dataPost = (array) $form->getData();
$hydrator = new ObjectPropertyHydrator();
$behavior = new Behavior();
$hydrator->hydrate($dataPost, $behavior);
if (!$behavior->status) {
$behavior->status = Behavior::STATUS_INACTIVE;
}
if ($currentCompany) {
$behavior->company_id = $currentCompany->id;
}
$behaviorMapper = BehaviorMapper::getInstance($this->adapter);
$recordExists = $behaviorMapper->fetchOneByDescription($dataPost['description'], $currentCompany ? $currentCompany->id : null);
if ($recordExists) {
$data = [
'success' => false,
'message' => 'El registro ya existe'
];
} else {
$result = $behaviorMapper->insert($behavior);
if ($result) {
$this->logger->info('Se agrego la conducta observable ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$recordExists = $behaviorMapper->fetchOneByDescription($dataPost['description'], $currentCompany ? $currentCompany->id : null);
if ($recordExists) {
$data = [
'success' => true,
'uuid' => $recordExists->uuid
];
}
} else {
$data = [
'success' => false,
'data' => $behaviorMapper->getError()
];
}
}
return new JsonModel($data);
} else {
$messages = [];
$form_messages = (array) $form->getMessages();
foreach ($form_messages as $fieldname => $field_messages) {
$messages[$fieldname] = array_values($field_messages);
}
return new JsonModel([
'success' => false,
'data' => $messages
]);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function editAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$id = $this->params()->fromRoute('id');
if (!$id) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$behaviorMapper = BehaviorMapper::getInstance($this->adapter);
$behavior = $behaviorMapper->fetchOneByUuid($id);
if (!$behavior) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($request->isPost()) {
$form = new BehaviorForm();
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if ($form->isValid()) {
$dataPost = (array) $form->getData();
$hydrator = new ObjectPropertyHydrator();
$hydrator->hydrate($dataPost, $behavior);
if (!$behavior->status) {
$behavior->status = Behavior::STATUS_INACTIVE;
}
$result = $behaviorMapper->update($behavior);
if ($result) {
$this->logger->info('Se actualizo la conducta observable ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_UPDATED'
];
} else {
$data = [
'success' => false,
'data' => $behaviorMapper->getError()
];
}
return new JsonModel($data);
} else {
$messages = [];
$form_messages = (array) $form->getMessages();
foreach ($form_messages as $fieldname => $field_messages) {
$messages[$fieldname] = array_values($field_messages);
}
return new JsonModel([
'success' => false,
'data' => $messages
]);
}
} else if ($request->isGet()) {
$hydrator = new ObjectPropertyHydrator();
$data = [
'success' => true,
'data' => $hydrator->extract($behavior)
];
return new JsonModel($data);
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function deleteAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$id = $this->params()->fromRoute('id');
if (!$id) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$behaviorMapper = BehaviorMapper::getInstance($this->adapter);
$behavior = $behaviorMapper->fetchOneByUuid($id);
if (!$behavior) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($request->isPost()) {
$result = $behaviorMapper->delete($behavior);
if ($result) {
$this->logger->info('Se borro el tipo de competencia ' . $behavior->description, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_DELETED'
];
} else {
$data = [
'success' => false,
'data' => $behaviorMapper->getError()
];
return new JsonModel($data);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
}