Zend framework: using BreadCrumbs with partial - zend-framework

I have some trouble making a customized breadcrumb:
I my layout.phtml I have:
$container = new Zend_Navigation();
$this->navigation($container);
$container->addPage(
array(
'label' => 'Dashboard',
'module' => 'default',
'controller' => 'dashboard',
'action' => 'index',
'pages' =>
array(
array(
'label' => 'Create Order',
'module' => 'default',
'controller' => 'createorder',
'action' => 'index'
),
array(
'label' => 'Query',
'module' => 'default',
'controller' => 'query',
'action' => 'index',
'pages' => array(
array(
'label' => 'View Order',
'module' => 'default',
'controller' => 'order',
'action' => 'vieworder'
)
)
),
array(
'label' => 'Administration',
'module' => 'default',
'controller' => 'admin',
'action' => 'index',
'pages' =>
array(
array(
'label' => 'News and Announcements',
'module' => 'default',
'controller' => 'admin',
'action' => 'addnews',
'pages' => array(
array(
'label' => 'Edit News and Announcements',
'module' => 'default',
'controller' => 'admin',
'action' => 'editnews'
)
)
)
)
)
)
)
);
The next line is:
echo $this->navigation()->breadcrumbs()->setPartial(array('BreadCrumb.phtml','default'));
The BreadCrumb.phtml is called well, but I don't know how to make an ul-li menu in my BreadCrumb.phtml. How do I get the navigation I'm actually in?
Thanks in advance for any help. Andrea

To do so, your BreadCrumb.phtml should look like this:
<?php
if (null === $this->container) {
$this->container = $this->breadcrumbs()->getContainer();
}
// find deepest active
if (!$active = $this->breadcrumbs()->findActive($this->container)) {
return '';
}
$active = $active['page'];
// put the deepest active page last in breadcrumbs
if ($this->breadcrumbs()->getLinkLast()) {
$html = ' <li>' . $this->breadcrumbs()->htmlify($active) . '</li>' . PHP_EOL;
} else {
$html = $active->getLabel();
if ($this->breadcrumbs()->getUseTranslator() && $t = $this->breadcrumbs()->getTranslator()) {
$html = $t->translate($html);
}
$html = ' <li>' . $this->escape($html) . '</li>' . PHP_EOL;
}
// walk back to root
while (($parent = $active->getParent()) != null) {
if ($parent instanceof Zend_Navigation_Page) {
// prepend crumb to html
$html = ' <li>' . $this->breadcrumbs()->htmlify($parent) . '</li>' . PHP_EOL . $html;
}
if ($parent === $this->container) {
// at the root of the given container
break;
}
$active = $parent;
}
echo strlen($html) ? $this->breadcrumbs()->getIndent() . '<ul id="breadcrumbs">' . PHP_EOL
. $html . '</ul>' . PHP_EOL : '';
?>
Then you will get something like this:
<ul id="breadcrumbs">
<li>Home</li>
<li>Articles</li>
<li>Shop</li>
<li>Last link</li>
</ul>

