View source
<?php
define('ENTITY_PRINT_CSS_GROUP', CSS_THEME);
function entity_print_menu() {
$items['entityprint/%/%'] = array(
'title' => 'Print PDF',
'page callback' => 'entity_print_entity_to_pdf',
'page arguments' => array(
1,
2,
),
'type' => MENU_CALLBACK,
'access callback' => 'entity_print_access',
'access arguments' => array(
1,
2,
),
);
$items['entityprint/%/%/debug'] = array(
'title' => 'Print PDF Debug',
'page callback' => 'entity_print_entity_debug',
'page arguments' => array(
1,
2,
),
'type' => MENU_CALLBACK,
'access callback' => 'entity_print_access',
'access arguments' => array(
1,
2,
),
);
$items['admin/config/content/entityprint'] = array(
'title' => 'Entity Print',
'description' => 'Configure the settings for Entity Print.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'entity_print_settings_form',
),
'type' => MENU_NORMAL_ITEM,
'access arguments' => array(
'administer entity print',
),
'file' => 'entity_print.admin.inc',
);
return $items;
}
function entity_print_access($entity_type, $entity_id) {
if (user_access('bypass entity print access')) {
return entity_print_view_access($entity_type, $entity_id);
}
if (user_access('entity print access type ' . $entity_type)) {
return entity_print_view_access($entity_type, $entity_id);
}
$entity = entity_load_single($entity_type, $entity_id);
list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
if (user_access('entity print access bundle ' . $bundle)) {
return entity_print_view_access($entity_type, $entity_id);
}
return FALSE;
}
function entity_print_view_access($entity_type, $entity_id) {
if ($entities = entity_load($entity_type, array(
$entity_id,
))) {
$entity = array_pop($entities);
return entity_access('view', $entity_type, $entity);
}
return FALSE;
}
function entity_print_entity_to_pdf($entity_type, $entity_id) {
if ($entities = entity_load($entity_type, array(
$entity_id,
))) {
$library = libraries_load('phpwkhtmltopdf');
if (!empty($library['loaded'])) {
$pdf = new WkHtmlToPdf(array(
'binary' => variable_get('entity_print_wkhtmltopdf', '/usr/local/bin/wkhtmltopdf'),
));
$entity = reset($entities);
$html = _entity_print_get_generated_html($entity_type, $entity);
$pdf
->addPage($html);
drupal_alter('entity_print_pdf', $pdf, $entity_type, $entity);
if (!$pdf
->send()) {
print $pdf
->getError();
}
}
else {
print $library['error message'];
}
}
}
function entity_print_entity_debug($entity_type, $entity_id) {
if ($entities = entity_load($entity_type, array(
$entity_id,
))) {
$entity = reset($entities);
print _entity_print_get_generated_html($entity_type, $entity);
}
}
function _entity_print_get_generated_html($entity_type, $entity) {
$info = entity_get_info($entity_type);
$entity_id = $entity->{$info['entity keys']['id']};
$html_array = entity_view($entity_type, array(
$entity,
), 'pdf');
if (variable_get('entity_print_default_css', TRUE)) {
entity_print_add_css(drupal_get_path('module', 'entity_print') . '/css/entity-print.css');
}
$entity_print_css = _entity_print_get_css($entity_type, $entity, $info);
return theme('entity_print__' . $entity_type . '__' . $entity_id, array(
'entity_array' => $html_array,
'entity' => $entity,
'entity_print_css' => $entity_print_css,
));
}
function _entity_print_get_css($entity_type, $entity, $entity_info) {
module_invoke_all('entity_print_css', $entity_type, $entity);
global $theme;
$theme_path = drupal_get_path('theme', $theme);
$theme_info = drupal_parse_info_file($theme_path . "/{$theme}.info");
if (isset($theme_info['entity_print'])) {
if (isset($theme_info['entity_print']['all'])) {
entity_print_add_css("{$theme_path}/" . $theme_info['entity_print']['all']);
unset($theme_info['entity_print']['all']);
}
foreach ($theme_info['entity_print'] as $key => $value) {
if ($key !== $entity_type) {
continue;
}
foreach ($value as $css_bundle => $css) {
if ($css_bundle === 'all' || $entity->{$entity_info['entity keys']['bundle']} === $css_bundle) {
entity_print_add_css("{$theme_path}/{$css}");
}
}
}
}
$entity_print_css = array_filter(drupal_add_css(), function ($a) {
return $a['group'] === ENTITY_PRINT_CSS_GROUP;
});
return $entity_print_css;
}
function entity_print_add_css($css_file) {
drupal_add_css($css_file, array(
'group' => ENTITY_PRINT_CSS_GROUP,
));
}
function entity_print_theme($existing, $type, $theme, $path) {
return array(
'entity_print' => array(
'path' => $path . '/templates',
'template' => 'entity-print',
'variables' => array(
'entity_array' => NULL,
'entity' => NULL,
'entity_print_css' => NULL,
),
),
);
}
function entity_print_permission() {
$permissions['administer entity print'] = array(
'title' => t('Administer Entity Print'),
'description' => t('Allow users to administer the Entity Print settings.'),
'restrict access' => TRUE,
);
$permissions['bypass entity print access'] = array(
'title' => t('Bypass entity print access'),
'description' => t('Allow a user to bypass the entity print access rights.'),
);
$entities = entity_get_info();
foreach ($entities as $entity_key => $entity_info) {
$permissions['entity print access type ' . $entity_key] = array(
'title' => t('%entity_label: Use entity print for all bundles', array(
'%entity_label' => $entity_info['label'],
)),
'description' => t('Allow a user to use entity print to view the generated PDF for all %entity_label bundles.', array(
'%entity_label' => $entity_info['label'],
)),
);
foreach ($entity_info['bundles'] as $bundle_key => $entity_bundle) {
$permissions['entity print access bundle ' . $bundle_key] = array(
'title' => t('%entity_label (%entity_bundle_label): Use entity print', array(
'%entity_label' => $entity_info['label'],
'%entity_bundle_label' => $entity_bundle['label'],
)),
'description' => t('Allow a user to use entity print to view the generated PDF for entity type %entity_label and bundle %entity_bundle_label', array(
'%entity_label' => $entity_info['label'],
'%entity_bundle_label' => $entity_bundle['label'],
)),
);
}
}
return $permissions;
}
function entity_print_entity_info_alter(&$entity_info) {
foreach ($entity_info as $type => $info) {
$entity_info[$type]['view modes']['pdf'] = array(
'label' => 'PDF',
'custom settings' => FALSE,
);
}
}