"Href" view helper
<?php
/**
* Creates an html link depending on Zend_Acl.
* Automatically adds attributes and parameters.
*
* @author Sergeev Anton xapon91@gmail.com
* @package View_Helper_Href
* @copyright Copyright (c) 2010 Sergeev Anton
* @license http://www.gnu.org/licenses/gpl.html GNU GPL v3 license
*/
class CMS_View_Helper_Href extends Zend_View_Helper_Url {
/**
* @var array
* Default options
*/
private $options=array('controller'=>null, //default controller
'action'=>'index', //default action
'params'=>array(), //array of parameters
'router'=>'default', //default router
'checkAcl'=>true, //check if this link allowed by Zend_Acl
'content'=>null, //Link content, text or what you want between <a> and </a>
'attribs'=>array(), //Link attributes, e.g. class or id
'resource'=>null, //ACL resource name, if not set - controller name will be used
'resource-prefix'=>'mvc:',//If "resource" option is null, the ACL will be checked with this option connected with "controller" option
'privilege'=>null, //ACL privilege name, if null - action name will be used
'reset'=>true, //reset router defaults
'url'=>null, //Url like "http://google.com", will be used if controller is null
'acl'=>'Acl', //Zend_Acl instance. If option is string, will try to get Zend_Acl from Zend_Registry by this string
'role'=>null //String - role name.
);
/**
* @param array $options
* @return string or bool FALSE if options are incorrect or access denied
*/
public function href(array $options) {
$options=array_merge($this->options, $options);
$url=$this->setUrl($options);
if (count($options['attribs'])>0) {
foreach ($options['attribs'] as $attrib=>$value)
$attribs.=$attrib."='".$value."' ";
} else
$attribs=null;
$link="<a href=\"".$url."\" ".$attribs.">".$options['content']."</a>";
if ($options['checkAcl']==true) {
if ($this->checkAcl($options))
return $link;
else
return false;
} else
return $link;
}
/**
* Construct URL based on standart url helper
* @param array $options
* @return string
*/
private function setUrl($options) {
if ($options['controller']!=null) {
$url=$this->url(array_merge(array('controller'=>$options['controller'],
'action'=>$options['action']),
$options['params']),
$options['router'],
$options['reset']);
} elseif ($options['url']!=null) {
$url=$options['url'];
} else {
$url='#';
}
return $url;
}
/**
* Check if current options are allowed by Zend_Acl
* @param array $options
* @return bool
*/
private function checkAcl($options) {
if ($options['resource']==null AND $options['controller']!=null) {
$resource=$options['resource-prefix'].$options['controller'];
$privilege=$options['action'];
} elseif ($options['resource']!=null AND $options['privilege']!=null) {
$resource=$options['resource'];
$privilege=$options['privilege'];
} else
return false;
if (is_a($options['acl'], 'Zend_Acl'))
$acl=$options['acl'];
else {
if (Zend_Registry::isRegistered(strval($options['acl'])))
$acl=Zend_Registry::get($options['acl']);
else
return false;
}
$role=$options['role'];
if ($role==null)
return false;
if ($acl->isAllowed($role, $resource, $privilege)) {
return true;
}
else
return false;
}
}
Comments
You must login before commenting on a snippet. If you do not have an account, please register.
I've been used standart "url" helper for a long time, until i've got bored with lots of non-presentation logic in view scripts. If i want to generate a link, i nedd to check - is it allowed by ACL, is there any identity in Auth, etc.
Now to generate a simple <a href="..."></a> i can just use my helper. Here are some examples:
<?
echo $this->href(array('controller'=>'users', params=>array('id'=>3), 'content'=>'Profile', 'attribs'=>array('class'=>'profile-link')));
// Will echo this:
<a href="/users/index/id/3" class="profile-link">Profile</a>
//Another one:
echo $this->href(array('controller'=>'files', 'action'=>'delete', 'checkAcl'=>true,
'acl'=>'Zend_Acl' //string 'Zend_Acl' means that Zend_Acl object is stored in registry with this key. Also you can send an instance of Zend_Acl
'role'=>'admin',
'content'=>'delete'));
//If admin is not allowed to delete files, returns false. Else returns:
<a href="/files/delete/">delete</a>
//And another example:
echo $this->href(array('url'->'http://google.com', 'content'=>'google', 'checkAcl'=>true, 'acl'=>$Zend_Acl_Instance, 'role'=>'user', 'resource'=>'external_link', 'privilege'=>'view'));
//We send "resource" and "privilege" options and a simple static url, and if you have such Zend_Acl rules, returns:
<a href="http://google.com">google</a>
//And the last one ;)
echo $this->href(array('content'=>'Some ajax?', 'class'=>'ajaxlink')));
//returns:
<a href="#" class="ajaxlink">Some ajax?</a>
//May be useful to build links for the js-interaction.
You can also change the defaults to make you call pretty simple and short.
Thanks for the attention ;)
Waiting for comments =)
- Created:
-
xapon
- Edited:
-
xapon
- Revision Id:
- 140
- Edit Message:
- Initial Release
- ZF Version
- 1.8.3
- Tags:
- view helper url links
- Comments:
- 0
- Points:
- 2 (2 votes)