Exemple #1
0
 public function ajaxCropPhoto($params)
 {
     if (!isset($params['coords']) || !isset($params['view_size'])) {
         return array('result' => false, 'case' => 0);
     }
     $coords = $params['coords'];
     $viewSize = $params['view_size'];
     $path = null;
     $localFile = false;
     $avatarService = BOL_AvatarService::getInstance();
     if (!empty($params['entityType']) && !empty($params['id'])) {
         $item = $avatarService->getAvatarChangeGalleryItem($params['entityType'], $params['entityId'], $params['id']);
         if (!$item || empty($item['path']) || !OW::getStorage()->fileExists($item['path'])) {
             return array('result' => false, 'case' => 1);
         }
         $path = $item['path'];
     } else {
         if (isset($params['url'])) {
             $path = UTIL_Url::getLocalPath($params['url']);
             if (!OW::getStorage()->fileExists($path)) {
                 if (!file_exists($path)) {
                     return array('result' => false, 'case' => 2);
                 }
                 $localFile = true;
             }
         }
     }
     $userId = OW_Auth::getInstance()->getUserId();
     if ($userId) {
         $avatar = $avatarService->findByUserId($userId);
         try {
             $event = new OW_Event('base.before_avatar_change', array('userId' => $userId, 'avatarId' => $avatar ? $avatar->id : null, 'upload' => false, 'crop' => true));
             OW::getEventManager()->trigger($event);
             if (!$avatarService->cropAvatar($userId, $path, $coords, $viewSize, array('isLocalFile' => $localFile))) {
                 return array('result' => false, 'case' => 6);
             }
             $avatar = $avatarService->findByUserId($userId, false);
             $event = new OW_Event('base.after_avatar_change', array('userId' => $userId, 'avatarId' => $avatar ? $avatar->id : null, 'upload' => false, 'crop' => true));
             OW::getEventManager()->trigger($event);
             return array('result' => true, 'modearationStatus' => $avatar->status, 'url' => $avatarService->getAvatarUrl($userId, 1, null, false, false), 'bigUrl' => $avatarService->getAvatarUrl($userId, 2, null, false, false));
         } catch (Exception $e) {
             return array('result' => false, 'case' => 4);
         }
     } else {
         $key = $avatarService->getAvatarChangeSessionKey();
         $path = $avatarService->getTempAvatarPath($key, 3);
         if (!file_exists($path)) {
             return array('result' => false, 'case' => 5);
         }
         $avatarService->cropTempAvatar($key, $coords, $viewSize);
         return array('result' => true, 'url' => $avatarService->getTempAvatarUrl($key, 1), 'bigUrl' => $avatarService->getTempAvatarUrl($key, 2));
     }
 }