admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <?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. |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 10 | * @license http://www.codeignitor.com/user_guide/license.html |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 11 | * @link http://www.codeigniter.com |
| 12 | * @since Version 1.0 |
| 13 | * @filesource |
| 14 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 16 | // ------------------------------------------------------------------------ |
| 17 | |
| 18 | /** |
| 19 | * Scaffolding Class |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 20 | * |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | */ |
| 28 | class Scaffolding { |
| 29 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 30 | var $CI; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 31 | var $current_table; |
| 32 | var $base_url = ''; |
| 33 | var $lang = array(); |
| 34 | |
| 35 | function Scaffolding($db_table) |
| 36 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 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(); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Set the current table name |
| 47 | * This is done when initializing scaffolding: |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 48 | * $this->load->scaffolding('table_name') |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | */ |
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 58 | |
admin | cf49390 | 2006-10-10 07:12:31 +0000 | [diff] [blame] | 59 | $this->CI->load->_ci_view_path = BASEPATH.'scaffolding/views/'; |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 60 | |
| 61 | // Set the base URL |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 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'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 64 | |
| 65 | // Set a few globals |
| 66 | $data = array( |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 67 | 'image_url' => $this->CI->config->system_url().'scaffolding/images/', |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 68 | 'base_uri' => $this->base_uri, |
| 69 | 'base_url' => $this->base_url, |
| 70 | 'title' => $this->current_table |
| 71 | ); |
| 72 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 73 | $this->CI->load->vars($data); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 74 | |
| 75 | // Load the language file and create variables |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 76 | $this->lang = $this->CI->load->language('scaffolding', '', TRUE); |
| 77 | $this->CI->load->vars($this->lang); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 78 | |
| 79 | // Load the helper files we plan to use |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 80 | $this->CI->load->helper(array('url', 'form')); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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'], |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 101 | 'fields' => $this->CI->db->field_data($this->current_table), |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 102 | 'action' => $this->base_uri.'/insert' |
| 103 | ); |
| 104 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 105 | $this->CI->load->view('add', $data); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 118 | if ($this->CI->db->insert($this->current_table, $_POST) === FALSE) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 142 | $total_rows = $this->CI->db->count_all($this->current_table); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 143 | |
| 144 | if ($total_rows < 1) |
| 145 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 146 | return $this->CI->load->view('no_data'); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // Set the query limit/offset |
| 150 | $per_page = 20; |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 151 | $offset = $this->CI->uri->segment(4, 0); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 152 | |
| 153 | // Run the query |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 154 | $query = $this->CI->db->get($this->current_table, $per_page, $offset); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 155 | |
| 156 | // Now let's get the field names |
admin | b6224a1 | 2006-10-23 01:25:46 +0000 | [diff] [blame] | 157 | $fields = $this->CI->db->list_fields($this->current_table); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 158 | |
| 159 | // We assume that the column in the first position is the primary field. |
| 160 | $primary = current($fields); |
| 161 | |
| 162 | // Pagination! |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 163 | $this->CI->pagination->initialize( |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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, |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 179 | 'paginate' => $this->CI->pagination->create_links() |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 180 | ); |
| 181 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 182 | $this->CI->load->view('view', $data); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 198 | if (FALSE === ($id = $this->CI->uri->segment(4))) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 199 | { |
| 200 | return $this->view(); |
| 201 | } |
| 202 | |
| 203 | // Fetch the primary field name |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 204 | $primary = $this->CI->db->primary($this->current_table); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 205 | |
| 206 | // Run the query |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 207 | $query = $this->CI->db->getwhere($this->current_table, array($primary => $id)); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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(), |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 213 | 'action' => $this->base_uri.'/update/'.$this->CI->uri->segment(4) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 214 | ); |
| 215 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 216 | $this->CI->load->view('edit', $data); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 230 | $primary = $this->CI->db->primary($this->current_table); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 231 | |
| 232 | // Now do the query |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 233 | $this->CI->db->update($this->current_table, $_POST, array($primary => $this->CI->uri->segment(4))); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 250 | $message = 'Are you sure you want to delete the following row: '.$this->CI->uri->segment(4); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 251 | } |
| 252 | else |
| 253 | { |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 254 | $message = $this->lang['scaff_del_confirm'].' '.$this->CI->uri->segment(4); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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']), |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 261 | 'yes' => anchor(array($this->base_uri, 'do_delete', $this->CI->uri->segment(4)), ( ! isset($this->lang['scaff_yes'])) ? 'Yes' : $this->lang['scaff_yes']) |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 262 | ); |
| 263 | |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 264 | $this->CI->load->view('delete', $data); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 278 | $primary = $this->CI->db->primary($this->current_table); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 279 | |
| 280 | // Now do the query |
admin | b3ab70b | 2006-10-07 03:07:29 +0000 | [diff] [blame] | 281 | $this->CI->db->where($primary, $this->CI->uri->segment(4)); |
| 282 | $this->CI->db->delete($this->current_table); |
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 283 | |
| 284 | header("Refresh:0;url=".site_url(array($this->base_uri, 'view'))); |
| 285 | exit; |
| 286 | } |
| 287 | |
| 288 | } |
| 289 | ?> |