1. Go to this page and download the library: Download sunlab/wn-measures-plugin library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
class Link extends Model
{
public $implement = ['SunLab.Measures.Behaviors.Measurable'];
//...
}
$post->incrementMeasure('views');
// Optional amount of incrementation can be passed
$post->incrementMeasure('views', 5);
// In Plugin.php file
function boot()
{
// Add the Measurable behavior to User model
Winter\User\Models\User::extend(function($user) {
// Verify it has not been already added by another plugin
if (!$user->isClassExtendedWith('SunLab.Measures.Behaviors.Measurable')) {
$user->extendClassWith('SunLab.Measures.Behaviors.Measurable');
}
});
Winter\Forum\Models\Post::extend(function($post) {
// Bind listener to update event
$post->bindEvent('model.afterUpdate', function() use ($model) {
// Increment measure on the member
$post->member->incrementMeasure('post_edit');
});
});
}
// Passing to the MeasureManager a Builder instance
$products = Product::where('name', 'like', '%shoes%');
MeasureManager::incrementMeasure($products, 'appearedInSearchResults');
return new JsonResponse([
'products' => $products->get()
]);
// Count how many users log-in
Event::listen('winter.user.login', function() {
MeasureManager::incrementOrphanMeasure('users_login');
// incrementMeasure also support orphan measure.
// Same as:
MeasureManager::incrementMeasure('users_login');
});
// On model implementing Measurable
$model->decrementMeasure('post_edit');
$model->decrementMeasure('post_edit', 3); // An amount of decrementation can be passed
// With orphan measures
MeasureManager::decrementOrphanMeasure('users_login');
MeasureManager::decrementOrphanMeasure('users_login', 3); // An amount of decrementation can be passed
// From a backend controller/view
$post = Post::first();
$views = $post->getMeasure('views')->amount;
// Same as:
$views = $post->getAmountOf('views');
// Plugin.php
Event::listen('sunlab.measures.incrementMeasure', function ($model, $measure) {
// Filter the model we need
if (!$model instanceof User) {
return;
}
// Process some custom logic depending on the measure name and current amount
// In that case, SunLab/Gamification assign "badges" depending on some measures incrementation
$correspondingBadges =
Badge::where([['measure_name', $measure->name], ['amount_needed', '<=', $measure->amount]])
->whereDoesntHave('users', function ($query) use ($model) {
$query->where('user_id', $model->id);
})->get();
if (!blank($correspondingBadges)) {
$now = now();
$attachedBadges = array_combine(
$correspondingBadges->pluck('id')->toArray(),
array_fill(0, count($correspondingBadges), ['updated_at' => $now, 'created_at' => $now])
);
$model->badges()->attach($attachedBadges);
}
});
// Using the MeasureManager static helpers:
use SunLab\Measures\Classes\MeasureManager;
$post = \Winter\Blog\Models\Post::first();
// Increment a model's measure
MeasureManager::incrementMeasure($post, 'views');
MeasureManager::incrementMeasure($post, 'views', 5);
// Decrement:
MeasureManager::decrementMeasure($model, 'views');
MeasureManager::decrementMeasure($model, 'views', 3);
// Reset
MeasureManager::resetMeasure('views');
MeasureManager::resetMeasure('views', 2);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.