By renaud.richardet on
hi,
i just thought of sharing a little snippet on how to print postal labels with drupal out of addresses.
using cck and views templates, i created a custom function in themes/mytheme/template.php corresponding to my view (named label2).
then, by customizing print.css, i can only print the labels. note the "div style="page-break-after:always" to create page breaks with css2.
Renaud
/**
* View: labels2
*/
function phptemplate_views_view_list_labels2($view, $nodes, $type) {
$fields = _views_get_fields();
$taken = array();
// Group our nodes
$set = array();
foreach ($nodes as $node) {
$set[$node->node_data_field_country_field_country_value][] = $node;
}
// Set up the fields in nicely named chunks.
foreach ($view->field as $id => $field) {
$field_name = $field['field'];
if (isset($taken[$field_name])) {
$field_name = $field['queryname'];
}
$taken[$field_name] = true;
$field_names[$id] = $field_name;
}
// Set up some variables that won't change.
$base_vars = array(
'view' => $view,
'view_type' => $type,
);
$nl = "\r\n"; //line break
$output = $nl . '<div id="print-labels">' . $nl . '<table><tr>';
$c = 0; // just a cell counter
foreach ($set as $label => $nodes) {
$items = array();
foreach ($nodes as $i => $node) {
// $output .= '<pre>'.print_r($node, TRUE).'</pre><br>';
// skip this address if it is "empty"
$emptyness = 0;
if (empty($node -> node_data_field_address_field_address_value )) { $emptyness++; };
if (empty($node -> node_data_field_city_field_city_value )) { $emptyness++; };
if (empty($node -> node_data_field_postalcode_field_postalcode_value)) { $emptyness++; };
if ($emptyness > 2){
$nonValidAdr .= l($node-> node_title, 'node/'. $node -> nid) . ', ';
continue; // skip this node from the table
}
// add new row every 3rd cell
if(($c % 3)== 0){
$output .= $nl . '</tr>' . $nl;
}
// add new PAGE every 6th row
if(($c % 18) == 0 && $c != 0){
$output .= '</table>' . $nl . $nl . '<div style="page-break-after:always"></div>' . $nl . $nl . '<table>' . $nl;
}
// close new row every 3rd cell
if(($c % 3)== 0){
$output .= '<tr cellspacing="20">' . $nl;
}
// output node info
$output .= ' <td class="printlabel-cell">' . $nl;
$output .= print_nonempty($node -> node_title, true);
//$output .= print_nonempty($node -> node_data_field_organization_field_organization_value, true);
$output .= print_nonempty($node -> node_data_field_address_field_address_value, true);
$output .= print_nonempty($node -> node_data_field_postoffice_field_postoffice_value, true);
$output .= print_nonempty($node -> node_data_field_policestation_field_policestation_value, true);
$output .= print_nonempty($node -> node_data_field_district_field_district_value, true);
$output .= print_nonempty($node -> node_data_field_city_field_city_value, true);
$output .= print_nonempty($node -> node_data_field_postalcode_field_postalcode_value, true);
$output .= print_nonempty($node -> node_data_field_country_field_country_value, true);
$output .= '<br/> </td>' . $nl;
$c++;
}
}
$output .= '</table>' . $nl . '</div>';
//too little info
if (!empty($nonValidAdr)){
$nonValidAdrAll = '<div class="dont-print"><font color="red">Addresses with too little information, cannot create labels!</font> ' . $nonValidAdr . '<br/><br/><br/></div>';
}
$title .= '<h2 class="dont-print" >Total: ' .$c . ' addresses. Click on "print" to print them on paper.</h2>';
return $nonValidAdrAll . $title . $output;
}