Enh: Added ContentActiveRecord::getIcon() for adding an badge icon for WallEntry content type badge.

Enh: Added `ContentActiveRecord::getLabels()` for managing WallEntry labels (badges)
Enh: Added `Label` widget for creating sortable labes/badges
This commit is contained in:
buddh4 2017-07-24 23:44:06 +02:00
parent 5cba95566a
commit 2fa6428c8b
8 changed files with 525 additions and 319 deletions

View File

@ -9,6 +9,7 @@
namespace humhub\modules\content\components;
use humhub\modules\content\widgets\WallEntry;
use humhub\widgets\Label;
use Yii;
use humhub\libs\BasePermission;
use humhub\modules\content\permissions\ManageContent;
@ -156,6 +157,52 @@ class ContentActiveRecord extends ActiveRecord implements ContentOwner
return $this->className();
}
/**
* Can be used to define an icon for this content type.
* @return string
*/
public function getIcon()
{
return null;
}
/**
* Returns either Label widget instances or strings.
*
* Subclasses should call `paren::getLabels()` as follows:
*
* ```php
* public function getLabels($labels = [], $includeContentName = true)
* {
* return parent::getLabels([Label::info('someText')->sortOrder(5)]);
* }
* ```
*
* @param array $result
* @param bool $includeContentName
* @return Label[]|\string[] content labels used for example in wallentrywidget
*/
public function getLabels($labels = [], $includeContentName = true)
{
if ($this->content->isPinned()) {
$labels[] = Label::danger(Yii::t('ContentModule.widgets_views_label', 'Pinned'))->sortOrder(100);
}
if($this->content->isArchived()) {
$labels[] = Label::warning(Yii::t('ContentModule.widgets_views_label', 'Archived'))->sortOrder(200);
}
if ($this->content->isPublic()) {
$labels[] = Label::info(Yii::t('ContentModule.widgets_views_label', 'Public'))->sortOrder(300);
}
if ($includeContentName) {
$labels[] = Label::defaultType($this->getContentName())->icon($this->getIcon())->sortOrder(400);
}
return Label::sort($labels);
}
/**
* Returns a description of this particular content.
*

View File

@ -1,31 +0,0 @@
<?php
namespace humhub\modules\content\widgets;
/**
* Labels for Wall Entries
* This widget will attached labels like Pinned, Archived to Wall Entries
*
* @package humhub.modules_core.wall.widgets
* @since 0.5
*/
class WallEntryLabelWidget extends HWidget {
/**
* Content Object with SIContentBehaviour
* @var type
*/
public $object;
/**
* Executes the widget.
*/
public function run() {
$this->render('label', array(
'object' => $this->object,
));
}
}
?>

View File

@ -16,9 +16,9 @@ class WallEntryLabels extends \yii\base\Widget
*/
public function run()
{
return $this->render('labels', array(
return $this->render('labels', [
'object' => $this->object,
));
]);
}
}

View File

@ -1,30 +1,19 @@
<?php
use humhub\modules\post\models\Post;
use humhub\widgets\Label;
/**
* This view shows common labels for wall entries.
* Its used by WallEntryLabelWidget.
*
* @property Mixed $object the content object (e.g. Post)
* @property \humhub\modules\content\components\ContentActiveRecord $object the content object (e.g. Post)
*
* @since 0.5
*/
?>
<span class="wallentry-labels">
<?php if ($object->content->isPinned()) : ?>
<span class="label label-danger"><?= Yii::t('ContentModule.widgets_views_label', 'Pinned'); ?></span>
<?php endif; ?>
<?php if ($object->content->isArchived()) : ?>
<span class="label label-warning"><?= Yii::t('ContentModule.widgets_views_label', 'Archived'); ?></span>
<?php endif; ?>
<?php if ($object->content->isPublic()) : ?>
<span class="label label-info"><?= Yii::t('ContentModule.widgets_views_label', 'Public'); ?></span>
<?php endif; ?>
<?php if (!$object instanceof Post) : ?>
<span class="label label-default"><?= $object->getContentName(); ?></span>
<?php endif; ?>
<?php foreach ($object->getLabels() as $label) : ?>
<?= $label ?>
<?php endforeach; ?>
<span>

View File

@ -92,6 +92,11 @@ class Post extends ContentActiveRecord implements Searchable
return Yii::t('PostModule.models_Post', 'post');
}
public function getLabels($result = [], $includeContentName = true)
{
return parent::getLabels($result, false);
}
/**
* @inheritdoc
*/

View File

