Scaffolding.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * Scaffolding Class
00020  *
00021  * Provides the Scaffolding framework
00022  *
00023  * @package             CodeIgniter
00024  * @subpackage  Scaffolding
00025  * @author              ExpressionEngine Dev Team
00026  * @link                http://codeigniter.com/user_guide/general/scaffolding.html
00027  */
00028 class Scaffolding {
00029 
00030         var $CI;
00031         var $current_table;
00032         var $base_url = '';
00033         var $lang = array();
00034 
00035         function Scaffolding($db_table)
00036         {
00037                 $this->CI =& get_instance();
00038                 
00039                 $this->CI->load->database("", FALSE, TRUE);                     
00040                 $this->CI->load->library('pagination');
00041                 
00042                 // Turn off caching
00043                 $this->CI->db->cache_off();
00044                                 
00045                 /**
00046                  * Set the current table name
00047                  * This is done when initializing scaffolding:
00048                  * $this->load->scaffolding('table_name')
00049                  *
00050                  */
00051                 $this->current_table = $db_table;
00052                 
00053                 /**
00054                  * Set the path to the "view" files
00055                  * We'll manually override the "view" path so that
00056                  * the load->view function knows where to look.
00057                  */
00058                 
00059                 $this->CI->load->_ci_view_path = BASEPATH.'scaffolding/views/';
00060 
00061                 // Set the base URL
00062                 $this->base_url = $this->CI->config->site_url().'/'.$this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'both');
00063                 $this->base_uri = $this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'leading');
00064 
00065                 // Set a few globals
00066                 $data = array(
00067                                                 'image_url'     => $this->CI->config->system_url().'scaffolding/images/',
00068                                                 'base_uri'  => $this->base_uri,
00069                                                 'base_url'      => $this->base_url,
00070                                                 'title'         => $this->current_table
00071                                         );
00072                 
00073                 $this->CI->load->vars($data);
00074                 
00075                 // Load the language file and create variables
00076                 $this->lang = $this->CI->load->scaffold_language('scaffolding', '', TRUE);
00077                 $this->CI->load->vars($this->lang);
00078                                 
00079                 //  Load the helper files we plan to use
00080                 $this->CI->load->helper(array('url', 'form'));
00081                 
00082                                 
00083                 log_message('debug', 'Scaffolding Class Initialized');
00084         }
00085         
00086         // --------------------------------------------------------------------
00087         
00088         /**
00089          * "Add" Page
00090          *
00091          * Shows a form representing the currently selected DB
00092          * so that data can be inserted
00093          *
00094          * @access      public
00095          * @return      string  the HTML "add" page
00096          */
00097         function add()
00098         {       
00099                 $data = array(
00100                                                 'title' =>  ( ! isset($this->lang['scaff_add'])) ? 'Add Data' : $this->lang['scaff_add'],
00101                                                 'fields' => $this->CI->db->field_data($this->current_table),
00102                                                 'action' => $this->base_uri.'/insert'
00103                                         );
00104         
00105                 $this->CI->load->view('add', $data);
00106         }
00107         
00108         // --------------------------------------------------------------------
00109         
00110         /**
00111          * Insert the data
00112          *
00113          * @access      public
00114          * @return      void    redirects to the view page
00115          */
00116         function insert()
00117         {               
00118                 if ($this->CI->db->insert($this->current_table, $_POST) === FALSE)
00119                 {
00120                         $this->add();
00121                 }
00122                 else
00123                 {
00124                         redirect($this->base_uri.'/view/');
00125                 }
00126         }
00127         
00128         // --------------------------------------------------------------------
00129         
00130         /**
00131          * "View" Page
00132          *
00133          * Shows a table containing the data in the currently
00134          * selected DB
00135          *
00136          * @access      public
00137          * @return      string  the HTML "view" page
00138          */
00139         function view()
00140         {
00141                 // Fetch the total number of DB rows
00142                 $total_rows = $this->CI->db->count_all($this->current_table);
00143                 
00144                 if ($total_rows < 1)
00145                 {
00146                         return $this->CI->load->view('no_data');
00147                 }
00148                 
00149                 // Set the query limit/offset
00150                 $per_page = 20;
00151                 $offset = $this->CI->uri->segment(4, 0);
00152                 
00153                 // Run the query
00154                 $query = $this->CI->db->get($this->current_table, $per_page, $offset);
00155 
00156                 // Now let's get the field names                                
00157                 $fields = $this->CI->db->list_fields($this->current_table);
00158                 
00159                 // We assume that the column in the first position is the primary field.
00160                 $primary = current($fields);
00161 
00162                 // Pagination!
00163                 $this->CI->pagination->initialize(
00164                                                         array(
00165                                                                         'base_url'               => $this->base_url.'/view',
00166                                                                         'total_rows'     => $total_rows,
00167                                                                         'per_page'               => $per_page,
00168                                                                         'uri_segment'    => 4,
00169                                                                         'full_tag_open'  => '<p>',
00170                                                                         'full_tag_close' => '</p>'
00171                                                                         )
00172                                                                 );      
00173 
00174                 $data = array(
00175                                                 'title' =>  ( ! isset($this->lang['scaff_view'])) ? 'View Data' : $this->lang['scaff_view'],
00176                                                 'query'         => $query,
00177                                                 'fields'        => $fields,
00178                                                 'primary'       => $primary,
00179                                                 'paginate'      => $this->CI->pagination->create_links()
00180                                         );
00181                                                 
00182                 $this->CI->load->view('view', $data);
00183         }
00184         
00185         // --------------------------------------------------------------------
00186         
00187         /**
00188          * "Edit" Page
00189          *
00190          * Shows a form representing the currently selected DB
00191          * so that data can be edited
00192          *
00193          * @access      public
00194          * @return      string  the HTML "edit" page
00195          */
00196         function edit()
00197         {
00198                 if (FALSE === ($id = $this->CI->uri->segment(4)))
00199                 {
00200                         return $this->view();
00201                 }
00202 
00203                 // Fetch the primary field name
00204                 $primary = $this->CI->db->primary($this->current_table);                                
00205 
00206                 // Run the query
00207                 $query = $this->CI->db->get_where($this->current_table, array($primary => $id));
00208 
00209                 $data = array(
00210                                                 'title' =>  ( ! isset($this->lang['scaff_edit'])) ? 'Edit Data' : $this->lang['scaff_edit'],
00211                                                 'fields'        => $query->field_data(),
00212                                                 'query'         => $query->row(),
00213                                                 'action'        => $this->base_uri.'/update/'.$this->CI->uri->segment(4)
00214                                         );
00215         
00216                 $this->CI->load->view('edit', $data);
00217         }
00218         
00219         // --------------------------------------------------------------------
00220         
00221         /**
00222          * Update
00223          *
00224          * @access      public
00225          * @return      void    redirects to the view page
00226          */
00227         function update()
00228         {       
00229                 // Fetch the primary key
00230                 $primary = $this->CI->db->primary($this->current_table);                                
00231 
00232                 // Now do the query
00233                 $this->CI->db->update($this->current_table, $_POST, array($primary => $this->CI->uri->segment(4)));
00234                 
00235                 redirect($this->base_uri.'/view/');
00236         }
00237         
00238         // --------------------------------------------------------------------
00239         
00240         /**
00241          * Delete Confirmation
00242          *
00243          * @access      public
00244          * @return      string  the HTML "delete confirm" page
00245          */
00246         function delete()
00247         {
00248                 if ( ! isset($this->lang['scaff_del_confirm']))
00249                 {
00250                         $message = 'Are you sure you want to delete the following row: '.$this->CI->uri->segment(4);
00251                 }
00252                 else
00253                 {
00254                         $message = $this->lang['scaff_del_confirm'].' '.$this->CI->uri->segment(4);
00255                 }
00256                 
00257                 $data = array(
00258                                                 'title'         => ( ! isset($this->lang['scaff_delete'])) ? 'Delete Data' : $this->lang['scaff_delete'],
00259                                                 'message'       => $message,
00260                                                 'no'            => anchor(array($this->base_uri, 'view'), ( ! isset($this->lang['scaff_no'])) ? 'No' : $this->lang['scaff_no']),
00261                                                 'yes'           => anchor(array($this->base_uri, 'do_delete', $this->CI->uri->segment(4)), ( ! isset($this->lang['scaff_yes'])) ? 'Yes' : $this->lang['scaff_yes'])
00262                                         );
00263         
00264                 $this->CI->load->view('delete', $data);
00265         }
00266         
00267         // --------------------------------------------------------------------
00268         
00269         /**
00270          * Delete
00271          *
00272          * @access      public
00273          * @return      void    redirects to the view page
00274          */
00275         function do_delete()
00276         {               
00277                 // Fetch the primary key
00278                 $primary = $this->CI->db->primary($this->current_table);                                
00279 
00280                 // Now do the query
00281                 $this->CI->db->where($primary, $this->CI->uri->segment(4));
00282                 $this->CI->db->delete($this->current_table);
00283 
00284                 header("Refresh:0;url=".site_url(array($this->base_uri, 'view')));
00285                 exit;
00286         }
00287 
00288 }
00289 
00290 /* End of file Scaffolding.php */
00291 /* Location: ./system/scaffolding/Scaffolding.php */