Inside the view script, the zend navigation container is retrievable via $this->container. The container is iterable, so you can walk through it with a simple foreach loop (e.g. foreach($this->container as $page)

I've found simple solution for render breadcrumbs wrapped in ul-li menu.
You can place this code in your breadcrumbs.phtml:
<?php $breadcrumbs = $this->navigation()->breadcrumbs()->setMinDepth(0); ?>
<?php $items = array_filter(explode($breadcrumbs->getSeparator(), $breadcrumbs->render())); ?>
<?php if ($items) : ?>
<ul class="breadcrumbs">
<?php foreach ($items as $item) : ?>
<li><?= $item ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

Related

update_post_meta for properties visibility?

I'm trying to find how can I display the list of imported properties. The problem is the metadata, after import the properties are not visible on the frontend and I need to update every item manually. How can I update metadata editing a following code:
<?php
$args = array(
'posts_per_page' => $custom_property_items_amount,
'post_type' => 'property',
'orderby' => array(
'menu_order'=>'ASC',
'date' =>'DESC',
),
'offset' => ( max( 1, get_query_var( 'paged' ) ) - 1 ) * $custom_property_items_amount,
'ignore_sticky_posts' => 1,
'post_status' => array('publish','pending','draft','future','private'),
);
$data = new WP_Query( $args );
?>
<div class="<?php echo join( ' ', $wrapper_classes ) ?>">
<?php if ( $data->have_posts() ) :
while ( $data->have_posts() ): $data->the_post(); ?>
<?php ere_get_template( 'content-property.php', array(
'custom_property_image_size' => $custom_property_image_size,
'property_item_class' => $property_item_class
)); ?>
<?php endwhile;
else: ?>
<div class="item-not-found"><?php esc_html_e( 'Not found', 'essential-real-estate' ); ?></div>
<?php endif; ?>
<div class="clearfix"></div>
<?php
$max_num_pages = $data->max_num_pages;
ere_get_template( 'global/pagination.php', array( 'max_num_pages' => $max_num_pages ) );
wp_reset_postdata(); ?>
</div>
Solved! The problem was in this part of the code:
if (!empty($features)) {
foreach($features as $feature){
$tax_query[] = array(
'taxonomy' => 'property-feature',
'field' => 'slug',
'terms' => $feature
);
$parameters.=sprintf( __('Feature: <strong>%s</strong>; ', 'essential-real-estate'), $feature);
}
}
$args['meta_query'] = array(
'relation' => 'AND',
$meta_query
);
$tax_count = count($tax_query);
if ($tax_count > 0) {
$args['tax_query'] = array(
'relation' => 'AND',
$tax_query
);
}
By deleting this all the items were displayed.

Symfony - two forms activate separately

I wrote a function that has two forms that are activate separately, persisting same Entity. First is activated for input field and second for random generated field.
But when I click submit it activates both forms one after other. If used conditions to prevent that but I seems that it doesn't work.
My code:
$id = $request->get('id');
$user = $this->container->get('account')->getUserRepository()->find($id);
$form1 = $this->createFormBuilder()
->add('password', PasswordType::class, array(
'label' => 'Enter New Password',
'attr' => ['class'=>'form-control']))
->add('save', SubmitType::class, array(
'label' => 'Send', 'attr' => ['class' => 'btn btn-primary action-save']
))
->getForm();
$form2 = $this->createFormBuilder()
->add('password', PasswordType::class, array(
'label' => 'Generate New Password',
'disabled'=> true,
'attr' => ['class'=>'form-control']))
->add('save', SubmitType::class, array(
'label' => 'Send',
'attr' => ['class' => 'btn btn-primary action-save']
))
->getForm();
$form1->handleRequest($request);
if($form1->isSubmitted() && $form1->isValid()) {
$this->addFlash(
'notice',
'You successfully changed the password!'
);
$data = $form1->getData();
$new_password = $data['password'];
$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
$new_pwd_encoded = $encoder->encodePassword($new_password);
$oneTimePsw = '';
$user->setPassword($new_pwd_encoded);
$manager = $this->getDoctrine()->getManager();
$manager->flush();
}
$form2->handleRequest($request);
if($form2->isSubmitted() && $form2->isValid()) {
$this->addFlash(
'notice',
'Password is successfully generated!'
);
$data = $form2->getData();
$new_password = $data['password'];);
$new = $this->get('member.account')->generateRandomPassword();
$oneTimePsw = '';
$user->setPassword($new);
$manager = $this->getDoctrine()->getManager();
$manager->flush();
}
return $this->render('#AdminTemplates/admin/reset_password.html.twig', array(
'form1' => $form1->createView(),
'form2' => $form2->createView()
));
My twig
<div id="setPassword" style="display:none;">
{{ form_start(form1) }}
{{ form_end(form1) }}
</div>
<div id="generatePassword" style="display:none;">
{{ form_start(form2) }}
{{ form_end(form2) }}
</div>
I think your problem is a simple if else anidation problem, when i had the same issue this is what i did:
$id = $request->get('id');
$user = $this->container->get('account')->getUserRepository()->find($id);
$form1 = $this->createFormBuilder()
->add('password', PasswordType::class, array(
'label' => 'Enter New Password',
'attr' => ['class'=>'form-control']))
->add('save', SubmitType::class, array(
'label' => 'Send', 'attr' => ['class' => 'btn btn-primary action-save']
))
->getForm();
$form2 = $this->createFormBuilder()
->add('password', PasswordType::class, array(
'label' => 'Generate New Password',
'disabled'=> true,
'attr' => ['class'=>'form-control']))
->add('save', SubmitType::class, array(
'label' => 'Send',
'attr' => ['class' => 'btn btn-primary action-save']
))
->getForm();
$form1->handleRequest($request);
$form2->handleRequest($request);
if($form1->isSubmitted() && $form1->isValid()) {
$this->addFlash(
'notice',
'You successfully changed the password!'
);
$data = $form1->getData();
$new_password = $data['password'];
$encoder = $this->container->get('security.encoder_factory')->getEncoder($user);
$new_pwd_encoded = $encoder->encodePassword($new_password);
$oneTimePsw = '';
$user->setPassword($new_pwd_encoded);
$manager = $this->getDoctrine()->getManager();
$manager->flush();
}else
if($form2->isSubmitted() && $form2->isValid()) {
$this->addFlash(
'notice',
'Password is successfully generated!'
);
$data = $form2->getData();
$new_password = $data['password'];);
$new = $this->get('member.account')->generateRandomPassword();
$oneTimePsw = '';
$user->setPassword($new);
$manager = $this->getDoctrine()->getManager();
$manager->flush();
}
return $this->render('#AdminTemplates/admin/reset_password.html.twig', array(
'form1' => $form1->createView(),
'form2' => $form2->createView()
));
This should work, because a user can't submit 2 forms at once. I also had a problem with the isValid() method so also try for testing taking it down.

