Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
9095 nelberth 1
<?php
2
declare(strict_types=1);
3
 
4
namespace LeadersLinked\Controller;
5
 
6
use Laminas\Authentication\AuthenticationService;
7
use Laminas\Authentication\Result as AuthResult;
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Cache\Storage\Adapter\AbstractAdapter;
10
use Laminas\Mvc\Controller\AbstractActionController;
11
use Laminas\Mvc\I18n\Translator;
12
use Laminas\Log\LoggerInterface;
13
use Laminas\View\Model\ViewModel;
14
use Laminas\View\Model\JsonModel;
15
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
9136 nelberth 16
use LeadersLinked\Model\Feed;
17
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsViewTopicMapper;
18
use LeadersLinked\Model\HighPerformanceTeamsGroupsMembers;
19
use LeadersLinked\Model\HighPerformanceTeamsGroups;
20
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMembersMapper;
21
use LeadersLinked\Mapper\HighPerformanceTeamsGroupsMapper;
9149 nelberth 22
use LeadersLinked\Mapper\FeedMapper;
9290 nelberth 23
use LeadersLinked\Mapper\UserMapper;
9154 nelberth 24
use Laminas\Db\Sql\Expression;
9095 nelberth 25
 
9298 nelberth 26
use LeadersLinked\Library\Functions;
27
 
9136 nelberth 28
use LeadersLinked\Mapper\QueryMapper;
29
 
9095 nelberth 30
class HighPerformanceTeamsGroupsViewCalendarController extends AbstractActionController
31
{
32
    /**
33
     *
34
     * @var AdapterInterface
35
     */
36
    private $adapter;
37
 
38
 
39
    /**
40
     *
41
     * @var AbstractAdapter
42
     */
43
    private $cache;
44
 
45
    /**
46
     *
47
     * @var  LoggerInterface
48
     */
49
    private $logger;
50
 
51
    /**
52
     *
53
     * @var array
54
     */
55
    private $config;
56
 
57
 
58
 
59
    /**
60
     *
61
     * @param AdapterInterface $adapter
62
     * @param AbstractAdapter $cache
63
     * @param LoggerInterface $logger
64
     * @param array $config
65
     */
66
    public function __construct($adapter, $cache , $logger, $config)
67
    {
68
        $this->adapter      = $adapter;
69
        $this->cache        = $cache;
70
        $this->logger       = $logger;
71
        $this->config       = $config;
72
 
73
 
74
    }
9135 nelberth 75
 
76
 
9095 nelberth 77
    public function indexAction()
78
    {
9135 nelberth 79
 
9095 nelberth 80
 
9135 nelberth 81
        $currentUserPlugin = $this->plugin('currentUserPlugin');
82
        $currentUser = $currentUserPlugin->getUser();
83
        $currentCompany = $currentUserPlugin->getCompany();
11084 nelberth 84
        $group_uuid =  $this->params()->fromRoute('group_id');
85
        $feed_uuid =  $this->params()->fromRoute('feed_id');
9135 nelberth 86
 
9095 nelberth 87
        $request = $this->getRequest();
88
        if($request->isGet()) {
89
 
90
 
9135 nelberth 91
            $headers  = $request->getHeaders();
9095 nelberth 92
 
9135 nelberth 93
            $isJson = false;
94
            if($headers->has('Accept')) {
95
                $accept = $headers->get('Accept');
96
 
97
                $prioritized = $accept->getPrioritized();
98
 
99
                foreach($prioritized as $key => $value) {
100
                    $raw = trim($value->getRaw());
101
 
102
                    if(!$isJson) {
103
                        $isJson = strpos($raw, 'json');
104
                    }
105
 
106
                }
107
            }
9141 nelberth 108
 
9318 nelberth 109
            $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
110
            $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
11184 nelberth 111
            $this->layout()->setTemplate('layout/layout-backend');
112
            $viewModel = new ViewModel();
9318 nelberth 113
            if (!$highPerformanceTeamsGroups) {
11184 nelberth 114
                $viewModel->setTemplate('leaders-linked/no-access/index.phtml');
115
                return $viewModel ;
9318 nelberth 116
            }
117
 
118
            if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
119
 
11184 nelberth 120
                $viewModel->setTemplate('leaders-linked/no-access/index.phtml');
121
                return $viewModel ;
9318 nelberth 122
 
123
            }
11184 nelberth 124
 
11136 nelberth 125
            $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
126
            $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
127
            if(!$highPerformanceTeamsGroupsMember) {
11184 nelberth 128
                $viewModel->setTemplate('leaders-linked/no-access/index.phtml');
129
                return $viewModel ;
11136 nelberth 130
            }
131
            if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
11184 nelberth 132
                $viewModel->setTemplate('leaders-linked/no-access/index.phtml');
133
                return $viewModel ;
11136 nelberth 134
            }
135
            $addTopicBoton=true;
136
            if($highPerformanceTeamsGroupsMember->type==HighPerformanceTeamsGroupsMembers::TYPE_MODERATOR||$highPerformanceTeamsGroupsMember->type==HighPerformanceTeamsGroupsMembers::TYPE_USER){
137
                $addTopicBoton=false;
138
            }
9922 nelberth 139
 
140
 
141
            $viewModel->setTemplate('leaders-linked/high-performance-teams-groups-view-calendar/index.phtml');
142
            $viewModel->setVariables([
143
                'group_uuid'  =>  $group_uuid,
144
                'group_title'=>$highPerformanceTeamsGroups->title,
145
                'group_description'=>$highPerformanceTeamsGroups->description,
146
            ]);
147
            return $viewModel ;
148
 
149
        } else {
150
            return new JsonModel([
151
                'success' => false,
152
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
153
            ]);
154
        }
