Proyectos de Subversion LeadersLinked - Services

Rev

Rev 338 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
302 www 1
<?php
352 ariadna 2
 
302 www 3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Controller;
6
 
7
use Laminas\Db\Adapter\AdapterInterface;
8
 
9
use Laminas\Mvc\Controller\AbstractActionController;
10
use Laminas\View\Model\ViewModel;
11
use Laminas\View\Model\JsonModel;
12
 
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Model\HabitValue;
15
use LeadersLinked\Mapper\HabitValueMapper;
16
use LeadersLinked\Form\Habit\HabitValueForm;
17
 
18
 
19
class HabitController extends AbstractActionController
20
{
21
    /**
22
     *
23
     * @var \Laminas\Db\Adapter\AdapterInterface
24
     */
25
    private $adapter;
352 ariadna 26
 
302 www 27
    /**
28
     *
29
     * @var \LeadersLinked\Cache\CacheInterface
30
     */
31
    private $cache;
352 ariadna 32
 
33
 
302 www 34
    /**
35
     *
36
     * @var \Laminas\Log\LoggerInterface
37
     */
38
    private $logger;
352 ariadna 39
 
302 www 40
    /**
41
     *
42
     * @var array
43
     */
44
    private $config;
352 ariadna 45
 
46
 
302 www 47
    /**
48
     *
49
     * @var \Laminas\Mvc\I18n\Translator
50
     */
51
    private $translator;
352 ariadna 52
 
53
 
302 www 54
    /**
55
     *
56
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
57
     * @param \LeadersLinked\Cache\CacheInterface $cache
58
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
59
     * @param array $config
60
     * @param \Laminas\Mvc\I18n\Translator $translator
61
     */
62
    public function __construct($adapter, $cache, $logger, $config, $translator)
63
    {
64
        $this->adapter      = $adapter;
65
        $this->cache        = $cache;
66
        $this->logger       = $logger;
67
        $this->config       = $config;
68
        $this->translator   = $translator;
69
    }
352 ariadna 70
 
302 www 71
    public function indexAction()
72
    {
73
 
74
 
352 ariadna 75
 
302 www 76
        $request = $this->getRequest();
352 ariadna 77
 
78
 
302 www 79
        $request = $this->getRequest();
352 ariadna 80
        if ($request->isGet()) {
302 www 81
            $currentUserPlugin = $this->plugin('currentUserPlugin');
82
            $currentUser = $currentUserPlugin->getUser();
352 ariadna 83
 
324 www 84
            $companyMapper = \LeadersLinked\Mapper\CompanyMapper::getInstance($this->adapter);
85
            $company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentUser->network_id);
352 ariadna 86
 
87
 
324 www 88
            $habitCategoryMapper = \LeadersLinked\Mapper\HabitCategoryMapper::getInstance($this->adapter);
89
            $habitUserLogCategoryMapper = \LeadersLinked\Mapper\HabitUserLogCategoryMapper::getInstance($this->adapter);
352 ariadna 90
 
324 www 91
            $categories = [];
92
            $records = $habitCategoryMapper->fetchAllActiveByCompanyId($company->id);
352 ariadna 93
 
324 www 94
            $date = date('Y-m-d');
352 ariadna 95
            foreach ($records as $record) {
324 www 96
                $habitUserLogCategory = $habitUserLogCategoryMapper->fetchOneByUserIdAndCategoryIdAndDate($currentUser->id, $record->id, $date);
352 ariadna 97
                if ($habitUserLogCategory) {
324 www 98
                    $code = $habitUserLogCategory->code;
99
                } else {
100
                    $code = '';
101
                }
352 ariadna 102
 
324 www 103
                array_push($categories, [
352 ariadna 104
                    'uuid' => $record->uuid,
324 www 105
                    'name' => $record->name,
106
                    'selected' => $code
107
                ]);
108
            }
352 ariadna 109
 
333 www 110
            $storage = \LeadersLinked\Library\Storage::getInstance($this->config, $this->adapter);
324 www 111
            $path = $storage->getPathHabitEmoji();
352 ariadna 112
 
324 www 113
            $emojis = [];
114
            $habitEmojiMapper = \LeadersLinked\Mapper\HabitEmojiMapper::getInstance($this->adapter);
115
            $records = $habitEmojiMapper->fetchAllActiveByCompanyId($company->id);
352 ariadna 116
 
117
 
118
 
119
 
120
            foreach ($records as $record) {
121
 
122
 
324 www 123
                array_push($emojis, [
352 ariadna 124
                    'code' => $record->code,
125
                    'name' => $record->name,
324 www 126
                    'image' => $storage->getGenericImage($path, $record->uuid, $record->image)
127
                ]);
128
            }
352 ariadna 129
 
130
 
131
 
132
 
302 www 133
            $acl = $this->getEvent()->getViewModel()->getVariable('acl');
134
            $allowValues = $acl->isAllowed($currentUser->usertype_id, 'habits/values');
135
            $allowParadigms = $acl->isAllowed($currentUser->usertype_id, 'habits/paradigms');
136
            $allowPurposes = $acl->isAllowed($currentUser->usertype_id, 'habits/purposes');
307 www 137
            $allowSkills = $acl->isAllowed($currentUser->usertype_id, 'habits/skills');
138
            $allowGoals = $acl->isAllowed($currentUser->usertype_id, 'habits/goals');
327 www 139
            $allowAspectDailyLog = $acl->isAllowed($currentUser->usertype_id, 'habits/aspect-daily-log');
352 ariadna 140
 
141
            return new JsonModel([
142
                'success' => true,
302 www 143
                'data' => [
352 ariadna 144
                    'categories' => $categories,
324 www 145
                    'emojis' => $emojis,
327 www 146
                    'link_aspect_daily_log' => $allowAspectDailyLog ? $this->url()->fromRoute('habits/aspect-daily-log', [], ['force_canonical' => true]) : '',
302 www 147
                    'link_values' => $allowValues ? $this->url()->fromRoute('habits/values', [], ['force_canonical' => true]) : '',
148
                    'link_paradigms' => $allowParadigms ? $this->url()->fromRoute('habits/paradigms', [], ['force_canonical' => true]) : '',
149
                    'link_purposes' => $allowPurposes ? $this->url()->fromRoute('habits/purposes', [], ['force_canonical' => true]) : '',
307 www 150
                    'link_skills' => $allowSkills ? $this->url()->fromRoute('habits/skills', [], ['force_canonical' => true]) : '',
151
                    'link_goals' => $allowGoals ? $this->url()->fromRoute('habits/goals', [], ['force_canonical' => true]) : '',
302 www 152
                ]
153
            ]);
154
        } else {
155
            return new JsonModel([
156
                'success' => false,
157
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
158
            ]);;
159
        }
160
    }
