Proyectos de Subversion LeadersLinked - Services

Rev

Rev 381 | 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 HabitReportController 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();
        $initialDate = $request->getQuery('init', null);
        $finalDate = $request->getQuery('final', null);


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

            $habitReportMapper = \LeadersLinked\Mapper\HabitReportMapper::getInstance($this->adapter);

            $listAccessDays = [];
            $dates = $habitReportMapper->validateAndAdjustDates($filter, $initialDate, $finalDate);
            $records = $habitReportMapper->fetchAccessDayRecordsByDayIntervals($currentUser->id, $dates[0], $dates[1]);

            foreach ($records as $record) {
                $listAccessDays[] = $record['date'];
            }
            $recordsListAccessDays = $habitReportMapper->generateDateList($listAccessDays, $initialDate, $finalDate);
            return new JsonModel([
                'success' => true,
                'data' => [
                    'total_access_days' => count($records),
                    'list_access_days' => $recordsListAccessDays
                ]
            ]);
        } 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'
            ]);
        }
    }
}