0

I am building a app in cakephp which have admin and users under it. I have make different menu for both . admin can view all menu title but users only few. I have written code, but i don't think this is optimized approach for default view.Can you suggest any small approach for it , Code optimization

here is my code:

  <?php 
       if ($this->Session->read('Auth.User')){

      if($this->Session->read('Auth.User.role')=='admin')
      {

      ?>    

Admin Section

    <div id='cssmenu'>
<ul>
   <li class='active'><?php echo $this->Html->link(
    'Home',
   array(
    'controller' => 'users',
    'action' => 'index',
    'full_base' => true
     ));?></li>
     <li class='has-sub'><a href='#'><span>Manage Users</span></a>
      <ul>
     <li class='has-sub'><?php echo $this->Html->link(
'Add New User',
   array(
    'controller' => 'users',
    'action' => 'add',
    'full_base' => true
   ));?>
        </li>
     <li class='has-sub'><?php echo $this->Html->link(
'InActive Users List',
array(
    'controller' => 'users',
    'action' => 'listUserInActive',
    'full_base' => true
));?>

     </li>
     <li class='has-sub'><?php echo $this->Html->link(
'Active Users',
array(
    'controller' => 'manageUsers',
    'action' => 'listUserActive',
    'full_base' => true
));?>

     </li>
  </ul>
 </li>
<li class='has-sub'><a href='#'><span>Manage Consignment</span></a>
   <ul>
     <li class='has-sub'><?php echo $this->Html->link(
 'Add Consignment',
 array(
    'controller' => 'manageConsignments',
    'action' => 'addConsignment',
    'full_base' => true
    ));?>
        </li>
     <li class='has-sub'><?php echo $this->Html->link(
    'Consignment List',
   array(
     'controller' => 'manageConsignments',
    'action' => 'listConsignment',
    'full_base' => true
 ));?>

       </li>
    </ul>
 </li>
<li class='has-sub'><a href='#'><span>Manage Parcels </span></a>
  <ul>
     <li class='has-sub'><?php echo $this->Html->link(
   'Add Parcel',
   array(
    'controller' => 'manageParcels',
    'action' => 'addParcel',
     'full_base' => true
  ));?>
        </li>
      <li class='has-sub'><?php echo $this->Html->link(
  'Parcel List',
  array(
    'controller' => 'manageParcels',
    'action' => 'listParcel',
    'full_base' => true
    ));?>

     </li>
    </ul>
  </li>
 <li class='has-sub'><a href='#'><span>Manage Pallets  </span></a>
  <ul>
     <li class='has-sub'><?php echo $this->Html->link(
   'Add Pallets',
   array(
    'controller' => 'managePallets',
    'action' => 'addPallet',
    'full_base' => true
     ));?>
        </li>
     <li class='has-sub'><?php echo $this->Html->link(
   'Pallets List',
     array(
    'controller' => 'managePallets',
    'action' => 'listPallet',
    'full_base' => true
));?>

     </li>
   </ul>
 </li>
 <li class='has-sub'><a href='#'><span>Manage Profile  </span></a>
  <ul>
     <li class='has-sub'><?php echo $this->Html->link(
'Update Profile',
array(
    'controller' => 'users',
    'action' => 'editProfile',
    'full_base' => true
));?>
        </li>
     <li class='has-sub'><?php echo $this->Html->link(
 'Update Password',
array(
     'controller' => 'users',
    'action' => 'updatePassword',
    'full_base' => true
  ));?>

     </li>
     <li class='has-sub'><?php echo $this->Html->link(
   'Delete Account',
   array(
    'controller' => 'users',
    'action' => 'delete',
    'full_base' => true
   ),
  array(),
 "Are you sure you wish to delete this Account?");?>

     </li>
  </ul>
 </li>
 <li><?php echo $this->Html->link(
'Logout',
array(
    'controller' => 'users',
    'action' => 'logout',
    'full_base' => true
));?>

</ul>
   </div>   


    <?php }

Other User Section

    else {?>

    <div id='cssmenu'>
   <ul>
    <li class='active'><?php echo $this->Html->link(
    'Home',
     array(
    'controller' => 'users',
    'action' => 'index',
    'full_base' => true
      ));?></li>

    <li class='has-sub'><a href='#'><span>Manage Consignment</span></a>
       <ul>
     <li class='has-sub'><?php echo $this->Html->link(
     'Add Consignment',
       array(
    'controller' => 'manageConsignments',
    'action' => 'addConsignment',
    'full_base' => true
      ));?>
        </li>
     <li class='has-sub'><?php echo $this->Html->link(
'Consignment List',
array(
    'controller' => 'manageConsignments',
    'action' => 'listConsignment',
    'full_base' => true
));?>

     </li>
   </ul>
</li>
<li class='has-sub'><a href='#'><span>Manage Parcels </span></a>
  <ul>
     <li class='has-sub'><?php echo $this->Html->link(
 'Add Parcel',
 array(
    'controller' => 'manageParcels',
    'action' => 'addParcel',
       'full_base' => true
    ));?>
        </li>
     <li class='has-sub'><?php echo $this->Html->link(
     'Parcel List',
      array(
    'controller' => 'manageParcels',
    'action' => 'listParcel',
    'full_base' => true
      ));?>

      </li>
   </ul>
 </li>
 <li class='has-sub'><a href='#'><span>Manage Pallets  </span></a>
  <ul>
     <li class='has-sub'><?php echo $this->Html->link(
    'Add Pallets',
     array(
     'controller' => 'managePallets',
    'action' => 'addPallet',
    'full_base' => true
));?>
        </li>
     <li class='has-sub'><?php echo $this->Html->link(
'Pallets List',
array(
    'controller' => 'managePallets',
    'action' => 'listPallet',
    'full_base' => true
));?>

     </li>
   </ul>
</li>
<li class='has-sub'><a href='#'><span>Manage Profile  </span></a>
  <ul>
     <li class='has-sub'><?php echo $this->Html->link(
'Update Profile',
array(
    'controller' => 'users',
    'action' => 'editProfile',
    'full_base' => true
));?>
        </li>
     <li class='has-sub'><?php echo $this->Html->link(
'Update Password',
array(
    'controller' => 'users',
    'action' => 'updatePassword',
    'full_base' => true
));?>

     </li>
<li class='has-sub'><?php echo $this->Html->link(
'Delete Account',
array(
    'controller' => 'users',
    'action' => 'delete',
    'full_base' => true
),
  array(),
"Are you sure you wish to delete this Account?");?>

  </li>
  </ul>
 </li>
 <li><?php echo $this->Html->link(
 'Logout',
 array(
    'controller' => 'users',
    'action' => 'logout',
    'full_base' => true
   ));?>

      </ul>
       </div>   
So can i create like oops concept so that which tabs are common
to all that are added in else 

and in admin section add extra tab .Please suggest me appropriate approach.

1
  • Look at ACL pattern to get an idea how to implement this. I suggest you make your menu in db and grant each item to type of user. When you made your tree structure then look at this stackoverflow.com/questions/22785181/… to make your tree menu Commented Apr 9, 2014 at 15:01

1 Answer 1

1

I can only suggest to put your manu into an array like:

$menu = array( 
   'item1' => array(
       'title' => 'Add Parcel',
       'controller' => 'manageParcels',
       'action' => 'addParcel',
       'admin' => false,
       // other fields used in menu
    ),
    'item1' => array(
       'title' => 'Add Consignment',
       'controller' => 'manageConsignmens',
       'action' => 'addConsignmen'
       'admin' => true,
       // other fields used in menu
    )
)

so you can loop through the array and create your menu dinamically

you can also create a tree structure for your array to take into account of sub-menues

a further step could be creating a MenuHelper to manage all

final step could be saving the menu in a table (i.e menu_items) in your DB creating a model that actsAs Tree. And finally putting all in a plugin.

I guess something already exists

Sign up to request clarification or add additional context in comments.

3 Comments

please can you provide some example or elaborate it.
Your question is too much generico so the answor would became too long. Try focusing to a single point and I'll try to give you some hints
Thanks Arilia . If you have example for it which work with jquery and css please forward me at [email protected]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.