Skip to content

Instantly share code, notes, and snippets.

@thinkadoo
Created June 2, 2013 09:35
Show Gist options
  • Save thinkadoo/5693149 to your computer and use it in GitHub Desktop.
Save thinkadoo/5693149 to your computer and use it in GitHub Desktop.
Function assets for CIBonfire Controller - helps to serve css, js, images, ejs etc. from the module folder
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class content extends Admin_Controller {
//--------------------------------------------------------------------
public function __construct()
{
parent::__construct();
$this->auth->restrict('Deploy.Content.View');
$this->load->model('deploy_model', null, true);
$this->lang->load('deploy');
$this->load->library('twig');
Template::set_block('sub_nav', 'content/_sub_nav');
}
//--------------------------------------------------------------------
function assets() {
//---get working directory and map it to your module
$word = 'public';
$string = getcwd();
$string = str_replace($word, "", $string);
$parts = $this->uri->segments;
$file = $string . 'application/modules/' . $parts[3] .'/'. $parts[4] .'/'. $parts[5] .'/'. $parts[6];
$path_parts = pathinfo($file);
//---set the type for the headers
$file_type= strtolower($path_parts['extension']);
if (is_file($file)) {
//----write propper headers
switch ($file_type) {
case 'css':
header('Content-type: text/css');
break;
case 'js':
header('Content-type: text/javascript');
break;
case 'json':
header('Content-type: application/json');
break;
case 'xml':
header('Content-type: text/xml');
break;
case 'pdf':
header('Content-type: application/pdf');
break;
case 'jpg' || 'jpeg' || 'png' || 'gif':
header('Content-type: image/'.$file_type);
readfile($file);
exit;
break;
}
include $file;
} else {
show_404();
}
exit;
}
/*
Method: index()
Displays a list of form data.
*/
public function index()
{
// Deleting anything?
if (isset($_POST['delete']))
{
$checked = $this->input->post('checked');
if (is_array($checked) && count($checked))
{
$result = FALSE;
foreach ($checked as $pid)
{
$result = $this->deploy_model->delete($pid);
}
if ($result)
{
Template::set_message(count($checked) .' '. lang('deploy_delete_success'), 'success');
}
else
{
Template::set_message(lang('deploy_delete_failure') . $this->deploy_model->error, 'error');
}
}
}
$records = $this->deploy_model->find_all();
//$this->load->library('twig');
//$data['title'] = "Testing Twig!!";
//$buffer = $this->twig->render('view.html', $data);
$this->load->library('twig'); // load the Twig library
$this->load->helper('url'); // load the CodeIgniter URL helper
// map the base_url() function as a Twig function
$this->twig->add_function('base_url');
$buffer = $this->twig->render('view.html');
Template::set('buffer', $buffer);
Template::set('records', $records);
Template::set('toolbar_title', 'Manage Deploy');
Template::render();
}
//--------------------------------------------------------------------
/*
Method: create()
Creates a Deploy object.
*/
public function create()
{
$this->auth->restrict('Deploy.Content.Create');
if (isset($_POST['save']))
{
if ($insert_id = $this->save_deploy())
{
// Log the activity
$this->activity_model->log_activity($this->current_user->id, lang('deploy_act_create_record').': ' . $insert_id . ' : ' . $this->input->ip_address(), 'deploy');
Template::set_message(lang('deploy_create_success'), 'success');
redirect(SITE_AREA .'/content/deploy');
}
else
{
Template::set_message(lang('deploy_create_failure') . $this->deploy_model->error, 'error');
}
}
Assets::add_module_js('deploy', 'deploy.js');
Template::set('toolbar_title', lang('deploy_create') . ' Deploy');
Template::render();
}
//--------------------------------------------------------------------
/*
Method: edit()
Allows editing of Deploy data.
*/
public function edit()
{
$id = $this->uri->segment(5);
if (empty($id))
{
Template::set_message(lang('deploy_invalid_id'), 'error');
redirect(SITE_AREA .'/content/deploy');
}
if (isset($_POST['save']))
{
$this->auth->restrict('Deploy.Content.Edit');
if ($this->save_deploy('update', $id))
{
// Log the activity
$this->activity_model->log_activity($this->current_user->id, lang('deploy_act_edit_record').': ' . $id . ' : ' . $this->input->ip_address(), 'deploy');
Template::set_message(lang('deploy_edit_success'), 'success');
}
else
{
Template::set_message(lang('deploy_edit_failure') . $this->deploy_model->error, 'error');
}
}
else if (isset($_POST['delete']))
{
$this->auth->restrict('Deploy.Content.Delete');
if ($this->deploy_model->delete($id))
{
// Log the activity
$this->activity_model->log_activity($this->current_user->id, lang('deploy_act_delete_record').': ' . $id . ' : ' . $this->input->ip_address(), 'deploy');
Template::set_message(lang('deploy_delete_success'), 'success');
redirect(SITE_AREA .'/content/deploy');
} else
{
Template::set_message(lang('deploy_delete_failure') . $this->deploy_model->error, 'error');
}
}
Template::set('deploy', $this->deploy_model->find($id));
Assets::add_module_js('deploy', 'deploy.js');
Template::set('toolbar_title', lang('deploy_edit') . ' Deploy');
Template::render();
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// !PRIVATE METHODS
//--------------------------------------------------------------------
/*
Method: save_deploy()
Does the actual validation and saving of form data.
Parameters:
$type - Either "insert" or "update"
$id - The ID of the record to update. Not needed for inserts.
Returns:
An INT id for successful inserts. If updating, returns TRUE on success.
Otherwise, returns FALSE.
*/
private function save_deploy($type='insert', $id=0)
{
if ($type == 'update') {
$_POST['id'] = $id;
}
$this->form_validation->set_rules('deploy_project','Project','required|max_length[255]');
$this->form_validation->set_rules('deploy_type','Type','required|max_length[255]');
if ($this->form_validation->run() === FALSE)
{
return FALSE;
}
// make sure we only pass in the fields we want
$data = array();
$data['deploy_project'] = $this->input->post('deploy_project');
$data['deploy_type'] = $this->input->post('deploy_type');
if ($type == 'insert')
{
$id = $this->deploy_model->insert($data);
if (is_numeric($id))
{
$return = $id;
} else
{
$return = FALSE;
}
}
else if ($type == 'update')
{
$return = $this->deploy_model->update($id, $data);
}
return $return;
}
//--------------------------------------------------------------------
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment