How to call another class construct method in to a construct method? - fpdf

I like to call FPDF Construct class in Invoice class construct method. How can i call construct method of FPDF cass into Invoice class?
Invoice Class:
public function __construct($size='A4',$currency='$',$language='en') {
$this->columns = 4;
$this->items = array();
$this->totals = array();
$this->addText = array();
$this->firstColumnWidth = 70;
$this->currency = $currency;
$this->maxImageDimensions = array(230,130);
$this->setLanguage($language);
$this->setDocumentSize($size);
$this->setColor("#222222");
<!-- I want to call here fpdf construct class -->
$this->FPDF('P','mm',array($this->document['w'],$this->document['h']));
$this->AliasNbPages();
$this->SetMargins($this->margins['l'],$this->margins['t'],$this->margins['r']);
}
FPDF class:
function __construct($orientation='P', $unit='mm', $size='A4')
{
// Some checks
$this->_dochecks();
// Initialization of properties
$this->state = 0;
$this->page = 0;
$this->n = 2;
$this->buffer = '';
$this->SetCompression(true);
// Set default PDF version number
$this->PDFVersion = '1.3';
}

By simply calling it:
parent::__construct('P','mm', array($this->document['w'],$this->document['h']));
See here for more details.

Related

Magento 2: passing parameters from controller to phtml view

