| 1 |
<?php
|
| 2 |
/* $Id: pdfidcard.module,v 1.5 2007/09/22 06:17:24 jredding Exp $ */
|
| 3 |
define('PDFIDCARD_PERM_MANAGE', 'manage idcard');
|
| 4 |
define('PDFIDCARD_PERM_CREATEOWN', 'create own ID card');
|
| 5 |
define('PDFIDCARD_PERM_CREATEALL', 'create all ID Cards');
|
| 6 |
define('PDFIDCARD_BGIMAGE_FILEPATH','pdfidcard_bgimage_filepath');
|
| 7 |
define('PDFIDCARD_TCPDF_PATH','pdfidcard_tcpdf_path');
|
| 8 |
define('PDFIDCARD_IMAGES_PATH','pdfidcard_tcpdf_path');
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Display help and module information
|
| 12 |
* @param section which section of the site we're displaying help
|
| 13 |
* @return help text for section
|
| 14 |
*/
|
| 15 |
function pdfidcard_help($path, $arg) {
|
| 16 |
$output = '';
|
| 17 |
switch ($section) {
|
| 18 |
case "admin/modules#description":
|
| 19 |
$output = t("Creates printable PDF ID cards for your Users");
|
| 20 |
break;
|
| 21 |
}
|
| 22 |
return $output;
|
| 23 |
} //function pdfidcard_help
|
| 24 |
|
| 25 |
|
| 26 |
/**
|
| 27 |
* Valid permissions for this module
|
| 28 |
* @return array An array of valid permissions for the project module
|
| 29 |
*/
|
| 30 |
function pdfidcard_perm() {
|
| 31 |
return array(
|
| 32 |
PDFIDCARD_PERM_MANAGE,
|
| 33 |
PDFIDCARD_PERM_CREATEOWN,
|
| 34 |
PDFIDCARD_PERM_CREATEALL,
|
| 35 |
);
|
| 36 |
} // function idcard_perm
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
/**
|
| 41 |
* menu hook
|
| 42 |
* @return array of menu items
|
| 43 |
**/
|
| 44 |
function pdfidcard_menu() {
|
| 45 |
$items = array();
|
| 46 |
|
| 47 |
if(!$may_cache){
|
| 48 |
$items['idcard/create'] = array(
|
| 49 |
'title' => 'ID Cards',
|
| 50 |
'page callback' => 'pdfidcard_create',
|
| 51 |
'access arguments' => array('access content'),
|
| 52 |
'type' => MENU_CALLBACK,
|
| 53 |
);
|
| 54 |
$items['admin/build/idcard/templates'] = array(
|
| 55 |
'title' => 'ID Cards',
|
| 56 |
'description' => 'Create Identification card templates',
|
| 57 |
'page callback' => 'pdfidcard_template_page',
|
| 58 |
'access arguments' => array(PDFIDCARD_PERM_MANAGE),
|
| 59 |
'type' => MENU_NORMAL_ITEM,
|
| 60 |
);
|
| 61 |
$items['admin/settings/pdfidcard'] = array(
|
| 62 |
'title' => 'PDF-IDCard',
|
| 63 |
'description' => 'Configure the background images folder and the location of TCPDF',
|
| 64 |
'page callback' => 'drupal_get_form',
|
| 65 |
'page arguments' => array('pdfidcard_form_admin_settings'),
|
| 66 |
'access arguments' => array(PDFIDCARD_PERM_MANAGE),
|
| 67 |
'type' => MENU_NORMAL_ITEM,
|
| 68 |
);
|
| 69 |
}
|
| 70 |
return $items;
|
| 71 |
}
|
| 72 |
|
| 73 |
function pdfidcard_form_admin_settings(){
|
| 74 |
$form['PDFIDCARD_IMAGES_PATH'] = array(
|
| 75 |
'#type' => 'textfield',
|
| 76 |
'#title' => t('Background image folder'),
|
| 77 |
'#description' => t('Name of the folder where the
|
| 78 |
template background images will be stored. This will
|
| 79 |
be created within the files directory for this site.
|
| 80 |
'),
|
| 81 |
'#default_value' => variable_get('PDFIDCARD_IMAGES_PATH','idcard'),
|
| 82 |
'#required' => true,
|
| 83 |
);
|
| 84 |
//Check to see if the TCPDF Path is correct
|
| 85 |
if(!pdfidcard_init()){
|
| 86 |
drupal_set_message(t("Could not initialize TCPDF, check the path"));
|
| 87 |
}
|
| 88 |
$form['PDFIDCARD_TCPDF_PATH'] = array(
|
| 89 |
'#type' => 'textfield',
|
| 90 |
'#title' => t('Path to TCPDF'),
|
| 91 |
'#description' => t('Path to the tcpdf folder. TCPDF must be
|
| 92 |
configured correctly in order for this module
|
| 93 |
to work properly
|
| 94 |
'),
|
| 95 |
'#default_value' => variable_get('PDFIDCARD_TCPDF_PATH',drupal_get_path('module', 'pdfidcard')."/tcpdf"),
|
| 96 |
'#required' => true,
|
| 97 |
);
|
| 98 |
$form = system_settings_form($form);
|
| 99 |
return $form;
|
| 100 |
}
|
| 101 |
|
| 102 |
function pdfidcard_template_page(){
|
| 103 |
$output = pdfidcard_form_showtemplates();
|
| 104 |
$output .= drupal_get_form('pdfidcard_form_template');
|
| 105 |
return $output;
|
| 106 |
}
|
| 107 |
|
| 108 |
/*
|
| 109 |
* Implementation of hook_forms to map multiple form IDs
|
| 110 |
* to the same callback function
|
| 111 |
*/
|
| 112 |
function pdfidcard_forms($form_id, $args){
|
| 113 |
/* TODO Your function did not have $args in its signature.
|
| 114 |
Any $args[n] values have been converted to $args[n].
|
| 115 |
You may need to reduce these indices by one. */
|
| 116 |
$sql = "SELECT * FROM {pdfidcard_templates}";
|
| 117 |
$results = db_query($sql);
|
| 118 |
while ($result = db_fetch_array($results)){
|
| 119 |
$forms['pdfidcard_form_template_'.$result['templateid']]['callback'] = 'pdfidcard_form_template';
|
| 120 |
}
|
| 121 |
return $forms;
|
| 122 |
} //pdfidcard_forms
|
| 123 |
|
| 124 |
function pdfidcard_form_showtemplates(){
|
| 125 |
$sql = "SELECT * FROM {pdfidcard_templates}";
|
| 126 |
$results = db_query($sql);
|
| 127 |
$output = "";
|
| 128 |
//Create the forms
|
| 129 |
while ($result = db_fetch_array($results)){
|
| 130 |
$output .= drupal_get_form('pdfidcard_form_template', $result);
|
| 131 |
}
|
| 132 |
return $output;
|
| 133 |
}
|
| 134 |
|
| 135 |
function pdfidcard_form_template($form_state, $template = NULL){
|
| 136 |
if($template['name']) {
|
| 137 |
$fstitle = $template['name'];
|
| 138 |
$fieldset = $template['name'];
|
| 139 |
}
|
| 140 |
else {
|
| 141 |
$fstitle = "Add a new template";
|
| 142 |
$fieldset = "newtemplate";
|
| 143 |
}
|
| 144 |
$form['#title'] = $fieldset;
|
| 145 |
$form['#attributes']['enctype'] = 'multipart/form-data';
|
| 146 |
$form['#validate'][] = 'pdfidcard_form_template_validate';
|
| 147 |
$form['#submit'][] = 'pdfidcard_form_template_submit';
|
| 148 |
$form['#theme'] = 'pdfidcard_form_template';
|
| 149 |
$form[$fieldset] = array (
|
| 150 |
'#type' => 'fieldset',
|
| 151 |
'#title' => $fstitle,
|
| 152 |
'#collapsible' => true,
|
| 153 |
'#collapsed' => true,
|
| 154 |
);
|
| 155 |
|
| 156 |
$form[$fieldset]['name'] = array(
|
| 157 |
'#title' => 'Template Name',
|
| 158 |
'#description' => 'Create a name for the new Template,
|
| 159 |
spaces are not allowed nor are special characters',
|
| 160 |
'#type' => 'textfield',
|
| 161 |
'#maxlength' => 30,
|
| 162 |
'#size' => 30,
|
| 163 |
'#required' => true,
|
| 164 |
'#default_value' => $template['name'],
|
| 165 |
);
|
| 166 |
$form[$fieldset]['pageorient'] = array(
|
| 167 |
'#title' => 'Page Orientation',
|
| 168 |
'#description' => 'Orientation of the printout',
|
| 169 |
'#type' => 'select',
|
| 170 |
'#options' => array('P' => 'Portrait', 'L' => 'Landscape'),
|
| 171 |
'#default_value' => $template['pageorient'],
|
| 172 |
);
|
| 173 |
if(!$template['bgimage']) {
|
| 174 |
$form[$fieldset]['bgimage'] = array(
|
| 175 |
'#title' => 'Background Image',
|
| 176 |
'#type' => 'file',
|
| 177 |
'#description' => t('Select a background image
|
| 178 |
to use for this template'),
|
| 179 |
);
|
| 180 |
}
|
| 181 |
else {
|
| 182 |
$path = file_create_url($template['bgimage']);
|
| 183 |
$form[$fieldset]['bgimagepath'] = array(
|
| 184 |
'#type' => 'markup',
|
| 185 |
'#title' => t('Background Image'),
|
| 186 |
'#description' => t(''),
|
| 187 |
'#value' => "<div class=\"\">".
|
| 188 |
"<a href=\"$path\">".$template['bgimage']."</a>".
|
| 189 |
"</div>",
|
| 190 |
);
|
| 191 |
$form[$fieldset]['bgimagepathvalue'] = array(
|
| 192 |
'#type' => 'value',
|
| 193 |
'#value' => $template['bgimage'],
|
| 194 |
);
|
| 195 |
$form[$fieldset]['bgimage'] = array(
|
| 196 |
'#title' => 'Background Image',
|
| 197 |
'#type' => 'file',
|
| 198 |
'#description' => t('Uploading a new image will DELETE
|
| 199 |
the current background image'),
|
| 200 |
);
|
| 201 |
}
|
| 202 |
$form[$fieldset]['pgstyle'] = array(
|
| 203 |
'#title' => 'Page Style',
|
| 204 |
'#description' => t('Custom = use user supplied height/width')."<br />".
|
| 205 |
t('Image = use dimensions of image')."<br />" ,
|
| 206 |
'#type' => 'select',
|
| 207 |
'#options' => _pdfidcard_pgstyles(),
|
| 208 |
'#default_value' => $template['pgstyle'],
|
| 209 |
);
|
| 210 |
$form[$fieldset]['width'] = array(
|
| 211 |
'#title' => 'Width',
|
| 212 |
'#description' => t('Width in mm'),
|
| 213 |
'#type' => 'textfield',
|
| 214 |
'#maxlength' => 7,
|
| 215 |
'#size' => 10,
|
| 216 |
'#default_value' => $template['width'],
|
| 217 |
);
|
| 218 |
$form[$fieldset]['height'] = array(
|
| 219 |
'#title' => 'Height',
|
| 220 |
'#description' => t('Height in mm'),
|
| 221 |
'#type' => 'textfield',
|
| 222 |
'#maxlength' => 7,
|
| 223 |
'#size' => 10,
|
| 224 |
'#default_value' => $template['height'],
|
| 225 |
);
|
| 226 |
if($template){
|
| 227 |
$doc_link = l(t('TCPDF documentation'),
|
| 228 |
$GLOBALS['base_url'] .'/'.
|
| 229 |
variable_get('PDFIDCARD_TCPDF_PATH', 'tcpdf') .
|
| 230 |
"/doc/index.html"
|
| 231 |
);
|
| 232 |
$form[$fieldset]['php_format_code'] = array(
|
| 233 |
'#title' => 'PHP Code',
|
| 234 |
'#description' => t('Enter TCPDF PHP Code here. For more information
|
| 235 |
consult the ') . $doc_link ."<br/>".
|
| 236 |
t('The following items are available: $pdf(PDF Object)
|
| 237 |
$template (db record array), $account (user object)
|
| 238 |
')
|
| 239 |
,
|
| 240 |
'#type' => 'textarea',
|
| 241 |
'#default_value' => $template['php_format_code'],
|
| 242 |
);
|
| 243 |
$form[$fieldset]['submit'] = array(
|
| 244 |
'#type' => 'submit',
|
| 245 |
'#value' => t("Save Changes"),
|
| 246 |
);
|
| 247 |
$form[$fieldset]['delete'] = array(
|
| 248 |
'#type' => 'submit',
|
| 249 |
'#value' => t("Delete this template"),
|
| 250 |
);
|
| 251 |
$form[$fieldset]['templateid'] = array(
|
| 252 |
'#type' => 'value',
|
| 253 |
'#value' => $template['templateid'],
|
| 254 |
);
|
| 255 |
}
|
| 256 |
else {
|
| 257 |
$form[$fieldset]['submit'] = array(
|
| 258 |
'#type' => 'submit',
|
| 259 |
'#value' => t("Create new template"),
|
| 260 |
);
|
| 261 |
}
|
| 262 |
return $form;
|
| 263 |
}
|
| 264 |
/*
|
| 265 |
** Theme the form
|
| 266 |
*/
|
| 267 |
function theme_pdfidcard_form_template($form){
|
| 268 |
$key = $form['#title'];
|
| 269 |
$rows[] = array(
|
| 270 |
array(data=>drupal_render($form[$key]['name']),colspan=>2),
|
| 271 |
array(data=>drupal_render($form[$key]['pageorient'])),
|
| 272 |
);
|
| 273 |
if($form[$key]['bgimagepath']){
|
| 274 |
$rows[] = array(
|
| 275 |
array(data => "<div class=\"form-item\"><label for=\"edit-bgimagepath\">Current Image</label>", colspan=>1),
|
| 276 |
array(data => drupal_render($form[$key]['bgimagepath'])."</div>", colspan=>3),
|
| 277 |
);
|
| 278 |
}
|
| 279 |
$rows[] = array(
|
| 280 |
array(data => drupal_render($form[$key]['bgimage']), colspan=>3),
|
| 281 |
);
|
| 282 |
$rows[] = array(
|
| 283 |
drupal_render($form[$key]['pgstyle']),
|
| 284 |
drupal_render($form[$key]['height']),
|
| 285 |
drupal_render($form[$key]['width']),
|
| 286 |
);
|
| 287 |
$rows[] = array(
|
| 288 |
array(data => drupal_render($form[$key]['php_format_code']), colspan => 3),
|
| 289 |
);
|
| 290 |
if ($form[$key]['delete']){
|
| 291 |
$rows[] = array(
|
| 292 |
array(data=>drupal_render($form[$key]['submit']),colspan=>1),
|
| 293 |
array(data=>drupal_render($form[$key]['delete']),colspan=>2),
|
| 294 |
);
|
| 295 |
}
|
| 296 |
else {
|
| 297 |
$rows[] = array(
|
| 298 |
array(data=>drupal_render($form[$key]['submit']),colspan=>3),
|
| 299 |
);
|
| 300 |
}
|
| 301 |
$form[$key]['#children'] = theme('table',$header, $rows);
|
| 302 |
return drupal_render($form);
|
| 303 |
}
|
| 304 |
|
| 305 |
|
| 306 |
/*
|
| 307 |
** Validates the addtemplate form to check for errors
|
| 308 |
*/
|
| 309 |
function pdfidcard_form_template_validate($form, &$form_state){
|
| 310 |
if ($_POST['op'] == t("Delete this template")){
|
| 311 |
return TRUE;
|
| 312 |
}
|
| 313 |
if(preg_match('/[^a-zA-Z0-9_]/',$form_state['values']['name'])) {
|
| 314 |
form_set_error('name',
|
| 315 |
t('Template name must be alphanumeric or underscores only')
|
| 316 |
);
|
| 317 |
return FALSE;
|
| 318 |
}
|
| 319 |
|
| 320 |
$templateid = db_result(db_query("SELECT templateid from {pdfidcard_templates} WHERE name = '%s'", $form_state['values']['name']));
|
| 321 |
if($templateid && $templateid != $form_state['values']['templateid']) {
|
| 322 |
drupal_set_message($form_state['values']['form_id']);
|
| 323 |
form_set_error('name',
|
| 324 |
t('Template name must be unique')
|
| 325 |
);
|
| 326 |
return FALSE;
|
| 327 |
}
|
| 328 |
|
| 329 |
if($file) {
|
| 330 |
//File made it ok but is it the right type?
|
| 331 |
/* TODO Modify the validators array to suit your needs.
|
| 332 |
This array is used in the revised file_save_upload */
|
| 333 |
$validators = array(
|
| 334 |
'file_validate_is_image' => array(),
|
| 335 |
'file_validate_image_resolution' => array('85x85'),
|
| 336 |
'file_validate_size' => array(30 * 1024),
|
| 337 |
);
|
| 338 |
|
| 339 |
$file = file_save_upload(file_check_upload, $validators);
|
| 340 |
$file->filename = file_munge_filename($file->filename, $extensions = NULL, $alerts = TRUE);
|
| 341 |
//use image_get_info which will return exif type data.
|
| 342 |
//if it fails then its not an image
|
| 343 |
$img_info = image_get_info($file->filepath);
|
| 344 |
if ($img_info['extension'] != 'png' && $img_info['extension'] != 'jpg') {
|
| 345 |
form_set_error('bgimage', 'Uploaded was not of the type jpg or png');
|
| 346 |
return FALSE;
|
| 347 |
}
|
| 348 |
}
|
| 349 |
}
|
| 350 |
|
| 351 |
/*
|
| 352 |
* Delete the Background image from template
|
| 353 |
*/
|
| 354 |
function pdfidcard_deletebgimage($templateid){
|
| 355 |
$sql = "SELECT bgimage from {pdfidcard_templates}
|
| 356 |
WHERE templateid=%d";
|
| 357 |
$filename = db_result(db_query($sql,$templateid));
|
| 358 |
if ($filename && !file_delete($filename)){
|
| 359 |
return FALSE;
|
| 360 |
}
|
| 361 |
else {
|
| 362 |
return TRUE;
|
| 363 |
}
|
| 364 |
} //pdfidcard_deletebgimage
|
| 365 |
|
| 366 |
/*
|
| 367 |
* Deletes a template based on the primary key
|
| 368 |
*/
|
| 369 |
function pdfidcard_deletetemplate($templateid){
|
| 370 |
if(pdfidcard_deletebgimage($templateid)) {
|
| 371 |
$sql = "DELETE FROM {pdfidcard_templates}
|
| 372 |
WHERE templateid=%d";
|
| 373 |
if(!db_query($sql,$templateid)){
|
| 374 |
return FALSE;
|
| 375 |
}
|
| 376 |
else {
|
| 377 |
return TRUE;
|
| 378 |
} //db_query
|
| 379 |
}
|
| 380 |
else {
|
| 381 |
return FALSE;
|
| 382 |
}// pdfidcard_deletebgimage
|
| 383 |
} //pdfidcard_deletetemplate
|
| 384 |
|
| 385 |
/*
|
| 386 |
** Handles submission from the form template
|
| 387 |
*/
|
| 388 |
function pdfidcard_form_template_submit($form, &$form_state){
|
| 389 |
switch ($_POST['op']){
|
| 390 |
case t("Delete this template") :
|
| 391 |
if (pdfidcard_deletetemplate($form_state['values']['templateid'])){
|
| 392 |
drupal_set_message(t("Successfully Deleted ") . $form_state['values']['name'] );
|
| 393 |
}
|
| 394 |
else {
|
| 395 |
drupal_set_message(t("Sorry, the template could not be deleted"));
|
| 396 |
}
|
| 397 |
break;
|
| 398 |
case t("Save Changes") :
|
| 399 |
|
| 400 |
/* TODO Modify the validators array to suit your needs.
|
| 401 |
This array is used in the revised file_save_upload */
|
| 402 |
$validators = array(
|
| 403 |
'file_validate_is_image' => array(),
|
| 404 |
'file_validate_image_resolution' => array('85x85'),
|
| 405 |
'file_validate_size' => array(30 * 1024),
|
| 406 |
);
|
| 407 |
|
| 408 |
$file = file_save_upload(file_check_upload, $validators);
|
| 409 |
if ($file) {
|
| 410 |
//The file uploaded replaces the existing file
|
| 411 |
$template_folder = variable_get('PDFIDCARD_IMAGES_PATH','idcard');
|
| 412 |
$filename ="";
|
| 413 |
$filepath = file_create_path('') . "/" . $template_folder;
|
| 414 |
//check the directory for existence and for write perms
|
| 415 |
$is_writable = file_check_directory($filepath, 1);
|
| 416 |
if($is_writable) {
|
| 417 |
if($form_state['values']['bgimagepathvalue']){
|
| 418 |
//Delete the old if is it exists
|
| 419 |
if(!pdfidcard_deletebgimage($form_state['values']['templateid'])){
|
| 420 |
drupal_set_message(t("Could not delete previous bgimage, please remove manually"));
|
| 421 |
}
|
| 422 |
}
|
| 423 |
$file->filename = file_munge_filename($file->filename, $extensions = NULL, $alerts = TRUE);
|
| 424 |
$file = file_save_upload($file, $filepath, false);
|
| 425 |
//Now check once more to make sure we have a useable image
|
| 426 |
if (image_get_info($file->filepath)) {
|
| 427 |
$filename = $file->filepath;
|
| 428 |
$sql = "UPDATE {pdfidcard_templates} SET
|
| 429 |
name = '%s', pageorient = '%s', bgimage = '%s',
|
| 430 |
pgstyle = '%s', width = '%d', height = '%d' ,
|
| 431 |
php_format_code = '%s'
|
| 432 |
WHERE templateid = '%d'";
|
| 433 |
db_query($sql,
|
| 434 |
$form_state['values']['name'],
|
| 435 |
$form_state['values']['pageorient'],
|
| 436 |
$filename,
|
| 437 |
$form_state['values']['pgstyle'],
|
| 438 |
$form_state['values']['width'],
|
| 439 |
$form_state['values']['height'],
|
| 440 |
$form_state['values']['php_format_code'],
|
| 441 |
$form_state['values']['templateid']
|
| 442 |
);
|
| 443 |
} //image_get_info
|
| 444 |
} //is_writable
|
| 445 |
}
|
| 446 |
else {
|
| 447 |
$sql = "UPDATE {pdfidcard_templates} SET
|
| 448 |
name = '%s', pageorient = '%s', pgstyle = '%s',
|
| 449 |
width = '%d', height = '%d' , php_format_code = '%s'
|
| 450 |
WHERE templateid = '%d'";
|
| 451 |
db_query($sql,
|
| 452 |
$form_state['values']['name'],
|
| 453 |
$form_state['values']['pageorient'],
|
| 454 |
$form_state['values']['pgstyle'],
|
| 455 |
$form_state['values']['width'],
|
| 456 |
$form_state['values']['height'],
|
| 457 |
$form_state['values']['php_format_code'],
|
| 458 |
$form_state['values']['templateid']
|
| 459 |
);
|
| 460 |
}
|
| 461 |
break;
|
| 462 |
case t("Create new template") :
|
| 463 |
//saving or updating
|
| 464 |
$template_folder = variable_get('PDFIDCARD_IMAGES_PATH','idcard');
|
| 465 |
$filename ="";
|
| 466 |
$filepath = file_create_path('') . "/" . $template_folder;
|
| 467 |
//check the directory for existence and for write perms
|
| 468 |
$is_writable = file_check_directory($filepath, 1);
|
| 469 |
if($is_writable) {
|
| 470 |
//we're good to go
|
| 471 |
/* TODO Modify the validators array to suit your needs.
|
| 472 |
This array is used in the revised file_save_upload */
|
| 473 |
$validators = array(
|
| 474 |
'file_validate_is_image' => array(),
|
| 475 |
'file_validate_image_resolution' => array('85x85'),
|
| 476 |
'file_validate_size' => array(30 * 1024),
|
| 477 |
);
|
| 478 |
|
| 479 |
//$file = file_save_upload(file_check_upload, $validators);
|
| 480 |
//$file->filename = file_munge_filename($file->filename, $extensions = NULL, $alerts = TRUE);
|
| 481 |
//HACK should probably restore the filename munging again at some point
|
| 482 |
$file = file_save_upload('bgimage', $validators = array(), $filepath, false);
|
| 483 |
|
| 484 |
//Now check once more to make sure we have a useable image
|
| 485 |
if (image_get_info($file->filepath)) {
|
| 486 |
$filename = $file->filepath;
|
| 487 |
}
|
| 488 |
}
|
| 489 |
//Create the INSERT statement
|
| 490 |
$sql = "INSERT INTO {pdfidcard_templates}
|
| 491 |
(templateid, name, pageorient, bgimage, pgstyle, width, height, php_format_code )
|
| 492 |
VALUES(
|
| 493 |
'', '%s', '%s', '%s', '%s', '%d', '%d', '%s')";
|
| 494 |
db_query($sql,
|
| 495 |
$form_state['values']['name'],
|
| 496 |
$form_state['values']['pageorient'],
|
| 497 |
$filename,
|
| 498 |
$form_state['values']['pgstyle'],
|
| 499 |
$form_state['values']['width'],
|
| 500 |
$form_state['values']['height'],
|
| 501 |
$form_state['values']['php_format_code']
|
| 502 |
);
|
| 503 |
drupal_set_message(t("Created new template "). $form_state['values']['name']);
|
| 504 |
break;
|
| 505 |
}
|
| 506 |
}
|
| 507 |
|
| 508 |
/**
|
| 509 |
* create is an interface to create IDCards for users
|
| 510 |
**/
|
| 511 |
function pdfidcard_create(){
|
| 512 |
|
| 513 |
//Check that we can generate PDFs before we go further
|
| 514 |
if(!pdfidcard_init()){
|
| 515 |
drupal_set_message(t("PDF Generation is not configured correctly
|
| 516 |
PDFs are unavailable"));
|
| 517 |
return false;
|
| 518 |
}
|
| 519 |
|
| 520 |
$uid = arg(3);
|
| 521 |
$templateid = arg(2);
|
| 522 |
//pull in the user object
|
| 523 |
global $user;
|
| 524 |
if (is_numeric($uid) && ($uid != $user->uid)) {
|
| 525 |
$account = user_load(array('uid' => $uid));
|
| 526 |
}
|
| 527 |
else {
|
| 528 |
$account = $user;
|
| 529 |
}
|
| 530 |
|
| 531 |
if(!$account){
|
| 532 |
drupal_set_message(t("Could not load the user account
|
| 533 |
to create the Identification card"));
|
| 534 |
return false;
|
| 535 |
}
|
| 536 |
|
| 537 |
//Permission check
|
| 538 |
if(
|
| 539 |
($user->uid == $account->uid && user_access(PDFIDCARD_PERM_CREATEOWN))
|
| 540 |
||
|
| 541 |
(user_access(PDFIDCARD_PERM_CREATEALL))
|
| 542 |
) {
|
| 543 |
//Ok we have permission so now let's grab the name of the template
|
| 544 |
//and create the card
|
| 545 |
$sql = "SELECT * from {pdfidcard_templates} where templateid='%s'";
|
| 546 |
$template = db_fetch_array(db_query($sql, $templateid));
|
| 547 |
if (!is_array($template)) {
|
| 548 |
drupal_set_message(t("A template must be selected"));
|
| 549 |
return false;
|
| 550 |
}
|
| 551 |
$pdf = theme('pdfidcard_pdf', $template);
|
| 552 |
$pdf = theme('pdfidcard_addpage', $pdf, $template, $account);
|
| 553 |
$filename = $account->name;
|
| 554 |
$pdf = pdfidcard_output_pdf($pdf, $filename);
|
| 555 |
}
|
| 556 |
else {
|
| 557 |
return "Access denied";
|
| 558 |
}
|
| 559 |
|
| 560 |
|
| 561 |
} //function pdfidcard_createpage
|
| 562 |
|
| 563 |
/**
|
| 564 |
** implementation of hook_theme
|
| 565 |
*/
|
| 566 |
function pdfidcard_theme() {
|
| 567 |
return array(
|
| 568 |
'pdfidcard_pdf' => array(
|
| 569 |
'arguments' => array('template')
|
| 570 |
),
|
| 571 |
'pdfidcard_addpage' => array(
|
| 572 |
'arguments' => array('pdf', 'template', 'account')
|
| 573 |
),
|
| 574 |
);
|
| 575 |
}
|
| 576 |
|
| 577 |
/*
|
| 578 |
** theme the creation of the PDF object,
|
| 579 |
** $template = array of values containing the width, height, pagestyle, etc.
|
| 580 |
*/
|
| 581 |
function theme_pdfidcard_pdf($template){
|
| 582 |
|
| 583 |
|
| 584 |
//Determine the pagestyle to use
|
| 585 |
switch ($template['pgstyle']) {
|
| 586 |
case 'custom':
|
| 587 |
//We use the h/w from the user settings
|
| 588 |
$pgstyle = array($template['$height'], $template['$width']);
|
| 589 |
break;
|
| 590 |
case 'image':
|
| 591 |
//On image pgstyle we pull the height width from the image
|
| 592 |
$img_info = image_get_info($template['bgimage']);
|
| 593 |
if ($img_info) {
|
| 594 |
$heightpx = $img_info['height'];
|
| 595 |
$widthpx = $img_info['width'];
|
| 596 |
//Now convert pixels to mm but first we need a PDF object
|
| 597 |
$pdf = new PDFIDCARD();
|
| 598 |
//Covert to mm at 72 dpi
|
| 599 |
$heightmm = $heightpx * 25.4 / 72;
|
| 600 |
$widthmm = $widthpx * 25.4 / 72;
|
| 601 |
unset($pdf);
|
| 602 |
$pgstyle = array($heightmm, $widthmm);
|
| 603 |
}
|
| 604 |
|
| 605 |
break;
|
| 606 |
default:
|
| 607 |
$pgstyle = $template['pgstyle'];
|
| 608 |
}
|
| 609 |
if($template['pageorient'] == 'P' || $template['pageorient'] == 'L' ){
|
| 610 |
$pageorient = $template['pageorient'];
|
| 611 |
}
|
| 612 |
else {
|
| 613 |
$pageorient = 'L';
|
| 614 |
}
|
| 615 |
|
| 616 |
$pdf = new PDFIDCARD($pageorient,'mm',$pgstyle);
|
| 617 |
$pdf->Open();
|
| 618 |
|
| 619 |
//Set Margins to zero (Left, Top, Right) bottom is included in footer
|
| 620 |
$pdf->SetMargins(0,0,0);
|
| 621 |
//Prevent header and footer from being printed
|
| 622 |
$pdf->setPrintHeader(false);
|
| 623 |
$pdf->setPrintFooter(false);
|
| 624 |
//Turn off auto page breaks
|
| 625 |
$pdf->SetAutoPageBreak(false);
|
| 626 |
//Return the PDF object
|
| 627 |
return $pdf;
|
| 628 |
}
|
| 629 |
|
| 630 |
/*
|
| 631 |
* Adds a page to the PDF documents
|
| 632 |
*/
|
| 633 |
function theme_pdfidcard_addpage(&$pdf, $template, &$account){
|
| 634 |
$pdf->AddPage();
|
| 635 |
if($template['bgimage']){
|
| 636 |
$pdf->Image($template['bgimage'],0,0);
|
| 637 |
}
|
| 638 |
eval($template['php_format_code']);
|
| 639 |
return $pdf;
|
| 640 |
}//theme_pdfidcard_addpage
|
| 641 |
|
| 642 |
/*
|
| 643 |
** Outputs the PDF file
|
| 644 |
*/
|
| 645 |
function pdfidcard_output_pdf(&$pdf, $filename){
|
| 646 |
$filename = strtr(strip_tags($filename), " .,?!&#", "_______");
|
| 647 |
print $pdf->Output($filename . ".pdf","I");
|
| 648 |
module_invoke_all('exit');
|
| 649 |
exit;
|
| 650 |
}
|
| 651 |
|
| 652 |
/**
|
| 653 |
* Implements hook_user to add the "Create ID card link to the bottom"
|
| 654 |
**/
|
| 655 |
function pdfidcard_user($op, &$edit, &$account, $category = NULL){
|
| 656 |
switch($op) {
|
| 657 |
case 'view':
|
| 658 |
//viewing the user account
|
| 659 |
global $user;
|
| 660 |
if (
|
| 661 |
($user->uid == $account->uid && user_access(PDFIDCARD_PERM_CREATEOWN))
|
| 662 |
||
|
| 663 |
($user->uid != $account->uid && user_access(PDFIDCARD_PERM_CREATEALL))
|
| 664 |
)
|
| 665 |
{
|
| 666 |
/* if there is only one template we'll just create a hyperlink
|
| 667 |
* otherwise we'll create a dropdown form
|
| 668 |
*/
|
| 669 |
$sql = "SELECT templateid, name FROM {pdfidcard_templates}";
|
| 670 |
$row_count = db_result(db_query("SELECT COUNT(templateid) FROM {pdfidcard_templates}"));
|
| 671 |
$results = db_query($sql);
|
| 672 |
if($results){
|
| 673 |
if($row_count > 1) {
|
| 674 |
//Create a dropdown form vs. a simple link
|
| 675 |
$links = array();
|
| 676 |
$links[NULL] = 'Select a Card';
|
| 677 |
while ($result = db_fetch_array($results)) {
|
| 678 |
$url = url('idcard/create/'.$result['templateid']."/".$account->uid);
|
| 679 |
$links[$url] = $result['name'];
|
| 680 |
}
|
| 681 |
$form = drupal_get_form('pdfidcard_form_createcard_dropdown', $links);
|
| 682 |
$account->content['ID Cards'] = $form['cardselect'];
|
| 683 |
}
|
| 684 |
else {
|
| 685 |
//Only one template so create a single link to it
|
| 686 |
$result = db_fetch_array($results);
|
| 687 |
$form = array(
|
| 688 |
'#value' => l(t("Create Identification Card"),
|
| 689 |
"idcard/create/".$result['templateid']."/".$account->uid),
|
| 690 |
'#weight' => 50,
|
| 691 |
);
|
| 692 |
$account->content['ID Cards'] = $form;
|
| 693 |
}
|
| 694 |
}
|
| 695 |
}
|
| 696 |
break;
|
| 697 |
}
|
| 698 |
}
|
| 699 |
function pdfidcard_form_createcard_dropdown(&$form_state, $links){
|
| 700 |
$form = array();
|
| 701 |
$formname = 'cardselect';
|
| 702 |
$form[$formname] = array(
|
| 703 |
'#type' => 'select',
|
| 704 |
'#name' => $formname,
|
| 705 |
'#id' => $formname,
|
| 706 |
'#title' => t('Select a Card'),
|
| 707 |
'#options' => $links,
|
| 708 |
'#description' => '',
|
| 709 |
'#multiple' => FALSE,
|
| 710 |
'#attributes' => array('onChange' => "top.location.href=document.getElementById('$formname').options[document.getElementById('$formname').selectedIndex].value"),
|
| 711 |
);
|
| 712 |
return $form;
|
| 713 |
}
|
| 714 |
|
| 715 |
/*
|
| 716 |
* Returns list of available PG Styles
|
| 717 |
*/
|
| 718 |
function _pdfidcard_pgstyles(){
|
| 719 |
//If you add to this list ensure that TCPDF recognizes it
|
| 720 |
//OR you write code to handle the page size in theme_pdfidcard_page
|
| 721 |
$pgstyles = array();
|
| 722 |
$pgstyles['custom'] = 'Custom';
|
| 723 |
$pgstyles['image'] = 'Image';
|
| 724 |
$pgstyles['4A0'] = '4A0';
|
| 725 |
$pgstyles['2A0'] = '2A0';
|
| 726 |
$pgstyles['A0'] = 'A0';
|
| 727 |
$pgstyles['A1'] = 'A1';
|
| 728 |
$pgstyles['A2'] = 'A2';
|
| 729 |
$pgstyles['A3'] = 'A3';
|
| 730 |
$pgstyles['A4'] = 'A4';
|
| 731 |
$pgstyles['A5'] = 'A5';
|
| 732 |
$pgstyles['A6'] = 'A6';
|
| 733 |
$pgstyles['A7'] = 'A7';
|
| 734 |
$pgstyles['A8'] = 'A8';
|
| 735 |
$pgstyles['A9'] = 'A9';
|
| 736 |
$pgstyles['A10'] = 'A10';
|
| 737 |
$pgstyles['B0'] = 'B0';
|
| 738 |
$pgstyles['B1'] = 'B1';
|
| 739 |
$pgstyles['B2'] = 'B2';
|
| 740 |
$pgstyles['B3'] = 'B3';
|
| 741 |
$pgstyles['B4'] = 'B4';
|
| 742 |
$pgstyles['B5'] = 'B5';
|
| 743 |
$pgstyles['B6'] = 'B6';
|
| 744 |
$pgstyles['B7'] = 'B7';
|
| 745 |
$pgstyles['B8'] = 'B8';
|
| 746 |
$pgstyles['B9'] = 'B9';
|
| 747 |
$pgstyles['B10'] = 'B10';
|
| 748 |
$pgstyles['C0'] = 'C0';
|
| 749 |
$pgstyles['C1'] = 'C1';
|
| 750 |
$pgstyles['C2'] = 'C2';
|
| 751 |
$pgstyles['C3'] = 'C3';
|
| 752 |
$pgstyles['C4'] = 'C4';
|
| 753 |
$pgstyles['C5'] = 'C5';
|
| 754 |
$pgstyles['C6'] = 'C6';
|
| 755 |
$pgstyles['C7'] = 'C7';
|
| 756 |
$pgstyles['C8'] = 'C8';
|
| 757 |
$pgstyles['C9'] = 'C9';
|
| 758 |
$pgstyles['C10'] = 'C10';
|
| 759 |
$pgstyles['RA0'] = 'RA0';
|
| 760 |
$pgstyles['RA1'] = 'RA1';
|
| 761 |
$pgstyles['RA2'] = 'RA2';
|
| 762 |
$pgstyles['RA3'] = 'RA3';
|
| 763 |
$pgstyles['RA4'] = 'RA4';
|
| 764 |
$pgstyles['SRA0'] = 'SRA0';
|
| 765 |
$pgstyles['SRA1'] = 'SRA1';
|
| 766 |
$pgstyles['SRA2'] = 'SRA2';
|
| 767 |
$pgstyles['SRA3'] = 'SRA3';
|
| 768 |
$pgstyles['SRA4'] = 'SRA4';
|
| 769 |
$pgstyles['LETTER'] = 'LETTER';
|
| 770 |
$pgstyles['LEGAL'] = 'LEGAL';
|
| 771 |
$pgstyles['EXECUTIVE'] = 'EXECUTIVE';
|
| 772 |
$pgstyles['FOLIO'] = 'FOLIO';
|
| 773 |
return $pgstyles;
|
| 774 |
} //pdfidcard_pgstyles
|
| 775 |
|
| 776 |
/*
|
| 777 |
** Initializes TCPDF, returns true for A-OK, returns false if no tcpdf
|
| 778 |
*/
|
| 779 |
function pdfidcard_init(){
|
| 780 |
$tcpdf_path = variable_get('PDFIDCARD_TCPDF_PATH','');
|
| 781 |
$tcpdf_file = $tcpdf_path .'/tcpdf.php';
|
| 782 |
if (file_exists($tcpdf_file)) {
|
| 783 |
define('FPDF_FONTPATH',$tcpdf_path.'/fonts/');
|
| 784 |
require_once($tcpdf_file);
|
| 785 |
//Extend the TCPDF class to add in rotation
|
| 786 |
//Rotate Code from original FPDF modules and written by
|
| 787 |
//Matthew Brichacek <mmbrich@fosslabs.com>
|
| 788 |
//Rotate function pulled from Goodwill Consulting http://www.goodwill.co.id
|
| 789 |
|
| 790 |
if (!class_exists("PDFIDCARD")){
|
| 791 |
class PDFIDCARD extends TCPDF {
|
| 792 |
function Rotate($angle,$x=-1, $y=-1) {
|
| 793 |
$this->angle=$angle;
|
| 794 |
if($x==-1) {
|
| 795 |
$x=$this->x;
|
| 796 |
}
|
| 797 |
if($y==-1) {
|
| 798 |
$y=$this->y;
|
| 799 |
}
|
| 800 |
if($this->angle!=0) {
|
| 801 |
$this->_out('Q');
|
| 802 |
$this->angle=$angle;
|
| 803 |
if($angle!=0) {
|
| 804 |
$angle*=M_PI/180;
|
| 805 |
$c=cos($angle);
|
| 806 |
$s=sin($angle);
|
| 807 |
$cx=$x*$this->k;
|
| 808 |
$cy=($this->h-$y)*$this->k;
|
| 809 |
$this->_out(sprintf('q %.5f %.5f %.5f %.5f %.2f %.2f cm 1 0 0 1 %.2f %.2f cm',$c,$s,-$s,$c,$cx,$cy,-$cx,-$cy));
|
| 810 |
}
|
| 811 |
} // if $this->angle !=0
|
| 812 |
} //function Rotate
|
| 813 |
} //class
|
| 814 |
}
|
| 815 |
return true;
|
| 816 |
}
|
| 817 |
else {
|
| 818 |
return false;
|
| 819 |
}
|
| 820 |
} //function pdfidcard_init
|