Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- form.php
- <form method="post" class="form-horizontal" enctype="multipart/form-data">
- <div class="headerbar">
- <h1>Create New Home Page Portlet</h1>
- <?php $this->layout->load_view('layout/buttons'); ?>
- </div>
- <div class="content">
- <?php $this->layout->load_view('layout/alerts'); ?>
- <fieldset>
- <legend><small>* indicates required fields</small></legend>
- <div class="control-group">
- <label class="control-label">* Title: </label>
- <div class="controls">
- <input type="text" name="title" id="title" value="<?php echo $this->wc_portlets->form_value('title'); ?>">
- </div>
- </div>
- <div class="control-group">
- <label class="control-label">* Image: </label>
- <div class="controls">
- <input type="file" name="userfile" id="userfile">
- </div>
- </div>
- <div class="control-group">
- <label class="control-label">Status: </label>
- <div class="controls">
- <select name="status">
- <option value="1" name="1">Active</option>
- <option value="0" name="0">Inactive</option>
- </select>
- </div>
- </div>
- </fieldset>
- </div>
- </form>
- Controller
- <?php defined('BASEPATH') OR exit('No direct script access allowed');
- class Portlets extends Admin_Controller
- {
- /**
- * Constructor method
- */
- public function __construct()
- {
- parent::__construct();
- // Load Model
- $this->load->model('wc_portlets');
- $gallery_path = realpath(APPPATH . '/../assets/uploads');
- $gallery_path_url = base_url().'uploads/';
- }
- // Upload and Resize
- public function _do_upload_file()
- {
- //upload config
- $config = array(
- 'allowed_types' => '*', //jpg|jpeg|gif|png|pdf|JPEG|PNG|JPG|GIF|tiff|PDF
- 'upload_path' => $this->gallery_path,
- 'max_size' => 50000, //50MB limit
- 'overwrite' => false, //Doesnt overwrite exsisting
- 'remove_spaces' => true, // Removes any white space
- 'encrypt_name' => true // Encrypt file name
- );
- $this->load->library('upload', $config);
- $this->upload->initialize($config);
- if (!$this->upload->do_upload())
- {
- $this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());
- return FALSE;
- }
- else
- {
- // Resize Config
- $config['image_library'] = 'gd2';
- $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
- $config['new_image'] = $this->gallery_path . '/thumbs';
- $config['maintain_ratio'] = TRUE;
- $config['width'] = 150;
- $config['quality'] = '100%';
- $config['height'] = 150;
- /* echo $this->upload->upload_path.$this->upload->file_name . "<BR>"; */
- $this->load->library('image_lib', $config);
- $this->image_lib->initialize($config);
- $this->image_lib->resize();
- if (!$this->image_lib->resize()){
- $this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());
- }
- }
- }
- public function index()
- {
- // Show all portlets
- $this->layout->set('portlets', $this->wc_portlets->paginate()->result());
- $this->frontend->set('title', 'Portlets');
- $this->layout->buffer('content', 'portlets/index');
- $this->layout->render();
- }
- public function form($id = NULL)
- {
- if ($this->input->post('btn_cancel'))
- {
- redirect('portlets');
- }
- if ($this->wc_portlets->run_validation())
- {
- $id = $this->wc_portlets->save($id);
- redirect('portlets/index/' . $id);
- }
- if ($id and !$this->input->post('btn_submit'))
- {
- $this->wc_portlets->prep_form($id);
- }
- $this->layout->buffer('content', 'portlets/form');
- $this->layout->render();
- }
- public function delete($id)
- {
- $this->wc_portlets->delete($id);
- redirect('portlets/index');
- }
- }
- Admin_Controller
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- class Admin_Controller extends User_Controller {
- public function __construct()
- {
- date_default_timezone_set('Europe/London');
- parent::__construct('status', 1);
- $gallery_path = realpath(APPPATH . '/../assets/uploads');
- $gallery_path_url = base_url().'uploads/';
- }
- }
- ?>
- Model in which is saves to database
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- class Wc_portlets extends Response_Model {
- public $table = 'wc_portlets';
- public $primary_key = 'id';
- public $date_created_field = 'date_created';
- public function __construct()
- {
- parent::__construct();
- }
- public function default_select()
- {
- $this->db->select('wc_portlets.*');
- }
- public function default_order_by()
- {
- $this->db->order_by('wc_portlets.id');
- }
- // Upload and Resize
- public function createNew()
- {
- $image_data = $this->upload->data();
- $data['image'] = $image_data['file_name'];
- $this->db->insert('wc_portlets', $data);
- }
- public function validation_rules()
- {
- $image_data = $this->upload->data();
- return array(
- 'title' => array(
- 'field' => 'title',
- 'label' => 'Page Title',
- 'rules' => 'trim|required'
- ),
- 'image' => array(
- 'field' => 'userfile',
- 'label' => 'Image',
- 'rules' => 'callback__do_upload_file|trim'
- ),
- 'status' => array(
- 'field' => 'status',
- 'label' => 'Please select',
- 'rules' => 'trim'
- )
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement