thyseus/yii2-favorites

General favorite management for Yii2

Installs: 1 186

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Forks: 0

Type:yii2-extension

v0.1.0 2017-02-15 09:54 UTC

This package is auto-updated.

Last update: 2024-10-29 01:57:10 UTC


README

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.