Git Product home page Git Product logo

cakephp3-draft's Introduction

Draft plugin for CakePHP 3

This Draft plugin give you the ability to quickly create draft system for your models.

This plugin is the Grafikart's Cakephp-Draft plugin for CakePHP 3.*

Build Status Coverage Status Latest Stable Version License

Installation

Requirements

Steps to install

  • Run :
composer require romano83/cakephp3-draft:1.*

How to use

In your config\bootstrap.php file, add this line

Plugin::load('Romano83/cakephp3-draft');

Or

Plugin::loadAll();

The plugin is now loaded and you can add the DraftBehavior in your tables:

<?php
namespace MyApp\Model\Table;

use Cake\ORM\Table;

class PostsTable extends Table
{
	public function initialize(array $config)
	{
		$this->addBehavior('Romano83/Cakephp3Draft.Draft');
	}
}

By default, the plugin use an "online" field to set the state of a content.

  • online = -1 when the content is a draft
  • online = 0 when the content is offline
  • online = 1 when content is online

Customization

If you want to use custom fields, you can override default behavior configuration :

<?php
namespace MyApp\Model\Table;

use Cake\ORM\Table;

class PostsTable extends Table
{
	public function initialize(array $config)
	{
		$this->addBehavior(
			'Romano83/Cakephp3Draft.Draft', [
				'conditions' => [
					'draft' => 1
				]
			]
		);
	}
}

For instance, this configuration will use a "draft" field (set to 1), to create Draft.

Methods

With the plugin attached, the model will have new method, getDraftId(Table $table, array $conditions = []) witch return draft ID. The first parameter is a \Cake\ORM\Table instance and the second one is an optional array.

How to implement this method

Here, is an example of how to use this method in your PostsController:

public function add(){
	$post = $this->Posts->newEntity();
	if($this->request->is(['post', 'put'])){
		// Do your stuff here...
	}else{
		$post->id = $this->Posts->getDraftId($this->Posts); // get the last draft Id or create new one if needed
	}
}

// OR
public function add(){
	$post = $this->Posts->newEntity();
	if($this->request->is(['post', 'put'])){
		// Do your stuff here...
	}else{
		$post->id = $this->Posts->getDraftId($this->Posts, ['user_id' => 2]); // Get a draft Id for a content belonging to user 2 (or create a new one)
	}
}

If you want to clean your table from drafts, you can implement this method:

$this->Posts->cleanDrafts($this->Posts);

How to contribute

  • Create a ticket in GitHub, if you have found a bug.
  • Create a new branch if you want do a PR.
  • Your code must follow the Coding Standard of CakePHP.
  • You must add testcases if needed.

Special thanks

  • Grafikart for the first version of this plugin !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.