00001 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Scaffolding {
00029
00030 var $CI;
00031 var $current_table;
00032 var $base_url = '';
00033 var $lang = array();
00034
00035 function Scaffolding($db_table)
00036 {
00037 $this->CI =& get_instance();
00038
00039 $this->CI->load->database("", FALSE, TRUE);
00040 $this->CI->load->library('pagination');
00041
00042
00043 $this->CI->db->cache_off();
00044
00045
00046
00047
00048
00049
00050
00051 $this->current_table = $db_table;
00052
00053
00054
00055
00056
00057
00058
00059 $this->CI->load->_ci_view_path = BASEPATH.'scaffolding/views/';
00060
00061
00062 $this->base_url = $this->CI->config->site_url().'/'.$this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'both');
00063 $this->base_uri = $this->CI->uri->segment(1).$this->CI->uri->slash_segment(2, 'leading');
00064
00065
00066 $data = array(
00067 'image_url' => $this->CI->config->system_url().'scaffolding/images/',
00068 'base_uri' => $this->base_uri,
00069 'base_url' => $this->base_url,
00070 'title' => $this->current_table
00071 );
00072
00073 $this->CI->load->vars($data);
00074
00075
00076 $this->lang = $this->CI->load->scaffold_language('scaffolding', '', TRUE);
00077 $this->CI->load->vars($this->lang);
00078
00079
00080 $this->CI->load->helper(array('url', 'form'));
00081
00082
00083 log_message('debug', 'Scaffolding Class Initialized');
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097 function add()
00098 {
00099 $data = array(
00100 'title' => ( ! isset($this->lang['scaff_add'])) ? 'Add Data' : $this->lang['scaff_add'],
00101 'fields' => $this->CI->db->field_data($this->current_table),
00102 'action' => $this->base_uri.'/insert'
00103 );
00104
00105 $this->CI->load->view('add', $data);
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 function insert()
00117 {
00118 if ($this->CI->db->insert($this->current_table, $_POST) === FALSE)
00119 {
00120 $this->add();
00121 }
00122 else
00123 {
00124 redirect($this->base_uri.'/view/');
00125 }
00126 }
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 function view()
00140 {
00141
00142 $total_rows = $this->CI->db->count_all($this->current_table);
00143
00144 if ($total_rows < 1)
00145 {
00146 return $this->CI->load->view('no_data');
00147 }
00148
00149
00150 $per_page = 20;
00151 $offset = $this->CI->uri->segment(4, 0);
00152
00153
00154 $query = $this->CI->db->get($this->current_table, $per_page, $offset);
00155
00156
00157 $fields = $this->CI->db->list_fields($this->current_table);
00158
00159
00160 $primary = current($fields);
00161
00162
00163 $this->CI->pagination->initialize(
00164 array(
00165 'base_url' => $this->base_url.'/view',
00166 'total_rows' => $total_rows,
00167 'per_page' => $per_page,
00168 'uri_segment' => 4,
00169 'full_tag_open' => '<p>',
00170 'full_tag_close' => '</p>'
00171 )
00172 );
00173
00174 $data = array(
00175 'title' => ( ! isset($this->lang['scaff_view'])) ? 'View Data' : $this->lang['scaff_view'],
00176 'query' => $query,
00177 'fields' => $fields,
00178 'primary' => $primary,
00179 'paginate' => $this->CI->pagination->create_links()
00180 );
00181
00182 $this->CI->load->view('view', $data);
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 function edit()
00197 {
00198 if (FALSE === ($id = $this->CI->uri->segment(4)))
00199 {
00200 return $this->view();
00201 }
00202
00203
00204 $primary = $this->CI->db->primary($this->current_table);
00205
00206
00207 $query = $this->CI->db->get_where($this->current_table, array($primary => $id));
00208
00209 $data = array(
00210 'title' => ( ! isset($this->lang['scaff_edit'])) ? 'Edit Data' : $this->lang['scaff_edit'],
00211 'fields' => $query->field_data(),
00212 'query' => $query->row(),
00213 'action' => $this->base_uri.'/update/'.$this->CI->uri->segment(4)
00214 );
00215
00216 $this->CI->load->view('edit', $data);
00217 }
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227 function update()
00228 {
00229
00230 $primary = $this->CI->db->primary($this->current_table);
00231
00232
00233 $this->CI->db->update($this->current_table, $_POST, array($primary => $this->CI->uri->segment(4)));
00234
00235 redirect($this->base_uri.'/view/');
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 function delete()
00247 {
00248 if ( ! isset($this->lang['scaff_del_confirm']))
00249 {
00250 $message = 'Are you sure you want to delete the following row: '.$this->CI->uri->segment(4);
00251 }
00252 else
00253 {
00254 $message = $this->lang['scaff_del_confirm'].' '.$this->CI->uri->segment(4);
00255 }
00256
00257 $data = array(
00258 'title' => ( ! isset($this->lang['scaff_delete'])) ? 'Delete Data' : $this->lang['scaff_delete'],
00259 'message' => $message,
00260 'no' => anchor(array($this->base_uri, 'view'), ( ! isset($this->lang['scaff_no'])) ? 'No' : $this->lang['scaff_no']),
00261 'yes' => anchor(array($this->base_uri, 'do_delete', $this->CI->uri->segment(4)), ( ! isset($this->lang['scaff_yes'])) ? 'Yes' : $this->lang['scaff_yes'])
00262 );
00263
00264 $this->CI->load->view('delete', $data);
00265 }
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 function do_delete()
00276 {
00277
00278 $primary = $this->CI->db->primary($this->current_table);
00279
00280
00281 $this->CI->db->where($primary, $this->CI->uri->segment(4));
00282 $this->CI->db->delete($this->current_table);
00283
00284 header("Refresh:0;url=".site_url(array($this->base_uri, 'view')));
00285 exit;
00286 }
00287
00288 }
00289
00290
00291