Skip to content

Instantly share code, notes, and snippets.

@airways
Last active August 29, 2015 14:20
Show Gist options
  • Save airways/5b93012ead357c285737 to your computer and use it in GitHub Desktop.
Save airways/5b93012ead357c285737 to your computer and use it in GitHub Desktop.
FuelCMS "site" module with custom datatypes ("modules") setup
<?php
/*
|--------------------------------------------------------------------------
| FUEL NAVIGATION: An array of navigation items for the left menu in the admin
|--------------------------------------------------------------------------
*/
$config['nav']['site'] = array(
'site/articles' => lang('module_site_articles'),
'site/authors' => lang('module_site_authors'),
);
<?php
/*
|--------------------------------------------------------------------------
| CONSTANTS: Optional file to make it easier to refer to values throughout the site config
|--------------------------------------------------------------------------
*/
define('SITE_VERSION', '1.00');
define('SITE_FOLDER', 'site');
define('SITE_PATH', __DIR__);
<?php
/*
|--------------------------------------------------------------------------
| SITE Custom Modules
|--------------------------------------------------------------------------
|
| Specifies the module controller (key) and the name (value) for fuel
| This is the real configuration for the site "advanced module", which itself
| consists only of two simple modules: authors and articles
*/
$config['modules']['authors'] = array(
'module_name' => 'Authors',
'module_uri' => 'site/authors',
'model_name' => 'authors_model',
'model_location' => 'site',
'table_headers' => array(
'id',
'name',
'email',
),
'folder' => 'site',
'display_field' => 'name',
'default_col' => 'name',
'default_order' => 'asc',
'nav_selected' => 'site/authors',
'sanitize_input' => array('template','php'),
'language' => array('site' => 'site'),
);
$config['modules']['articles'] = array(
'module_name' => 'Articles',
'module_uri' => 'site/articles',
'model_name' => 'articles_model',
'model_location' => 'site',
'table_headers' => array(
'id',
'title',
),
'folder' => 'site',
'display_field' => 'title',
'default_col' => 'title',
'default_order' => 'asc',
'nav_selected' => 'site/articles',
'sanitize_input' => array('template','php'),
'language' => array('site' => 'site'),
);
//$config['modules']['categories'] = array();
<?php
/*
|--------------------------------------------------------------------------
| ROUTING Some internal mapping for FUEL, just copy/paste and modify
|--------------------------------------------------------------------------
|
*/
$blog_controllers = array('authors', 'articles');
foreach($blog_controllers as $c)
{
$route[FUEL_ROUTE.'site/'.$c] = FUEL_FOLDER.'/module';
$route[FUEL_ROUTE.'site/'.$c.'/(.*)'] = FUEL_FOLDER.'/module/$1';
}
//$route[FUEL_ROUTE.'site/settings'] = SITE_FOLDER.'/settings';
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
|--------------------------------------------------------------------------
| MODEL Example model copied from FuelCMS documentation on "simple modules":
| http://docs.getfuelcms.com/modules/simple
|--------------------------------------------------------------------------
*/
require_once(FUEL_PATH.'models/base_module_model.php');
class Articles_model extends Base_module_model {
// read more about models in the user guide to get a list of all properties. Below is a subset of the most common:
public $filters = array('content'); // filters to apply to when searching for items
public $filter_join = 'or'; // how to combine the filters in the query (and or or)
public $required = array('title'); // an array of required fields. If a key => val is provided, the key is name of the field and the value is the error message to display
public $foreign_keys = array('author_id' => 'site/authors_model'); // map foreign keys to table models
public $linked_fields = array(); // fields that are linked meaning one value helps to determine another. Key is the field, value is a function name to transform it. (e.g. array('slug' => 'title'), or array('slug' => arry('name' => 'strtolower')));
public $boolean_fields = array(); // fields that are tinyint and should be treated as boolean
public $unique_fields = array('slug'); // fields that are not IDs but are unique. Can also be an array of arrays for compound keys
public $parsed_fields = array(); // fields to automatically parse
public $serialized_fields = array(); // fields that contain serialized data. This will automatically serialize before saving and unserialize data upon retrieving
public $has_many = array('tags' => array(FUEL_FOLDER => 'fuel_tags_model')); // keys are model, which can be a key value pair with the key being the module and the value being the model, module (if not specified in model parameter), relationships_model, foreign_key, candidate_key
public $belongs_to = array(); // keys are model, which can be a key value pair with the key being the module and the value being the model, module (if not specified in model parameter), relationships_model, foreign_key, candidate_key
public $formatters = array(); // an array of helper formatter functions related to a specific field type (e.g. string, datetime, number), or name (e.g. title, content) that can augment field results
public $display_unpublished_if_logged_in = FALSE;
protected $friendly_name = ''; // a friendlier name of the group of objects
protected $singular_name = ''; // a friendly singular name of the object
function __construct()
{
parent::__construct('articles'); // table name
}
function list_items($limit = NULL, $offset = NULL, $col = 'name', $order = 'asc', $just_count = FALSE)
{
$this->db->join('authors', 'authors.id = articles.author_id', 'left');
$this->db->select('articles.id, title, SUBSTRING(content, 1, 50) AS content, authors.name AS author, date_added, articles.published', FALSE);
$data = parent::list_items($limit, $offset, $col, $order, $just_count);
if (empty($just_count))
{
foreach($data as $key => $val)
{
$data[$key]['content'] = htmlentities($val['content'], ENT_QUOTES, 'UTF-8');
}
}
return $data;
}
function form_fields($values = array(), $related = array())
{
$fields = parent::form_fields($values, $related);
// ******************* ADD CUSTOM FORM STUFF HERE *******************
$fields['content']['img_folder'] = 'articles/';
$fields['image']['folder'] = 'images/articles/';
$fields['thumb_image']['folder'] = 'images/articles/thumbs/';
return $fields;
}
function tree()
{
return $this->_tree('has_many');
}
}
class Article_model extends Base_module_record {
public function get_url()
{
return site_url('articles/'.$this->slug);
}
public function get_image_path()
{
return img_path('images/articles/'.$this->image);
}
public function get_thumb_image_path()
{
return img_path('images/articles/'.$this->thumb_image);
}
}
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
|--------------------------------------------------------------------------
| MODEL Example model copied from FuelCMS documentation on "simple modules":
| http://docs.getfuelcms.com/modules/simple
|--------------------------------------------------------------------------
*/
require_once(FUEL_PATH.'models/base_module_model.php');
class Authors_model extends Base_module_model {
// read more about models in the user guide to get a list of all properties. Below is a subset of the most common:
public $filters = array('name', 'email'); // filters to apply to when searching for items
public $required = array('name', 'email'); // an array of required fields. If a key => val is provided, the key is name of the field and the value is the error message to display
public $foreign_keys = array(); // map foreign keys to table models
public $linked_fields = array(); // fields that are linked meaning one value helps to determine another. Key is the field, value is a function name to transform it. (e.g. array('slug' => 'title'), or array('slug' => arry('name' => 'strtolower')));
public $boolean_fields = array(); // fields that are tinyint and should be treated as boolean
public $unique_fields = array(); // fields that are not IDs but are unique. Can also be an array of arrays for compound keys
public $parsed_fields = array(); // fields to automatically parse
public $serialized_fields = array(); // fields that contain serialized data. This will automatically serialize before saving and unserialize data upon retrieving
public $has_many = array(); // keys are model, which can be a key value pair with the key being the module and the value being the model, module (if not specified in model parameter), relationships_model, foreign_key, candidate_key
public $belongs_to = array(); // keys are model, which can be a key value pair with the key being the module and the value being the model, module (if not specified in model parameter), relationships_model, foreign_key, candidate_key
public $formatters = array(); // an array of helper formatter functions related to a specific field type (e.g. string, datetime, number), or name (e.g. title, content) that can augment field results
public $display_unpublished_if_logged_in = FALSE;
protected $friendly_name = ''; // a friendlier name of the group of objects
protected $singular_name = ''; // a friendly singular name of the object
function __construct()
{
parent::__construct('authors'); // table name
}
}
class Author_model extends Base_module_record {
// put your record model code here
}
<?php
/*
|--------------------------------------------------------------------------
| PUBLIC This is the public side view of an article, available at:
| articles/{slug}
|--------------------------------------------------------------------------
*/
$slug = uri_segment(2);
if ($slug) :
$article = fuel_model('articles', array('find' => 'one', 'where' => array('slug' => $slug)));
if (empty($article)) :
redirect_404();
endif;
else:
$tags = fuel_model('tags');
endif;
if (!empty($article)) : ?>
<h1><?=fuel_edit($article)?><?=$article->title?></h1>
<div class="author"><?=$article->author->name?></div>
<img src="<?=$article->image_path?>" alt="<?=$article->title_entities?>" class="img_right" />
<article><?=$article->content_formatted?></article>
<?php else: ?>
<h1>Articles</h1>
<?=fuel_edit('create?title=xxx', 'Create Article', 'articles')?>
<?php foreach($tags as $tag) : ?>
<h2><?=$tag->name?></h2>
<ul>
<?php foreach($tag->articles as $article) : ?>
<li><?=fuel_edit($article)?><a href="<?=$article->url?>"><?=$article->title?></a></li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment