Drupal 9 custom module to download file - php

I want to write custom module to send file to browser, but I don know how to do it. How the DefaultControler of the module should be written? I think about something like this:
class DefaultController extends ControllerBase
{
private function go()
{
ob_clean();
$gen = new SomeFileGenerator();
$gen->downloadFileAs('xxx.yyy'); //send file to browser
return 'File was sent';
}
public function start() //function defined in mymodule.routing.yml as main controller function
{
\Drupal::service('page_cache_kill_switch')->trigger();
return [
'#type' => 'markup',
'#markup' => $this->go(),
];
}
}
I thing that is not valid way, so please tell me how should i do it?
//I am noob in drupal/php ;), but is my hobby project, professionally I write low level c/c++ programs

Related

How set module view for custom searcher in prestashop 1.6

I'm trying to integrate Fotolia Api with Prestashop 1.6.0.9.
I already make module with custom tab, but I have no idea how set view from module folder for this tab. Sorry to say, but "documentation for developers" SUCKS.
I can't find any working solution.
public function install() {
if (!parent::install()
|| !$this->registerHook('backOfficeHeader')
|| !$this->registerHook('header')
) return false;
$tab = new Tab();
$tab->class_name = 'AdminFotoliaSelector';
$tab->id_parent = 0;
$tab->module = $this->name;
$tab->name[(int)(Configuration::get('PS_LANG_DEFAULT'))] = 'Fotolia Selector';
$tab->add();
return true;
}
I had big problem with make proper controller, and now I just can't load anything/ I have no idea how do this.
<?php
if (!defined('_PS_VERSION_'))
exit;
class AdminFotoliaSelectorController extends ModuleAdminController {
public $name;
public function __construct() {
$this->lang = (!isset($this->context->cookie) || !is_object($this->context->cookie)) ? intval(Configuration::get('PS_LANG_DEFAULT')) : intval($this->context->cookie->id_lang);
parent::__construct();
}
public function initContent() {
parent::initContent();
$this->renderForm();
}
public function renderForm() {
$path = _MODULE_DIR_."fotoliaselector";
$more = $this->module->display($path, 'views/templates/admin/fotoliaselector.tpl');
return $more.parent::renderForm();
}
When I try die($more) it gives me content of .tpl, anyway when I click tab in back office it's still empty.
I have debug options on, compiling on, cache off.
So just enlight me please, how am I supose to show ANYTHING there?
I think the problem is that you don't display tab's content at all.
I don't know what module->display method does, but I think you should change in initContent() method line:
$this->renderForm();
into
echo $this->renderForm();
If it won't help you should look at this documentation and try to do it without external classes - only try to use Smarty to display simple content without using Tab class or AdminFotoliaSelector
Well i know it will sounds weird but you need to take some similar modules, and read his code and will see some methods names are the same in each module.
Then copy that, install and play with some changes etc.
Imho you miss standard method getContent() form where you need to pass some data for smarty:
public function getContent()
{
global $smarty, $cookie;
......
//some code
......
$this->_html .= '<script type="text/javascript" src="'.__PS_BASE_URI__.'js/tinymce/jscripts/tiny_mce/tiny_mce.js"></script>';
$this->_html .= '<h1>My module title or stuff</h1>';
$this->_html .= $this->getMyCoolFormOrConfig();
$smarty->assign('errors', $this->errors);
$smarty->assign('message', $this->message);
$this->_html .= $this->display(__FILE__, 'name_of_tpl_file.tpl');
return $this->_html;
}
to simple add tab in BackOffice code like this:
$id_tab=Tab::getIdFromClassName('AdminPayment');
$newtab=new Tab();
$newtab->id_parent=$id_tab;
$newtab->module=$this->name;
$newtab->class_name='MyClassName'; //will be same like MyClassName.php in folder of you module where you need to create you class and extend the AdminTab and from there with function you need to echo you name module
$newtab->position=Tab::getNbTabs($id_tab)+1;
$newtab->name[$cookie->id_lang]=$this->l("Name of you stuff");
$newtab->name[Configuration::get('PS_LANG_DEFAULT')]=$this->l("Name of you stuff");
$newtab->add();
Study this file there /controllers/admin/AdminModulesController.php
and you see what methods are using in each module
Take a look greater feature to generate you module structure (register requeired) https://validator.prestashop.com/generator

Can Analog library be used to create multiple log files?

I'm looking for a decent, easy to use logging helper class-set/framework.
I discovered Analog and find it to be exactly what I need, despite the fact that it seems to be usable for one logfile at a time only.
Am I wrong ?!
Do you know some similar (in size/functionality) project that allows multiple logs to be written? An Analog-Branch maybe? I had a look at log4php, KLogger and Monolog already.
Judging from the source code at
https://github.com/jbroadway/analog/blob/master/examples/file.php and
https://github.com/jbroadway/analog/blob/master/examples/multi.php and
https://github.com/jbroadway/analog/blob/master/lib/Analog/Handler/Multi.php
you should be able to use several file handlers at the same time. Try something along the lines of this:
Analog::handler(Analog\Handler\Multi::init(array(
Analog::ERROR => Analog\Handler\File::init('/path/to/logs/errors.log'),
Analog::WARNING => Analog\Handler\File::init('/path/to/logs/warnings.log'),
Analog::DEBUG => Analog\Handler\File::init('/path/to/logs/debug.log')
)));
If you cannot make it work with Analog\Handler\Multi, you can still write your own Composite Logger, adapting the Analog File Handler. To do that, first create an Interface defining how you want to use Loggers in your application:
interface Logger
{
const ERROR = 'error';
const WARNING = 'warning';
const DEBUG = 'debug';
public function log($message, $level);
}
Then create the Adapter for Analog so that it satisfies the Interface:
class AnalogAdapter implements Logger
{
private $adaptee;
public function __construct(Analog $analog)
{
$this->adaptee = $analog;
}
public function log($message, $level)
{
$adaptee = $this->adaptee;
$adaptee::log($message, $adaptee::$level);
}
}
Finally, write the Composite Logger:
class CompositeLogger implements Logger
{
private $loggers = array;
public function registerLogger(Logger $logger)
{
$this->loggers[] = $logger;
}
public function log($message, $level)
{
foreach ($this->loggers as $logger)
{
$logger->log($message, $level);
}
}
}
Then you create your Analog file handlers and register them with the Composite:
$logger = new CompositeLogger;
$logger->registerLogger(
new AnalogAdapter(
Analog\Handler\File::init('/path/to/logs/errors.log')
)
);
// … add more Loggers in the same way
$logger->log('This is a warning', Logger::WARNING);
The warning will then get written to all the registered Loggers.
Yes it does work great. And you can create different log functions for different log types.
EG. This will email errors. But write warnings to a log.
Analog::handler(Analog\Handler\Multi::init(array(
Analog::ERROR => Analog\Handler\Buffer::init (
Analog\Handler\Mail::init (
'you#example.com',
'Log messages',
'noreply#example.com'
)
)
,Analog::WARNING => Analog\Handler\File::init(__DIR__.'/log/warning.log')
//Analog::DEBUG => Analog\Handler\File::init('/path/to/logs/debug.log')
)));

Test move_uploaded_file and is_uploaded_file with vfsStream

I've tried to test move_uploaded_file and is_uploaded_file with PHPUnit and vfsStream. They always return false.
public function testShouldUploadAZipFileAndMoveIt()
{
$_FILES = array('fieldName' => array(
'name' => 'file.zip',
'type' => 'application/zip',
'tmp_name' => 'vfs://root/file.zip',
'error' => 0,
'size' => 0,
));
vfsStream::setup();
$vfsStreamFile = vfsStream::newFile('file.zip');
vfsStreamWrapper::getRoot()
->addChild($vfsStreamFile);
$vfsStreamDirectory = vfsStream::newDirectory('/destination');
vfsStreamWrapper::getRoot()
->addChild($vfsStreamDirectory);
$fileUpload = new File_Upload();
$fileUpload->upload(
vfsStream::url('root/file.zip'),
vfsStream::url('root/destination/file.zip')
);
$this->assertFileExists(vfsStream::url('root/destination/file.zip'));
}
Is it possible? How do I do that?
Can I post a vfsStreamFile (or any data) without a form, just using PHP code?
Thank you.
No. move_uploaded_file and is_uploaded_file are specifically designed to handle uploaded files. They include extra security checks to ensure that the file's not been tampered with in the time between the upload completing and the controlling script accessing the file.
Note: changing the file from within the script counts as tampering.
Assuming you're using classes you can create a parent class.
// this is the class you want to test
class File {
public function verify($file) {
return $this->isUploadedFile($file);
}
public function isUploadedFile($file) {
return is_uploaded_file($file);
}
}
// for the unit test create a wrapper that overrides the isUploadedFile method
class FileWrapper extends File {
public function isUploadedFile($file) {
return true;
}
}
// write your unit test using the wrapper class
class FileTest extends PHPUnit_Framework_TestCase {
public function setup() {
$this->fileObj = new FileWrapper;
}
public function testFile() {
$result = $this->fileObj->verify('/some/random/path/to/file');
$this->assertTrue($result);
}
}

how to auto load mobile templates by agent in codeigniter?

dir:
application
-controllers
-models
-views
-mobile_views
How do I auto load templates at mobile_views when I use $this->load->view and view by iphone or other mobile phone?
Check this
You can do it in two way.
Way 1: Its very simple. In the above answer (the link I have given) add following line in the end of MyController function
$this->load->_ci_view_path . = $this->view_type .'/';
You are done. You can simply load view like normal view load.
Way 2:
To autoload a view based on user agent, I think you can implement it using hooks. To implement this hooks you need to follow the following steps
Autoload user agent library in autoload.php
$autoload['libraries'] = array('user_agent');
Enable hooks in config.php
$config['enable_hooks'] = TRUE;
Not implement hooks on post_controller_constructor. Add following codes to hooks.php
$hook['post_controller_constructor'][] = array('class' => 'Loadview',
'function' => 'load',
'filename' => 'loadview.php',
'filepath' => 'hooks'
);
Now create a page named loadview.php under hooks directory having following code
class Loadview
{
public static $MOBILE_PLATFORM = 'mobile';
public static $DEFAULT_PLATFORM = 'default';
public function load(){
$this->CI =& get_instance();
$view_type = $this->CI->agent->is_mobile() ? self::$MOBILE_PLATFORM : self::$DEFAULT_PLATFORM;
$this->CI->load->_ci_view_path = $this->CI->load->_ci_view_path . $view_type .'/';
}
}
You are done now. You can simply load view like normal view load.
to load views from another dir aside from "views", i found this forum topic to be helpful
http://codeigniter.com/forums/viewthread/132960/
function external_view($path, $view, $vars = array(), $return = FALSE)
{
$full_path = $path.$view.'.php';
if (file_exists($full_path))
{
return $this->_ci_load(array('_ci_path' => $full_path, '_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
else
{
show_error('Unable to load the requested module template file: '.$view);
}
}
and you can work the rest from the controller.
I do this in my controller:
public function index()
{
if($this->agent->is_mobile())
{
$this->load_mobile();
}
else
{
$this->load_web();
}
}
public function load_mobile()
{
$this->load->view('mobile/home');
}
public function load_web()
{
$this->load->view('web/home');
}
In this way I can add different data to mobile and to web pages.
I also extend the default controller and add some useful extra features:
Enables the usage of master page/templates.
Can add css and javascript files.
Uses the _output method for controlling the controllers output.
Can load relative content with in the form of modules (views)
So I can manage better the different pages.
Bye!!

Zend - refactoring to include a form globally on site

I currently have a search form in the search controller, so the only way I can get to it is through /search/. I have to refactor my code so that this search form appears not only in the Search Controller but also globally throughout the site.
( The code isnt exact as I had to retype some of it )
My class that extends Zend_Form is located in application/forms/forms/SearchForm.php:
class Form_SearchForm extends Zend_Form {
public function init() {};
}
My search controller is something like..
class SearchController extends Zend_Controller_Action
{
public function search() {
$searchForm = new Form_SearchForm();
$this->view->form = $searchForm;
}
}
In my Bootstrap.php I have an autoloader for models:
protected function _initAutoload() {
$autoLoader = Zend_Loader_Autoloader::getInstance();
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'form' => array(
'path' => 'forms',
'namespace' => 'Form_',
),
'model' => array(
'path' => 'models/',
'namespace' => 'Model_',
),
),
)
);
return $autoLoader;
}
I'm wondering where I can store my code so that globally the search form is generated in the view.
My global layout file is located in application/layouts/scripts/layout.phtml and currently spits out a dynamic content area:
<div id="main">
<?php echo $this->layout()->content;?>
</div>
Should I just add the form to this layout.phtml or is there some generic controller I should use?
Edit: Sorry for not specifying this too, but what if for example I wanted to not include it for 1-2 special pages ( maybe an admin section ).. if I hardcoded it into layout.phtml it would still appear.. or should I serve a different layout file to say, an admin area?
Creating a searchAction() is not good for performance because it requires a brand new dispatch cycle. If, and only if, you have very complex logic that justifies a separate action, you could create a Controller Plugin and add searchAction() to the ActionStack. If you are only instantiating/assigning the form or if you don't need the search form for every request, it's not an optimal solution.
Another possibility would be to instantiate and assign the form in the bootstrap. This kind-of breaks separation of concerns, but provides better performance.
protected function _initSearchForm()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$searchForm = new Form_SearchForm();
$view->searchForm = $searchForm;
return $searchForm;
}
Finally, my preferred solution would be a custom view helper:
<?php
class My_View_Helper_SearchForm extends Zend_View_Helper_Abstract
{
public function searchForm()
{
$searchForm = new Form_SearchForm();
return $searchForm;
}
}
For either of these solutions, you'd ideally output the form in your layout file to minimise duplication.
layout.phtml:
<?php echo $this->searchForm() ?>
And create an alternate layout admin.phtml for admin area pages. This gives you the flexibility to change the admin pages significantly when new requirements pop up.
You can create your Form in a Controller Plugin and add it to view vars somehow (by Zend_Controller_Front?), which are accessible in layout, too. But it's too complicated in current ZF version (or I'm too dumb)
You can make Form_SearchForm a singleton
class Form_SearchForm ... {
static function getInstance() {
static $instance;
if (!$instance)
$instance = new Form_SearchForm();
return $instance;
}
}
Now instead of creating new Form_SearchForm() just get it as
$form = Form_SearchForm::getInstance();
You can put an instance of Form_SearchForm to the registry
I probably have missed a very cool a simple way :)
I would split it into a partial and a place holder.
in layout.phtml:
<?php if($searchForm = $this->placeHolder('searchForm'): ?>
<?php echo $searchForm; ?>
<?php endif; ?>
then in your views you can call:
<?php $this->placeHolder('searchForm')->set($this->partial('search-from.phtml', 'search')); ?>
IF you wanted you could even make a search view helper that basically does the place holder call.
The Controller plugin would be better if you have more pages that dont need it than d though. I would still probably use placeholder though to accomplish it. That way you can easily override or append to it later on a view-by-view basis without calling anything on the front controller.

Categories