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