352 ariadna 161
 
162
 
325 www 163
    public function aspectDailyLogAction()
164
    {
165
        $request = $this->getRequest();
352 ariadna 166
 
167
 
325 www 168
        $request = $this->getRequest();
352 ariadna 169
        if ($request->isPost()) {
325 www 170
            $currentUserPlugin = $this->plugin('currentUserPlugin');
171
            $currentUser = $currentUserPlugin->getUser();
352 ariadna 172
 
325 www 173
            $companyMapper = \LeadersLinked\Mapper\CompanyMapper::getInstance($this->adapter);
174
            $company = $companyMapper->fetchDefaultForNetworkByNetworkId($currentUser->network_id);
352 ariadna 175
 
325 www 176
            $codes = [];
352 ariadna 177
 
325 www 178
            $habitEmojiMapper = \LeadersLinked\Mapper\HabitEmojiMapper::getInstance($this->adapter);
179
            $records = $habitEmojiMapper->fetchAllActiveByCompanyId($company->id);
352 ariadna 180
            foreach ($records as $record) {
181
                $codes[$record->code] = $record->id;
325 www 182
            }
352 ariadna 183
 
325 www 184
            $habitCategoryMapper = \LeadersLinked\Mapper\HabitCategoryMapper::getInstance($this->adapter);
352 ariadna 185
 
325 www 186
            $habitUserLogCategoryMapper = \LeadersLinked\Mapper\HabitUserLogCategoryMapper::getInstance($this->adapter);
352 ariadna 187
 
325 www 188
            $records = $habitCategoryMapper->fetchAllActiveByCompanyId($company->id);
352 ariadna 189
 
325 www 190
            $date = date('Y-m-d');
191
            $time = date('H:i:s');
352 ariadna 192
            foreach ($records as $record) {
338 www 193
                $code = trim($this->params()->fromPost($record->uuid, ''));
352 ariadna 194
                if (isset($codes[$code])) {
325 www 195
                    $habitUserLogCategory = $habitUserLogCategoryMapper->fetchOneByUserIdAndCategoryIdAndDate($currentUser->id, $record->id, $date);
352 ariadna 196
                    if ($habitUserLogCategory) {
325 www 197
                        $habitUserLogCategory->code     = $code;
352 ariadna 198
 
325 www 199
                        $habitUserLogCategoryMapper->update($habitUserLogCategory);
200
                    } else {
201
                        $habitUserLogCategory = new \LeadersLinked\Model\HabitUserLogCategory();
202
                        $habitUserLogCategory->company_id = $company->id;
203
                        $habitUserLogCategory->date = $date;
204
                        $habitUserLogCategory->time = $time;
205
                        $habitUserLogCategory->user_id = $currentUser->id;
338 www 206
                        $habitUserLogCategory->category_id = $record->id;
352 ariadna 207
                        $habitUserLogCategory->code = $code;
208
 
325 www 209
                        $habitUserLogCategoryMapper->insert($habitUserLogCategory);
210
                    }
211
                }
212
            }
352 ariadna 213
 
214
 
215
 
325 www 216
            $userLogContentMapper = \LeadersLinked\Mapper\HabitUserLogContentMapper::getInstance($this->adapter);
217
            $userLogContent = $userLogContentMapper->fetchOneMaxByCompanyIdAndUserId($company->id, $currentUser->id);
352 ariadna 218
 
325 www 219
            $contentMapper = \LeadersLinked\Mapper\HabitContentMapper::getInstance($this->adapter);
352 ariadna 220
 
325 www 221
            $order = 0;
352 ariadna 222
            if ($userLogContent) {
223
 
325 www 224
                $habitContent = $contentMapper->fetchOne($userLogContent->content_id);
352 ariadna 225
                if ($habitContent) {
325 www 226
                    $order = $habitContent->order;
227
                }
228
            }
352 ariadna 229
 
230
 
231
 
325 www 232
            $link = '';
328 www 233
            $type = '';
325 www 234
            $habitContent = $contentMapper->fetchOneNextAvailableForOrderByCompanyId($company->id, $order);
352 ariadna 235
            if ($habitContent) {
333 www 236
                $storage = \LeadersLinked\Library\Storage::getInstance($this->config, $this->adapter);
325 www 237
                $path = $storage->getPathHabitContent();
352 ariadna 238
 
239
 
328 www 240
                $type = $habitContent->type;
325 www 241
                $link = $storage->getGenericFile($path, $habitContent->uuid, $habitContent->file);
352 ariadna 242
 
243
 
338 www 244
                $userLogContent = new \LeadersLinked\Model\HabitUserLogContent();
245
                $userLogContent->company_id = $company->id;
246
                $userLogContent->content_id = $habitContent->id;
247
                $userLogContent->user_id    = $currentUser->id;
248
                $userLogContent->date       = $date;
249
                $userLogContent->time       = $time;
352 ariadna 250
 
338 www 251
                $userLogContentMapper->insert($userLogContent);
325 www 252
            };
352 ariadna 253
 
254
 
255
 
256
 
257
 
258
 
325 www 259
            return new JsonModel([
260
                'success' => true,
328 www 261
                'data' => [
352 ariadna 262
                    'type' => $type,
328 www 263
                    'link' => $link
352 ariadna 264
                ]
325 www 265
            ]);
266
        } else {
267
            return new JsonModel([
268
                'success' => false,
269
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
270
            ]);
271
        }
272
    }
302 www 273
}