<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gergely Hodicska &#187; tutorial</title>
	<atom:link href="http://blog.felho.hu/stock/tutorial/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.felho.hu</link>
	<description>Random secrets of PHP, web development</description>
	<lastBuildDate>Fri, 30 Nov 2007 10:16:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Extending Zend_Acl to support custom roles and resources</title>
		<link>http://blog.felho.hu/extending-zend_acl-to-support-custom-roles-and-resources.html</link>
		<comments>http://blog.felho.hu/extending-zend_acl-to-support-custom-roles-and-resources.html#comments</comments>
		<pubDate>Thu, 29 Nov 2007 16:08:03 +0000</pubDate>
		<dc:creator>Felho</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[zend acl]]></category>

		<guid isPermaLink="false">http://blog.felho.hu/extending-zend_acl-to-support-custom-roles-and-resources.html</guid>
		<description><![CDATA[Few days ago I played a little with the Zend_Acl package, which provides lightweight and flexible access control list functionality and privileges management. It was very easy to use, but I found that the base Zend_Acl package has some limitation/problem if you want to use it in a bigger real life project. Zend_Acl supports only [...]]]></description>
			<content:encoded><![CDATA[<p>Few days ago I played a little with the Zend_Acl package, which provides lightweight and flexible access control list functionality and privileges management. It was very easy to use, but I found that the base Zend_Acl package has some limitation/problem if you want to use it in a bigger real life project. Zend_Acl supports only logical roles, resources so I decided to extend it to allow using custom roles and resources which can represent existing entities (for example users/groups and topics in a database). You will find the full source code and sample sql dump in the end of the post.<span id="more-56"></span></p>
<h2>Zend_Acl</h2>
<p>The package provides classical ACL functionality. There are roles, resources and privileges, and you can assign them to each other via deny and allow rules. For example you can say that <code>XY user</code>(role) is allowed to <code>moderate</code>(privilege) the <code>PHP topic</code>(resource) in a forum. You don&#8217;t have to always specify all the three part, for example you can say, that <code>administrator</code> can <code>moderate</code> any topic.</p>
<p>I won&#8217;t introduce the <code>Zend_Acl</code> package, it has an easy to understand <a href="http://framework.zend.com/manual/en/zend.acl.html" title="Zend_Acl's manual page">manual page</a>. Some observation which could be interesting related to this post:</p>
<ul>
<li>
There is no separate user and group in Zend_Acl, the concept of role includes both of them. A role can have multiple parents, so roles compose a directed graph.
</li>
<li>
Resources can have only one parent so they compose a tree.
</li>
</ul>
<h2>Zend_Acl limitations/problems</h2>
<p>Consider a forum where you have a lot of users/groups and a lot of resources (topics). You have have to add all of them (manually one at a time) to the <code>Acl</code> object which is very memory and time consuming task. Of course once you build the <code>Acl</code> object you can save it (in a serialized form) and reuse it next time but it will eat a lot of memory too. To make this clear check the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #0000ff;">$acl</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Acl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">addRole</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Role<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'guest'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; -&gt;<span style="color: #006600;">addRole</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Role<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'member'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; -&gt;<span style="color: #006600;">addRole</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Role<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'admin'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #0000ff;">$parents</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'guest'</span>, <span style="color: #ff0000;">'member'</span>, <span style="color: #ff0000;">'admin'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">addRole</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Role<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'someUser'</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0000ff;">$parents</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Zend_Acl_Resource<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'someResource'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">deny</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'guest'</span>, <span style="color: #ff0000;">'someResource'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">allow</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'member'</span>, <span style="color: #ff0000;">'someResource'</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">isAllowed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'someUser'</span>, <span style="color: #ff0000;">'someResource'</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #ff0000;">'allowed'</span> : <span style="color: #ff0000;">'denied'</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>You can see that if I want to reference a role or a resource in the <code>allow/deny</code> method, first I have to add it to the <code>Acl</code> object.</p>
<p>My other problem is that the Zend_Acl doesn&#8217;t support different type of roles/resources. Of course you can use a prefix, but maybe different roles/resources should be handled in a different way.</p>
<p>I wanted to come up with a solution where I have to define only the access rules and the Acl automatically checks if the referenced entity exists and automatically handles its relations (parent[s]), something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #0000ff;">$acl</span> = <span style="color: #000000; font-weight: bold;">new</span> Zend_Acl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">deny</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'guest'</span>, <span style="color: #ff0000;">'someResource'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">allow</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'members'</span>, <span style="color: #ff0000;">'otherResource'</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">isAllowed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'someUser'</span>, <span style="color: #ff0000;">'someResource'</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #ff0000;">'allowed'</span> : <span style="color: #ff0000;">'denied'</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>You may have immeditaley a problem with this code: how can we distinguish the different roles/resources? Yes, we need one more step to have enough felxibility:<br />
<?php<br />
$acl = new Zend_Acl();</p>
<p>$acl->deny(&#8216;group:guest&#8217;, &#8216;topic:membersonly&#8217;);</p>
<p>echo $acl->isAllowed(&#8216;user:someUser&#8217;, &#8216;topic:membersonly&#8217;) ? &#8216;allowed&#8217; : &#8216;denied&#8217;;<br />
?><br />
With this notation we can have any type of roles and resources. In a typical application you will have a user and a group roles, but you can have for example an ldapuser too.</p>
<h2>Foo_Acl</h2>
<p>After a little planning I find out the following structure:<br />
<a href="http://blog.felho.hu/wp-content/fooaclclassdiagram.png" title="Foo_Acl class diagram"><img src="http://blog.felho.hu/wp-content/fooaclclassdiagram497.png" alt="Foo_Acl class diagram" /></a></p>
<p>Zend_Acl has a RoleRegistry but I needed more functionality so I extended it. In Zend_Acl there is no ResourceRegistry, so I added to the Foo_Acl package, its functionality is very similar to the RoleRegistry. For both of them I defined an interface, they can handle any role/resource which implements it. This interfaces extends the proper interfaces in the Zend_Acl package, and defines some more basic functionality: getting the relation(s) of the role/resource and loading it. For example the Foo_Acl_Role_Interface is the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #808080; font-style: italic;">/**<br />
&nbsp;* Interface of custom roles, Foo_Acl_Role_Registry can handle an object which implements it.<br />
&nbsp;* &nbsp; <br />
&nbsp;* @category &nbsp; Foo<br />
&nbsp;* @package &nbsp; &nbsp;Foo_Acl<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">interface</span> Foo_Acl_Role_Interface <span style="color: #000000; font-weight: bold;">extends</span> Zend_Acl_Role_Interface<br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Returns the array of the identifier of th parent roles<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @return array<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getParents<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Returns the specified role if it exists or null. <br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @return Foo_Acl_Role_Interface<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> load<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$resourceId</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>The functionality of different roles/resources is very similar, so I created a default implementation for both of this interfaces, and you can easily create custom ones by extending it. The default implementation of the Foo_Acl_Role_Interface is the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #808080; font-style: italic;">/**<br />
&nbsp;* The base implementation of Foo_Acl_Role_Interface, custom roles can extend it.<br />
&nbsp;* <br />
&nbsp;* @category &nbsp; Foo<br />
&nbsp;* @package &nbsp; &nbsp;Foo_Acl<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">class</span> Foo_Acl_Role_CustomBase implements Foo_Acl_Role_Interface<br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Unique id of Role<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @var string<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span style="color: #0000ff;">$_roleType</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Unique id of Role<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @var string<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span style="color: #0000ff;">$_roleId</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Array of the identifiers of the Role's parents<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; protected <span style="color: #0000ff;">$_parents</span>;<br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Sets the Role data<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @param &nbsp;string $id<br />
&nbsp; &nbsp; &nbsp;* @param &nbsp;array $parents<br />
&nbsp; &nbsp; &nbsp;* <br />
&nbsp; &nbsp; &nbsp;* @return void<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$roleId</span>, <span style="color: #0000ff;">$parents</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$this</span>-&gt;_roleId = <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">&#41;</span> <span style="color: #0000ff;">$roleId</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$this</span>-&gt;_parents = <span style="color: #0000ff;">$parents</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$className</span> = <span style="color: #000066;">get_class</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$this</span><span style="color: #66cc66;">&#41;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$this</span>-&gt;_roleType = <span style="color: #000066;">strtolower</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">substr</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$className</span>, <span style="color: #000066;">strrpos</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$className</span>, <span style="color: #ff0000;">'_'</span><span style="color: #66cc66;">&#41;</span><span style="color: #cc66cc;">+1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Defined by Zend_Acl_Role_Interface; returns the Role identifier<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @return string<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getRoleId<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;_roleType.<span style="color: #ff0000;">':'</span>.<span style="color: #0000ff;">$this</span>-&gt;_roleId;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Defined by Foo_Acl_Role_Interface; returns the Role's parents<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @return array<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getParents<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$this</span>-&gt;_parents;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Returns the specified role if it exists or null. <br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @return Foo_Acl_Role_Interface<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> load<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$roleId</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>If you want to create a custom role you should extend this class and overwrite the load function which responsibility is to check if the specified role exists and to instantiate the proper role object. I wrote some sample custom rules, the following is for representing users:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #808080; font-style: italic;">/**<br />
&nbsp;* Custom role representing users in the forum.<br />
&nbsp;* <br />
&nbsp;* @category &nbsp; Foo<br />
&nbsp;* @package &nbsp; &nbsp;Foo_Acl<br />
&nbsp;*/</span><br />
<span style="color: #000000; font-weight: bold;">class</span> Foo_Acl_Role_Custom_User <span style="color: #000000; font-weight: bold;">extends</span> Foo_Acl_Role_CustomBase<br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Returns the specified role if it exists or null.<br />
&nbsp; &nbsp; &nbsp;*<br />
&nbsp; &nbsp; &nbsp;* @return Foo_Acl_Role_Interface<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> load<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$roleId</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// For more flexibility we support to refer a role with its name.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$lookUpByName</span> = !<span style="color: #000066;">ctype_digit</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$roleId</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp;if ($cache = Foo_Cache_Factory::getInstance()) {</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($lookUpByName) {</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($storedRoleId = $cache-&gt;fetch('acl.role.user.name2id.'.$roleId)) {</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$roleId = $storedRoleId;</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($storedRole = $cache-&gt;fetch('acl.role.user.obj.'.$roleId)) {</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return &nbsp;$storedRole;</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp;}</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$query</span> = <span style="color: #0000ff;">$lookUpByName</span> ?<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'SELECT u.user_id, g.group_id FROM user u LEFT JOIN user2group g ON u.user_id = g.user_id WHERE u.nickname = ?'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; :<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff0000;">'SELECT u.user_id, g.group_id FROM user u LEFT JOIN user2group g ON u.user_id = g.user_id WHERE u.user_id = ?'</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$rows</span> = <span style="color: #0000ff;">$GLOBALS</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'DB'</span><span style="color: #66cc66;">&#93;</span>-&gt;<span style="color: #006600;">getAll</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$query</span>, <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$roleId</span><span style="color: #66cc66;">&#41;</span>, DB_FETCHMODE_ASSOC<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$rows</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$parents</span> = <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// If the user belongs to some groups we set them as its parents.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">is_null</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$rows</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'group_id'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$rows</span> <span style="color: #b1b100;">as</span> <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$parents</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = Foo_Acl_Role_Custom_Group::<span style="color: #006600;">load</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'group_id'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$lookUpByName</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($cache) {</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$cache-&gt;store('acl.role.user.name2id.'.$roleId, $row['user_id'], 1800);</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$roleId</span> = <span style="color: #0000ff;">$row</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'user_id'</span><span style="color: #66cc66;">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$role</span> = <span style="color: #000000; font-weight: bold;">new</span> self<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$roleId</span>, <span style="color: #0000ff;">$parents</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($cache) {</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$cache-&gt;store('acl.role.user.obj.'.$roleId, $role, 1800);</span><br />
<span style="color: #808080; font-style: italic;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$role</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>You can see it is very straightforward to create a new role. I commented out the caching codes because I am in changing the cache implementations and I could not put it to sample code.</p>
<p>The Foo_Acl package provide an effective and convenient way to handle a huge number of entity:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<br />
<span style="color: #808080; font-style: italic;">//insert into topic values(1, null);</span><br />
<span style="color: #808080; font-style: italic;">//insert into topic values(2, null);</span><br />
<span style="color: #808080; font-style: italic;">//insert into topic values(3, null);</span><br />
<span style="color: #808080; font-style: italic;">//insert into topic values(4, 1);</span><br />
<span style="color: #808080; font-style: italic;">//insert into topic values(5, 2);</span><br />
<span style="color: #808080; font-style: italic;">//insert into topic values(6, 4);</span><br />
<span style="color: #808080; font-style: italic;">//insert into user_group values(1, 'group1');</span><br />
<span style="color: #808080; font-style: italic;">//insert into user_group values(2, 'group2');</span><br />
<span style="color: #808080; font-style: italic;">//insert into user values(1, 'user1');</span><br />
<span style="color: #808080; font-style: italic;">//insert into user values(2, 'user2');</span><br />
<span style="color: #808080; font-style: italic;">//insert into user values(3, 'user3');</span><br />
<span style="color: #808080; font-style: italic;">//insert into user values(4, 'user4');</span><br />
<span style="color: #808080; font-style: italic;">//insert into user2group values (1,1);</span><br />
<span style="color: #808080; font-style: italic;">//insert into user2group values (2,1);</span><br />
<span style="color: #808080; font-style: italic;">//insert into user2group values (3,2);</span><br />
<span style="color: #808080; font-style: italic;">//insert into user2group values (4,2);</span><br />
<br />
<span style="color: #0000ff;">$acl</span> = <span style="color: #000000; font-weight: bold;">new</span> Foo_Acl<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">allow</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'user:1'</span>, <span style="color: #ff0000;">'dummy:foo'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000066;">var_dump</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">isAllowed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'user:1'</span>, <span style="color: #ff0000;">'dummy:foo'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// true</span><br />
<span style="color: #000066;">var_dump</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">isAllowed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'user:1'</span>, <span style="color: #ff0000;">'dummy:bar'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// flase</span><br />
<br />
<span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">allow</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'group:group1'</span>, <span style="color: #ff0000;">'dummy:coverpage'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000066;">var_dump</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">isAllowed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'user:user2'</span>, <span style="color: #ff0000;">'dummy:coverpage'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// true</span><br />
<span style="color: #000066;">var_dump</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">isAllowed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'user:user3'</span>, <span style="color: #ff0000;">'dummy:coverpage'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// flase</span><br />
<br />
<span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">allow</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'group:group1'</span>, <span style="color: #ff0000;">'hierarchy:1'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000066;">var_dump</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">isAllowed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'user:1'</span>, <span style="color: #ff0000;">'hierarchy:4'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// true</span><br />
<span style="color: #000066;">var_dump</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">isAllowed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'user:2'</span>, <span style="color: #ff0000;">'hierarchy:6'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// true</span><br />
<span style="color: #000066;">var_dump</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$acl</span>-&gt;<span style="color: #006600;">isAllowed</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'user:1'</span>, <span style="color: #ff0000;">'hierarchy:2'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// flase</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>I tested it on the database of a bigger forum and it worked very well. You can download the <a href='http://blog.felho.hu/wp-content/fooacl.zip' title='Source code of Foo_Acl package and some sample code.'>sample code here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.felho.hu/extending-zend_acl-to-support-custom-roles-and-resources.html/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>What is new in PHP 5.3 &#8211; part 2: late static binding</title>
		<link>http://blog.felho.hu/what-is-new-in-php-53-part-2-late-static-binding.html</link>
		<comments>http://blog.felho.hu/what-is-new-in-php-53-part-2-late-static-binding.html#comments</comments>
		<pubDate>Thu, 15 Nov 2007 02:26:40 +0000</pubDate>
		<dc:creator>Felho</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[late static binding]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://blog.felho.hu/what%e2%80%99s-new-in-php-53-part-2-late-static-binding.html</guid>
		<description><![CDATA[In the first part of this series I wrote about namespaces, in the second part we will deal with late static binding, which is a very exciting new feature in PHP 5.3 and which promise some really nifty code.   This topic attracted attention after Zend&#8217;s webcast about the Zend Framework. There was a [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://blog.felho.hu/whats-new-in-php-53-part-1-namespaces.html" title="Whatâ€™s new in PHP 5.3 - part 1: namespaces">first part</a> of this series I wrote about namespaces, in the second part we will deal with late static binding, which is a very exciting new feature in PHP 5.3 and which promise some really nifty code. <img src='http://blog.felho.hu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  This topic attracted attention after <a href="http://www.phparch.com/webcasts/player/Main_content.php?p=L3RyYWluaW5nL3NsaWRlcy9aRkNBU1QvcmVjb3JkaW5ncy9yZWNvcmQuZmx2" title="Zend Framework webcast">Zend&#8217;s webcast</a> about the <a href="http://framework.zend.com/" title="Zend Framework">Zend Framework</a>. There was a little outcry in the PHP blogosphere, that the ActiveRecord examples presented in this webcast can&#8217;t work in PHP even with the version 5.2. Now with late static binding it is possible to implement this style of ActiveRecord <strong>almost</strong> correctly.<span id="more-43"></span></p>
<h2>Late static binding basic</h2>
<p>First let me introduce what late static binding is about. I am not going to write too much about the basics, because fortunately I get a <a href="http://livedocs.phpdoc.info/manual/en/language.oop5.late-static-bindings.php" title="Late Static Bindings">helping hand</a> <img src='http://blog.felho.hu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . So the base problem is the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> A <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> who<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">echo</span> <span style="color: #000000; font-weight: bold;">__CLASS__</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; self::<span style="color: #006600;">who</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp;<br />
<span style="color: #66cc66;">&#125;</span> &nbsp;<br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> B <span style="color: #000000; font-weight: bold;">extends</span> A <span style="color: #66cc66;">&#123;</span>&nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> who<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">echo</span> <span style="color: #000000; font-weight: bold;">__CLASS__</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp;<br />
<span style="color: #66cc66;">&#125;</span> &nbsp; <br />
&nbsp; &nbsp; <br />
B::<span style="color: #006600;">test</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Output: &amp;qout;A&amp;qout;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>Static references to the current class like self:: or __CLASS__ are resolved using the class in which the function belongs, as in where it was defined, so when we call the <code>test()</code> method on class <code>B</code> it will call the <code>who()</code> method of class <code>A</code>. </p>
<p>PHP 5.3 introduces a new possibility by allowing a keyword that references the class that was initially called at runtime:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000000; font-weight: bold;">class</span> A <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> who<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">echo</span> <span style="color: #000000; font-weight: bold;">__CLASS__</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">static</span>::<span style="color: #006600;">who</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Here comes Late Static Bindings&nbsp; &nbsp; </span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp;<br />
<span style="color: #66cc66;">&#125;</span> &nbsp;<br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> B <span style="color: #000000; font-weight: bold;">extends</span> A <span style="color: #66cc66;">&#123;</span>&nbsp; &nbsp;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> who<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">echo</span> <span style="color: #000000; font-weight: bold;">__CLASS__</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp;<br />
<span style="color: #66cc66;">&#125;</span> &nbsp; <br />
<br />
B::<span style="color: #006600;">test</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Output: &quot;B&quot;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>This is the basic behavior of the static keyword in this context, you can get more information in the manual. Now back to the ActiveRecord.</p>
<h2>Facing the problem</h2>
<p>Zend&#8217;s proposal was something similar:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> ActiveRecord <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> findByPk<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// something maqic</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Blog <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><br />
<br />
Blog::<span style="color: #006600;">findByPk</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>The conception is nice, the base implementation of <code>findByPk()</code> finds out from the name of the child class the name of the table on which it should be operate. There is only one (huge) problem with this code, when <code>findByPk()</code> is called it can&#8217;t determines the name of the class on which it was called. There was an ugly workaround using <code>debug_backtrace()</code>, but its performance was quite poor, so finally it was not used in ZF. </p>
<p>After the late static binding introduction you may come up with the following code in PHP 5.3:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">class</span> ActiveRecord <br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">__CLASS__</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> findByPk<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$calledClass</span> = <span style="color: #000066;">static</span>::<span style="color: #006600;">getClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// The magic...</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Blog <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord <br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> getClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">__CLASS__</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
Blog::<span style="color: #006600;">findByPk</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>Running this code the value of <code>$calledClass</code> will be &#8220;Blog&#8221;. This is great, but it would be very annoying if we have to add the <code>getClass()</code> method to every child class. Fortunately with the late static binding we get a great new function too: <code>get_called_class()</code>, which returns the name of the current calling scope. Using this function we get a nifty code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> ActiveRecord <br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> findByPk<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$calledClass</span> = get_called_class<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// The magic remains...</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Blog <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><br />
<br />
Blog::<span style="color: #006600;">findByPk</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>This works very well, but unfortunately we can&#8217;t be absolutely happy, because there is a big flaw (I believe it is) in the current implementation of late static binding. <a href="http://www.digitalsandwich.com/" title="Michael Lively's blog">Michael Lively</a> <a href="http://www.mail-archive.com/internals@lists.php.net/msg30731.html" title="Design flaw in the current implementation of late static binding">pointed to this problem</a> on the internal list and <a href="http://www.digitalsandwich.com/archives/65-Late-static-binding....sorta.html" title="Late static binding....sorta :/">on his blog</a>. How does this problem affect our example? Imagine that we want a simple logging functionality only in the Blog class. Consider the following code (it seems to a logical and suitable solution for this task):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> ActiveRecord <br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> findByPk<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$calledClass</span> = get_called_class<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// The magic remains...</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> Blog <span style="color: #000000; font-weight: bold;">extends</span> ActiveRecord <br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> findByPk<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// We want to log something.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Then the parent should do the magic.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; parent::<span style="color: #006600;">findByPk</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
Blog::<span style="color: #006600;">findByPk</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>If you try to run this program maybe you will surprise, because the value of the <code>$calledClass</code> variable will be &#8220;ActiveRecord&#8221;. OOPS! What here happens: parent will resolve to ActiveRecord and this breaks late static binding. From the manual: &#8220;Late static bindings&#8217; reslution will stop at a fully resolved static call with no fallback&#8221;. It is possible that this explanation is not completely exact (maybe somebody from the internal list can explain it more precisely), but the substance is that this is a legitimate usage of late static binding and is not currently supported. I hope it will be fixed/supported in the final version of PHP 5.3.</p>
<p>I believe that this feature holds a lot of potential, for example you should check <a href="http://personal.schmalls.com/" title="Joshua Thompson's blog">Joshua Thompson&#8217;s</a> very interesting post: <a href="http://personal.schmalls.com/2007/10/29/return-to-prototype-based-programming-in-php/" title="Return to Prototype Based Programming in PHP">Return to Prototype Based Programming in PHP</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.felho.hu/what-is-new-in-php-53-part-2-late-static-binding.html/feed</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>What&#8217;s new in PHP 5.3 &#8211; part 1: namespaces</title>
		<link>http://blog.felho.hu/whats-new-in-php-53-part-1-namespaces.html</link>
		<comments>http://blog.felho.hu/whats-new-in-php-53-part-1-namespaces.html#comments</comments>
		<pubDate>Tue, 13 Nov 2007 21:59:59 +0000</pubDate>
		<dc:creator>Felho</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[namespaces]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://blog.felho.hu/whats-new-in-php-53-part-1-namespaces.html</guid>
		<description><![CDATA[In my previous post I mentioned that PHP 5.3 will be released in early 2008 so I think it&#8217;s just in time to talk about the features of this version. It is started by this polling (in detail, in an ordered version) on the internal list. The big gun features are namespaces, late static binding [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://blog.felho.hu/international-php-conference-2007-megapost.html" title="International PHP Conference 2007 Megapost">previous post</a> I mentioned that PHP 5.3 will be released in early 2008 so I think it&#8217;s just in time to talk about the features of this version. It is started <a href="http://marc.info/?l=php-internals&#038;m=118989314505802&#038;w=2" title="Polling result about PHP 5.3 features">by this polling</a> (<a href="http://bb.prohost.org/53Features.pdf" title="Polling result in detail about PHP 5.3 features">in detail</a>, in an <a href="http://www.wolerized.com/articles/first-most-likely-featurelist-php-5-3" title="First most likely featurelist for PHP 5.3">ordered version</a>) on the internal list. The big gun features are namespaces, late static binding and mysqlnd, but there are other interesting improvements, for example __callStatic, dynamic static calls. In this part of this series we are going to analyze namespaces in detail.<span id="more-44"></span></p>
<h2>Namespaces basic</h2>
<p>Namespace support in PHP was a long-felt want feature. In PHP the main motivation behind adding namespace support to the language was to solve the problem of long class names. If you develop a bigger library, you have to use long class names to avoid naming conflicts, for example look at this monster: <code>Zend_Search_Lucene_Analysis_Analyzer_Common_Text_CaseInsensitive</code>. </p>
<p>From the version 5.3 you can group your code into namespaces. Different namespaces can contains classes, functions, constants with the same name. Defining a namespace is very straightforward, you should use the <code>namespace</code> statement in the very beginning of the file, for example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #808080; font-style: italic;">/** classes/my/foo/MyClass.php */</span><br />
<br />
namespace my::<span style="color: #006600;">foo</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">class</span> MyClass <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #808080; font-style: italic;">// You can define functions and constants in the namespace too.</span><br />
<span style="color: #000000; font-weight: bold;">function</span> myFunc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #66cc66;">&#125;</span><br />
const MY_CONST = <span style="color: #ff0000;">'foo'</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>You have a lot of options to use items defined in this namespace:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #808080; font-style: italic;">/** test.php */</span><br />
<span style="color: #b1b100;">include</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'classes/my/foo/MyClass.php'</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// You can always access anything âˆˆan object with fully qualified name.</span><br />
<span style="color: #0000ff;">$foo</span> = <span style="color: #000000; font-weight: bold;">new</span> my::<span style="color: #006600;">foo</span>::<span style="color: #006600;">MyClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// You can use the use statement to import a namespace.</span><br />
use my::<span style="color: #006600;">foo</span>;<br />
<span style="color: #808080; font-style: italic;">// After the above statement you can reffer for the my::foo namespace with foo.</span><br />
<span style="color: #0000ff;">$foo</span> = <span style="color: #000000; font-weight: bold;">new</span> foo::<span style="color: #006600;">MyClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// You can import only one class.</span><br />
use my::<span style="color: #006600;">foo</span>::<span style="color: #006600;">MyClass</span>;<br />
<span style="color: #0000ff;">$foo</span> = <span style="color: #000000; font-weight: bold;">new</span> MyClass;<br />
<br />
<span style="color: #808080; font-style: italic;">// You can create aliases.</span><br />
use my::<span style="color: #006600;">foo</span> <span style="color: #b1b100;">as</span> MyFoo;<br />
use my::<span style="color: #006600;">foo</span>::<span style="color: #006600;">MyClass</span> <span style="color: #b1b100;">as</span> MyFooClass;<br />
<span style="color: #0000ff;">$foo</span> = <span style="color: #000000; font-weight: bold;">new</span> MyFoo::<span style="color: #006600;">MyClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0000ff;">$foo</span> = <span style="color: #000000; font-weight: bold;">new</span> MyFooClass<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// Note, that the following two statements are equivalent:</span><br />
use my::<span style="color: #006600;">foo</span>;<br />
use my::<span style="color: #006600;">foo</span> <span style="color: #b1b100;">as</span> foo;<br />
<br />
<span style="color: #808080; font-style: italic;">// You can access functions and constants in the same way:</span><br />
my::<span style="color: #006600;">foo</span>::<span style="color: #006600;">myFunc</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
myFoo::<span style="color: #006600;">myFunc</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
my::<span style="color: #006600;">foo</span>::<span style="color: #006600;">MY_CONST</span>;<br />
myFoo::<span style="color: #006600;">MY_CONST</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>Only namespaces and classes can be imported with the <code>use</code> statement, for example you can&#8217;t say: <code>use my::foo::MY_CONST;</code>. A use statement takes effect from its definition to the end of the file, you can use it everywhere in the global scope. You can use the same namespace in more files, but one file should contain only one namespace (this behavior maybe will changed in the final version, or the maybe the <code>namespace</code> keyword will be replaced with <code>package</code> <img src='http://blog.felho.hu/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ). While you can&#8217;t import a function or a constant, you should always use some kind of prefix if you want to access any of them from a namespace. In the earlier versions of PHP 5.3 the <code>import</code> keyword was used insted of <code>use</code>, but few weeks ago <a href="http://marc.info/?l=php-internals&#038;m=119308858512331&#038;w=2" title="Using keyword<br />
'use' instead of 'import'">it was changed</a>. </p>
<h3>The &#8220;empty&#8221; namespace (<code>::</code>)</h3>
<p>If you use the <code>::</code> prefix by function and class names, they are interpreted as global independently from the current import rules. This is useful inside a namespace.</p>
<h2>Life with namesapces (porting code to PHP 5.3)</h2>
<p>If you decide to use namespaces there are some pitfalls you should pay attention. </p>
<h3>Class naming convention</h3>
<p>You should forget to use reserved words in you class names. Consider the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #808080; font-style: italic;">/** classes/my/form/element/static.php */</span><br />
<span style="color: #000000; font-weight: bold;">class</span> MyFormElementStatic <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>The equivalent would be this using namespace:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #808080; font-style: italic;">/** classes/my/form/element/static.php */</span><br />
namespace my::<span style="color: #006600;">form</span>::<span style="color: #006600;">element</span>;<br />
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #000066;">Static</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>But <code><del datetime="2007-11-14T19:41:25+00:00">Date</del>Static</code> is a reserved word <del datetime="2007-11-14T19:41:25+00:00">(and an existent internal class)</del> so this code ends in fatal error. This could be a bad news if you have a lot of similar classes.</p>
<h3>Autoloading</h3>
<p>From PHP 5.3 <code>__autoload()</code> will get the fully qualified class name. This mean that you have to modify you <code>__autoload</code> funciton if you have used one. A basic example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #808080; font-style: italic;">/** test.php */</span><br />
<span style="color: #000000; font-weight: bold;">function</span> __autoload<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$className</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">'classes/'</span>.<span style="color: #000066;">str_replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'::'</span>, DIRECTORY_SEPARATOR, <span style="color: #0000ff;">$className</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">'.php'</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<br />
<span style="color: #0000ff;">$foo</span> = <span style="color: #000000; font-weight: bold;">new</span> my::<span style="color: #006600;">foo</span>::<span style="color: #006600;">MyClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>Or you prefer the <a href="http://www.php.net/~helly/php/ext/spl/" title="SPL - Standard PHP Library">SPL way</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #808080; font-style: italic;">/** classes/my/core/classloader.php */</span><br />
namespace my::<span style="color: #006600;">core</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> classLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$className</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">'classes/'</span>.<span style="color: #000066;">str_replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'::'</span>, DIRECTORY_SEPARATOR, <span style="color: #0000ff;">$className</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">'.php'</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
spl_autoload_register<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'my::core::classLoader'</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #808080; font-style: italic;">/** test.php */</span><br />
<span style="color: #b1b100;">require</span> <span style="color: #ff0000;">'classes/my/core/classLoader.php'</span>;<br />
<span style="color: #0000ff;">$foo</span> = <span style="color: #000000; font-weight: bold;">new</span> my::<span style="color: #006600;">foo</span>::<span style="color: #006600;">MyClass</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></table></div>

<h3><code>get_class()</code>, <code>get_parent_class()</code>, etc.</h3>
<p>When you use these type of functions you should take into consideration that they returns fully qualified class names too.</p>
<h3>Reflection API</h3>
<p>The Reflection API should reflect (;)) to this new language feature. The plan is the following:</p>
<ul>
<li>Create a new <code>ReflectionNamespace</code> class with the following methods: <code>getName()</code>, <code>getClasses()</code>, <code>getFunctions()</code>, <code>getFiles()</code>
	</li>
<li>Extend the <code>ReflectionClass</code> and <code>ReflectionFunction</code> classes with a new <code>getNamespace()</code> method.
	</li>
</ul>
<h2>Life inside a namespace</h2>
<p>There is some special knowledge you should be familiar with, if you want to write codes in namespaces.</p>
<h3>The <code>__NAMESPACE__</code> constant</h3>
<p>There is a new special constant: __NAMESPACE__, which contains the name of the current namespace. For example you can write the SPL autoloader in the following way:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #808080; font-style: italic;">/** classes/my/core/classloader.php */</span><br />
namespace my::<span style="color: #006600;">core</span>;<br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> classLoader<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$className</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">require</span> <span style="color: #ff0000;">'classes/'</span>.<span style="color: #000066;">str_replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'::'</span>, DIRECTORY_SEPARATOR, <span style="color: #0000ff;">$className</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #ff0000;">'.php'</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
spl_autoload_register<span style="color: #66cc66;">&#40;</span>__NAMESPACE__.<span style="color: #ff0000;">'::classLoader'</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></table></div>

<h3>Resolving names inside a namespace</h3>
<p>I don&#8217;t want to describe the complete rule set here, you can read it in the <a href="http://cvs.php.net/viewvc.cgi/php-src/README.namespaces?revision=1.7&#038;view=markup" title="PHP namespaces README">PHP namespaces README</a>. The main point is, that PHP try to resolve a name first according to the current namesapce. Let&#8217;s see some example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
namespace my::<span style="color: #006600;">foo</span>;<br />
...<br />
bar<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
...<br />
::<span style="color: #006600;">bar</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>When you call <code>bar()</code> then PHP try to find the function in the <code>my::foo</code> namespace. If it finds it will call it, if not it will try to call the <strong>internal</strong> <code>bar()</code> function, so it is important to notice, that using this notation, you can&#8217;t call user defined function inside a namespace. Using the <code>::bar()</code> form PHP will call the <code>bar()</code> function in the <strong>global</strong> namespace, so it could be an internal or a user defined function. This logic is true when you try to create a new object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
namespace my::<span style="color: #006600;">foo</span>;<br />
<br />
core::<span style="color: #006600;">bar</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
::<span style="color: #006600;">core</span>::<span style="color: #006600;">bar</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>In this case of <code>core::bar()</code> PHP first tries to call function <code>bar()</code> from namespace <code>my::foo::core</code> then calls method <code>bar()</code> of <strong>internal</strong> class <code>core</code>. In the case of <code>::core::bar()</code> it first tries to call function <code>bar()</code> from namespace <code>core</code> then calls method <code>bar()</code> of class <code>core</code> from <strong>global</strong> scope.</p>
<p>The namespace support in PHP is not yet finished, it is possible some modifications in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.felho.hu/whats-new-in-php-53-part-1-namespaces.html/feed</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>WordPress quicktag toolbar tweaking again</title>
		<link>http://blog.felho.hu/wordpress-quicktag-toolbar-tweaking-again.html</link>
		<comments>http://blog.felho.hu/wordpress-quicktag-toolbar-tweaking-again.html#comments</comments>
		<pubDate>Sat, 10 Nov 2007 20:39:03 +0000</pubDate>
		<dc:creator>Felho</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://blog.felho.hu/wordpress-quicktag-toolbar-tweaking-again.html</guid>
		<description><![CDATA[It is said that it is a good behavior to set title attribute to a tags. I usually put to the title the text of the link. The process is: I put the link to the clipboard, go to the editor, select the text, press Ctrl-A, insert the link to the dialog box, press enter, [...]]]></description>
			<content:encoded><![CDATA[<p>It is said that it is a good behavior to set <code>title</code> attribute to <code>a</code> tags. I usually put to the <code>title</code> the text of the link. The process is: I put the link to the clipboard, go to the editor, select the text, press <code>Ctrl-A</code>, insert the link to the dialog box, press enter, then put the text of the link to the clipboard, go to the opening <code>a</code> tag, insert <code> title=""</code> and finally paste the text from the clipboard to the attribute. This is a repetitive and tedious task, so like in the <a href="http://blog.felho.hu/posting-source-code-in-wordpress-escaping-and-syntax-highlighting-the-inserted-code.html" title="previous case">previous case</a> I automate it. <span id="more-42"></span></p>
<p>To get <a href="http://wordpress.org/" title="WordPress">WordPress</a> to insert the <code>title</code> attribute automatically to <code>a</code> tag we had to modify the <a href="http://" title="wp-includes/js/quicktags.js">wp-includes/js/quicktags.js</a> file by adding a few lines of code to the <code>edInsertTag</code> function at two places:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>341
342
343
</pre></td><td class="code"><div class="javascript" style="font-family: monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">id</span> == <span style="color: #3366CC;">'ed_link'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span> = edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/&gt;/</span>, <span style="color: #3366CC;">' title=&amp;qout;'</span>+selectedText+<span style="color: #3366CC;">'&amp;qout;&gt;'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>357
358
359
</pre></td><td class="code"><div class="javascript" style="font-family: monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">id</span> == <span style="color: #3366CC;">'ed_link'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span> = edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/&gt;/</span>, <span style="color: #3366CC;">' title=&amp;qout;'</span>+selectedText+<span style="color: #3366CC;">'&amp;qout;&gt;'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></table></div>

<p>You can download the fully modified version <a href="http://blog.felho.hu/wp-includes/js/quicktags.js" title="The modified version of quicktags.js">from here</a> (this file contains my former modifications).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.felho.hu/wordpress-quicktag-toolbar-tweaking-again.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to use Smarty resources</title>
		<link>http://blog.felho.hu/how-to-use-smarty-resources.html</link>
		<comments>http://blog.felho.hu/how-to-use-smarty-resources.html#comments</comments>
		<pubDate>Tue, 06 Nov 2007 21:19:21 +0000</pubDate>
		<dc:creator>Felho</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[smarty]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[smarty resources]]></category>

		<guid isPermaLink="false">http://blog.felho.hu/how-to-use-smarty-resources.html</guid>
		<description><![CDATA[Have you ever faced the situation, that the content of a variable is a Smarty template like string, and you want to fetch or display it? I worked on project where we decided that our language file entries will be Smarty templates, because we need the flexibility of the template engine (sprintf was not enough). [...]]]></description>
			<content:encoded><![CDATA[<p>Have you ever faced the situation, that the content of a variable is a <a href="http://www.smarty.net/" title="Smarty template engine">Smarty</a> template like string, and you want to fetch or display it? I worked on project where we decided that our language file entries will be Smarty templates, because we need the flexibility of the template engine (<a href="http://php.net/sprintf" title="Return a formatted string">sprintf</a> was not enough). The problem is, that by default Smarty&#8217;s fetch/display method accepts a template file name. Of course we could write the text to a temporary file, but it would be bigger hack than PHPNuke. <img src='http://blog.felho.hu/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Fortunately Smarty has the mechanism to solve this problem via defining custom resource types.<span id="more-27"></span></p>
<h2>Smarty resources</h2>
<p>Smarty supports several type of plug-ins including <a href="http://www.smarty.net/manual/en/plugins.resources.php" title="Smarty manual - Resources">resource plug-ins</a>. Resource plug-ins allow you to change the way Smarty looks for templates. For example you can create a <code>db</code> resource plug-in, which allows you to store your templates in database. To use this plug-in you should refer to the templates with a <code>db:</code> prefix. For example <code>$smarty->display('db:login.tpl');</code> or in templates <code>{include file='db:header.tpl'}</code>.   The plug-in will find the template in the database, and return it.</p>
<h2>Register a Smarty resource plug-in</h2>
<p>You can register a custom Smarty resource plug-in with the<code><a href="http://www.smarty.net/manual/en/api.register.resource.php" title="Smarty manual - register_resource() method">register_resource()</a></code> method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #0000ff;">$smarty</span>-&gt;<span style="color: #006600;">register_resource</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'db'</span>, <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">'db_get_template'</span>,<br />
&nbsp; &nbsp; <span style="color: #ff0000;">'db_get_timestamp'</span>,<br />
&nbsp; &nbsp; <span style="color: #ff0000;">'db_get_secure'</span>,<br />
&nbsp; &nbsp; <span style="color: #ff0000;">'db_get_trusted'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>The first parameter is the name of the resource (should be at least two characters in length) and the second is an array with four function name which implement the plug-in. Every function will receive the requested resource as the first parameter and the Smarty object as the last parameter. The rest of parameters depend on the function. </p>
<p>From the Smarty manual:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><div class="php" style="font-family: monospace;">bool smarty_resource_name_source <span style="color: #66cc66;">&#40;</span>string <span style="color: #0000ff;">$rsrc_name</span>, string &amp;<span style="color: #0000ff;">$source</span>, object &amp;<span style="color: #0000ff;">$smarty</span><span style="color: #66cc66;">&#41;</span><br />
<br />
bool smarty_resource_name_timestamp <span style="color: #66cc66;">&#40;</span>string <span style="color: #0000ff;">$rsrc_name</span>, int &amp;<span style="color: #0000ff;">$timestamp</span>, object &amp;<span style="color: #0000ff;">$smarty</span><span style="color: #66cc66;">&#41;</span><br />
<br />
bool smarty_resource_name_secure <span style="color: #66cc66;">&#40;</span>string <span style="color: #0000ff;">$rsrc_name</span>, object &amp;<span style="color: #0000ff;">$smarty</span><span style="color: #66cc66;">&#41;</span><br />
<br />
bool smarty_resource_name_trusted <span style="color: #66cc66;">&#40;</span>string <span style="color: #0000ff;">$rsrc_name</span>, object &amp;<span style="color: #0000ff;">$smarty</span><span style="color: #66cc66;">&#41;</span></div></td></tr></table></div>

<ul>
<li>
      The first function, source() is supposed to retrieve the resource. Its second parameter $source is a variable passed by reference where the result should be stored. The function is supposed to return TRUE if it was able to successfully retrieve the resource and FALSE otherwise.</p>
<li>
      The second function, timestamp() is supposed to retrieve the last modification time of the requested resource, as a UNIX timestamp. The second parameter $timestamp is a variable passed by reference where the timestamp should be stored. The function is supposed to return TRUE if the timestamp could be successfully determined, or FALSE otherwise.
	</li>
<li>
      The third function, secure()is supposed to return TRUE or FALSE, depending on whether the requested resource is secure or not. This function is used only for template resources but should still be defined.
	</li>
<li>
      The fourth function, trusted() is supposed to return TRUE or FALSE, depending on whether the requested resource is trusted or not. This function is used for only for PHP script components requested by {include_php} tag or {insert} tag with the src attribute. However, it should still be defined even for template resources.
	</li>
</ul>
<p>You can find on the manual page a possible implementation of the <code>db</code> resource plugin.</p>
<h2>Solution for our original problem: text resource</h2>
<p>We can create a custom <code>text</code> resource to solve the problem mentioned in the introduction. The necessary code will be very straightforward:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> smarty_resource_text_get_template<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tpl_name</span>, &amp;<span style="color: #0000ff;">$tpl_source</span>, &amp;<span style="color: #0000ff;">$smarty_obj</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$tpl_source</span> = <span style="color: #0000ff;">$tpl_name</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> smarty_resource_text_get_timestamp<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tpl_name</span>, &amp;<span style="color: #0000ff;">$tpl_timestamp</span>, &amp;<span style="color: #0000ff;">$smarty_obj</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0000ff;">$tpl_timestamp</span> = <span style="color: #000066;">time</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> smarty_resource_text_get_secure<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tpl_name</span>, &amp;<span style="color: #0000ff;">$smarty_obj</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> smarty_resource_text_get_trusted<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$tpl_name</span>, &amp;<span style="color: #0000ff;">$smarty_obj</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span><br />
<br />
<br />
<span style="color: #b1b100;">include</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Smarty-2.6.18/libs/Smarty.class.php'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0000ff;">$smarty</span> = <span style="color: #000000; font-weight: bold;">new</span> Smarty<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0000ff;">$smarty</span>-&gt;<span style="color: #006600;">register_resource</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'text'</span>, <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #ff0000;">'smarty_resource_text_get_template'</span>, <br />
&nbsp; &nbsp; <span style="color: #ff0000;">'smarty_resource_text_get_timestamp'</span>, <br />
&nbsp; &nbsp; <span style="color: #ff0000;">'smarty_resource_text_get_secure'</span>, <br />
&nbsp; &nbsp; <span style="color: #ff0000;">'smarty_resource_text_get_trusted'</span><span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span>;<br />
<br />
<br />
<span style="color: #0000ff;">$smarty</span>-&gt;<span style="color: #006600;">assign</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'foo'</span>, <span style="color: #ff0000;">'world'</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #000066;">echo</span> <span style="color: #0000ff;">$smarty</span>-&gt;<span style="color: #006600;">fetch</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'text:Hello {$foo}!'</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// Hello world!</span><br />
<br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></table></div>

<p>Smarty resources could be very useful in some situations, keep in mind this feature! </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.felho.hu/how-to-use-smarty-resources.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Posting source code in WordPress: escaping and syntax highlighting the inserted code</title>
		<link>http://blog.felho.hu/posting-source-code-in-wordpress-escaping-and-syntax-highlighting-the-inserted-code.html</link>
		<comments>http://blog.felho.hu/posting-source-code-in-wordpress-escaping-and-syntax-highlighting-the-inserted-code.html#comments</comments>
		<pubDate>Sun, 04 Nov 2007 08:26:00 +0000</pubDate>
		<dc:creator>Felho</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[geshi]]></category>
		<category><![CDATA[highlighting]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[wp-syntax]]></category>

		<guid isPermaLink="false">http://blog.felho.hu/posting-source-code-in-wordpress-escaping-and-syntax-highlighting-the-inserted-code.html</guid>
		<description><![CDATA[I have already mentioned that I had difficulty in finding a suitable solution to post source code in my blog. However good the workaround presented in my previous post is functioning, I was not satisfied with it. I noticed two annoying things. The first was that I had to escape HTML special chars &#8220;manually&#8221; in [...]]]></description>
			<content:encoded><![CDATA[<p>I <a href="http://blog.felho.hu/escaping-problem-with-wp-syntax-wordpress-plugin.html">have already mentioned</a> that I had difficulty in finding a suitable solution to post source code in my blog. However good the workaround presented in my previous post is functioning, I was not satisfied with it. I noticed two annoying things. The first was that I had to escape HTML special chars &#8220;manually&#8221; in the inserted code, which was a pain in the ass. My second problem was (I like to indent my code with TAB) that indentation was too wide, which looked not really nice out (and was impractical when your code has multiple nested control structures). After a little JavaScript hacking and playing with <a href="http://qbnz.com/highlighter/" title="GeSHi - Generic Syntax Highlighter">GeSHi</a> and CSS I solved both of them.<span id="more-20"></span></p>
<p>A little side note: I found that the built-in rich text editor is almost absolutely useless, so I turned it off, and I suggest it to every WordPress user.  </p>
<h2>Escaping the inserted code</h2>
<p>When you want to put some source code to the post, you usually simply enclose it in <code>code</code> or <code>pre</code> tags. The problem is, that the source code may contain characters which have special meaning in the HTML language, so they should be escaped. These characters are: <code>&lt;</code>, <code>&gt;</code>, <code>&amp;</code>, <code>&qout;</code> and they should be converted to: <code>&amp;lt;</code>, <code>&amp;gt;</code>, <code>&amp;amp;</code>, <code>&amp;qout;</code>. I would like to have some kind of automation for this task, which could be run when we insert the text to the post.</p>
<p>When you insert a code (and use a syntax highlighter) you should specify the language of that code. For example if you use the WP-Syntax plugin you should enclose your code in <code>pre</code> tags and use the <code>lang</code> attribute to specify the language. In addition you can mark if you want line numbers to appear. Enclosing the copy-pasted text is another repetitive and tedious task so it is another good candidate for automation</p>
<p>It would be great if we could do this two task in one step, so I thought I extend the toolbar above the textarea to provide this functionality by adding a new <code>sourcecode</code> button to it:<br />
<img width="500" src='http://blog.felho.hu/wp-content/toolbarsourcecodebutton.png' alt='Toolbar sourcecode button' /></p>
<p>The <code>sourcecode</code> button works similar as the others button on the toolbar, if no text is selected on the first click it inserts the open tag, on the second click it inserts the close tag. If text is selected it encloses the text in <code>pre</code> tags when you activate it. The button can be accessed via <code>Ctrl-F</code> keyboard shortcut too. Before it ads the open tag to the editor it prompts for the syntax highlighting parameters and generates the proper open tag to them. When text is selected the special characters will be replaced to HTML entities in it. This way adding source code to your post will be very straightforward:</p>
<p>1. Select the text in te editor:<br />
<img width="500" src='http://blog.felho.hu/wp-content/selectthesourcecode.png' alt='Select the source code' /><br />
2. Select the inserted text and press <code>Ctrl-F</code>:<br />
<img width="500" src='http://blog.felho.hu/wp-content/selecttheinsertedtext.png' alt='Select the inserted text' /><br />
3. Type the syntax highlighting information:<br />
<img width="350" src='http://blog.felho.hu/wp-content/typesyntaxhighlightinginformation.png' alt='Type syntax highlighting information' /><br />
4. The resulted text is:<br />
<img width="500" src='http://blog.felho.hu/wp-content/theresulttext.png' alt='The resulted text' /></p>
<p>Extending the toolbar is quite easy, only the <code>wp-includes/js/quicktags.js</code> file should be slightly modified (you can download the fully modified version <a href="http://blog.felho.hu/wp-includes/js/quicktags.js">from here</a>, all the modifications are commented). To add a new button to the toolbar you should add the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>127
128
129
130
131
132
133
</pre></td><td class="code"><div class="javascript" style="font-family: monospace;">edButtons<span style="color: #66cc66;">&#91;</span>edButtons.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#93;</span> = <br />
<span style="color: #003366; font-weight: bold;">new</span> edButton<span style="color: #66cc66;">&#40;</span>&amp;#<span style="color: #CC0000;">39</span>;ed_pre&amp;#<span style="color: #CC0000;">39</span>;<br />
,&amp;#<span style="color: #CC0000;">39</span>;pre&amp;#<span style="color: #CC0000;">39</span>;<br />
,&amp;#<span style="color: #CC0000;">39</span>;&lt;pre&gt;&amp;#<span style="color: #CC0000;">39</span>;<br />
,&amp;#<span style="color: #CC0000;">39</span>;&lt;/pre&gt;&amp;#<span style="color: #CC0000;">39</span>;<br />
,&amp;#<span style="color: #CC0000;">39</span>;f&amp;#<span style="color: #CC0000;">39</span>;<br />
<span style="color: #66cc66;">&#41;</span>;</div></td></tr></table></div>

<p>After then we should modify the <code>edInsertTag</code> function, and we are ready:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
</pre></td><td class="code"><div class="javascript" style="font-family: monospace;"><span style="color: #009900; font-style: italic;">//------&lt;code modification&gt;-------</span><br />
<span style="color: #009900; font-style: italic;">// Prompts for syntax highlighting parameter, and return the appropriate start tag.</span><br />
<span style="color: #009900; font-style: italic;">// TODO: l10n support.</span><br />
<span style="color: #003366; font-weight: bold;">function</span> edGetPreStartTag<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> syntaxInfo = <span style="color: #000066;">prompt</span><span style="color: #66cc66;">&#40;</span>&amp;#<span style="color: #CC0000;">39</span>;Please insert syntax highlighting information:&amp;#<span style="color: #CC0000;">39</span>;+&amp;qout;\n&amp;qout;+&amp;#<span style="color: #CC0000;">39</span>;language<span style="color: #66cc66;">&#91;</span>, line number<span style="color: #66cc66;">&#93;</span>!&amp;#<span style="color: #CC0000;">39</span>;, &amp;#<span style="color: #CC0000;">39</span>;php,<span style="color: #CC0000;">1</span>&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">split</span><span style="color: #66cc66;">&#40;</span>&amp;#<span style="color: #CC0000;">39</span>;,&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> &amp;#<span style="color: #CC0000;">39</span>;&lt;pre lang=&amp;qout;&amp;#<span style="color: #CC0000;">39</span>;+syntaxInfo<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>+&amp;#<span style="color: #CC0000;">39</span>;&amp;qout;&amp;#<span style="color: #CC0000;">39</span>;+<span style="color: #66cc66;">&#40;</span>syntaxInfo<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span> != undefined ? &amp;#<span style="color: #CC0000;">39</span>; line=&amp;qout;&amp;#<span style="color: #CC0000;">39</span>;+syntaxInfo<span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>+&amp;#<span style="color: #CC0000;">39</span>;&amp;qout;&amp;#<span style="color: #CC0000;">39</span>; : &amp;#<span style="color: #CC0000;">39</span>;&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span>+&amp;#<span style="color: #CC0000;">39</span>;&gt;&amp;#<span style="color: #CC0000;">39</span>;+&amp;qout;\n&amp;qout;;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #009900; font-style: italic;">// It converts all HTML special chars to HTML entites</span><br />
<span style="color: #003366; font-weight: bold;">function</span> edHtmlSpecialChars<span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> string.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/&amp;/g, &amp;#<span style="color: #CC0000;">39</span>;&amp;amp;&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span>/&lt;/g</span>, &amp;#<span style="color: #CC0000;">39</span>;&amp;lt;&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/&gt;/g, &amp;#<span style="color: #CC0000;">39</span>;&amp;gt;&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span>/&amp;qout;/g</span>, &amp;#<span style="color: #CC0000;">39</span>;&amp;qout;&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span>.<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066FF;">/&amp;#<span style="color: #CC0000;">39</span>;/g</span>, &amp;#<span style="color: #CC0000;">39</span>;&amp;#<span style="color: #CC0000;">39</span>;&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #009900; font-style: italic;">//------&lt;/code modification&gt;-------</span><br />
<br />
<span style="color: #009900; font-style: italic;">// insertion code</span><br />
<br />
<span style="color: #003366; font-weight: bold;">function</span> edInsertTag<span style="color: #66cc66;">&#40;</span>myField, i<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//IE support</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">selection</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #000066;">focus</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; sel = document.<span style="color: #006600;">selection</span>.<span style="color: #006600;">createRange</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>sel.<span style="color: #006600;">text</span>.<span style="color: #006600;">length</span> &gt; <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//------&lt;code modification&gt;-------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">id</span> == &amp;#<span style="color: #CC0000;">39</span>;ed_pre&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sel.<span style="color: #006600;">text</span> = edHtmlSpecialChars<span style="color: #66cc66;">&#40;</span>sel.<span style="color: #006600;">text</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span> = edGetPreStartTag<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//------&lt;/code modification&gt;-------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sel.<span style="color: #006600;">text</span> = edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span> + sel.<span style="color: #006600;">text</span> + edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagEnd</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!edCheckOpenTags<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span> || edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagEnd</span> == &amp;#<span style="color: #CC0000;">39</span>;&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//------&lt;code modification&gt;-------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">id</span> == &amp;#<span style="color: #CC0000;">39</span>;ed_pre&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span> = edGetPreStartTag<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//------&lt;/code modification&gt;-------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sel.<span style="color: #006600;">text</span> = edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edAddTag<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sel.<span style="color: #006600;">text</span> = edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagEnd</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edRemoveTag<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #000066;">focus</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//MOZILLA/NETSCAPE support</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>myField.<span style="color: #006600;">selectionStart</span> || myField.<span style="color: #006600;">selectionStart</span> == &amp;#<span style="color: #CC0000;">39</span>;<span style="color: #CC0000;">0</span>&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startPos = myField.<span style="color: #006600;">selectionStart</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endPos = myField.<span style="color: #006600;">selectionEnd</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> cursorPos = endPos;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> scrollTop = myField.<span style="color: #006600;">scrollTop</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>startPos != endPos<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//------&lt;code modification&gt;-------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> selectedText = myField.<span style="color: #006600;">value</span>.<span style="color: #006600;">substring</span><span style="color: #66cc66;">&#40;</span>startPos, endPos<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> selectedTextLengthGrowth = <span style="color: #CC0000;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">id</span> == &amp;#<span style="color: #CC0000;">39</span>;ed_pre&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span> = edGetPreStartTag<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedTextLengthGrowth = selectedText.<span style="color: #006600;">length</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedText = edHtmlSpecialChars<span style="color: #66cc66;">&#40;</span>selectedText<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedTextLengthGrowth = selectedText.<span style="color: #006600;">length</span> - selectedTextLengthGrowth;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #006600;">value</span> = myField.<span style="color: #006600;">value</span>.<span style="color: #006600;">substring</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">0</span>, startPos<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + selectedText<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagEnd</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + myField.<span style="color: #006600;">value</span>.<span style="color: #006600;">substring</span><span style="color: #66cc66;">&#40;</span>endPos, myField.<span style="color: #006600;">value</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursorPos += edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span>.<span style="color: #006600;">length</span> + edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagEnd</span>.<span style="color: #006600;">length</span> + selectedTextLengthGrowth;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//------&lt;/code modification&gt;-------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!edCheckOpenTags<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span> || edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagEnd</span> == &amp;#<span style="color: #CC0000;">39</span>;&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//------&lt;code modification&gt;-------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">id</span> == &amp;#<span style="color: #CC0000;">39</span>;ed_pre&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span> = edGetPreStartTag<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//------&lt;/code modification&gt;-------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #006600;">value</span> = myField.<span style="color: #006600;">value</span>.<span style="color: #006600;">substring</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">0</span>, startPos<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + myField.<span style="color: #006600;">value</span>.<span style="color: #006600;">substring</span><span style="color: #66cc66;">&#40;</span>endPos, myField.<span style="color: #006600;">value</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edAddTag<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursorPos = startPos + edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span>.<span style="color: #006600;">length</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #006600;">value</span> = myField.<span style="color: #006600;">value</span>.<span style="color: #006600;">substring</span><span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">0</span>, startPos<span style="color: #66cc66;">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagEnd</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + myField.<span style="color: #006600;">value</span>.<span style="color: #006600;">substring</span><span style="color: #66cc66;">&#40;</span>endPos, myField.<span style="color: #006600;">value</span>.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edRemoveTag<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursorPos = startPos + edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagEnd</span>.<span style="color: #006600;">length</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #000066;">focus</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #006600;">selectionStart</span> = cursorPos;<br />
&nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #006600;">selectionEnd</span> = cursorPos;<br />
&nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #006600;">scrollTop</span> = scrollTop;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!edCheckOpenTags<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span> || edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagEnd</span> == &amp;#<span style="color: #CC0000;">39</span>;&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//------&lt;code modification&gt;-------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">id</span> == &amp;#<span style="color: #CC0000;">39</span>;ed_pre&amp;#<span style="color: #CC0000;">39</span>;<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span> = edGetPreStartTag<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900; font-style: italic;">//------&lt;/code modification&gt;-------</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #006600;">value</span> += edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagStart</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edAddTag<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #006600;">value</span> += edButtons<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>.<span style="color: #006600;">tagEnd</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edRemoveTag<span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; myField.<span style="color: #000066;">focus</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></table></div>

<h2>Controlling the indentation</h2>
<p>The <a href="http://wordpress.org/extend/plugins/wp-syntax/" title="WP-Syntax plugin home page">WP-Syntax plugin</a> uses GeSHi with default settings, which means that GeSHi wraps code blocks in <code>pre</code> tags. In this mode GeSHi doesn&#8217;t replace TAB characters with spaces, so we can&#8217;t control the appearance of the indentation. But if we instruct GeSHi to use <code>div</code> tags instead of <code>pre</code>, it will replace leading TAB characters to spaces, and you can set the number of spaces. If we want to use 4 spaces we should modify the plugin accordingly to the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>79
80
81
82
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #0000ff;">$geshi</span> = <span style="color: #000000; font-weight: bold;">new</span> GeSHi<span style="color: #66cc66;">&#40;</span>htmlspecialchars_decode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0000ff;">$language</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0000ff;">$geshi</span>-&gt;<span style="color: #006600;">enable_keyword_links</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0000ff;">$geshi</span>-&gt;<span style="color: #006600;">set_header_type</span><span style="color: #66cc66;">&#40;</span>GESHI_HEADER_DIV<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #0000ff;">$geshi</span>-&gt;<span style="color: #006600;">set_tab_width</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></table></div>

<p>After this modification you should probably modify your CSS. I use now the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><div class="css" style="font-family: monospace;"><span style="color: #808080; font-style: italic;">/***** SYNTAX HIGHLIGHTER *****/</span><br />
<span style="color: #6666ff;">.wp_syntax</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">100</span></span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#f9f9f9</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">border</span>: <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #993333;">silver</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span>: <span style="color: #933;">0</span> <span style="color: #933;">0</span> <span style="color: #933;">1</span><span style="color: #6666ff;"><span style="color: #933;">.5em</span></span> <span style="color: #933;">0</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">overflow</span>: <span style="color: #993333;">auto</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* IE FIX */</span><br />
<span style="color: #6666ff;">.wp_syntax</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; overflow-x: <span style="color: #993333;">auto</span>;<br />
&nbsp; &nbsp; overflow-y: <span style="color: #993333;">hidden</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">padding-bottom</span>: expression<span style="color: #66cc66;">&#40;</span>this<span style="color: #6666ff;">.scrollWidth</span> &gt; this<span style="color: #6666ff;">.offsetWidth</span> ? <span style="color: #933;">15</span> : <span style="color: #933;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #933;"><span style="color: #933;">100</span>%</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.wp_syntax</span> table <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">border-collapse</span>: <span style="color: #993333;">collapse</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.wp_syntax</span> div, <span style="color: #6666ff;">.wp_syntax</span> td <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">vertical-align</span>: <span style="color: #000000; font-weight: bold;">top</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #933;">2px</span> <span style="color: #933;">4px</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">white-space</span>: <span style="color: #993333;">nowrap</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.wp_syntax</span> <span style="color: #6666ff;">.line_numbers</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">text-align</span>: <span style="color: #000000; font-weight: bold;">right</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#e8e9dc</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #993333;">gray</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">overflow</span>: <span style="color: #993333;">visible</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
td<span style="color: #6666ff;">.code</span> div <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">padding-top</span>: <span style="color: #933;">0px</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></table></div>

<p>I think these modifications would be interesting in the official WordPress version too, I found them useful, now I can insert source code to the post in a very convenient way.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.felho.hu/posting-source-code-in-wordpress-escaping-and-syntax-highlighting-the-inserted-code.html/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Symfony form handling recipe</title>
		<link>http://blog.felho.hu/symfony-form-handling-receipt.html</link>
		<comments>http://blog.felho.hu/symfony-form-handling-receipt.html#comments</comments>
		<pubDate>Fri, 02 Nov 2007 22:50:53 +0000</pubDate>
		<dc:creator>Felho</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[doctrine]]></category>
		<category><![CDATA[form handling]]></category>

		<guid isPermaLink="false">http://blog.felho.hu/symfony-form-handling-receipt</guid>
		<description><![CDATA[Symfony is a really great and well-thought-out framework, but I think its form handling support needs some more improvements. 5-6 years ago I developed an MVC framework as my thesis which has the following features related to form handling:

You can define forms logically (including labels, validation rules, error message parameters etc.) via form configuration files. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.symfony-project.com/" title="symfony - Open-Source PHP Web Framework">Symfony</a> is a really great and well-thought-out framework, but I think its form handling support needs some more improvements. 5-6 years ago I developed an <a href="http://en.wikipedia.org/wiki/Model-view-controller" title="Model View Controller design pattern">MVC</a> framework as my thesis which has the following features related to form handling:</p>
<ul>
<li>You can define forms logically (including labels, validation rules, error message parameters etc.) via form configuration files. The developer module provides a web based front-end to edit these files in a convenient way.</li>
<li>You have an API to create or modify forms from program code (add extra validations rule, add/modify/freeze elements etc.), this could be very useful.</li>
<li>A form can render itself via renderer objects, so you can insert a form to a template adding only one line to it. Generally you should only set up a few renderer object per project, they can be reused.</li>
<li>The framework validates forms automatically, it can generate client side validation code too. It redisplays the form on errors with the error messages.</li>
</ul>
<p>It would be great if symfony came up with something similar, I found these features very handy. After this little introduction (feature request <img src='http://blog.felho.hu/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ) let me show you a solution how you can effectively handle a form for creating and modifying something.<span id="more-18"></span></p>
<p>This tutorial is not an introduction to symfony, you should have some knowledge about it. It has a really <a href="http://www.symfony-project.com/book/1_0/" title="The Definitive Guide to symfony">great documentation</a> and a <a href="http://www.symfony-project.com/askeet/1_0/" title=" Askeet - symfony Advent Calendar">super tutorial</a>, so you can learn the basics easily.</p>
<p>Imagine a social site, where users can get points after their activities. There are events (creating forum topic, answering a question etc.). When a user for example creates a forum topic we record a <code>new_forum_topic</code> event to him/her. We will now build a form for creating and editing the events in the system. </p>
<h2>Database layout</h2>
<p>An event has the following attributes:</p>
<ul>
<li><strong>id</strong>: primary key, auto generated.</li>
<li><strong>code</strong>: we can refer to an event with it in program code, which improves the readability of the code.</li>
<li><strong>type</strong>: we differentiate  public and admin actions.</li>
<li><strong>weight</strong>: how many points worth is the event.</li>
<li><strong>title</strong>: we show this text on the administration area.</li>
<li><strong>description</strong>: the description of the event.</li>
</ul>
<p>We store the events in the following database table (you may notice that it would be nice to use an enum for the type field, but unfortunately <a href="http://doctrine.pengus.net/" title="Doctrine - Open Source PHP 5 ORM">Doctrine</a> (a <a href="http://en.wikipedia.org/wiki/Object-relational_mapping" title="Object-relational mapping on Wikipedia">PHP ORM tool</a>) does not support it yet):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><div class="sql" style="font-family: monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> award_event<span style="color: #66cc66;">&#40;</span> &nbsp; &nbsp; <br />
&nbsp; &nbsp; id INT <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span>,<br />
&nbsp; &nbsp; code VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,<br />
&nbsp; &nbsp; type VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,<br />
&nbsp; &nbsp; weight SMALLINT <span style="color: #993333; font-weight: bold;">UNSIGNED</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,<br />
&nbsp; &nbsp; title VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>,<br />
&nbsp; &nbsp; description TEXT,<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span>,<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">UNIQUE</span> <span style="color: #66cc66;">&#40;</span>code<span style="color: #66cc66;">&#41;</span><br />
<span style="color: #66cc66;">&#41;</span> ENGINE = INNODB;</div></td></tr></table></div>

<h2>Setting up the controller</h2>
<p>We have two actions from the user&#8217;s point of view: creating a new event and editing an existing one), but with a little trick we can use the same symfony action and view for both of them. First let us find out the interface of the two actions. We can create an event with the URL <code>http://example.com/award/event/add</code> and edit it with <code>http://example.com/award/edit/id</code>. To achieve this we have to add the following to our <code>routing.yml</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><div style="font-family: monospace;">award_edit_event:<br />
&nbsp; url: &nbsp; /award/event/edit/:id<br />
&nbsp; param: { module: award, action: editEvent }<br />
&nbsp; requirements: { id: \d+ }<br />
<br />
award_add_event:<br />
&nbsp; url: &nbsp; /award/event/add<br />
&nbsp; param: { module: award, action: editEvent, id: ~ }</div></td></tr></table></div>

<p>You can see that we mapped both functions to the same <code>editEvent</code> action. In the first rule we ensure that <code>id</code> parameter should be an integer, in the second rule we ensure that the value of the <code>id</code> parameter will be constantly <code>null</code>. Now we should add to <code>award</code> module&#8217;s <code>actions.class.php</code> the <code>executeEditEvents</code> method:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">class</span> awardActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/**<br />
&nbsp; &nbsp; &nbsp;* Editing award events.<br />
&nbsp; &nbsp; &nbsp;*/</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeEditEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$request</span> = <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">getRequest</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">getMethod</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> != sfRequest::<span style="color: #006600;">POST</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Are we editing an existing event?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span> = <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$event</span> = sfDoctrine::<span style="color: #006600;">getTable</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'AwardEvent'</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">forward404Unless</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$event</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// The template always gets the value of the inputs from the request object.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">setParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'code'</span>, <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">code</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">setParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'type'</span>, <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">type</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">setParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'weight'</span>, <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">weight</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">setParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span>, <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">title</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">setParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'description'</span>, <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">description</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Are we editing an existing event?</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span> = <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$event</span> = sfDoctrine::<span style="color: #006600;">getTable</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'AwardEvent'</span><span style="color: #66cc66;">&#41;</span>-&gt;<span style="color: #006600;">find</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">forward404Unless</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$event</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// No, so create a new event.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$event</span> = <span style="color: #000000; font-weight: bold;">new</span> AwardEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">code</span> = <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'code'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">type</span> = <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'type'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">weight</span> = <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'weight'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">title</span> = <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">description</span> = <span style="color: #0000ff;">$request</span>-&gt;<span style="color: #006600;">getParameter</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$event</span>-&gt;<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">setFlash</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'appMessage'</span>, <span style="color: #ff0000;">'Your modifications was saved.'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000ff;">$this</span>-&gt;<span style="color: #006600;">redirect</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'@award_list_events'</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></table></div>

<p>Let&#8217;s walk through the code. When the page is first loaded we have one thing to do: if we are on edit phase we have to read the given event&#8217;s data from the database, and set its attributes into the request object. Maybe this seems a little strange, but I think it is a good idea, because this way the template can use transparently the request object to get the values of the input fields. Don&#8217;t worry about the line <code>sfDoctrine::getTable('AwardEvent')-&gt;find($id);</code> if you are not familiar with Doctrine, it loads the event from the database via the primary key, and returns an <code>AwardEvent</code> object filled with these data.</p>
<p>After posting the form we check if we are creating a new event or editing an existing one. In the first case we create a new <code>AwardEvent</code> object, in the second case we load it from the database. Then we set up the object&#8217;s properties from the request and save the event. Finally we assign a flash message to the user and make a redirect to the list page of the events.</p>
<h2>Creating the view</h2>
<p>We are almost ready with the controller part of our program, let&#8217;s make now the view. We should create a file named <code>editEventSuccess.php</code> in the module&#8217;s <code>templates</code> directory with the following content:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> use_helper<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Validation'</span><span style="color: #66cc66;">&#41;</span> ?&gt;&lt;?php <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span> = <span style="color: #0000ff;">$sf_params</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'id'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>: <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;h1&gt;Edit event&lt;/h1&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span>: <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;h1&gt;Create event&lt;/h1&gt;<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
<br />
&lt;br /&gt;<br />
<br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span>: <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= form_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'@award_edit_event?id='</span>.<span style="color: #0000ff;">$id</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span>: <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= form_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'@award_add_event'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span>; <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= form_error<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'code'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;label <span style="color: #b1b100;">for</span>=<span style="color: #ff0000;">&quot;code&quot;</span>&gt;Code:&lt;/label&gt;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= input_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'code'</span>, <span style="color: #0000ff;">$sf_params</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'code'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;br /&gt;&lt;br /&gt;<br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= form_error<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'type'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;label <span style="color: #b1b100;">for</span>=<span style="color: #ff0000;">&quot;type&quot;</span>&gt;Type:&lt;/label&gt;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= select_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'type'</span>, options_for_select<span style="color: #66cc66;">&#40;</span><span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'admin'</span> =&gt; <span style="color: #ff0000;">'admin'</span>, <span style="color: #ff0000;">'user'</span> =&gt; <span style="color: #ff0000;">'user'</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0000ff;">$sf_params</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'type'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;br /&gt;&lt;br /&gt;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= form_error<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'weight'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;label <span style="color: #b1b100;">for</span>=<span style="color: #ff0000;">&quot;weight&quot;</span>&gt;Weight:&lt;/label&gt;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= input_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'weight'</span>, <span style="color: #0000ff;">$sf_params</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'weight'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;br /&gt;&lt;br /&gt;<br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= form_error<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;label <span style="color: #b1b100;">for</span>=<span style="color: #ff0000;">&quot;title&quot;</span>&gt;Title:&lt;/label&gt;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= input_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span>, <span style="color: #0000ff;">$sf_params</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'title'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;br /&gt;&lt;br /&gt;<br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= form_error<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;label <span style="color: #b1b100;">for</span>=<span style="color: #ff0000;">&quot;code&quot;</span>&gt;Description:&lt;/label&gt;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= textarea_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'description'</span>, <span style="color: #0000ff;">$sf_params</span>-&gt;<span style="color: #006600;">get</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'description'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; &lt;br /&gt;&lt;br /&gt;<br />
<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?</span>= submit_tag<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Save&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&lt;/form&gt;</div></td></tr></table></div>

<p>There is nothing special here, we display the form, the only important thing is to set the proper action to the form tag. You can see that we use the <code>form_error()</code> helper (that&#8217;s why we include the <code>Validation</code> helper), which displays the error message related to the given field. Maybe you are wondering a little, because we haven&#8217;t done any error handling yet. You are right, so let&#8217;s do it. </p>
<h2>Input validation</h2>
<p>To make symfony validate the user input you should create an <code>editEvent.yml</code> file in the module&#8217;s <code>validate</code> directory with following content:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><div style="font-family: monospace;">methods: &nbsp; &nbsp; &nbsp; &nbsp; [post]<br />
fields:<br />
&nbsp; code:<br />
&nbsp; &nbsp; required:<br />
&nbsp; &nbsp; &nbsp; msg: &nbsp; &nbsp; &nbsp; The code field cannot be left blank<br />
&nbsp; &nbsp; sfStringValidator:<br />
&nbsp; &nbsp; &nbsp; max: &nbsp; &nbsp; &nbsp; 50<br />
&nbsp; &nbsp; &nbsp; max_error: This field is too long (50 characters maximum)<br />
&nbsp; &nbsp; sfDoctrineUniqueValidator:<br />
&nbsp; &nbsp; &nbsp; class: &nbsp; &nbsp; AwardEvent<br />
&nbsp; &nbsp; &nbsp; column: &nbsp; &nbsp;code<br />
&nbsp; &nbsp; &nbsp; unique_error: An event with this code already exists. Please choose another one.<br />
&nbsp; type:<br />
&nbsp; &nbsp; required:<br />
&nbsp; &nbsp; &nbsp; msg: &nbsp; &nbsp; &nbsp; The type field cannot be left blank<br />
&nbsp; &nbsp; sfRegexValidator:<br />
&nbsp; &nbsp; &nbsp; match: &nbsp; &nbsp; &nbsp; Yes<br />
&nbsp; &nbsp; &nbsp; match_error: The type field should be &quot;admin&quot; or &quot;user&quot;<br />
&nbsp; &nbsp; &nbsp; pattern: &nbsp; &nbsp; /^(admin|user)$/<br />
&nbsp; weight:<br />
&nbsp; &nbsp; required:<br />
&nbsp; &nbsp; &nbsp; msg: &nbsp; &nbsp; &nbsp; The weight field cannot be left blank<br />
&nbsp; &nbsp; sfNumberValidator:<br />
&nbsp; &nbsp; &nbsp; nan_error: &nbsp;This field should be an integer<br />
&nbsp; &nbsp; &nbsp; type: &nbsp; &nbsp; &nbsp; int<br />
&nbsp; &nbsp; &nbsp; type_error: This field should be an integer<br />
&nbsp; title:<br />
&nbsp; &nbsp; required:<br />
&nbsp; &nbsp; &nbsp; msg: &nbsp; &nbsp; &nbsp; The title field cannot be left blank<br />
&nbsp; &nbsp; sfStringValidator:<br />
&nbsp; &nbsp; &nbsp; max: &nbsp; &nbsp; &nbsp; 255<br />
&nbsp; &nbsp; &nbsp; max_error: This field is too long (255 characters maximum)</div></td></tr></table></div>

<p>I won&#8217;t introduce symfony&#8217;s input validation, you can <a href="http://www.symfony-project.com/book/1_0/10-Forms#Validation%020File" title="symfony form validation file">read about it</a> in the previously mentioned symfony book. The structure of the file is straightforward, the only interesting thing is the <code>sfDoctrineUniqueValidator</code> which ensures that the value of <code>code</code> field should be unique in the <code>award_event</code> table. If we define this file in the proper directory, symfony will automatically validate the input data. If everything is okey it calls the <code>executeEditEvent</code> method of our action class, but if there is any error, it tries to call the <code>handleErrorEditEvent</code> method. If this method is not presented in the class symfony would use the <code>sfView::ERROR</code> template. While we want to show the error messages in the same template (which is the <code>sfView::SUCCESS</code>), we have to add the following method to the <code>awardAction</code> class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> handleErrorEditEvent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">return</span> sfView::<span style="color: #006600;">SUCCESS</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></table></div>

<p>And now we have the fully functional create/edit form. I hope you have found this tutorial useful, I believe this is a good solution to reuse as much code as possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.felho.hu/symfony-form-handling-receipt.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Escaping problem with WP-Syntax WordPress plugin</title>
		<link>http://blog.felho.hu/escaping-problem-with-wp-syntax-wordpress-plugin.html</link>
		<comments>http://blog.felho.hu/escaping-problem-with-wp-syntax-wordpress-plugin.html#comments</comments>
		<pubDate>Tue, 30 Oct 2007 00:48:40 +0000</pubDate>
		<dc:creator>Felho</dc:creator>
				<category><![CDATA[tutorial]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[geshi]]></category>
		<category><![CDATA[highlighting]]></category>
		<category><![CDATA[syntax]]></category>
		<category><![CDATA[wp-syntax]]></category>

		<guid isPermaLink="false">http://blog.felho.hu/escaping-problem-with-wp-syntax-wordpress-plugin</guid>
		<description><![CDATA[When I started this blog I had spent some time finding a syntax highlighter plug-in for the code snippets presented in the posts. I went to wordpress.org and I tried out some of the plug-ins, but I had some problems with all of them. After a few hours I found the WP-Syntax plug-in. I had [...]]]></description>
			<content:encoded><![CDATA[<p>When I started this blog I had spent some time finding a syntax highlighter plug-in for the code snippets presented in the posts. I went to <a href="http://wordpress.org" title="WordPress.org">wordpress.org</a> and I tried out some of the plug-ins, but I had some problems with all of them. After a few hours I found the WP-Syntax plug-in. I had had already some positive experience with the <a href="http://qbnz.com/highlighter/" title="Generic Syntax Highlighter">GeSHi </a>(Generic Syntax Highlighter) library, and I liked the <a href="http://www.emacsblog.org/2007/02/22/maximize-on-startup-part-2/" title="Maximize on startup - Part 2">demo</a> on the <a href="http://www.emacsblog.org" title="EmacsBlog">EmacsBlog</a> so I thought this is what I wanted.<span id="more-14"></span></p>
<p>I tried it out and I noticed that it doesn&#8217;t look as fancy as in the demo page. After a fast search I found that I need the following CSS addition:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><div class="css" style="font-family: monospace;"><span style="color: #6666ff;">.wp_syntax</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #cc00cc;">#<span style="color: #933;">100</span></span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#f9f9f9</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">border</span>: <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #993333;">silver</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span>: <span style="color: #933;">0</span> <span style="color: #933;">0</span> <span style="color: #933;">1</span><span style="color: #6666ff;"><span style="color: #933;">.5em</span></span> <span style="color: #933;">0</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">overflow</span>: <span style="color: #993333;">auto</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* IE FIX */</span><br />
<span style="color: #6666ff;">.wp_syntax</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; overflow-x: <span style="color: #993333;">auto</span>;<br />
&nbsp; &nbsp; overflow-y: <span style="color: #993333;">hidden</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">padding-bottom</span>: expression<span style="color: #66cc66;">&#40;</span>this<span style="color: #6666ff;">.scrollWidth</span> &gt; this<span style="color: #6666ff;">.offsetWidth</span> ? <span style="color: #933;">15</span> : <span style="color: #933;">0</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #933;"><span style="color: #933;">100</span>%</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.wp_syntax</span> table <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">border-collapse</span>: <span style="color: #993333;">collapse</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.wp_syntax</span> div, <span style="color: #6666ff;">.wp_syntax</span> td <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">vertical-align</span>: <span style="color: #000000; font-weight: bold;">top</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">padding</span>: <span style="color: #933;">2px</span> <span style="color: #933;">4px</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #6666ff;">.wp_syntax</span> <span style="color: #6666ff;">.line_numbers</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">text-align</span>: <span style="color: #000000; font-weight: bold;">right</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">background-color</span>: <span style="color: #cc00cc;">#def</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">color</span>: <span style="color: #993333;">gray</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">overflow</span>: <span style="color: #993333;">visible</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
<br />
<span style="color: #808080; font-style: italic;">/* potential overrides for other styles */</span><br />
<span style="color: #6666ff;">.wp_syntax</span> pre <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">margin</span>: <span style="color: #933;">0</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">width</span>: <span style="color: #993333;">auto</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">float</span>: <span style="color: #993333;">none</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">clear</span>: <span style="color: #993333;">none</span>;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">overflow</span>: <span style="color: #993333;">visible</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></table></div>

<p>I added it to theme&#8217;s CSS, and I was happy. I started to finish my <a href="http://blog.felho.hu/neon-zend-studio-for-eclipse">first post</a>, but I noticed a very annoying behavior of the plug-in. The sample code contained &#8220;&gt;&#8221; character, but in the post appeared &#8220;&amp;gt;&#8221;. I was a little disappointed, but I thought it will be easy to find the source of the problem.</p>
<p>First I looked into the source of the highlighter plug-in, and observed that GeSHi converts the HTML special chars to HTML entities with its own <code>hcs()</code> function.  GeSHi could not be asked not to do this conversion, so I had to modify the plug-in a little:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>79
80
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #808080; font-style: italic;">//$geshi = new GeSHi($code, $language);</span><br />
<span style="color: #0000ff;">$geshi</span> = <span style="color: #000000; font-weight: bold;">new</span> GeSHi<span style="color: #66cc66;">&#40;</span>htmlspecialchars_decode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$code</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #0000ff;">$language</span><span style="color: #66cc66;">&#41;</span>;</div></td></tr></table></div>

<p>Unfortunately the server where this blog runs has PHP older than 5.1, so I had to create my own version of <a href="http://php.net/htmlspecialchars_decode"><code>htmlspecialchars_decode()</code></a>, so I add this code to the plug-in too:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>63
64
65
66
67
</pre></td><td class="code"><div class="php" style="font-family: monospace;"><span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #000066;">function_exists</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;htmlspecialchars_decode&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">function</span> htmlspecialchars_decode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #0000ff;">$quote_style</span> = ENT_COMPAT<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> <span style="color: #000066;">strtr</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$string</span>, <span style="color: #000066;">array_flip</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">get_html_translation_table</span><span style="color: #66cc66;">&#40;</span>HTML_SPECIALCHARS, <span style="color: #0000ff;">$quote_style</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></table></div>

<p>This little hack solved my problem, I hope it could be useful for others too.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.felho.hu/escaping-problem-with-wp-syntax-wordpress-plugin.html/feed</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>


