CSV Upload and Populate Module
I am wondering how one might go about creating a simple fuel module that would just have a form that would upload a csv file and then populate a database table with the data from that file. I know how to parse a csv file in php but am sketchy on how to setup the upload form since fields on the module forms generally map to the database table fields. Also, am sketchy on how once I have the rows from the csv table in array how to loop over the data check that's the row doesn't already exist and then insert it if it doesn't. Any thoughts or resources on how to approach this? Input would be much appreciated! Thank you in advance!
Comments
http://www.getfuelcms.com/user_guide/modules/advanced
With regards to the saving of the data, a FUEL models "save()" method uses INSERT IGNORE and then an ON DUPLICATE KEY UPDATE the values which essentially is similar to checking if a record exists and if it does, update it, else insert a new one (found in fuel/application/core/MY_DB_mysql_driver.php).
//MUST RESET THE VALUE HERE $config['nav'] = array(); // site... Dashboard will always be there $config['nav']['site'] = array( 'dashboard' => lang('module_dashboard'), 'pages' => lang('module_pages'), 'blocks' => lang('module_blocks'), 'navigation' => lang('module_navigation'), 'assets' => lang('module_assets'), 'sitevariables' => lang('module_sitevariables') ); // YOUR NEW CUSTOM MODULE $config['nav']['my_custom_module'] = array(); // blog placeholder if it exists $config['nav']['blog'] = array(); // my modules... if set to auto, then it will automatically include all in MY_fuel_modules.php $config['nav']['modules'] = 'AUTO'; // tools $config['nav']['tools'] = array(); // manage $config['nav']['manage'] = array( 'users' => lang('module_users'), 'permissions' => lang('module_permissions'), 'manage/cache' => lang('module_manage_cache'), 'manage/activity' => lang('module_manage_activity') );
$blog_controllers = array('posts', 'comments', 'categories', 'links', 'users'); foreach($blog_controllers as $c) { $route[FUEL_ROUTE.'blog/'.$c] = FUEL_FOLDER.'/module'; $route[FUEL_ROUTE.'blog/'.$c.'/(.*)'] = FUEL_FOLDER.'/module/$1'; }
require_once(FUEL_PATH.'libraries/Fuel_base_controller.php'); class Members extends Fuel_base_controller { public $view_location = 'members'; function __construct() { parent::__construct(); $this->load->config('members'); $this->load->language('members'); $this->_validate_user('members'); } function _remap() { $vars = array(); $vars['type'] = ($this->uri->segment(3) !== FALSE) ? $this->uri->segment(3) : 'names'; if($this->input->post('upload')) { $this->_handle_upload($vars['type']); } $this->nav_selected = 'members/' . $vars['type']; $this->_render('members_upload', $vars); } function _handle_upload($type) { $config = array(); $config['allowed_types'] = 'csv'; $config['file_name'] = $type . '.csv'; $config['overwrite'] = TRUE; $config['upload_path'] = MEMBERS_PATH . 'assets/csv'; $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $this->session->set_flashdata('error', $this->upload->display_errors('', '')); redirect(fuel_uri('members/' . $type)); } else { $upload_data = $this->upload->data(); $this->load->module_library(MEMBERS_FOLDER, 'csv_reader'); $csv_rows = $this->csv_reader->parse_file($upload_data['full_path']); $model_variable = $type . '_model'; $this->load->module_model(MEMBERS_FOLDER, $model_variable); $this->$model_variable->truncate(); foreach($csv_rows as $csv_row) { $record = $this->$model_variable->create(); $record->fill($csv_row); if (!$record->save()) { $this->session->set_flashdata('error', lang('error_members_save')); redirect(fuel_uri('members/' . $type)); } } $this->session->set_flashdata('success', lang('success_members_save')); redirect(fuel_uri('members/' . $type)); } } } /* End of file members.php */ /* Location: ./fuel/modules/members/controllers/members.php */
With regards to the right side of the routes, if you create a 'simple' module, meaning one that is declared in you fuel/application/config/MY_fuel_modules.php file, then it gets routed to the generic FUEL 'module' controller. In the blogs case, each of those modules is a 'simple' sub module of the blog (essentially has a model) and thus we route it to the generic 'module' controller. The generic 'module' controller looks at the URI structure and grabs the correct initialization information from MY_fuel_modules.php to allow you to do the CRUD work for a module (or sub-module in this case). Does that make sense?
1. Controllers in an advanced module that extend CI_Controller get pulled up at the uri of the module folder name followed by / and the controller name unless the controller has the same name as the module folder then no controller name is needed.
2. The routes file for the module is for mapping the admin controller routes, front end routes happen by default. The default admin routes start with fuel/.
3. Simple modules can be used, all that is needed is a model and the routes to the module is mapped to the generic FUEL module like the blog routes file. If not using a simple module which I am not in this case set the route to point to that administrative controller.
Anyway, I add these in hopes they will help someone else. Basically what I have done now is moved my admin controller to another name different then the module folder name. I had made it the same before because of following the user guide and not grasping the above statements. Then I setup the routes in the route file for the admin to map to the changed controller name. Now I am creating my controllers for the front end of the web site starting with one that does have the same name as the directory. Anyway, seems like small revelations but they paramount for me. Thanks again for the help!
$config = array(); $config['location'] = 'members/change_password'; $config['render_mode'] = 'auto'; $this->fuel_page->initialize($config);
when attempting to render the pages. This worked fine for the members default controller it loaded the page as expected but it didn't work for members/change_password. I can only seem to get it to work if I make the page location change_password without members/ on the front it. Does this make sense and is it possible to do what I want to do?
1. Is the change_password view file located at fuel/application/views/members/change_password? Or are the view files in a different module's view folder (e.g. fuel/modules/members/views)?
2. What is showing up right now?... anything?
CHANGE:
$segments = $this->_CI->uri->rsegment_array();
TO:
if (!empty($this->location)) { $segs = explode('/', $this->location); $segments = array_combine(range(1, count($segs)), array_values($segs)); } else { $segments = $this->_CI->uri->rsegment_array(); }