Proyectos de Subversion LeadersLinked - Services

Rev

Rev 742 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
/**
3
 *
4
 * Controlador: Mis Perfiles
5
 *
6
 */
7
declare(strict_types=1);
8
 
9
namespace LeadersLinked\Controller;
10
 
11
use Laminas\Db\Adapter\AdapterInterface;
12
 
13
use Laminas\Mvc\Controller\AbstractActionController;
14
use Laminas\Log\LoggerInterface;
15
use Laminas\View\Model\ViewModel;
16
use Laminas\View\Model\JsonModel;
17
 
18
use LeadersLinked\Mapper\UserProfileMapper;
19
use LeadersLinked\Mapper\CompanyFollowerMapper;
20
use LeadersLinked\Mapper\LocationMapper;
21
use LeadersLinked\Mapper\UserLanguageMapper;
22
use LeadersLinked\Mapper\UserSkillMapper;
23
 
24
use LeadersLinked\Mapper\UserMapper;
25
use LeadersLinked\Mapper\UserEducationMapper;
26
use LeadersLinked\Mapper\DegreeMapper;
27
use LeadersLinked\Mapper\UserExperienceMapper;
28
use LeadersLinked\Mapper\IndustryMapper;
29
use LeadersLinked\Mapper\CompanySizeMapper;
30
use LeadersLinked\Mapper\ConnectionMapper;
31
use LeadersLinked\Mapper\UserBlockedMapper;
32
use LeadersLinked\Model\Connection;
33
use LeadersLinked\Model\Network;
34
use LeadersLinked\Mapper\ProfileVisitMapper;
35
use LeadersLinked\Model\ProfileVisit;
36
use LeadersLinked\Mapper\QueryMapper;
37
use Laminas\Paginator\Adapter\DbSelect;
38
use Laminas\Paginator\Paginator;
39
use LeadersLinked\Model\User;
40
use Laminas\Db\Sql\Expression;
41
use LeadersLinked\Mapper\LanguageMapper;
42
use LeadersLinked\Mapper\SkillMapper;
43
use LeadersLinked\Mapper\AptitudeMapper;
44
use LeadersLinked\Mapper\UserAptitudeMapper;
45
use LeadersLinked\Mapper\HobbyAndInterestMapper;
46
use LeadersLinked\Mapper\UserHobbyAndInterestMapper;
47
use LeadersLinked\Library\Functions;
283 www 48
use LeadersLinked\Library\Storage;
1 efrain 49
 
50
 