@ -0,0 +1,333 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*
*/
/**
* Created by PhpStorm.
* User: buddha
* Date: 21.07.2017
* Time: 21:31
*/
namespace humhub\widgets;
use humhub\components\Widget;
use yii\helpers\ArrayHelper;
use humhub\libs\Html;
abstract class BootstrapComponent extends Widget
{
const TYPE_PRIMARY = 'primary';
const TYPE_DEFAULT = 'default';
const TYPE_INFO = 'info';
const TYPE_WARNING = 'warning';
const TYPE_DANGER = 'danger';
const TYPE_SUCCESS = 'success';
const TYPE_NONE = 'none';
public $type;
public $htmlOptions = [];
public $text;
public $_icon;
public $_visible = true;
/**
* @param string $text Button text
* @return static
*/
public static function instance($text = null)
{
return new static(['type' => self::TYPE_NONE, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function none($text = null)
{
return new static(['type' => self::TYPE_NONE, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function primary($text = null)
{
return new static(['type' => self::TYPE_PRIMARY, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function defaultType($text = null)
{
return new static(['type' => self::TYPE_DEFAULT, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function info($text = null)
{
return new static(['type' => self::TYPE_INFO, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function warning($text = null)
{
return new static(['type' => self::TYPE_WARNING, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function success($text = null)
{
return new static(['type' => self::TYPE_SUCCESS, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function danger($text = null)
{
return new static(['type' => self::TYPE_DANGER, 'text' => $text]);
}
/**
* @param $color
* @param null $text
* @return $this
*/
public static function asColor($color, $text = null)
{
return static::info($text)->color($color);
}
/**
* @param $text
* @return $this
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* @param bool $right
* @return $this
*/
public function right($right = true)
{
if($right) {
Html::removeCssClass($this->htmlOptions,'pull-left');
Html::addCssClass($this->htmlOptions, 'pull-right');
} else {
Html::removeCssClass($this->htmlOptions,'pull-right');
}
return $this;
}
/**
* @param bool $left
* @return $this
*/
public function left($left = true)
{
if($left) {
Html::removeCssClass($this->htmlOptions,'pull-right');
Html::addCssClass($this->htmlOptions, 'pull-left');
} else {
Html::removeCssClass($this->htmlOptions,'pull-left');
}
return $this;
}
/**
* @return $this
*/
public function sm()
{
Html::addCssClass($this->htmlOptions, 'btn-sm');
return $this;
}
/**
* @return $this
*/
public function lg()
{
Html::addCssClass($this->htmlOptions, 'btn-lg');
return $this;
}
/**
* @return $this
*/
public function xs()
{
Html::addCssClass($this->htmlOptions, 'btn-xs');
return $this;
}
/**
* @param $style
* @return $this
*/
public function style($style)
{
Html::addCssStyle($this->htmlOptions, $style);
return $this;
}
/**
* @param $id
* @return $this
*/
public function id($id)
{
$this->id = $id;
return $this;
}
/**
* @param $cssClass
* @return $this
*/
public function cssClass($cssClass)
{
Html::addCssClass($this->htmlOptions, $cssClass);
return $this;
}
/**
* @param $options
* @return $this
*/
public function options($options)
{
if(isset($options['class'])) {
$this->cssClass($options['class']);
unset($options['class']);
}
if(isset($options['style'])) {
$this->style($options['style']);
unset($options['style']);
}
$this->htmlOptions = ArrayHelper::merge($this->htmlOptions, $options);
return $this;
}
public function icon($content, $raw = false)
{
if(!$raw) {
$this->icon(Html::tag('i', '', ['class' => 'fa '.$content]), true);
} else {
$this->_icon = $content;
}
return $this;
}
/**
* @inheritdoc
*/
public function run()
{
$this->setCssClass();
$this->htmlOptions['id'] = $this->getId(true);
return $this->renderComponent();
}
public function color($color)
{
$this->style('background-color:'.$color);
return $this;
}
public function textColor($color)
{
$this->style('color:'.$color);
return $this;
}
/**
* @return string renders and returns the actual html element by means of the current settings
*/
public abstract function renderComponent();
/**
* @return string the bootstrap css base class
*/
public abstract function getComponentBaseClass();
/**
* @return string the bootstrap css class by $type
*/
public abstract function getTypedClass($type);
protected function setCssClass()
{
if($this->type !== self::TYPE_NONE) {
Html::addCssClass($this->htmlOptions, $this->getComponentBaseClass());
Html::addCssClass($this->htmlOptions, $this->getTypedClass($this->type));
}
}
protected function getText()
{
if($this->_icon) {
return $this->_icon.' '.$this->text;
}
return $this->text;
}
public function visible($isVisible = true) {
$this->_visible = $isVisible;
return $this;
}
public function __toString()
{
$result = $this::widget($this->getWidgetOptions());
return $result ? $result : '';
}
/**
* @return array all options required for rendering the widget
*/
public function getWidgetOptions()
{
return [
'id' => $this->id,
'type' => $this->type,
'text' => $this->text,
'htmlOptions' => $this->htmlOptions,
'_icon' => $this->_icon,
'render' => $this->_visible
];
}
}

View File

@ -31,34 +31,33 @@ use yii\helpers\Url;
*
* @package humhub\widgets
*/
class Button extends Widget
class Button extends BootstrapComponent
{
const TYPE_PRIMARY = 'primary';
const TYPE_DEFAULT = 'default';
const TYPE_INFO = 'info';
const TYPE_WARNING = 'warning';
const TYPE_DANGER = 'danger';
const TYPE_SUCCESS = 'success';
const TYPE_NONE = 'none';
public $type;
public $htmlOptions = [];
public $text;
public $_icon;
public $_loader = true;
public $_link = false;
public $_visible = true;
/**
* @param string $text Button text
* @return static
*/
public static function instance($text = null)
public static function save($text = null)
{
return new static(['type' => self::TYPE_NONE, 'text' => $text]);
if(!$text) {
$text = Yii::t('base', 'Save');
}
return self::primary($text);
}
/**
* @param string $text Button text
* @param string $href
* @return static
*/
public static function asLink($text = null, $href = '#')
{
return self::none($text)->link($href);
}
/**
@ -82,92 +81,6 @@ class Button extends Widget
return self::asLink($text)->action('selectSelf', null, $selector)->icon('fa-check-circle-o')->right()->cssClass('input-field-addon');
}
/**
* @param string $text Button text
* @return static
*/
public static function none($text = null)
{
return new static(['type' => self::TYPE_NONE, 'text' => $text]);
}
/**
* @param string $text Button text
* @param string $href
* @return static
*/
public static function asLink($text = null, $href = '#')
{
return self::none($text)->link($href);
}
/**
* @param string $text Button text
* @return static
*/
public static function primary($text = null)
{
return new static(['type' => self::TYPE_PRIMARY, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function save($text = null)
{
if(!$text) {
$text = Yii::t('base', 'Save');
}
return self::primary($text);
}
/**
* @param string $text Button text
* @return static
*/
public static function defaultType($text = null)
{
return new static(['type' => self::TYPE_DEFAULT, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function info($text = null)
{
return new static(['type' => self::TYPE_INFO, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function warning($text = null)
{
return new static(['type' => self::TYPE_WARNING, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function success($text = null)
{
return new static(['type' => self::TYPE_SUCCESS, 'text' => $text]);
}
/**
* @param string $text Button text
* @return static
*/
public static function danger($text = null)
{
return new static(['type' => self::TYPE_DANGER, 'text' => $text]);
}
/**
* @param bool $active
* @return $this
@ -193,6 +106,11 @@ class Button extends Widget
return $this;
}
/**
* If set to false the [data-pjax-prevent] flag is attached to the link.
* @param bool $pjax
* @return $this
*/
public function pjax($pjax = true)
{
if(!$pjax) {
@ -201,75 +119,6 @@ class Button extends Widget
return $this;
}
/**
* @param $text
* @return $this
*/
public function setText($text)
{
$this->text = $text;
return $this;
}
/**
* @param bool $right
* @return $this
*/
public function right($right = true)
{
if($right) {
Html::removeCssClass($this->htmlOptions,'pull-left');
Html::addCssClass($this->htmlOptions, 'pull-right');
} else {
Html::removeCssClass($this->htmlOptions,'pull-right');
}
return $this;
}
/**
* @param bool $left
* @return $this
*/
public function left($left = true)
{
if($left) {
Html::removeCssClass($this->htmlOptions,'pull-right');
Html::addCssClass($this->htmlOptions, 'pull-left');
} else {
Html::removeCssClass($this->htmlOptions,'pull-left');
}
return $this;
}
/**
* @return $this
*/
public function sm()
{
Html::addCssClass($this->htmlOptions, 'btn-sm');
return $this;
}
/**
* @return $this
*/
public function lg()
{
Html::addCssClass($this->htmlOptions, 'btn-lg');
return $this;
}
/**
* @return $this
*/
public function xs()
{
Html::addCssClass($this->htmlOptions, 'btn-xs');
return $this;
}
/**
* @return $this
*/
@ -280,56 +129,7 @@ class Button extends Widget
}
/**
* @param $style
* @return $this
*/
public function style($style)
{
Html::addCssStyle($this->htmlOptions, $style);
return $this;
}
/**
* @param $id
* @return $this
*/
public function id($id)
{
$this->id = $id;
return $this;
}
/**
* @param $cssClass
* @return $this
*/
public function cssClass($cssClass)
{
Html::addCssClass($this->htmlOptions, $cssClass);
return $this;
}
/**
* @param $options
* @return $this
*/
public function options($options)
{
if(isset($options['class'])) {
$this->cssClass($options['class']);
unset($options['class']);
}
if(isset($options['style'])) {
$this->style($options['style']);
unset($options['style']);
}
$this->htmlOptions = ArrayHelper::merge($this->htmlOptions, $options);
return $this;
}
/**
* Adds a data-action-click handler to the button.
* @param $handler
* @param null $url
* @param null $target
@ -340,6 +140,15 @@ class Button extends Widget
return $this->onAction('click', $handler, $url, $target);
}
/**
* Adds a data-action-* handler to the button.
*
* @param $event
* @param $handler
* @param null $url
* @param null $target
* @return $this
*/
public function onAction($event, $handler, $url = null, $target = null)
{
$this->htmlOptions['data-action-'.$event] = $handler;
@ -355,6 +164,15 @@ class Button extends Widget
return $this;
}
/**
* Adds a confirmation behaviour to the button.
*
* @param null $title
* @param null $body
* @param null $confirmButtonText
* @param null $cancelButtonText
* @return $this
*/
public function confirm($title = null, $body = null, $confirmButtonText = null, $cancelButtonText = null)
{
if($title) {
@ -378,30 +196,15 @@ class Button extends Widget
return $this;
}
public function icon($content, $raw = false)
{
if(!$raw) {
$this->icon(Html::tag('i', '', ['class' => 'fa '.$content]), true);
} else {
$this->_icon = $content;
}
return $this;
}
/**
* @inheritdoc
* @return string renders and returns the actual html element by means of the current settings
*/
public function run()
public function renderComponent()
{
$this->setCssClass();
if($this->_loader) {
$this->htmlOptions['data-ui-loader'] = '';
}
$this->htmlOptions['id'] = $this->getId(true);
if($this->_link) {
$href = isset($this->htmlOptions['href']) ? $this->htmlOptions['href'] : null;
return Html::a($this->getText(), $href, $this->htmlOptions);
@ -410,42 +213,27 @@ class Button extends Widget
}
}
protected function setCssClass()
public function getWidgetOptions()
{
if($this->type !== self::TYPE_NONE) {
Html::addCssClass($this->htmlOptions, 'btn');
Html::addCssClass($this->htmlOptions, 'btn-'.$this->type);
}
$options = parent::getWidgetOptions();
$options['_link'] = $this->_link;
$options['_loader'] = $this->_loader;
return $options;
}
protected function getText()
/**
* @inheritdoc
*/
public function getComponentBaseClass()
{
if($this->_icon) {
return $this->_icon.' '.$this->text;
return 'btn';
}
return $this->text;
}
public function visible($isVisible = true) {
$this->_visible = $isVisible;
return $this;
}
public function __toString()
/**
* @inheritdoc
*/
public function getTypedClass($type)
{
$result = $this::widget([
'id' => $this->id,
'type' => $this->type,
'text' => $this->text,
'htmlOptions' => $this->htmlOptions,
'_icon' => $this->_icon,
'_link' => $this->_link,
'_loader' => $this->_loader,
'render' => $this->_visible
]);
return $result ? $result : '';
return 'btn-'.$type;
}
}

View File

@ -0,0 +1,75 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) 2017 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*
*/
namespace humhub\widgets;
use humhub\components\Widget;
use humhub\libs\Html;
use Yii;
/**
* Labels for Wall Entries
* This widget will attached labels like Pinned, Archived to Wall Entries
*
* @package humhub.modules_core.wall.widgets
* @since 0.5
*/
class Label extends BootstrapComponent
{
public $_sortOrder = 1000;
public function sortOrder($sortOrder)
{
$this->_sortOrder = $sortOrder;
return $this;
}
/**
* @return string renders and returns the actual html element by means of the current settings
*/
public function renderComponent()
{
return Html::tag('span', $this->getText(), $this->htmlOptions);
}
/**
* @return string the bootstrap css base class
*/
public function getComponentBaseClass()
{
return 'label';
}
/**
* @return string the bootstrap css class by $type
*/
public function getTypedClass($type)
{
return 'label-'.$type;
}
public static function sort(&$labels)
{
usort($labels, function ($a, $b) {
if ($a->_sortOrder == $b->_sortOrder) {
return 0;
} else
if ($a->_sortOrder < $b->_sortOrder) {
return - 1;
} else {
return 1;
}
});
return $labels;
}
}
?>