Commit c73b31e8 authored by jaapjansma's avatar jaapjansma

Create PDF action added

parent 195edece
......@@ -23,4 +23,5 @@ Version 1.2
* Find Individual by Name and Email address
* Get Relationship
* Update Activity Status
* Create PDF
<?php
use CRM_ActionProvider_ExtensionUtil as E;
class CRM_ActionProvider_Page_DownloadFile extends CRM_Core_Page {
public function run() {
$fileName = CRM_Utils_Request::retrieve('filename', 'String', $this, FALSE);
if (empty($fileName)) {
CRM_Core_Error::statusBounce("Cannot access file");
}
if (!self::isValidFileName($fileName)) {
CRM_Core_Error::statusBounce("Malformed filename");
}
$basePath = CRM_Core_Config::singleton()->templateCompileDir . 'action_provider';
$path = $basePath.'/'.$fileName;
$mimeType = mime_content_type($path);
if (!$path) {
CRM_Core_Error::statusBounce('Could not retrieve the file');
}
$buffer = file_get_contents($path);
if (!$buffer) {
CRM_Core_Error::statusBounce('The file is either empty or you do not have permission to retrieve the file');
}
CRM_Utils_System::download(
$fileName,
$mimeType,
$buffer,
NULL,
TRUE,
'download'
);
}
/**
* Is the filename a safe and valid filename passed in from URL
*
* @param string $fileName
* @return bool
*/
protected static function isValidFileName($fileName = NULL) {
if ($fileName) {
$check = $fileName !== basename($fileName) ? FALSE : TRUE;
if ($check) {
if (substr($fileName, 0, 1) == '/' || substr($fileName, 0, 1) == '.' || substr($fileName, 0, 1) == DIRECTORY_SEPARATOR) {
$check = FALSE;
}
}
return $check;
}
return FALSE;
}
}
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\ActionProvider\Action\Communication;
use Civi\ActionProvider\Action\AbstractAction;
use \Civi\ActionProvider\Parameter\ParameterBagInterface;
use Civi\ActionProvider\Parameter\SpecificationBag;
use Civi\ActionProvider\Parameter\Specification;
use Civi\ActionProvider\Utils\FileWriter;
use CRM_ActionProvider_ExtensionUtil as E;
class CreatePdf extends AbstractAction {
public function doAction(ParameterBagInterface $parameters, ParameterBagInterface $output) {
$domain = \CRM_Core_BAO_Domain::getDomain();
$message = $parameters->getParameter('message');
$contactId = $parameters->getParameter('contact_id');
$filename = $this->configuration->getParameter('filename');
$filename .= '_' . $contactId . '.pdf';
$contact = civicrm_api3('Contact', 'getsingle', array('id' => $contactId));
$tokens = \CRM_Utils_Token::getTokens($message);
\CRM_Utils_Hook::tokenValues($contact, $contactId, NULL, $tokens);
// call token hook
$hookTokens = array();
\CRM_Utils_Hook::tokens($hookTokens);
$categories = array_keys($hookTokens);
$message = \CRM_Utils_Token::replaceDomainTokens($message, $domain, TRUE, $tokens, TRUE);
$message = \CRM_Utils_Token::replaceHookTokens($message, $contact, $categories, TRUE);
\CRM_Utils_Token::replaceGreetingTokens($message, $contact, $contactId);
$message = \CRM_Utils_Token::replaceContactTokens($message, $contact, FALSE, $tokens, FALSE, TRUE);
$message = \CRM_Utils_Token::replaceComponentTokens($message, $contact, $tokens, TRUE);
if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
$smarty = \CRM_Core_Smarty::singleton();
$message = $smarty->fetch("string:{$message}");
}
$contents = \CRM_Utils_PDF_Utils::html2pdf($message, $filename, TRUE);
$fullFilePath = FileWriter::writeFile($contents, $filename);
$mimeType = mime_content_type($fullFilePath);
$activityParams = array(
'activity_type_id' => \CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Print PDF Letter'),
'activity_date_time' => date('YmdHis'),
'details' => $message,
'target_contact_id' => $contactId,
);
$result = civicrm_api3('Activity', 'create', $activityParams);
civicrm_api3('Attachment', 'create', array(
'entity_table' => 'civicrm_activity',
'entity_id' => $result['id'],
'name' => $filename,
'mime_type' => $mimeType,
'content' => $contents,
));
$downloadUrl = \CRM_Utils_System::url('civicrm/actionprovider/downloadfile', array('filename' => $filename));
\CRM_Core_Session::setStatus(E::ts('Created document for %1 <a href="%2">Download document<a/>', array(1=>$contact['display_name'], 2=>$downloadUrl)));
}
/**
* Returns the specification of the parameters of the actual action.
*
* @return SpecificationBag
*/
public function getParameterSpecification() {
return new SpecificationBag(array(
new Specification('contact_id', 'Integer', E::ts('Contact ID'), true),
new Specification('message', 'String', E::ts('Message'), true),
));
}
public function getConfigurationSpecification() {
return new SpecificationBag(array(
new Specification('filename', 'String', E::ts('Filename'), true, E::ts('document')),
));
}
}
\ No newline at end of file
......@@ -233,6 +233,10 @@ class Provider {
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::SEND_MESSAGES_TO_CONTACTS
));
$this->addActionWithoutFiltering('CreatePdf', '\Civi\ActionProvider\Action\Communication\CreatePdf', E::ts('Create PDF'), array(
AbstractAction::SINGLE_CONTACT_ACTION_TAG,
AbstractAction::SEND_MESSAGES_TO_CONTACTS
));
$this->addActionWithoutFiltering('CreateContribution', '\Civi\ActionProvider\Action\Contribution\CreateContribution', E::ts('Create contribution'), array(
AbstractAction::DATA_MANIPULATION_TAG,
AbstractAction::SINGLE_CONTACT_ACTION_TAG
......
<?php
/**
* @author Jaap Jansma <jaap.jansma@civicoop.org>
* @license AGPL-3.0
*/
namespace Civi\ActionProvider\Utils;
class FileWriter {
/**
* Write to a file in a restricted directory. E.g. a directory which is not accesible by
* a url.
*
* @param $contents
* @param $filename
* @return string
* The full file path.
*/
public static function writeFile($contents, $filename) {
$basePath = \CRM_Core_Config::singleton()->templateCompileDir . 'action_provider';
\CRM_Utils_File::createDir($basePath);
\CRM_Utils_File::restrictAccess($basePath.'/');
$fullFilePath = $basePath.'/'. $filename;
file_put_contents($fullFilePath, $contents);
return $fullFilePath;
}
}
\ No newline at end of file
......@@ -164,14 +164,9 @@ class SendEmail {
$$bodyType = \CRM_Utils_Token::replaceDomainTokens($$bodyType, $domain, TRUE, $tokens, TRUE);
$$bodyType = \CRM_Utils_Token::replaceHookTokens($$bodyType, $contact, $categories, TRUE);
foreach ($tokens as $type => $tokenValue) {
\CRM_Utils_Token::replaceGreetingTokens($$bodyType, $contact, $contact['contact_id']);
foreach ($tokenValue as $var) {
$$bodyType = \CRM_Utils_Token::replaceContactTokens($$bodyType, $contact, FALSE, $tokens, FALSE, TRUE);
$contactKey = NULL;
$$bodyType = \CRM_Utils_Token::replaceComponentTokens($$bodyType, $contact, $tokens, TRUE);
}
}
\CRM_Utils_Token::replaceGreetingTokens($$bodyType, $contact, $contact['contact_id']);
$$bodyType = \CRM_Utils_Token::replaceContactTokens($$bodyType, $contact, FALSE, $tokens, FALSE, TRUE);
$$bodyType = \CRM_Utils_Token::replaceComponentTokens($$bodyType, $contact, $tokens, TRUE);
}
}
$html = $body_html;
......
......@@ -263,16 +263,17 @@ function _action_provider_civix_find_files($dir, $pattern) {
*/
function _action_provider_civix_civicrm_managed(&$entities) {
$mgdFiles = _action_provider_civix_find_files(__DIR__, '*.mgd.php');
sort($mgdFiles);
foreach ($mgdFiles as $file) {
$es = include $file;
foreach ($es as $e) {
if (empty($e['module'])) {
$e['module'] = E::LONG_NAME;
}
$entities[] = $e;
if (empty($e['params']['version'])) {
$e['params']['version'] = '3';
}
$entities[] = $e;
}
}
}
......@@ -352,8 +353,10 @@ function _action_provider_civix_glob($pattern) {
* Inserts a navigation menu item at a given place in the hierarchy.
*
* @param array $menu - menu hierarchy
* @param string $path - path where insertion should happen (ie. Administer/System Settings)
* @param array $item - menu you need to insert (parent/child attributes will be filled for you)
* @param string $path - path to parent of this item, e.g. 'my_extension/submenu'
* 'Mailing', or 'Administer/System Settings'
* @param array $item - the item to insert (parent/child attributes will be
* filled for you)
*/
function _action_provider_civix_insert_navigation_menu(&$menu, $path, $item) {
// If we are done going down the path, insert menu
......@@ -443,3 +446,16 @@ function _action_provider_civix_civicrm_alterSettingsFolders(&$metaDataFolders =
$metaDataFolders[] = $settingsDir;
}
}
/**
* (Delegated) Implements hook_civicrm_entityTypes().
*
* Find any *.entityType.php files, merge their content, and return.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
*/
function _action_provider_civix_civicrm_entityTypes(&$entityTypes) {
$entityTypes = array_merge($entityTypes, array (
));
}
<?xml version="1.0"?>
<menu>
<item>
<path>civicrm/actionprovider/downloadfile</path>
<page_callback>CRM_ActionProvider_Page_DownloadFile</page_callback>
<title>DownloadFile</title>
<access_arguments>access CiviCRM</access_arguments>
</item>
</menu>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment