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