How to set menu items as active in Yii framework? - php

I'm new to Yii framework..
Now i working with yii1.1.6 version.In admin panel menus i want to display as active menu while which menu item is selected.Please help me how to make menu item as active...
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav side-nav">
<li class="">
<i class="fa fa-fw fa-dashboard"></i> Home
</li>
<li class="">
<i class="fa fa-fw fa-bar-chart-o"></i> About Us
</li>
<li class="">
<i class="fa fa-fw fa-table"></i> Contact Us
</li>
</ul>
</div>

You can compare your controller id and action id like this:
if(Yii::app()->controller->id == 'site' && Yii::app()->controller->action->id == 'index'){
echo '<li class="active">';
}

Related

Is there a way of using PDO or Smarty to change a navigation bar if a user is logged in?

I am creating a website using Smarty and PDO, here is my header file and I'm not sure how I can make it change depending on whether a user is logged in or not?
When the user isn't logged in these options should appear (Friday, Saturday, Sunday, Sign up, Log in)
But when they sign in I want 'Sign up' and 'log in' to change to 'My account' and 'Sign Out'. Below are is the code I currently have.
//If user is logged in
<div id="navigation"> <ul class="nav" id="navigation">
<nav class="navbar navbar-expand-lg">
<button class="navbar-toggler" id="hideshow" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"><i class="far fa-caret-square-down"></i></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="index.php?p=main">Home <i class="far fa-arrow-alt-circle-right"></i><span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php?p=friday">Friday <i class="far fa-arrow-alt-circle-right"></i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php?p=saturday">Saturday <i class="far fa-arrow-alt-circle-right"></i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php?p=sunday">Sunday <i class="far fa-arrow-alt-circle-right"></i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php?p=logout" id="Logout" />Logout <i class="fas fa-sign-out-alt"></i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php?p=account">My account <i class="far fa-arrow-alt-circle-right"></i></a>
</li>
//If user isn't signed in
<div id="navigation"> <ul class="nav" id="navigation">
<nav class="navbar navbar-expand-lg ">
<button class="navbar-toggler" id="hideshow" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"><i class="far fa-caret-square-down"></i></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="index.php?p=main">Home <i class="far fa-arrow-alt-circle-right"></i><span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php?p=friday">Friday <i class="far fa-arrow-alt-circle-right"></i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php?p=saturday">Saturday <i class="far fa-arrow-alt-circle-right"></i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php?p=sunday">Sunday <i class="far fa-arrow-alt-circle-right"></i></a>
</li>
<li class="nav-item">
<a class="nav-link" href="index.php?p=signup" id="signup" />Signup <i class="fas fa-user-plus"></i></a>
</li>
<li class="nav-item">
</i>
</li></ul>
{/if}
</div>
</nav></div>
First of all assign a variable from Controller to your view, example:
$view->assign('USER_ID', $this->auth->getUserId());
then in template:
{if $USER_ID > 0}
{* html or template when user is logged in *}
{* if differences are big, better is create new sub-template: *}
{include file='./header-logged-in.tpl'}
{else}
{* html or template when user is not logged in *}
{include file='./header-logged-out.tpl'}
{/if}
if(isset($_SESSION['user_id'])) {
// User Logged In Navigation
} else {
// User Logged Out Navigation
}
Of course, you didn't provide enough information, this assumes your user session is set to $_SESSION['user_id'] <-- change to whatever your session is saved as.

Bootstrap prevent multiple collapsable elements from being open at the same time

I have the following html sidebar menu:
<li {{{ (Request::is('bookings/*') }}} data-toggle="collapse" data-target="#bookings">
<i class="fa fa-address-book" aria-hidden="true"></i> Bookings <i class="fa fa-chevron-down" aria-hidden="true"></i>
</li>
<ul class="sub-menu collapse" id="bookings">
<li class="collapsed">All Bookings</li>
<li class="collapsed" >Add New</li>
</ul>
<li {{{ (Request::is('bookings/*') || Request::is('bookings') ? 'class=active' : '') }}} data-toggle="collapse" data-target="#item2">
<i class="fa fa-address-book" aria-hidden="true"></i> Item 2 <i class="fa fa-chevron-down" aria-hidden="true"></i>
</li>
<ul class="sub-menu collapse" id="item2">
<li {{{ (Request::is('bookings') ? 'class=active' : 'collapsed') }}}>All Bookings</li>
<li {{{ (Request::is('bookings/create') ? 'class=active' : 'collapsed') }}} >Add New</li>
</ul>
This is a basic Bootstrap collapse menu that contains a sub menu which expands when the li element is clicked.
The problem I have is lets say I have 2 or 3 of these menu items that all have sub menus. There is a possibility that all of them could open at the same time, I don't like this because this forces a scroll overflow as the height increases which then shows a scrollbar for the side menu.
Is there a way I can prevent multiple elements from being expanded in Bootstrap?
I am using Laravel 5 if that helps.
I think this should help you, just play a little with it to adapt it to your needs:
<div class="row">
<div class="col">
<ul class="nav nav-stacked" id="accordion1">
<li class="panel"> <a data-toggle="collapse" data-parent="#accordion1" href="#firstLink">Test12</a>
<ul id="firstLink" class="collapse">
<li>SubTest1</li>
<li>SubTest1</li>
<li>SubTest1</li>
</ul>
</li>
<li class="panel"> <a data-toggle="collapse" data-parent="#accordion1" href="#secondLink">Test2</a>
<ul id="secondLink" class="collapse">
<li>SubTest2</li>
<li>SubTest2</li>
<li>SubTest2</li>
<li>SubTest2</li>
</ul>
</li>
</ul>
</div>
</div>
See it here
https://codepen.io/anon/pen/ZaMOxN

Menu can't go directly to a page

I have menu which is has sub menus.
<li>
<i class="fa fa-flag-checkered" aria-hidden="true"></i> <span>Lead</span> <i class="icon-submenu lnr lnr-chevron-left"></i>
<div id="lead" class="collapse ">
<ul class="nav">
<li><i class="fa fa-quote-right" aria-hidden="true"></i><span> Quotation </span><i class="icon-submenu lnr lnr-chevron-left"></i>
<div id="quotation" class="collapse ">
<ul class="nav">
<li><i class="fa fa-dot-circle-o" aria-hidden="true"></i> Create Quotation</li>
<li><i class="fa fa-dot-circle-o" aria-hidden="true"></i> Quotation Summary</li>
<li><i class="fa fa-dot-circle-o" aria-hidden="true"></i> Send Quotation</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
When I clicked it, I can not go directly to the page. I have to right-clicked on it to go to the desired page.
Here's an example of my controller
public function cquotation(){
$data = $this->salesModel->showCustomer();
$this->load->view('v_navbar');
$this->load->view('v_leftside');
$this->load->view('v_cquotation',array('data'=>$data));
}
Do you guys have any idea what's wrong with it?
I'm totally confused
Why are you using data-toggle="collapse" class="collapsed" on every hyperlink?. your code has data-toggle="collapse" class="collapsed" in every hyperlink.Bootstrap will prevent the default click event of a href .So remove every data-toggle="collapse" class="collapsed" part of a href in your code and it will work.
<li>
<i class="fa fa-flag-checkered" aria-hidden="true"></i> <span>Lead</span> <i class="icon-submenu lnr lnr-chevron-left"></i>
<div id="lead" class="collapse ">
<ul class="nav">
<li><i class="fa fa-quote-right" aria-hidden="true"></i><span> Quotation </span><i class="icon-submenu lnr lnr-chevron-left"></i>
<div id="quotation" class="collapse ">
<ul class="nav">
<li></i> Create Quotation</li>
<li></i> Quotation Summary</li>
<li></i> Send Quotation</li>
</ul>
</div>
</li>
</ul>
</div>
</li>

How to show menu according to session in php?

I have 2 tables admin and superadmin. I have same login page for both
users. i can login according to query.i had create sessions for both
users. each table have column name roleID. for superadmin roleID is
1, and for admin roleID is 2. Below is my code for login where i
create session. i used print_r and my session are working. Below is
my code for login page.
if (isset($_REQUEST['submit']))
{
$username = $_REQUEST['user'];
$password = $_REQUEST['pass'];
$sql = mysqli_query($conn,"SELECT * FROM `accountants` where `acc_email` = '".$username."' AND `acc_pass` = '".$password."'");
$data = mysqli_fetch_array($sql);
$_SESSION['role0']=$data['roleId'];
$_SESSION['username']=$data['acc_name'];
$sql1 = mysqli_query($conn,"SELECT * FROM `superadmin` where `username` = '".$username."' AND `password` = '".$password."'");
$data1 = mysqli_fetch_array($sql1);
$_SESSION['role1']=$data1['roleId'];
if ($data>0)
{
header('Location: societyList.php');
}
elseif ($data1>0) {
header('Location: home.php');
}
else
{
header('Location: index.php');
echo 'incorrect login';
}
}
Now on home.php i have some menus to show accoording to roleID my
code for menu.
<div id="sidebar-menu" class="main_menu_side hidden-print main_menu">
<div class="menu_section">
<h3>General</h3>
<ul class="nav side-menu">
<li><a><i class="fa fa-home"></i> Home <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Create Society</li>
</ul>
</li>
<li><a><i class="fa fa-home"></i> Master <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Units</li>
<li>Members</li>
<li>Parking Lots</li>
<li>Charges</li>
<li>Chart of Account</li>
<li>Interest Penalties</li>
<li>Billing Templates</li>
<li>Tax Structure</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Transactions <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Bill</li>
<li>Collection</li>
<li>Expenses</li>
<li>Journal</li>
<li>Bank Reco</li>
<li>Drop Box</li>
<li>Online Payment</li>
</ul>
</li>
<li><a><i class="fa fa-desktop"></i> Reports <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Income & Expenses</li>
<li>Balance Sheet</li>
<li>Cash Flow</li>
<li>Interest Calculation</li>
</ul>
</li>
<li><i class="fa fa-table"></i> Notices <span class="fa fa-chevron-down"></span>
</li>
<li><a><i class="fa fa-bar-chart-o"></i> Registers <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Form-I</li>
</ul>
</li>
<li><a><i class="fa fa-clone"></i> Forum <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Cultural Activity</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Domestic Help <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Request for Plumber</li>
<li>Request for Maid</li>
<li>Request for House Cleaner</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Emergency <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Ambulance</li>
<li>Fire Brigade</li>
<li>Police</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Helpdesk <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Request NOC for Tenancy</li>
<li>Request NOC for Home Loan</li>
<li>Request NOC for Mortgage as collateral</li>
<li>Request for Vehicle Parking</li>
<li>Application for Transfer</li>
<li>Request for waiver of interest</li>
<li>Request for waiver of a charge</li>
<li>Suggestion</li>
<li><a><i class="fa fa-edit"></i><span class="fa fa-chevron-down"></span>Complaint</a>
<ul class="nav child_menu">
<li>About leakage</li>
<li>About tenants issues</li>
<li>About parking nuisance
</ul>
</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> CFO Desk Assists <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Tenant Rating</li>
<li>Owners Rating</li>
<li>Your Reviews</li>
</ul>
</li>
<li><a><i class="fa fa-edit"></i> Masters <span class="fa fa-chevron-down"></span></a>
<ul class="nav child_menu">
<li>Auto Bank reconcilliation</li>
<li>AMC Masters</li>
<li>Auto Adjust pending reference</li>
<li>Default GL for defined transactions</li>
</ul>
</li>
</ul>
</div>
</div>
As you can see lists. i want if $_SESSION['role0']=$data['roleId']; in session so first 4 lists will be visible for him only.
If $_SESSION['role1']=$data['roleId']; in session then rest of all lists will be display to him.
How to done it please help me with the same. I used if condition like
if{$_REQUEST($_SESSION['role0'])
echo 'some lists';
}
if i used like this if condition nothing will display.
The session data is note stored in the $_REQUEST.
$_SESSION['role0']=$data1['roleId'];
$_SESSION['role1']=$data1['roleId'];
Both 'role0' and 'role1' will have the same value. It can be simplified to:
$_SESSION['role']=$data1['roleId'];
Then use:
if( $_SESSION['role'] === 1) {
echo 'admin role 1';
echo 'Show first half of menu';
} elseif ($_SESSION['role'] === 2) {
echo 'Show second half of menu';
} else {
echo 'Other or missing admin value. show no menu';
}
SQL injection is possible the way it is currently written. Prepared statements would be a good thing to add next.

How do I keep the current tab active?

I use bootstrap theme bs-admin-bcore and I want to make menu in file "menu.php" to be more dynamic.
For example, I have this code :
<ul id="menu" class="collapse">
<li class="panel">
<a href="index.php" >
<i class="icon-table"></i> Dashboard
</a>
</li>
<li class="panel ">
<a href="#" data-parent="#menu" data-toggle="collapse" class="accordion-toggle" data-target="#component-nav">
<i class="icon-tasks"> </i> UI Elements<span class="pull-right"><i class="icon-angle-left"></i></span> <span class="label label-default">10</span>
</a>
<ul class="collapse" id="component-nav">
<li class=""><i class="icon-angle-right"></i> Buttons </li>
</ul>
</li>
</ul>
when I call index.php <li class="panel"> becomes <li class="panel active"> and when I call button.php EU Elements becomes active.
You have to add "active" class to the parent li of buttons as well n it will work.

Categories