155
    }
156
 
157
    public function viewAction()
158
    {
159
 
160
 
161
        $currentUserPlugin = $this->plugin('currentUserPlugin');
162
        $currentUser = $currentUserPlugin->getUser();
163
        $currentCompany = $currentUserPlugin->getCompany();
11084 nelberth 164
        $group_uuid =  $this->params()->fromRoute('group_id');
165
        $feed_uuid =  $this->params()->fromRoute('feed_id');
9922 nelberth 166
 
167
        $request = $this->getRequest();
168
        if($request->isGet()) {
9142 nelberth 169
 
9922 nelberth 170
 
171
            $headers  = $request->getHeaders();
172
 
173
            $isJson = false;
174
            if($headers->has('Accept')) {
175
                $accept = $headers->get('Accept');
176
 
177
                $prioritized = $accept->getPrioritized();
178
 
179
                foreach($prioritized as $key => $value) {
180
                    $raw = trim($value->getRaw());
181
 
182
                    if(!$isJson) {
183
                        $isJson = strpos($raw, 'json');
184
                    }
185
 
186
                }
187
            }
188
            $highPerformanceTeamsGroupsMapper = HighPerformanceTeamsGroupsMapper::getInstance($this->adapter);
189
            $highPerformanceTeamsGroups = $highPerformanceTeamsGroupsMapper->fetchOneByUuid($group_uuid);
190
 
191
            if (!$highPerformanceTeamsGroups) {
192
                $data = [
193
                    'success' => false,
194
                    'data' => 'ERROR_RECORD_NOT_FOUND'
195
                ];
196
 
197
                return new JsonModel($data);
198
            }
199
 
200
            if($highPerformanceTeamsGroups->status != HighPerformanceTeamsGroups::STATUS_ACTIVE) {
201
 
202
                return new JsonModel([
203
                    'success' => false,
204
                    'data' => 'ERROR_UNAUTHORIZED'
205
                ]);
206
 
207
            }
11136 nelberth 208
 
209
            $highPerformanceTeamsGroupsMembersMapper = HighPerformanceTeamsGroupsMembersMapper::getInstance($this->adapter);
210
            $highPerformanceTeamsGroupsMember = $highPerformanceTeamsGroupsMembersMapper->fetchOneByGroupIdAndUserId($highPerformanceTeamsGroups->id, $currentUser->id);
211
            if(!$highPerformanceTeamsGroupsMember) {
212
                return new JsonModel([
213
                    'success' => false,
214
                    'data' => 'ERROR_RECORD_NOT_FOUND'
215
                ]);
216
            }
217
            if(!($highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ACCEPTED || $highPerformanceTeamsGroupsMember->status == HighPerformanceTeamsGroupsMembers::STATUS_ADDED_BY_ADMIN) ){
218
                return new JsonModel([
219
                    'success' => false,
220
                    'data' => 'ERROR_YOU_DO_NOT_HAVE_ACCESS'
221
                ]);
222
            }
223
            $addTopicBoton=true;
224
            if($highPerformanceTeamsGroupsMember->type==HighPerformanceTeamsGroupsMembers::TYPE_MODERATOR||$highPerformanceTeamsGroupsMember->type==HighPerformanceTeamsGroupsMembers::TYPE_USER){
225
                $addTopicBoton=false;
226
            }
9922 nelberth 227
 
9135 nelberth 228
            if($isJson) {
9255 nelberth 229
                $acl = $this->getEvent()->getViewModel()->getVariable('acl');
9256 nelberth 230
                $allowView = $acl->isAllowed($currentUser->usertype_id,'high-performance-teams/groups/view/calendar');
9135 nelberth 231
 
9144 nelberth 232
 
9281 nelberth 233
                $items = [];
9156 nelberth 234
                $feedMapper = FeedMapper::getInstance($this->adapter);
9264 nelberth 235
                if(isset($feed_uuid)){
236
                    $records  = $feedMapper->fetchOneByUuid($feed_uuid);
9269 nelberth 237
 
9281 nelberth 238
                    if($records->high_performance_group_id!=$highPerformanceTeamsGroups->id){
9264 nelberth 239
                        return new JsonModel([
240
                            'success' => false,
9276 nelberth 241
                            'data' => 'ERROR_METHOD_NOT_ALLOWED'
9264 nelberth 242
                        ]);
9281 nelberth 243
                    }
9290 nelberth 244
 
245
                    $items = $this->renderFeed($records);
9264 nelberth 246
                }else{
247
                    $records  = $feedMapper->fetchAllTypeCalendar($highPerformanceTeamsGroups->id);
9282 nelberth 248
                    $contador=1;
9271 nelberth 249
                    foreach($records as $record)
250
                    {
251
                        $item = [
252
                            'id' => $record->uuid,
253
                            'title' => 'Reunion #'.$contador,
254
                            'start' => $record->date.'T'.$record->time,
255
                            'color'=>'#00000'.$contador,
11084 nelberth 256
                            'link_feed'=> $allowView ? $this->url()->fromRoute('high-performance-teams/groups/view/calendar/view', ['group_id' => $highPerformanceTeamsGroups->uuid, 'feed_id'=>$record->uuid]) : '',
9271 nelberth 257
                        ];
258
                        $contador++;
259
                        array_push($items, $item);
260
                    }
9281 nelberth 261
                }
9273 nelberth 262
 
9179 nelberth 263
                return new JsonModel([
264
                    'success' => true,
265
                    'data' => [
266
                        'items' => $items,
267
                    ]
268
                ]);
9135 nelberth 269
 
270
            }
9095 nelberth 271
        } else {
9179 nelberth 272
            return new JsonModel([
273
                'success' => false,
274
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
275
            ]);
9095 nelberth 276
        }
277
    }
9135 nelberth 278
 
9290 nelberth 279
    private function renderFeed($feed)
280
    {
281
 
282
        $sandbox = $this->config['leaderslinked.runmode.sandbox'];
283
        if($sandbox) {
284
            $company_profile_url = $this->config['leaderslinked.frontend.sandbox_company_profile'];
285
            $user_profile_url = $this->config['leaderslinked.frontend.sandbox_user_profile'];
286
 
287
        } else {
288
            $company_profile_url = $this->config['leaderslinked.frontend.production_company_profile'];
289
            $user_profile_url = $this->config['leaderslinked.frontend.production_user_profile'];
290
        }
291
 
292
        $userMapper = UserMapper::getInstance($this->adapter);
293
        $user = $userMapper->fetchOne($feed->user_id);
9297 nelberth 294
        $dt = \DateTime::createFromFormat('Y-m-d H:i:s', $feed->added_on);
295
        $item['owner_time_elapse'] = Functions::timeElapsedString( $dt->getTimestamp());
9290 nelberth 296
        $item['feed_priority'] = $feed->priority;
297
        $item['owner_url'] =  str_replace('[uuid]', $user->uuid, $user_profile_url);
298
        $item['owner_image'] = $this->url()->fromRoute('storage', ['code' => $user->uuid, 'type' => 'user', 'filename' => $user->image]);
299
        $item['owner_name'] = $user->first_name . ' ' . $user->last_name;
300
        $item['owner_description'] = strip_tags($feed->description, 'p');
301
        $dt = \DateTime::createFromFormat('Y-m-d', $feed->date);
302
        $dt2 = \DateTime::createFromFormat('H:i:s', $feed->time);
303
        $item['owner_meeting_date'] =  $dt->format('d/m/Y');
304
        $item['owner_meeting_time'] =  $dt2->format('h:i a');
305
        $item['owner_meeting_link_type'] =  $feed->link_type;
306
        $item['owner_meeting_link_name'] =  $feed->link_name;
307
 
308
        return $item;
309
    }
310
 
9095 nelberth 311
}