adrian Posted October 10, 2014 Share Posted October 10, 2014 This module adds CSV import and export functionality to Profields Table fields on both the admin and front-end. http://modules.processwire.com/modules/table-csv-import-export/https://github.com/adrianbj/TableCsvImportExport Access to the admin import/export for non-superusers is controlled by two automatically created permissions: table-csv-import and table-csv-export Another permission (table-csv-import-overwrite) allows you to control access to the overwrite option when importing. The overwrite option is also controlled at the field level. Go to the table field's Input tab and check the new "Allow overwrite option" if you want this enabled at all for the specific field. Please consider limiting import overwrite option to trusted roles as you could do a lot of damage very quickly with the overwrite option Front-end export of a table field to CSV can be achieved with the exportCsv() method: // export as CSV if csv_export=1 is in url if($input->get->csv_export==1){ $modules->get('ProcessTableCsvExport'); // load module // delimiter, enclosure, file extension, multiple fields separator, names in first row $page->fields->tablefield->exportCsv('tab', '"', 'tsv', '|', true); } // display content of template with link to same page with appended csv_export=1 else{ include("./head.inc"); echo $page->tablefield->render(); //render table - not necessary for export - just displaying the table echo "<a href='./?csv_export=1'>Export Table as CSV</a>"; //link to initiate export include("./foot.inc"); } Front-end import can be achieved with the importCsv() method: $modules->get('TableCsvImportExport'); // load module // data, delimiter, enclosure, convert decimals, ignore first row, multiple fields separator, append or overwrite $page->fields->tablefield->importCsv($csvData, ';', '"', true, false, '|', 'append'); Please let me know if you have any problems, or suggestions for improvements. Enjoy! 21 1 Link to comment Share on other sites More sharing options...
Martijn Geerts Posted October 10, 2014 Share Posted October 10, 2014 I love what you've done. I could imagine you can add an extra data cleaner for the importer with the help of json_encode. Forget this, to much much my own module in my mind. 2 Link to comment Share on other sites More sharing options...
adrian Posted October 10, 2014 Author Share Posted October 10, 2014 I was confused by that which is why I hadn't commented yet - I was trying to let it sink in to see if I could get what you meant I should probably do a little more cleaning of the CSV for trailing commas, extra spaces, and the like - it's on my list. Link to comment Share on other sites More sharing options...
adrian Posted October 11, 2014 Author Share Posted October 11, 2014 Small update committed for finer control over the overwrite option when importing. The new table-csv-import-overwrite permission allows you to control access to the overwrite option when importing. If a user doesn't have this permission, they won't have the option to choose overwrite. The overwrite option is also controlled at the field level. Go to the table field's Input tab and check the new "Allow overwrite option" if you want this enabled at all for the specific field. So if overwrite is not enabled for each particular table field, it doesn't matter what permission the user has, no-one will have access to it. 2 Link to comment Share on other sites More sharing options...
Marty Walker Posted December 18, 2014 Share Posted December 18, 2014 Thanks very much for this Adrian. This has saved me hours of work today. 1 Link to comment Share on other sites More sharing options...
RyanJ Posted January 30, 2015 Share Posted January 30, 2015 Awesome module Adrian thanks for sharing. I am getting some strange behavior though. I have two different table field types assigned to one template. When I try uploading a csv I get the following error message. "That is not an allowed file extension for a CSV import. Try again with a .csv, .tsv, or .txt file". My file is indeed a csv. I have tried both tab and comma separated columns. If "ignore first row" is checked, it will not import anything. If it is unchecked, it will import only one row. Also, if I paste the csv data it imports it fine into the desired columns and table, but also imports the data into the other table field assigned to that template. The two table field types have different names and a different amounts of columns. Any suggestions or tips is appreciated. I will be glad to pm you the csv I am using if you so wish. Thanks in advance. Link to comment Share on other sites More sharing options...
adrian Posted January 30, 2015 Author Share Posted January 30, 2015 Hi RJay - wow, that doesn't sound good Yes, please PM me the file. Also, does the same CSV file work on a template with only one Table field? Link to comment Share on other sites More sharing options...
RyanJ Posted January 30, 2015 Share Posted January 30, 2015 Hi Adrain, I removed one table field from the template and that is when I was able to get it to import the one row after I unchecked "ignore first row" however, I still received the same error message, but it did import the one row. It sound like something is wrong with my csv but I cant see where. I think there may be a separate issue with it importing into both field types. I am sending you the csv file now. Thanks for taking the time to look at it. Link to comment Share on other sites More sharing options...
adrian Posted January 30, 2015 Author Share Posted January 30, 2015 Thanks @RJay - I think I have managed to fix all those issues - I hadn't tested with multiple Table fields on the one page. Please check the latest version and let me know how it goes for you. PS There was nothing wrong with your CSV file. Link to comment Share on other sites More sharing options...
RyanJ Posted January 30, 2015 Share Posted January 30, 2015 Hi Adrian, Thanks for such a quick response time. Premium service there . After applying your updates, I was able to paste the csv data in and it was applied to the proper table, ignoring the other. However, when trying to import the csv file I still get the message "Session: That is not an allowed file extension for a CSV import. Try again with a .csv, .tsv, or .txt file" I tried this on both local and development servers just in case it was a permission issue. I then tried removing one of the tables, once I did that, I still received the message, but it did import the first row of the csv file. I am going to try maybe a text file and then just redoing the fields and go from there. I'll let you know what my results are. *Update Uploading a txt file works perfectly. Not sure why it does not like the csv files. Link to comment Share on other sites More sharing options...
adrian Posted January 30, 2015 Author Share Posted January 30, 2015 However, when trying to import the csv file I still get the message "Session: That is not an allowed file extension for a CSV import. Try again with a .csv, .tsv, or .txt file" I am thinking it must be my mimetype checks. Can you please try replacing: if(($csv_file_extension == 'csv' || $csv_file_extension == 'txt' || $csv_file_extension == 'tsv') && ($csv_type == 'text/plain' || $csv_type == 'text/tab-separated-values' || $csv_type == 'text/csv')){ $this->session->{$fieldName . '_csv_data'} = file_get_contents($_FILES[$csv_filename]["tmp_name"]); } with: if($csv_file_extension == 'csv' || $csv_file_extension == 'txt' || $csv_file_extension == 'tsv'){ $this->session->{$fieldName . '_csv_data'} = file_get_contents($_FILES[$csv_filename]["tmp_name"]); } and see if that allows the csv upload to work. 1 Link to comment Share on other sites More sharing options...
RyanJ Posted January 30, 2015 Share Posted January 30, 2015 Worked like a charm! 1 Link to comment Share on other sites More sharing options...
adrian Posted January 30, 2015 Author Share Posted January 30, 2015 Great to hear. I have committed another update that removes those checks - they really aren't very useful anyways as they can be manipulated in the browser and aren't always provided (hence the errors you were getting). Can you please confirm that all the other multiple Table field and issues with only one row importing are also now solved? Thanks for your help in getting this all fixed. Link to comment Share on other sites More sharing options...
RyanJ Posted January 31, 2015 Share Posted January 31, 2015 Importing and exporting both appear to be working perfectly. Thank you for providing such a great module and excellent support for it. It still amazes me how helpful the staff, moderators and other forum members are around here. 1 Link to comment Share on other sites More sharing options...
sgt.blikey Posted March 13, 2015 Share Posted March 13, 2015 Hi Adrian, This is a jolly helpful module, thanks for developing it. I ran into a problem when using it on a page where the table field is contained within a repeater on the page's template. The set up is as follows: The page template has the following fields: Title Programmes Programmes is a repeater field containing two fields: Year Programme The Year field is an integer field The Programme field is the table field The error occurs on page save and is reported as follows: Notice: Trying to get property of non-object in [my_path_to]/site/modules/TableCsvImportExport/TableCsvImportExport.module on line 263 Warning: Invalid argument supplied for foreach() in [my_path_to]/site/modules/TableCsvImportExport/TableCsvImportExport.module on line 263 Fatal error: Call to a member function makeBlankItem() on a non-object in [my_path_to]/site/modules/TableCsvImportExport/TableCsvImportExport.module on line 277 Error: Call to a member function makeBlankItem() on a non-object (line 277 of [my_path_to]/site/modules/TableCsvImportExport/TableCsvImportExport.module) This error message was shown because site is in debug mode ($config->debug = true; in /site/config.php). Error has been logged. By reworking the template to include only Title and Programme fields (i.e. a single table, no repeater) the problem goes away. I can work with that but wondered, since one of the posts above deals with a similar/related use case, if you can help at all. Many thanks, Nic Link to comment Share on other sites More sharing options...
adrian Posted March 14, 2015 Author Share Posted March 14, 2015 @sgt.blikey - I am glad you like it. I have put together a fix for supporting repeaters - please let me know if it works for you as expected. If anyone sees any issues with normal Table fields (not in repeaters), with this new version, please let me know! Even though I just added support for repeaters, I want to make sure you have you read this thread: https://processwire.com/talk/topic/8832-error-in-complex-form/ - it is in the Profields support board, but I am assuming you have access. In particular read the post by Ryan where he says that Tables in Repeaters are not officially supported, although he does say that if they were working and now aren't, he'd consider investigating. Mostly just wanted to warn you that maybe you will run into other issues with this approach. Have you considered switching to PageTables or even simple child pages and putting your Table field in those? Link to comment Share on other sites More sharing options...
sgt.blikey Posted March 16, 2015 Share Posted March 16, 2015 @adrian I (don't) think I had seen the thread you linked to. I guess table fields in repeaters is adding a layer of complexity that need not be there and it would be sensible to follow the PageTables/child pages route. I'll have a go with your updated module and let you know how that goes but will probably implement as PageTables/child pages. Many thanks for acting so promptly. Nic (oops, hadn't actually seen that thread!) 1 Link to comment Share on other sites More sharing options...
sgt.blikey Posted March 20, 2015 Share Posted March 20, 2015 @adrian I haven't had an opportunity to use the module within a repeater. I don't think I will do following your guidance. Thanks for your help. Nic Link to comment Share on other sites More sharing options...
sgt.blikey Posted April 1, 2015 Share Posted April 1, 2015 @adrian I've had an opportunity to try out the updated module with a repeater containing a table field. It works! 1 Link to comment Share on other sites More sharing options...
adrian Posted June 16, 2015 Author Share Posted June 16, 2015 Hi everyone, I have just added support for single and multiple page-reference fields and multiple options - requires PW 2.6.4 and Table 7-beta that Ryan released last week. Export converts page IDs to the title of the page. Import looks up the titles and populates the table with the IDs of the matching pages. Please let me know if you have any problems with this new version. 1 Link to comment Share on other sites More sharing options...
adrian Posted June 21, 2015 Author Share Posted June 21, 2015 A few more tweaks - you can now specify the separator when importing and exporting tables with the new multi-value fields. Also a couple of bug fixes and a speed improvement when importing. 1 Link to comment Share on other sites More sharing options...
netcarver Posted May 19, 2016 Share Posted May 19, 2016 Hi Adrian, I think there's a problem when using your module with any other code or modules that use fgets() or file() - such as Horst's WireQueueTextfile module (and possibly others.) I've just spent a couple of hours tracking this down over IRC with a client. The init() routine of your module sets PHPs auto_detect_line_endings flag to true - which can have un-intended consequences on other code run after your module initialises. In my client's case, they are using WireQueue with the textfile storage and it was throwing up errors, with your module installed, while unserialising a line pulled from the queue file using fgets() at the end of the getItem() method that pulls an item from the Wire Queue. Commenting out the following in TableCsvImportExport.module; ini_set('auto_detect_line_endings', true); fixed things for us. I understand the attraction of making the exported/imported files cross-platform compatible, but I think your module has other options open rather than changing the PHP setting during the init() routine. Perhaps, at the start of the import routine, you could read the initial state of that flag, then ini_set() it to true while you do the import and finally restore it to the initial setting on exit from your routine? Thanks for your great modules and for considering this request! 2 Link to comment Share on other sites More sharing options...
adrian Posted May 19, 2016 Author Share Posted May 19, 2016 Hi Steve - sorry you had to waste a couple of hours troubleshooting that, but thanks for the detailed analysis of the problem and the solution. The latest version should take care of it - please let me know if there are any continued problems. Thanks again! 4 Link to comment Share on other sites More sharing options...
netcarver Posted May 20, 2016 Share Posted May 20, 2016 Hi Adrian, Many thanks for that. I've just had word from the client that the new version of your module has their application up and running again. Outstanding response time! 4 Link to comment Share on other sites More sharing options...
awebcreature Posted June 16, 2016 Share Posted June 16, 2016 Hello ! I have a strange problem when importing .csv file. The error is: 32×PHP Notice: Array to string conversion in .../TableCsvImportExport/TableCsvImportExport.module:36016×PHP Notice: Indirect modification of overloaded property TableRow::$Array has no effect in .../TableCsvImportExport/TableCsvImportExport.module:360 The content of csv file : 00003;07-05-2016;14:37:57;0;3;5106;18;58;31;102;1;0;237;7;31;3;16;1;0;0;0 Can you help me ? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now