Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 2219 | Autoría | 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\JsonModel;

class PlanningObjectivesAndGoalsController 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() {

        return new JsonModel([
            'success' => false,
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
        ]);
    }

}        $request = $this->getRequest();
        if($request->isPost()) {
            $form = new  PlanningObjectivesAndGoalsForm();
            $dataPost = $request->getPost()->toArray();
            
            $form->setData($dataPost);
            
            if($form->isValid()) {
                $dataPost = (array) $form->getData();
                $dataPost['status']='a';
                $dataPost['id_company']=$currentCompany;

                $hydrator = new ObjectPropertyHydrator();
                $planningObjectivesAndGoals = new PlanningObjectivesAndGoals();
                $hydrator->hydrate($dataPost, $planningObjectivesAndGoals);
                
                $PlanningObjectivesAndGoalsMapper = PlanningObjectivesAndGoalsMapper::getInstance($this->adapter);
                $result = $PlanningObjectivesAndGoalsMapper->insert($PlanningObjectivesAndGoals);
                
                if($result) {
                    $this->logger->info('Se agrego el objetivo ' . $PlanningObjectivesAndGoals->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
                    
                    $data = [
                        'success'   => true,
                        'data'   => 'LABEL_RECORD_ADDED'
                    ];
                } else {
                    $data = [
                        'success'   => false,
                        'data'      => $PlanningObjectivesAndGoalsMapper->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);
        }

        

        $PlanningObjectivesAndGoalsMapper = PlanningObjectivesAndGoalsMapper::getInstance($this->adapter);
        $objective = $PlanningObjectivesAndGoalsMapper->fetchOneByUuid($uuid);
       
        if (!$objective) {
            $data = [
                'success' => false,
                'data' => 'ERROR_RECORD_NOT_FOUND'
            ];

            return new JsonModel($data);
        }
       
        if ($objective->id_company != $currentCompany->id) {
            return new JsonModel([
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ]);
        } 

        if($request->isPost()) {
            $form = new  PlanningObjectivesAndGoalsForm();
            $dataPost = $request->getPost()->toArray();
            
            $form->setData($dataPost);
            
            if($form->isValid()) {
                $dataPost = (array) $form->getData();
                
                $hydrator = new ObjectPropertyHydrator();
                $hydrator->hydrate($dataPost, $objective);
                $result = $PlanningObjectivesAndGoalsMapper->update($objectivesMapper);
                
                if($result) {
                    $this->logger->info('Se actualizo el objetivo ' . $objectivesMapper->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
                    
                    $data = [
                        'success' => true,
                        'data' => 'LABEL_RECORD_UPDATED'
                    ];
                } else {
                    $data = [
                        'success'   => false,
                        'data'      => $PlanningObjectivesAndGoalsMapper->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($objectivesMapper)
            ];
            
            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()->id;
        $currentUser = $currentUserPlugin->getUser();

        $request = $this->getRequest();
        $uuid = $this->params()->fromRoute('id');
        

        if (!$uuid) {
            $data = [
                'success' => false,
                'data' => 'ERROR_INVALID_PARAMETER'
            ];

            return new JsonModel($data);
        }

       

        $PlanningObjectivesAndGoalsMapper = PlanningObjectivesAndGoalsMapper::getInstance($this->adapter);
        $objectivesMapper = $PlanningObjectivesAndGoalsMapper->fetchOneByUuid($uuid);
       
        if (!$objectivesMapper) {
            $data = [
                'success' => false,
                'data' => 'ERROR_RECORD_NOT_FOUND'
            ];

            return new JsonModel($data);
        }
       
        if ($objectivesMapper->id_company != $currentCompany) {
            return new JsonModel([
                'success' => false,
                'data' => 'ERROR_UNAUTHORIZED'
            ]);
        } 
    
        if ($request->isPost()) {


            $result = $PlanningObjectivesAndGoalsMapper->delete($objectivesMapper->id);
            if ($result) {
                $this->logger->info('Se borro el objetivo con el titulo ' . $objectivesMapper->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);

                $data = [
                    'success' => true,
                    'data' => 'LABEL_RECORD_DELETED'
                ];
            } else {

                $data = [
                    'success' => false,
                    'data' => $PlanningObjectivesAndGoalsMapper->getError()
                ];

                return new JsonModel($data);
            }
        } else {
            $data = [
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ];

            return new JsonModel($data);
        }

        return new JsonModel($data);
    }
    
    
   
}