Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2005 Aurelien 1
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP 4.3.2 or newer
6
 *
7
 * @package		CodeIgniter
8
 * @author		ExpressionEngine Dev Team
9
 * @copyright	Copyright (c) 2008, EllisLab, Inc.
10
 * @license		http://codeigniter.com/user_guide/license.html
11
 * @link		http://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		ExpressionEngine Dev Team
26
 * @link		http://codeigniter.com/user_guide/general/scaffolding.html
27
 */
28
class Scaffolding {
29
30
	var $CI;
31
	var $current_table;
32
	var $base_url = '';
33
	var $lang = array();
34
35
	function Scaffolding($db_table)
36
	{
37
		$this->CI =& get_instance();
38
39
		$this->CI->load->database("", FALSE, TRUE);
40
		$this->CI->load->library('pagination');
41
42
		// Turn off caching
43
		$this->CI->db->cache_off();
44
45
		/**
46
		 * Set the current table name
47
		 * This is done when initializing scaffolding:
48
		 * $this->load->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
59
		$this->CI->load->_ci_view_path = BASEPATH.'scaffolding/views/';
60
61
		// Set the base URL
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');
64
65
		// Set a few globals
66
		$data = array(
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
71
					);
72
73
		$this->CI->load->vars($data);
74
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);
78
79
		//  Load the helper files we plan to use
80
		$this->CI->load->helper(array('url', 'form'));
81
82
83
		log_message('debug', 'Scaffolding Class Initialized');
84
	}
85
86
	// --------------------------------------------------------------------
87
88
	/**
89
	 * "Add" Page
90
	 *
91
	 * Shows a form representing the currently selected DB
92
	 * so that data can be inserted
93
	 *
94
	 * @access	public
95
	 * @return	string	the HTML "add" page
96
	 */
97
	function add()
98
	{
99
		$data = array(
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'
103
					);
104
105
		$this->CI->load->view('add', $data);
106
	}
107
108
	// --------------------------------------------------------------------
109
110
	/**
111
	 * Insert the data
112
	 *
113
	 * @access	public
114
	 * @return	void	redirects to the view page
115
	 */
116
	function insert()
117
	{
118
		if ($this->CI->db->insert($this->current_table, $_POST) === FALSE)
119
		{
120
			$this->add();
121
		}
122
		else
123
		{
124
			redirect($this->base_uri.'/view/');
125
		}
126
	}
127
128
	// --------------------------------------------------------------------
129
130
	/**
131
	 * "View" Page
132
	 *
133
	 * Shows a table containing the data in the currently
134
	 * selected DB
135
	 *
136
	 * @access	public
137
	 * @return	string	the HTML "view" page
138
	 */
139
	function view()
140
	{
141
		// Fetch the total number of DB rows
142
		$total_rows = $this->CI->db->count_all($this->current_table);
143
144
		if ($total_rows < 1)
145
		{
146
			return $this->CI->load->view('no_data');
147
		}
148
149
		// Set the query limit/offset
150
		$per_page = 20;
151
		$offset = $this->CI->uri->segment(4, 0);
152
153
		// Run the query
154
		$query = $this->CI->db->get($this->current_table, $per_page, $offset);
155
156
		// Now let's get the field names
157
		$fields = $this->CI->db->list_fields($this->current_table);
158
159
		// We assume that the column in the first position is the primary field.
160
		$primary = current($fields);
161
162
		// Pagination!
163
		$this->CI->pagination->initialize(
164
							array(
165
									'base_url'		 => $this->base_url.'/view',
166
									'total_rows'	 => $total_rows,
167
									'per_page'		 => $per_page,
168
									'uri_segment'	 => 4,
169
									'full_tag_open'	 => '<p>',
170
									'full_tag_close' => '</p>'
171
									)
172
								);
173
174
		$data = array(
175
						'title'	=>  ( ! isset($this->lang['scaff_view'])) ? 'View Data' : $this->lang['scaff_view'],
176
						'query'		=> $query,
177
						'fields'	=> $fields,
178
						'primary'	=> $primary,
179
						'paginate'	=> $this->CI->pagination->create_links()
180
					);
181
182
		$this->CI->load->view('view', $data);
183
	}
184
185
	// --------------------------------------------------------------------
186
187
	/**
188
	 * "Edit" Page
189
	 *
190
	 * Shows a form representing the currently selected DB
191
	 * so that data can be edited
192
	 *
193
	 * @access	public
194
	 * @return	string	the HTML "edit" page
195
	 */
196
	function edit()
197
	{
198
		if (FALSE === ($id = $this->CI->uri->segment(4)))
199
		{
200
			return $this->view();
201
		}
202
203
		// Fetch the primary field name
204
		$primary = $this->CI->db->primary($this->current_table);
205
206
		// Run the query
207
		$query = $this->CI->db->get_where($this->current_table, array($primary => $id));
208
209
		$data = array(
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)
214
					);
215
216
		$this->CI->load->view('edit', $data);
217
	}
218
219
	// --------------------------------------------------------------------
220
221
	/**
222
	 * Update
223
	 *
224
	 * @access	public
225
	 * @return	void	redirects to the view page
226
	 */
227
	function update()
228
	{
229
		// Fetch the primary key
230
		$primary = $this->CI->db->primary($this->current_table);
231
232
		// Now do the query
233
		$this->CI->db->update($this->current_table, $_POST, array($primary => $this->CI->uri->segment(4)));
234
235
		redirect($this->base_uri.'/view/');
236
	}
237
238
	// --------------------------------------------------------------------
239
240
	/**
241
	 * Delete Confirmation
242
	 *
243
	 * @access	public
244
	 * @return	string	the HTML "delete confirm" page
245
	 */
246
	function delete()
247
	{
248
		if ( ! isset($this->lang['scaff_del_confirm']))
249
		{
250
			$message = 'Are you sure you want to delete the following row: '.$this->CI->uri->segment(4);
251
		}
252
		else
253
		{
254
			$message = $this->lang['scaff_del_confirm'].' '.$this->CI->uri->segment(4);
255
		}
256
257
		$data = array(
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'])
262
					);
263
264
		$this->CI->load->view('delete', $data);
265
	}
266
267
	// --------------------------------------------------------------------
268
269
	/**
270
	 * Delete
271
	 *
272
	 * @access	public
273
	 * @return	void	redirects to the view page
274
	 */
275
	function do_delete()
276
	{
277
		// Fetch the primary key
278
		$primary = $this->CI->db->primary($this->current_table);
279
280
		// Now do the query
281
		$this->CI->db->where($primary, $this->CI->uri->segment(4));
282
		$this->CI->db->delete($this->current_table);
283
284
		header("Refresh:0;url=".site_url(array($this->base_uri, 'view')));
285
		exit;
286
	}
287
288
}
289
290
/* End of file Scaffolding.php */
291
/* Location: ./system/scaffolding/Scaffolding.php */