Community & Support

Add form and checkboxes to view?

Hi everyone,

I would like to add checkboxes and a submit button to a view. The checkboxes would appear next to each node and then when the submit button was clicked it would pass the info about each checked node (the filename of the attached file) to a php script (which will zip and force the download of all the selected attachments).

Can anyone point me in the right direction? Would this have to do with theming my view?

Thanks!

Comments

Two Options

When I've done this, I've done it without Views (because I was importing from a legacy database) - create a form and then use a theme callback to create the table with the checkboxes, but I think it could be done using Views theming.

If you use the Table style for your view (I'll assume the view name if "listing"), you should be able to copy views-view-table.tpl.php from your modules\views\themes folder and rename it to views-view-table--listing.tpl.php in your theme directory. In there you can control the output of the table to add your checkboxes to each row, add the submit button and the form tags.

It may not be the ideal solution but it should work.

David

Don't have a modules\views\themes folder

Don't seem to have a modules\views\themes folder. Do I need to create a view with the view theme wizard for this to appear?

What file controls the HTML output for a view?

Maybe it is because I am on drupal 5 but I can't find a views-view-table.tpl.php file. I can control the $headers and $rows data that gets sent to the theme in template.php but where can I add form tags to the view?

It is because you're Drupal

It is because you're Drupal 5 and Views 1... you should look at these pages for information on theming views 1 tables:

http://drupal.org/node/128741

and

http://drupal.org/node/42597

You should be able to do something similar but it's not in the .tpl.php file

Another option (although I haven't tested this) is that you might be able to use the Computed Field module to make your checkbox fields and then put the form open/close tags and the submit button in the header/footer of the view.

David

Thanks got it working

Thanks for the help! I was able to get it working by theming the table view in template.php to add the checkboxes and then added the form (and select all javascript) tags in the header and footer of the view like you suggested.

Any chance you can post the

Any chance you can post the code here your developed to get this working? It could help me out.

UPDATE - Found a great module for this use : http://drupal.org/project/views_embed_form

Thanks!

Here is the code I used. I

Here is the code I used. I used this code to insert a checkbox in a column with the header "Download link". Hope it helps!

function bluemarine_views_view_table_bookmarks_1($view, $nodes, $type) {

  $header = array();
    foreach ($view->field as $field) {
        $cell['data']=$field['label'];
$cell['class']="header";
        $header[] = $cell;
    }
//add an extra header for the checkbox column which is not stored in the db
$cell['data']="Select to Download";
        $cell['class']="header";
        $header[] = $cell;
  $fields = _views_get_fields();


foreach ($nodes as $node) {
        $row = array();
$myfields = $view->field;
$link = $node->node_data_field_download_link_field_download_link_value;

//stripping out the filepath from the full download link
$links_regex = '%<a[^/>]*'.'href=["|\']download.php\?file=([^javascript:].*)["|\']%Ui'; 
preg_match($links_regex, $link, $check);
//setting the value of the checkbox
$check = $check[1];
$nid = $node->nid;
$regex2 = '%^(\w*)/(\w*)/(\w*)%'; 
preg_match($regex2, $check, $matches);
        foreach ($myfields as $field) {
            $cell['data'] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $node, $view);
            $cell['class']='view-field-'.$field['queryname'];
            $row[] = $cell;
            
        }

//now add one more cell for the checkbox only if the checkbox has a value
if (!empty($check)){
$cell['data'] = "<input type=\"checkbox\" name=\"list[]\" value=\"$check\"/>";
            $cell['class']= "checkbox";
            $row[] = $cell;
}else{
//if there is a download link but the checkbox has no value (for video and multiple attachments
if(!empty($link)){
$cell['data'] = "use download link";
$cell['class']= "";
$row[] = $cell;

}else{
//still want to add an empty cell
$cell['data'] = "";
$cell['class']= "";
$row[] = $cell;
}
}
       
$rows[] = $row;
    }

 
  return theme('table', $header, $rows, array('class' => 'show-table'));
}

Thanks for the

Thanks for the reply.
However, if you don't make use of drupal_get_form, you are missing out on the validation and other aspects of the FORM API that drupal provides. If I were you I'd move to that module I commented on above, it's been working great for me.

additional information needed

Your code helped me a lot.
I have my checkboxes and a button (can be a link) "Download selected files".
A fiew files are selected and what happen after that?
Do you use download.php? Can you please explain how you did this?

Sorry, i didn't see your

Sorry, i didn't see your request earlier. Here is the code I used to force the user to download the attachments from the selected nodes as a zip file. I needed to customize it to return the correct file names inside the zipped archive. Also, I used a timestamp to ensure that the name of the zip archive would be different each time.

<?php
/*this code adapted from <a href="http://roshanbh.com.np/2008/09/force-download-mutiple-files-zip-archive-php.html*/
//need" title="http://roshanbh.com.np/2008/09/force-download-mutiple-files-zip-archive-php.html*/
//need" rel="nofollow">http://roshanbh.com.np/2008/09/force-download-mutiple-files-zip-archive-...</a> to detect and delete old archive or give dynamic name
//function to zip and force download the files using PHP
function zipFilesAndDownload($file_names,$archive_file_name)
{
 
//create the object
 
$zip = new ZipArchive();
// print_r($zip);
  //create the file and throw the error if unsuccessful
 
if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
    exit(
"cannot open <$archive_file_name>\n");
  }

 
//add each files of $file_name array to archive
 
foreach($file_names as $files){
 
//here is where I will need to parse the path and filename
  /*this is assuming 2 directories i.e. files/images/myfile.jpg.  Attachments (which are uploaded to the files/ dir will show up in the archive in a folder called files.*/

$regex = '%^(\w*)/(\w*)/([\w\s]*.\w*)%'
   
preg_match($regex, $files, $matches);
   
$dir1 = $matches[1];
   
$dir2 = $matches[2];
   
$filename = $matches[3];
   
//echo("<br>filename=" . $filename);
 
 
   
$zip->addFile($filepath.$files,$filename);
  }
 
$zip->close();

 
//then send the headers to foce download the zip file
 
header("Content-type: application/zip");
 
header("Content-Disposition: attachment; filename=$archive_file_name");
 
header("Pragma: no-cache");
 
header("Expires: 0");
 
readfile("$archive_file_name");
  exit;
}


$file_names =($_POST["list"]);
//print_r($file_names);
//adding the current timestamp to ensure a unique file name for the archive
$time = time();
$archive_file_name='resources' . $time . '.zip';
//echo("<br> archive_file_name = $archive_file_name");
$file_path=dirname(__FILE__).'/';
zipFilesAndDownload($file_names,$archive_file_name);
?>

Just want to say thank you,

Just want to say thank you, it works perfectly and saved a lot of my time

...

You should "invent the wheel" as little as possible. The UI for collecting nodes can be provided by other modules ("Views Bulk Operations" might suit you; "Flag" certainly can). Then you're left to implement only the "meat". (BTW, an interesting possibility is to implement the zipping as a Views page- or style- plugin.)

nobody click here