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