Proyectos de Subversion LeadersLinked - Services

Rev

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

Rev 549 Rev 550
Línea 1760... Línea 1760...
1760
 
1760
 
1761
        return new JsonModel($data);
1761
        return new JsonModel($data);
Línea 1762... Línea 1762...
1762
    }
1762
    }
1763
 
1763
 
-
 
1764
    /**
1764
    /**
1765
     * Handles changing the user profile's cover image.
1765
     * Cambio de la imagen de fondo superior (cover) del perfil
1766
     * Supports uploading a new cover or deleting the existing one.
1766
     * @return \Laminas\View\Model\JsonModel
1767
     * @return \\Laminas\\View\\Model\\JsonModel
1767
     */
1768
     */
-
 
1769
    public function coverAction()
1768
    public function coverAction()
1770
    {
1769
    {
1771
        // Get current user
Línea -... Línea 1772...
-
 
1772
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1770
        $currentUserPlugin = $this->plugin('currentUserPlugin');
1773
        $currentUser = $currentUserPlugin->getUser();
1771
        $currentUser = $currentUserPlugin->getUser();
1774
 
Línea -... Línea 1775...
-
 
1775
        // Get parameters from route
1772
 
1776
        $user_profile_id    = $this->params()->fromRoute('id');
1773
        $user_profile_id    = $this->params()->fromRoute('id');
-
 
1774
        $operation          = $this->params()->fromRoute('operation');
1777
        $operation          = $this->params()->fromRoute('operation'); // Expected: 'upload' or 'delete'
1775
 
-
 
1776
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
-
 
1777
 
-
 
1778
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
-
 
1779
        if (!$userProfile) {
-
 
Línea 1780... Línea 1778...
1780
            $response = [
1778
 
-
 
1779
        // Fetch user profile
-
 
1780
        $userProfileMapper = UserProfileMapper::getInstance($this->adapter);
1781
                'success' => false,
1781
        $userProfile = $userProfileMapper->fetchOneByUuid($user_profile_id);
Línea 1782... Línea -...
1782
                'data' => 'ERROR_INVALID_PARAMETER'
-
 
1783
            ];
-
 
-
 
1782
 
1784
 
1783
        // Validate profile existence
1785
            return new JsonModel($response);
-
 
1786
        }
-
 
1787
 
1784
        if (!$userProfile) {
1788
 
-
 
1789
 
-
 
1790
        if ($currentUser->id != $userProfile->user_id) {
-
 
1791
            $response = [
1785
            return $this->_createSimpleErrorResponse('ERROR_INVALID_PARAMETER');
Línea 1792... Línea -...
1792
                'success' => false,
-
 
1793
                'data' => 'ERROR_UNAUTHORIZED'
-
 
1794
            ];
1786
        }
1795
 
1787
 
1796
            return new JsonModel($response);
1788
        // Authorize current user against the profile: only the profile owner can modify it
1797
        }
1789
        if ($currentUser->id != $userProfile->user_id) {
Línea -... Línea 1790...
-
 
1790
            return $this->_createSimpleErrorResponse('ERROR_UNAUTHORIZED');
1798
 
1791
        }
1799
 
1792
 
-
 
1793
        $request = $this->getRequest();
1800
 
1794
        if ($request->isPost()) {
1801
        $request = $this->getRequest();
-
 
1802
        if ($request->isPost()) {
-
 
1803
            $storage = Storage::getInstance($this->config, $this->adapter);
1795
            $storage = Storage::getInstance($this->config, $this->adapter);
1804
            $target_path = $storage->getPathUser();
-
 
1805
  
1796
            $target_path = $storage->getPathUser(); // Base path for user-specific files
1806
            if ($operation == 'delete') {
1797
  
1807
                if ($userProfile->cover) {
-
 
-
 
1798
            // Handle cover deletion operation
1808
                    if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->cover)) {
1799
            if ($operation == 'delete') {
-
 
1800
                if ($userProfile->cover) {
-
 
1801
                    // Attempt to delete the existing cover file from storage
-
 
1802
                    if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->cover)) {
1809
                        return new JsonModel([
1803
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
-
 
1804
                    }
-
 
1805
                }
1810
                            'success'   => false,
1806
                // Log the deletion action
-
 
1807
                $this->logger->info('Se borro el cover del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1811
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1808
                $userProfile->cover = ''; // Clear the cover field in the profile model
-
 
1809
                
1812
                        ]);
1810
                // Persist the cleared cover information to the database
1813
                    }
-
 
1814
                }
-
 
1815
 
-
 
1816
                $this->logger->info('Se borro el cover del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
1811
                if (!$userProfileMapper->updateCover($userProfile)) {
Línea 1817... Línea 1812...
1817
                $userProfile->cover = '';
1812
                    // Handle error if database update fails for deletion
1818
            } else {
-
 
-
 
1813
                    return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1819
                $form = new CoverForm($this->config);
1814
                }
1820
                $data     = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
1815
            } else { // Handle cover upload operation
1821
 
-
 
1822
 
-
 
1823
 
-
 
1824
                $form->setData($data);
1816
                $form = new CoverForm($this->config);
1825
 
-
 
1826
                if ($form->isValid()) {
1817
                // Merge POST data and FILES data for the form
-
 
1818
                $data = array_merge($request->getPost()->toArray(), $request->getFiles()->toArray());
-
 
1819
                $form->setData($data);
-
 
1820
 
-
 
1821
                if ($form->isValid()) {
-
 
1822
                    // Set files in storage and select the 'cover' file by its input name
-
 
1823
                    $storage->setFiles($request->getFiles()->toArray());
Línea 1827... Línea -...
1827
 
-
 
1828
                    $storage->setFiles($request->getFiles()->toArray());
-
 
1829
                    if (!$storage->setCurrentFilename('cover')) {
-
 
1830
 
-
 
1831
                        return new JsonModel([
-
 
1832
                            'success'   => false,
-
 
1833
                            'data'   =>  'ERROR_UPLOAD_FILE'
-
 
1834
                        ]);
1824
                    if (!$storage->setCurrentFilename('cover')) { // Ensure 'cover' is the correct file input name from CoverForm
1835
                    }
1825
                        return $this->_createSimpleErrorResponse('ERROR_UPLOAD_FILE');
1836
 
1826
                    }
Línea -... Línea 1827...
-
 
1827
                    
1837
 
1828
                    // Prepare for image resize and save: Get target dimensions from config
1838
             
-
 
1839
 
-
 
1840
                    //echo '$target_path = ' . $target_path . ' cover =  ' . $userProfile->cover;
1829
                    // Example config value: '1200x400'
1841
                    //exit;
-
 
1842
 
1830
                    list($target_width_str, $target_height_str) = explode('x', $this->config['leaderslinked.image_sizes.user_cover_size']);
Línea -... Línea 1831...
-
 
1831
                    $target_width = (int)$target_width_str;
1843
                    list($target_width, $target_height) = explode('x', $this->config['leaderslinked.image_sizes.user_cover_size']);
1832
                    $target_height = (int)$target_height_str;
1844
                    $filename           = 'user-cover-' . uniqid() . '.png';
1833
 
1845
                    $source_filename    = $storage->getTmpFilename();
1834
                    $filename           = 'user-cover-' . uniqid() . '.png'; // Generate unique filename for the new cover
1846
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $currentUser->id, $filename);
1835
                    $source_filename    = $storage->getTmpFilename(); // Temporary path of the uploaded file
1847
 
1836
                    $target_filename    = $storage->composePathToFilename(Storage::TYPE_USER, $currentUser->id, $filename);
1848
                    if (!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
-
 
1849
                        return new JsonModel([
1837
 
1850
                            'success'   => false,
1838
                    // Upload, resize, and save the image
Línea -... Línea 1839...
-
 
1839
                    if (!$storage->uploadImageResize($source_filename, $target_filename, $target_width, $target_height)) {
1851
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1840
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
-
 
1841
                    }
1852
                        ]);
1842
                    
1853
                    }
-
 
1854
                    
-
 
1855
                    if ($userProfile->cover) {
1843
                    // If an old cover exists, delete it from storage
1856
                        if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->cover)) {
-
 
1857
                            return new JsonModel([
1844
                    if ($userProfile->cover) {
Línea 1858... Línea -...
1858
                                'success'   => false,
-
 
-
 
1845
                        if (!$storage->deleteFile($target_path, $currentUser->uuid, $userProfile->cover)) {
1859
                                'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
1846
                            // Decide if this is a critical error. Original code returns error.
1860
                            ]);
1847
                            // Consider logging this and proceeding if deletion of old file is non-critical.
1861
                        }
-
 
1862
                    }
-
 
1863
 
-
 
1864
                    $userProfile->cover = $filename;
1848
                            return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1865
                    if (!$userProfileMapper->updateCover($userProfile)) {
-
 
1866
                        return new JsonModel([
-
 
1867
                            'success'   => false,
1849
                        }
1868
                            'data'   =>  'ERROR_THERE_WAS_AN_ERROR'
-
 
1869
                        ]);
-
 
1870
                    }
-
 
1871
 
1850
                    }
1872
 
-
 
1873
                    $this->logger->info('Se actualizo el cover del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
-
 
1874
                } else {
1851
 
Línea -... Línea 1852...
-
 
1852
                    // Update profile model with new cover filename
-
 
1853
                    $userProfile->cover = $filename;
-
 
1854
                    // Persist the new cover information to the database
1875
                    $messages = [];
1855
                    if (!$userProfileMapper->updateCover($userProfile)) {
1876
                    $form_messages = (array) $form->getMessages();
1856
                        return $this->_createSimpleErrorResponse('ERROR_THERE_WAS_AN_ERROR');
1877
                    foreach ($form_messages  as $fieldname => $field_messages) {
1857
                    }
1878
                        $messages[$fieldname] = array_values($field_messages);
-
 
1879
                    }
1858
 
1880
 
-
 
1881
                    return new JsonModel([
-
 
1882
                        'success'   => false,
-
 
1883
                        'data'   => $messages
-
 
1884
                    ]);
1859
                    // Log successful update
1885
                }
-
 
1886
            }
-
 
1887
            
-
 
Línea 1888... Línea 1860...
1888
            $storage = Storage::getInstance($this->config, $this->adapter);
1860
                    $this->logger->info('Se actualizo el cover del perfil ' . ($userProfile->public == UserProfile::PUBLIC_YES ? 'público' : $userProfile->name), ['user_id' => $userProfile->user_id, 'ip' => Functions::getUserIP()]);
-
 
1861
                } else {
1889
            
1862
                    // Form is invalid, return validation messages
Línea 1890... Línea 1863...
1890
            return new JsonModel([
1863
                    return $this->_createFormErrorResponse($form);
1891
                'success'   => true,
1864
                }
1892
                'data' => $storage->getUserProfileCover($currentUser, $userProfile)
1865
            } // End of upload/delete specific logic
Línea 2146... Línea 2119...
2146
        ];
2119
        ];
Línea 2147... Línea 2120...
2147
 
2120
 
2148
 
2121
 
-
 
2122
        return new JsonModel($data);
-
 
2123
    }
-
 
2124
 
-
 
2125
    // Helper methods for creating standardized JSON responses
-
 
2126
    // These can be used across various actions in this controller
-
 
2127
 
-
 
2128
    /**
-
 
2129
     * Creates a standardized JSON error response with a simple message.
-
 
2130
     * @param string $errorMessageKey Identifier for the error message (e.g., a translation key or constant).
-
 
2131
     * @return JsonModel
-
 
2132
     */
-
 
2133
    private function _createSimpleErrorResponse(string $errorMessageKey): JsonModel
-
 
2134
    {
-
 
2135
        return new JsonModel([
-
 
2136
            'success' => false,
-
 
2137
            'data'    => $errorMessageKey 
-
 
2138
        ]);
-
 
2139
    }
-
 
2140
 
-
 
2141
    /**
-
 
2142
     * Creates a JSON response for form validation errors.
-
 
2143
     * Extracts messages from the form and structures them for the client.
-
 
2144
     * @param \\Laminas\\Form\\FormInterface $form The form instance containing validation messages.
-
 
2145
     * @return JsonModel
-
 
2146
     */
-
 
2147
    private function _createFormErrorResponse(\\Laminas\\Form\\FormInterface $form): JsonModel
-
 
2148
    {
-
 
2149
        $messages = [];
-
 
2150
        $form_messages = (array) $form->getMessages(); // Get all error messages from the form
-
 
2151
        foreach ($form_messages as $fieldname => $field_messages_for_field) {
-
 
2152
            // Ensure messages for each field are in a simple array format for the frontend
-
 
2153
            $messages[$fieldname] = array_values((array)$field_messages_for_field);
-
 
2154
        }
-
 
2155
        return new JsonModel([
-
 
2156
            'success' => false,
-
 
2157
            'data'    => $messages
2149
        return new JsonModel($data);
2158
        ]);