Skip to content

General Favorites Manager for the Yii 2 framework.

Notifications You must be signed in to change notification settings

thyseus/yii2-favorites

Repository files navigation

ATTENTION:

github has been bought by Microsoft. This repository is orphaned and has been moved to:

https://gitlab.com/thyseus/yii2-favorites

Thanks a lot for your understanding and blame Microsoft.

Yii2-favorites

A General Favorites Manager for the Yii 2 framework. Every ActiveRecord Model can be bookmarked and accessed later by an authorized user. Contains only one table 'favorites' where everything is stored.

Installation

$ composer require thyseus/yii2-favorites
$ php yii migrate/up --migrationPath=@vendor/thyseus/yii2-favorites/migrations

Configuration

Add following lines to your main configuration file:

'modules' => [
    'favorites' => [
        'class' => 'thyseus\favorites\Module',
    ],
],

Integration in your application:

use

use thyseus\favorites\models\Favorite;

$model = CurrentModel::findOne(57);
echo $this->render('@vendor/thyseus/yii2-favorites/views/favorites/_button', [
    'model' => CurrentModel::className(),
    'target' => $model->slug
]);

to display a "Set as Favorite" / "Remove Favorite" toggle Button in the view files of the models you want to make your users to be able to add favorites to.

Note that you can shorten the call if you set an alias like this in your application configuration:

'aliases' => [
    '@favorites' => '@app/vendor/thyseus/yii2-favorites'
],

And call the view like:

echo $this->render('@favorites/views/favorites/_button', [

If the model is not identified by the column 'id' by default, for example if you are using slugs, you can define the indentifierAttribute inside the model like this:

public function identifierAttribute()
{
    return 'slug';
}

If the automatic URL creation if yii2-favorites fails, you can append the URL manually:

use thyseus\favorites\models\Favorite;

$model = CurrentModel::findOne(57);
echo $this->render('@vendor/thyseus/yii2-favorites/views/favorites/_button', [
    'model' => CurrentModel::className(),
    'target' => $model->id,
    'icon' => '<i class="fa fa-users" aria-hidden="true"></i>', // optional
    'url' => Url::to(['fancy-url', 'id' => 1337]) ,
]);

Use the fourth parameter to set an custom target_attribute:

use thyseus\favorites\models\Favorite;

$model = CurrentModel::findOne(57);

echo $this->render('@vendor/thyseus/yii2-favorites/views/favorites/_button', [
    'model' => CurrentModel::className(),
    'target' => $model->id,
    'target_attribute' => 'i-am-referenced-by-this-column',
]);

If you want to use composite label identifiers, you can do it like this:

public function getName()
{
    return $this->firstname . ' ' . $this->lastname;
}

You can use this code example to make an dynamic menu containing your favorites in the NavBar:

echo Nav::widget([
    'options' => ['class' => 'navbar-nav navbar-right'],
    'encodeLabels' => false,
    'items' => [
        ['label' => '<span class="glyphicon glyphicon-bookmark"></span>', 'options' => ['class' => 'favorites-menu clickable', 'style' => 'cursor: pointer;'], 'url' => false, 'visible' => !$user->isGuest, 'items' => ['' => '']],
      ]
    ]);
$this->registerJs("
    $('.favorites-menu').click(function() {
        $.getJSON('".Url::to(['//favorites/favorites/json'])."', function (data) {
            dd = $('.favorites-menu').find('.dropdown-menu');
            dd.html('');
            dd.append('<li><a href=\"".Url::to(['//favorites/favorites/index'])."\">Manage favorites</a></li>');
            data.forEach(function(elem) {
              dd.append('<li>' + (elem.icon ? elem.icon : '') + ' <a href=\"' + elem.url + '\">' + elem.title.substring(0, 60) + '</a></li>');
             });
        });
    });
");
.favorites-menu li a { display: inline; padding: 0; }
.favorites-menu .dropdown-menu { padding: 10px; }

Routes

You can use the following routes to access the favorites module:

License

Yii2-favorites is released under the GPLv3 License.

About

General Favorites Manager for the Yii 2 framework.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages