3.0 DOCUMENTATION
NOTE: This version is in active development.

CONTENT TYPE BUILDER COMPONENT



HOW IT WORKS:

EVENTS ON BACKEND:

When dealing with backend, in every action like creating content, creating a new content type, updating, deleting etc has its own events triggered where you can hook in to process any other relevant data to your application. So if you have some work to be done before a content is created or after a content is created, events will help you.


EVENT: PARAMETERS PASSED: DESCRIPTION:
{type}.creating
(replace {type} for any content type you created. If it is blog, then it will be "blog.creating")
Param 1: Array of data of the new content to be created This event is triggered before the content is being created.
{type}.created
(replace {type} for any content type you created. If it is blog, then it will be "blog.creating")
Param 1: Eloquent object of the new created content. This event is triggered after the content is successfully created.
{type}.deleting
(replace {type} for any content type you created. If it is blog, then it will be "blog.deleting")
Param 1: Eloquent object of the content to be deleted This event is triggered before the content is being deleted.
{type}.deleted
(replace {type} for any content type you created. If it is blog, then it will be "blog.deleted")
Param 1: Eloquent object of the content to be deleted This event is triggered after the content is successfully deleted.
{type}.updating
(replace {type} for any content type you created. If it is blog, then it will be "blog.updating")
Param 1: Eloquent object of the content to be updated.
Param 2: Array of data for update.
This event is triggered before the content is updated.
{type}.updated
(replace {type} for any content type you created. If it is blog, then it will be "blog.updated")
Param 1: Eloquent object of the content that has been updated. This event is triggered after the content is successfully updated.

API:

The best way to query contents is to use the commands. You can use the raw Eloquent query but its more convenient to use the built-in commands for ease of query.

NOTE: When using the commands publicly and you get permission errors, put this on your parameters to bypass the permission checking.

'disablePermissionChecking' => true

Query Contents

On your controller, you can do this:


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\ContentBuilder\Commands\QueryContentsCommand',
    array(
        'type' => 'blog', // the content type. This can be string or int(id)
        'terms' => array(
            'Size'  => array('small'),
            'Color' => array('blue','green'),
            'Availability' => array('yes'),
        ), // the taxonomy terms
        'paginated' => true, // whether you want paginated results or not, default=true
        'perPage' => 8, // pagination per page, default=8
        'sortBy' => 'created_at',
        'sortOrder' => 'DESC',
        'status' => 'any', // status of the content. This can be (published|draft|any) default=any
        'authorId' => 1, // author of the content. Int
        'disablePermissionChecking' => true, // this is mandatory when using built-in commands so it will ignore permission checking!
        'startDate' => '2015-10-10', // query date range
        'endDate' => '2015-10-12', // query date range
        'with' => array(), // relations
        'queryHook' => function($query) {

            // add query here

            return $query;
        } // (optional) callable. This function hook is called before calling the $query->get() or $query->paginate()
    )
);

$result->isSuccessful(); // check if the command transaction was successful or not
$result->getStatusCode(); // the status code Ex 400,200,201 etc
$result->getData(); // the data or set of results
$result->getMessage(); // the message (success or error)
    

Query Single Content

On your controller, you can do this:


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

// by ID
$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\ContentBuilder\Commands\QueryContentCommand',
    array(
        'id' => 1,
        'with' => array(), // relations
        'disablePermissionChecking' => true // mandatory
    )
);

// by slug
$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\ContentBuilder\Commands\QueryContentCommand',
    array(
        'slug' => 'some-entry-2',
        'with' => array(), // relations
        'disablePermissionChecking' => true // mandatory
    )
);

// by title match
$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\ContentBuilder\Commands\QueryContentCommand',
    array(
        'title' => 'Some Title',
        'with' => array(), // relations
        'disablePermissionChecking' => true // mandatory
    )
);

$result->isSuccessful(); // check if the command transaction was successful or not
$result->getStatusCode(); // the status code Ex 400,200,201 etc
$result->getData(); // the data or set of results
$result->getMessage(); // the message (success or error)
    

