Example #1
0
 public function saveCover(&$post, $uid, $type)
 {
     $coverData = !empty($post[$this->inputName]) ? $post[$this->inputName] : '';
     unset($post[$this->inputName]);
     if (empty($coverData)) {
         return true;
     }
     $coverData = FD::makeObject($coverData);
     // Get the cover table.
     $cover = FD::table('Cover');
     // Try to load existing cover
     $state = $cover->load(array('uid' => $uid, 'type' => $type));
     // If no existing cover, then we set the uid and type.
     if (!$state) {
         $cover->uid = $uid;
         $cover->type = $type;
     }
     // If both data does not exist, then we don't proceed to store the data.
     if (empty($coverData->data) && empty($coverData->position)) {
         return true;
     }
     if (!empty($coverData->data)) {
         if ($coverData->data === 'delete') {
             $cover->delete();
             return true;
         }
         $coverObj = FD::makeObject($coverData->data);
         // Get the cover album.
         $album = SocialFieldsUserCoverHelper::getDefaultAlbum($uid, $type);
         // Create the photo object.
         $photo = SocialFieldsUserCoverHelper::createPhotoObject($uid, $type, $album->id, $coverObj->stock->title, false);
         // If there are no cover set for this album, set it as cover.
         if (empty($album->cover_id)) {
             $album->cover_id = $photo->id;
             $album->store();
         }
         // Construct the path to where the photo is temporarily uploaded.
         // $tmpPath = SocialFieldsUserCoverHelper::getPath($this->inputName);
         $tmpPath = dirname($coverObj->stock->path);
         // Get the supposed path of where the cover should be
         // Instead of doing SocialPhotos::getStoragePath, I copied the logic from there but only to create the folders up until albumId section.
         // We do not want JPATH_ROOT to be included in the $storage variable
         $storage = '/' . FD::cleanPath(FD::config()->get('photos.storage.container'));
         FD::makeFolder(JPATH_ROOT . $storage);
         $storage .= '/' . $album->id;
         FD::makeFolder(JPATH_ROOT . $storage);
         $storage .= '/' . $photo->id;
         FD::makeFolder(JPATH_ROOT . $storage);
         // Copy the photo from the temporary path to the storage folder.
         $state = JFolder::copy($tmpPath, JPATH_ROOT . $storage, '', true);
         // If cannot copy out the photo, then don't proceed
         if ($state !== true) {
             $this->setError(JText::_('PLG_FIELDS_COVER_ERROR_UNABLE_TO_MOVE_FILE'));
             return false;
         }
         // Create the photo meta for each available sizes.
         foreach ($coverObj as $key => $value) {
             SocialFieldsUserCoverHelper::createPhotoMeta($photo, $key, $storage . '/' . $value->file);
         }
         // Set the uploaded photo as cover for this user.
         $cover->setPhotoAsCover($photo->id);
     }
     // Set the position of the cover if available.
     if (!empty($coverData->position)) {
         $position = FD::makeObject($coverData->position);
         if (isset($position->x)) {
             $cover->x = $position->x;
         }
         if (isset($position->y)) {
             $cover->y = $position->y;
         }
     }
     // Store the cover object
     $cover->store();
     // And we're done.
     return true;
 }