Example #1
0
 private function processAvatar()
 {
     $db = FD::db();
     $sql = $db->sql();
     $query = 'select a.*';
     $query .= ' from `#__comprofiler` as a';
     $query .= ' where not exists ( ';
     $query .= '		select b.`id` from `#__social_migrators` as b';
     $query .= ' 			where a.`user_id` = b.`oid` and b.`element` = ' . $db->Quote('avatar') . ' and b.`component` = ' . $db->Quote($this->name);
     $query .= ' )';
     $query .= ' and a.`avatar` is not null';
     $query .= ' and a.`avatarapproved` = 1';
     $query .= ' ORDER BY a.`user_id` ASC';
     $query .= ' LIMIT ' . $this->limit;
     $sql->raw($query);
     $db->setQuery($sql);
     $cbAvatars = $db->loadObjectList();
     if (count($cbAvatars) <= 0) {
         return null;
     }
     foreach ($cbAvatars as $cbAvatar) {
         if (!$cbAvatar->avatar) {
             // no need to process further.
             $this->log('avatar', $cbAvatar->user_id, $cbAvatar->user_id);
             $this->info->setInfo('User ' . $cbAvatar->user_id . ' is using default avatar. no migration is needed.');
             continue;
         }
         $userid = $cbAvatar->user_id;
         // images/avatar/c7a88f6daec02aea3fd3bc4e.jpg
         $imagePath = JPATH_ROOT . '/images/comprofiler/' . $cbAvatar->avatar;
         $tmp = explode('/', $imagePath);
         $filename = $tmp[count($tmp) - 1];
         if (!JFile::exists($imagePath)) {
             $this->log('avatar', $cbAvatar->user_id, $cbAvatar->user_id);
             $this->info->setInfo('User ' . $cbAvatar->user_id . ' the avatar image file is not found from the server. Process aborted.');
             continue;
         }
         // lets copy this file to tmp folder 1st.
         $tmp = JFactory::getConfig()->get('tmp_path');
         $tmpImageFile = $tmp . '/' . md5(JFactory::getDate()->toSql());
         JFile::copy($imagePath, $tmpImageFile);
         $image = FD::image();
         $image->load($tmpImageFile);
         $avatar = FD::avatar($image, $userid, SOCIAL_TYPE_USER);
         // Check if there's a profile photos album that already exists.
         $albumModel = FD::model('Albums');
         // Retrieve the user's default album
         $album = $albumModel->getDefaultAlbum($userid, SOCIAL_TYPE_USER, SOCIAL_ALBUM_PROFILE_PHOTOS);
         // we need to update the album user_id to this current user.
         $album->user_id = $userid;
         $album->store();
         $photo = FD::table('Photo');
         $photo->uid = $userid;
         $photo->type = SOCIAL_TYPE_USER;
         $photo->user_id = $userid;
         $photo->album_id = $album->id;
         $photo->title = $filename;
         $photo->caption = '';
         $photo->ordering = 0;
         // We need to set the photo state to "SOCIAL_PHOTOS_STATE_TMP"
         $photo->state = SOCIAL_PHOTOS_STATE_TMP;
         // Try to store the photo first
         $state = $photo->store();
         // Push all the ordering of the photo down
         $photosModel = FD::model('photos');
         $photosModel->pushPhotosOrdering($album->id, $photo->id);
         // Render photos library
         $photoLib = FD::get('Photos', $image);
         $storage = $photoLib->getStoragePath($album->id, $photo->id);
         $paths = $photoLib->create($storage);
         // Create metadata about the photos
         foreach ($paths as $type => $fileName) {
             $meta = FD::table('PhotoMeta');
             $meta->photo_id = $photo->id;
             $meta->group = SOCIAL_PHOTOS_META_PATH;
             $meta->property = $type;
             $meta->value = $storage . '/' . $fileName;
             $meta->store();
         }
         // Create the avatars now, but we do not want the store function to create stream.
         // so we pass in the option. we will create the stream ourown.
         $options = array('addstream' => false);
         $avatar->store($photo, $options);
         // add photo privacy
         $this->addItemPrivacy('photos.view', $photo->id, SOCIAL_TYPE_PHOTO, $cbAvatar->user_id, '0');
         // add photo stream
         $photo->addPhotosStream('uploadAvatar', $cbAvatar->lastupdatedate);
         // log into mgirator
         $this->log('avatar', $cbAvatar->user_id, $cbAvatar->user_id);
         $this->info->setInfo('User avatar ' . $cbAvatar->user_id . ' is now migrated into EasySocial.');
     }
     //end foreach
     return $this->info;
 }
