Skip to content

Commit

Permalink
Add shortcode registry functionality to modules.
Browse files Browse the repository at this point in the history
Add the ability for a module to register a new shortcode and updated
the content_filter to utilize twig for rendering shortcodes in content.
  • Loading branch information
Noah Mormino committed Jul 6, 2015
1 parent 3b17675 commit e1648cf
Show file tree
Hide file tree
Showing 8 changed files with 325 additions and 136 deletions.
7 changes: 5 additions & 2 deletions application/controllers/Bootstrap.php
Expand Up @@ -46,6 +46,7 @@ public function init()

$paymentModules = [];
$shippingModules = [];
$themeShortcodes = [];
$routes = [];
$modules = [];

Expand Down Expand Up @@ -93,7 +94,8 @@ public function init()

$manifest .= '//ClassMap for autoloader'."\n".'$classes = '.var_export($this->classMap, true).';';
$manifest .= "\n\n".'//Available Payment Modules'."\n".'$GLOBALS[\'paymentModules\'] ='.var_export($paymentModules, true).';';
$manifest .= "\n\n".'//Available SHipping Modules'."\n".'$GLOBALS[\'shippingModules\'] = '.var_export($shippingModules, true).';';
$manifest .= "\n\n".'//Available Shipping Modules'."\n".'$GLOBALS[\'shippingModules\'] = '.var_export($shippingModules, true).';';
$manifest .= "\n\n".'//Theme Shortcodes'."\n".'$GLOBALS[\'themeShortcodes\'] = '.var_export($themeShortcodes, true).';';
$manifest .= "\n\n".'//Complete Module List'."\n".'$GLOBALS[\'modules\'] = '.var_export($modules,true).';';
$manifest .= "\n\n".'//Defined Routes'."\n".'$routes = '.var_export($routes,true).';';

Expand Down Expand Up @@ -199,6 +201,7 @@ public function init()
call_user_func_array([$class, $target[1]], $match['params']);

} catch(Exception $e) {
var_dump($e);
throw_404();
}
}
Expand Down Expand Up @@ -251,4 +254,4 @@ private function getPhpClasses($file) {
}
}
}
}
}
230 changes: 230 additions & 0 deletions application/modules/banners/controllers/Banners.php
@@ -0,0 +1,230 @@
<?php namespace GoCart\Controller;
/**
* AdminBanners Class
*
* @package GoCart
* @subpackage Controllers
* @category AdminBanners
* @author Clear Sky Designs
* @link http://gocartdv.com
*/

class AdminBanners extends Admin {

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

\CI::load()->model('Banners');
\CI::lang()->load('banners');
}

public function get_details()
{
return $this->details;
}

public function index()
{
$data['page_title'] = lang('banner_collections');

$data['banner_collections'] = \CI::Banners()->banner_collections();
$this->view('banner_collections', $data);
}

public function banner_collection_form($banner_collection_id = false)
{
$data['page_title'] = lang('banner_collection_form');

\CI::load()->library('form_validation');

$data['banner_collection_id'] = $banner_collection_id;
$data['name'] = '';

if($banner_collection_id)
{
$banner_collection = \CI::Banners()->banner_collection($banner_collection_id);

if(!$banner_collection)
{
\CI::session()->set_flashdara('error', lang('banner_collection_not_found'));
redirect('admin/banners');
}
else
{
$data = array_merge($data, (array)$banner_collection);
}
}

\CI::form_validation()->set_rules('name', 'lang:name', 'trim|required');

if (\CI::form_validation()->run() == false)
{
$this->view('banner_collection_form', $data);
}
else
{
$save['banner_collection_id'] = $banner_collection_id;
$save['name'] = \CI::input()->post('name');

\CI::Banners()->save_banner_collection($save);

\CI::session()->set_flashdata('message', lang('message_banner_collection_saved'));

redirect('admin/banners');
}
}

public function delete_banner_collection($banner_collection_id)
{
$banner_collection = \CI::Banners()->banner_collection($banner_collection_id);
if(!$banner_collection)
{
\CI::session()->set_flashdata('error', lang('banner_collection_not_found'));
}
else
{
\CI::Banners()->delete_banner_collection($banner_collection_id);
\CI::session()->set_flashdata('message', lang('message_delete_banner_collection'));
}

redirect('admin/banners');
}

