Where should I locate logic related to layout in Zend Framework? - zend-framework

I need to customize the attributes of my body tag. Where should I locate the logic? In a Base Controller, view Helper ?
This should be the layout
<?=$this->doctype() ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...
</head>
<body<?=$this->bodyAttrs?>> <!-- or <?=$this->bodyAttrs()?> -->
...
</body>
</html>
And this should be the variables declaration in controllers
class Applicant_HomeController extends Zend_Controller_Action
{
public function indexAction()
{
$this->idBody = "someId1";
$this->classesBody = array("wide","dark");
}
public function loginAction()
{
$this->idBody = "someId2";
$this->classesBody = array();
}
public function signUpAction()
{
$this->idBody = "someId3";
$this->classesBody = array("no-menu","narrow");
}
}
This is the function where the attributes are concatenated.
/**
* #param string $idBody id Attribute
* #param array $classesBody class Attribute (array of strings)
*/
protected function _makeBodyAttribs($idBody,$classesBody)
{
$id = isset($idBody)?' id="'.$idBody.'"':'';
$hasClasses = isset($classesBody)&&count($classesBody);
$class = $hasClasses?' class="'.implode(' ',$classesBody).'"':'';
return $id.$class;
}
I need the last glue code.

Got one better for ya:
<?php
class My_View_Helper_Attribs extends Zend_View_Helper_HtmlElement
{
public function attribs($attribs) {
if (!is_array($attribs)) {
return '';
}
//flatten the array for multiple values
$attribs = array_map(function($item) {
if (is_array($item) {
return implode(' ', $item)
}
return $item;
}, $attribs);
//the htmlelemnt has the build in function for the rest
return $this->_htmlAttribs($attribs)
}
}
in your controller:
public function indexAction()
{
//notice it is $this->view and not just $this
$this->view->bodyAttribs= array('id' => 'someId', 'class' => array("wide","dark"));
}
public function loginAction()
{
$this->view->bodyAttribs['id'] = "someId2";
$this->view->bodyAttribs['class'] = array();
}
in your view script:
<body <?= $this->attribs($this->bodyAtrribs) ?>>

Related

TYPO3 Repository query returns uid and pid but no other fields

I'm doing a query on a really simple table in a typo 3 task. However, only the fields "uid" and "pid" are returned, the other fields are NULL.
My Entity:
<?php
namespace Name\SampleExtension\Domain\Model;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
class MailAgent extends AbstractEntity
{
/**
* #var integer
*/
protected $uid;
/**
* #var string
*/
protected $customeremail;
/**
* #var string
*/
protected $searchparameters;
/**
* #var string
*/
protected $resultlist;
public function getUid()
{
return $this->uid;
}
public function setCustomerEmail($customeremail)
{
$this->customeremail = $customeremail;
}
public function getCustomerEmail()
{
return $this->customeremail;
}
public function setSearchParameters($searchparameters)
{
$this->searchparameters = $searchparameters;
}
public function getSearchParameters()
{
return $this->searchparameters;
}
public function setResultList($resultlist)
{
$this->resultlist = $resultlist;
}
public function getResultList()
{
return $this->resultlist;
}
}
?>
The Repository:
<?php
namespace Name\SampleExtension\Domain\Repository;
use TYPO3\CMS\Extbase\Persistence\Repository;
class MailAgentRepository extends Repository
{
public function findByUids($uids)
{
$query = $this->createQuery();
foreach ($uids as $uid) {
$constraints[] = $query->equals('uid', $uid);
}
return $query->matching(
$query->logicalOr(
$constraints
)
)->execute();
}
}
?>
And the query inside the task:
<?php
namespace Name\SampleExtension\Task;
use TYPO3\CMS\Scheduler\Task\AbstractTask;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use Name\SampleExtension\Domain\Model\MailAgent;
use Name\SampleExtension\Domain\Repository\MailAgentRepository;
class MailAgentCheckup extends AbstractTask
{
public function execute() {
$objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$this->MailAgentRepository = $objectManager->get(MailAgentRepository::class);
$query = $this->MailAgentRepository->createQuery();
$allCustomers = $this->MailAgentRepository->findAll();
foreach ($allCustomers as $customer) {
var_dump($customer);
}
return true;
}
}
?>
I have no idea why the other fields are not returned, but the uid and the pid are. My guess is that I need to declare the mapping somewhere else.
EDIT: Heres the content of my TCA, which is probably wrong or not enough, but since I'm working on a existing extension I was copying from the TCA's of the tables that work.
tx_sampleextension_domain_model_mailagent.php
return [
'columns' => [
'uid' => [],
'customer_email' => [],
'search_parameters' => [],
'result_list' => [],
],
'types' => [],
];
This is from another table for which querys etc work
return [
'columns' => [
'immovable' => [],
'type' => [],
'title' => [],
'path' => [],
'mark_to_delete' => [],
],
];
Give a try to inject your repository
<?php
namespace Name\SampleExtension\Task;
use TYPO3\CMS\Scheduler\Task\AbstractTask;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
use Name\SampleExtension\Domain\Model\MailAgent;
use Name\SampleExtension\Domain\Repository\MailAgentRepository;
use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
class MailAgentCheckup extends AbstractTask
{
/**
* mailAgentRepository
*
* #var \Name\SampleExtension\Domain\Repository\MailAgentRepository
* #inject
*/
protected $mailAgentRepository = NULL;
public function injectMailAgentRepository(\Name\SampleExtension\Domain\Repository\MailAgentRepository $mailAgentRepository) {
$this->mailAgentRepository = $mailAgentRepository;
}
public function execute() {
$allCustomers = $this->mailAgentRepository->findAll();
DebuggerUtility::var_dump($allCustomers);exit;
// OR
$arguments = $this->request->getArguments();
$uid = $arguments['uid'];
$singleCustomer = $this->mailAgentRepository->findByUid(intval($uid));
DebuggerUtility::var_dump($singleCustomer);exit;
/*foreach ($allCustomers as $customer) {
var_dump($customer);
}*/
return true;
}
}
?>
I was missing the TCA file for the table. After adding it and declaring all the columns in there, the extbase domain object variables got filled.

Zend variable not accessible in view

I have my own abstract class that extends Zend_Controller_Action and all my controllers then extend this class. Here is my abstract class:
<?php
abstract class CLG_Controller_Action extends Zend_Controller_Action
{
public $admin;
public $staff;
public $pool;
public $it;
//public $staff;
/**
*
* #var HTMLPurifier
*/
public $purifier;
public $action;
public $controller;
public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array())
{
parent::__construct($request, $response, $invokeArgs);
if( Zend_Registry::isRegistered('admin') ) {
$this->admin = Zend_Registry::get('admin');
}
if( Zend_Registry::isRegistered('staff') ) {
$this->staff = Zend_Registry::get('staff');
}
if( Zend_Registry::isRegistered('pool') ) {
$this->pool = Zend_Registry::get('pool');
}
$this->purifier = Zend_Registry::get('purifier');
$this->controller = $this->getRequest()->getControllerName();
$this->action = $this->getRequest()->getActionName();
$this->registerViewObjects();
}
public function postDispatch()
{
/************************************************
* Prepare JS and CSS FILES FOR THIS REQUEST
************************************************/
$action = $this->_request->getActionName();
$controller = $this->_request->getControllerName();
$this->view->headScript()->appendFile('/js/jquery-2.0.2.min.js');
if (key_exists ( $this->_request->getActionName (), $this->assets ))
{
$action = $this->_request->getActionName ();
foreach ( $this->assets [$action] ['css'] as $css )
{
$this->view->headLink()->appendStylesheet ( $css , 'print');
}
foreach ( $this->assets [$action] ['js'] as $js )
{
$this->view->headScript()->appendFile( $js );
}
}
$css = '/css/' . $controller . '/' . $action . '.css';
$js = '/js/' . $controller . '/' . $action . '.js';
$this->view->headLink()->appendStylesheet ( $css , 'print');
$this->view->headScript()->appendFile( $js );
}
private function registerViewObjects()
{
// THESE ARE ALWAYS AVAILABLE IN THE VIEW
$this->view->admin = $this->admin;
$this->view->staff = $this->staff;
$this->view->pool = $this->pool;
$this->view->controller = $this->controller;
$this->view->action = $this->action;
$this->view->purifier = $this->purifier;
}
}
However, for some reason, the variables registered in the registerViewObjects() are not accessible in my view files.
What am I missing here?
Thanks
UPDATE:
I should say that I have another class ActionMenu that extends Action, and my controllers then extend that class!
Is there a reason you are using __construct over init()? I'm pretty sure this is the reason of your problem because Zend performs various actions regarding the request, action etc in the __construct() stage.
/**
* #return void
*/
public function init()
{
if( Zend_Registry::isRegistered('admin') ) {
$this->admin = Zend_Registry::get('admin');
}
if( Zend_Registry::isRegistered('staff') ) {
$this->staff = Zend_Registry::get('staff');
}
if( Zend_Registry::isRegistered('pool') ) {
$this->pool = Zend_Registry::get('pool');
}
$this->purifier = Zend_Registry::get('purifier');
$this->controller = $this->getRequest()->getControllerName();
$this->action = $this->getRequest()->getActionName();
$this->registerViewObjects();
}
See also:
http://framework.zend.com/manual/1.12/en/zend.controller.action.html#zend.controller.action.initialization
Seeing as you're attempting to use the $view property so early in the controller lifecycle, maybe you just need to initialise it before putting values in, eg
private function registerViewObjects() {
$this->initView();
// and the rest
See http://framework.zend.com/manual/1.12/en/zend.controller.action.html#zend.controller.action.viewintegration

Symfony2: Delete entity through post request (improvements required)

I need some improvements about my actual way to delete entities:
public function deleteAction($path)
{
$form = $this->createFormBuilder(array('path' => $path))
->add('path')
->setReadOnly(true)
->getForm();
if ($this->getRequest()->getMethod() === 'POST') {
$form->bindRequest($this->getRequest());
if ($form->isValid()) {
$image = $this->getImageManager()->findImageByPath($path);
$this->getImageManager()->deleteImage($image);
return $this->redirect($this->generateUrl('AcmeImageBundle_Image_index'));
}
}
return $this->render('AcmeImageBundle:Image:delete.html.twig', array(
'form' => $form->createView(),
));
}
Two improvements I already found while writting:
CreateFormBuilder in extra method in controller
Hidden field and overgive extra image-entity to get rendered
Are there other thing I could make better?
Regards
(my answer is too long for the comment so i add it here)
First you have to create a Type file (generally in YourApp\YourBundle\Form\yourHandler.php), some basique code to put inside if you don't know:
<?php
namespace ***\****Bundle\Form;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManager;
use ***\****Bundle\Entity\your_entity;
class *****Handler
{
protected $form;
protected $request;
protected $em;
public function __construct(Form $form, Request $request, EntityManager $em)
{
$this->form = $form;
$this->request = $request;
$this->em = $em;
}
public function process()
{
if( $this->request->getMethod() == 'POST' )
{
$this->form->bindRequest($this->request);
if( $this->form->isValid() )
{
$this->onSuccess($this->form->getData());
return true;
}
}
return false;
}
public function onSuccess(your_entity $object)
{
// Make your stuff here (remove,....)
}
}
And in your controller i just call it this way:
if (!empty($_POST))
{
$formHandler = new *****Handler($my_form, $this->get('request'), $this->getDoctrine()->getEntityManager());
$formHandler->process();
}
Hope i'm clear enough

Appending scripts from view helper method not working in zend framework

I have a view helper method which is like this
class Zend_View_Helper_LoginForm extends Zend_View_Helper_Abstract
{
public function loginForm()
{
$script = "<script type='text/javascript'>(function (){ $('#submit').click(function (){alert('hello'); return false;})})</script>";
$this->view->headScript()->appendScript($script, $type = 'text/javascript');
$login = new Application_Form_User();
return $login;
}
}
But this is not working. I also tried
$this->view->headScript()->appendFile($this->view->baseUrl('/js/jquery.js'), 'text/javascript');
but this is not working either. If i try this code in layout.phtml then it works.Any Idea?
In view file:
<?php $this->headScript()->appendFile('your/sript/file.js') ?>
In your layout:
<?php echo $this->headScript() ?>
You have to add setView method:
class My_View_Helper_ScriptPath
{
public $view;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
public function scriptPath($script)
{
return $this->view->getScriptPath($script);
}
}

Problem With View Helpers

I wrote few custom view helpers but I have a little trouble using them. If I add the helper path in controller action like this:
public function fooAction()
{
$this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
}
Then I can use the views from that path without a problem. But when I add the path in the bootstrap file like this:
protected function _initView()
{
$this->view = new Zend_View();
$this->view->doctype('XHTML1_STRICT');
$this->view->headScript()->appendFile($this->view->baseUrl()
. '/js/jquery-ui/jquery.js');
$this->view->headMeta()->appendHttpEquiv('Content-Type',
'text/html; charset=UTF-8');
$this->view->headMeta()->appendHttpEquiv('Content-Style-Type',
'text/css');
$this->view->headMeta()->appendHttpEquiv('Content-Language', 'sk');
$this->view->headLink()->appendStylesheet($this->view->baseUrl()
. '/css/reset.css');
$this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
}
Then the view helpers don't work. Why is that? It's too troublesome to add the path in every controller action. Here is an example of how my custom view helpers look:
class My_View_Helper_FooBar
{
public function fooBar() {
return 'hello world';
}
}
I use them like this in views:
<?php echo $this->fooBar(); ?>
Should I post my whole bootstrap file?
UPDATE:
Added complete bootstrap file just in case:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initFrontController()
{
$this->frontController = Zend_Controller_Front::getInstance();
$this->frontController->addModuleDirectory(APPLICATION_PATH
. '/modules');
Zend_Controller_Action_HelperBroker::addPath(
'My/Controller/Action/Helper',
'My_Controller_Action_Helper'
);
$this->frontController->registerPlugin(new My_Controller_Plugin_Auth());
$this->frontController->setBaseUrl('/');
}
protected function _initView()
{
$this->view = new Zend_View();
$this->view->doctype('XHTML1_STRICT');
$this->view->headScript()->appendFile($this->view->baseUrl()
. '/js/jquery-ui/jquery.js');
$this->view->headMeta()->appendHttpEquiv('Content-Type',
'text/html; charset=UTF-8');
$this->view->headMeta()->appendHttpEquiv('Content-Style-Type',
'text/css');
$this->view->headMeta()->appendHttpEquiv('Content-Language', 'sk');
$this->view->headLink()->appendStylesheet($this->view->baseUrl()
. '/css/reset.css');
$this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
}
protected function _initDb()
{
$this->configuration = new Zend_Config_Ini(APPLICATION_PATH
. '/configs/application.ini',
APPLICATION_ENVIRONMENT);
$this->dbAdapter = Zend_Db::factory($this->configuration->database);
Zend_Db_Table_Abstract::setDefaultAdapter($this->dbAdapter);
$stmt = new Zend_Db_Statement_Pdo($this->dbAdapter,
"SET NAMES 'utf8'");
$stmt->execute();
}
protected function _initAuth()
{
$this->auth = Zend_Auth::getInstance();
}
protected function _initCache()
{
$frontend= array('lifetime' => 7200,
'automatic_serialization' => true);
$backend= array('cache_dir' => 'cache');
$this->cache = Zend_Cache::factory('core',
'File',
$frontend,
$backend);
}
public function _initTranslate()
{
$this->translate = new Zend_Translate('Array',
BASE_PATH . '/languages/Slovak.php',
'sk_SK');
$this->translate->setLocale('sk_SK');
}
protected function _initRegistry()
{
$this->registry = Zend_Registry::getInstance();
$this->registry->configuration = $this->configuration;
$this->registry->dbAdapter = $this->dbAdapter;
$this->registry->auth = $this->auth;
$this->registry->cache = $this->cache;
$this->registry->Zend_Translate = $this->translate;
}
protected function _initUnset()
{
unset($this->frontController,
$this->view,
$this->configuration,
$this->dbAdapter,
$this->auth,
$this->cache,
$this->translate,
$this->registry);
}
protected function _initGetRidOfMagicQuotes()
{
if (get_magic_quotes_gpc()) {
function stripslashes_deep($value) {
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
$_POST = array_map('stripslashes_deep', $_POST);
$_GET = array_map('stripslashes_deep', $_GET);
$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
}
public function run()
{
$frontController = Zend_Controller_Front::getInstance();
$frontController->dispatch();
}
}
Solved. I just needed to add these lines at the end of _initView() method:
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
$viewRenderer->setView($this->view);
I in my _initView() have something like this:
protected function _initView() {
$view = new Zend_View();
#some view initialization ...
$view->addHelperPath(APPLICATION_PATH . '/views/helpers', 'My_View_Helper');
return $view;
}
Then in a view I can execute:
<?php echo $this->fooBar(); ?>
Without APPLICATION_PATH it does not work in my case.
Just a thought: are you sure that the view that you are creating in your bootstrap ($this->view = new Zend_View();) is the same as '$this' in your view file?
I think you would need to change your initView code to the following:
protected function _initView()
{
$view = new Zend_View();
$view->doctype('XHTML1_STRICT');
$view->headScript()->appendFile($this->view->baseUrl()
. '/js/jquery-ui/jquery.js');
$view->headMeta()->appendHttpEquiv('Content-Type',
'text/html; charset=UTF-8');
$view->headMeta()->appendHttpEquiv('Content-Style-Type',
'text/css');
$view->headMeta()->appendHttpEquiv('Content-Language', 'sk');
$view->headLink()->appendStylesheet($this->view->baseUrl()
. '/css/reset.css');
$view->addHelperPath('My/View/Helper', 'My_View_Helper');
return $view;
}
If you have some View related settings in your config.ini file, you might want to change your code a little bit:
protected function _initMyView()
{
$view = $this->bootstrap('view')->getResource('view');
...
instead of:
protected function _initView()
{
$view = new Zend_View();
....
You might consider adding another init function just for your view helpers:
protected function _initViewHelpers()
{
$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath('My/View/Helper', 'My_View_Helper');
}
This way the built in view setup is not overridden.
If you add only $this->view->addHelperPath('My/View/Helper', 'My_View_Helper');
in your bootstrap use this format:
class Zend_View_Helper_FooBar extends Zend_View_Helper_Abstract {
public function fooBar() {
return 'hello world';
}
}