Example #2
0
 private function processAvatar()
 {
     $config = FD::config();
     $db = FD::db();
     $sql = $db->sql();
     $query = 'select a.*, c.uid as `esgroupid`';
     $query .= ' from `#__community_groups` as a';
     $query .= ' inner join `#__social_migrators` as c on a.`id` = c.`oid` and c.`element` = ' . $db->Quote('groups') . ' and c.`component` = ' . $db->Quote('com_community');
     $query .= ' where not exists ( ';
     $query .= '		select b.`id` from `#__social_migrators` as b';
     $query .= ' 			where a.`id` = b.`oid` and b.`element` = ' . $db->Quote('groupavatar') . ' and b.`component` = ' . $db->Quote($this->name);
     $query .= ' )';
     $query .= ' ORDER BY a.`id` ASC';
     $query .= ' LIMIT ' . $this->limit;
     $sql->raw($query);
     $db->setQuery($sql);
     $jsGroups = $db->loadObjectList();
     if (count($jsGroups) <= 0) {
         return null;
     }
     foreach ($jsGroups as $jsGroup) {
         if (!$jsGroup->avatar) {
             // no need to process further.
             $this->log('groupavatar', $jsGroup->id, $jsGroup->id);
             $this->info->setInfo('Group ' . $jsGroup->id . ' is using default avatar. no migration is needed.');
             continue;
         }
         $imagePath = JPATH_ROOT . '/' . $jsGroup->avatar;
         $tmp = explode('/', $imagePath);
         $filename = $tmp[count($tmp) - 1];
         if (!JFile::exists($imagePath)) {
             $this->log('groupavatar', $jsGroup->id, $jsGroup->id);
             $this->info->setInfo('Group ' . $jsGroup->id . ' the avatar image file is not found from the server. Process aborted.');
             continue;
         }
         // lets copy this file to tmp folder 1st.
         $tmp = JFactory::getConfig()->get('tmp_path');
         $tmpImageFile = $tmp . '/' . md5(JFactory::getDate()->toSql());
         JFile::copy($imagePath, $tmpImageFile);
         $image = FD::image();
         $image->load($tmpImageFile);
         $avatar = FD::avatar($image, $jsGroup->esgroupid, SOCIAL_TYPE_GROUP);
         // Check if there's a profile photos album that already exists.
         $albumModel = FD::model('Albums');
         // Retrieve the group's default album
         $album = $albumModel->getDefaultAlbum($jsGroup->esgroupid, SOCIAL_TYPE_GROUP, SOCIAL_ALBUM_PROFILE_PHOTOS);
         $album->user_id = $jsGroup->ownerid;
         $album->store();
         $photo = FD::table('Photo');
         $photo->uid = $jsGroup->esgroupid;
         $photo->user_id = $jsGroup->ownerid;
         $photo->type = SOCIAL_TYPE_GROUP;
         $photo->album_id = $album->id;
         $photo->title = $filename;
         $photo->caption = '';
         $photo->ordering = 0;
         // We need to set the photo state to "SOCIAL_PHOTOS_STATE_TMP"
         $photo->state = SOCIAL_PHOTOS_STATE_TMP;
         // Try to store the photo first
         $state = $photo->store();
         // Push all the ordering of the photo down
         $photosModel = FD::model('photos');
         $photosModel->pushPhotosOrdering($album->id, $photo->id);
         // Render photos library
         $photoLib = FD::get('Photos', $image);
         $storage = $photoLib->getStoragePath($album->id, $photo->id);
         $paths = $photoLib->create($storage);
         // Create metadata about the photos
         foreach ($paths as $type => $fileName) {
             $meta = FD::table('PhotoMeta');
             $meta->photo_id = $photo->id;
             $meta->group = SOCIAL_PHOTOS_META_PATH;
             $meta->property = $type;
             $meta->value = $storage . '/' . $fileName;
             $meta->store();
         }
         // Create the avatars now, but we do not want the store function to create stream.
         // so we pass in the option. we will create the stream our own.
         $options = array('addstream' => false);
         $avatar->store($photo, $options);
         // @Add stream item when a new event cover is uploaded
         // get the cover update date.
         $uploadDate = $this->getMediaUploadDate('groups.avatar.upload', $jsGroup->id);
         if (!$uploadDate) {
             // if empty, then lets just use event creation date.
             $uploadDate = $jsGroup->created;
         }
         $photo->addPhotosStream('uploadAvatar', $uploadDate);
         // log into mgirator
         $this->log('groupavatar', $jsGroup->id, $photo->id);
         $this->info->setInfo('Group avatar ' . $jsGroup->id . ' is now migrated into EasySocial.');
     }
     return $this->info;
 }