public function banner_collection($banner_collection_id)
{
$data['banner_collection'] = \CI::Banners()->banner_collection($banner_collection_id);
if(!$data['banner_collection'])
{
\CI::session()->set_flashdata('error', lang('banner_collection_not_found'));
redirect('admin/banners');
}

$data['banner_collection_id'] = $banner_collection_id;
$data['page_title'] = lang('banners').' : '.$data['banner_collection']->name;
$data['banners'] = \CI::Banners()->banner_collection_banners($banner_collection_id);

$this->view('banner_collection', $data);
}

public function banner_form($banner_collection_id, $id = false)
{

$config['upload_path'] = 'uploads';
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = true;
\CI::load()->library('upload', $config);


\CI::load()->helper(array('form', 'date'));
\CI::load()->library('form_validation');

//set the default values
$data = array( 'banner_id' => $id,
'banner_collection_id' => $banner_collection_id,
'name' => '',
'enable_date' => '',
'disable_date' => '',
'image' => '',
'link' => '',
'new_window' => false
);

if($id)
{
$data = array_merge($data, (array)\CI::Banners()->banner($id));
$data['new_window'] = (bool) $data['new_window'];
}

$data['page_title'] = lang('banner_form');

\CI::form_validation()->set_rules('name', 'lang:name', 'trim|required|full_decode');
\CI::form_validation()->set_rules('enable_date', 'lang:enable_date', 'trim');
\CI::form_validation()->set_rules('disable_date', 'lang:disable_date', 'trim');
\CI::form_validation()->set_rules('image', 'lang:image', 'trim');
\CI::form_validation()->set_rules('link', 'lang:link', 'trim');
\CI::form_validation()->set_rules('new_window', 'lang:new_window', 'trim');

if (\CI::form_validation()->run() == false)
{
$data['error'] = validation_errors();
$this->view('banner_form', $data);
}
else
{

$uploaded = \CI::upload()->do_upload('image');

$save['banner_collection_id'] = $banner_collection_id;
$save['name'] = \CI::input()->post('name');
$save['enable_date'] = \CI::input()->post('enable_date');
$save['disable_date'] = \CI::input()->post('disable_date');
$save['link'] = \CI::input()->post('link');
$save['new_window'] = (bool)\CI::input()->post('new_window');

if ($id)
{
$save['banner_id'] = $id;

//delete the original file if another is uploaded
if($uploaded)
{
if($data['image'] != '')
{
$file = 'uploads/'.$data['image'];

//delete the existing file if needed
if(file_exists($file))
{
unlink($file);
}
}
}

}
else
{
if(!$uploaded)
{
$data['error'] = \CI::upload()->display_errors();
$this->view('banner_form', $data);
return; //end script here if there is an error
}
}

if($uploaded)
{
$image = \CI::upload()->data();
$save['image'] = $image['file_name'];
}

\CI::Banners()->save_banner($save);

\CI::session()->set_flashdata('message', lang('message_banner_saved'));

redirect('admin/banners/banner_collection/'.$banner_collection_id);
}
}

public function delete_banner($banner_id)
{
$banner = \CI::Banners()->banner($banner_id);
if(!$banner)
{
\CI::session()->set_flashdata('error', lang('banner_not_found'));
}
else
{
\CI::Banners()->delete_banner($banner_id);
\CI::session()->set_flashdata('message', lang('message_delete_banner'));
}

redirect('admin/banners/banner_collection/'.$banner->banner_collection_id);
}

public function organize()
{
$banners = \CI::input()->post('banners');
\CI::Banners()->organize($banners);
}
}
4 changes: 3 additions & 1 deletion application/modules/banners/manifest.php
Expand Up @@ -6,4 +6,6 @@
$routes[] = ['GET|POST', '/admin/banners/banner_collection/[i:id]', 'GoCart\Controller\AdminBanners#banner_collection'];
$routes[] = ['GET|POST', '/admin/banners/banner_form/[i:banner_collection_id]/[i:id]?', 'GoCart\Controller\AdminBanners#banner_form'];
$routes[] = ['GET|POST', '/admin/banners/delete_banner/[i:id]', 'GoCart\Controller\AdminBanners#delete_banner'];
$routes[] = ['GET|POST', '/admin/banners/organize', 'GoCart\Controller\AdminBanners#organize'];
$routes[] = ['GET|POST', '/admin/banners/organize', 'GoCart\Controller\AdminBanners#organize'];

