Enh: Added ContentContainer::findRecord($guid) for loading a ContentContainerActiveRecord by guid

Enh: Implemented `ProfileImage::render($width, $cfg)` for e.g. `$container->getProfileImage()->render(400)`
This commit is contained in:
buddh4 2019-10-04 14:23:42 +02:00
parent 568e3b350d
commit 5098ae7653
8 changed files with 123 additions and 21 deletions

View File

@ -69,3 +69,5 @@ HumHub Change Log (DEVELOP)
- Enh: Added show password feature for password form elements
- Fix: `humhub\components\acces\AccessValidator` now returns http code `401` for guest and `403` for users by default
- Enh: Added generic `humhub\modules\ui\helpers\models\ItemDrop` model for drag/drop reordering
- Enh: Added `ContentContainer::findRecord($guid)` for loading a ContentContainerActiveRecord by guid
- Enh: Implemented `ProfileImage::render($width, $cfg)` for e.g. `$container->getProfileImage()->render(400)`

View File

@ -9,7 +9,11 @@
namespace humhub\libs;
use humhub\modules\file\libs\ImageConverter;
use humhub\modules\space\models\Space;
use humhub\modules\space\widgets\ProfileBannerImage as SpaceImage;
use humhub\modules\user\widgets\Image as UserImage;
use yii\helpers\FileHelper;
use yii\helpers\Html;
/**
* ProfileBannerImage is responsible for the profile banner images.
@ -60,6 +64,7 @@ class ProfileBannerImage extends ProfileImage
* Sets a new profile image by given temp file
*
* @param \yii\web\UploadedFile $file
* @throws \yii\base\Exception
*/
public function setNew($file)
{
@ -68,4 +73,17 @@ class ProfileBannerImage extends ProfileImage
ImageConverter::Resize($this->getPath('_org'), $this->getPath('_org'), ['width' => 1134, 'mode' => 'max']);
ImageConverter::Resize($this->getPath('_org'), $this->getPath(''), ['width' => $this->width, 'height' => $this->height]);
}
/**
* @inheritDoc
*/
public function render($width, $cfg = [])
{
if(is_int($width)) {
$width .= 'px';
}
Html::addCssStyle($cfg,['width' => $width]);
return Html::img($this->getUrl(),$cfg);
}
}

View File