51
class ProfileController extends AbstractActionController
52
{
53
    /**
54
     *
55
     * @var \Laminas\Db\Adapter\AdapterInterface
56
     */
57
    private $adapter;
58
 
59
    /**
60
     *
61
     * @var \LeadersLinked\Cache\CacheInterface
62
     */
63
    private $cache;
64
 
65
 
66
    /**
67
     *
68
     * @var \Laminas\Log\LoggerInterface
69
     */
70
    private $logger;
71
 
72
    /**
73
     *
74
     * @var array
75
     */
76
    private $config;
77
 
78
 
79
    /**
80
     *
81
     * @var \Laminas\Mvc\I18n\Translator
82
     */
83
    private $translator;
84
 
85
 
86
    /**
87
     *
88
     * @param \Laminas\Db\Adapter\AdapterInterface $adapter
89
     * @param \LeadersLinked\Cache\CacheInterface $cache
90
     * @param \Laminas\Log\LoggerInterface LoggerInterface $logger
91
     * @param array $config
92
     * @param \Laminas\Mvc\I18n\Translator $translator
93
     */
94
    public function __construct($adapter, $cache, $logger, $config, $translator)
95
    {
96
        $this->adapter      = $adapter;
97
        $this->cache        = $cache;
98
        $this->logger       = $logger;
99
        $this->config       = $config;
100
        $this->translator   = $translator;
101
    }
102
 
103
    /**
104
     *
105
     * Generación del listado de perfiles
106
     * {@inheritDoc}
107
     * @see \Laminas\Mvc\Controller\AbstractActionController::indexAction()
108
     */
109
    public function indexAction()
110
    {
111
        return new JsonModel([
112
            'success' => false,
113
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
114
        ]);
115
    }
116
 
117
 
118
    /**
119
     * Presenta el perfil con las opciónes de edición de cada sección
120
     * @return \Laminas\Http\Response|\Laminas\View\Model\ViewModel|\Laminas\View\Model\JsonModel
121
     */
122
    public function viewAction()
123
    {
124
 
125
        $currentUserPlugin = $this->plugin('currentUserPlugin');
126
        $currentUser = $currentUserPlugin->getUser();
127
 
128
        $currentNetworkPlugin = $this->plugin('currentNetworkPlugin');
129
        $currentNetwork= $currentNetworkPlugin->getNetwork();
130
 
131
 
132
        $request = $this->getRequest();
133
        if($request->isGet()) {
134
 
135
 
136
            $flashMessenger = $this->plugin('FlashMessenger');
137
 
138
 
139
            $id = $this->params()->fromRoute('id');
140
            if(!$id) {
141
                $flashMessenger->addErrorMessage('ERROR_INVALID_PARAMETER');
142
                return $this->redirect()->toRoute('dashboard');
143
            }
144
 
145
 
146
            $userMapper = UserMapper::getInstance($this->adapter);
147
            $user = $userMapper->fetchOneByUuidAndNetworkId($id, $currentUser->network_id);
148
 
149
            $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
150
            if($user) {
151
                $userProfile = $userProfileMapper->fetchOnePublicByUserId($user->id);
152
            } else {
153
                $userProfile = $userProfileMapper->fetchOneByUuid($id);
154
 
155
                if($userProfile) {
156
                    $user = $userMapper->fetchOne($userProfile->user_id);
157
 
158
                    if($user && $currentUser->network_id != $user->network_id) {
159
                        $response = [
160
                            'success' => false,
161
                            'data' => 'ERROR_UNAUTHORIZED'
162
                        ];
163
 
164
                        return new JsonModel($response);
165
                    }
166
                }
167
            }
168
 
169
            if(!$userProfile) {
170
                $flashMessenger->addErrorMessage('ERROR_RECORD_NOT_FOUND');
171
                return $this->redirect()->toRoute('dashboard');
172
            }
173
 
174
 
175
 
176
            $userBlockedMapper = UserBlockedMapper::getInstance($this->adapter);
177
            $userBlocked = $userBlockedMapper->fetchOneByUserIdAndBlockedId($userProfile->user_id, $currentUser->id);
178
 
179
            if($userBlocked) {
180
                $flashMessenger->addErrorMessage('ERROR_UNAUTHORIZED');
181
                return $this->redirect()->toRoute('dashboard');
182
            }
183
 
184
            if($currentUser->id != $userProfile->user_id) {
185
 
186
                $visited_on = date('Y-m-d');;
187
                $profileVisitMapper = ProfileVisitMapper::getInstance($this->adapter);
188
                $profileVisit = $profileVisitMapper->fetchOneByVisitorIdAndVisitedIdAndVisitedOn($currentUser->id, $userProfile->user_id, $visited_on);
189
                if(!$profileVisit) {
190
                    $profileVisit = new ProfileVisit();
191
                    $profileVisit->user_profile_id = $userProfile->id;
192
                    $profileVisit->visited_id = $userProfile->user_id;
193
                    $profileVisit->visitor_id = $currentUser->id;
194
                    $profileVisit->visited_on = date('Y-m-d');
195
                    $profileVisitMapper->insert($profileVisit);
196
                }
197
            }
198
 
199
            if($userProfile->location_id) {
200
                $locationMapper= LocationMapper::getInstance($this->adapter);
201
                $location = $locationMapper->fetchOne($userProfile->location_id);
202
 
203
                $formattedAddress = $location->formatted_address;
204
                $country = $location->country;
205
            } else {
206
                $formattedAddress = '';
207
                $country = '';
208
            }
209
 
210
            $connectionMapper = ConnectionMapper::getInstance($this->adapter);
211
            $connection = $connectionMapper->fetchOneByUserId1AndUserId2($currentUser->id, $userProfile->user_id);
212
 
213
            $userMapper = UserMapper::getInstance($this->adapter);
214
            $user = $userMapper->fetchOne($userProfile->user_id);
215
 
216
            $userLanguages = [];
217
            $languageMapper = LanguageMapper::getInstance($this->adapter);
218
            $userLanguageMapper = UserLanguageMapper::getInstance($this->adapter);
219
            $records = $userLanguageMapper->fetchAllByUserProfileId($userProfile->id);
220
            foreach($records as $record)
221
            {
222
                $language = $languageMapper->fetchOne($record->language_id);
223
                $userLanguages[$language->id] = $language->name;
224
            }
225
 
226
 
227
            $locationMapper = LocationMapper::getInstance($this->adapter);
228
 
229
            $degreeMapper = DegreeMapper::getInstance($this->adapter);
230
            $userEducationMapper = UserEducationMapper::getInstance($this->adapter);
231
            $userEducations = $userEducationMapper->fetchAllByUserProfileId($userProfile->id);
232
 
233
            foreach($userEducations  as &$userEducation)
234
            {
235
                $location = $locationMapper->fetchOne($userEducation->location_id);
236
                $degree = $degreeMapper->fetchOne($userEducation->degree_id)    ;
237
 
238
 
239
                $userEducation = [
240
                    'university' => $userEducation->university,
241
                    'degree' => $degree->name,
242
                    'field_of_study' => $userEducation->field_of_study,
243
                    'grade_or_percentage' => $userEducation->grade_or_percentage,
244
                    'formatted_address' => $location->formatted_address,
245
                    'from_year' => $userEducation->from_year,
246
                    'to_year' => $userEducation->to_year,
247
                    'description' => $userEducation->description,
248
                ];
249
            }
250
 
251
            /*
252
            $industries = [];
253
            $industryMapper = IndustryMapper::getInstance($this->adapter);
254
            $records = $industryMapper->fetchAllActive();
255
            foreach($records as $record)
256
            {
257
                $industries[$record->uuid] = $record->name;
258
            }
259
 
260
            $companySizes = [];
261
            $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
262
            $records = $companySizeMapper->fetchAllActive();
263
            foreach($records as $record)
264
            {
265
                $companySizes[$record->uuid] = $record->name . ' ('.$record->minimum_no_of_employee . '-' . $record->maximum_no_of_employee .')';
266
            }
267
            */
268
 
269
            $industryMapper = IndustryMapper::getInstance($this->adapter);
270
            $companySizeMapper = CompanySizeMapper::getInstance($this->adapter);
271
 
272
 
273
            $userExperienceMapper = UserExperienceMapper::getInstance($this->adapter);
274
            $userExperiences = $userExperienceMapper->fetchAllByUserProfileId($userProfile->id);
275
 
276
            foreach($userExperiences  as &$userExperience)
277
            {
278
                $industry = $industryMapper->fetchOne($userExperience->industry_id);
279
                $companySize = $companySizeMapper->fetchOne($userExperience->company_size_id);
280
 
281
 
282
                $location = $locationMapper->fetchOne($userExperience->location_id);
283
 
284
                $userExperience = [
285
                    'company' => $userExperience->company,
286
                    'industry' => $industry->name,
287
                    'size' => $companySize->name . ' (' . $companySize->minimum_no_of_employee . ' - ' . $companySize->maximum_no_of_employee . ' ) ',
288
                    'title' => $userExperience->title,
289
                    'formatted_address' => $location->formatted_address,
290
                    'from_year' => $userExperience->from_year,
291
                    'from_month' => $userExperience->from_month,
292
                    'to_year' => $userExperience->to_year,
293
                    'to_month' => $userExperience->to_month,
294
                    'description' => $userExperience->description,
295
                    'is_current' => $userExperience->is_current,
296
                ];
297
            }
298
 
299
            $userAptitudes = [];
300
            $aptitudeMapper = AptitudeMapper::getInstance($this->adapter);
301
            $userAptitudeMapper = UserAptitudeMapper::getInstance($this->adapter);
302
            $records  = $userAptitudeMapper->fetchAllByUserProfileId($userProfile->id);
303
            foreach($records as $record)
304
            {
305
                $aptitude = $aptitudeMapper->fetchOne($record->aptitude_id);
306
                if($aptitude) {
307
                    $userAptitudes[$aptitude->uuid] = $aptitude->name;
308
                }
309
            }
310
 
311
            $userHobbiesAndInterests = [];
312
            $hobbyAndInterestMapper = HobbyAndInterestMapper::getInstance($this->adapter);
313
            $userHobbyAndInterestMapper = UserHobbyAndInterestMapper::getInstance($this->adapter);
314
            $records  = $userHobbyAndInterestMapper->fetchAllByUserProfileId($userProfile->id);
315
            foreach($records as $record)
316
            {
317
                $hobbyAndInterest = $hobbyAndInterestMapper->fetchOne($record->hobby_and_interest_id);
318
                if($hobbyAndInterest) {
319
                    $userHobbiesAndInterests[$hobbyAndInterest->uuid] = $hobbyAndInterest->name;
320
                }
321
            }
322
 
323
 
324
            $userSkills = [];
325
            $skillMapper = SkillMapper::getInstance($this->adapter);
326
            $userSkillMapper = UserSkillMapper::getInstance($this->adapter);
327
            $records  = $userSkillMapper->fetchAllByUserProfileId($userProfile->id);
328
            foreach($records as $record)
329
            {
330
                $skill = $skillMapper->fetchOne($record->skill_id);
331
                if($skill) {
332
                    $userSkills[$skill->uuid] = $skill->name;
333
                }
334
            }
335
 
336
 
337
 
338
 
339
 
340
            if($currentNetwork->relationship_user_mode == Network::RELATIONSHIP_USER_MODE_USER_2_USER)  {
341
 
342
                $connectionMapper = ConnectionMapper::getInstance($this->adapter);
343
                $total_connections = $connectionMapper->fetchTotalConnectionByUser($user->id);
344
 
345
                $request_connection = 0;
346
                if($connection) {
347
                    if($connection->status == Connection::STATUS_REJECTED || $connection->status == Connection::STATUS_CANCELLED) {
348
                        $request_connection = 1;
349
                    }
350
                } else {
351
                    $request_connection = 1;
352
                }
353
 
354
 
355
 
356
                $common_connection = count($connectionMapper->fetchAllCommonConnectionsUserIdByUser1ReturnIds($currentUser->id, $userProfile->user_id));
357
            }
283 www 358
 
333 www 359
            $storage = Storage::getInstance($this->config, $this->adapter);
1 efrain 360
 
361
            $profileVisitMapper = ProfileVisitMapper::getInstance($this->adapter);
362
            $views          = $profileVisitMapper->getTotalByVisitedId( $userProfile->user_id );
363
 
364
 
365
                $data = [
366
                    'user_id'                       => $user->uuid,
367
                    'user_uuid'                     => ($user->uuid),
368
                    'full_name'                     => trim($user->first_name . ' ' . $user->last_name),
369
                    'user_profile_id'               => $userProfile->id,
370
                    'user_profile_uuid'             => $userProfile->uuid,
595 stevensc 371
                    'image'                         => $storage->getUserProfileImage($currentUser, $userProfile),
372
                    'cover'                         => $storage->getUserProfileCover($currentUser, $userProfile),
1 efrain 373
                    'overview'                      => $userProfile->description,
374
                    'facebook'                      => $userProfile->facebook,
375
                    'instagram'                     => $userProfile->instagram,
376
                    'twitter'                       => $userProfile->twitter,
377
                    'formatted_address'             => $formattedAddress,
378
                    'country'                       => $country,
379
                    'user_skills'                   => $userSkills,
380
                    'user_languages'                => $userLanguages,
381
                    'user_educations'               => $userEducations,
382
                    'user_experiences'              => $userExperiences,
383
                    'user_aptitudes'                => $userAptitudes,
384
                    'user_hobbies_and_interests'    => $userHobbiesAndInterests,
385
                    'views'                         => $views,
386
                    'show_contact'                  => $user->id != $currentUser->id,
742 stevensc 387
                    'link_inmail'                   => $this->url()->fromRoute('inmail/user',['id' => $user->uuid ]),
1 efrain 388
                ];
389
 
390
 
391
                if($currentNetwork->default == Network::DEFAULT_YES) {
392
 
393
                    $companyFollowerMapper = CompanyFollowerMapper::getInstance($this->adapter);
394
                    $data['following'] = $companyFollowerMapper->getCountFollowing($user->id);
395
                    $data['view_following'] = 1;
396
                } else {
397
                    $data['following'] = 0;
398
                    $data['view_following'] = 0;
399
                }
400
 
401
 
402
                if($currentNetwork->relationship_user_mode == Network::RELATIONSHIP_USER_MODE_USER_2_USER)  {
403
                    $data['total_connections']  = $total_connections;
404
                    $data['view_total_connections']  = 1;
405
                    $data['request_connection'] = $request_connection;
406
                    $data['common_connection']  = $common_connection;
407
                    $data['link_cancel']        = $connection && $connection->status == Connection::STATUS_SENT ? $this->url()->fromRoute('connection/delete',['id' => $user->uuid ]) :  $this->url()->fromRoute('connection/cancel',['id' => $user->uuid]);
408
                    $data['link_request']       = $request_connection ? $this->url()->fromRoute('connection/request',['id' => $user->uuid ]) : '';
409
                } else {
410
                    $data['total_connections']  = 0;
411
                    $data['view_total_connections']  = 0;
412
                    $data['request_connection'] = 0;
413
                    $data['common_connection']  = 0;
414
                    $data['link_cancel']        = '';
415
                    $data['link_request']       = '';
416
                }
417
 
418
                return new JsonModel($data);
419
 
420
        }
421
 
422
        return new JsonModel([
423
            'success' => false,
424
            'data' => 'ERROR_METHOD_NOT_ALLOWED'
425
        ]);
426
 
427
 
428
 
429
    }
430
 
431
    public function peopleViewedProfileAction()
432
    {
433
        $currentUserPlugin = $this->plugin('currentUserPlugin');
434
        $currentUser = $currentUserPlugin->getUser();
435
 
436
        $request = $this->getRequest();
437
        if($request->isGet()) {
438
 
439
                $page = (int) $this->params()->fromQuery('page');
440
                $search = Functions::sanitizeFilterString($this->params()->fromQuery('search', ''));
441
 
442
                $mapper = QueryMapper::getInstance($this->adapter);
443
                $select = $mapper->getSql()->select();
444
                $select->columns(['visitor_id' => new Expression('DISTINCT(visitor_id)')]);
445
                $select->from(['p' => ProfileVisitMapper::_TABLE]);
446
                $select->join(['u' => UserMapper::_TABLE], 'p.visitor_id = u.id ', ['uuid', 'first_name', 'last_name', 'image']);
447
                $select->where->equalTo('p.visited_id', $currentUser->id)->and->equalTo('u.status', User::STATUS_ACTIVE);
448
 
449
                if($search) {
450
                    $select->where->NEST->like('first_name', '%' . $search . '%')->or->like('last_name', '%' . $search . '%')->UNNEST;
451
                }
452
                $select->order('first_name','last_name');
453
 
454
                //echo $select->getSqlString($this->adapter->platform); exit;
455
 
456
                $dbSelect = new DbSelect($select, $this->adapter);
457
                $paginator = new Paginator($dbSelect);
458
                $paginator->setCurrentPageNumber($page ? $page : 1);
459
                $paginator->setItemCountPerPage(10);
460
 
461
                $items = [];
462
                $records = $paginator->getCurrentItems();
463
 
333 www 464
                $storage = Storage::getInstance($this->config, $this->adapter);
1 efrain 465
                foreach($records as $record)
466
                {
467
 
468
                    $item = [
469
                        'name' => trim($record['first_name'] . ' ' . $record['last_name']),
283 www 470
                        'image' => $storage->getUserImageForCodeAndFilename($record['uuid'], $record['image']),
1 efrain 471
                        'link_view' => $this->url()->fromRoute('profile/view', ['id' => $record['uuid']  ]),
744 stevensc 472
                        'link_inmail'   => $this->url()->fromRoute('inmail/user', ['id' => $record['uuid']  ]),
1 efrain 473
                    ];
474
 
475
                    array_push($items, $item);
476
                }
477
 
478
                $response = [
479
                    'success' => true,
480
                    'data' => [
481
                        'total' => [
482
                            'count' => $paginator->getTotalItemCount(),
483
                            'pages' => $paginator->getPages()->pageCount,
484
                        ],
485
                        'current' => [
486
                            'items'    => $items,
487
                            'page'     => $paginator->getCurrentPageNumber(),
488
                            'count'    => $paginator->getCurrentItemCount(),
489
                        ]
490
                    ]
491
                ];
492
 
493
 
494
 
495
                return new JsonModel($response);
496
 
497
 
498
        } else {
499
            return new JsonModel([
500
                'success' => false,
501
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
502
            ]);
503
        }
504
    }
505
 
506
 
507
}