How do I send variables to an error layout in ZF3? - zend-framework

What is right way to send variables to the layout templete for it be approachable in error pages?
I have AppFrontController above all my frontend controllers. It have code (code is near) in onDispatch() method:
$assocArrayOfVars = $this->MyPlugin()->getDbVariablesArray();
foreach($assocArrayOfVars as $name => $value){
$this->layout()->$name = $value;
}
list($catalog, $count_goods) = $this->MyPlugin()->getStandardCatalogDataForLayout();
$this->layout()->catalog = $catalog;
$this->layout()->count_goods = $count_goods;
As the result, I have my local variables in every frontend page. But I have’nt it in an error page. How I can to deside this problem? I very need your advices! Thank you!

Thank you for your advices! Problem is solved. Code of final version Module.php file below. I use listener instead of a “parent controller” by froschdesign advice.
public function onBootstrap(MvcEvent $event)
{
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$eventManager->attach('dispatch', array($this, 'loadConfiguration'), 2);
$eventManager->attach('dispatch.error', array($this, 'loadConfiguration'), 2);
}
public function loadConfiguration(MvcEvent $e)
{
$application = $e->getApplication();
$sm = $application->getServiceManager();
$sharedManager = $application->getEventManager()->getSharedManager();
$router = $sm->get('router');
$request = $sm->get('request');
$zendCart = $sm->get('ControllerPluginManager')->get('ZendCart');
$myPlugin = $sm->get('ControllerPluginManager')->get('MyPlugin');
$viewModel = $e->getViewModel();
$viewModel->setVariable('total', $zendCart->total());
$viewModel->setVariable('total_items', $zendCart->total_items());
$viewModel->setVariable('rusmonth', $rusmonth);
/* Layout variables */
$assocArrayOfVars = $myPlugin->getDbVariablesArray();
foreach ($assocArrayOfVars as $name => $value) {
$viewModel->setVariable($name, $value);
}
list($catalog, $count_goods) = $myPlugin->getStandardCatalogDataForLayout();
$viewModel->setVariable('catalog', $catalog);
$viewModel->setVariable('count_goods', $count_goods);
}
More listener examples here.

Related

output records in the form of html table from moodle database , custom block development

Please check this code for the custom block to be placed in the dashboard. We want to display an HTML table for the records. But it does not add up to the custom block, rather it appears on the top page.
enter image description here
class block_scorecard extends block_base {
function init() {
$this->title = get_string('pluginname', 'block_scorecard');
}
function get_content() {
global $DB;
if ($this->content !== NULL) {
return $this->content;
}
$content = '';
$courses = $DB->get_records('scorm_scoes_track', ['element' => 'cmi.core.total_time']);
$table=new html_table();
$table->head=array('No of attempts','Time modified','status');
foreach ($courses as $course) {
$attempt=$course->attempt;
$timemodified=userdate($course->timemodified, get_string('strftimerecentfull'));
$status=$course->value;
$table->data[]=array($attempt, $timemodified, $status);
}
echo html_writer::table($table);
$this->content = new stdClass;
$this->content->text = $content;
}}
echo html_writer::table($table);
Should be
$content .= html_writer::table($table);

Magento 2 How to add product to cart with textarea type custom option?

How to add product to cart with custom option type textarea? I tried the below code
$cart = $this->cart;
$params = array();
$options = array();
$params['qty'] = 1;
$params['product'] = $productId;
foreach ($product->getOptions() as $o) {
foreach ($o->getValues() as $value) {
$options[$value['option_id']] = $value['option_type_id'];
}
}
$params['options'] = $options;
$cart->addProduct($product, $params);
$cart->save();
But It return error while custom option type is field or area on line
foreach ($o->getValues() as $value)
It works fine with dropdown, radio, checkbox and multiselect.
Thanks in advance.

How to get typoscript setup in a scheduler/cron script?

I need to get the extension typoscript setup in schedular script.
I am using typo3 v 4.5.
My schedular script looks like this.
class tx_myext_scheduler extends tx_scheduler_Task {
public function execute() {
//here i need to get typoscript setup
}
}
and my extension setup looks like this.
plugin.tx_myext_pi1{
listView{
file.height = 216c
}
}
In schedualr script I need to get the file.height value.
How to do that ?
Currently i tried this without success
$pObj = $GLOBALS['TSFE'];
$conf = $pObj->tmpl->setup['plugin.']['tx_myext_pi1.'];
Thank you.
The TSFE is only available in the frontend, so have to initialize it yourself (that consumes some resources!). You can create it like that in scheduler: (source)
$GLOBALS['TT'] = new t3lib_timeTrackNull;
$GLOBALS['TSFE'] = t3lib_div::makeInstance('tslib_fe', $GLOBALS['TYPO3_CONF_VARS'], 2, 0);
$GLOBALS['TSFE']->sys_page = t3lib_div::makeInstance('t3lib_pageSelect');
$GLOBALS['TSFE']->sys_page->init(TRUE);
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->rootLine = '';
$GLOBALS['TSFE']->sys_page->getRootLine(1, '');
$GLOBALS['TSFE']->getConfigArray();
or in an eID script: (source)
require_once(PATH_tslib.'class.tslib_fe.php');
require_once(PATH_t3lib.'class.t3lib_page.php');
$temp_TSFEclassName = t3lib_div::makeInstanceClassName('tslib_fe');
$GLOBALS['TSFE'] = new $temp_TSFEclassName($TYPO3_CONF_VARS, $pid, 0, true);
$GLOBALS['TSFE']->connectToDB();
$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->determineId();
$GLOBALS['TSFE']->getCompressedTCarray();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->getConfigArray();
or in a backend module: (source)
function loadTypoScriptForBEModule($extKey) {
require_once(PATH_t3lib . 'class.t3lib_page.php');
require_once(PATH_t3lib . 'class.t3lib_tstemplate.php');
require_once(PATH_t3lib . 'class.t3lib_tsparser_ext.php');
list($page) = t3lib_BEfunc::getRecordsByField('pages', 'pid', 0);
$pageUid = intval($page['uid']);
$sysPageObj = t3lib_div::makeInstance('t3lib_pageSelect');
$rootLine = $sysPageObj->getRootLine($pageUid);
$TSObj = t3lib_div::makeInstance('t3lib_tsparser_ext');
$TSObj->tt_track = 0;
$TSObj->init();
$TSObj->runThroughTemplates($rootLine);
$TSObj->generateConfig();
return $TSObj->setup['plugin.'][$extKey . '.'];
}
If you have missing class errors somewhere, maybe you have to add some requires.
This solution is perfect if the page is in standard mode, but doesn't work if the page is a Draft:
function loadTypoScriptForBEModule($extKey) {
require_once(PATH_t3lib . 'class.t3lib_page.php');
require_once(PATH_t3lib . 'class.t3lib_tstemplate.php');
require_once(PATH_t3lib . 'class.t3lib_tsparser_ext.php');
list($page) = t3lib_BEfunc::getRecordsByField('pages', 'pid', 0);
$pageUid = intval($page['uid']);
$sysPageObj = t3lib_div::makeInstance('t3lib_pageSelect');
$rootLine = $sysPageObj->getRootLine($pageUid);
$TSObj = t3lib_div::makeInstance('t3lib_tsparser_ext');
$TSObj->tt_track = 0;
$TSObj->init();
$TSObj->runThroughTemplates($rootLine);
$TSObj->generateConfig();
return $TSObj->setup['plugin.'][$extKey . '.'];
}

Emails are not send to the user

I have a problem with sending emails with raports. I am create some data, and then try to send it as an email to the user. But emails are not deliwered. I am using swiftmailer configured as in the Symfony cookbool. Parameters for the swiftmailer are set properly because emails from FosUserBundle are working without problems. I have write a method to use it in comand line, the code is below
class DailyRaportCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('raport:daily')
->setDescription('Send daily raports to the Users');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine')->getEntityManager();
$users = $em->getRepository('GLUserBundle:User')->findAllByRaport('DAILY');
foreach($users as $user)
{
$date_to = new \DateTime();
$date_to = $date_to->sub(date_interval_create_from_date_string('1 day'));
$date_from = new \DateTime();
$date_from = $date_from->sub(date_interval_create_from_date_string('1 day'));
$format = "Y-m-d";
$raport = array();
foreach($user->getShops() as $shop)
{
$raport[$shop->getName()] = array();
$shop_list = array();
$shop_list[] = $shop->getId();
$groupBy = array();
$all_policies = $em->getRepository('GLPolicyBundle:Policy')
->findAllByOptions($shop_list, $date_from, $date_to, $groupBy);
$raport[$shop->getName()]['all'] = $all_policies;
$groupBy[] = 'typePolicy';
$policies_by_type = $em->getRepository('GLPolicyBundle:Policy')
->findAllByOptions($shop_list, $date_from, $date_to, $groupBy);
$raport[$shop->getName()]['type'] = $policies_by_type;
$groupBy[] = 'bundle';
$policies_by_bundle = $em->getRepository('GLPolicyBundle:Policy')
->findAllByOptions($shop_list, $date_from, $date_to, $groupBy);
$raport[$shop->getName()]['bundle'] = $policies_by_bundle;
}
$message = \Swift_Message::newInstance()
->setSubject('Dzienny raport sprzedaży')
->setFrom('g#######1#gmail.com')
->setTo($user->getEmail())
->setBody(
$this->getContainer()->get('templating')->render(
'GLRaportBundle:Raport:raport.html.twig',
array('raport' => $raport)
));
$this->getContainer()->get('mailer')->send($message);
}
The wiev of the email is rendered in outside twig file. I will be grateful if someone point whots the reason of this problem.
You might need to manually flush the memory spool, see the following answer which helped me with the same problem:
Unable to send e-mail from within custom Symfony2 command but can from elsewhere in app

Jomsocial Extra Field

I am trying to create extra fields on the Jomsocial Groups Create New Group page, its suggested on the Jomsocial docs that the best way is to creat a plugin to do this.
As I have never created such a complex plugin do anyone have a working example to start with?
Here is the code that I have tried with
<?php
defined('_JEXEC') or die('Restricted access');
if (! class_exists ( 'plgSpbgrouppostcode' )) {
class plgSpbgrouppostcode extends JPlugin {
/**
* Method construct
*/
function plgSystemExample($subject, $config) {
parent::__construct($subject, $config);
// JPlugin::loadLanguage ( 'plg_system_example', JPATH_ADMINISTRATOR ); // only use if theres any language file
include_once( JPATH_ROOT .'/components/com_community/libraries/core.php' ); // loading the core library now
}
function onFormDisplay( $form_name )
{
/*
Add additional form elements at the bottom privacy page
*/
$elements = array();
if( $form_name == 'jsform-groups-forms' )
{
$obj = new CFormElement();
$obj->label = 'Labe1 1';
$obj->position = 'after';
$obj->html = '<input name="custom1" type="text">';
$elements[] = $obj;
$obj = new CFormElement();
$obj->label = 'Labe1 2';
$obj->position = 'after';
$obj->html = '<input name="custom2" type="text">';
$elements[] = $obj;
}
return $elements;