Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 7515 | 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\Cache\Storage\Adapter\AbstractAdapter;
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\HighPerformanceTeamsGroupsViewTopic;
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
use LeadersLinked\Form\HighPerformanceTeamsGroupsViewTopicForm;
use LeadersLinked\Library\Functions;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsViewTopicMapper;
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMapper;

use LeadersLinked\Mapper\CompanyMapper;
use LeadersLinked\Mapper\CompanyUserMapper;



class HighPerformanceTeamsGroupsViewTopicController 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();
        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, 'high-performance-teams/groups/edit');
                $allowDelete = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/delete');
                $allowView = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/view');
                

                $search = $this->params()->fromQuery('search', []);
                $search = empty($search['value']) ? '' : filter_var($search['value'], FILTER_SANITIZE_STRING);
                
                
                $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(filter_var( $order[0]['dir'], FILTER_SANITIZE_STRING));
    
                $fields =  ['title', 'date'];
                $order_field = isset($fields[$order_field]) ? $fields[$order_field] : 'title';
                
                if(!in_array($order_direction, ['ASC', 'DESC'])) {
                    $order_direction = 'ASC';
                }

                $highPerformanceTeamsGroupsViewTopicMapper = HighPerformanceTeamsGroupsViewTopicMapper::getInstance($this->adapter);
                $paginator = $highPerformanceTeamsGroupsViewTopicMapper->fetchAllDataTable($search, $page, $records_x_page, $order_field, $order_direction, $currentCompany->id);
               
                $items = [];
            
                $records = $paginator->getCurrentItems();
               
              
                foreach($records as $record)
                {

                   

                    $item = [
                        'title' => $record->title,
                        'description' => $record->description,
                        'status'=> $record->status,
                        'actions' => [
                            'link_edit' => $allowEdit ? $this->url()->fromRoute('high-performance-teams/groups/edit', ['id' => $record->uuid]) : '',
                            'link_delete' => $allowDelete ? $this->url()->fromRoute('high-performance-teams/groups/delete', ['id' => $record->uuid]) : '',
                            'link_view' => $allowView ? $this->url()->fromRoute('high-performance-teams/groups/view', ['id' => $record->uuid]) : '',
                        ]

                    ];
                    
                    array_push($items, $item);
                    
                }
                 
                return new JsonModel([
                    'success' => true,
                    'data' => [
                        'items' => $items,
                        'total' => $paginator->getTotalItemCount(),
                    ]
                ]);
                
            } 
        } 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  HighPerformanceTeamsGroupsViewTopicForm($this->adapter);
            $dataPost = $request->getPost()->toArray();
            
            $form->setData($dataPost);
            
            if($form->isValid()) {
                $dataPost = (array) $form->getData();
                $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : HighPerformanceTeamsGroupsViewTopic::STATUS_INACTIVE;
                

                $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
                $group = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($dataPost['group_id']);
               
                if (!$group) {
                    $data = [
                        'success' => false,
                        'data' => 'ERROR_RECORD_NOT_FOUND'
                    ];
        
                    return new JsonModel($data);
                }
               
                if ($group->company_id != $currentCompany->id) {
                    return new JsonModel([
                        'success' => false,
                        'data' => 'ERROR_UNAUTHORIZED'
                    ]);
                } 
        
                $dataPost['group_id']=$group->id;

                $hydrator = new ObjectPropertyHydrator();
                $highPerformanceTeamsGroupsViewTopic = new HighPerformanceTeamsGroupsViewTopic();
                $hydrator->hydrate($dataPost, $highPerformanceTeamsGroupsViewTopic);
                
                $highPerformanceTeamsGroupsViewTopicMapper = HighPerformanceTeamsGroupsViewTopicMapper::getInstance($this->adapter);
                $result = $highPerformanceTeamsGroupsViewTopicMapper->insert($highPerformanceTeamsGroupsViewTopic);
                
                if($result) {

                    $this->logger->info('Se agrego el topic del grupo ' . $highPerformanceTeamsGroupsViewTopic->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
                    
                    $data = [
                        'success'   => true,
                        'data'   => 'LABEL_RECORD_ADDED'
                    ];
                } else {
                    $data = [
                        'success'   => false,
                        'data'      => $highPerformanceTeamsGroupsViewTopicMapper->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);
        }

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

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

        if($request->isPost()) {
            $form = new  HighPerformanceTeamsGroupsViewTopicForm($this->adapter);
            $dataPost = $request->getPost()->toArray();
            $dataPost['status'] =  isset($dataPost['status']) ? $dataPost['status'] : HighPerformanceTeamsGroupsViewTopic::STATUS_INACTIVE;
            $form->setData($dataPost);
            
            if($form->isValid()) {
                $dataPost = (array) $form->getData();
                
                $hydrator = new ObjectPropertyHydrator();
                $hydrator->hydrate($dataPost, $group);
                $result = $highPerformanceTeamsGroupsViewTopicMapper->update($group);
                
                if($result) {
                    $this->logger->info('Se actualizo el grupo de alto rendimiento ' . $group->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);
                    
                    $data = [
                        'success' => true,
                        'data' => 'LABEL_RECORD_UPDATED'
                    ];
                } else {
                    $data = [
                        'success'   => false,
                        'data'      => $highPerformanceTeamsGroupsViewTopicMapper->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($group)
            ];
            
            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);
        }

       

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

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


            $result = $highPerformanceTeamsGroupsViewTopicMapper->delete($group->id);
            if ($result) {
                $this->logger->info('Se borro el grupo de alto rendimiento  ' . $group->title, ['user_id' => $currentUser->id, 'ip' => Functions::getUserIP()]);

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

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

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

            return new JsonModel($data);
        }

        return new JsonModel($data);
    }
    
    


}