I am newish to drupal... and have a code question perhaps someone has already done.
This question is for D6, but I would also like to see some sample code for D7 as well... I am not sure which version of DRUPAL I will end up using
I have a NODE type in D6.
It has a lot of file field of images ( that have been cropped).

IMAGE1 field_image1
IMAGE2 field_image2
IMAGE3 field_image3

I would like to create a button somewhere on the node, that will do the following...
It will create a zip file of all three images, but put into separate directories...

res/image1/default.png
res/image2/default.png
res/image3/default.png

so when the zip file is downloaded, it will create a file structure that looks like the above.

If you leave the code snippet, can you calso explain how I would pu thte putton on the page? should I use rules, or a write a custom module? or something else?
Perhaps there is a button control where i can put this code already in D6 and D7?

thanks

Comments

rohnjeynolds’s picture

I'm inferring here that you want the button to be visible when the node is being viewed, as opposed to edited? I don't know of a contrib module that does exactly what you're describing, maybe others do. I would approach it as a custom module that uses Forms API to insert a form with a submit button into the node body, and put the zipping and dowloading in the submit hook for that form. Here comes some ugly pseudo-code for imaginary, untested module ZipButton. I think hook_nodeapi() would be the way to get the form and button into the node but I guess it could also be done with hook_preprocess in a module or (less desirably) the theme:

function zipbutton_nodeapi($op, &$node) {
  if ($node->type == 'your type' && $op == 'view') {
    $node->body .= "\n" . '<div id="zipbutton_form">' . drupal_get_form(zipbutton_form($node)) . '</div>' . "\n";
  }
}

function zipbutton_form($node) {
  $form = array(
      'image1' => array (
        '#type' => 'value',
        '#default_value' => $node->image1_field[0]['value'];
      ),
      'image2' => array (
        '#type' => 'value',
        '#default_value' => $node->image2_field[0]['value'];
      ),
      'image3' => array (
        '#type' => 'value',
        '#default_value' => $node->image3_field[0]['value'];
      ),
      'submit' => array(
         '#type' => 'submit', 
         '#value' => t('Download images'),
      ),
  );
  return $form;
}

function zipbutton_form_submit($form, &$form_state) {
    ...
    here you'd maybe get the file paths from $form_state['values']
    or a value you could use to do a light query against the files table to get the paths
    then create the zip and push it to the user with header magic.
    ...
}