Skip to content

Commit

Permalink
Inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jerel committed Dec 1, 2011
0 parents commit a132e57
Show file tree
Hide file tree
Showing 23 changed files with 613 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# PyroCMS Sample Module

* Version: 2.0 -- For PyroCMS v2.0
* Version: 1.0 -- For PyroCMS v1.3.x

## Legal

This module was written by Jerel Unruh, one of the PyroCMS core developers. You may praise, whine, or hire via [his website](http://unruhdesigns.com/)
or you may use [twitter](http://twitter.com/jerelunruh) if you prefer.

It is unlicensed, meaning that you can do what you want with it. [visit unlicense.org](http://unlicense.org)
Please spread the news about PyroCMS and point new developers here if you like.
You may also be interested in [PyroCMS Workless](http://github.com/jerel/pyrocms-workless) a theme based on iKreativ's Workless.

## Description

I put this module together to give new users something to start with when building their own modules. It makes use of some best practices (like using MY_Model to
simplify database interaction) and hopefully shows a couple of shortcuts (like using the pagination helper instead of the default CI pagination). It includes an
event file and it's own plugin.


## Usage

To use this module simply clone it using Git or download the zip. If you are running PyroCMS v1.3.2 you will want to download v1.0 of this module. Likewise if
you have v2.0 of PyroCMS you need to download v2.0 of this module. Once you have the folder you will need to rename it to "sample". This is very important as
PyroCMS uses the folder name to determine the class to call when installing or when loading a controller. This may mean that you will need to unzip the
downloaded version and then rezip it before uploading to your PyroCMS install.
Empty file added config/index.html
Empty file.
24 changes: 24 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| URI ROUTING
| -------------------------------------------------------------------------
| This file lets you re-map URI requests to specific controller functions.
|
| Typically there is a one-to-one relationship between a URL string
| and its corresponding controller class/method. The segments in a
| URL normally follow this pattern:
|
| www.your-site.com/class/method/id/
|
| In some instances, however, you may want to remap this relationship
| so that a different class/function is called than the one
| corresponding to the URL.
|
| Please see the user guide for complete details:
|
| http://www.codeigniter.com/user_guide/general/routing.html
*/

// front-end
$route['sample(/:num)?'] = 'sample/index$1';
133 changes: 133 additions & 0 deletions controllers/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* This is a sample module for PyroCMS
*
* @author Jerel Unruh - PyroCMS Dev Team
* @website http://unruhdesigns.com
* @package PyroCMS
* @subpackage Sample Module
*/
class Admin extends Admin_Controller
{

public function __construct()
{
parent::__construct();

// Load all the required classes
$this->load->model('sample_m');
$this->load->library('form_validation');
$this->lang->load('sample');

// Set the validation rules
$this->item_validation_rules = array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|max_length[100]|required'
),
array(
'field' => 'slug',
'label' => 'Slug',
'rules' => 'trim|max_length[100]|required'
)
);

// We'll set the partials and metadata here since they're used everywhere
$this->template->set_partial('shortcuts', 'admin/partials/shortcuts')
->append_metadata(js('admin.js', 'sample'))
->append_metadata(css('admin.css', 'sample'));
}

/**
* List all items
*/
public function index()
{
// here we use MY_Model's get_all() method to fetch everything
$items = $this->sample_m->get_all();

// Build the view with sample/views/admin/items.php
$this->data->items =& $items;
$this->template->title($this->module_details['name'])
->build('admin/items', $this->data);
}

public function create()
{
// Set the validation rules from the array above
$this->form_validation->set_rules($this->item_validation_rules);

// check if the form validation passed
if($this->form_validation->run())
{
// See if the model can create the record
if($this->sample_m->create($_POST))
{
// All good...
$this->session->set_flashdata('success', lang('sample.success'));
redirect('admin/sample');
}
// Something went wrong. Show them an error
else
{
$this->session->set_flashdata('error', lang('sample.error'));
redirect('admin/sample/create');
}
}

// Build the view using sample/views/admin/form.php
$this->template->title($this->module_details['name'], lang('sample.new_item'))
->build('admin/form', $this->data);
}

public function edit($id = 0)
{
$this->data = $this->sample_m->get($id);

// Set the validation rules from the array above
$this->form_validation->set_rules($this->item_validation_rules);

// check if the form validation passed
if($this->form_validation->run())
{
// get rid of the btnAction item that tells us which button was clicked.
// If we don't unset it MY_Model will try to insert it
unset($_POST['btnAction']);

// See if the model can create the record
if($this->sample_m->update($id, $_POST))
{
// All good...
$this->session->set_flashdata('success', lang('sample.success'));
redirect('admin/sample');
}
// Something went wrong. Show them an error
else
{
$this->session->set_flashdata('error', lang('sample.error'));
redirect('admin/sample/create');
}
}

// Build the view using sample/views/admin/form.php
$this->template->title($this->module_details['name'], lang('sample.edit'))
->build('admin/form', $this->data);
}

public function delete($id = 0)
{
// make sure the button was clicked and that there is an array of ids
if (isset($_POST['btnAction']) AND is_array($_POST['action_to']))
{
// pass the ids and let MY_Model delete the items
$this->sample_m->delete_many($this->input->post('action_to'));
}
elseif (is_numeric($id))
{
// they just clicked the link so we'll delete that one
$this->sample_m->delete($id);
}
redirect('admin/sample');
}
}
56 changes: 56 additions & 0 deletions controllers/sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* This is a sample module for PyroCMS
*
* @author Jerel Unruh - PyroCMS Dev Team
* @website http://unruhdesigns.com
* @package PyroCMS
* @subpackage Sample Module
*/
class Sample extends Public_Controller
{

public function __construct()
{
parent::__construct();

// Load the required classes
$this->load->model('sample_m');
$this->lang->load('sample');

$this->template->append_metadata(css('sample.css', 'sample'))
->append_metadata(js('sample.js', 'sample'));
}

/**
* All items
*/
public function index($offset = 0)
{
// set the pagination limit
$limit = 5;

// instead of using MY_Models get_all() we use our own get_array()
// because Tags cannot handle an object
$this->data->items = $this->sample_m
->get_array($limit, $offset);

// we'll do a quick check here so we can tell tags whether there is data or not
if ( ! $this->data->items) $this->data->empty = TRUE;

// we're using the pagination helper to do the pagination for us. Params are: (module/method, total count, limit, uri segment)
$this->data->pagination = create_pagination('sample', $this->sample_m->count_all(), $limit, 2);

/**
* You'll notice that we are setting the "pagination" partial. An alternative is
* to do $this->load->view('admin/partials/pagination'); in the view but that
* requires php. By setting the partial here and displaying it with
* {pyro:template:partial name="pagination"} in index.php we can get rid of php.
* The only requirement is that the pagination data is available to the partial
* so we set $this->data->pagination in this controller and it gets passed when we build.
*/
$this->template->title($this->module_details['name'], 'the rest of the page title')
->set_partial('pagination', 'admin/partials/pagination')
->build('index', $this->data);
}
}
Empty file added css/admin.css
Empty file.
5 changes: 5 additions & 0 deletions css/sample.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

.sample-data table { width: 100%; }
.sample-data th { font-weight: bold; border-bottom: 1px solid #d8d8d8; }
.sample-data th,td { padding: 5px; width: 50%; text-align: center; }
.sample-data td { background-color: #f2f2f2; }
90 changes: 90 additions & 0 deletions details.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');

class Module_Sample extends Module {

public $version = '1.0';

public function info()
{
return array(
'name' => array(
'en' => 'Sample'
),
'description' => array(
'en' => 'This is a PyroCMS module sample.'
),
'frontend' => TRUE,
'backend' => TRUE,
'menu' => 'content'
);
}

public function install()
{
$this->dbforge->drop_table('sample');
$this->db->delete('settings', array('module' => 'sample'));

$sample = array(
'id' => array(
'type' => 'INT',
'constraint' => '11',
'auto_increment' => TRUE
),
'name' => array(
'type' => 'VARCHAR',
'constraint' => '100'
),
'slug' => array(
'type' => 'VARCHAR',
'constraint' => '100'
)
);

$sample_setting = array(
'slug' => 'sample_setting',
'title' => 'Sample Setting',
'description' => 'A Yes or No option for the Sample module',
'`default`' => '1',
'`value`' => '1',
'type' => 'select',
'`options`' => '1=Yes|0=No',
'is_required' => 1,
'is_gui' => 1,
'module' => 'sample'
);

$this->dbforge->add_field($sample);
$this->dbforge->add_key('id', TRUE);

if($this->dbforge->create_table('sample') AND
$this->db->insert('settings', $sample_setting) AND
is_dir('uploads/sample') OR @mkdir('uploads/sample',0777,TRUE))
{
return TRUE;
}
}

public function uninstall()
{
$this->dbforge->drop_table('sample');
$this->db->delete('settings', array('module' => 'sample'));
{
return TRUE;
}
}


public function upgrade($old_version)
{
// Your Upgrade Logic
return TRUE;
}

public function help()
{
// Return a string containing help info
// You could include a file and return it here.
return "No documentation has been added for this module.<br />Contact the module developer for assistance.";
}
}
/* End of file details.php */
31 changes: 31 additions & 0 deletions events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Sample Events Class
*
* @package PyroCMS
* @subpackage Sample Module
* @category events
* @author Jerel Unruh - PyroCMS Dev Team
* @website http://unruhdesigns.com
*/
class Events_Sample {

protected $ci;

public function __construct()
{
$this->ci =& get_instance();

//register the public_controller event
Events::register('public_controller', array($this, 'run'));
}

public function run()
{
$this->ci->load->model('sample/sample_m');

$this->ci->sample_m->get_all();
}

}
/* End of file events.php */
Empty file added helpers/index.html
Empty file.
Binary file added img/album.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added js/admin.js
Empty file.
Empty file added js/sample.js
Empty file.
Loading

0 comments on commit a132e57

Please sign in to comment.