Hi All

At the moment I have set up table manager in drupal 5 so that employees can add sales figures to a central sales sheet, which is working fine. Once the sheet has been completed I would then like the ability to export the completed sheet back to a CSV file on the local machine so that it can be used in our accounts department with excell to complete our audit process.

Does any one know if this process has been done before???

Would the feature be difficult to achieve???

Thanks, Matt

Comments

pobster’s picture

It's already a feature of the 6.x branch, but I'm afraid I'm not going to add anything to the current 5.x branch as it's not a *real* branch, it's just a messy hack on the 4.7.x branch and as such, is very, very old and outdated... I am planning on downgrading the 6.x branch to 5.x when I'm done with it but I wouldn't hold your breath as for how long that'll take - none of this is a priority for me...

...There's a really awful patch you can apply somewhere in this issue queue which allows for some sort of csv exporting. It's completely overkill though in that it's attached to the display function which is already complicated enough as it is (!) The current code for 6.x can easily be converted to work with 5.x (it's all the same principal) so if you've anyone who knows even just a little php - it'll be quite simple for them;

http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/tablemanage...

Pobster

rivena’s picture

just subscribing because I was looking for this just now.

Oh, and just a rudimentary suggestion, but with your data, can you just copy and paste from the website?

Anisa.

pobster’s picture

If you want this functionality then I'll do it tonight - as ever you only have to ask...

And yes you can just copy and paste a table into Excel (use "Paste Special" then specify 'unicode text') works fine... The export function I have works better though, it saves it as a proper csv file.

Pobster (I'll answer your other email in a bit, I've just woken up!)

shmatty29’s picture

Hi Pobster
Many thanks for the reply - look forward to seeing your fix. Had a look through and tried to modify the code from 6 to 5, but i guess i just dont know enough yet.
Thanks again.
Matt

rivena’s picture

Well, if that's all it takes! ;) I don't need it for testing anymore, but you know I love tablemanager. Being able export may come in handy for my members, especially if I import in all the checklists.

Anisa.

pobster’s picture

Status: Active » Needs review

You *know* you only have to ask ;o) And apologies, I didn't get home till 1am last night - so I did it this morning instead...

...Never one to create patches... Here's what to do (I'll submit it to the repository once it's been tested);

  // Add this to tablemanager_menu in the same way as the other entries are present
  $items[] = array('path' => 'admin/content/tablemanager/csv_export',
    'title' => t('Export CSV'),
    'callback' => 'drupal_get_form',
    'callback arguments' => array('tablemanager_csv_export'),
    'access' => $admin_access,
    'type' => MENU_NORMAL_ITEM,
    'weight' => 1,
  );

  // Add this to the bottom of the module;
/**
 * Allows a user to export a CSV file.
 */
function tablemanager_csv_export() {
  $tables = tablemanager_loadtables();
  if (!$tables) {
    return array('message' => array('#type' => 'item', '#value' => t('No tables defined yet.<p>!link</p>', array('!link' => l('Create Table', 'node/add/table')))));
  }
  $form['table'] = array(
    '#type' => 'select',
    '#title' => t('Table to export as CSV'),
    '#options' => $tables,
  );
  $form['separator'] = array(
    '#type' => 'textfield',
    '#title' => t('Separator'),
    '#default_value' => ',',
    '#description' => t('The character to separate each entry<br />NOTE newlines always separate each row'),
    '#size' => 1,
    '#maxlength' => 1,
    '#required' => TRUE,
  );
  return confirm_form($form, NULL, 'admin/content/tablemanager', ' ', t('Create'), t('Cancel'));
} // tablemanager_csv_export

/**
 * Submit function for tablemanager_csv_export form.
 */
function tablemanager_csv_export_submit($form_id, $form) {
  $tables = tablemanager_loadtables();
  $form['redirect'] = 'admin/content/tablemanager';
  $fetch = db_fetch_object(db_query('SELECT name, header FROM {tablemanager} WHERE tid = %d', $tables[$form['table']]));
  $name = is_numeric($fetch->name) || !$fetch->name ? 'Table-'. $tables[$form['table']] : str_replace(' ', '_', tablemanager_fetchname($tables[$form['table']]));
  $data = unserialize($fetch->header);
  foreach ($data as $build) {
    $csv_header[] = $build['data'];
  }
  $csv_header = implode($form['separator'], $csv_header);
  $query = db_query('SELECT data FROM {tablemanager_data} WHERE tid = %d ORDER BY id', $tables[$form['table']]);
  while ($fetch = db_fetch_object($query)) {
    $csv_data[] = implode($form['separator'], unserialize($fetch->data));
  }
  $csv = implode("\n", $csv_data);

  // need to 'Drupal-ize' - not sure how as this isn't actually a file?
  ob_end_clean();
  $headers[] = "Content-Disposition: attachment; filename = $name.csv";
  $headers[] = "Content-type: text/csv";
  foreach ($headers as $header) {
    $header = preg_replace('/\r?\n(?!\t| )/', '', $header);
    drupal_set_header($header);
  }
  print "$csv_header\n$csv";
  exit();
} // tablemanager_csv_export_submit

Pobster

rivena’s picture

Late night? The Cyprians again, I see. ;)

Would you like me to test it on the test site?

(I'm writing a howto for the checklists...for the third time. I think it may be jinxed, the browser window suddenly closes every time I try it. ;p)

Anisa.

pobster’s picture

Please... Would appreciate anyone testing it! :o) Sorry for not getting back to you yet by email, things have been ultra manic here since I got back! Plus that and I have absolutely no idea about your problem - so I've been trying a couple of things out when I get the chance... And I'm still none the wiser as of yet :o(

Pobster

rivena’s picture

I've forgotten what I asked you, but I'm sure it was something I was really stuck on... oh, was it the fuzzy date thing? I don't want anyone to write code for it, just a kind of, this functionality isn't available yet, what can I do in the meanwhile? I really want that released set thing. Writing this HOWTO make checklists thing is killing me.

As far as your export thingy, I had to run update.php before it showed up properly. Then, it actually worked pretty well! I was impressed. No errors on my screen, but a couple of fixits. 1) biggie, I didn't notice this at first, but when I open it up in Notepad instead of Excel first, it all runs on one line. Excel didn't seem to have any problem with it though, so not sure what's up with that. 2) you have to do that thing where you select the table not just by table number but also by table title. I did like the default save options (you have Table-1 or the table title if it exists, nice touch.)

Anisa.

pobster’s picture

Yeah it was the fuzzy date thing and well... I've not really ever used CCK so I'm a bit puzzled on what to suggest... I'll look into it a bit more, hopefully I'll come up with something? Fingers crossed!

Yep - sorry should have mentioned that you'd need to clear your cache (which is what happened when you ran update.php) that was to be expected. Also, Notepad doesn't tend to format non-text files very well, on more than a hundred occasions I've opened up module files via ftp (which open in Notepad) only to find they're all on one line as well... Not helpful... There's nothing you can do about it I'm afraid (Notepad-wise anyway, it's a throw-back to olden days anyway) you'll need to open it in Wordpad instead as it understands newlines better. As for the #2 request, I'm afraid that's a 6.x feature - it'll take a lot of hacking about to put it into the current 5.x branch I'm afraid! You'll have to wait until I backport HEAD... Which I'll do as soon as I get that bikini pic ;o)

Pobster

rivena’s picture

Ah, yes, I had forgotten that part of the thing. I only remembered that you had found some way to fix it.

Well, since I can't upgrade to 6 without a corresponding upgrade in all the major modules, and I really only use about 2 tablemanager tables, I'm afraid you're just going to have to live without.

Anisa.

pobster’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)

Closing as D5.x is now unsupported.