@ -8,7 +8,13 @@
namespace humhub\libs;
use humhub\modules\content\components\ContentContainerActiveRecord;
use humhub\modules\content\models\ContentContainer;
use humhub\modules\space\models\Space;
use humhub\modules\space\widgets\Image as SpaceImage;
use humhub\modules\user\widgets\Image as UserImage;
use Yii;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\FileHelper;
use yii\web\UploadedFile;
@ -35,6 +41,11 @@ class ProfileImage
*/
protected $guid = '';
/**
* @var ContentContainerActiveRecord
*/
protected $container;
/**
* @var Integer width of the Image
*/
@ -65,7 +76,12 @@ class ProfileImage
*/
public function __construct($guid, $defaultImage = 'default_user')
{
$this->guid = $guid;
if($guid instanceof ContentContainerActiveRecord) {
$this->container = $guid;
$this->guid = $this->container->guid;
} else {
$this->guid = $guid;
}
$this->defaultImage = $defaultImage;
}
@ -75,6 +91,8 @@ class ProfileImage
* @param String $prefix Prefix of the returned image
* @param boolean $scheme URL Scheme
* @return String Url of the profile image
* @throws \yii\base\InvalidConfigException
* @throws \yii\base\Exception
*/
public function getUrl($prefix = '', $scheme = false)
{
@ -95,6 +113,7 @@ class ProfileImage
* Indicates there is a custom profile image
*
* @return Boolean is there a profile image
* @throws \yii\base\Exception
*/
public function hasImage()
{
@ -106,6 +125,7 @@ class ProfileImage
*
* @param String $prefix for the profile image
* @return String Path to the profile image
* @throws \yii\base\Exception
*/
public function getPath($prefix = '')
{
@ -128,6 +148,7 @@ class ProfileImage
* @param Int $h
* @param Int $w
* @return boolean indicates the success
* @throws \yii\base\Exception
*/
public function cropOriginal($x, $y, $h, $w)
{
@ -148,6 +169,7 @@ class ProfileImage
* Sets a new profile image by given temp file
*
* @param mixed $file CUploadedFile or file path
* @throws \yii\base\Exception
*/
public function setNew($file)
{
@ -176,4 +198,43 @@ class ProfileImage
FileHelper::unlink($prefixPath);
}
}
/**
* @return ContentContainerActiveRecord|string
* @throws \yii\db\IntegrityException
* @since 1.4
*/
public function getContainer()
{
if(!$this->container) {
$this->container = ContentContainer::findRecord([$this->guid]);
}
return $this->container;
}
/**
* Renders this profile image
* @param int $width
* @param array $cfg
* @return string
* @throws \yii\db\IntegrityException
* @since 1.4
*/
public function render($width, $cfg = [])
{
$container = $this->getContainer();
if(!$container) {
return '';
}
$cfg['width'] = $width;
if($container instanceof Space) {
return SpaceImage::widget(['width' => $width, 'space' => $container, 'htmlOptions' => $cfg]);
}
return UserImage::widget(['width' => $width, 'user' => $container, 'imageOptions' => $cfg]);
}
}

View File

@ -14,6 +14,7 @@ use humhub\libs\ProfileBannerImage;
use humhub\libs\ProfileImage;
use humhub\modules\content\models\Content;
use humhub\modules\content\models\ContentContainer;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use Yii;
use yii\helpers\Url;
@ -85,10 +86,7 @@ abstract class ContentContainerActiveRecord extends ActiveRecord
*/
public function getProfileImage()
{
if ($this instanceof \humhub\modules\space\models\Space) {
return new ProfileImage($this->guid, 'default_space');
}
return new ProfileImage($this->guid);
return new ProfileImage($this);
}
/**
@ -98,7 +96,7 @@ abstract class ContentContainerActiveRecord extends ActiveRecord
*/
public function getProfileBannerImage()
{
return new ProfileBannerImage($this->guid);
return new ProfileBannerImage($this);
}
/**
@ -146,6 +144,10 @@ abstract class ContentContainerActiveRecord extends ActiveRecord
return "Default Wall Output for Class " . get_class($this);
}
/**
* @param $token
* @return ContentContainerActiveRecord|null
*/
public static function findByGuid($token)
{
return static::findOne(['guid' => $token]);

View File

@ -11,6 +11,7 @@ namespace humhub\modules\content\models;
use humhub\components\behaviors\PolymorphicRelation;
use humhub\modules\content\components\ContentContainerActiveRecord;
use yii\db\ActiveRecord;
/**
* This is the model class for table "contentcontainer".
@ -22,7 +23,7 @@ use humhub\modules\content\components\ContentContainerActiveRecord;
* @property integer $owner_user_id
* @mixin PolymorphicRelation
*/
class ContentContainer extends \yii\db\ActiveRecord
class ContentContainer extends ActiveRecord
{
/**
@ -75,4 +76,18 @@ class ContentContainer extends \yii\db\ActiveRecord
];
}
/**
* @param $guid
* @return ContentContainerActiveRecord
* @throws \yii\db\IntegrityException
* @since 1.4
*/
public static function findRecord($guid)
{
$instance = static::findOne(['guid' => $guid]);
if($instance) {
return $instance->getPolymorphicRelation();
}
}
}

View File

@ -22,10 +22,7 @@
* Note: Inline styles have been retained for legacy theme compatibility (prior to v1.4)
*/
use humhub\modules\space\models\Space;
use humhub\modules\space\widgets\Image as SpaceImage;
use humhub\modules\content\assets\ContainerHeaderAsset;
use humhub\modules\user\widgets\Image as UserImage;
use humhub\modules\file\widgets\Upload;
use yii\helpers\Html;
@ -36,10 +33,6 @@ $bannerProgressBarPadding = $container->getProfileBannerImage()->hasImage() ? '9
$bannerUpload = Upload::withName($coverUploadName, ['url' => $coverUploadUrl]);
$profileImageUpload = Upload::withName($imageUploadName, ['url' => $imageUploadUrl]);
$image = ($container instanceof Space)
? SpaceImage::widget(['space' => $container, 'width' => 140, 'htmlOptions' => ['class' => 'img-profile-header-background']])
: UserImage::widget(['user' => $container, 'width' => 140, 'imageOptions' => ['class' => 'img-profile-header-background']]);
?>
<?= Html::beginTag('div', $options) ?>
@ -48,10 +41,7 @@ $image = ($container instanceof Space)
<div class="image-upload-container profile-banner-image-container">
<!-- profile image output-->
<?= Html::img($container->getProfileBannerImage()->getUrl(), [
'class' => 'img-profile-header-background',
'style' => 'width:100%'
])?>
<?= $container->getProfileBannerImage()->render('width:100%', ['class' => 'img-profile-header-background']) ?>
<!-- show user name and title -->
<div class="img-profile-data">
@ -81,10 +71,10 @@ $image = ($container instanceof Space)
<?php if ($container->getProfileImage()->hasImage()) : ?>
<a data-ui-gallery="spaceHeader" href="<?= $container->profileImage->getUrl('_org'); ?>">
<?= $image ?>
<?= $container->getProfileImage()->render( 140, ['class' => 'img-profile-header-background']); ?>
</a>
<?php else : ?>
<?= $image ?>
<?= $container->getProfileImage()->render(140, ['class' => 'img-profile-header-background']); ?>
<?php endif; ?>
<?php if ($canEdit) : ?>

View File

@ -8,6 +8,7 @@
namespace humhub\modules\space\models;
use humhub\libs\ProfileImage;
use humhub\modules\search\interfaces\Searchable;
use humhub\modules\search\events\SearchAddEvent;
use humhub\modules\search\jobs\DeleteDocument;
@ -464,6 +465,14 @@ class Space extends ContentContainerActiveRecord implements Searchable
return $this->description;
}
/**
* @inheritDoc
*/
public function getProfileImage()
{
return new ProfileImage($this, 'default_space');
}
/**
* @inheritdoc
*/

View File

@ -86,8 +86,10 @@ class User extends \yii\web\User
*
* @param string|string[]|BasePermission $permission
* @return boolean
* @see PermissionManager::can()
* @throws \yii\base\InvalidConfigException
* @throws \Throwable
* @since 1.2
* @see PermissionManager::can()
*/
public function can($permission, $params = [], $allowCaching = true)
{
@ -96,6 +98,7 @@ class User extends \yii\web\User
/**
* @return PermissionManager instance with the related identity instance as permission subject.
* @throws \Throwable
*/
public function getPermissionManager()
{
@ -125,6 +128,7 @@ class User extends \yii\web\User
/**
* Determines if this user is able to change the email address.
* @return boolean
* @throws \Throwable
*/
public function canChangeEmail()
{
@ -138,6 +142,7 @@ class User extends \yii\web\User
/**
* Determines if this user is able to change his username.
* @return boolean
* @throws \Throwable
*/
public function canChangeUsername()
{