Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2013 04:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/6068106 to your computer and use it in GitHub Desktop.
Save anonymous/6068106 to your computer and use it in GitHub Desktop.
<?php
/*
This component is based on FPDF
from http://www.fpdf.org/
and uses the provided Script29
as per http://www.fpdf.org/en/script/script29.php
Example of custom format
$pdf = new PDF_Label(array('paper-size'=>'A4', 'metric'=>'mm', 'marginLeft'=>1, 'marginTop'=>1, 'NX'=>2, 'NY'=>7, 'SpaceX'=>0, 'SpaceY'=>0, 'width'=>99, 'height'=>38, 'font-size'=>14));
Standard format
$pdf = new PDF_Label('L7163');
*/
require('PDF_Label.php');
// Title, SELECT, FROM (optional), WHERE = $id (optional)
$fields = array(
array('Title', 'cb_title'),
array('First Name', 'firstname'),
array('Last Name', 'lastname'),
array('Division or Faculty', 'cb_divisionorganisation'),
array('Institution', 'company'),
array('Address 1', 'cb_addressline1'),
array('Address 2', 'cb_addressline2'),
array('Suburb', 'city'),
array('State', 'state'),
array('Postcode', 'zipcode'),
array('Country', 'country'),
);
/*------------------------------------------------
To create the object, 2 possibilities:
either pass a custom format via an array
or use a built-in AVERY name
------------------------------------------------*/
// Get a list (an array) of active subscriber IDs
$activeSubscribers = getActiveSubscribers($fields);
// Standard format
$pdf = new PDF_Label('L7163');
$pdf->AddPage();
// Print labels
//for($i=1;$i<=4;$i++) {
// $text = sprintf("%s\n%s\n%s\n%s %s, %s", "Laurent $i", 'Immeuble Toto', 'av. Fragonard', '06000', 'NICE', 'FRANCE');
// $pdf->Add_Label($text);
//d}
// Print more labels -- using actual data this time :p
foreach($activeSubscribers as $subscriber) {
$title = $subscriber->$fields[0][1];
$firstname = $subscriber->$fields[1][1];
$surname = $subscriber->$fields[2][1];
$faculty = $subscriber->$fields[3][1]; // doesn't work
$institution = $subscriber->$fields[4][1]; // doesn't work
$address1 = $subscriber->$fields[5][1]; // doesn't work
$address2 = $subscriber->$fields[6][1]; // doesn't work
$suburb = $subscriber->$fields[7][1];
$state = $subscriber->$fields[8][1]; // doesn't work
$postcode = $subscriber->$fields[9][1]; //doesn't work
$country = $subscriber->$fields[10][1]; //doesn't work
$lineOne = sprintf("%s %s %s",$title, $firstname, $surname);
$lineTwo = sprintf("%s", $faculty); // should have faculty and institution
$lineThree = sprintf("%s",$suburb); // should have address line 1
$lineFour = ""; // should have address line 2
$lineFive = ""; // should have suburb, state, postcode
$lineSix = ""; // should have country
// String lines together
$text = sprintf("%s\n%s\n%s\n%s", $lineOne, $lineTwo, $lineThree, $lineFour);
// Put em on a label
$pdf->Add_Label($text);
}
$pdf->Output();
exportPdf($pdf);
/*
Get a list of active subscribers
Returns array $subscribers
*/
function getActiveSubscribers($fields)
{
$db =& JFactory::getDBO();
// Get a list of subscriber ID's
$query = "SELECT `user_id` "
."FROM `jos_cbsubs_subscriptions` "
."WHERE `status` = 'A'";
$db->setQuery($query);
$subscribers = $db->loadObjectList();
// Build an object of subscribers information
foreach($subscribers as $subscriber)
{
// Get all info
foreach($fields as $field)
{
//echo $field;
$q = "SELECT `{$field[1]}` ";
// If an attribute representing a different table is provided,
// get field from that; else use the jos_comprofiler default.
if($field[2]) {
$q .= "FROM `{$field[2]}` ";
} else {
$q .= "FROM `jos_comprofiler` ";
}
// If a fourth attribute has been provided, we're using a different table
// and providing a column name for the user ID in that table. If no fourth attribute
// is provided, default to user_id because we're just using jos_comprofiler.
if($field[3]) {
// And if more than those four attributes have been provided, we're getting the name of the user's subscription
// plan from the appropriate table based on the plan_id for that user stored in the subscriptions table.
if($field[4]) {
// SELECT `name` FROM `jos_cbsubs_plans` WHERE `id` = ( SELECT `plan_id` FROM `jos_cbsubs_subscriptions` WHERE `user_id` = 121 AND `status` = 'A')
$q .= "WHERE `{$field[3]}` = ( SELECT `{$field[4]}` FROM `{$field[5]}` WHERE `{$field[6]}` = '{$subscriber->user_id}' AND `status` = 'A')";
} else {
$q .= "WHERE `{$field[3]}` = '{$subscriber->user_id}'";
}
} else {
$q .= "WHERE `user_id` = '{$subscriber->user_id}'";
}
$db->setQuery($q);
$subscriber->$field[1] = $db->loadResult();
}
}
return $subscribers;
}
// Set headers etc, and pop up the download
function exportPdf($pdf)
{
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($pdf));
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: application/pdf");
//header("Content-type: application/pdf");
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=Subscribers.pdf");
echo $pdf;
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment