Ejemplo n.º 1
0
 public function avatarAction()
 {
     try {
         /**
          * @var \DDD\Service\Upload $uploadService
          */
         $uploadService = $this->getServiceLocator()->get('service_upload');
         $request = $this->getRequest();
         // check request method
         if ($request->getMethod() !== 'POST') {
             throw new \Exception(TextConstants::AJAX_NO_POST_ERROR);
         }
         // take avatar file and user id from POST
         $avatarFile = $request->getFiles();
         $profileId = $request->getPost('userId');
         // send resoult to service
         $newAvatar = $uploadService->updateAvatar((int) $profileId, [$avatarFile['file']]);
         if (is_array($newAvatar) && $newAvatar['status'] === 'error') {
             return new JsonModel($newAvatar);
         }
         $session = Helper::getSession('Zend_Auth');
         $session['storage']->avatar = $newAvatar;
         $result['status'] = 'success';
         $result['msg'] = TextConstants::SUCCESS_UPDATE;
         $result['src'] = $profileId . '/' . $newAvatar;
         return new JsonModel($result);
     } catch (Exception $e) {
         throw new \Exception($e->getMessage());
     }
 }