Article controller

The ArticleController class is responsible for the actions of article.

module/admin/controller/ArticleController.js

const Base = require('../component/CrudController');

module.exports = class ArticleController extends Base {

  async renderForm (template, params) {
    await this.render(template, {
      categories: await this.spawn(Category).findNames().all(),
      ...params
    });
  }
};
module.exports.init(module);

const Category = require('../model/Category');
const Article = require('../model/Article');
Article list

The actionIndex method lists articles to search and sort by ID, status or title. Each article is loaded together with related models (author, mainPhoto) are required to display it in the list.

module/admin/controllers/ArticleController.js

async actionIndex () {
  const searchText = this.getQueryParam('search');
  const query = this.spawn(Article).findBySearch(this.getQueryParam('search')).with('author', 'mainPhoto');
  const provider = this.createDataProvider({
    query: this.spawn(Article).findBySearch(searchText).with('author', 'mainPhoto'),
    pagination: {pageSize: 10},
    sort: {
      attrs: {
        [Article.PK]: true,
        status: true,
        title: true
      },
      defaultOrder: {[Article.PK]: -1}
    }
  });
  await this.renderDataProvider(provider, 'index', {provider, searchText});
}
View article Article view

The actionView method shows an article. The author, photos, mainPhoto, tags related models are loaded along with an article. Paging a list of comments related to an article formed from the relComments relation by using the ActiveDataProvider data provider.

module/admin/controller/ArticleController.js

async actionView () {
  const model = await this.getModel({
    with: ['author', 'category', 'photos', 'mainPhoto', 'tags']
  });
  const comments = this.createDataProvider({
    query: model.relComments(),
    sort: {
      attrs: {
        [model.PK]: true
      },
      defaultOrder: {[model.PK]: -1}
    }
  });
  await this.renderDataProvider(comments, 'view', {model, comments});
}
Create article

The actionCreate method creates a new article. The authorId attribute containing article author, is set by the current user.

module/admin/controller/ArticleController.js

actionCreate () {
  const model = this.spawn(Article);
  if (this.isGet()) {
    return this.renderForm('create', {model});
  }
  model.load(this.getPostParams());
  model.set('authorId', this.user.getId());
  await model.save()
    ? this.redirectToReferrer()
    : await this.renderForm('create', {model});
}
Article update

The actionUpdate updates an article. Found article is checked for access for the current user. Parameters ({ authorId: model.get('authorId') }) used for initialization of business rules of access, are passed to the can asynchronous function by the last argument.

module/admin/controller/ArticleController.js

actionUpdate () {
  const model = await this.getModel({with: ['photos', 'tags']});
  const access = await this.user.can('updateArticle', {authorId: model.get('authorId')});
  if (!access) {
    throw new Forbidden;
  }
  return this.isPost() && await model.load(this.getPostParams()).save()
    ? this.redirectToReferrer()
    : this.renderForm('update', {model});
}