Friday, 23 August 2013

CakePHP: Cant use html helper inside my helper, causes fatal error

CakePHP: Cant use html helper inside my helper, causes fatal error

echo $this->Html->link($menuItem->title, array('controller' =>
$controllerName, 'action' => $menuItem->action)) creates fatal error.
My full code:
App::uses('AppHelper', 'View/Helper');
class MenuHelper extends AppHelper {
public $helpers = array('Html');
public $module;
public function __construct() {
//$module is a 2D associative array containing the menu list for each
module. The first index is the name of the module to be displayed and
the second index is the name of controller. The content is the array
of menuItem objects
$this->module['Meeting Management']['meetings'] = array(new
MenuItem('Create New Meeting', 'createMeeting', array('BUGS
Secretary')),
new MenuItem('View Draft Meetings', 'viewDraft', array('BUGS
Secretary', 'Teacher')),
new MenuItem('View Closed Meetings', 'viewClosed', array('BUGS
Secretary')),
new MenuItem('View Draft Resolutions', 'viewDraftResolutions',
array('BUGS Secretary', 'Teacher')),
new MenuItem('View Resolutions Sent to Head', 'viewSentToHead',
array('BUGS Secretary', 'Head')),
new MenuItem('View Sent Back Meetings', 'viewSentBack',
array('BUGS Secretary')),
new MenuItem('View Approved Resolutions', 'viewApproved',
array('BUGS Secretary', 'Head')),
new MenuItem('View All Meetings', 'viewAll', array('Teacher')),
new MenuItem('Search Meeting', 'searchMeeting', array('Teacher'))
);
$this->module['Application Management']['applications'] = array(new
MenuItem('Create Application', 'selectApplication', array('Student')),
new MenuItem('Application List', 'viewAll', array('Student',
'Head', 'Advisor', 'BUGS Secretary'))
);
}
public function genereteMenuList($currentRole) {
foreach ($this->module as $moduleName => $menuContent) {
foreach ($menuContent as $controllerName => $menuList) {
$visibleMenuList = $this->getVisibleMenuList($currentRole,
$menuList);
if (!empty($visibleMenuList)) {
echo '<h2>' . $moduleName . '</h2>';
echo '<ul>';
foreach ($visibleMenuList as $menuItem) {
echo '<li>';
echo $this->Html->link($menuItem->title,
array('controller' => $controllerName, 'action' =>
$menuItem->action));
echo '</li>';
}
echo '</ul>';
}
}
}
}
public function getVisibleMenuList($currentRole, $menuList) {
if (is_null($currentRole))
return array();
$visibleMenuList = array();
foreach ($menuList as $menuItem) {
if (in_array($currentRole, $menuItem->permittedRoles)) {
$visibleMenuList[] = $menuItem;
}
}
return $visibleMenuList;
}
}
class MenuItem {
public $action;
public $title;
public $permittedRoles; //who can access this menu
public function __construct($title, $action, $permittedRoles) {
$this->action = $action;
$this->title = $title;
$this->permittedRoles = $permittedRoles;
}
}

No comments:

Post a Comment