Thursday, January 13, 2011

drupal template.php

custom login block & check user role

<?
/**
* custom user login forms
*
*/

function ocean_user_bar() { // print ocean_user_bar()
global $user;
$output = '';

if (!$user->uid) { //if users is not login
$output .= drupal_get_form('user_login_block'); //then show the login form
}
else { // else, show the welcome msg
$output .= t('<p>Hi <strong>!user</strong>, welcome back :-)</p>',
array('!user' => theme('username', $user))
);
$output .= '<div id="user-bar-item" style="display: none;">';

//  add content
$output .= theme('item_list', array(
t('Create content'),
l(t('+ page'), 'node/add/page', array('attributes' => array('title' => t('Tạo page mới'), 'class' => t('add-page')))),
l(t('+ story'), 'node/add/story', array('attributes' => array('title' => t('Tạo bài viết chung chung khác'), 'class' => t('add-story')))),
));

//$output .= '<hr />';
// account info
$output .= theme('item_list', array(
t('Your Menu'),
l(t('My content'), 'tracker/'.$user->uid, array('attributes' => array('title' => t('My recent post'), 'class' => t('my-post')))),
l(t('My account'), 'user/'.$user->uid, array('attributes' => array('title' => t('Thông tin tài khoản và các thiết lập cá nhân'), 'class' => t('my-account')))),
l(t('My Files'), 'user/'.$user->uid.'/imce', array('attributes' => array('title' => t('Các files tôi đã upload lên website'), 'class' => t('my-files')))),
l(t('Logout'), 'logout', array('attributes' => array('title' => t('Logout'), 'class' => t('logout'))))
));

/****
*
*  check roles
*
*****/
$adminRoles= array('dev','editor','admin2');
$adminAble= FALSE;
foreach($adminRoles as $role) {
if( in_array($role, array_values($user->roles)) ) $adminAble= TRUE;
}
// also show menu for uid #1
if($adminAble or ($user->uid == 1)) {
$output .= theme('item_list', array(
t('Admin only'),
l(t('Category'), 'admin/content/taxonomy', array('attributes' => array('title' => t('Quản lý tags, category'), 'class' => t('taxonomy-manager')))),
l(t('Content'), 'admin/content/node', array('attributes' => array('title' => t('Quản lý nội dung của các trang, bài viết'), 'class' => t('taxonomy-manager')))),
l(t('Block'), 'admin/build/block', array('attributes' => array('title' => t('Quản lý các blocks'), 'class' => t('my-block')))),
l(t('Contact'), 'admin/build/contact', array('attributes' => array('title' => t('Quản lý các form contact'), 'class' => t('site-contact')))),
l(t('Menu'), 'admin/build/menu', array('attributes' => array('title' => t('quản lý toàn bộ menu trên site'), 'class' => t('site-menu'))))
));
}

$output .= '</div>';

}
$output = '<div id="user-bar">'.$output.'</div>';    //wrapper in div for easy style
return $output;
}

paste this <?php print ocean_user_bar() ?> somewhere in page.tpl.php to show the form

No comments:

Post a Comment