Zend Framework Source Code Snippets

"Href" view helper

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
102
103
104
105106
107
108
109
110
<?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.

Snippet description

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 =)

Snippet details

Created:
xapon xapon
6 months ago
Edited:
xapon xapon
6 months ago
Revision Id:
140
Edit Message:
Initial Release
ZF Version
1.8.3
Tags:
view helper url links
Comments:
0
Views:
738
Points:
2 (2 votes)

History

r140

Initial Release

xapon xapon
6 months ago
diff
r139

Initial Release

xapon xapon
6 months ago