Proyectos de Subversion LeadersLinked - Services

Rev

Rev 352 | 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\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Laminas\View\Model\JsonModel;

use LeadersLinked\Library\Functions;
use LeadersLinked\Model\HabitValue;
use LeadersLinked\Mapper\HabitValueMapper;
use LeadersLinked\Form\Habit\HabitValueForm;


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



        $request = $this->getRequest();


        $request = $this->getRequest();
        if ($request->isGet()) {
            $currentUserPlugin = $this->plugin('currentUserPlugin');
            $currentUser = $currentUserPlugin->getUser();

            $companyMapper = \LeadersLinked\Mapper\CompanyMapper::getInstance($this->adapter);
            $company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentUser->network_id);


            $habitCategoryMapper = \LeadersLinked\Mapper\HabitCategoryMapper::getInstance($this->adapter);
            $habitUserLogCategoryMapper = \LeadersLinked\Mapper\HabitUserLogCategoryMapper::getInstance($this->adapter);

            $categories = [];
            $records = $habitCategoryMapper->fetchAllActiveByCompanyId($company->id);

            $date = date('Y-m-d');
            foreach ($records as $record) {
                $habitUserLogCategory = $habitUserLogCategoryMapper->fetchOneByUserIdAndCategoryIdAndDate($currentUser->id, $record->id, $date);
                if ($habitUserLogCategory) {
                    $code = $habitUserLogCategory->code;
                } else {
                    $code = '';
                }

                array_push($categories, [
                    'uuid' => $record->uuid,
                    'name' => $record->name,
                    'selected' => $code
                ]);
            }

            $storage = \LeadersLinked\Library\Storage::getInstance($this->config, $this->adapter);
            $path = $storage->getPathHabitEmoji();

            $emojis = [];
            $habitEmojiMapper = \LeadersLinked\Mapper\HabitEmojiMapper::getInstance($this->adapter);
            $records = $habitEmojiMapper->fetchAllActiveByCompanyId($company->id);




            foreach ($records as $record) {


                array_push($emojis, [
                    'code' => $record->code,
                    'name' => $record->name,
                    'image' => $storage->getGenericImage($path, $record->uuid, $record->image)
                ]);
            }




            $acl = $this->getEvent()->getViewModel()->getVariable('acl');
            $allowValues = $acl->isAllowed($currentUser->usertype_id, 'habits/values');
            $allowParadigms = $acl->isAllowed($currentUser->usertype_id, 'habits/paradigms');
            $allowPurposes = $acl->isAllowed($currentUser->usertype_id, 'habits/purposes');
            $allowSkills = $acl->isAllowed($currentUser->usertype_id, 'habits/skills');
            $allowGoals = $acl->isAllowed($currentUser->usertype_id, 'habits/goals');
            $allowAspectDailyLog = $acl->isAllowed($currentUser->usertype_id, 'habits/aspect-daily-log');

            return new JsonModel([
                'success' => true,
                'data' => [
                    'categories' => $categories,
                    'emojis' => $emojis,
                    'link_aspect_daily_log' => $allowAspectDailyLog ? $this->url()->fromRoute('habits/aspect-daily-log', [], ['force_canonical' => true]) : '',
                    'link_values' => $allowValues ? $this->url()->fromRoute('habits/values', [], ['force_canonical' => true]) : '',
                    'link_paradigms' => $allowParadigms ? $this->url()->fromRoute('habits/paradigms', [], ['force_canonical' => true]) : '',
                    'link_purposes' => $allowPurposes ? $this->url()->fromRoute('habits/purposes', [], ['force_canonical' => true]) : '',
                    'link_skills' => $allowSkills ? $this->url()->fromRoute('habits/skills', [], ['force_canonical' => true]) : '',
                    'link_goals' => $allowGoals ? $this->url()->fromRoute('habits/goals', [], ['force_canonical' => true]) : '',
                ]
            ]);
        } else {
            return new JsonModel([
                'success' => false,
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
            ]);;
        }
    }


    public function aspectDailyLogAction()
    {
        $request = $this->getRequest();


        $request = $this->getRequest();
        if ($request->isPost()) {
            $currentUserPlugin = $this->plugin('currentUserPlugin');
            $currentUser = $currentUserPlugin->getUser();

            $companyMapper = \LeadersLinked\Mapper\CompanyMapper::getInstance($this->adapter);
            $company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentUser->network_id);

            $codes = [];

            $habitEmojiMapper = \LeadersLinked\Mapper\HabitEmojiMapper::getInstance($this->adapter);
            $records = $habitEmojiMapper->fetchAllActiveByCompanyId($company->id);
            foreach ($records as $record) {
                $codes[$record->code] = $record->id;
            }

            $habitCategoryMapper = \LeadersLinked\Mapper\HabitCategoryMapper::getInstance($this->adapter);

            $habitUserLogCategoryMapper = \LeadersLinked\Mapper\HabitUserLogCategoryMapper::getInstance($this->adapter);

            $records = $habitCategoryMapper->fetchAllActiveByCompanyId($company->id);

            $date = date('Y-m-d');
            $time = date('H:i:s');
            foreach ($records as $record) {
                $code = trim($this->params()->fromPost($record->uuid, ''));
                if (isset($codes[$code])) {
                    $habitUserLogCategory = $habitUserLogCategoryMapper->fetchOneByUserIdAndCategoryIdAndDate($currentUser->id, $record->id, $date);
                    if ($habitUserLogCategory) {
                        $habitUserLogCategory->code     = $code;

                        $habitUserLogCategoryMapper->update($habitUserLogCategory);
                    } else {
                        $habitUserLogCategory = new \LeadersLinked\Model\HabitUserLogCategory();
                        $habitUserLogCategory->company_id = $company->id;
                        $habitUserLogCategory->date = $date;
                        $habitUserLogCategory->time = $time;
                        $habitUserLogCategory->user_id = $currentUser->id;
                        $habitUserLogCategory->category_id = $record->id;
                        $habitUserLogCategory->code = $code;

                        $habitUserLogCategoryMapper->insert($habitUserLogCategory);
                    }
                }
            }



            $userLogContentMapper = \LeadersLinked\Mapper\HabitUserLogContentMapper::getInstance($this->adapter);
            $userLogContent = $userLogContentMapper->fetchOneMaxByCompanyIdAndUserId($company->id, $currentUser->id);

            $contentMapper = \LeadersLinked\Mapper\HabitContentMapper::getInstance($this->adapter);

            $order = 0;
            if ($userLogContent) {

                $habitContent = $contentMapper->fetchOne($userLogContent->content_id);
                if ($habitContent) {
                    $order = $habitContent->order;
                }
            }



            $link = '';
            $type = '';
            $habitContent = $contentMapper->fetchOneNextAvailableForOrderByCompanyId($company->id, $order);
            if ($habitContent) {
                $storage = \LeadersLinked\Library\Storage::getInstance($this->config, $this->adapter);
                $path = $storage->getPathHabitContent();


                $type = $habitContent->type;
                $link = $storage->getGenericFile($path, $habitContent->uuid, $habitContent->file);


                $userLogContent = new \LeadersLinked\Model\HabitUserLogContent();
                $userLogContent->company_id = $company->id;
                $userLogContent->content_id = $habitContent->id;
                $userLogContent->user_id    = $currentUser->id;
                $userLogContent->date       = $date;
                $userLogContent->time       = $time;

                $userLogContentMapper->insert($userLogContent);
            };

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