Example #3
0
 /**
  * Allows caller to set profile photo based on the photo that they have.
  *
  * @since   1.0
  * @access  public
  * @return
  */
 public function createAvatar()
 {
     // Check for request forgeries
     FD::checkToken();
     // Only registered users should be allowed to upload photos
     FD::requireLogin();
     // Get the current view
     $view = $this->getCurrentView();
     // Get the photo id
     $id = JRequest::getInt('id');
     // Try to load the photo.
     $photo = FD::table('Photo');
     $photo->load($id);
     // Try to load the photo with the provided id.
     if (!$id || !$photo->id) {
         $view->setMessage(JText::_('COM_EASYSOCIAL_PHOTOS_INVALID_ID_PROVIDED'), SOCIAL_MSG_ERROR);
         return $view->call(__FUNCTION__);
     }
     // Get the photos lib
     $lib = FD::photo($photo->uid, $photo->type, $photo);
     if (!$lib->canUseAvatar()) {
         $view->setMessage(JText::_('COM_EASYSOCIAL_PHOTOS_NO_PERMISSION_TO_USE_PHOTO_AS_AVATAR'), SOCIAL_MSG_ERROR);
         return $view->call(__FUNCTION__);
     }
     // Get the image object for the photo
     // Use "original" not "stock" because it might be rotated before this.
     $image = $photo->getImageObject('stock');
     if ($image === false) {
         $image = $photo->getImageObject('original');
     }
     // Need to rotate as necessary here because we're loading up using the stock photo and the stock photo
     // is as is when the user initially uploaded.
     $image->rotate($photo->getAngle());
     $tmp = JFactory::getConfig()->get('tmp_path');
     $tmpPath = $tmp . '/' . md5($photo->id) . $image->getExtension();
     $image->save($tmpPath);
     unset($image);
     $image = FD::image();
     $image->load($tmpPath);
     // Get the current user.
     $my = FD::user();
     // Load up the avatar library
     $avatar = FD::avatar($image, $photo->uid, $photo->type);
     // Crop the image to follow the avatar format. Get the dimensions from the request.
     $width = JRequest::getVar('width');
     $height = JRequest::getVar('height');
     $top = JRequest::getVar('top');
     $left = JRequest::getVar('left');
     // We need to get the temporary path so that we can delete it later once everything is done.
     $avatar->crop($top, $left, $width, $height);
     // Create the avatars now
     $avatar->store($photo);
     // Delete the temporary file.
     JFile::delete($tmpPath);
     return $view->call(__FUNCTION__, $photo);
 }
Example #4
0
 public function createAvatar($value, $uid, $createStream = true, $deleteImage = true)
 {
     $value = FD::makeObject($value);
     if (!empty($value->data)) {
         $value->data = FD::makeObject($value->data);
     }
     if ($value->type === 'remove') {
         $table = FD::table('avatar');
         $state = $table->load(array('uid' => $uid, 'type' => $this->group));
         if ($state) {
             $table->delete();
             if ($this->group == SOCIAL_TYPE_USER) {
                 $user = FD::user($uid);
                 // Prepare the dispatcher
                 FD::apps()->load(SOCIAL_TYPE_USER);
                 $dispatcher = FD::dispatcher();
                 $args = array(&$user, &$table);
                 // @trigger: onUserAvatarRemove
                 $dispatcher->trigger(SOCIAL_TYPE_USER, 'onUserAvatarRemove', $args);
             }
         }
         return true;
     }
     if ($value->type === 'gallery') {
         $table = FD::table('avatar');
         $state = $table->load(array('uid' => $uid, 'type' => $this->group));
         if (!$state) {
             $table->uid = $uid;
             $table->type = $this->group;
         }
         $table->avatar_id = $value->source;
         $table->store();
         return true;
     }
     if ($value->type === 'upload') {
         $data = new stdClass();
         if (!empty($value->path)) {
             $image = FD::image();
             $image->load($value->path);
             $avatar = FD::avatar($image, $uid, $this->group);
             // Check if there's a profile photos album that already exists.
             $albumModel = FD::model('Albums');
             // Retrieve the user's default album
             $album = $albumModel->getDefaultAlbum($uid, $this->group, SOCIAL_ALBUM_PROFILE_PHOTOS);
             $photo = FD::table('Photo');
             $photo->uid = $uid;
             $photo->type = $this->group;
             $photo->user_id = $this->group == SOCIAL_TYPE_USER ? $uid : FD::user()->id;
             $photo->album_id = $album->id;
             $photo->title = $value->name;
             $photo->caption = '';
             $photo->ordering = 0;
             // We need to set the photo state to "SOCIAL_PHOTOS_STATE_TMP"
             $photo->state = SOCIAL_PHOTOS_STATE_TMP;
             // Try to store the photo first
             $state = $photo->store();
             if (!$state) {
                 $this->setError(JText::_('PLG_FIELDS_AVATAR_ERROR_CREATING_PHOTO_OBJECT'));
                 return false;
             }
             // Push all the ordering of the photo down
             $photosModel = FD::model('photos');
             $photosModel->pushPhotosOrdering($album->id, $photo->id);
             // If album doesn't have a cover, set the current photo as the cover.
             if (!$album->hasCover()) {
                 $album->cover_id = $photo->id;
                 // Store the album
                 $album->store();
             }
             // Get the photos library
             $photoLib = FD::get('Photos', $image);
             $storage = $photoLib->getStoragePath($album->id, $photo->id);
             $paths = $photoLib->create($storage);
             // Create metadata about the photos
             foreach ($paths as $type => $fileName) {
                 $meta = FD::table('PhotoMeta');
                 $meta->photo_id = $photo->id;
                 $meta->group = SOCIAL_PHOTOS_META_PATH;
                 $meta->property = $type;
                 $meta->value = $storage . '/' . $fileName;
                 $meta->store();
             }
             // Synchronize Indexer
             $indexer = FD::get('Indexer');
             $template = $indexer->getTemplate();
             $template->setContent($photo->title, $photo->caption);
             //$url 	= FRoute::photos(array('layout' => 'item', 'id' => $photo->getAlias()));
             $url = $photo->getPermalink();
             $url = '/' . ltrim($url, '/');
             $url = str_replace('/administrator/', '/', $url);
             $template->setSource($photo->id, SOCIAL_INDEXER_TYPE_PHOTOS, $photo->uid, $url);
             $template->setThumbnail($photo->getSource('thumbnail'));
             $indexer->index($template);
             // Crop the image to follow the avatar format. Get the dimensions from the request.
             if (!empty($value->data) && is_object($value->data)) {
                 $width = $value->data->width;
                 $height = $value->data->height;
                 $top = $value->data->top;
                 $left = $value->data->left;
                 $avatar->crop($top, $left, $width, $height);
             }
             $options = array();
             // Create the avatars now
             if (!$createStream) {
                 $options = array('addstream' => false);
             }
             $options['deleteimage'] = false;
             $avatar->store($photo, $options);
         }
         return true;
     }
 }