Create Content


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\ContentBuilder\Commands\CreateContentCommand',
    array(
        'title' => 'Sample Content', // (required) string.
        'body' => 'some body', // (required) string.
        'slug' => 'sample-content', // (required) string.
        'authorId' => 1, // (required) int.  The author ID
        'contentTypeId' => 1, // (required) int. The content type ID
        'miscData' => 'string or array', // (optional)
        'status' => 'draft|publish', // (required)
        'metaData' => array( // This is likely the custom fields
            'form_group_name' => array(
                'field_key_1' => 'field value',
                'field_key_2' => 'field value',
                'field_key_3' => 'field value',
            )
        ), // (optional)
        'taxonomies' => array( // where key is the term ID
            1 => true,
            2 => true,
        ), // (optional)
        'disablePermissionChecking' => true, // (required) this is mandatory when using commands as API
    )
);
    

Update Content


// When updating, you may just provide only the fields you want to update
$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\ContentBuilder\Commands\UpdateContentCommand',
    array(
        'id' => 1, // (required)
        'title' => 'Sample Content', // (optional) string.
        'body' => 'some body', // (optional) string.
        'slug' => 'sample-content', // (optional) string.
        'authorId' => 1, // (optional) int.  The author ID
        'contentTypeId' => 1, // (optional) int. The content type ID
        'miscData' => 'string or array', // (optional)
        'status' => 'draft|publish', // (required)
        'metaData' => array( // This is likely the custom fields
        'form_group_name' => array(
            'field_key_1' => 'field value',
            'field_key_2' => 'field value',
            'field_key_3' => 'field value',
            )
        ), // (optional)
        'taxonomies' => array( // where key is the term ID
            1 => true,
            2 => true,
        ), // (optional)
        'disablePermissionChecking' => true, // (required) this is mandatory when using commands as API
    )
);
    

Delete Content


$commandDispatcher = app('Darryldecode\Backend\Base\Contracts\Bus\Dispatcher');

$result = $commandDispatcher->dispatchFromArray(
    'Darryldecode\Backend\Components\ContentBuilder\Commands\DeleteContentCommand',
    array(
        'id' => $id, // (required) int.
        'disablePermissionChecking' => true, // (required) this is mandatory when using commands as API
    )
);
    

Extending Content Model

There are cases you want to extend the Content's Model like adding new relations to it. To do that, follow steps below:

STEP 1.)

Navigate to "config/backend/backend.php" config file. Open that file and you will see on config array that looks like this:


/*
* built-in component models being used
*
* NOTE:
*
* The purpose of this is for extensibility, if you want to extend relationships for user/content model
* you can change this to your own and make sure to extend this models
*/
'user_model'    => 'Darryldecode\Backend\Components\User\Models\User',
'content_model' => 'Darryldecode\Backend\Components\ContentBuilder\Models\Content',

You will see above we have this two currently used models, Change it to your new model and make sure to extend it, like this:


namespace App\Backend\Extensions;

use Darryldecode\Backend\Components\ContentBuilder\Models\Content;

// your new class, name it whatever you want and just make sure you extent the main Content Model
class ContentExtended extends Content {

    // your new added relations to the Content Model
    public function newAddedRelation() {
        return $this->belongsToMany('anything here..');
    }
}

After you have created your new Content Model, make sure to update the backend config file, like so:


    /*
    * built-in component models being used
    *
    * NOTE:
    *
    * The purpose of this is for extensibility, if you want to extend relationships for user/content model
    * you can change this to your own and make sure to extend this models
    */
    'user_model'    => 'Darryldecode\Backend\Components\User\Models\User',
    'content_model' => 'App\Backend\Extensions\ContentExtended', // <-- your new Content Model

VOILA! You have now full control of your Content's Model! When querying content, you can now add querying its relations like so,


$result = $this->dispatchFromArray(
    'Darryldecode\Backend\Components\ContentBuilder\Commands\QueryContentsCommand',
    array(
        'type' => 'blog',
        'terms' => array(
        'Size'  => array('small'),
        'Color' => array('blue','green'),
        'Availability' => array('yes'),
        ),
        'paginated' => true,
        'status' => 'any',
        'disablePermissionChecking' => true,
        'with' => array('newAddedRelation') // <-- query your new relation
    )
);
More documentation coming soon or you may help us improve the docs by making pull request here: https://github.com/darryldecode/laravelbackend-site