Proyectos de Subversion LeadersLinked - Services

Rev

Rev 551 | Rev 554 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 551 Rev 553
Línea 263... Línea 263...
263
 
263
 
264
        $request = $this->getRequest();
264
        $request = $this->getRequest();
Línea 265... Línea 265...
265
        $id = $this->params()->fromRoute('id');
265
        $id = $this->params()->fromRoute('id');
266
 
-
 
267
        if (!$id) {
266
 
268
            $data = [
267
        if (!$id) {
269
                'success'   => false,
-
 
270
                'data'   => 'ERROR_INVALID_PARAMETER'
-
 
271
            ];
-
 
272
 
268
            // Use helper for consistency
Línea 273... Línea -...
273
            return new JsonModel($data);
-
 
274
        }
-
 
275
 
269
            return $this->_createSimpleErrorResponse('ERROR_INVALID_PARAMETER');
276
 
270
        }
277
 
271
 
278
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
-
 
279
        $userProfile = $userProfileMapper->fetchOneByUuid($id);
272
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
280
        if (!$userProfile) {
273
        $userProfile = $userProfileMapper->fetchOneByUuid($id);
281
            $data = [
-
 
282
                'success'   => false,
-
 
283
                'data'   => 'ERROR_RECORD_NOT_FOUND'
-
 
284
            ];
274
        if (!$userProfile) {
Línea 285... Línea -...
285
 
-
 
-
 
275
             // Use helper for consistency
286
            return new JsonModel($data);
276
            return $this->_createSimpleErrorResponse('ERROR_RECORD_NOT_FOUND');
287
        }
-
 
288
 
277
        }
289
 
278
 
290
        if ($currentUser->id != $userProfile->user_id) {
-
 
291
            $response = [
-
 
292
                'success' => false,
-
 
293
                'data' => 'ERROR_UNAUTHORIZED'
279
        // Authorize: Check if current user owns the profile
Línea -... Línea 280...
-
 
280
        if ($currentUser->id != $userProfile->user_id) {
294
            ];
281
             // Use helper for consistency
295
 
-
 
296
            return new JsonModel($response);
282
            return $this->_createSimpleErrorResponse('ERROR_UNAUTHORIZED');
297
        }
283
        }
298
 
-
 
