Trying to write a plugin for Fabrik
Posted: 06 Aug 2009, 17:25
HI
I'm trying to set up a plugin for use with fabrik but I'm pretty stuck I've set up a skeleton phoca system plugin that I've installed successfully. To do that I copied the content plugin's files and changed the occurances of content to fabrik.
But I don't see how the core phocaPDF code can ever call my plugin?
For example In libraries/joomla/document/phocapdf/phocapdf.php render(): lines 143 - 156
there is:
so it looks like my option (com_fabrik) is never going to get called and it wont write out its buffer into the pdf document.
By changing the code to:
Everything works fine
Below is the modified render function that I used which works
Perhaps I'm not understanding something about how the code works, if so can someone point me in the right direction
For completeness here is my plugin php file:
I'm trying to set up a plugin for use with fabrik but I'm pretty stuck I've set up a skeleton phoca system plugin that I've installed successfully. To do that I copied the content plugin's files and changed the occurances of content to fabrik.
But I don't see how the core phocaPDF code can ever call my plugin?
For example In libraries/joomla/document/phocapdf/phocapdf.php render(): lines 143 - 156
there is:
Code: Select all
switch ($option) {
case 'com_content':
$results = $dispatcher->trigger('onBeforeDisplayPDFContent', array (&$pdf, &$content, &$this));
break;
case 'com_phocamenu':
$results = $dispatcher->trigger('onBeforeDisplayPDFRestaurantMenu', array (&$pdf, &$content, &$this));
break;
}By changing the code to:
Code: Select all
$t = JString::ucfirst(str_replace('com_', '', $option));
switch ($option) {
default:
$results = $dispatcher->trigger('onBeforeDisplayPDF'.$t, array (&$pdf, &$content, &$this));
break;
case 'com_content':
$results = $dispatcher->trigger('onBeforeDisplayPDFContent', array (&$pdf, &$content, &$this));
break;
case 'com_phocamenu':
$results = $dispatcher->trigger('onBeforeDisplayPDFRestaurantMenu', array (&$pdf, &$content, &$this));
break;
}
Below is the modified render function that I used which works
Code: Select all
function render( $cache = false, $params = array()) {
// Define - must be called before calling the plugin (because plugin includes definition file of tcpdf,
// so it must be defined before
define('K_TCPDF_EXTERNAL_CONFIG', true);
define("K_PATH_MAIN", JPATH_ADMINISTRATOR.DS.'components'.DS.'com_phocapdf'.DS.'assets'.DS.'tcpdf');// Installation path
define("K_PATH_URL", JPATH_BASE);// URL path
define("K_PATH_FONTS", JPATH_ADMINISTRATOR.DS.'components'.DS.'com_phocapdf'.DS.'fonts'.DS);// Fonts path
define("K_PATH_CACHE", K_PATH_MAIN.DS.'cache'.DS);// Cache directory path
$urlPath = JURI::base(true) . '/administrator/components/com_phocapdf/assets/tcpdf/';// Cache URL path
define("K_PATH_URL_CACHE", $urlPath.'cache/');
define("K_PATH_IMAGES", K_PATH_MAIN.DS.'images'.DS);// Images path
define("K_BLANK_IMAGE", K_PATH_IMAGES.DS."_blank.png");// Blank image path
define("K_CELL_HEIGHT_RATIO", 1.25);// Cell height ratio
define("K_TITLE_MAGNIFICATION", 1.3);// Magnification scale for titles
define("K_SMALL_RATIO", 2/3);// Reduction scale for small font
define("HEAD_MAGNIFICATION", 1.1);// Magnication scale for head
define('PDF_PAGE_FORMAT', 'A4');// page format
define('PDF_PAGE_ORIENTATION', 'P');// page orientation (P=portrait, L=landscape)
define('PDF_CREATOR', 'Phoca PDF');// document creator
define('PDF_AUTHOR', 'Phoca PDF');// document author
define('PDF_HEADER_TITLE', 'Phoca PDF');// header title
define('PDF_HEADER_STRING', "Phoca PDF");// header description string
//define('PDF_HEADER_LOGO', 'tcpdf_logo.jpg');// image logo
//define('PDF_HEADER_LOGO_WIDTH', 30);// header logo image width [mm]
define('PDF_UNIT', 'mm');// document unit of measure [pt=point, mm=millimeter, cm=centimeter, in=inch]
define('PDF_header_margin', 10);// header margin
define('PDF_footer_margin', 10);// footer margin
define('PDF_MARGIN_TOP', 27);// top margin
define('PDF_MARGIN_BOTTOM', 25);// bottom margin
define('PDF_MARGIN_LEFT', 15);// left margin
define('PDF_MARGIN_RIGHT', 15);// right margin
define('PDF_FONT_NAME_MAIN', 'freemono');// main font name
define('PDF_FONT_SIZE_MAIN', 10);// main font size
define('PDF_FONT_NAME_DATA', 'freemono');// data font name
define('PDF_FONT_SIZE_DATA', 8);// data font size
define('PDF_IMAGE_SCALE_RATIO', 4);// Ratio used to scale the images
// LOADING OF HELPER FILES (extended TCPDF library), LISTENING TO Phoca PDF Plugins
$option = JRequest::getCmd('option');
$t = JString::ucfirst(str_replace('com_', '', $option));
switch ($option) {
default:
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_phocapdf'.DS.'helpers'.DS.'phocapdf.php');
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_phocapdf'.DS.'assets'.DS.'tcpdf'.DS.'tcpdf.php');
$content = new JObject();
// Get info from Phoca PDF Content Plugin
$dispatcher = &JDispatcher::getInstance();
JPluginHelper::importPlugin('system');
$results = $dispatcher->trigger('onBeforeCreatePDF'.$t, array (&$content));
$pdf = new TCPDF($content->page_orientation, 'mm', $content->page_format, true, 'UTF-8', $content->use_cache);
break;
case 'com_content':
$content = new JObject();
// Get info from Phoca PDF Content Plugin
$dispatcher = &JDispatcher::getInstance();
JPluginHelper::importPlugin('system');
$results = $dispatcher->trigger('onBeforeCreatePDFContent', array (&$content));
if (JFile::exists(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_phocapdf'.DS.'helpers'.DS.'phocapdfcontenttcpdf.php')) {
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_phocapdf'.DS.'helpers'.DS.'phocapdfcontenttcpdf.php');
$pdf = new PhocaPDFContentTCPDF($content->page_orientation, 'mm', $content->page_format, true, 'UTF-8', $content->use_cache);
} else {
return JError::raiseError('PDF ERROR', 'Document cannot be created - Loading of Phoca PDF library (Content) failed');
}
break;
case 'com_phocamenu':
// Get info from Phoca PDF Restaurant Menu Plugin
$content = new JObject();
$dispatcher = &JDispatcher::getInstance();
JPluginHelper::importPlugin('phocapdf');
$results = $dispatcher->trigger('onBeforeCreatePDFRestaurantMenu', array (&$content));
if (JFile::exists(JPATH_SITE.DS.'plugins'.DS.'phocapdf'.DS.'restaurantmenu.php')) {
require_once(JPATH_SITE.DS.'plugins'.DS.'phocapdf'.DS.'restaurantmenu.php');
$pdf = new PhocaPDFRestaurantMenuTCPDF($content->page_orientation, 'mm', $content->page_format, true, 'UTF-8', $content->use_cache);
} else {
return JError::raiseError('PDF ERROR', 'Document cannot be created - Loading of Phoca PDF Plugin (Restaurant Menu) failed');
}
break;
/*default:
return JError::raiseError('PDF ERROR', 'Document cannot be created (No known option)');*/
break;
}
$pdf->SetMargins($content->margin_left, $content->margin_top, $content->margin_right);
$pdf->SetAutoPageBreak(TRUE, $content->margin_bottom);
$pdf->setCellHeightRatio($content->site_cell_height);
$pdf->setFont($content->font_type);
$siteFontColor = $pdf->convertHTMLColorToDec($content->site_font_color);
$pdf->SetTextColor($siteFontColor['R'], $siteFontColor['G'], $siteFontColor['B']);
$pdf->setPageFormat($content->page_format, $content->page_orientation);
$pdf->SetHeaderMargin($content->header_margin);
$pdf->SetFooterMargin($content->footer_margin);
$pdf->setImageScale($content->image_scale);
// PDF Metadata
$pdf->SetCreator(PDF_CREATOR);
// Content
switch ($option) {
default:
$results = $dispatcher->trigger('onBeforeDisplayPDF'.$t, array (&$pdf, &$content, &$this));
break;
case 'com_content':
$results = $dispatcher->trigger('onBeforeDisplayPDFContent', array (&$pdf, &$content, &$this));
break;
case 'com_phocamenu':
$results = $dispatcher->trigger('onBeforeDisplayPDFRestaurantMenu', array (&$pdf, &$content, &$this));
break;
}
// Called from administrator area (administrator calls frontend view, but it still administrator area)
$adminView = JRequest::getVar('admin', 0, '', 'int');
if ($adminView == 1) {
$content->pdf_destination = 'S';
}
if ($content->pdf_destination == 'D' || $content->pdf_destination == 'I') {
$pdf->Output($content->pdf_name, $content->pdf_destination);
return true;
}
$data = $pdf->Output($content->pdf_name, $content->pdf_destination);
// Set document type headers
parent::render();
JResponse::setHeader('Content-disposition', 'inline; filename="'.$this->getName().'.pdf"', true);
//Close and output PDF document
return $data;
}
For completeness here is my plugin php file:
Code: Select all
<?php
/*
* @package Joomla 1.5
* @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*
* @component Phoca Plugin
* @copyright Copyright (C) Jan Pavelka www.phoca.cz
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*
*/
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.plugin.plugin' );
class plgSystemPhocaPDFFabrik extends JPlugin
{
function plgSystemPhocaPDFFabrik(& $subject, $config) {
parent :: __construct($subject, $config);
}
function onAfterRender() {
$plugin =& JPluginHelper::getPlugin('system', 'phocapdffabrik');
$pluginP = new JParameter( $plugin->params );
$pdfDestination = $pluginP->get('pdf_destination', 'S');
/*global $mainframe;
if ($mainframe->isAdmin()) {
return;
}*/
// IE 7 bug
include_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_phocapdf'.DS.'helpers'.DS.'phocapdfbrowser.php');
$document = &JFactory::getDocument();
$doctype = $document->getType();
if ($doctype == 'html') {
$bodySite = JResponse::getBody();
if ($pdfDestination == 'I' || $pdfDestination == 'D') {
// Remome OnClick
$bodySite = preg_replace_callback('/<a(.+)href="(.*)format=pdf(.*)"(.+)onclick="(.*)"/Ui', array('plgSystemPhocaPDFFabrik', 'phocaPDFCallbackOnClick'), $bodySite);
} else {
//$bodySite = preg_replace_callback('/<a(.+)href="(.*)format=pdf(.*)"/Ui', array('plgSystemPhocaPDFFabrik', 'phocaPDFCallback'), $bodySite);
// IE 7 bug
$bodySite = preg_replace_callback('/<a(.+)href="(.*)format=pdf(.*)"(.+)onclick="(.*)"/Ui', array('plgSystemPhocaPDFFabrik', 'phocaPDFCallbackOnClickIE'), $bodySite);
}
JResponse::setBody($bodySite);
}
return true;
}
function phocaPDFCallbackOnClick ($matches) {
$a = $matches[0];
$b = $matches[1];
$c = $matches[2];
$d = $matches[3];
$e = $matches[4];
$f = $matches[5];
$replacement = '<a ' . $b . 'href="'. $c . 'format=phocapdf' . $d .'"'.$e.'';
return $replacement;
}
function phocaPDFCallbackOnClickIE ($matches) {
$a = $matches[0];
$b = $matches[1];
$c = $matches[2];
$d = $matches[3];
$e = $matches[4];// IE 7 bug
$f = $matches[5];// IE 7 bug
// IE 7 bug
$browser = PhocaPDFHelperBrowser::browserDetection('browser');
if ($browser == 'msie7' || $browser == 'msie8') {
$replacement = '<a '. $b . 'href="'. $c . 'format=phocapdf' . $d .'"'.$e.'target="_blank" ';
} else {
$replacement = '<a ' . $b . 'href="'. $c . 'format=phocapdf' .$d.'"'.$e.'onclick="'.$f.'"';
}
return $replacement;
}
/*
function phocaPDFCallback ($matches) {
$a = $matches[0];
$b = $matches[1];
$c = $matches[2];
$d = $matches[3];
$replacement = '<a ' . $b . 'href="'. $c . 'format=phocapdf' . $d .'"';
return $replacement;
}*/
function onBeforeCreatePDFFabrik(&$content) {
$content->content = '';
// load plugin params info
$plugin = &JPluginHelper::getPlugin('system', 'phocapdffabrik');
$pluginP = new JParameter( $plugin->params );
$content->margin_top = $pluginP->get('margin_top', 27);
$content->margin_left = $pluginP->get('margin_left', 15);
$content->margin_right = $pluginP->get('margin_right', 15);
$content->margin_bottom = $pluginP->get('margin_bottom', 25);
$content->site_font_color = $pluginP->get('site_font_color', '#000000');
$content->site_cell_height = $pluginP->get('site_cell_height', 1.2);
$content->font_type = $pluginP->get('font_type', 'freemono');
$content->page_format = $pluginP->get('page_format', 'A4');
$content->page_orientation = $pluginP->get('page_orientation', 'L');
$content->header_data = $pluginP->get('header_data', '');
$content->header_font_type = $pluginP->get('header_font_type', 'freemono');
$content->header_font_style = $pluginP->get('header_font_style', '');
$content->header_font_size = $pluginP->get('header_font_size', 10);
$content->header_margin = $pluginP->get('header_margin', 5);
$content->footer_font_type = $pluginP->get('footer_font_type', 'freemono');
$content->footer_font_style = $pluginP->get('footer_font_style', '');
$content->footer_font_size = $pluginP->get('footer_font_size', 10);
$content->footer_margin = $pluginP->get('footer_margin', 15);
$content->pdf_name = $pluginP->get('pdf_name', 'Phoca PDF');
$content->pdf_destination = $pluginP->get('pdf_destination', 'S');
$content->image_scale = $pluginP->get('image_scale', 4);
$content->display_plugin = $pluginP->get('display_plugin', 0);
$content->display_image = $pluginP->get('display_image', 1);
$content->use_cache = $pluginP->get('use_cache', 0);
//Extra values
if ((int)$content->site_cell_height > 3) {
$content->site_cell_height = 3;
}
if ((int)$content->margin_top > 200) {
$content->margin_top = 200;
}
if ((int)$content->margin_left > 50) {
$content->margin_left = 50;
}
if ((int)$content->margin_right > 50) {
$content->margin_right = 50;
}
if ((int)$content->margin_bottom > 150) {
$content->margin_bottom = 150;
}
if ((int)$content->header_font_size > 30) {
$content->header_font_size = 30;
}
if ((int)$content->footer_font_size > 30) {
$content->footer_font_size = 30;
}
if ((int)$content->header_margin > 50) {
$content->header_margin = 50;
}
if ((int)$content->footer_margin > 50) {
$content->footer_margin = 50;
}
if ((int)$content->image_scale < 0.5) {
$content->image_scale = 0.5;
}
return true;
}
function onBeforeDisplayPDFFabrik(&$pdf, &$content, &$document) {
$pdf->SetTitle($document->getTitle());
$pdf->SetSubject($document->getDescription());
$pdf->SetKeywords($document->getMetaData('keywords'));
// - - - - - - - - - - - - - - - -
// HEADER
if ($content->header_data != '') {
$pdf->setHeaderData('' , 0, '', $content->header_data);
} else {
$pdf->setHeaderData('' , 0, $document->getTitle(), $document->getHeader());
}
$pdf->setHeaderFont(array($content->header_font_type, $content->header_font_style, $content->header_font_size));
$lang = &JFactory::getLanguage();
$font = $content->font_type;
$pdf->setRTL($lang->isRTL());
$pdf->setFooterFont(array($content->footer_font_type, $content->footer_font_style, $content->footer_font_size));
// Initialize PDF Document
$pdf->AliasNbPages();
$pdf->AddPage();
$documentOutput = $document->getBuffer();
if ($content->display_plugin == 0) {
$documentOutput = preg_replace_callback('/\{(.*)\}/Ui', array('plgSystemPhocaPDFFabrik', 'phocaPDFCallbackPlugin'), $documentOutput);
}
if ($content->display_image == 0) {
$documentOutput = preg_replace_callback('/<img(.*)>/Ui', array('plgSystemPhocaPDFFabrik', 'phocaPDFCallbackImage'), $documentOutput);
}
// Build the PDF Document string from the document buffer
$pdf->writeHTML($documentOutput , true);
return true;
}
function phocaPDFCallbackPlugin ($matches) {
// Don't display Plugin Code in PDF
$a = $matches[0];
$replacement = '';
return $replacement;
}
function phocaPDFCallbackImage ($matches) {
// Don't display Images in PDF
$a = $matches[0];
$replacement = '';
return $replacement;
}
}
?>