PHP code example of indifferend / yii2-moderation

1. Go to this page and download the library: Download indifferend/yii2-moderation 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/ */

    

indifferend / yii2-moderation example snippets




use yii\db\Migration;

/**
 * Handles adding moderation columns to table `post`.
 */
class m161117_092603_add_moderation_columns_to_post_table extends Migration
{
    /**
     * @inheritdoc
     */
    public function up()
    {
        $this->addColumn('post', 'status', $this->smallInteger());
        $this->addColumn('post', 'moderated_by', $this->integer());
    }

    /**
     * @inheritdoc
     */
    public function down()
    {
        $this->dropColumn('post', 'status');
        $this->dropColumn('post', 'moderated_by');
    }
}


use indifferend\moderation\ModerationBehavior;

class Post extends ActiveRecord 
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            ModerationBehavior::class,
        ];
    }
}

use indifferend\moderation\ModerationBehavior;

class Post extends ActiveRecord 
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
      return [
          [
              'class' => ModerationBehavior::class,
              'statusAttribute' => 'status_id',
              'moderatedByAttribute' => 'moderator_id', // or set to `false` to disable this attribute.
          ],
      ];
    }
}

$post->markApproved(); // Change post status to Approved

$post->markRejected(); // Change post status to Rejected

$post->markPostponed(); // Change post status to Postponed

$post->markPending(); // Change post status to Pending

$post->isPending(); // Check if a post is pending

$post->isApproved(); // Check if a post is approved

$post->isRejected(); // Check if a post is rejected

$post->isPostponed(); // Check if a post is postponed

$post = Post::findOne($id);
$post->on(ModerationBehavior::EVENT_BEFORE_MODERATION, function ($event) {
    $event->isValid = false; // prevent moderation for the model
});

use indifferend\moderation\ModerationBehavior;

class Post extends ActiveRecord 
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            ModerationBehavior::class,
        ];
    }
    
    public function beforeModeration()
    {
        $this->moderated_at = time(); // log the moderation date

        return true;
    }
}

use indifferend\moderation\ModerationQuery;

class Post extends ActiveRecord 
{
    public static function find()
    {
        return new ModerationQuery(get_called_class());
    }
}

Post::find()->approved()->all(); // It will return all Approved Posts

Post::find()->pending()->all(); // It will return all Pending Posts

Post::find()->rejected()->all(); // It will return all Rejected Posts

Post::find()->postponed()->all(); // It will return all Postponed Posts

Post::find()->approvedWithPending()->all() // It will return all Approved and Pending Posts

php composer.phar