Proyectos de Subversion LeadersLinked - Services

Rev

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