Rev 16766 | 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\PlanningObjectives;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Form\Planning\PlanningObjectivesForm;
use LeadersLinked\Library\PlanningPdfOne;
use LeadersLinked\Library\Functions;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Mapper\PlanningTaskMapper;
use LeadersLinked\Mapper\PlanningGoalsMapper;
use LeadersLinked\Mapper\PlanningObjectivesMapper;
use LeadersLinked\Mapper\PlanningTaskMembersMapper;
use LeadersLinked\Mapper\CompanyMapper;
class PlanningObjectivesController extends AbstractActionController
{
/**
*
* @var AdapterInterface
*/
private $adapter;
/**
*
* @var LoggerInterface
*/
private $logger;
/**
*
* @var array
*/
private $config;
/**
*
* @param AdapterInterface $adapter
* @param LoggerInterface $logger
* @param array $config
*/
public function __construct($adapter, $logger, $config)
{
$this->adapter = $adapter;
$this->logger = $logger;
$this->config = $config;
}
public function indexAction()
{
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$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) {
$acl = $this->getEvent()->getViewModel()->getVariable('acl');
$allowEdit = $acl->isAllowed($currentUser->usertype_id, 'planning/objectives/edit');
$allowDelete = $acl->isAllowed($currentUser->usertype_id, 'planning/objectives/delete');
$allowObjective = $acl->isAllowed($currentUser->usertype_id, 'planning/objectives/goals');
$allowObjectiveReport = $acl->isAllowed($currentUser->usertype_id, 'planning/objectives/report');
$allowObjectiveReportAll = $acl->isAllowed($currentUser->usertype_id, 'planning/objectives/reportall');
$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', 'date'];
$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);
$planningGoalsMapper = PlanningGoalsMapper::getInstance($this->adapter);
$planningTaskMapper = PlanningTaskMapper::getInstance($this->adapter);
$paginator = $planningObjectivesMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction, $currentCompany->id);
$items = [];
$records = $paginator->getCurrentItems();
foreach($records as $record)
{
/*
$recordsGoals = $planningGoalsMapper->fetchAll($record->id);
$costObjective=0;
$indicatorObjective=0;
$contador=0;
$countRecordsGoals = count($recordsGoals);
if($countRecordsGoals>0){
foreach($recordsGoals as $record2)
{
$recordsTask = $planningTaskMapper->fetchAll($record2->id);
$costGoals=0;
$indicatorGoals=0;
$countRecordsTask = count($recordsTask);
if($countRecordsTask>0){
foreach($recordsTask as $record3){
$indicatorGoals=$indicatorGoals+$record3->indicator;
$costGoals=$costGoals+$record3->cost;
}
$indicatorObjective=$indicatorObjective+($indicatorGoals/$countRecordsTask);
$costObjective=$costObjective+$costGoals;
$contador++;
}
}
if($indicatorObjective==0){
$indicatorObjective=(-1);
}else{
$indicatorObjective=round($indicatorObjective/$contador,2);
}
}else{
$indicatorObjective=(-2);
}*/
$date='';
if($record->date!=NULL){
$dt = \DateTime::createFromFormat('Y-m-d', $record->date);
$date = $dt->format('d/m/Y');
}
$item = [
'title' => $record->title,
'description' => $record->description,
'date'=> $date,
'cost'=> 0,
'progress'=>0,
'status'=> $record->status,
'link_report_all'> $allowObjectiveReportAll ? $this->url()->fromRoute('planning/objectives/reportall') : '',
'actions' => [
'link_edit' => $allowEdit ? $this->url()->fromRoute('planning/objectives/edit', ['id' => $record->uuid]) : '',
'link_delete' => $allowDelete ? $this->url()->fromRoute('planning/objectives/delete', ['id' => $record->uuid]) : '',
'link_objective' => $allowObjective ? $this->url()->fromRoute('planning/objectives/goals', ['objective_id' => $record->uuid]) : '',
'link_objective_report' => $allowObjectiveReport ? $this->url()->fromRoute('planning/objectives/report', ['id' => $record->uuid]) : '',
]
];
array_push($items, $item);
}
$recordsObjectivesModule = $planningObjectivesMapper->fetchAll($currentCompany->id);
$costModule=0;
$indicatorModule=0;
$contadorTotalModule=0;
$countRecordsObjectives = count($recordsObjectivesModule);
if($countRecordsObjectives>0){
foreach($recordsObjectivesModule as $record)
{
$recordsGoalsModule = $planningGoalsMapper->fetchAll($record->id);
$costObjectiveModule=0;
$indicatorObjectiveModule=0;
$contadorModule=0;
$countRecordsGoalsModule = count($recordsGoalsModule);
if($countRecordsGoalsModule>0){
foreach($recordsGoalsModule as $record2)
{
$recordsTaskModule = $planningTaskMapper->fetchAll($record2->id);
$costGoalsModule=0;
$indicatorGoalsModule=0;
$countRecordsTaskModule = count($recordsTaskModule);
if($countRecordsTaskModule>0){
foreach($recordsTaskModule as $record3){
$indicatorGoalsModule=$indicatorGoalsModule+$record3->indicator;
$costGoalsModule=$costGoalsModule+$record3->cost;
}
$indicatorObjectiveModule=$indicatorObjectiveModule+($indicatorGoalsModule/$countRecordsTaskModule);
$costModule=$costModule+$costGoalsModule;
$contadorModule++;
}
}
if($indicatorObjectiveModule>0){
$indicatorModule=$indicatorModule+($indicatorObjectiveModule/$contadorModule);
$contadorTotalModule++;
}
}
}
if($indicatorModule==0){
$indicatorModule=(-5);
}else{
$indicatorModule=round($indicatorModule/$contadorTotalModule,2);
}
}else{
$indicatorModule=(-3);
}
return new JsonModel([
'success' => true,
'data' => [
'items' => $items,
'total' => $paginator->getTotalItemCount(),
'module'=>[
'costModule' => '$'.$costModule,
'indicatorModule'=>$indicatorModule
]
]
]);
} else {
$formAdd = new PlanningObjectivesForm();
$this->layout()->setTemplate('layout/layout-backend');
$viewModel = new ViewModel();
$viewModel->setTemplate('leaders-linked/planning-objectives/index.phtml');
$viewModel->setVariables([
'formAdd' => $formAdd,
]);
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();
$request = $this->getRequest();
if($request->isPost()) {
$form = new PlanningObjectivesForm();
$dataPost = $request->getPost()->toArray();
$form->setData($dataPost);
if($form->isValid()) {
$dataPost = (array) $form->getData();
$dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : PlanningObjectives::STATUS_INACTIVE;
$dataPost['company_id']=$currentCompany->id;
$hydrator = new ObjectPropertyHydrator();
$planningObjectives = new PlanningObjectives();
$hydrator->hydrate($dataPost, $planningObjectives);
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$result = $planningObjectivesMapper->insert($planningObjectives);
if($result) {
$this->logger->info('Se agrego el objetivo ' . $planningObjectives->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_ADDED'
];
} else {
$data = [
'success' => false,
'data' => $planningObjectivesMapper->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);
}
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$objectives = $planningObjectivesMapper->fetchOneByUuid($uuid);
if (!$objectives) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($objectives->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
if($request->isPost()) {
$form = new PlanningObjectivesForm();
$dataPost = $request->getPost()->toArray();
$dataPost['status'] = isset($dataPost['status']) ? $dataPost['status'] : PlanningObjectives::STATUS_INACTIVE;
$form->setData($dataPost);
if($form->isValid()) {
$dataPost = (array) $form->getData();
$hydrator = new ObjectPropertyHydrator();
$hydrator->hydrate($dataPost, $objectives);
$result = $planningObjectivesMapper->update($objectives);
if($result) {
$this->logger->info('Se actualizo el objetivo ' . $objectives->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_UPDATED'
];
} else {
$data = [
'success' => false,
'data' => $planningObjectivesMapper->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($objectives)
];
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);
}
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$objectives = $planningObjectivesMapper->fetchOneByUuid($uuid);
if (!$objectives) {
$data = [
'success' => false,
'data' => 'ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($objectives->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
if ($request->isPost()) {
$result = $planningObjectivesMapper->delete($objectives->id);
if ($result) {
$this->logger->info('Se borro el objetivo con el titulo ' . $objectives->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
$data = [
'success' => true,
'data' => 'LABEL_RECORD_DELETED'
];
} else {
$data = [
'success' => false,
'data' => $planningObjectivesMapper->getError()
];
return new JsonModel($data);
}
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function reportAction() {
$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);
}
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$recordsObjectives = $planningObjectivesMapper->fetchOneByUuid($uuid);
if (!$recordsObjectives) {
$data = [
'success' => false,
'data' => 'Objetivo ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
if ($recordsObjectives->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
$planningGoalsMapper = PlanningGoalsMapper::getInstance($this->adapter);
$recordsGoals = $planningGoalsMapper->fetchAll($recordsObjectives->id);
$planningTaskMapper = PlanningTaskMapper::getInstance($this->adapter);
$userMapper = UserMapper::getInstance($this->adapter);
$PlanningTaskMemberMapper = PlanningTaskMembersMapper::getInstance($this->adapter);
foreach($recordsGoals as $record2){
$recordsTask = $planningTaskMapper->fetchAll($record2->id);
foreach($recordsTask as $record3){
$recordsTaskMembers = $PlanningTaskMemberMapper->fetchAll($record3->id);
$usersName=[];
foreach($recordsTaskMembers as $record4){
$datosUser = $userMapper->fetchOne($record4->user_id);
$usersName[$record4->id]=$datosUser->first_name.' '.$datosUser->last_name;
}
$record3->who=$usersName;
}
$record2->task = $recordsTask;
}
$items=[
'objetives' => [
'title'=>$recordsObjectives->title,
'description' =>$recordsObjectives->description,
'date'=>$recordsObjectives->date,
'status'=>$recordsObjectives->status,
'goals'=>$recordsGoals
]
];
if ($request->isGet()) {
return $this->renderPdf($currentCompany,$items);
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function reportallAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$recordsObjectives = $planningObjectivesMapper->fetchAll($currentCompany->id);
if (!$recordsObjectives) {
$data = [
'success' => false,
'data' => 'Objetivo ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
$planningGoalsMapper = PlanningGoalsMapper::getInstance($this->adapter);
$planningTaskMapper = PlanningTaskMapper::getInstance($this->adapter);
$items;
$contador=0;
foreach($recordsObjectives as $record){
if ($record->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
$userMapper = UserMapper::getInstance($this->adapter);
$PlanningTaskMemberMapper = PlanningTaskMembersMapper::getInstance($this->adapter);
$recordsGoals = $planningGoalsMapper->fetchAll($record->id);
foreach($recordsGoals as $record2){
$recordsTask = $planningTaskMapper->fetchAll($record2->id);
foreach($recordsTask as $record3){
$recordsTaskMembers = $PlanningTaskMemberMapper->fetchAll($record3->id);
$usersName=[];
foreach($recordsTaskMembers as $record4){
$datosUser = $userMapper->fetchOne($record4->user_id);
$usersName[$record4->id]=$datosUser->first_name.' '.$datosUser->last_name;
}
$record3->who=$usersName;
}
$record2->task = $recordsTask;
}
/* $item=[
$contador => [
'title'=>$record->title,
'description' =>$record->description,
'date'=>$record->date,
'status'=>$record->status,
'goals'=>$recordsGoals
]
];
array_push($items, $item);
$contador++;*/
$record->goals=$recordsGoals;
}
if ($request->isGet()) {
return $this->renderPdf($currentCompany,$recordsObjectives,2);
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function matrizAction() {
$currentUserPlugin = $this->plugin('currentUserPlugin');
$currentUser = $currentUserPlugin->getUser();
$currentCompany = $currentUserPlugin->getCompany();
$request = $this->getRequest();
$planningObjectivesMapper = PlanningObjectivesMapper::getInstance($this->adapter);
$recordsObjectives = $planningObjectivesMapper->fetchAll($currentCompany->id);
if (!$recordsObjectives) {
$data = [
'success' => false,
'data' => 'Objetivo ERROR_RECORD_NOT_FOUND'
];
return new JsonModel($data);
}
$planningGoalsMapper = PlanningGoalsMapper::getInstance($this->adapter);
$planningTaskMapper = PlanningTaskMapper::getInstance($this->adapter);
$items;
$contador=0;
foreach($recordsObjectives as $record){
if ($record->company_id != $currentCompany->id) {
return new JsonModel([
'success' => false,
'data' => 'ERROR_UNAUTHORIZED'
]);
}
$recordsGoals = $planningGoalsMapper->fetchAll($record->id);
foreach($recordsGoals as $record2){
$recordsTask = $planningTaskMapper->fetchAll($record2->id);
$record2->task = $recordsTask;
}
/* $item=[
$contador => [
'title'=>$record->title,
'description' =>$record->description,
'date'=>$record->date,
'status'=>$record->status,
'goals'=>$recordsGoals
]
];
array_push($items, $item);
$contador++;*/
$record->goals=$recordsGoals;
}
if ($request->isGet()) {
return $this->renderPdf($currentCompany,$recordsObjectives,3);
} else {
$data = [
'success' => false,
'data' => 'ERROR_METHOD_NOT_ALLOWED'
];
return new JsonModel($data);
}
return new JsonModel($data);
}
public function renderPDF($currentCompany,$items,$switch=1) {
$pdf = new PlanningPdfOne();
$pdf->header = '';
$pdf->footer = '';
if ($currentCompany) {
//get company Data
$companyMapper = CompanyMapper::getInstance($this->adapter);
$company = $companyMapper->fetchOne($currentCompany->id);
$target_path = $this->config['leaderslinked.fullpath.company'] . DIRECTORY_SEPARATOR . $company->uuid;
$pdf->header = $company->header ? $target_path . DIRECTORY_SEPARATOR . $company->header : '';
$pdf->footer = $company->footer ? $target_path . DIRECTORY_SEPARATOR . $company->footer : '';
}
$pdf->SetMargins(10,200,10);
$pdf->SetAutoPageBreak(true,40);
$pdf->AliasNbPages();
if($switch==3){
$pdf->borderTableMatriz('Planificacion - Objetivos y metas',$items);
}else if($switch==2){
$pdf->borderTableAll('Planificacion - Objetivos y metas',$items);
}else{
$pdf->borderTable('Planificacion - Objetivos y metas',$items);
}
return $pdf->Output();
}
}