Skip to content

Instantly share code, notes, and snippets.

@esimonetti
Last active June 20, 2017 07:59
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 esimonetti/6f37a44e0d39158946d2aa9f523844cb to your computer and use it in GitHub Desktop.
Save esimonetti/6f37a44e0d39158946d2aa9f523844cb to your computer and use it in GitHub Desktop.
This is an example of how to have templates on sidecar modules, printable on either portrait or landscape based on user's choice. It excludes Reports, Quotes and Project as they do have a custom pdf generation
{{!--
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-06-20 on Sugar 7.9.0.0
// filename: custom/clients/base/fields/pdfaction/detail.hbs
//
// Allow for landscape pdf template printing for all modules aside Quotes, Reports and Projects
--}}
<a class="rowaction" href="javascript:void(0);"
data-action="link"
{{#if def.tooltip}}
rel="tooltip"
data-container="body"
data-placement="bottom"
data-original-title="{{str def.tooltip module}}"
{{/if}}
name="{{name}}">
{{#if def.icon}}<i class="fa {{def.icon}}"></i>{{/if}}{{str label}}</a>
{{#if fetchCalled}}
{{#if templateCollection.length}}
<ul class="dropdown-inset">
{{#each templateCollection.models}}
{{#if ../isCustomPDFView}}
<li>
<a href="javascript:void(0);" data-action="{{../../../def.action}}" data-id="{{attributes.id}}" data-landscape="0">{{attributes.name}} - {{str 'LBL_PDF_PORTRAIT'}}</a>
</li>
<li>
<a href="javascript:void(0);" data-action="{{../../../def.action}}" data-id="{{attributes.id}}" data-landscape="1">{{attributes.name}} - {{str 'LBL_PDF_LANDSCAPE'}}</a>
</li>
{{else}}
<li>
<a href="javascript:void(0);" data-action="{{../../../def.action}}" data-id="{{attributes.id}}">{{attributes.name}}</a>
</li>
{{/if}}
{{/each}}
</ul>
{{/if}}
{{/if}}
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-06-20 on Sugar 7.9.0.0
// filename: custom/Extension/application/Ext/Language/en_us.pdf_print.php
//
// Allow for landscape pdf template printing for all modules aside Quotes, Reports and Projects
$app_strings['LBL_PDF_LANDSCAPE'] = 'Landscape';
$app_strings['LBL_PDF_PORTRAIT'] = 'Portrait';
{{!--
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-06-20 on Sugar 7.9.0.0
// filename: custom/clients/base/fields/pdfaction/list.hbs
//
// Allow for landscape pdf template printing for all modules aside Quotes, Reports and Projects
--}}
<a class="rowaction" href="javascript:void(0);"
data-action="link"
{{#if def.tooltip}}
rel="tooltip"
data-container="body"
data-placement="bottom"
data-original-title="{{str def.tooltip module}}"
{{/if}}
name="{{name}}">
{{#if def.icon}}<i class="fa {{def.icon}}"></i>{{/if}}{{str label}}</a>
{{#if fetchCalled}}
{{#if templateCollection.length}}
<ul class="dropdown-inset">
{{#each templateCollection.models}}
{{#if ../isCustomPDFView}}
<li>
<a href="javascript:void(0);" data-action="{{../../../def.action}}" data-id="{{attributes.id}}" data-landscape="0">{{attributes.name}} - {{str 'LBL_PDF_PORTRAIT'}}</a>
</li>
<li>
<a href="javascript:void(0);" data-action="{{../../../def.action}}" data-id="{{attributes.id}}" data-landscape="1">{{attributes.name}} - {{str 'LBL_PDF_LANDSCAPE'}}</a>
</li>
{{else}}
<li>
<a href="javascript:void(0);" data-action="{{../../../def.action}}" data-id="{{attributes.id}}">{{attributes.name}}</a>
</li>
{{/if}}
{{/each}}
</ul>
{{/if}}
{{/if}}
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-06-20 on Sugar 7.9.0.0
// filename: custom/clients/base/fields/pdfaction/pdfaction.js
//
// Allow for landscape pdf template printing for all modules aside Quotes, Reports and Projects
({
extendsFrom: 'PdfactionField',
isCustomPDFView: false,
disabledModules: ['Reports', 'Quotes', 'Project'],
initialize: function(options) {
this._super('initialize', [options]);
if($.inArray(this.module, this.disabledModules) == -1) {
this.isCustomPDFView = true;
}
},
_buildDownloadLink: function(templateId, isLandscape) {
var urlParams = $.param({
'action': 'sugarpdf',
'module': this.module,
'sugarpdf': 'pdfmanager',
'record': this.model.id,
'pdf_template_id': templateId,
'landscape': isLandscape
});
return '?' + urlParams;
},
_buildEmailLink: function(templateId, isLandscape) {
return '#' + app.bwc.buildRoute(this.module, null, 'sugarpdf', {
'sugarpdf': 'pdfmanager',
'record': this.model.id,
'pdf_template_id': templateId,
'to_email': '1',
'landscape': isLandscape
});
},
emailClicked: function(evt) {
var templateId = this.$(evt.currentTarget).data('id');
var isLandscape = this.$(evt.currentTarget).data('landscape');
app.router.navigate(this._buildEmailLink(templateId, isLandscape), {
trigger: true
});
},
downloadClicked: function(evt) {
var templateId = this.$(evt.currentTarget).data('id');
var isLandscape = this.$(evt.currentTarget).data('landscape');
app.bwc.login(null, _.bind(function() {
this._triggerDownload(this._buildDownloadLink(templateId, isLandscape));
}, this));
}
})
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-06-20 on Sugar 7.9.0.0
// filename: custom/include/MVC/View/views/view.sugarpdf.php
//
// Allow for landscape pdf template printing for all modules aside Quotes, Reports and Projects
class CustomViewSugarpdf extends ViewSugarpdf
{
public function preDisplay()
{
parent::preDisplay();
if(isset($_REQUEST['landscape']) && $_REQUEST['landscape'] == 1) {
$this->sugarpdfBean->setPageFormat(PDF_PAGE_FORMAT, 'L');
} else {
$this->sugarpdfBean->setPageFormat(PDF_PAGE_FORMAT, 'P');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment