How can I display/hide menu items with Zend Framework - php

[See update at the end]
I'm working with Zend framework, in PHP and I have some difficulties with Zend Navigation. It's my first question here, so if something's wrong with it, just tell me and I'll correct it.
I have a menu looking like this in my application
Home
Login
Logout
Member's Page
I have a navigation xml file containing my menu.
<nav>
<home>
<label>Home</label>
<uri>/</uri>
</home>
<login>
<label>Login</label>
<uri>/index/login</uri>
</login>
<logout>
<label>Logout</label>
<uri>/index/logout</uri>
</logout>
<member>
<label>Member's Page</label>
<uri>/index/member</uri>
</member>
</nav>
Also a menu.phtml containing this
<div class="top-level">
<?php
foreach ($this->container as $page) {
if ($page->isVisible()) {
if ($page->isActive(true)) {
if ($page->isActive(false)
)$page->setClass("active");
else
$page->setClass("open");
echo $this->navigation()->menu()->htmlify($page);
//... the same continue for the 3 menu level
Finally, in my layout.phtml, I have this to render the menu
<?php
$partial = array('menu.phtml', 'default');
$this->navigation()->menu()->setPartial($partial);
echo $this->navigation()->menu()->render();
?>
For now, my menu works good, but I can't have Login and Logout always displayed in my menu. So, what I need to do, is to hide Login when I'm logged in, and to hide Logout when I'm logged out. It looked pretty simple when I started, and it still does, but I can't make it work. I don't know how and if I can hide and show item depending on logged users. I really need to make it work, because I will need to hide/display other items in the future.
So is there a way of doing that ?
Thanks !
EDIT :
I'm currently not using Zend::Auth or Zend_ACL for roles and authorization. If I want to know if the user is logged in or not, I have a token in the session that is valid only when the user is logged in. I'd like my menu to work without changing that if it's possible.
UPDATE :
I had it working in another way than those suggested. I'm really not sure it's a clean way, but it's doing the job for now. So now, my xml navigation file look like
<menuAnonymous>
<home>
<label>Login</label>
<uri>/login</uri>
</home>
</menuAnonymous>
<menuLogged>
<home>
<label>Logout</label>
<uri>/Logout</uri>
</home>
</menuLogged>
I initialize both in my bootstrap like this.
$config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml', 'menuNotLogged');
$container = new Zend_Navigation($config);
Zend_Registry::set('main',$container);
And then, in my layout, I look at my token and display the menu depending on that.
if ($tokenValid) {
echo $this->navigation()->menu()->render(Zend_Registry::get('main'));
} else {
echo $this->navigation()->menu()->render(Zend_Registry::get('logged'));
}
So it's working like I want now, but I still want to do it cleaner, so if you have any suggestions to help me ... thank you !

You should have a look at the "Leveraging Zend_Navigator" webinar on http://www.zend.com/en/resources/webinars/framework. It explains how you can tie your navigation to specifiec roles/ACL.

I updated my question with the solution I used, maybe I'll find a better way to do it later but for now this is it. I didn't find the solution here, that's why I'm answering my own question. Thanks for your help !

Related

How to modify magento front end pages

I am trying to create a module which has both frontend and backend functionality. Like I need to ask for the city in the home page when the store loads. And all the available cities are entered/managed in backend admin panel.
Before I used to write for only backend things, frontend seems little confusing.
There is a design folder which is completely for theme development.
All the example are little different(https://www.mageplaza.com/magento-2-module-development/,http://inchoo.net/magento-2/how-to-create-a-basic-module-in-magento-2/]2), they have routes.xml, where route_id, and all are defined, but here I don't need any extra route. Need some additional tweaks in frontend pages.
I created module V_name/M_name/adminhtml/block controllers etc view ...
Guide me how to create a module, which has both front end and backend connection, cities should be entered in admin, they should show on the frontend homepage.
For now, I only managed to edit home page content CMS page by adding some HTML which shows a popup with a dropdown for cities when the page loads.
Since you already have the back-end figured out I will focus on front-end. Also, since all you need to do is populate a list that you already have created this should be easy. I did something like this before and I found it easier to just use JSON to query a list of, in your case the cities, and populate the drop down. I don't believe this is the most 'MVP/proper' way to go, but it is less work then the other ways. (At least for me it is. I always prefer the JavaScript option since it allows for easy future page customization.)
To use the JSON method you need to create a Block with a method like the one below. You will see that you will also have to create a Resource Model (I'm not going to go over creating the Resource Model or the details of Blocks since there are much better resources than me already online that will go into every single detail you need.). Once this is complete you can access the data straight from the .phtml page in an easy to use JSON array.
First you need to make sure you are now structuring your Modules properly. The new Block below should be in a structure like this...
app/code/<VENDOR>/<MODULE>/Block/Wrapper.php (or whatever you name it)
The admin Blocks should be in the structure below, which it sounds like you are already know how to do.
app/code/<VENDOR>/<MODULE>/Block/Adminhtml
Create your Block and add a method to create a JOSN array like below...
public function getCityList()
{
$city_array = array();
/** #var \<VENDOR>\<MODULE>\Model\ResourceModel\City\Collection $collection */
$collection = $this->_cityCollectionFactory->create();
$collection->addFieldToFilter('active','1')->addFieldToSelect(['city_id', 'city']);
$collection->getSelect()->order(array('city ASC', 'city_id ASC'));
$count = 0;
foreach ($collection as $model)
{
$city_array["$count"] = $model->getData();
$count++;
}
return \Zend_Json::encode($city_array);
}
FYI... The foreach loop in the code above is weird and uses $count because I needed to do some tricky things to get something to work.
Then you can create the Block in your .phtml file to access the data via javascript.
<?php
$block_obj = $block->getLayout()->createBlock('<VENDOR>\<MODULE>\Block\Wrapper');
?>
<script type="text/javascript">
window.citylistJson = <?php echo $block_obj->getCityList() ?>;
</script>

Load pagination on different component

I would like to load pagination module on different module than article. I have found that
components\com_content\views\article\tmpl\default.php contains this:
<?php
if (!empty($this->item->pagination) && $this->item->pagination && $this->item->paginationposition && !$this->item->paginationrelative):
echo $this->item->pagination;
?>
<?php endif; ?>
unfortunately if I put this code for example to my template index file it does not work. Aparently I have to add something more to this.
Could you advice me what other part of the code is needed?
Thank you!
It's a little complicated to wrap your mind around but if you look in the plugins/content folder you will see the pagenavigation plugin. This is the plugin that creates the pagination you see in articles.
THis plugin is triggered by
$results = $dispatcher->trigger('onContentBeforeDisplay', array('com_content.article', &$item, &$item->params, $offset));
which you can find in `\components\com_content\views\article (and also archive and the tag view of com_tags). I have no idea why it's not triggered in other components except at some point probably someone thought there wasn't a usecase for it.
To trigger the plugin in another component you would need to add that same event or a different event that basically does what the onContentBeforeDisplay method in the plugin does. If it is your own component I would do it in the same place content and tags do. If you need it in one of the core components you could probably do it by using another event.

How to remove breadcrumbs from homepage

When Full Page Cache is on the breadcrumbs show up on the home page,
i've tried changing the line <?php if($crumbs && is_array($crumbs)): ?> to <?php if($crumbs && is_array($crumbs) && !$this->getIsHomePage()): ?> in \app\design\frontend\enterprise\[THEME]\template\page\html\breadcrumbs.phtml yet it still shows up
i've tried adding <remove name="breadcrumbs" /> to the "Layout Update XML" of the CMS page which is the homepage yet it still shows up
I've tried putting in Mage::log() or a <p> before the if statement however it works on every other page but the home page
a google search gives me nothing that i don't know (the Layout Update XML thing i got from one site) and most results are asking how to add the breadcrumbs when i search for "how to remove breadcrumbs from homepage
i'm wondering if there is any other way to remove the breadcrumbs from the homepage which i may have yet tried
You can achieve this by using the code from your second paragraph but changing !$this->getIsHomePage() to
!Mage::getBlockSingleton('page/html_header')->getIsHomePage()
If that doesn't work (depends on your version of Magento: you should find the function getIsHomePage defined in ./code/core/Mage/Page/Block/Html/Header.php), then you can use the following instead.
$this->getUrl('') !== $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true))

Change menubar in zend

Hy,
I have managed to make a top menu bar in Zend Framework and the code is placed in layouts/scripts/layout.phtml, and i have the followind items: Item1, Item2, Item3, Login, Register. What i want is that when somebody logs in, to change the last 2 elements, replace Login and Register with Logout. Can someone help me with something please? I readed something about placeholders, but i don`t understand them clearly.
Thank you.
Since it sounds like you are using a view-script (rather Zend_Navigation) for your menu, it should relatively straightforward to include a check as follows right in layouts/scripts/layout.phtml:
// Render your static menu elements
// render, render
// Now handle the dynamic part
if (!Zend_Auth::getInstance()->hasIdentity()){
// Render the `Register` and `Login` links.
} else {
// Render your `Logout` link.
}

wordpress plugin admin menu

I am trying to create a wordpress plugin admin menu. The problem that I am running into is with the menus. I am trying to add a page in the admin without actually adding the menu link. So for example I want to have a menu called test then I want to have some extra pages but I don't want physical links to them because they are only going to be used when there is an id to pass to them. is this possible and if so please someone explain because i can't seem to figure it out.
Yes. In your callback function for the admin page, just write out different sections and use conditional checks to display the right content. Then, under the page's title, add a <ul> with the class subsubsub containing the links to take the user to the right place. Something like this:
function my_awesome_admin_page(){
echo '<h2>My Title</h2>';
echo '<ul class="subsubsub"> <li>Foo</li> <li>Bar</li> </ul>';
if($_GET['foo'] != 'bar'){
//You're on the first page
} else {
//You're on the second page
}
}
I forget what the class is to signify the current subpage, but you can take a look on the 'Add Plugin' admin page. I think it's selected.

Categories