299
        if (!$userProfile->public == UserProfile::PUBLIC_YES) {
-
 
300
            $data = [
-
 
301
                'success'   => false,
284
 
Línea 302... Línea 285...
302
                'data'   => 'ERROR_PUBLIC_PROFILE'
285
        // Prevent deletion of the public profile
-
 
286
        if ($userProfile->public == UserProfile::PUBLIC_YES) {
-
 
287
            // Use helper for consistency
-
 
288
            return $this->_createSimpleErrorResponse('ERROR_PUBLIC_PROFILE');
-
 
289
        }
-
 
290
 
-
 
291
        if ($request->isPost()) {
-
 
292
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
293
            $target_path = $storage->getPathUser();
-
 
294
            $user_uuid = $currentUser->uuid; // Assuming user UUID matches profile owner's UUID for path
-
 
295
 
-
 
296
            // Attempt to delete associated files first
-
 
297
            $image_deleted = true;
-
 
298
            if ($userProfile->image) {
-
 
299
                $image_deleted = $storage->deleteFile($target_path, $user_uuid, $userProfile->image);
-
 
300
                if (!$image_deleted) {
-
 
301
                    $this->logger->err("Failed to delete profile image file: {$target_path}/{$user_uuid}/{$userProfile->image}", ['user_id' => $userProfile->user_id]);
-
 
302
                    // Decide if this is a critical error. Proceeding with DB deletion for now.
-
 
303
                }
-
 
304
            }
-
 
305
            $cover_deleted = true;
-
 
306
            if ($userProfile->cover) {
-
 
307
                $cover_deleted = $storage->deleteFile($target_path, $user_uuid, $userProfile->cover);
-
 
308
                 if (!$cover_deleted) {
303
            ];
309
                    $this->logger->err("Failed to delete profile cover file: {$target_path}/{$user_uuid}/{$userProfile->cover}", ['user_id' => $userProfile->user_id]);
304
 
310
                    // Decide if this is a critical error. Proceeding with DB deletion for now.
305
            return new JsonModel($data);
311
                }
Línea -... Línea 312...
-
 
312
            }
306
        }
313
 
307
 
314
            // Delete the database record
308
        if ($request->isPost()) {
315
            $result = $userProfileMapper->delete($userProfile);
309
            $result = $userProfileMapper->delete($userProfile);
316
            if ($result) {
310
            if ($result) {
317
                $this->logger->info('Se borro el perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
311
                $this->logger->info('Se borro el perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
-
 
312
 
-
 
313
                $data = [
318
 
314
                    'success' => true,
319
                // Success response
315
                    'data' => 'LABEL_RECORD_DELETED'
-
 
316
                ];
-
 
317
            } else {
-
 
318
 
320
                $data = [
319
                $data = [
321
                    'success' => true,
320
                    'success'   => false,
-
 
321
                    'data'      => $userProfileMapper->getError()
322
                    'data' => 'LABEL_RECORD_DELETED'
322
                ];
323
                ];
323
 
-
 
324
                return new JsonModel($data);
-
 
325
            }
-
 
326
        } else {
324
            } else {
Línea 327... Línea 325...
327
            $data = [
325
                // Use helper for DB error
-
 
326
                $data = $this->_createSimpleErrorResponse($userProfileMapper->getError() ?: 'ERROR_THERE_WAS_AN_ERROR');
328
                'success' => false,
327
            }
Línea 329... Línea 328...
329
                'data' => 'ERROR_METHOD_NOT_ALLOWED'
328
        } else {
330
            ];
329
            // Use helper for method error
331
 
330
            $data = $this->_createSimpleErrorResponse('ERROR_METHOD_NOT_ALLOWED');
Línea 1598... Línea 1597...
1598
 
1597
 
Línea 1599... Línea 1598...
1599
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1598
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1600
 
1599
 
1601
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1600
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
1602
        if (!$userProfile) {
-
 
1603
            $response = [
1601
        if (!$userProfile) {
1604
                'success' => false,
-
 
1605
                'data' => 'ERROR_INVALID_PARAMETER'
-
 
1606
            ];
-
 
1607
 
1602
            // Use helper
Línea -... Línea 1603...
-
 
1603
            return $this->_createSimpleErrorResponse('ERROR_INVALID_PARAMETER');
1608
            return new JsonModel($response);
1604
        }
1609
        }
1605
 
1610
 
-
 
1611
        if ($currentUser->id != $userProfile->user_id) {
1606
        // Authorize: Check if current user owns the profile
1612
            $response = [
-
 
1613
                'success' => false,
-
 
1614
                'data' => 'ERROR_UNAUTHORIZED'
-
 
1615
            ];
1607
        if ($currentUser->id != $userProfile->user_id) {
Línea 1616... Línea -...
1616
 
-
 
1617
            return new JsonModel($response);
-
 
1618
        }
1608
            // Use helper
1619
 
1609
            return $this->_createSimpleErrorResponse('ERROR_UNAUTHORIZED');
Línea 1620... Línea 1610...
1620
 
1610
        }
1621
 
1611
 
-
 
1612
        $request = $this->getRequest();
Línea 1622... Línea 1613...
1622
        $request = $this->getRequest();
1613
        if ($request->isPost()) {
1623
        if ($request->isPost()) {
1614
            
Línea 1624... Línea 1615...
1624
            
1615
            $storage = Storage::getInstance($this->config, $this->adapter);
-
 
1616
            $target_path = $storage->getPathUser();
1625
            $storage = Storage::getInstance($this->config, $this->adapter);
1617
            $user_uuid = $currentUser->uuid; // Assuming user UUID for path
1626
            $target_path = $storage->getPathUser();
-
 
1627
            
1618
            
1628
            if ($operation == 'delete') {
1619
            if ($operation == 'delete') {
1629
                $this->logger->info('Se borro el image del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
-
 
1630
 
1620
                $this->logger->info('Se borro el image del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1631
                if ($userProfile->image) {
1621
 
Línea -... Línea 1622...
-
 
1622
                if ($userProfile->image) {
1632
                    if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->image)) {
1623
                    // Attempt deletion
1633
                        return new JsonModel([
1624
                    if (!$storage->deleteFile($target_path, $user_uuid, $userProfile->image)) {
1634
                            'success'   => false,
-
 
1635
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1625
                        // Use helper
1636
                        ]);
1626
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1637
                    }
-
 
1638
                }
1627
                    }
1639
 
1628
                }
1640
                $userProfile->image = '';
1629
 
1641
                if (!$userProfileMapper->updateImage($userProfile)) {
1630
                // Clear image field and update DB
Línea 1642... Línea 1631...
1642
                    return new JsonModel([
1631
                $userProfile->image = '';
Línea 1643... Línea 1632...
1643
                        'success'   => false,
1632
                if (!$userProfileMapper->updateImage($userProfile)) {
Línea 1644... Línea 1633...
1644
                        'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1633
                    // Use helper
1645
                    ]);
-
 
1646
                }
1634
                    return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1647
            } else {
-
 
1648
                $form = new ImageForm($this->config);
1635
                }
1649
                $data     = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1636
            } else { // Handle upload
1650
 
1637
                $form = new ImageForm($this->config);
1651
                $form->setData($data);
-
 
1652
 
1638
                $data = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
Línea 1653... Línea -...
1653
                if ($form->isValid()) {
-
 
-
 
1639
 
-
 
1640
                $form->setData($data);
-
 
1641
 
-
 
1642
                if ($form->isValid()) {
1654
 
1643
 
1655
                    $storage->setFiles($request->getFiles()->toArray());
-
 
1656
                    
1644
                    $storage->setFiles($request->getFiles()->toArray());
1657
                    if (!$storage->setCurrentFilename('error')) {
1645
                    // *** FIX: Use correct input field name 'image' ***
1658
 
1646
                    if (!$storage->setCurrentFilename('image')) { 
Línea 1659... Línea -...
1659
                        return new JsonModel([
-
 
-
 
1647
                        // Use helper
1660
                            'success'   => false,
1648
                        return $this->_createSimpleErrorResponse('ERROR_UPLOAD_FILE');
1661
                            'data'   =>  'ERROR_UPLOAD_FILE'
-
 
1662
                        ]);
1649
                    }
1663
                    }
1650
 
1664
 
-
 
1665
                
1651
                    // Prepare for resize and save
Línea -... Línea 1652...
-
 
1652
                    list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
1666
 
1653
                    $target_width = (int)$target_width_str;
1667
                    list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.user_size']);
1654
                    $target_height = (int)$target_height_str;
1668
                    $source_filename    = $storage->getTmpFilename();
-
 
1669
                    $filename           = 'user-profile-' . uniqid() . '.png';
1655
                    
1670
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $currentUser->uuid, $filename); 
1656
                    $source_filename    = $storage->getTmpFilename();
1671
                   
-
 
1672
 
1657
                    $filename           = 'user-profile-' . uniqid() . '.png';
1673
                    if (!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
1658
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $user_uuid, $filename); 
Línea -... Línea 1659...
-
 
1659
                   
1674
                        return new JsonModel([
1660
                    // Upload, resize, save
1675
                            'success'   => false,
1661
                    if (!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
1676
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
1677
                        ]);
1662
                        // Use helper
1678
                    }
1663
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1679
                    
-
 
1680
                    if ($userProfile->image) {
1664
                    }
Línea -... Línea 1665...
-
 
1665
                    
1681
                        if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->image)) {
1666
                    // Delete old image if exists
Línea 1682... Línea 1667...
1682
                            return new JsonModel([
1667
                    if ($userProfile->image) {
1683
                                'success'   => false,
1668
                        if (!$storage->deleteFile($target_path, $user_uuid, $userProfile->image)) {
1684
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1669
                            // Use helper
1685
                            ]);
-
 
Línea 1686... Línea 1670...
1686
                        }
1670
                            return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1687
                    }
1671
                        }
1688
 
1672
                    }
1689
                    $userProfile->image = $filename;
1673
 
1690
                    if (!$userProfileMapper->updateImage($userProfile)) {
-
 
1691
                        return new JsonModel([
1674
                    // Update profile model and DB
Línea -... Línea 1675...
-
 
1675
                    $userProfile->image = $filename;
1692
                            'success'   => false,
1676
                    if (!$userProfileMapper->updateImage($userProfile)) {
1693
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1677
                        // Use helper
1694
                        ]);
-
 
1695
                    }
1678
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1696
 
1679
                    }
1697
                    if ($userProfile->public == UserProfile::PUBLIC_YES) {
-
 
1698
                        
1680
 
1699
                        $filename    = 'user-' . uniqid() . '.png';
1681
                    // Special handling: If this is the PUBLIC profile, also update the main user image
Línea -... Línea 1682...
-
 
1682
                    if ($userProfile->public == UserProfile::PUBLIC_YES) {
1700
                        $source_filename = $target_filename;
1683
                        
1701
                        $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $currentUser->uuid, $filename); 
-
 
1702
 
1684
                        $main_user_filename = 'user-' . uniqid() . '.png'; // Use different name for safety?
1703
                        
1685
                        // $source_filename_for_copy = $target_filename; // The newly saved profile image
-
 
1686
                        $target_filename_main_user = $storage->composePathToFilename(Storage::TYPE_USER, $user_uuid, $main_user_filename); 
-
 
1687
                        
-
 
1688
                        // Copy the *newly processed* profile image to be the main user image
1704
                        if (!$storage->copyFile($source_filename, $target_filename)) {
1689
                        if (!$storage->copyFile($target_filename, $target_filename_main_user)) { // Source is the *new* profile image
Línea 1705... Línea 1690...
1705
                            return new JsonModel([
1690
                            // Use helper
1706
                                'success'   => false,
1691
                             return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1707
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1692
                        }
1708
                            ]);
1693
                        
1709
                        }
-
 
1710
                        
-
 
1711
                        if ($currentUser->image) {
-
 
1712
                            if (!$storage->deleteFile($target_path, $currentUser->uuid, $currentUser->image)) {
-
 
1713
                                return new JsonModel([
-
 
1714
                                    'success'   => false,
-
 
1715
                                    'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
1716
                                ]);
-
 
1717
                            }
1694
                        // Delete the old main user image if it exists
1718
                        }
1695
                        if ($currentUser->image) {
Línea -... Línea 1696...
-
 
1696
                            if (!$storage->deleteFile($target_path, $user_uuid, $currentUser->image)) {
-
 
1697
                                 // Use helper
1719
 
1698
                                return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1720
                        $currentUser->image = $filename;
-
 
1721
 
1699
                            }
1722
                        $userMapper = UserMapper::getInstance($this->adapter);
1700
                        }
1723
                        $userMapper->updateImage($currentUser);
1701
 
1724
                    }
1702
                        // Update current user model and DB
1725
 
1703
                        $currentUser->image = $main_user_filename;
1726
                    $this->logger->info('Se actualizo el image del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1704
                        $userMapper = UserMapper::getInstance($this->adapter);
1727
                } else {
-
 
1728
                    $messages = [];
1705
                        if (!$userMapper->updateImage($currentUser)) {
1729
                    $form_messages = (array) $form->getMessages();
1706
                             // Use helper
1730
                    foreach ($form_messages  as $fieldname => $field_messages) {
1707
                             return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1731
                        $messages[$fieldname] = array_values($field_messages);
-
 
Línea 1732... Línea 1708...
1732
                    }
1708
                        }
1733
 
1709
                    }
1734
                    return new JsonModel([
1710
 
1735
                        'success'   => false,
-
 
1736
                        'data'   => $messages
-
 
1737
                    ]);
-
 
1738
                }
-
 
1739
            }
1711
                    $this->logger->info('Se actualizo el image del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
Línea 1740... Línea 1712...
1740
            
1712
                } else {
1741
            $storage = Storage::getInstance($this->config, $this->adapter);
1713
                    // Use helper for form errors
1742
            
1714
                    return $this->_createFormErrorResponse($form);
Línea 1822... Línea 1794...
1822
                    // Set files in storage and select the 'cover' file by its input name
1794
                    // Set files in storage and select the 'cover' file by its input name
1823
                    $storage->setFiles($request->getFiles()->toArray());
1795
                    $storage->setFiles($request->getFiles()->toArray());
1824
                    if (!$storage->setCurrentFilename('cover')) { // Ensure 'cover' is the correct file input name from CoverForm
1796
                    if (!$storage->setCurrentFilename('cover')) { // Ensure 'cover' is the correct file input name from CoverForm
1825
                        return $this->_createSimpleErrorResponse('ERROR_UPLOAD_FILE');
1797
                        return $this->_createSimpleErrorResponse('ERROR_UPLOAD_FILE');
1826
                    }
1798
                    }
1827
                    
1799
 
1828
                    // Prepare for image resize and save: Get target dimensions from config
1800
                    // Prepare for image resize and save: Get target dimensions from config
1829
                    // Example config value: '1200x400'
1801
                    // Example config value: '1200x400'
1830
                    list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.user_cover_size']);
1802
                    list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.user_cover_size']);
1831
                    $target_width = (int)$target_width_str;
1803
                    $target_width = (int)$target_width_str;
1832
                    $target_height = (int)$target_height_str;
1804
                    $target_height = (int)$target_height_str;