Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3262 | Rev 3302 | 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\Model\Page;
use LeadersLinked\Mapper\NotificationMapper;
use LeadersLinked\Mapper\CompanyMapper;
use LeadersLinked\Mapper\CompanyUserMapper;
use LeadersLinked\Model\Company;
use LeadersLinked\Mapper\PageMapper;
use LeadersLinked\Mapper\MessageMapper;
use LeadersLinked\Mapper\UserMapper;
use LeadersLinked\Mapper\UserProfileMapper;
use LeadersLinked\Mapper\CompanyUserRoleMapper;
use LeadersLinked\Model\Role;
use LeadersLinked\Library\Functions;
use LeadersLinked\Mapper\ConnectionMapper;
use LeadersLinked\Mapper\LocationMapper;
use LeadersLinked\Mapper\PostMapper;
use LeadersLinked\Mapper\ProfileVisitMapper;
use LeadersLinked\Model\Post;
use LeadersLinked\Mapper\UtilMapper;
use LeadersLinked\Mapper\FeedMapper;
use LeadersLinked\Model\Feed;
use LeadersLinked\Model\User;
use LeadersLinked\Model\Connection;

class HomeController 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');
        if (!$currentUserPlugin->hasIdentity()) {
            return $this->redirect()->toRoute('dashboard');
        } else {
            return $this->redirect()->toRoute('signin');
        }
    }




    public function privacyPolicyAction()
    {
        $pageMapper = PageMapper::getInstance($this->adapter);
        $page = $pageMapper->fetchOne(Page::PAGE_ID_PRIVACY_POLICY);

        $this->layout()->setTemplate('layout/layout.phtml');
        $viewModel = new ViewModel();
        $viewModel->setTemplate('leaders-linked/home/privacy-policy.phtml');
        $viewModel->setVariable('page', $page);
        return $viewModel;
    }

    public function cookiesAction()
    {
        $pageMapper = PageMapper::getInstance($this->adapter);
        $page = $pageMapper->fetchOne(Page::PAGE_ID_COOKIES);

        $this->layout()->setTemplate('layout/layout.phtml');
        $viewModel = new ViewModel();
        $viewModel->setTemplate('leaders-linked/home/cookies.phtml');
        $viewModel->setVariable('page', $page);
        return $viewModel;
    }

    public function professionalismPolicyAction()
    {
        //

        $pageMapper = PageMapper::getInstance($this->adapter);
        $page = $pageMapper->fetchOne(Page::PAGE_ID_PROFESSIONALISM_POLICY);

        $this->layout()->setTemplate('layout/layout.phtml');
        $viewModel = new ViewModel();
        $viewModel->setTemplate('leaders-linked/home/professionalism-policy');
        $viewModel->setVariable('page', $page);
        return $viewModel;
    }


    public function termsAndConditionsAction()
    {
        $pageMapper = PageMapper::getInstance($this->adapter);
        $page = $pageMapper->fetchOne(Page::PAGE_ID_TERMS_AND_CONDITIONS);

        $this->layout()->setTemplate('layout/layout.phtml');
        $viewModel = new ViewModel();
        $viewModel->setTemplate('leaders-linked/home/terms-and-conditions.phtml');
        $viewModel->setVariable('page', $page);
        return $viewModel;
    }

    public function checkSessionAction()
    {

        $request = $this->getRequest();
        if ($request->isGet()) {

            $currentUserPlugin = $this->plugin('currentUserPlugin');
            if (!$currentUserPlugin->hasIdentity()) {
                $flashMessenger = $this->plugin('FlashMessenger');
                $flashMessenger->addErrorMessage('ERROR_SESSION_NOT_FOUND');

                $response = [
                    'success' => false,
                    'data' => [
                        'message' =>  'ERROR_SESSION_NOT_FOUND',
                        'url' => $this->url()->fromRoute('signout')
                    ]
                ];

                return new JsonModel($response);
            }

            $currentUser = $currentUserPlugin->getUser();


            if ($currentUser->last_activity_on) {
                $last_activity_on = strtotime($currentUser->last_activity_on);
            } else {
                $last_activity_on = strtotime('-1 day');
            }

            $expiry_time = $last_activity_on + $this->config['leaderslinked.security.last_activity_expired'];
            if (time() > $expiry_time) {
                //$flashMessenger = $this->plugin('FlashMessenger');
                //$flashMessenger->addErrorMessage('ERROR_SESSION_EXPIRED');

                $response = [
                    'success' => false,
                    'data' => [
                        'message' => 'ERROR_SESSION_EXPIRED',
                        'url' => $this->url()->fromRoute('signout')
                    ]
                ];
            } else {
                $notificationMapper = NotificationMapper::getInstance($this->adapter);
                $total_notifications = $notificationMapper->fetchUnreadNotificationsCount($currentUser->id);

                $messageMapper = MessageMapper::getInstance($this->adapter);
                $total_messages =  $messageMapper->fetchCountUnreadMessagesReceiverId($currentUser->id);
                $response = [
                    'success' => true,
                    'data' => [
                        'total_notifications' => $total_notifications,
                        'total_messages' => $total_messages
                    ]
                ];
            }
        } else {
            $response = [
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ];
        }

        return new JsonModel($response);
    }

   
    public function postAction()
    {
        $id = $this->params()->fromRoute('id');

        $postMapper = PostMapper::getInstance($this->adapter);
        $post = $postMapper->fetchOneByUuid($id);

        if (!$post || $post->status != Post::STATUS_ACTIVE) {
            $flashMessenger = $this->plugin('FlashMessenger');

            if (!$id) {
                $flashMessenger->addErrorMessage('ERROR_POST_NOT_AVAILABLE');
                return $this->redirect()->toRoute('dashboard');
            }
        }


        $this->layout()->setTemplate('layout/layout.phtml');
        $viewModel = new ViewModel();
        $viewModel->setTemplate('leaders-linked/home/post.phtml');
        $viewModel->setVariables([
            'post' => $post,
            'id' => $post->id,
            'uuid' => $post->uuid,
            'title' => $post->title,
            'description' => $post->description,
            'url' => $post->url,
            'date' => $post->date,
            'status' => $post->status,
            'image' => $post->image,
            'file' => $post->file,
            'added_on' => $post->added_on,
            'share_external_url' => $this->url()->fromRoute('share',  ['type' => 'post', 'code' => $post->uuid]),
            
        ]);
        return $viewModel;
    }
    
    public function shareAction()
    {
        $request = $this->getRequest();
        if ($request->isGet()) {
            $currentUserPlugin = $this->plugin('currentUserPlugin');
            $currentUser = $currentUserPlugin->getUser();
            
            $code = $this->params()->fromRoute('code');
            $type = $this->params()->fromRoute('type');
       
           
            
            if(strpos($_SERVER['SERVER_PROTOCOL'], 'HTTPS') === false) {
                $base_share_image = 'http://' . $_SERVER['HTTP_HOST']; 
            } else {
                $base_share_image = 'https://' . $_SERVER['HTTP_HOST'];
            }
            
        
            
            
            $share_image        = $base_share_image . '/images/ll-logo.png';
            $share_title        = '';
            $share_description  = '';
            /*
            [fullpath]
            chat=data/storage/chat/
            group=data/storage/group/
            user=data/storage/user/
            image=data/storage/image/
            job=data/storage/job/
            company=data/storage/company/
            feed=data/storage/feed/
            post=data/storage/post/
            /storage/type/feed/code/ef1038de-4f26-4253-a886-e125784ab604/filename/th-2400499377.png/
            *
            */
            
            if($type == 'feed' && $code ) {
                $feedMapper =  FeedMapper::getInstance($this->adapter);
                $feed = $feedMapper->fetchOneByUuid($code);
                
                if($feed && $feed->status == Feed::STATUS_PUBLISHED) {
                    $share_title = $feed->title ? $feed->title : $feed->description;
                    $share_description = $feed->description;
      
                    $image_name = '';
                    if($feed->file_type == Feed::FILE_TYPE_IMAGE) {
                        
                        $image_name = $feed->file_name;
                        
                    } else  if($feed->file_image_preview) {
                        $image_name = $feed->file_image_preview;
                    }
                        
         
                    
                    if( $image_name ) {
                        
                        $source = $this->config['leaderslinked.fullpath.feed'] . $feed->uuid . DIRECTORY_SEPARATOR . $image_name;
                        
                
                        $target_path = 'public' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'feed'. DIRECTORY_SEPARATOR . $feed->uuid;
                        
                        if(!file_exists($target_path)) {
                            mkdir($target_path, 0755, true);
                        }
                        
                        
                        
                        $target = $target_path . DIRECTORY_SEPARATOR . $image_name;
                        
               
                        
                        if(!file_exists($target)) {
                            
                            copy($source, $target);
                            $share_image =  $base_share_image . '/images/feed/' . $feed->uuid . '/' . $image_name;
                            
                        } else {
                            $share_image =  $base_share_image . '/images/feed/' . $feed->uuid . '/' . $image_name;
                            
                        }
                        
                        
                        
                        
                    }
                  
                }
                
                
            } else if ($type == 'post' && $code) {
                
                $postMapper = PostMapper::getInstance($this->adapter);
                $post = $postMapper->fetchOneByUuid($code);
                
                if($post && $post->status == Post::STATUS_ACTIVE) {
                    $share_title = $post->title;
                    $share_description = $post->description;  
                    
                    
                    if($post->image) {
                        $source = $this->config['leaderslinked.fullpath.post'] . $post->uuid . DIRECTORY_SEPARATOR . $post->image;
                        
                        
                        $target_path = 'public' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'post'. DIRECTORY_SEPARATOR . $post->uuid;
                        
                        if(!file_exists($target_path)) {
                            mkdir($target_path, 0755, true);
                        }
                        
                        
                        
                        $target = $target_path . DIRECTORY_SEPARATOR . $post->image;
                        
                        
                        
                        if(!file_exists($target)) {
                            
                            copy($source, $target);
                            $share_image =  $base_share_image . '/images/post/' . $post->uuid . '/' . $post->image;
                            
                        } else {
                            $share_image =  $base_share_image . '/images/post/' . $post->uuid . '/' . $post->image;
                            
                        }
                    }
                }
            }
           
            $share_url = $this->url()->fromRoute('share-callback', ['type' => $type, 'code' => $code ], ['force_canonical' => true, 'query' => ['user' => $currentUser->uuid]]);
            
            

            
            
            $this->layout()->setTemplate('layout/share.phtml');
            $viewModel = new ViewModel();
            $viewModel->setTemplate('leaders-linked/home/share.phtml');
            $viewModel->setVariables([
                'share_image' => $share_image,
                'share_url' => $share_url,
                'share_title' => $share_title,
                'share_description' => $share_description,
                'share_description' => $share_description,
            ]);

            
            return $viewModel;
            

        } else {
            $response = [
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ];
            
            return new JsonModel($response);
        }
        
       
    }
    
    public function shareCallbackAction()
    {
        $request = $this->getRequest();
        if ($request->isGet()) {
            $currentUserPlugin = $this->plugin('currentUserPlugin');
           
            
            $code = $this->params()->fromRoute('code');
            $type = $this->params()->fromRoute('type');
            $user = $this->params()->fromQuery('user');
            
            
            
            $url_redirect = '';
            $user_redirect = '';
            if($type == 'feed' && $code ) {
                $feedMapper =  FeedMapper::getInstance($this->adapter);
                $feed = $feedMapper->fetchOneByUuid($code);
                
                if($feed && $feed->status == Feed::STATUS_PUBLISHED) {
                        
                    $url_redirect = $this->url()->fromRoute('dashboard', ['feed' => $feed->uuid]);
                }
                
                
            } else if ($type == 'post' && $code) {
                
                $postMapper = PostMapper::getInstance($this->adapter);
                $post = $postMapper->fetchOneByUuid($code);
                
                if($post && $post->status == Post::STATUS_ACTIVE) {
                    $url_redirect = $this->url()->fromRoute('post', ['id' => $post->uuid]);
                }
            }
            
            if($user) {
                $userMapper = UserMapper::getInstance($this->adapter);
                $user = $userMapper->fetchOneByUuid($user);
                
                if($user && $user->status == User::STATUS_ACTIVE && $currentUserPlugin->hasIdentity()) {
                    $currentUser = $currentUserPlugin->getUser();
                    
                    if($user->id != $currentUser->id) {
                        
                        $connectionMapper = ConnectionMapper::getInstance($this->adapter);
                        $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $user->id);
                        
                        if($connection) {
                            
                            if($connection->status != Connection::STATUS_ACCEPTED) {
                                $connectionMapper->approve($connection);
                            }
                            
                        } else {
                            $connection = new Connection();
                            $connection->request_from = $currentUser->id;
                            $connection->request_to = $user->id;
                            $connection->status = Connection::STATUS_ACCEPTED;
                            
                            $connectionMapper->insert($connection);
                        }
                    }
                }
            }
                
            
            if ($currentUserPlugin->hasIdentity()) {
                if($url_redirect) {
                    return $this->redirect()->toUrl($url_redirect);
                } else {
                    return $this->redirect()->toRoute('dashboard');
                }
            } else {
                $this->cache->addItem('url_redirect', $url_redirect);
                $this->cache->addItem('user_redirect', $user_redirect);

                return $this->redirect()->toRoute('signin');
            }
        } else {
            $response = [
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ];
        }
        
        return new JsonModel($response);
    }
}