<?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>blog.nanomuelle.com</title>
	<atom:link href="http://blog.nanomuelle.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nanomuelle.com</link>
	<description>nanomuelle</description>
	<lastBuildDate>Fri, 27 Jan 2012 12:52:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Symfony2 &#8211; Get action name from twig templates</title>
		<link>http://blog.nanomuelle.com/2012/01/27/symfony2-get-action-name-from-twig-templates/</link>
		<comments>http://blog.nanomuelle.com/2012/01/27/symfony2-get-action-name-from-twig-templates/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 12:49:09 +0000</pubDate>
		<dc:creator>nanomuelle</dc:creator>
				<category><![CDATA[snippet]]></category>
		<category><![CDATA[Symfony2]]></category>
		<category><![CDATA[twig]]></category>

		<guid isPermaLink="false">http://blog.nanomuelle.com/?p=400</guid>
		<description><![CDATA[This is the easyest way that I have found to get the action name from a twig template: &#160; {{ app.request.attributes.get('_template').get('name') }} &#8230;and the controller object: &#160; {{ app.request.attributes.get('_template').get('controller') }}]]></description>
			<content:encoded><![CDATA[<p>This is the easyest way that I have found to get the action name from a twig template:</p>

<div class="wp_syntax"><div class="code"><pre class="twig" style="font-family:monospace;">&nbsp;
{{ app.request.attributes.get('_template').get('name') }}</pre></div></div>

<p>&#8230;and the controller object:</p>

<div class="wp_syntax"><div class="code"><pre class="twig" style="font-family:monospace;">&nbsp;
{{ app.request.attributes.get('_template').get('controller') }}</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.nanomuelle.com/2012/01/27/symfony2-get-action-name-from-twig-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unity first impressions</title>
		<link>http://blog.nanomuelle.com/2011/04/12/unity-first-impressions/</link>
		<comments>http://blog.nanomuelle.com/2011/04/12/unity-first-impressions/#comments</comments>
		<pubDate>Tue, 12 Apr 2011 19:54:49 +0000</pubDate>
		<dc:creator>nanomuelle</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[Unity3d]]></category>

		<guid isPermaLink="false">http://blog.nanomuelle.com/?p=395</guid>
		<description><![CDATA[I am developping a simple mini-game with Unity. My first impression is that Unity is as powered as easy to use. I am really impresed. I only I had better 3D Studio skills! Woodman progress: http://nanomuelle.com/woodman Unity home page: http://unity3d.com/]]></description>
			<content:encoded><![CDATA[<p>I am developping a simple mini-game with Unity.</p>
<p>My first impression is that Unity is as powered as easy to use. I am really impresed.<br />
I only I had better 3D Studio skills!</p>
<p>Woodman progress: http://nanomuelle.com/woodman<br />
Unity home page: http://unity3d.com/</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nanomuelle.com/2011/04/12/unity-first-impressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Owner Credential &amp; Propel Admin Generator</title>
		<link>http://blog.nanomuelle.com/2010/12/03/owner-credential-propel-admin-generator/</link>
		<comments>http://blog.nanomuelle.com/2010/12/03/owner-credential-propel-admin-generator/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 12:11:37 +0000</pubDate>
		<dc:creator>nanomuelle</dc:creator>
				<category><![CDATA[admin-generator]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[propel]]></category>
		<category><![CDATA[Symfony]]></category>

		<guid isPermaLink="false">http://blog.nanomuelle.com/?p=366</guid>
		<description><![CDATA[It would be great to be able to specify in generator.yml a per object owner credential, so the generator only showed that actions to the owner of each object. Lets see how we can achieve this behavior easily. Setting up the project. Install and Configure Symfony Follow the instructions of the great jobeet tutorial to [...]]]></description>
			<content:encoded><![CDATA[<p>It would be great to be able to specify in generator.yml a per object owner credential, so the generator only showed that actions to the owner of each object. Lets see how we can achieve this behavior easily.</p>
<h2>Setting up the project.</h2>
<h3>Install and Configure Symfony</h3>
<p>Follow the instructions of the great jobeet tutorial to <a href="http://www.symfony-project.org/jobeet/1_4/Propel/en/01">install symfony and create the frontend application</a>. We will use the propel ORM.
    </p>
<h3>Instaling sfGuardPlugin</h3>
<p>Following the symfony philosophy, we do not need to reinvent the wheel.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ symfony plugin:<span style="color: #c20cb9; font-weight: bold;">install</span> sfGuardPlugin</pre></div></div>

<h3>Preparing schema and some sample data</h3>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;"># config/schema.yml
propel:
  post:
    id:
    title:    { type: varchar(255), required: true }
    content:  { type: longvarchar, required: false }
    owner_id: 
      type: integer
      foreignTable: sf_guard_user
      foreignReference: id
      required: true</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="yml" style="font-family:monospace;"># data/fixtures/fixtures.yml
sfGuardUser:
  andrew:
    username: andrew
    password: andrew
  valentine:
    username: valentine
    password: valentine
&nbsp;
Post:
  -
    title: Andrew's Post
    content: This is the content of the first Andrew's post.
    owner_id: andrew
  -
    title: Valentine's Post
    content: This is the content of the first Valentine's post.
    owner_id: valentine</pre></div></div>

<h3>Build models, load data and create the post module</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ symfony generate:app frontend
$ symfony propel:build <span style="color: #660033;">--all</span> <span style="color: #660033;">--and-load</span>
$ symfony propel:generate-admin frontend Post
$ symfony plugin:publish-assets
$ symfony <span style="color: #c20cb9; font-weight: bold;">clear</span> cache</pre></div></div>

<h2>The funny work</h2>
<p>At this point, if you go to the url <strong>ocag.localhost/post</strong>, we can see the list of posts, with two actions attached to each post: edit and delete.</p>
<p><img src="http://blog.nanomuelle.com/wp-content/uploads/2010/post_list_before.png"/></p>
<p>What we want is that the admin-generator only shows the actions edit and/or delete to the owner of each post.</p>
<h3>Enabling login module and securing post module</h3>
<p>Add the following to settings.yml</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;"># apps/frontend/settings.yml
# ...
all:
  .settings:
    # ...
    # Modules
    enabled_modules: [default, sfGuardAuth]
&nbsp;
  .actions:
    # ...
    login_module:   sfGuardAuth
    login_action:   signin
    secure_module:  sfGuardAuth
    secure_action:  secure</pre></div></div>

<p>Secure the post module</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;"># apps/frontend/modules/post/config/security.yml
default:
  is_secure: true</pre></div></div>

<h3>Edit the generator.yml</h3>
<p>First, we set the <em>owner</em> credential to an object action.</p>
<p>What we woult like the admin generator to do with this, is to hide that action to everybody but the creator of the object being procesed.</p>
<p>Saddly this is not way the default admin generator works, but we will soon see how to build our own customized admin theme with this behavior.</p>
<p>Don&#8217;t be afraid! It&#8217;s easyer than it appears.</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;"># apps/modules/post/config/generator.yml
generator:
  class: sfPropelGenerator
  param:
    model_class:           Post
    theme:                 admin
    non_verbose_templates: true
    with_show:             false
    singular:              Post
    plural:                Posts
    route_prefix:          post
    with_propel_route:     1
    actions_base_class:    sfActions
&nbsp;
    config:
      actions: ~
      fields:  ~
      list:
        #
        # setting owner credential
        #
        object_actions:
          _edit:
            credentials: owner
          _delete:
            credentials: owner
      filter:  ~
      form:    ~
      edit:    ~
      new:     ~</pre></div></div>

<h3>Creating myadmin theme</h3>
<p>Create the folder data/generator/sfPropelModule/myadmin and copy the admin theme files in order to customize them:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> data<span style="color: #000000; font-weight: bold;">/</span>generator<span style="color: #000000; font-weight: bold;">/</span>sfPropelModule<span style="color: #000000; font-weight: bold;">/</span>myadmin
$ <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-r</span> lib<span style="color: #000000; font-weight: bold;">/</span>vendor<span style="color: #000000; font-weight: bold;">/</span>symfony<span style="color: #000000; font-weight: bold;">/</span>lib<span style="color: #000000; font-weight: bold;">/</span>plugins<span style="color: #000000; font-weight: bold;">/</span>sfPropelPlugin<span style="color: #000000; font-weight: bold;">/</span>data<span style="color: #000000; font-weight: bold;">/</span>generator<span style="color: #000000; font-weight: bold;">/</span>sfPropelModule<span style="color: #000000; font-weight: bold;">/</span>admin<span style="color: #000000; font-weight: bold;">/*</span> \
data<span style="color: #000000; font-weight: bold;">/</span>generator<span style="color: #000000; font-weight: bold;">/</span>sfPropelModule<span style="color: #000000; font-weight: bold;">/</span>myadmin</pre></div></div>

<p>Configure the generator.yml to use myadmin theme</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;"># apps/modules/post/config/generator.yml
generator:
  #...
  param:
    #...
    theme:                 myadmin</pre></div></div>

<p>The partial-generator responsible of the object_actions is <strong>data/generator/sfPropelModule/myadmin/template/templates/_list_td_actions.php</strong>.  We are going to add a line that calls a user function that dinamically adds or  revoques the owner credential depending on the object being processed.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;?php $sf_user-&gt;addOwnerCredentials($'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSingularName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">') ?&gt;'</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;td&gt;
  &lt;ul class=&quot;sf_admin_td_actions&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">configuration</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'list.object_actions'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$name</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_delete'</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addCredentialCondition</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'[?php echo $helper-&gt;linkToDelete($'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSingularName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">', '</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asPhp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">') ?]'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'_edit'</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addCredentialCondition</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'[?php echo $helper-&gt;linkToEdit($'</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getSingularName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">', '</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">asPhp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">') ?]'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #339933;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
    &lt;li class=&quot;sf_admin_action_<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'class_suffix'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot;&gt;
      <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addCredentialCondition</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLinkToAction</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
    &lt;/li&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  &lt;/ul&gt;
&lt;/td&gt;</pre></div></div>

<p>Now edit myUser.class.php to code the addOwnerCredentials method</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// apps/frontend/lib/myUser.class.php</span>
<span style="color: #000000; font-weight: bold;">class</span> myUser <span style="color: #000000; font-weight: bold;">extends</span> sfGuardSecurityUser
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">protected</span> static <span style="color: #000088;">$OWNER_CREDENTIAL</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'owner'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/**
   * If the user is the owner of the $object adds owner credentials,
   * otherwise remvoques it.
   */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> addOwnerCredentials<span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isAuthenticated</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'sfOutputEscaperObjectDecorator'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRawValue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$object</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'isOwner'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$object</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isOwner</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getGuardUser</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addCredential</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$OWNER_CREDENTIAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">removeCredential</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$OWNER_CREDENTIAL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Tunning Post model</h3>
<p>The only thing we have to do now is to add the <em>isOnwer</em> method to each model that we want to be checked against owner credential, Post model in our case. So lets edit lib/model/Post.php</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// lib/model/Post.php</span>
<span style="color: #000000; font-weight: bold;">class</span> Post <span style="color: #000000; font-weight: bold;">extends</span> BasePost <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> isOwner<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sf_guard_user</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">owner_id</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$sf_guard_user</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Try it</h3>
<p>And thats all. Clear caches, login as Andrew or Alice and see how the admin generator only shows the action links to the owner of each post. See the post list logged as Andrew:</p>
<p><img src="http://blog.nanomuelle.com/wp-content/uploads/2010/post_list_after.png"/></p>
<h3>Very immportant final comment</h3>
<p>Remember that the only thing that we have done with this is to <strong>automatize  the view layer</strong> generated by the admin generator. You still <strong>must securize the edit and delete actions</strong> of each module in order to avoid undeserable people to be able to execute them.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nanomuelle.com/2010/12/03/owner-credential-propel-admin-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How I beat Bejeweled Blitz</title>
		<link>http://blog.nanomuelle.com/2010/10/21/how-i-beat-bejeweled-blitz/</link>
		<comments>http://blog.nanomuelle.com/2010/10/21/how-i-beat-bejeweled-blitz/#comments</comments>
		<pubDate>Thu, 21 Oct 2010 17:36:27 +0000</pubDate>
		<dc:creator>nanomuelle</dc:creator>
				<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.nanomuelle.com/?p=317</guid>
		<description><![CDATA[Tired of being beated week after week by my facebook friends, I decided to investigate the most hidden secrets of Bewejeled Blitz. After one or two thousands games, I finally realize that the only secret was to be the fastest. As I was not the fastest, there was only one thing I could do to [...]]]></description>
			<content:encoded><![CDATA[<p>Tired  of being beated week after week by my facebook friends, I decided to  investigate the most hidden secrets of Bewejeled Blitz. After one or two  thousands games, I finally realize that the only secret was to be the fastest. As I was not the fastest, there was only one thing I could do to  beat my friends&#8230; I would program a script that beat them for me.</p>
<p>Of course, it was an incredible opportunity to learn a little more about python also.</p>
<h3>How the script works</h3>
<p>Basically, the script do what a human do while playing:</p>
<ol>
<li>Look at the board. The script captures the Bejeweled Blitz board, and  analyzes where the jewels are. To do so, I used the PIL library to capture the screen. After that, I posterize the image reducing the number of colors, making it easy to distinguish the jewels between each other.</li>
<li>Find a goal. The script simulates a jeweled move in memory, and looks up for a goal (three-in-a-row). If there is a goal, the script moves the mouse  (PyWin32 API) and actually makes the move.</li>
<li>Repeat everything again and again until game-time ends.</li>
</ol>
<h3>How to use it</h3>
<p>The first thing that you need is to find out the (x, y) coordinates of the  Bejeweled Blitz Board. To do this, start a Bejewelled Blitz and capture the screen with the &#8211;save option of the script. The next command makes a capture of the screen and saves it with the name “my_capture.bmp”.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">c:<span style="color: #000000; font-weight: bold;">&gt;</span>python Bwbot.pyw <span style="color: #660033;">--save</span> my_capture.bmp <span style="color: #000000;">0</span> <span style="color: #000000;">0</span></pre></div></div>

<p>Then open the image with a program like GIMP <a href="https://docs.google.com/document/edit?id=1o-1XXWMraXx9QiqYt5LJ-djtSrCmEGnTHZspaD7AN4o&amp;hl=es&amp;pli=1">(http://www.gimp.org/)</a> and look up the upper left coordinates of the Bejewelled Blitz Board.</p>
<p><img src="http://blog.nanomuelle.com/wp-content/uploads/2010/bwbot_coordenadas.png" alt="Bejeweled Blitz Board coordinates" width="390px" height="109px"/></p>
<p>To make the script play, just start a new game, and execute the script passing it the x and y coordinates you has just find out, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">c:<span style="color: #000000; font-weight: bold;">&gt;</span>python Bwbot.pyw <span style="color: #000000;">527</span> <span style="color: #000000;">455</span></pre></div></div>

<h3>The script</h3>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">##</span>
<span style="color: #808080; font-style: italic;">## Bwbot.pyw</span>
<span style="color: #808080; font-style: italic;">##</span>
<span style="color: #808080; font-style: italic;">## A python script to play Bejeweled Blitz</span>
<span style="color: #808080; font-style: italic;">##</span>
<span style="color: #808080; font-style: italic;">## Author:  Fernando García</span>
<span style="color: #808080; font-style: italic;">## Licence: The script is under public domain. You can use, distribute or</span>
<span style="color: #808080; font-style: italic;">##          modify it at your own risk.</span>
<span style="color: #808080; font-style: italic;">##</span>
<span style="color: #ff7700;font-weight:bold;">import</span> win32api, win32con
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
<span style="color: #ff7700;font-weight:bold;">import</span> Image
<span style="color: #ff7700;font-weight:bold;">import</span> ImageChops
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">math</span>, <span style="color: #dc143c;">operator</span>
<span style="color: #ff7700;font-weight:bold;">from</span> PIL <span style="color: #ff7700;font-weight:bold;">import</span> ImageGrab
<span style="color: #ff7700;font-weight:bold;">from</span> PIL <span style="color: #ff7700;font-weight:bold;">import</span> ImageOps
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
<span style="color: #ff7700;font-weight:bold;">import</span> argparse
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> saveImage<span style="color: black;">&#40;</span>filename<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;Capture screen and save it to a bmp file&quot;</span>
&nbsp;
    _im = ImageGrab.<span style="color: black;">grab</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    _im.<span style="color: black;">save</span><span style="color: black;">&#40;</span>filename, <span style="color: #483d8b;">&quot;BMP&quot;</span><span style="color: black;">&#41;</span>        
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Bwbot:    
    WIDTH = <span style="color: #ff4500;">320</span>   <span style="color: #808080; font-style: italic;"># width of the board</span>
    HEIGHT = <span style="color: #ff4500;">320</span>  <span style="color: #808080; font-style: italic;"># height of the board</span>
&nbsp;
    COLS = <span style="color: #ff4500;">8</span>      <span style="color: #808080; font-style: italic;"># number of cols of the board</span>
    ROWS = <span style="color: #ff4500;">8</span>      <span style="color: #808080; font-style: italic;"># number of rows of the board</span>
    LAST_ROW_INDEX = <span style="color: #ff4500;">7</span> <span style="color: #808080; font-style: italic;"># index of the last row of the board</span>
    LAST_COL_INDEX = <span style="color: #ff4500;">7</span> <span style="color: #808080; font-style: italic;"># index of the last row of the board</span>
&nbsp;
    SQUARE_WIDTH = WIDTH / COLS   <span style="color: #808080; font-style: italic;"># width of a grid square</span>
    MID_SQUARE_WIDTH = SQUARE_WIDTH / <span style="color: #ff4500;">2</span> <span style="color: #808080; font-style: italic;"># mid width of a grid square</span>
&nbsp;
    SQUARE_HEIGHT = HEIGHT / ROWS   <span style="color: #808080; font-style: italic;"># heigth of a grid square</span>
    MID_SQUARE_HEIGHT = SQUARE_HEIGHT / <span style="color: #ff4500;">2</span> <span style="color: #808080; font-style: italic;"># mid height of a grid square</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, ox, oy<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Initializes instance variables&quot;</span>
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #808080; font-style: italic;"># play time in secconds</span>
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">playtime</span> = <span style="color: #ff4500;">55</span>  
&nbsp;
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #808080; font-style: italic;"># drag delay in miliseconds</span>
        <span style="color: #808080; font-style: italic;"># 20.-human 15.-optimal 10.-quick</span>
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">drag_delay</span> = <span style="color: #ff4500;">20</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #808080; font-style: italic;"># screen position of the board in pixels</span>
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">ox</span> = ox        <span style="color: #808080; font-style: italic;"># x screen coordinate of the board</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">oy</span> = oy        <span style="color: #808080; font-style: italic;"># y screen coordinate of the board</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #808080; font-style: italic;"># rect of screen to capture de board </span>
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">grid</span> = <span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">ox</span>, <span style="color: #008000;">self</span>.<span style="color: black;">oy</span>, <span style="color: #008000;">self</span>.<span style="color: black;">ox</span> + Bwbot.<span style="color: black;">WIDTH</span>, <span style="color: #008000;">self</span>.<span style="color: black;">oy</span> + Bwbot.<span style="color: black;">HEIGHT</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #808080; font-style: italic;"># Inner representation of the board</span>
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#91;</span><span style="color: #008000;">None</span> <span style="color: #ff7700;font-weight:bold;">for</span> _col <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>Bwbot.<span style="color: black;">COLS</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span> \
            <span style="color: #ff7700;font-weight:bold;">for</span> _row <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>Bwbot.<span style="color: black;">ROWS</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">#</span>
        <span style="color: #808080; font-style: italic;"># Sample size. The size in pixels of the square used to sample colors</span>
        <span style="color: #808080; font-style: italic;"># </span>
        <span style="color: #008000;">self</span>.<span style="color: black;">sample_size</span> = <span style="color: #ff4500;">8</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> toMouseCoordinates<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, pos<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Translates grid coordinates to mouse coordinates&quot;</span>
&nbsp;
        _x = <span style="color: #008000;">self</span>.<span style="color: black;">ox</span> + pos<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">*</span> Bwbot.<span style="color: black;">SQUARE_WIDTH</span> + Bwbot.<span style="color: black;">MID_SQUARE_WIDTH</span>
        _y = <span style="color: #008000;">self</span>.<span style="color: black;">oy</span> + pos<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> <span style="color: #66cc66;">*</span> Bwbot.<span style="color: black;">SQUARE_HEIGHT</span> + Bwbot.<span style="color: black;">MID_SQUARE_HEIGHT</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span>_x, _y<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> click<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, pos<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Make a mouse click. Pos especified in grid coordinates&quot;</span>
        _x, _y = <span style="color: #008000;">self</span>.<span style="color: black;">toMouseCoordinates</span><span style="color: black;">&#40;</span>pos<span style="color: black;">&#41;</span>
        win32api.<span style="color: black;">SetCursorPos</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span>_x, _y<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        win32api.<span style="color: black;">mouse_event</span><span style="color: black;">&#40;</span>win32con.<span style="color: black;">MOUSEEVENTF_LEFTDOWN</span>, _x, _y, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
        win32api.<span style="color: black;">mouse_event</span><span style="color: black;">&#40;</span>win32con.<span style="color: black;">MOUSEEVENTF_LEFTUP</span>, _x, _y, <span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> dragJewel<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, pos1, pos2<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Drags a jewel from pos1 to pos2&quot;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">click</span><span style="color: black;">&#40;</span>pos1<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">click</span><span style="color: black;">&#40;</span>pos2<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">wait</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">drag_delay</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> wait<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, delay<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Do nothing during delay miliseconds&quot;</span>
&nbsp;
        _delay = delay / <span style="color: #ff4500;">1000.0</span>
        _ref = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> - _ref<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> _delay:
          <span style="color: #ff7700;font-weight:bold;">pass</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> captureBoard<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Capture and preproceses Bejeweled Blitz board&quot;</span>
&nbsp;
        _im = ImageGrab.<span style="color: black;">grab</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>                           <span style="color: #808080; font-style: italic;"># capture and</span>
        _im = ImageOps.<span style="color: black;">posterize</span><span style="color: black;">&#40;</span>_im.<span style="color: black;">crop</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">grid</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># reduce colors</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> _im
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> buildMatrix<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Build the matrix of colors representing the board&quot;</span>
&nbsp;
        _im = <span style="color: #008000;">self</span>.<span style="color: black;">captureBoard</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> _row <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>Bwbot.<span style="color: black;">ROWS</span><span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">for</span> _col <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>Bwbot.<span style="color: black;">COLS</span><span style="color: black;">&#41;</span>:
                _cx = <span style="color: black;">&#40;</span>_col <span style="color: #66cc66;">*</span> Bwbot.<span style="color: black;">SQUARE_WIDTH</span><span style="color: black;">&#41;</span> + Bwbot.<span style="color: black;">MID_SQUARE_WIDTH</span>
                _cy = <span style="color: black;">&#40;</span>_row <span style="color: #66cc66;">*</span> Bwbot.<span style="color: black;">SQUARE_HEIGHT</span><span style="color: black;">&#41;</span> + Bwbot.<span style="color: black;">MID_SQUARE_HEIGHT</span>
                _rect = <span style="color: black;">&#40;</span>_cx, _cy, _cx + <span style="color: #008000;">self</span>.<span style="color: black;">sample_size</span>, _cy + <span style="color: #008000;">self</span>.<span style="color: black;">sample_size</span><span style="color: black;">&#41;</span>
                _sample_im = _im.<span style="color: black;">crop</span><span style="color: black;">&#40;</span>_rect<span style="color: black;">&#41;</span>
&nbsp;
                <span style="color: #808080; font-style: italic;"># store only de predominant color</span>
                <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row<span style="color: black;">&#93;</span> = <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span>_sample_im.<span style="color: black;">getcolors</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> evaluatePos<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, pos<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Returns True if matrix contains a goal in the specified pos&quot;</span>
&nbsp;
        _col, _row = pos
        _val = <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row<span style="color: black;">&#93;</span>
&nbsp;
        _col_m1 = _col - <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;"># col minus 1</span>
        _col_m2 = _col - <span style="color: #ff4500;">2</span> <span style="color: #808080; font-style: italic;"># col minus 2</span>
        _col_p1 = _col + <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;"># col plus 1</span>
        _col_p2 = _col + <span style="color: #ff4500;">2</span> <span style="color: #808080; font-style: italic;"># col plus 2</span>
&nbsp;
        _row_m1 = _row - <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;"># row minus 1</span>
        _row_m2 = _row - <span style="color: #ff4500;">2</span> <span style="color: #808080; font-style: italic;"># row minus 2</span>
        _row_p1 = _row + <span style="color: #ff4500;">1</span> <span style="color: #808080; font-style: italic;"># row plus 1</span>
        _row_p2 = _row + <span style="color: #ff4500;">2</span> <span style="color: #808080; font-style: italic;"># row plus 2</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># horizontal</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> _col_m1 <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> _val == <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col_m1<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row<span style="color: black;">&#93;</span>:
          <span style="color: #ff7700;font-weight:bold;">if</span> _col_m2 <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> _val == <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col_m2<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row<span style="color: black;">&#93;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
          <span style="color: #ff7700;font-weight:bold;">elif</span> _col_p1 <span style="color: #66cc66;">&lt;</span> Bwbot.<span style="color: black;">COLS</span> <span style="color: #ff7700;font-weight:bold;">and</span> _val == <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col_p1<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row<span style="color: black;">&#93;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> _col_p2 <span style="color: #66cc66;">&lt;</span> Bwbot.<span style="color: black;">COLS</span> <span style="color: #ff7700;font-weight:bold;">and</span> _val == <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col_p1<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row<span style="color: black;">&#93;</span> \
            <span style="color: #ff7700;font-weight:bold;">and</span> _val == <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col_p2<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row<span style="color: black;">&#93;</span>:
          <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># vertical</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> _row_m1 <span style="color: #66cc66;">&gt;</span>=<span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> _val == <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row_m1<span style="color: black;">&#93;</span>:
          <span style="color: #ff7700;font-weight:bold;">if</span> _row_m2 <span style="color: #66cc66;">&gt;</span>= <span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">and</span> _val == <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row_m2<span style="color: black;">&#93;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
          <span style="color: #ff7700;font-weight:bold;">elif</span> _row_p1 <span style="color: #66cc66;">&lt;</span> Bwbot.<span style="color: black;">ROWS</span> <span style="color: #ff7700;font-weight:bold;">and</span> _val == <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row_p1<span style="color: black;">&#93;</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> _row_p2 <span style="color: #66cc66;">&lt;</span> Bwbot.<span style="color: black;">ROWS</span> <span style="color: #ff7700;font-weight:bold;">and</span> _val == <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row_p1<span style="color: black;">&#93;</span> \
            <span style="color: #ff7700;font-weight:bold;">and</span> _val == <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>_col<span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>_row_p2<span style="color: black;">&#93;</span>:
          <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># no goal                </span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> swap<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, pos1, pos2<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Swap the values of pos1 and pos2 in the matrix&quot;</span>
&nbsp;
        _aux = <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>pos1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>pos1<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>pos1<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>pos1<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>pos2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>pos2<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">matrix</span><span style="color: black;">&#91;</span>pos2<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span>pos2<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#93;</span> = _aux
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> tryMove<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, pos1, pos2<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;Simulates de move in the matrix and return True if there is a goal&quot;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">swap</span><span style="color: black;">&#40;</span>pos1, pos2<span style="color: black;">&#41;</span>                                  <span style="color: #808080; font-style: italic;"># swap positions</span>
        _goal = <span style="color: #008000;">self</span>.<span style="color: black;">evaluatePos</span><span style="color: black;">&#40;</span>pos1<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #008000;">self</span>.<span style="color: black;">evaluatePos</span><span style="color: black;">&#40;</span>pos2<span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># evaluate and</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">swap</span><span style="color: black;">&#40;</span>pos1, pos2<span style="color: black;">&#41;</span>                                  <span style="color: #808080; font-style: italic;"># restore matrix</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> _goal
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> play<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;plays the game during&quot;</span>
&nbsp;
        _tnow = _tini = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">while</span><span style="color: black;">&#40;</span>_tnow - _tini<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&lt;</span> <span style="color: #008000;">self</span>.<span style="color: black;">playtime</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">buildMatrix</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">for</span> _row <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>Bwbot.<span style="color: black;">ROWS</span><span style="color: black;">&#41;</span>:
                <span style="color: #ff7700;font-weight:bold;">for</span> _col <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span>Bwbot.<span style="color: black;">LAST_COL_INDEX</span><span style="color: black;">&#41;</span>:
                    <span style="color: #808080; font-style: italic;"># evaluating pos</span>
                    _pos1 = <span style="color: black;">&#40;</span>_col, _row<span style="color: black;">&#41;</span>
&nbsp;
                    <span style="color: #808080; font-style: italic;"># try swap right</span>
                    _pos2 = <span style="color: black;">&#40;</span>_col + <span style="color: #ff4500;">1</span>, _row<span style="color: black;">&#41;</span>
                    _goal = <span style="color: #008000;">self</span>.<span style="color: black;">tryMove</span><span style="color: black;">&#40;</span>_pos1, _pos2<span style="color: black;">&#41;</span>
&nbsp;
                    <span style="color: #808080; font-style: italic;"># if not goal, try swap down</span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> _row <span style="color: #66cc66;">&lt;</span> Bwbot.<span style="color: black;">LAST_ROW_INDEX</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> _goal:
                        _pos2 = <span style="color: black;">&#40;</span>_col, _row + <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
                        _goal = <span style="color: #008000;">self</span>.<span style="color: black;">tryMove</span><span style="color: black;">&#40;</span>_pos1, _pos2<span style="color: black;">&#41;</span>
&nbsp;
                    <span style="color: #808080; font-style: italic;"># if goal, do the move </span>
                    <span style="color: #ff7700;font-weight:bold;">if</span> _goal:
                        <span style="color: #008000;">self</span>.<span style="color: black;">dragJewel</span><span style="color: black;">&#40;</span>_pos1, _pos2<span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;"># time elapsed</span>
            _tnow = <span style="color: #dc143c;">time</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
&nbsp;
    <span style="color: #dc143c;">parser</span> = argparse.<span style="color: black;">ArgumentParser</span><span style="color: black;">&#40;</span>description=<span style="color: #483d8b;">&quot;Bejeweled Blitz Bot.&quot;</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_argument</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'x'</span>, <span style="color: #008000;">type</span>=<span style="color: #008000;">int</span>, nargs=<span style="color: #ff4500;">1</span>, \
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'xpos of the Bejeweled Blitz board'</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_argument</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'y'</span>, <span style="color: #008000;">type</span>=<span style="color: #008000;">int</span>, nargs=<span style="color: #ff4500;">1</span>, \
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'ypos of the Bejeweled Blitz board'</span><span style="color: black;">&#41;</span>
    <span style="color: #dc143c;">parser</span>.<span style="color: black;">add_argument</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'--save'</span>, nargs=<span style="color: #ff4500;">1</span>, required=<span style="color: #008000;">False</span>, \
        <span style="color: #008000;">help</span>=<span style="color: #483d8b;">'capture de screen and saves it to a file'</span><span style="color: black;">&#41;</span>
    args = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> args.<span style="color: black;">save</span>:        
        saveImage<span style="color: black;">&#40;</span>args.<span style="color: black;">save</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        bwb = Bwbot<span style="color: black;">&#40;</span>args.<span style="color: black;">x</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>, args.<span style="color: black;">y</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        bwb.<span style="color: black;">play</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<h3>Requisites and Limitations</h3>
<ul>
<li>Phyton 2.7 (<a href="http://python.org/download/">http://python.org/download/</a>)</li>
<li>Phyton Imaging Library 1.1.7 <a href="https://docs.google.com/document/edit?id=1o-1XXWMraXx9QiqYt5LJ-djtSrCmEGnTHZspaD7AN4o&amp;hl=es&amp;pli=1">(http://effbot.org/downloads/#pil)</a></li>
<li>PyWin32 <a href="https://docs.google.com/document/edit?id=1o-1XXWMraXx9QiqYt5LJ-djtSrCmEGnTHZspaD7AN4o&amp;hl=es&amp;pli=1">(http://sourceforge.net/projects/pywin32/files/)</a></li>
<li>The script only works under Windows and I have only try it with a true color definition screen.</li>
</ul>
<h3>Future improvements and ToDo List</h3>
<p>My only intention was to beat my friends, something that I successfully did. My hi-score is now over 1.000.000 points, much more than any human player could even dream. But the script is far to be perfect. It lacks of a more efficient evaluation routine that selects the best move at each moment, choosing a 5-in-a-row move before the others for example. Another important defect is its inability to detect multipliers and other special jewels.</p>
<p>I am not planning to improve the script, but if you want to improve it, I will appreciate that you email me, just to know how the script evolves.</p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nanomuelle.com/2010/10/21/how-i-beat-bejeweled-blitz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Snippet: Recursively Remove .svn Directories in Linux</title>
		<link>http://blog.nanomuelle.com/2010/01/29/snippet-recursively-remove-svn-directories-in-linux/</link>
		<comments>http://blog.nanomuelle.com/2010/01/29/snippet-recursively-remove-svn-directories-in-linux/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 23:24:55 +0000</pubDate>
		<dc:creator>nanomuelle</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[subversion]]></category>

		<guid isPermaLink="false">http://blog.nanomuelle.com/?p=309</guid>
		<description><![CDATA[Just a couple of snippets found in the net to make my life easier. In the first one beware of the grave accent quotes. rm -rf `find . -type d -name .svn` or someone else proposed find ./ -name .svn &#124; xargs rm -fr]]></description>
			<content:encoded><![CDATA[<p>Just a couple of snippets found in the net to make my life easier.<br />
In the first one beware of the grave accent quotes.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-type</span> d <span style="color: #660033;">-name</span> .svn<span style="color: #000000; font-weight: bold;">`</span></pre></div></div>

<p>or someone else proposed</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> .<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #660033;">-name</span> .svn <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">xargs</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-fr</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.nanomuelle.com/2010/01/29/snippet-recursively-remove-svn-directories-in-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Snippet: PHP Calling static class methods</title>
		<link>http://blog.nanomuelle.com/2009/12/01/snippet-php-calling-static-class-methods/</link>
		<comments>http://blog.nanomuelle.com/2009/12/01/snippet-php-calling-static-class-methods/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 10:57:03 +0000</pubDate>
		<dc:creator>nanomuelle</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[snippet]]></category>
		<category><![CDATA[static]]></category>

		<guid isPermaLink="false">http://blog.nanomuelle.com/?p=284</guid>
		<description><![CDATA[Given the example class foo: class foo &#123; public static function myFunc&#40;&#41; &#123; return &#34;foo - myFunc&#34;; &#125; &#125; The class name is known in the calling context: echo foo::myFunc&#40;&#41;; The class name is stored in a php var: $class_name = &#34;foo&#34;; &#160; /** PHP 5.2.x */ echo call_user_func&#40;array&#40;$class_name, &#34;myFunc&#34;&#41;&#41;; &#160; /** PHP 5.3.x */ [...]]]></description>
			<content:encoded><![CDATA[<p>Given the example class foo:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> foo <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> myFunc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;foo - myFunc&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The class name is known in the calling context:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #b1b100;">echo</span> foo<span style="color: #339933;">::</span><span style="color: #004000;">myFunc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The class name is stored in a php var:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$class_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;foo&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #009933; font-style: italic;">/** PHP 5.2.x */</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #990000;">call_user_func</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$class_name</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;myFunc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
  <span style="color: #009933; font-style: italic;">/** PHP 5.3.x */</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$class_name</span><span style="color: #339933;">::</span><span style="color: #004000;">myFunc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>More info on <strong>call_user_func</strong>: <a href="http://es.php.net/manual/en/function.call-user-func.php">http://es.php.net/manual/en/function.call-user-func.php</a><br />
More info on <strong>static keyword</strong>: <a href="http://es.php.net/manual/en/language.oop5.static.php">http://es.php.net/manual/en/language.oop5.static.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nanomuelle.com/2009/12/01/snippet-php-calling-static-class-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Snippet: sfWidgetFormJQueryDate for Spanish culture</title>
		<link>http://blog.nanomuelle.com/2009/11/16/snippet-sfwidgetformjquerydate-for-spanish-culture/</link>
		<comments>http://blog.nanomuelle.com/2009/11/16/snippet-sfwidgetformjquerydate-for-spanish-culture/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 10:47:38 +0000</pubDate>
		<dc:creator>nanomuelle</dc:creator>
				<category><![CDATA[Symfony]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[snippet]]></category>

		<guid isPermaLink="false">http://blog.nanomuelle.com/?p=257</guid>
		<description><![CDATA[Be sure that sfFormExtraPlugin is installed: $ php symfony plugin:install sfFormExtraPlugin In the template: &#60;?php use_javascript&#40;'ui/i18n/jquery-ui-i18n.js'&#41; ?&#62; In the filter or form: $this-&#62;widgetSchema&#91;'my_date_field'&#93; = new sfWidgetFormJQueryDate&#40;array&#40; 'culture' =&#62; 'es', 'format' =&#62; '%day%/%month%/%year%', 'config' =&#62; '{&#34;showMonthAfterYear&#34;: false, &#34;firstDay&#34;: 1 }' &#41;&#41;; More info on &#8216;config&#8217; option in http://jqueryui.com/demos/datepicker.]]></description>
			<content:encoded><![CDATA[<ol>
<li>
<p>Be sure that sfFormExtraPlugin is installed:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ php symfony plugin:<span style="color: #c20cb9; font-weight: bold;">install</span> sfFormExtraPlugin</pre></div></div>

</li>
<li>
<p>In the template:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> use_javascript<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ui/i18n/jquery-ui-i18n.js'</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

</li>
<li>
<p>In the filter or form:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'my_date_field'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormJQueryDate<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'culture'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'es'</span><span style="color: #339933;">,</span>
      <span style="color: #0000ff;">'format'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'%day%/%month%/%year%'</span><span style="color: #339933;">,</span> 
      <span style="color: #0000ff;">'config'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'{&quot;showMonthAfterYear&quot;: false, &quot;firstDay&quot;: 1 }'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

</li>
</ol>
<p>More info on &#8216;config&#8217; option in <a href="http://jqueryui.com/demos/datepicker" target="blank">http://jqueryui.com/demos/datepicker</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nanomuelle.com/2009/11/16/snippet-sfwidgetformjquerydate-for-spanish-culture/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Symfony Partials: Accessing Raw Data defined in an Action</title>
		<link>http://blog.nanomuelle.com/2009/10/15/symfony-partials-accessing-raw-data-defined-in-an-action/</link>
		<comments>http://blog.nanomuelle.com/2009/10/15/symfony-partials-accessing-raw-data-defined-in-an-action/#comments</comments>
		<pubDate>Thu, 15 Oct 2009 09:54:55 +0000</pubDate>
		<dc:creator>nanomuelle</dc:creator>
				<category><![CDATA[Symfony]]></category>
		<category><![CDATA[actions]]></category>
		<category><![CDATA[partials]]></category>
		<category><![CDATA[raw]]></category>

		<guid isPermaLink="false">http://blog.nanomuelle.com/?p=195</guid>
		<description><![CDATA[Sometimes we need to pass some message from the action to a partial via template. If this message only contains plain text there is no problem, we simply define the property in the action, and in the template we pass it to the partial, like this: In the action: // modules/mymodule/actions/actions.class.php class mymoduleActions extends sfActions [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes we need to pass some message from the action to a partial via template. If this message only contains plain text there is no problem, we simply define the property in the action, and in the template we pass it to the partial, like this:</p>
<p>In the action:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// modules/mymodule/actions/actions.class.php</span>
<span style="color: #000000; font-weight: bold;">class</span> mymoduleActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeShow<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">msg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;My plain info message&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the template:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- modules/mymodule/templates/showSuccess.php --&gt;
&lt;hr /&gt;
&lt;h1&gt;My Module&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> include_partial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mypartial'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'msg'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$msg</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;hr /&gt;</pre></div></div>

<p>And in the partial:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- modules/mymodule/templates/_mypartial.php --&gt;
&lt;p&gt;<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$msg</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;</pre></div></div>

<p>The problem comes when we include a little of html in the message, for example, lets change the action:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> mymoduleActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeShow<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span> 
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">msg</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;My &lt;b&gt;html&lt;/b&gt; info message&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now, if we leave the template and the partial unchanged, you get this in the navigator:</p>
<hr />
<h1>My Module</h1>
<p>My &amp;lt;b&amp;gt;html&amp;lt;/b&amp;gt; info message</p>
<hr />
<p>Ok, it seems that we forgot to get raw data, so lets change the template:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- modules/mymodule/templates/showSuccess.php --&gt;
&lt;hr /&gt;
&lt;h1&gt;My Module&lt;/h1&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> include_partial<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'mypartial'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'msg'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$sf_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRaw</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$msg</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;hr /&gt;</pre></div></div>

<p>And now the result is:</p>
<hr />
<h1>My Module</h1>
<p>My &lt;b&gt;html&lt;/b&gt; info message</p>
<hr />
<p>That is a little better, but it is not still the desired result. To get it, we have to also change the partial:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&lt;!-- modules/mymodule/templates/_mypartial.php --&gt;
&lt;p&gt;<span style="color: #000000; font-weight: bold;">&lt;?</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sf_data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRaw</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$msg</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/p&gt;</pre></div></div>

<p>And finally we get what we wanted:</p>
<hr />
<h1>My Module</h1>
<p>My <b>html</b> info message</p>
<hr />
<p>The conclusion is that if we want to send raw data from the action to the partial, we will have to <strong>raw it in both</strong>, the template and the partial.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nanomuelle.com/2009/10/15/symfony-partials-accessing-raw-data-defined-in-an-action/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Easy configuring timezone in web server</title>
		<link>http://blog.nanomuelle.com/2009/09/18/easy-configuring-timezone-in-web-server/</link>
		<comments>http://blog.nanomuelle.com/2009/09/18/easy-configuring-timezone-in-web-server/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 11:36:22 +0000</pubDate>
		<dc:creator>nanomuelle</dc:creator>
				<category><![CDATA[snippet]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[.htaccess]]></category>

		<guid isPermaLink="false">http://blog.nanomuelle.com/?p=191</guid>
		<description><![CDATA[Simply Include the next line in the .htaccess file of your web server: php_value date.timezone Europe/Madrid Here you can see the complete List of Supported Timezones.]]></description>
			<content:encoded><![CDATA[<p>Simply Include the next line in the .htaccess file of your web server:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">php_value date.timezone Europe<span style="color: #000000; font-weight: bold;">/</span>Madrid</pre></div></div>

<p>Here you can see the complete <a href="http://us3.php.net/manual/en/timezones.php">List of Supported Timezones</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nanomuelle.com/2009/09/18/easy-configuring-timezone-in-web-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting a value to a hidden form symfony field</title>
		<link>http://blog.nanomuelle.com/2009/07/16/setting-a-value-to-a-hidden-form-symfony-field/</link>
		<comments>http://blog.nanomuelle.com/2009/07/16/setting-a-value-to-a-hidden-form-symfony-field/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 09:23:06 +0000</pubDate>
		<dc:creator>nanomuelle</dc:creator>
				<category><![CDATA[Symfony]]></category>
		<category><![CDATA[actions]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://blog.nanomuelle.com/?p=142</guid>
		<description><![CDATA[The case Sometimes, when creating a new object, we need to give a default value to a specific field but we don&#8217;t want this field to be visible in the form. A tipical example would it be a blog -> post relationship. Where we want the blog_id field to be set automatically but we want [...]]]></description>
			<content:encoded><![CDATA[<h2>The case</h2>
<p>Sometimes, when creating a <strong>new object</strong>, we need to give a <strong>default value</strong> to a specific field but we don&#8217;t want this field to be visible in the form.<br />
A tipical example would it be a <strong>blog -> post</strong> relationship. Where we want the <em>blog_id</em> field to be set automatically but we want it to be hidden.</p>
<div id="attachment_160" class="wp-caption aligncenter" style="width: 287px"><img src="http://blog.nanomuelle.com/wp-content/uploads/2009/07/5a2c445f.png" alt="Blog-&gt;Post" title="Blog-&gt;Post" width="277" height="69" class="size-full wp-image-160" /><p class="wp-caption-text">Blog->Post</p></div>
<h2>The mistake</h2>
<p>When creating a new post for a blog, we do not want the <em>blog_id</em> field to be visible, so we typically go to PostForm.class.php and unset the field <em>blog_id</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//myproject/lib/form/PostForm.class.form</span>
<span style="color: #000000; font-weight: bold;">class</span> PostForm <span style="color: #000000; font-weight: bold;">extends</span> BasePostForm
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blog_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then, we edit the action class and set a default value for the <em>blog_id</em> field:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// mypoject/modules/post/actions/actions.class.php</span>
<span style="color: #000000; font-weight: bold;">class</span> postActions <span style="color: #000000; font-weight: bold;">extends</span> sfActions
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> executeNew<span style="color: #009900;">&#40;</span>sfWebRequest <span style="color: #000088;">$request</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>  
    <span style="color: #666666; font-style: italic;">// create the form</span>
    <span style="color: #000088;">$form</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PostForm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// get blog_id parameter from the request</span>
    <span style="color: #666666; font-style: italic;">// and set the default value</span>
    <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setDefault</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;blog_id&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'blog_id'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">form</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #666666; font-style: italic;">//... rest of actions here ...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Finally, when we try to create a new post, we realize that the <em>blog_id</em> has not been set correctly.</p>
<h2>The solution</h2>
<p>Instead of unsetting the <em>blog_id</em> field, we have to change its widget to a sfWidgetFormInputHidden:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//PostForm.class.form</span>
<span style="color: #000000; font-weight: bold;">class</span> PostForm <span style="color: #000000; font-weight: bold;">extends</span> BasePostForm
<span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> configure<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// the wrong way in this case</span>
    <span style="color: #666666; font-style: italic;">// unset( $this['blog_id'] );</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// the right way in this case</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">widgetSchema</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'blog_id'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfWidgetFormInputHidden<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This way the field <em>blog_id</em> <strong>remains hidden but do exists in the form</strong>, so the <em>setDefault</em> method works correctly.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nanomuelle.com/2009/07/16/setting-a-value-to-a-hidden-form-symfony-field/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

