I have some code I'm working on for a PDF download page type in SilverStripe that allows people to upload a PDF file to the backend. In turn, this PDF file is then read into the top navigation as a link that, when clicked, automatically downloads the PDF file.
I have most of the code set up:
<?php
class PDFTemplate extends Page {
public static $db = array(
);
public static $has_one = array(
'PDFFile' => 'File'
);
public static $has_many = array(
);
public function Link() {
return '/home/download?ID=' . $this->ID;
}
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Main", new UploadField('PDFFile', "PDF File"), "Content");
return $fields;
}
}
class PDFTemplate_Controller extends Page_Controller {
public static $allowed_actions = array (
'download'
);
public function init() {
parent::init();
}
public function download() {
$id = $_GET['ID'];
$obj = DataObject::get_by_id('PDFTemplate', $id);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment;');
header('Pragma: no-cache');
return readfile("");
}
}
But the one thing I'm stuck on at the moment is how to get the url of the PDF file into the readfile() command. $obj right now is being set to get the id of the specific PDF file for the page....so do I need do use something like $obj.URL or $obj.Link in readfile()?
You're requesting a PDFTemplate object at the moment, not a File object so that's a problem - but you shouldn't need to pass an ID to this page to download it anyway because the Page already has that information.
If you don't need to hide the direct URL of the PDF in question, something like this might be a lot easier and offer better performance:
Delete the download() and Link() functions.
In the .ss template for your menu do: <a <% if $PDFFile %>href="$PDFFile.Link" target="_blank"<% else %>href="$Link"<% end_if %>>
Related
I am going to use DynamicPDF plugin to export to pdf some fields from backend on update/edit view of my plugin in OctoberCMS, can someone help me?
on plugin controller i have this call:
<?php namespace Vimagem\Pacientes\Controllers;
use Backend\Classes\Controller;
use BackendMenu;
use Renatio\DynamicPDF\Classes\PDF;
use Renatio\DynamicPDF\Classes\PDFWrapper;
class Pacientes extends Controller
{
public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController', 'Backend\Behaviors\ReorderController' ];
public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $reorderConfig = 'config_reorder.yaml';
public function __construct()
{
parent::__construct();
BackendMenu::setContext('Vimagem.Pacientes', 'main-menu-item');
}
/** PDF **/
public function pdf($id)
{
return PDF::loadTemplate('export-data-pdf')->stream('download.pdf');
}
}
On the PDF Template (export-data-pdf) i need to call some form fields from one client:
{{ name }}
{{ address }}
{{ phone }}
etc...
but i canĀ“t get the fields show up, what its wrong ?
Thank you,
Vitor
This code was found in the plugins documents.
use Renatio\DynamicPDF\Classes\PDF; // import facade
...
public function pdf()
{
$templateCode = 'renatio::invoice'; // unique code of the template
$data = ['name' => 'John Doe']; // optional data used in template
return PDF::loadTemplate($templateCode, $data)->stream('download.pdf');
}
I have used this plugin and it works well. You need to pass in data to the PDF stream.
This is done, worked around a solution for this.
Here is the controller application:
<?php namespace Vimagem\Pacientes\Controllers;
use Backend\Classes\Controller;
use BackendMenu;
use Renatio\DynamicPDF\Classes\PDFWrapper;
use Vimagem\Pacientes\Models\Paciente;
use \October\Rain\Database\Traits\Validation;
use Str;
class Pacientes extends Controller
{
public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController', 'Backend\Behaviors\ReorderController' ];
public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public $reorderConfig = 'config_reorder.yaml';
public function __construct()
{
parent::__construct();
BackendMenu::setContext('Vimagem.Pacientes', 'main-menu-item');
}
/**** PDF Export ***/
public function pdf($id)
{
$paciente = Paciente::find($id);
if ($paciente === null) {
throw new ApplicationException('User not found.');
}
$filename = Str::slug($paciente->nome) . '.pdf';
try {
/** #var PDFWrapper $pdf */
$pdf = app('dynamicpdf');
$options = [
'logOutputFile' => storage_path('temp/log.htm'),
];
return $pdf
->loadTemplate('export-data-pdf', compact('paciente'))
->setOptions($options)
->stream($filename);
} catch (Exception $e) {
throw new ApplicationException($e->getMessage());
}
}
}
Now i can use partials on the template like this:
<p>{{ paciente.nome }}</p>
<p>{{ paciente.morada }}</p>
etc...
Thank you all that try to helped me.
Vitor
I want to create a simple page in Prestashop back-office. I don't need any ObjectModel.
I've created a new admin Tab. My problem is in AdminController.
You can see the following code : the variables are not transmitted to the template file. I don't understand how to do it.
class AdminAzertyController extends AdminController
{
public function initContent()
{
parent::initContent();
// Le template smarty
$tpl_path = _PS_MODULE_DIR_ .'paniersdegout/views/templates/admin/view.tpl';
$tpl = $this->context->smarty->createTemplate($tpl_path, $this->context->smarty);
$content = $tpl->fetch();
$this->context->smarty->assign('content', $content);
// Le passage de variable
$this->context->smarty->assign('test', 'test');
}
}
To render custom tpl file in the custom controller, you can use smarty to assign content.
For example, if you have to render customtemplate.tpl file of custom module.
public function initContent() {
parent::initContent();
$content = $this->context->smarty->fetch(_PS_MODULE_DIR_ . 'custommodule/views/templates/admin/customtemplate.tpl');
$this->context->smarty->assign(
array(
'content' => $this->content . $content,
)
);
}
I would like to disable the contact form in my prestashop installation but there is no plugin to do so. Any suggestions how to do that?
Depends what you mean by disabling contact form but here are few possibilities.
Modifying core contact controller (not recommended since you will lose custom code when updating Prestashop)
Open file controllers/front/ContactController.php and add this code inside the ContactControllerCode class.
public function init()
{
Tools::redirect('pagenotfound'); // redirect contact page to 404 page
}
Overriding contact controller
Create a new file ContactController.php and place it in folder overrides/controllers/front/ and add the following code
class ContactController extends ContactControllerCore {
public function init()
{
Tools::redirect('pagenotfound'); // redirect contact page to 404 page
}
}
Create a small module
Create a new directory contactpagedisabler in folder modules and inside create a file contactpagedisabler.php and put this code in
class ContactPageDisabler extends Module
{
public function __construct()
{
$this->name = 'contactpagedisabler';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'whatever';
parent::__construct();
$this->displayName = $this->l('Contact page disabler');
$this->description = $this->l('Disables contact page.');
}
public function install()
{
return parent::install() && $this->registerHook('actionDispatcher');
}
// hook runs just after controller has been instantiated
public function hookActionDispatcher($params)
{
if ($params['controller_type'] === 1 && $params['controller_class'] === 'ContactController') {
Tools::redirect('pagenotfound'); // redirect contact page to 404 page
}
}
}
And then install this module from backoffice.
2nd option is simplest and it doesn't interfere with core files.
3rd option is probably overkill for such a small thing however it doesn't require overriding and if you or store manager ever needs the contact page back he can just disable the module from backoffice.
The module could also be expanded/modified with configuration page where you could for example get a list of all pages in store and let user decide which ones to enable/disable etc.
Update April 2018
Forget first two options and use third. Always use a module (if possible) when modifying your shop.
If You want to block just contact form but You want to display contact page You can put in override\controllers\front\ContactController.php:
<?php
class ContactController extends ContactControllerCore
{
public function postProcess()
{
if (Tools::isSubmit('submitMessage'))
{die('Form disabled');}
else
parent::postProcess();
//return null;
}
}
This will disable ability to send mails.
Then You can cut contact form from theme: /themes/YOUR-THEME/contact-form.tpl
to not display contact form at all
After this You have to delete file /cache/class_index.php to refresh classes in prestashop.
Barto's solution can also be achieved without an override.
Create another module contactformdisabler
class ContactFormDisabler extends Module
{
public function __construct()
{
$this->name = 'contactformdisabler';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'whatever';
parent::__construct();
$this->displayName = $this->l('Contact form disabler');
$this->description = $this->l('Disables contact form submission.');
}
public function install()
{
return parent::install() && $this->registerHook('actionDispatcher');
}
public function hookActionDispatcher($params)
{
if ($params['controller_type'] === 1
&& $params['controller_class'] === 'ContactController'
&& Tools::isSubmit('submitMessage')) {
die('Contact form submission disabled');
}
}
}
How to load a template file from my admin controller in custom module in prestashop 1.6
if (!defined('_PS_VERSION_')) exit;
class QueryAllTrxController extends ModuleAdminController
{
public $module;
public function __construct()
{
parent::__construct();
}
public function initContent()
{
parent::initContent();
$this->setTemplate('display.tpl');
//$this->setTemplate(_PS_THEME_DIR_.'mypage.tpl');
}
}
I had the same problem and it took forever to figure out.
I ended up spotting the solution in this video : https://www.youtube.com/watch?v=CdnJpLqqvcM
Any this is how I got it to work :
1 - Create the controller in ModuleName/controllers/AdminMyControllerNameController.php
class AdminMyControllerNameController extends ModuleAdminController
{
public function __construct()
{
$this->display = 'view';
$this->meta_title = $this->l('metatitle');
$this->toolbar_title = $this->l('tollbartitle');
parent::__construct();
}
public function initContent()
{
$this->show_toolbar = true;
$this->display = 'view';
$this->meta_title = $this->l('META TITLE');
parent::initContent();
$this->setTemplate('templatename.tpl');
}
public function initToolBarTitle()
{
$this->toolbar_title = $this->l('TOOLBAR TITLE??');
}
public function initToolBar()
{
return true;
}
}
2 - Create the template file in ModuleName/views/admin/my_controller_name/template.tpl
You have to create a directory in the views/admin folder using the name of your controller written in snake case.
Anyway I hope this will help.
WORKING CODE HERE
Background:
You want to add a custom admin page with a custom module controller. But you cannot customize template because you're stuck with this error message:
Fatal error: Uncaught --> Smarty: Unable to load template file '/var/www/html/admin-dev/themes/default/template/catalog/index.tpl' <-- thrown in /var/www/html/tools/smarty/sysplugins/smarty_internal_templatebase.php on line 129
Your current source code is:
class AdminYourModuleNameProductsController extends ModuleAdminController {
public function initContent() {
parent::initContent();
// enable these lines if you're stuck with damn stupid blank page with just 500 error
// ini_set('display_errors', '1');
// ini_set('display_startup_errors', '1');
// error_reporting(E_ALL);
$this->setTemplate('products/index.tpl');
}
}
And you don't know what to do because PrestaShop dev doc is the worst document in the history of ecommerce platform developer document and moreover its forum is full of chitchats and junks.
Solution
Place index.tpl at
{%PRESTA_ROOT%}/modules/{%YOUR MODULE DIR%}/views/templates/admin/{% snake case version of controller %}/products/index.tpl
For example, if your module name is yourmodulename and the controller name is AdminYourModuleNameProductsController (as in the example), the correct path is:
{%PRESTA_ROOT%}/modules/yourmodulename/views/templates/admin/your_module_name_products/products/index.tpl
If the error still persists:
Check this line:
{%PRESTA_ROOT%}/classes/controller/ModuleAdminController.php
public function createTemplate($tpl_name)
{
if (file_exists(_PS_THEME_DIR_.'modules/'.$this->module->name.'/views/templates/admin/'.$tpl_name) && $this->viewAccess()) {
// echo the following line and exit
return $this->context->smarty->createTemplate(_PS_THEME_DIR_.'modules/'.$this->module->name.'/views/templates/admin/'.$tpl_name, $this->context->smarty);
} elseif (file_exists($this->getTemplatePath().$this->override_folder.$tpl_name) && $this->viewAccess()) {
// echo the following line and exit
return $this->context->smarty->createTemplate($this->getTemplatePath().$this->override_folder.$tpl_name, $this->context->smarty);
}
// the error occurs because php get reach to the following line:
return parent::createTemplate($tpl_name);
}
Do as I commented and you can get the correct file path. Make sure the file exists in the path.
My PrestaShop version is 1.6.1.24
The code $this->setTemplate('display.tpl'); is loading a template file modules/your-custom-module/views/templates/admin/display.tpl or modules/your-custom-module/display.tpl.
The classname must be named that way: AdminQueryAllTrxController
you can put display.tpl in :
modules\module_name\views\templates\admin\classe_name(QueryAllTrx)
and use :$this->setTemplate('display.tpl'); in your AdminQueryAllTrxController
First of all add controller to your module:
modules\module_name\controllers\admin\SomeNameController.php
and extend it by ModuleAdminController, you need at least two methods for it to work properly __construct and initContent
put the following code to the later method:
$this->content .= $this->context->smarty->fetch($this->pathToTpl);
$this->context->smarty->assign(array(
'content' => $this->content,
));
You could replace $this->pathToTpl with any path which is pointed to your tpl file, I'm prefer to create the path dynamically. You can see a simple example here:
class SomeNameController extends ModuleAdminController{
var $pathToTpl;
public function __construct()
{
$this->bootstrap = true;
$this->context = Context::getContext();
$this->pathToTpl = _PS_MODULE_DIR_ .
$this->module->name . // put the name of module
'/views/templates/admin' .
'/' .
'templateName.tpl';
parent::__construct();
}
public function initContent()
{
parent::initContent();
$this->content .= $this->context->smarty->fetch($this->pathToTpl);
$this->context->smarty->assign(array(
'content' => $this->content,
));
}
}
finally you need to place templateName.tpl in the path you wanted to be:
modules\module_name\views\templates\admin\templateName.tpl
My system is working fine for small database and my report is generates from at least 5 table of phpmyadmin after certain limit of data load '500 internal server error' will come.I want enhance exporting a report to csv/excel from phpmyadmin using yii for larger database.
I use this extension to export to CSV. http://www.yiiframework.com/extension/csvexport/
I have created an action that I can attach to any controller that I need to export.
<?php
class Csv extends CAction {
public $field_list;
public function run() {
$controller = $this->getController();
/* Disable the logging because it should not run on this function */
foreach (\Yii::app()->log->routes as $route) {
if ($route instanceof \CWebLogRoute) {
$route->enabled = false;
}
}
\Yii::import('core.extensions.ECSVExport.ECSVExport');
//use the existing filters
$model_name = $controller->modelName();
$model = new $model_name('search');
$dataProvider = $model->search();
$criteria = $dataProvider->criteria;
//remove the pagination
$dataProvider->setPagination(false);
//changing the criteria to only select what we want
$criteria->select = implode(',', $this->field_list);
$dataProvider->setCriteria($criteria);
//export to CSV
$csv = new \ECSVExport($dataProvider);
if(isset($_GET['test'])) {
echo $csv->toCSV();
} else {
\Yii::app()->getRequest()->sendFile($controller->modelName() . '_'.date('Y-m-d').'.csv', $csv->toCSV(), "text/csv", false);
exit();
}
}
}
field_list are the fields that I need to export.
For the controller I add
/**
* #return array actions to be mapped to this controller
*/
public function actions(){
return array(
'csv'=>array(
'class'=>'core.components.actions.Csv',
'field_list'=>array('t.id', 't.name', 't.status'),
),
);
}
/**
I use the same search as in the controller because it suits me and because I use http://www.yiiframework.com/extension/remember-filters-gridview/ I actually can export exactly what is on the screen. Change the field list to what you need. Remember to give access to the csvAction function.
You can use yiiexcell extension for that: http://www.yiiframework.com/extension/yiiexcel/ it is simple wrapper for PHPExcel http://phpexcel.codeplex.com/releases/view/107442