There's a way to passing parameters from controller to phtml view using PageFactory class?
Controller code:
use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use MyModule\Services\Service\CurlService;
class Index extends Action implements HttpGetActionInterface
{
protected $resultPageFactory;
protected $curlService;
public function __construct(
Context $context,
PageFactory $resultPageFactory,
CurlService $curlService
) {
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
$this->curlService = $curlService;
}
public function execute()
{
if(isset($_GET['action']) && $_GET['action'] == true)
{
$response = json_decode($this->curlService->response());
switch($response->status)
{
case 'ok': $msgReturn = 'Successfull'; break;
default: $msgReturn = 'Error'; break;
}
}
$resultPage = $this->resultPageFactory->create();
$resultPage->setActiveMenu(static::MENU_ID);
$resultPage->getConfig()->getTitle()->prepend(__('Page Title'));
return $resultPage;
}
I need passing $msgReturn using PageFactory to the corresponding view
To add data to your phtml block, simply enter this example in the controller where you set your data.
use Magento\Framework\View\Result\PageFactory;
$page = $this->pageFactory->create();
$block = $page->getLayout()->getBlock('block_alias');
$block->setData('name_var', $data);
And then in the phtml file
$data = $block->getData('name_var');

common object zend framework for Zend_Filter_StripTags which can be used throughout the application?

I'm trying to create a function in bootstrap to intitiate object for Zend_Filter_StripTags, so that I can use its object throughout the application.
protected function _initHtmlFilter() {
$allowedTags = array('p','b','br','strong'); // Allowed tags
$allowedAttributes = array('href'); // Allowed attributes
$stripTags = new Zend_Filter_StripTags($allowedTags,$allowedAttributes);
}
but I'm unable to use this object($stripTags) in any controller.
I would create an Controller Action Helper for this:
class My_Controller_Action_Helper_StripTags extends
Zend_Controller_Action_Helper_Abstract
{
/**
* StripTags
*
* #param string $input String to strip tags from
*
* #return string String without tags
*/
public function stripTags($input)
{
$allowedTags = array('p','b','br','strong'); // Allowed tags
$allowedAttributes = array('href'); // Allowed attributes
$stripTags = new Zend_Filter_StripTags($allowedTags,$allowedAttributes);
// return input without tags
return $stripTags->filter($input);
}
}
// example in indexAction
$noTags = $this->_helper->stripTags('<h2>TEST</h2>');
You have to add the path to your helpers in your application.ini:
resources.frontController.actionhelperpaths.My_Controller_Action_Helper_ = "My/Controller/Action/Helper"
You can use zend registry for this:
protected function _initHtmlFilter() {
$allowedTags = array('p','b','br','strong'); // Allowed tags
$allowedAttributes = array('href'); // Allowed attributes
$stripTags = new Zend_Filter_StripTags($allowedTags,$allowedAttributes);
Zend_Registry::set('zend_strip_tags', $stripTags);
}
and can access it any where like :
Zend_Registry::get('zend_strip_tags');

zend: parameter collision

I wonder why no one ever asked this question.
Every zend Action function in controller class has 3 paramters, namely 'module', 'controller', and 'action'.
What happens, when I get a parameter named 'action' from a form or url, for example "?action=edit" ??
I tested it: action holds its value from router, not 'edit'.
public function someAction() {
$params = $this->getRequest()->getParams();
...
How could I pass the parameter named "action", if I had to ??
Thanks in advance.
The default route is Zend_Controller_Router_Route_Module which uses default keys for module, controller, & action:
protected $_moduleKey = 'module';
protected $_controllerKey = 'controller';
protected $_actionKey = 'action';
// ...
/**
* Set request keys based on values in request object
*
* #return void
*/
protected function _setRequestKeys()
{
if (null !== $this->_request) {
$this->_moduleKey = $this->_request->getModuleKey();
$this->_controllerKey = $this->_request->getControllerKey();
$this->_actionKey = $this->_request->getActionKey();
}
if (null !== $this->_dispatcher) {
$this->_defaults += array(
$this->_controllerKey => $this->_dispatcher->getDefaultControllerName(),
$this->_actionKey => $this->_dispatcher->getDefaultAction(),
$this->_moduleKey => $this->_dispatcher->getDefaultModule()
);
}
$this->_keysSet = true;
}
/**
* Matches a user submitted path. Assigns and returns an array of variables
* on a successful match.
*
* If a request object is registered, it uses its setModuleName(),
* setControllerName(), and setActionName() accessors to set those values.
* Always returns the values as an array.
*
* #param string $path Path used to match against this routing map
* #return array An array of assigned values or a false on a mismatch
*/
public function match($path, $partial = false)
{
$this->_setRequestKeys();
$values = array();
$params = array();
if (!$partial) {
$path = trim($path, self::URI_DELIMITER);
} else {
$matchedPath = $path;
}
if ($path != '') {
$path = explode(self::URI_DELIMITER, $path);
if ($this->_dispatcher && $this->_dispatcher->isValidModule($path[0])) {
$values[$this->_moduleKey] = array_shift($path);
$this->_moduleValid = true;
}
if (count($path) && !empty($path[0])) {
$values[$this->_controllerKey] = array_shift($path);
}
if (count($path) && !empty($path[0])) {
$values[$this->_actionKey] = array_shift($path);
}
if ($numSegs = count($path)) {
for ($i = 0; $i < $numSegs; $i = $i + 2) {
$key = urldecode($path[$i]);
$val = isset($path[$i + 1]) ? urldecode($path[$i + 1]) : null;
$params[$key] = (isset($params[$key]) ? (array_merge((array) $params[$key], array($val))): $val);
}
}
}
if ($partial) {
$this->setMatchedPath($matchedPath);
}
$this->_values = $values + $params;
return $this->_values + $this->_defaults;
}
You can see that the default module route has default keys for mvc params, however, it will use the keys set by the request object if it exists and we can modify these keys.
e.g. in your bootstrap:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initRequestKeys()
{
$this->bootstrap('frontcontroller');
$frontController = $this->getResource('frontcontroller');
/* #var $frontController Zend_Controller_Front */
$request = new Zend_Controller_Request_Http();
// change action key
$request->setActionKey("new_action_key");
// change module
$request->setModuleKey("new_module_key");
// change controller
$request->setControllerKey("new_controller_key");
// don't forget to set the configured request
// object to the front controller
$frontController->setRequest($request);
}
}
Now you can use module, controller, & action as $_GET params.
After a little testing it seems that how you pass the key "action" matters.
If you try and pass a parameter named "action" with $this->_request->getParams() you will get the controller action value key pair.
If you pass the "action" key from a form with $form->getValues() you will retrieve the value from the form element named "action".
As with so many things, your use case determines how you need to handle the situation.
Good Luck.

Zend Form: Custom validator isvalid()

I have to create my custom FORM:isvalid() function in my form like below, because I have to check if one of these 2 fields are at least filled out:
class Products_AddForm extends Zend_Form {
public function isValid($data)
{
// Check special post data
$pzn_val = $data['PZN'];
$mar_val = $data['PZO'];
if(empty($pzn_val) && empty($mar_val)) {
$this->getSubForm('sub1')->getElement('PZN')->setErrors(array('PZN or PZO needed'));
$this->getSubForm('sub2')->getElement('PZO')->setErrors(array('PZN or PZO needed'));
}
// Standard validation
return parent::isValid($data);
}
The errors for PZN and PZO will only fire if another error (other field) will be found.
How can I get the form error?
Field PZN and PZO are defined as not required.
TIA
Matt
Try:
public function isValid($data) {
$isValid = parent::isValid($data);
// Check special post data
$pzn_val = $data['PZN'];
$mar_val = $data['PZO'];
if(empty($pzn_val) && empty($mar_val)) {
$this->getSubForm('sub1')->getElement('PZN')->setErrors(array('PZN or PZO needed'));
$this->getSubForm('sub2')->getElement('PZO')->setErrors(array('PZN or PZO needed'));
$isValid = false;
}
return $isValid;
}

Zend Framework view script not containing variables from parent class

I created a parent CRUD class for several controllers and when I render the script it isn't recognizing the paginator variable I set within the listAction(). The code is from my parent class. For instance, I extend Admin_UserController to create Webapp_Controller_Crud.
class Webapp_Controller_Crud extends Zend_Controller_Action
{
public function init()
{
$actionController = get_class($this);
$actionController = str_replace('Admin_',null,$actionController);
$actionController = str_replace('Controller',null,$actionController);
$this->_actionClassName = $actionController;
$actionController = 'Model_' . $this->_actionClassName;
$this->_actionModel = new $actionController();
}
/**
* #return Zend_Paginator
*/
public function getPaginator()
{
$model = $this->getActionModel();
$adapter = new Zend_Paginator_Adapter_DbSelect($model->select());
$paginator = new Zend_Paginator($adapter);
$paginator->setItemCountPerPage(10);
$page = $this->_request->getParam('page', 1);
$paginator->setCurrentPageNumber($page);
return $paginator;
}
public function listAction()
{
$this->view->paginator = $this->getPaginator();
}
}
You use
$this->getView()->paginator = $this->getPaginator();
Which should be
$this->view->paginator = $this->getPaginator();