$themeShortcodes[] = ['shortcode'=>'banner', 'method'=>['Banners', 'show_collection']];
27 changes: 14 additions & 13 deletions application/modules/banners/models/Banners.php
Expand Up @@ -12,17 +12,17 @@
Class Banners extends CI_Model
{

function banner_collections()
public function banner_collections()
{
return CI::db()->order_by('name', 'ASC')->get('banner_collections')->result();
}

function banner_collection($banner_collection_id)
public function banner_collection($banner_collection_id)
{
return CI::db()->where('banner_collection_id', $banner_collection_id)->get('banner_collections')->row();
}

function banner_collection_banners($banner_collection_id, $only_active=false, $limit=5)
public function banner_collection_banners($banner_collection_id, $only_active=false, $limit=5)
{
CI::db()->where('banner_collection_id', $banner_collection_id);
$banners = CI::db()->order_by('sequence', 'ASC')->get('banners')->result();
Expand Down Expand Up @@ -77,7 +77,7 @@ function banner_collection_banners($banner_collection_id, $only_active=false, $l
}
}

function banner($banner_id)
public function banner($banner_id)
{
CI::db()->where('banner_id', $banner_id);
$result = CI::db()->get('banners');
Expand All @@ -103,7 +103,7 @@ function banner($banner_id)
}
}

function save_banner($data)
public function save_banner($data)
{
if(isset($data['banner_id']))
{
Expand All @@ -117,7 +117,7 @@ function save_banner($data)
}
}

function save_banner_collection($data)
public function save_banner_collection($data)
{
if(isset($data['banner_collection_id']) && (bool)$data['banner_collection_id'])
{
Expand All @@ -130,7 +130,7 @@ function save_banner_collection($data)
}
}

function get_homepage_banners($limit = false)
public function get_homepage_banners($limit = false)
{
$banners = CI::db()->order_by('sequence ASC')->get('banners')->result();
$count = 1;
Expand Down Expand Up @@ -182,13 +182,13 @@ function get_homepage_banners($limit = false)
return $banners;
}

function delete_banner($banner_id)
public function delete_banner($banner_id)
{
CI::db()->where('banner_id', $banner_id);
CI::db()->delete('banners');
}

function delete_banner_collection($banner_collection_id)
public function delete_banner_collection($banner_collection_id)
{
CI::db()->where('banner_collection_id', $banner_collection_id);
CI::db()->delete('banners');
Expand All @@ -197,7 +197,7 @@ function delete_banner_collection($banner_collection_id)
CI::db()->delete('banner_collections');
}

function getNextSequence($banner_collection_id)
public function getNextSequence($banner_collection_id)
{
CI::db()->where('banner_collection_id', $banner_collection_id);
CI::db()->select('sequence');
Expand All @@ -215,7 +215,7 @@ function getNextSequence($banner_collection_id)
}
}

function organize($banners)
public function organize($banners)
{
foreach ($banners as $sequence => $id)
{
Expand All @@ -225,11 +225,12 @@ function organize($banners)
}
}

function show_collection($banner_collection_id, $quantity=5, $theme='default')
public function show_collection($banner_collection_id, $quantity=5, $theme='default')
{
$data['id'] = $banner_collection_id;
$data['banners'] = $this->banner_collection_banners($banner_collection_id, true, $quantity);

return \GoCart\Libraries\View::getInstance()->get('banners/'.$theme, $data);

echo \GoCart\Libraries\View::getInstance()->get('banners/'.$theme, $data);
}
}
12 changes: 12 additions & 0 deletions application/modules/categories/controllers/Category.php
Expand Up @@ -44,4 +44,16 @@ public function index($slug, $sort='id', $dir="ASC", $page=0) {
$this->view('categories/category', $categories);
}

public function shortcode($slug = false, $perPage = false)
{
if(!$perPage)
{
$perPage = config_item('products_per_page');
}

$products = \CI::Categories()->get($slug, 'id', 'ASC', 0, $perPage);

return $this->partial('categories/products', $products);
}

}

0 comments on commit e1648cf

Please sign in to comment.