blob: 4b6ebeed5e8cfca412a62366712dff0b03e90cc5 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Scaffolding Class
20 *
21 * Provides the Scaffolding framework
22 *
23 * @package CodeIgniter
24 * @subpackage Scaffolding
25 * @author Rick Ellis
26 * @link http://www.codeigniter.com/user_guide/general/scaffolding.html
27 */
28class Scaffolding {
29
30 var $current_table;
31 var $base_url = '';
32 var $lang = array();
33
34 function Scaffolding($db_table)
35 {
36 $obj =& get_instance();
37 foreach ($obj->ci_is_loaded as $val)
38 {
39 $this->$val =& $obj->$val;
40 }
41
42 /**
43 * Set the current table name
44 * This is done when initializing scaffolding:
45 * $this->_ci_init_scaffolding('table_name')
46 *
47 */
48 $this->current_table = $db_table;
49
50 /**
51 * Set the path to the "view" files
52 * We'll manually override the "view" path so that
53 * the load->view function knows where to look.
54 */
55 $this->load->_ci_set_view_path(BASEPATH.'scaffolding/views/');
56
57 // Set the base URL
58 $this->base_url = $this->config->site_url().'/'.$this->uri->segment(1).$this->uri->slash_segment(2, 'both');
59 $this->base_uri = $this->uri->segment(1).$this->uri->slash_segment(2, 'leading');
60
61 // Set a few globals
62 $data = array(
63 'image_url' => $this->config->system_url().'scaffolding/images/',
64 'base_uri' => $this->base_uri,
65 'base_url' => $this->base_url,
66 'title' => $this->current_table
67 );
68
69 $this->load->vars($data);
70
71 // Load the language file and create variables
72 $this->lang = $this->load->language('scaffolding', '', TRUE);
73 $this->load->vars($this->lang);
74
75 // Load the helper files we plan to use
76 $this->load->helper(array('url', 'form'));
77
78
79 log_message('debug', 'Scaffolding Class Initialized');
80 }
81
82 // --------------------------------------------------------------------
83
84 /**
85 * "Add" Page
86 *
87 * Shows a form representing the currently selected DB
88 * so that data can be inserted
89 *
90 * @access public
91 * @return string the HTML "add" page
92 */
93 function add()
94 {
95 $data = array(
96 'title' => ( ! isset($this->lang['scaff_add'])) ? 'Add Data' : $this->lang['scaff_add'],
97 'fields' => $this->db->field_data($this->current_table),
98 'action' => $this->base_uri.'/insert'
99 );
100
101 $this->load->view('add', $data);
102 }
103
104 // --------------------------------------------------------------------
105
106 /**
107 * Insert the data
108 *
109 * @access public
110 * @return void redirects to the view page
111 */
112 function insert()
113 {
114 if ($this->db->insert($this->current_table, $_POST) === FALSE)
115 {
116 $this->add();
117 }
118 else
119 {
120 redirect($this->base_uri.'/view/');
121 }
122 }
123
124 // --------------------------------------------------------------------
125
126 /**
127 * "View" Page
128 *
129 * Shows a table containing the data in the currently
130 * selected DB
131 *
132 * @access public
133 * @return string the HTML "view" page
134 */
135 function view()
136 {
137 // Fetch the total number of DB rows
138 $total_rows = $this->db->count_all($this->current_table);
139
140 if ($total_rows < 1)
141 {
142 return $this->load->view('no_data');
143 }
144
145 // Set the query limit/offset
146 $per_page = 20;
147 $offset = $this->uri->segment(4, 0);
148
149 // Run the query
150 $query = $this->db->get($this->current_table, $per_page, $offset);
151
152 // Now let's get the field names
153 $fields = $this->db->field_names($this->current_table);
154
155 // We assume that the column in the first position is the primary field.
156 $primary = current($fields);
157
158 // Pagination!
159 $this->pagination->initialize(
160 array(
161 'base_url' => $this->base_url.'/view',
162 'total_rows' => $total_rows,
163 'per_page' => $per_page,
164 'uri_segment' => 4,
165 'full_tag_open' => '<p>',
166 'full_tag_close' => '</p>'
167 )
168 );
169
170 $data = array(
171 'title' => ( ! isset($this->lang['scaff_view'])) ? 'View Data' : $this->lang['scaff_view'],
172 'query' => $query,
173 'fields' => $fields,
174 'primary' => $primary,
175 'paginate' => $this->pagination->create_links()
176 );
177
178 $this->load->view('view', $data);
179 }
180
181 // --------------------------------------------------------------------
182
183 /**
184 * "Edit" Page
185 *
186 * Shows a form representing the currently selected DB
187 * so that data can be edited
188 *
189 * @access public
190 * @return string the HTML "edit" page
191 */
192 function edit()
193 {
194 if (FALSE === ($id = $this->uri->segment(4)))
195 {
196 return $this->view();
197 }
198
199 // Fetch the primary field name
200 $primary = $this->db->primary($this->current_table);
201
202 // Run the query
203 $query = $this->db->getwhere($this->current_table, array($primary => $id));
204
205 $data = array(
206 'title' => ( ! isset($this->lang['scaff_edit'])) ? 'Edit Data' : $this->lang['scaff_edit'],
207 'fields' => $query->field_data(),
208 'query' => $query->row(),
209 'action' => $this->base_uri.'/update/'.$this->uri->segment(4)
210 );
211
212 $this->load->view('edit', $data);
213 }
214
215 // --------------------------------------------------------------------
216
217 /**
218 * Update
219 *
220 * @access public
221 * @return void redirects to the view page
222 */
223 function update()
224 {
225 // Fetch the primary key
226 $primary = $this->db->primary($this->current_table);
227
228 // Now do the query
229 $this->db->update($this->current_table, $_POST, array($primary => $this->uri->segment(4)));
230
231 redirect($this->base_uri.'/view/');
232 }
233
234 // --------------------------------------------------------------------
235
236 /**
237 * Delete Confirmation
238 *
239 * @access public
240 * @return string the HTML "delete confirm" page
241 */
242 function delete()
243 {
244 if ( ! isset($this->lang['scaff_del_confirm']))
245 {
246 $message = 'Are you sure you want to delete the following row: '.$this->uri->segment(4);
247 }
248 else
249 {
250 $message = $this->lang['scaff_del_confirm'].' '.$this->uri->segment(4);
251 }
252
253 $data = array(
254 'title' => ( ! isset($this->lang['scaff_delete'])) ? 'Delete Data' : $this->lang['scaff_delete'],
255 'message' => $message,
256 'no' => anchor(array($this->base_uri, 'view'), ( ! isset($this->lang['scaff_no'])) ? 'No' : $this->lang['scaff_no']),
257 'yes' => anchor(array($this->base_uri, 'do_delete', $this->uri->segment(4)), ( ! isset($this->lang['scaff_yes'])) ? 'Yes' : $this->lang['scaff_yes'])
258 );
259
260 $this->load->view('delete', $data);
261 }
262
263 // --------------------------------------------------------------------
264
265 /**
266 * Delete
267 *
268 * @access public
269 * @return void redirects to the view page
270 */
271 function do_delete()
272 {
273 // Fetch the primary key
274 $primary = $this->db->primary($this->current_table);
275
276 // Now do the query
277 $this->db->where($primary, $this->uri->segment(4));
278 $this->db->delete($this->current_table);
279
280 header("Refresh:0;url=".site_url(array($this->base_uri, 'view')));
281 exit;
282 }
283
284}
285?>