Advertisement
Guest User

Untitled

a guest
Mar 6th, 2013
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.38 KB | None | 0 0
  1. form.php
  2.  
  3. <form method="post" class="form-horizontal" enctype="multipart/form-data">
  4.  
  5.     <div class="headerbar">
  6.         <h1>Create New Home Page Portlet</h1>
  7.         <?php $this->layout->load_view('layout/buttons'); ?>
  8.     </div>
  9.  
  10.     <div class="content">
  11.  
  12.         <?php $this->layout->load_view('layout/alerts'); ?>
  13.        
  14.             <fieldset>
  15.                 <legend><small>* indicates required fields</small></legend>
  16.  
  17.                 <div class="control-group">
  18.                     <label class="control-label">* Title: </label>
  19.                     <div class="controls">
  20.                         <input type="text" name="title" id="title" value="<?php echo $this->wc_portlets->form_value('title'); ?>">
  21.                     </div>
  22.                 </div>
  23.  
  24.                 <div class="control-group">
  25.                     <label class="control-label">* Image: </label>
  26.                     <div class="controls">
  27.                         <input type="file" name="userfile" id="userfile">
  28.                     </div>
  29.  
  30.                 </div>
  31.                
  32.                 <div class="control-group">
  33.                     <label class="control-label">Status: </label>
  34.                     <div class="controls">
  35.                         <select name="status">
  36.                             <option value="1" name="1">Active</option>
  37.                             <option value="0" name="0">Inactive</option>
  38.                         </select>
  39.                     </div>
  40.                 </div>
  41.                
  42.                
  43.  
  44.             </fieldset>
  45.  
  46.     </div>
  47.  
  48. </form>
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. Controller
  56. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  57.  
  58. class Portlets extends Admin_Controller
  59. {
  60.     /**
  61.      * Constructor method
  62.      */
  63.     public function __construct()
  64.     {
  65.         parent::__construct();
  66.  
  67.         // Load Model
  68.         $this->load->model('wc_portlets');
  69.  
  70.         $gallery_path = realpath(APPPATH . '/../assets/uploads');
  71.         $gallery_path_url = base_url().'uploads/';
  72.     }
  73.     // Upload and Resize
  74.     public function _do_upload_file()
  75.     {  
  76.  
  77.         //upload config
  78.         $config = array(
  79.              'allowed_types' => '*', //jpg|jpeg|gif|png|pdf|JPEG|PNG|JPG|GIF|tiff|PDF
  80.              'upload_path'   => $this->gallery_path,
  81.              'max_size'      => 50000, //50MB limit
  82.              'overwrite'     => false, //Doesnt overwrite exsisting
  83.             'remove_spaces' => true, // Removes any white space
  84.             'encrypt_name'  => true // Encrypt file name
  85.         );
  86.        
  87.         $this->load->library('upload', $config);
  88.          $this->upload->initialize($config);  
  89.          
  90.        
  91.         if (!$this->upload->do_upload())
  92.         {
  93.             $this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());
  94.             return FALSE;
  95.         }
  96.         else
  97.         {
  98.         // Resize Config
  99.             $config['image_library'] = 'gd2';
  100.             $config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
  101.             $config['new_image'] = $this->gallery_path . '/thumbs';
  102.             $config['maintain_ratio'] = TRUE;
  103.             $config['width'] = 150;
  104.             $config['quality'] = '100%';
  105.             $config['height'] = 150;
  106.            
  107.             /* echo $this->upload->upload_path.$this->upload->file_name . "<BR>";  */
  108.            
  109.             $this->load->library('image_lib', $config);
  110.             $this->image_lib->initialize($config);
  111.             $this->image_lib->resize();
  112.                  
  113.             if (!$this->image_lib->resize()){
  114.                    $this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());              
  115.             }
  116.            
  117.            
  118.         }
  119.  
  120.     }
  121.  
  122.     public function index()
  123.     {
  124.         // Show all portlets
  125.         $this->layout->set('portlets', $this->wc_portlets->paginate()->result());
  126.         $this->frontend->set('title', 'Portlets');
  127.         $this->layout->buffer('content', 'portlets/index');
  128.         $this->layout->render();
  129.     }
  130.     public function form($id = NULL)
  131.     {
  132.         if ($this->input->post('btn_cancel'))
  133.         {
  134.             redirect('portlets');
  135.         }
  136.  
  137.         if ($this->wc_portlets->run_validation())
  138.         {
  139.             $id = $this->wc_portlets->save($id);
  140.             redirect('portlets/index/' . $id);
  141.  
  142.         }
  143.  
  144.         if ($id and !$this->input->post('btn_submit'))
  145.         {
  146.             $this->wc_portlets->prep_form($id);
  147.         }
  148.  
  149.         $this->layout->buffer('content', 'portlets/form');
  150.         $this->layout->render();
  151.     }
  152.     public function delete($id)
  153.     {
  154.         $this->wc_portlets->delete($id);
  155.         redirect('portlets/index');
  156.     }
  157.    
  158.  
  159.  
  160. }
  161.  
  162.  
  163. Admin_Controller
  164. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  165.  
  166.  
  167. class Admin_Controller extends User_Controller {
  168.  
  169.     public function __construct()
  170.     {
  171.         date_default_timezone_set('Europe/London');
  172.         parent::__construct('status', 1);
  173.  
  174.         $gallery_path = realpath(APPPATH . '/../assets/uploads');
  175.         $gallery_path_url = base_url().'uploads/';
  176.     }
  177.  
  178. }
  179.  
  180. ?>
  181.  
  182.  
  183. Model in which is saves to database
  184. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  185.  
  186.  
  187. class Wc_portlets extends Response_Model {
  188.    
  189.     public $table = 'wc_portlets';
  190.     public $primary_key = 'id';
  191.     public $date_created_field = 'date_created';
  192.  
  193.     public function __construct()
  194.     {
  195.         parent::__construct();
  196.     }  
  197.  
  198.     public function default_select()
  199.     {
  200.         $this->db->select('wc_portlets.*');
  201.     }
  202.    
  203.     public function default_order_by()
  204.     {
  205.         $this->db->order_by('wc_portlets.id');
  206.     }
  207.     // Upload and Resize
  208.    
  209.     public function createNew()
  210.     {
  211.         $image_data = $this->upload->data();
  212.         $data['image'] = $image_data['file_name'];
  213.  
  214.         $this->db->insert('wc_portlets', $data);
  215.        
  216.     }
  217.     public function validation_rules()
  218.     {
  219.  
  220.         $image_data = $this->upload->data();
  221.  
  222.         return array(
  223.             'title' => array(
  224.                 'field' => 'title',
  225.                 'label' => 'Page Title',
  226.                 'rules' => 'trim|required'
  227.             ),
  228.             'image' => array(
  229.                 'field' => 'userfile',
  230.                 'label' => 'Image',
  231.                 'rules' => 'callback__do_upload_file|trim'
  232.             ),
  233.             'status' => array(
  234.                 'field' => 'status',
  235.                 'label' => 'Please select',
  236.                 'rules' => 'trim'
  237.             )
  238.         );
  239.        
  240.     }
  241.  
  242.  
  243.  
  244.  
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement