Rev 16768 | AutorÃa | Ultima modificación | Ver Log |
<?php
declare(strict_types=1);
namespace LeadersLinked\Controller;
use Laminas\Authentication\AuthenticationService;
use Laminas\Authentication\Result as AuthResult;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\Mvc\I18n\Translator;
use Laminas\Log\LoggerInterface;
use Laminas\View\Model\ViewModel;
use Laminas\View\Model\JsonModel;
use LeadersLinked\Model\PlanningGoals;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Form\Planning\PlanningGoalsForm;
use LeadersLinked\Library\Functions;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Mapper\PlanningTaskMapper;
use LeadersLinked\Mapper\PlanningGoalsMapper;
use LeadersLinked\Mapper\PlanningObjectivesMapper;
class PlanningGoalsController extends AbstractActionController
{
/**
*
* @var \Laminas\Db\Adapter\AdapterInterface
*/
private $adapter;
/**
*
* @var \LeadersLinked\Cache\CacheInterface
*/
private $cache;
/**
*
* @var \Laminas\Log\LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @var \Laminas\Mvc\I18n\Translator
*/
private $translator;
/**
*
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
* @param \LeadersLinked\Cache\CacheInterface $cache
* @param \Laminas\Log\LoggerInterface LoggerInterface $logger
* @param array $config
* @param \Laminas\Mvc\I18n\Translator $translator
*/
public function __construct($adapter, $cache, $logger, $config, $translator)
{
$this->adapter = $adapter;
$this->cache = $cache;
$this->logger = $logger;
$this->config = $config;
$this->translator = $translator;
}
/*
public function idObjective($uuid){
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentCompany = $currentUserPlugin->getCompany();
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$objectives = $planningObjectivesMapper->fetchOneByUuid($uuid);
if (!$objectives) {
return false;
}else{
if($objectives->company_id==$currentCompany->id){
return $objectives;
}else{
return false;
}
}
}*/
public function indexAction()
{
$objective_uuid = $this->params()->fromRoute('objective_id');
if(!$objective_uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$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) {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'planning/objectives/goals/edit');
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'planning/objectives/goals/delete');
$allowGoals = $acl->isAllowed($currentUser->usertype_id, 'planning/objectives/goals/task');
$search = $this->params()->fromQuery('search', []);
$search = empty($search['value']) ? '' : Functions::sanitizeFilterString($search['value']);
$records_x_page = intval($this->params()->fromQuery('length', 10), 10);
$page = (intval($this->params()->fromQuery('start', 1), 10)/$records_x_page)+1;
$order = $this->params()->fromQuery('order', []);
$order_field = empty($order[0]['column']) ? 99 : intval($order[0]['column'], 10);
$order_direction = empty($order[0]['dir']) ? 'ASC' : strtoupper(Functions::sanitizeFilterString($order[0]['dir']));
$fields = ['title'];
$order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
if(!in_array($order_direction, ['ASC', 'DESC'])) {
$order_direction = 'ASC';
}
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$objectives = $planningObjectivesMapper->fetchOneByUuid($objective_uuid);
if (!$objectives) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if($objectives->company_id!=$currentCompany->id){
$data = [
'success' => false,
'data' => 'ERROR_UNAUTHORIZED',
];
return new JsonModel($data);
}
$planningGoalsMapper = PlanningGoalsMapper::getInstance($this->adapter);
$paginator = $planningGoalsMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction, $objectives->id);
$items = [];
$records = $paginator->getCurrentItems();
$planningTaskMapper = PlanningTaskMapper::getInstance($this->adapter);
foreach($records as $record)
{
$recordsTask = $planningTaskMapper->fetchAll($record->id);
$costGoals=0;
$indicatorGoals=0;
foreach($recordsTask as $record2){
$indicatorGoals=$indicatorGoals+$record2->indicator;
$costGoals=$costGoals+$record2->cost;
}
$countRecordsTask = count($recordsTask);
if($countRecordsTask>0){
$indicatorGoals=round($indicatorGoals/$countRecordsTask, 2);
}else{
$indicatorGoals=-1;
}
$item = [
'title' => $record->title,
'description' => $record->description,
'progress'=>$indicatorGoals,
'cost' => $costGoals,
/*'DT_RowData'=>[
'id' =>$record->uuid,
],
'DT_RowClass'=> 'Goals',*/
'status'=>$record->status,
'actions' => [
'link_edit' => $allowEdit ? $this->url()->fromRoute('planning/objectives/goals/edit', ['objective_id' => $objective_uuid,'id' => $record->uuid]) : '',
'link_delete' => $allowDelete ? $this->url()->fromRoute('planning/objectives/goals/delete', ['objective_id' => $objective_uuid,'id' => $record->uuid]) : '',
'link_goals' => $allowGoals ? $this->url()->fromRoute('planning/objectives/goals/task', ['objective_id' => $objective_uuid,'goal_id' => $record->uuid]) : ''
]
];
array_push($items, $item);
}
$recordsGoals = $planningGoalsMapper->fetchAll($objectives->id);
$costObjective=0;
$indicatorObjective=0;
$contador=0;
$countRecordsGoals = count($recordsGoals);
if($countRecordsGoals>0){
$planningTaskMapper = PlanningTaskMapper::getInstance($this->adapter);
foreach($recordsGoals as $record)
{
$recordsTask = $planningTaskMapper->fetchAll($record->id);
$costGoals=0;
$indicatorGoals=0;
$countRecordsTask = count($recordsTask);
if($countRecordsTask>0){
foreach($recordsTask as $record2){
$indicatorGoals=$indicatorGoals+$record2->indicator;
$costGoals=$costGoals+$record2->cost;
}
$indicatorObjective=$indicatorObjective+($indicatorGoals/$countRecordsTask);
$costObjective=$costObjective+$costGoals;
$contador++;
}
}
if($indicatorObjective==0){
$indicatorObjective=(-1);
}else{
$indicatorObjective=round($indicatorObjective/$contador,2);
}
}else{
$indicatorObjective=(-2);
}
$date='Sin fecha';
if($objectives->date!=NULL){
$dt = \DateTime::createFromFormat('Y-m-d', $objectives->date);
$date = $dt->format('d/m/Y');
}
return new JsonModel([
'success' => true,
'data' => [
'items' => $items,
'total' => $paginator->getTotalItemCount(),
'objective' => [
'titleObjective' =>$objectives->title,
'descriptionObjective' =>$objectives->description,
'dateObjective' =>$date,
'costObjective' =>'$'.$costObjective,
'indicatorObjective'=>$indicatorObjective,
'statusObjective'=>$objectives->status=='a'?'LABEL_ACTIVE':'LABEL_INACTIVE',
]
]
]);
} else {
$formAdd = new PlanningGoalsForm();
$this->layout()->setTemplate('layout/layout-backend');
$viewModel = new ViewModel();
$viewModel->setTemplate('leaders-linked/planning-goals/index.phtml');
$viewModel->setVariables([
'formAdd' => $formAdd,
'uuid'=> $objective_uuid
]);
return $viewModel ;
}
} else {
return new JsonModel([
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
]);
}
}
public function addAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$objective_uuid = $this->params()->fromRoute('objective_id');
if(!$objective_uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$request = $this->getRequest();
if($request->isPost()) {
$form = new PlanningGoalsForm();
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if($form->isValid()) {
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$objectives = $planningObjectivesMapper->fetchOneByUuidAndCompanyId($objective_uuid, $currentCompany->id);
if (!$objectives) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if($objectives->company_id!=$currentCompany->id){
$data = [
'success' => false,
'data' => 'ERROR_UNAUTHORIZED',
];
return new JsonModel($data);
}
$dataPost = (array) $form->getData();
$dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : PlanningGoals::STATUS_INACTIVE;
$dataPost['objective_id']=$objectives->id;
$hydrator = new ObjectPropertyHydrator();
$planningGoals = new PlanningGoals();
$hydrator->hydrate($dataPost, $planningGoals);
$planningGoalsMapper = PlanningGoalsMapper::getInstance($this->adapter);
$result = $planningGoalsMapper->insert($planningGoals);
if($result) {
$this->logger->info('Se agrego la meta ' . $planningGoals->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_ADDED'
];
} else {
$data = [
'success' => false,
'data' => $planningGoalsMapper->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();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
$uuid = $this->params()->fromRoute('id');
if(!$uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$objective_uuid = $this->params()->fromRoute('objective_id');
if(!$objective_uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$objectives = $planningObjectivesMapper->fetchOneByUuid($objective_uuid);
if (!$objectives) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if($objectives->company_id!=$currentCompany->id){
$data = [
'success' => false,
'data' => 'ERROR_UNAUTHORIZED',
];
return new JsonModel($data);
}
$planningGoalsMapper = PlanningGoalsMapper::getInstance($this->adapter);
$goals = $planningGoalsMapper->fetchOneByUuid($uuid);
if (!$goals) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if($request->isPost()) {
$form = new PlanningGoalsForm();
$dataPost = $request->getPost()->toArray();
$dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : PlanningGoals::STATUS_INACTIVE;
$form->setData($dataPost);
if($form->isValid()) {
$dataPost = (array) $form->getData();
$hydrator = new ObjectPropertyHydrator();
$hydrator->hydrate($dataPost, $goals);
$result = $planningGoalsMapper->update($goals);
if($result) {
$this->logger->info('Se actualizo la meta ' . $goals->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_UPDATED'
];
} else {
$data = [
'success' => false,
'data' => $planningGoalsMapper->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($goals)
];
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');
$currentCompany = $currentUserPlugin->getCompany();
$currentUser = $currentUserPlugin->getUser();
$request = $this->getRequest();
$uuid = $this->params()->fromRoute('id');
if (!$uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$objective_uuid = $this->params()->fromRoute('objective_id');
if(!$objective_uuid) {
$data = [
'success' => false,
'data' => 'ERROR_INVALID_PARAMETER'
];
return new JsonModel($data);
}
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$objectives = $planningObjectivesMapper->fetchOneByUuid($objective_uuid);
if (!$objectives) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if($objectives->company_id!=$currentCompany->id){
$data = [
'success' => false,
'data' => 'ERROR_UNAUTHORIZED',
];
return new JsonModel($data);
}
$planningGoalsMapper = PlanningGoalsMapper::getInstance($this->adapter);
$goals = $planningGoalsMapper->fetchOneByUuid($uuid);
if (!$goals) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($request->isPost()) {
$result = $planningGoalsMapper->delete($goals->id);
if ($result) {
$this->logger->info('Se borro la meta con el titulo ' . $goals->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_DELETED'
];
} else {
$data = [
'success' => false,
'data' => $planningGoalsMapper->getError()
];
return new JsonModel($data);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
}