Example #5
0
 /**
  * Responsible to store the uploaded images.
  *
  * @since	1.0
  * @access	public
  * @param	null
  *
  * @author	Mark Lee <*****@*****.**>
  */
 public function upload($files)
 {
     // Get config object.
     $config = FD::config();
     // Do not proceed if image doesn't exist.
     if (empty($files) || !isset($files['file'])) {
         $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_AVATARS_FILE_UNAVAILABLE'));
         return false;
     }
     // Get the single file input since the $files is an array.
     $file = $files['file'];
     // Get the default avatars storage location.
     $avatarsPath = JPATH_ROOT . '/' . FD::cleanPath($config->get('avatars.storage.container'));
     // Test if the avatars path folder exists. If it doesn't we need to create it.
     if (!FD::makeFolder($avatarsPath)) {
         $this->setError(JText::_('Errors when creating default container for avatar'));
         return false;
     }
     // Get the defaults avatar path.
     $defaultsPath = $avatarsPath . '/' . FD::cleanPath($config->get('avatars.storage.default'));
     // Ensure that the defaults path exist
     if (!FD::makeFolder($defaultsPath)) {
         $this->setError(JText::_('Errors when creating default path for avatar'));
         return false;
     }
     // Get the default avatars storage location for this type.
     $typePath = $config->get('avatars.storage.defaults.' . $this->type);
     $storagePath = $defaultsPath . '/' . FD::cleanPath($typePath);
     // Ensure storage path exists.
     if (!FD::makeFolder($storagePath)) {
         FD::logError(__FILE__, __LINE__, 'DEFAULT_AVATARS: Unable to create the path ' . $storagePath);
         $this->setError(JText::_('Errors when creating default path for avatar'));
         return false;
     }
     // Get the profile id and construct the final path.
     $idPath = FD::cleanPath($this->uid);
     $storagePath = $storagePath . '/' . $idPath;
     // Ensure storage path exists.
     if (!FD::makeFolder($storagePath)) {
         $this->setError(JText::_('Errors when creating default path for avatar'));
         return false;
     }
     // Get the image library to perform some checks.
     $image = FD::get('Image');
     $image->load($file['tmp_name']);
     // Test if the image is really a valid image.
     if (!$image->isValid()) {
         FD::logError(__FILE__, __LINE__, 'DEFAULT_AVATARS: Image uploaded ' . $file['name'] . ' is invalid');
         $this->setError(JText::_('COM_EASYSOCIAL_PROFILES_DEFAULT_AVATARS_FILE_NOT_IMAGE'));
         return false;
     }
     // Process avatar storage.
     $avatar = FD::avatar($image, $this->uid, $this->type);
     // Let's create the avatar.
     $sizes = $avatar->create($storagePath);
     // Assign the values back.
     foreach ($sizes as $size => $url) {
         $this->{$size} = $url;
     }
     return true;
 }