/** * Process Uploaded Photo Cover * @return [JSON OBJECT] [description] */ public function ajaxCoverUpload() { $jinput = JFactory::getApplication()->input; $parentId = $jinput->get->get('parentId', null, 'Int'); $type = strtolower($jinput->get->get('type', null, 'String')); $file = $jinput->files->get('uploadCover'); $config = CFactory::getConfig(); $my = CFactory::getUser(); $now = new JDate(); $addStream = true; if (!CImageHelper::checkImageSize(filesize($file['tmp_name']))) { $msg['error'] = JText::sprintf('COM_COMMUNITY_VIDEOS_IMAGE_FILE_SIZE_EXCEEDED_MB', CFactory::getConfig()->get('maxuploadsize')); echo json_encode($msg); exit; } //check if file is allwoed if (!CImageHelper::isValidType($file['type'])) { $msg['error'] = JText::_('COM_COMMUNITY_IMAGE_FILE_NOT_SUPPORTED'); echo json_encode($msg); exit; } CImageHelper::autoRotate($file['tmp_name']); $album = JTable::getInstance('Album', 'CTable'); if (!($albumId = $album->isCoverExist($type, $parentId))) { $albumId = $album->addCoverAlbum($type, $parentId); } $imgMaxWidht = 1140; // Get a hash for the file name. $fileName = JApplication::getHash($file['tmp_name'] . time()); $hashFileName = JString::substr($fileName, 0, 24); if (!JFolder::exists(JPATH_ROOT . '/' . $config->getString('imagefolder') . '/cover/' . $type . '/' . $parentId . '/')) { JFolder::create(JPATH_ROOT . '/' . $config->getString('imagefolder') . '/cover/' . $type . '/' . $parentId . '/'); } $dest = JPATH_ROOT . '/' . $config->getString('imagefolder') . '/cover/' . $type . '/' . $parentId . '/' . md5($type . '_cover' . time()) . CImageHelper::getExtension($file['type']); $thumbPath = JPATH_ROOT . '/' . $config->getString('imagefolder') . '/cover/' . $type . '/' . $parentId . '/thumb_' . md5($type . '_cover' . time()) . CImageHelper::getExtension($file['type']); // Generate full image if (!CImageHelper::resizeProportional($file['tmp_name'], $dest, $file['type'], $imgMaxWidht)) { $msg['error'] = JText::sprintf('COM_COMMUNITY_ERROR_MOVING_UPLOADED_FILE', $storageImage); echo json_encode($msg); exit; } CPhotos::generateThumbnail($file['tmp_name'], $thumbPath, $file['type']); $cTable = JTable::getInstance(ucfirst($type), 'CTable'); $cTable->load($parentId); if ($cTable->setCover(str_replace(JPATH_ROOT . '/', '', $dest))) { $photo = JTable::getInstance('Photo', 'CTable'); $photo->albumid = $albumId; $photo->image = str_replace(JPATH_ROOT . '/', '', $dest); $photo->caption = $file['name']; $photo->filesize = $file['size']; $photo->creator = $my->id; $photo->created = $now->toSql(); $photo->published = 1; $photo->thumbnail = str_replace(JPATH_ROOT . '/', '', $thumbPath); if ($photo->store()) { $album->load($albumId); $album->photoid = $photo->id; $album->store(); } $msg['success'] = true; $msg['path'] = JURI::root() . str_replace(JPATH_ROOT . '/', '', $dest); $msg['cancelbutton'] = JText::_('COM_COMMUNITY_CANCEL_BUTTON'); $msg['savebutton'] = JText::_("COM_COMMUNITY_SAVE_BUTTON"); // Generate activity stream. $act = new stdClass(); $act->cmd = 'cover.upload'; $act->actor = $my->id; $act->target = 0; $act->title = ''; $act->content = ''; $act->access = $type == 'profile' ? $my->_cparams->get("privacyPhotoView") : 0; $act->app = 'cover.upload'; $act->cid = $photo->id; $act->comment_id = CActivities::COMMENT_SELF; $act->comment_type = 'cover.upload'; $act->groupid = $type == 'group' ? $parentId : 0; $act->eventid = $type == 'event' ? $parentId : 0; $act->group_access = $type == 'group' ? $cTable->approvals : 0; $act->event_access = $type == 'event' ? $cTable->permission : 0; $act->like_id = CActivities::LIKE_SELF; $act->like_type = 'cover.upload'; $params = new JRegistry(); $params->set('attachment', str_replace(JPATH_ROOT . '/', '', $dest)); $params->set('type', $type); $params->set('album_id', $albumId); $params->set('photo_id', $photo->id); //assign points based on types. switch ($type) { case 'group': $addStream = CUserPoints::assignPoint('group.cover.upload'); break; case 'event': $addStream = CUserPoints::assignPoint('event.cover.upload'); break; default: $addStream = CUserPoints::assignPoint('profile.cover.upload'); } if ($type == 'event') { $event = JTable::getInstance('Event', 'CTable'); $event->load($parentId); $group = JTable::getInstance('Group', 'CTable'); $group->load($event->contentid); if ($group->approvals == 1) { $addStream = false; } } if ($addStream) { // Add activity logging if ($type != 'profile' || $type == 'profile' && $parentId == $my->id) { CActivityStream::add($act, $params->toString()); } } echo json_encode($msg); exit; } }