1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
21 * Provides the Scaffolding framework
23 * @package CodeIgniter
24 * @subpackage Scaffolding
25 * @author ExpressionEngine Dev Team
26 * @link http://codeigniter.com/user_guide/general/scaffolding.html
35 function Scaffolding($db_table)
37 $this->CI =& get_instance();
39 $this->CI->load->database("", FALSE, TRUE);
40 $this->CI->load->library('pagination');
43 $this->CI->db->cache_off();
46 * Set the current table name
47 * This is done when initializing scaffolding:
48 * $this->load->scaffolding('table_name')
51 $this->current_table = $db_table;
54 * Set the path to the "view" files
55 * We'll manually override the "view" path so that
56 * the load->view function knows where to look.
59 $this->CI->load->_ci_view_path = BASEPATH.'scaffolding/views/';
62 $this->base_url = $this->CI->config->site_url().'/'.$this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'both');
63 $this->base_uri = $this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'leading');
67 'image_url' => $this->CI->config->system_url().'scaffolding/images/',
68 'base_uri' => $this->base_uri,
69 'base_url' => $this->base_url,
70 'title' => $this->current_table
73 $this->CI->load->vars($data);
75 // Load the language file and create variables
76 $this->lang = $this->CI->load->scaffold_language('scaffolding', '', TRUE);
77 $this->CI->load->vars($this->lang);
79 // Load the helper files we plan to use
80 $this->CI->load->helper(array('url', 'form'));
83 log_message('debug', 'Scaffolding Class Initialized');
86 // --------------------------------------------------------------------
91 * Shows a form representing the currently selected DB
92 * so that data can be inserted
95 * @return string the HTML "add" page
100 'title' => ( ! isset($this->lang['scaff_add'])) ? 'Add Data' : $this->lang['scaff_add'],
101 'fields' => $this->CI->db->field_data($this->current_table),
102 'action' => $this->base_uri.'/insert'
105 $this->CI->load->view('add', $data);
108 // --------------------------------------------------------------------
114 * @return void redirects to the view page
118 if ($this->CI->db->insert($this->current_table, $_POST) === FALSE)
124 redirect($this->base_uri.'/view/');
128 // --------------------------------------------------------------------
133 * Shows a table containing the data in the currently
137 * @return string the HTML "view" page
141 // Fetch the total number of DB rows
142 $total_rows = $this->CI->db->count_all($this->current_table);
146 return $this->CI->load->view('no_data');
149 // Set the query limit/offset
151 $offset = $this->CI->uri->segment(4, 0);
154 $query = $this->CI->db->get($this->current_table, $per_page, $offset);
156 // Now let's get the field names
157 $fields = $this->CI->db->list_fields($this->current_table);
159 // We assume that the column in the first position is the primary field.
160 $primary = current($fields);
163 $this->CI->pagination->initialize(
165 'base_url' => $this->base_url.'/view',
166 'total_rows' => $total_rows,
167 'per_page' => $per_page,
169 'full_tag_open' => '<p>',
170 'full_tag_close' => '</p>'
175 'title' => ( ! isset($this->lang['scaff_view'])) ? 'View Data' : $this->lang['scaff_view'],
178 'primary' => $primary,
179 'paginate' => $this->CI->pagination->create_links()
182 $this->CI->load->view('view', $data);
185 // --------------------------------------------------------------------
190 * Shows a form representing the currently selected DB
191 * so that data can be edited
194 * @return string the HTML "edit" page
198 if (FALSE === ($id = $this->CI->uri->segment(4)))
200 return $this->view();
203 // Fetch the primary field name
204 $primary = $this->CI->db->primary($this->current_table);
207 $query = $this->CI->db->get_where($this->current_table, array($primary => $id));
210 'title' => ( ! isset($this->lang['scaff_edit'])) ? 'Edit Data' : $this->lang['scaff_edit'],
211 'fields' => $query->field_data(),
212 'query' => $query->row(),
213 'action' => $this->base_uri.'/update/'.$this->CI->uri->segment(4)
216 $this->CI->load->view('edit', $data);
219 // --------------------------------------------------------------------
225 * @return void redirects to the view page
229 // Fetch the primary key
230 $primary = $this->CI->db->primary($this->current_table);
233 $this->CI->db->update($this->current_table, $_POST, array($primary => $this->CI->uri->segment(4)));
235 redirect($this->base_uri.'/view/');
238 // --------------------------------------------------------------------
241 * Delete Confirmation
244 * @return string the HTML "delete confirm" page
248 if ( ! isset($this->lang['scaff_del_confirm']))
250 $message = 'Are you sure you want to delete the following row: '.$this->CI->uri->segment(4);
254 $message = $this->lang['scaff_del_confirm'].' '.$this->CI->uri->segment(4);
258 'title' => ( ! isset($this->lang['scaff_delete'])) ? 'Delete Data' : $this->lang['scaff_delete'],
259 'message' => $message,
260 'no' => anchor(array($this->base_uri, 'view'), ( ! isset($this->lang['scaff_no'])) ? 'No' : $this->lang['scaff_no']),
261 'yes' => anchor(array($this->base_uri, 'do_delete', $this->CI->uri->segment(4)), ( ! isset($this->lang['scaff_yes'])) ? 'Yes' : $this->lang['scaff_yes'])
264 $this->CI->load->view('delete', $data);
267 // --------------------------------------------------------------------
273 * @return void redirects to the view page
277 // Fetch the primary key
278 $primary = $this->CI->db->primary($this->current_table);
281 $this->CI->db->where($primary, $this->CI->uri->segment(4));
282 $this->CI->db->delete($this->current_table);
284 header("Refresh:0;url=".site_url(array($this->base_uri, 'view')));
290 /* End of file Scaffolding.php */
291 /* Location: ./system/scaffolding/Scaffolding.php */