$this->getRequest()->getPost() return empty array in magento back end form submission

I am creating a magento custom admin module and a form. I want update this form but not updating. In Controller, under SaveAction() I print $this->getRequest()->getPost() and get empty array. please help me. Below code for form declination..
protected function _prepareForm() {
$form = new Varien_Data_Form(array(
'id' => 'edit_form1',
'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
'method' => 'post',
'enctype' => 'multipart/form-data'
)
);
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
And Create a from filed set like
protected function _prepareForm() {
$form = new Varien_Data_Form();
$this->setForm($form);
$fieldset = $form->addFieldset('qbanner_form', array('legend' => Mage::helper('qbanner')->__('Art information')));
$fieldset->addField('name', 'text', array(
'label' => Mage::helper('catalog')->__('Product'),
'required' => false,
'name' => 'name',
));
$fieldset->addField('artist_name', 'text', array(
'label' => Mage::helper('catalog')->__('Artist Name'),
// 'name' => 'artist_name',
'value' => Mage::helper('catalog')->__('Art Name value'),
));
$fieldset->addField('bca_status', 'select', array(
'label' => Mage::helper('catalog')->__('Art status'),
'name' => 'bca_status',
'values' =>$this->_getAttributeOptions('bca_status'),
));
$fieldset->addField('reason', 'editor', array(
'name' => 'reason',
'label' => Mage::helper('catalog')->__('Reason'),
'title' => Mage::helper('catalog')->__('Reason'),
'style' => 'width:440px; height:300px;',
'wysiwyg' => true,
'required' => false,
));
$fieldset->addField('thumbnail', 'text', array(
'label' => Mage::helper('catalog')->__('Art status'),
'name' => 'thumbnail',
//'values' =>$this->_getAttributeOptions('thumbnail'),
//'renderer' => 'Qaz_Qbanner_Block_Adminhtml_Qbanner_Grid_Renderer_Image'
));
if (Mage::getSingleton('adminhtml/session')->getQbannerData()) {
$form->setValues(Mage::getSingleton('adminhtml/session')->getQbannerData());
Mage::getSingleton('adminhtml/session')->setQbannerData(null);
} elseif (Mage::registry('qbanner_data')) {
$form->setValues(Mage::registry('qbanner_data')->getData());
}
return parent::_prepareForm();
}
protected function _getAttributeOptions($attribute_code)
{
$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', $attribute_code);
$options = array();
foreach( $attribute->getSource()->getAllOptions(true, true) as $option ) {
$options[$option['value']] = $option['label'];
}
return $options;
}
Here my
SaveAction()
public function saveAction() {
echo print_r( $this->getRequest()->getPost());
}
I have tied verious post. Any ideas?
Common error for all. You just need to add form key to your form.
Just add this line below your form declaration.
<input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>" />
Like this
<form action="<?php echo Mage::helper("adminhtml")->getUrl("demo/adminhtml_demo/demo");?>" method="post" id="custom-payment-form" enctype="multipart/form-data">
<input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>" />
Add this. Now you can get parameters by $this->getRequest()->getPost().
you can get variable of post and get method in magento with $this->getRequest()->getParams(); getParams() method But if you want to get exactly some variable data then use getParam('id');
/magento/catalog/product/view/id/406/category/14
$this->getRequest()->getParam('id') // 406
$this->getRequest()->getParams(); //get all get and post variables

ZendFramework2 - Literal- or Segment-Routing and Parameters

I am using ZendFramework 2.x and trying to add a route to some existing ones. Moreover I want to put some parameters in die URL as well. If I use the segment-type for my new route ('showroom') I am able to call my new URL and will get forwarded to the corresponding view. Unfortunately I am not able to set some parameters in the URL. The other option is to use segment type in my module.config.php-file, but I will get some ZF-2-Exception, that my route is not configured correctly, this happens even before rendering my intro-view. Thanks in advance for showing me, how to combine segment and literal-type usage in route-child-route-combination or for telling me how to add parameters to literal type URLs.
Route-configuration in module.config.php of Module:
'router' => array(
'routes' => array(
'zfcuser' => array(
'type' => 'Literal',
'priority' => 1000,
'options' => array(
'route' => '/user',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'showroom' => array(
'type' => 'Segment',
'options' => array(
'route' => '/:id',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'showRoom',
'id' => '1',
),
),
),
'login' => array(
'type' => 'Literal',
UPDATE: snippet which triggers the error (see comments)
<?php
$roomIndex = 1;
foreach($roomsPaginator->getCurrentItems() as $room){
$roomURL = 'zfcuser/showroom' . '/' . $roomIndex;
echo "<p>Name: " . $room['name'] . "; Luftfeuchtigkeit: " . $room['humidity'] . "; <a class='btn' href='" . $this->url($roomURL) . "'>Betreten »</a></p>";
$roomIndex++;
}
?>
I'm not sure if this is the cause of your error, but I don't think you're using the URL helper correctly. You don't build a URL and pass it to the helper, the idea is the helper builds the URL for you using the route name and params. You probably want something like this:
$roomIndex = 1;
foreach($roomsPaginator->getCurrentItems() as $room){
echo "<p>Name: " . $room['name'] . "; Luftfeuchtigkeit: " . $room['humidity'] . "; <a class='btn' href='" . $this->url('showroom', array('id' => $roomIndex)) . "'>Betreten »</a></p>";
$roomIndex++;
}

ZendFramework - Zend_Navigation. Find* methods removes pages from container object

When using findAll method on Zend_Navigation_Container pages, that are found, are being removed from container object.
Same thing happens, even when I clone container object and findAll is being called on this cloned one.
I can't figure how is it happens.
However, i've noticed that it only happens, when i try to "find" pages on the second level (or deeper)
Same problem seems to be described here, no answer was given...
Here is some code, to reproduce problem:
1. First method, no cloning, any page that is "found" is removed from container
<?php
$container = new Zend_Navigation(array(
array(
'label' => 'Page 1',
'route' => 'default',
'pages' => array(
array(
'label' => 'Page 1.1',
'route' => 'default',
),
)
),
array(
'label' => 'Page 2',
'route' => 'default',
),
array(
'label' => 'Page 3',
'route' => 'default',
)
));
echo $this->navigation($container)->menu()->renderMenu($container);
echo '<br /><hr /><br />';
echo $this->navigation($container)->menu()->renderMenu(new Zend_Navigation($container->findAllByLabel('Page 2')));
echo '<br /><hr /><br />';
echo $this->navigation($container)->menu()->renderMenu($container);
?>
2. Second try, cloning, page on second level (and deeper) that is "found" is removed from container
<?php
$container = new Zend_Navigation(array(
array(
'label' => 'Page 1',
'route' => 'default',
'pages' => array(
array(
'label' => 'Page 1.1',
'route' => 'default',
),
)
),
array(
'label' => 'Page 2',
'route' => 'default',
),
array(
'label' => 'Page 3',
'route' => 'default',
)
));
$container1 = clone $container;
$container2 = clone $container;
$container3 = clone $container;
echo $this->navigation($container1)->menu()->renderMenu($container1);
echo '<br /><hr /><br />';
echo $this->navigation($container2)->menu()->renderMenu(new Zend_Navigation($container2->findAllByLabel('Page 1.1')));
echo '<br /><hr /><br />';
echo $this->navigation($container3)->menu()->renderMenu($container3);
?>
3. Last one, cloning, page on first level (and deeper) that is "found" is NOT removed from container
Same code as above, only 'Page 2' in place of 'Page 1.1'
Can someone tell me what's going on here?
All I want to achieve, is to display same menu in two different places. In both places menu is build from part of container pages, filtered with findXXX method...
But with described problem, it seems to be impossible :(
Thanks in advance for any suggestions.
Try this, you're on the right track, I just think your having trouble with the difference between Zend_Navigation and navigation() view helper.
<?php
//instantiate Zend_navigation object... This also registers this container to the view helper
$container = new Zend_Navigation(array(
array(
'label' => 'Page 1',
'route' => 'default',
'pages' => array(
array(
'label' => 'Page 1.1',
'route' => 'default',
),
)
),
array(
'label' => 'Page 2',
'route' => 'default',
),
array(
'label' => 'Page 3',
'route' => 'default',
)
));
//now we use the view helper
echo $this->navigation()->menu()->renderMenu($container);
echo '<br /><hr /><br />';
$label = $this->navigation()->findAllByLabel('Page 2');
echo $this->navigation()->menu()->renderMenu($label);
echo '<br /><hr /><br />';
echo $this->navigation()->menu()->renderMenu($container);
?>
personally I like to set navigation up in the bootstrap and use config files to make containers.
protected function _initNavigation() {
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/site.xml');
$container = new Zend_Navigation($config);
$registry = Zend_Registry::getInstance();
$registry->set('Zend_Navigation', $container);
}
Hope this helps :)