Aero – The minimal PHP framework based on Zend

For quite a while (Since my post on LightMVC) I have given thought to writing a minimalistic framework modeled after the Zend Framework that I was (I say was cause the bloat gets on my nerves a bit) so fond of. After some kicking around I have come up with Aero ( I know I know Microsoft blah blah blah ). I finished what I consider a rough beta about a week ago so I figured I would release it to everyone. It is licensed with the New BSD license – much like Zend (You’ll notice that’s a theme).

There really isn’t a lot to say about it currently since it is still a beta. I know it needs some work but I’m always interested in peoples thoughts so either post a comment or leave any issues you find in the ticketing system on Google Code. I know the documentation is non-existent but I will be working on that soon (It will definitely be out before the official release). So please everyone feel free to test the hell out of it and if you have any questions let me know.

Aero Framework

I’m back

Well… Its been a long 5 months. Quite a bit has happened, but lets boil it down the this: I started a new job with a web development company. I’ve learned quite a bit since starting with them so I’m hoping my experience there will fuel some interesting blog posts. So keep an eye out. You never know what will pop up. For now I’m going to spend some time with some blog changes and we’ll see where it ends at.

Zend_Form Heading & Link & Password Element Hell

Well, after playing with Zend_Form for the past few days, I came across a lack of elements for the form I was working on. First, let’s look at the form I was working on. This is just a simple login form:

<div id="wrap">
    <div id="inner">
        <form action="" method="post">
            <fieldset>
            	<h1><a href="#back" title="Return to: MyApp.com">Return to MyApp.com</a></h1>
                <h2>Login to MyApp</h2>
 
                <p id="username"><input type="text" value="Please enter your username here..." onblur="if(this.value=='') this.value='Please enter your username here...';" onfocus="if(this.value=='Please enter your username here...') this.value='';" /></p>
                <p id="password"><input type="password" value="********" onblur="if(this.value=='') this.value='********';" onfocus="if(this.value=='********') this.value='';" /></p>
                <p id="forgot"><a href="#forgot">Forgotten your password? Click here.</a></p>
                <p id="loginBtn"><input type="submit" value="Login here" /></p>
            </fieldset>
        </form>
    </div>
</div>

As you can see, I have two elements here that will not work with the stock implementation of Zend_Form. For those of you that have never worked with Zend_Form_Elements, you will find that each element has two classes. The first thing you have is the actual element; after that, you have the view helper. Typically, the element class is simple and in most cases consists of just one variable, with the rest obtained from the abstract element class. Let’s take a look at my first added element: MyApp_Form_Element_Heading

class MyApp_Form_Element_Heading extends Zend_Form_Element_Xhtml
{
    /**
     * Default form view helper to use for rendering
     * @var string
     */
    public $helper = 'formHeading';
 
    /**
     * Set H level
     * 
     * @var int $level
     * @return MyApp_Form_Element_Heading
     */
    public function setLevel($level)
    {
        $this->level = $level;
 
        return $this;
    }
}

I did add in one method to this class to set the heading “level”(1-6). This is just to make the actual code a little prettier. Everything here is fairly simple, so let’s move on to the view helper: MyApp_View_Helper_FormHeading

class MyApp_View_Helper_FormHeading extends Zend_View_Helper_FormElement
{
    public function formHeading($name, $value, $attribs = null)
    {
        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable
 
        $level = $attribs['level'];
        unset($attribs['level']);
        $level = 'h' . $this->view->escape($level);
 
        $xHtml = '< ' . $level
                 . ' id="' . $this->view->escape($id) . '"'
                 . $this->_htmlAttribs($attribs)
                 . '>'
                 . $value
                 . "< /$level>"
 
        return $xHtml;
    }
}

Again, this is all straightforward if you’re familiar with PHP. The only thing that is a little fuzzy, if you’re not, is the extract() function. Essentially all this does is “extract” the array keys as actual variables. For more of how this works just check out the PHP Manual. Past this, we pull the level variable from the attributes and unset it so it doesn’t add it as its own attribute to the heading tag. Then, simply set the return value in such a way that it is xHtml valid. You will notice that I didn’t escape the $value variable. This is because in the form I am working on the heading is a link. I’m sure there are easier and cleaner ways of going about this, but I’ll worry about that later :) . Now the next element: MyApp_Form_Element_Link

class MyApp_Form_Element_Link extends Zend_Form_Element_Xhtml
{
    /**
     * Default form view helper to use for rendering
     * @var string
     */
    public $helper = 'formLink';
 
    /**
     * Set href
     * 
     * @var string $href
     * @return MyApp_Form_Element_Link
     */
    public function setHref($href)
    {
        $this->href = $href;
 
        return $this;
    }
}

Once again, you’ll notice this is extremely simple. Only added in a setHref() method to finish, again just for aesthetics. Everything else is exactly the same as above. Moving on: MyApp_View_Helper_FormLink

class MyApp_View_Helper_FormLink extends Zend_View_Helper_FormElement
{
    public function formLink($name, $value, $attribs = null)
    {
        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable
 
        $xHtml = '<a '
                 . $this->_htmlAttribs($attribs)
                 . '>'
                 . $value
                 . '</a>';
 
        return $xHtml;
    }
}

Again, this is all exactly as above. Using these two elements, I was able to build all of the form, save for one element. Notice that in the original form I was working on, that it has a pre-populated password value. Using the basic Zend_View_Helper_FormPassword you cannot do this. It will not populate the value even if you set it. I got around this with the following extension of that class: MyApp_View_Helper_FormPassword

class MyApp_View_Helper_FormPassword extends Zend_View_Helper_FormElement
{
    /**
     * Generates a 'password' element.
     *
     * @access public
     *
     * @param string|array $name If a string, the element name.  If an
     * array, all other parameters are ignored, and the array elements
     * are extracted in place of added parameters.
     *
     * @param mixed $value The element value.
     *
     * @param array $attribs Attributes for the element tag.
     *
     * @return string The element XHTML.
     */
    public function formPassword($name, $value = null, $attribs = null)
    {
        $info = $this->_getInfo($name, $value, $attribs);
        extract($info); // name, value, attribs, options, listsep, disable
 
        // is it disabled?
        $disabled = '';
        if ($disable) {
            // disabled
            $disabled = ' disabled="disabled"';
        }
 
        // determine the XHTML value
        $valueString = ' value="'.$value.'"';
        if (array_key_exists('renderPassword', $attribs)) {
            if ($attribs['renderPassword']) {
                $valueString = ' value="' . $this->view->escape($value) . '"';
            }
            unset($attribs['renderPassword']);
        }
 
        // XHTML or HTML end tag?
        $endTag = ' />';
        if (($this->view instanceof Zend_View_Abstract) && !$this->view->doctype()->isXhtml()) {
            $endTag= '>';
        }
 
        // render the element
        $xhtml = '<input type="password"'
                . ' name="' . $this-/>view->escape($name) . '"'
                . ' id="' . $this->view->escape($id) . '"'
                . $valueString
                . $disabled
                . $this->_htmlAttribs($attribs)
                . $endTag;
 
        return $xhtml;
    }
 
}

The only thing that I changed in this class is the value line. I changed it from
$valueString = ' value=""';
to
$valueString = ' value="'.$value.'"';
to allow for the population of the value for the class.

Well, that’s the end of this long blog post. I have to say that no matter how much headache this has caused me, the Zend_Form class is extremely clean and powerful. The validation and filtering tools make checking forms extremely easy. Add to this the fact that even when you don’t have the element you’re looking for in the stock implementation, its not that hard to add it, Zend_Form is a powerful tool for any developer to have in their bag of tricks.

Expanded Zend_Form_Decorator_Label

So, after getting back to working with the Zend Framework on a new project(more on this later), I met back up with an old enemy: Zend_Form. This particular class has caused me some headache in the past, and eventually I just opted to scrap the whole thing and do it manually. With this project I was determined to make it work for me, and so far, it has worked out just fine. After going through my first form (a login form), I found something that I hadn’t noticed before that gave me a headache when I searched for a fix to it. For a visual look at the following:

<label for="password" class="loginLabel optional">Password:</label>

What I want you to see here are the classes. The “optional” is added by default by Zend_Form_Decorator_Label. For some reason that escapes me, the developers of this particular decorator didn’t see fit to add in the ability to not add this class. After searching on Google for a way to remove this (and coming up with nothing), I decided the best answer to this problem was a customer decorator. So I set forth to expanding the label decorator to fit what I thought it should be a little bit more. So without further ado, lets take a look at the class I came up with (minus the documentation to shorten it a bit).

class MyApps_Form_Decorator_Label extends Zend_Form_Decorator_Label
{
    public function getClass()
    {
        $class   = '';
 
        $decoratorClass = $this->getOption('class');
        if (!empty($decoratorClass)) {
            if(is_array($decoratorClass)) {
                foreach ($decoratorClass as $dClass) {
                    $class .= " $dClass";
                }
            } else {
                $class .= ' ' . $decoratorClass;
            }
        }
 
        return trim($class);
    }
 
    public function render($content)
    {
        $element = $this->getElement();
        $view    = $element->getView();
        if (null === $view) {
            return $content;
        }
 
        $label     = $this->getLabel();
        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        $tag       = $this->getTag();
        $class     = $this->getClass();
        $options   = $this->getOptions();
 
 
        if (empty($label) && empty($tag)) {
            return $content;
        }
 
        if (!empty($label)) {
            $class == '' ? NULL : $options['class'] = $class;
            $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options); 
        } else {
            $label = '&nbsp;';
        }
 
        if (null !== $tag) {
            require_once 'Zend/Form/Decorator/HtmlTag.php';
            $decorator = new Zend_Form_Decorator_HtmlTag();
            $decorator->setOptions(array('tag' => $tag));
            $label = $decorator->render($label);
        }
 
        switch ($placement) {
            case self::APPEND:
                return $content . $separator . $label;
            case self::PREPEND:
                return $label . $separator . $content;
        }
    }
}

Let’s take it from top to bottom. Obviously, I’m expanding the Zend_Form_Decorator_Label class. This is because I do not want to overwrite the WHOLE class, just two functions. I’m not going to post the unmodified functions in this post, but they can be found HERE. Here is the first function.

    public function getClass()
    {
        $class   = '';
 
        $decoratorClass = $this->getOption('class');
        if (!empty($decoratorClass)) {
            if(is_array($decoratorClass)) {
                foreach ($decoratorClass as $dClass) {
                    $class .= " $dClass";
                }
            } else {
                $class .= ' ' . $decoratorClass;
            }
        }
 
        return trim($class);
    }

Again from top to bottom I remember the $element variable due to the fact that it was not needed in the function, since I’m removing the “optional” and “required” class. Then I added a foreach() statement to the class section to allow for multiple classes in the configuration of the label. I have to admit that it wasn’t as hard as I anticipated. And now on to the render function.

    public function render($content)
    {
        $element = $this->getElement();
        $view    = $element->getView();
        if (null === $view) {
            return $content;
        }
 
        $label     = $this->getLabel();
        $separator = $this->getSeparator();
        $placement = $this->getPlacement();
        $tag       = $this->getTag();
        $class     = $this->getClass();
        $options   = $this->getOptions();
 
 
        if (empty($label) && empty($tag)) {
            return $content;
        }
 
        if (!empty($label)) {
            $class == '' ? NULL : $options['class'] = $class;
            $label = $view->formLabel($element->getFullyQualifiedName(), trim($label), $options); 
        } else {
            $label = '&nbsp;';
        }
 
        if (null !== $tag) {
            require_once 'Zend/Form/Decorator/HtmlTag.php';
            $decorator = new Zend_Form_Decorator_HtmlTag();
            $decorator->setOptions(array('tag' => $tag));
            $label = $decorator->render($label);
        }
 
        switch ($placement) {
            case self::APPEND:
                return $content . $separator . $label;
            case self::PREPEND:
                return $label . $separator . $content;
        }
    }

The modifications here were quite simple. I removed the $id variable since it wasn’t needed. the only other change is this:

$class == '' ? NULL : $options['class'] = $class;

This is just to keep the decorator from adding class=”” to the label if you leave the class section of the config empty.

So to wrap up, these are just a few small changes to the class that I think make it work a little more like the way I want it to. I’m interested in what anyone has changed in Zend Form decorators to make them work differently. That is all I have for now.

To framework or not to framework?

It seems as time goes by there are more and more PHP frameworks, and if you follow any forums I can almost guarantee that you have see quite a few “I need help writing my framework” posts.  I’ve followed along with one or two of the larger ones (Zend Framework mostly), and it has become obvious to me that too many of them are trying to be the Swiss army knife of PHP. My biggest question is becoming – At what point do you pass usefulness and jump straight into bloat. As an example Zend Framework. I have followed it since version 1.0.0 or so, and you can tell as the framework itself gets bigger it gets so much slower. So again I pose the question when do you just have to cut the bloat?

Continue Reading

Email Address Image

In the current times of endless spam we need more and more ways to combat it. To fight the bots that crawl the web looking for email addresses I have made the following script. All it does is take 2 get variables and turn it into an image of a text email address. As such the email address is not copy and paste-able. If anyone has any questions or ideas for the script let me know.

Continue Reading

Welcome to the new blog!

Well for those that know me, you know that I’ve been playing around with the idea of writing my own blog system for quite sometime. Well I gave that up. This site is run with wordpress. I will be expanding the site itself as time goes on, but just as a side project. Currently I’m focusing most of my time to getting the theme right. I ask for your patience during the time of actually playing with theme and customizing everything. If you find any issues please let me know. At this time I haven’t tested the theme on anything except Firefox in a linux environment, so if you find problems in a different browser or a different OS please feel free to let me know and I will add it to my TODO list.

Now for those who don’t know me. My name is Christian A.K.A. xistins (pronouced existence). I’ve been programming for about 10 years now but recently started working with web languages. Before that most of my experience was in C, and VB (Depending on the OS). I am finding the transistion over to the new language wasn’t as difficult as I thought it would be. This blog will probably be mostly on programing. I hope that you find something to help you here. As always if you have a comment or question on any of the posts please post a comment on that post. If you would like to request a certain post please just send me and IM or e-mail and I will see what I can do to get something put together for you.

So what can you expect from Xistins.com in the future? Well as time goes on I will be adding several things. These should include tutorials (video and written) a forum of the community should I actually generate  one and maybe a few other things as time goes on. You can expect several rants about programming problems I come across(I will not pretend to be an expert) as well as several posts on how I fixed them(I hope these help you). As with any good site the this one should grow with the community so if you have any recommendations please let me know.

Well thats going to be all for now. I’m going to get back to working on the theme and I hope to bring you my first real post sometime soon.


EDIT:

I validated all of the HTML and CSS (WTF was wordpress thinking….). The site still doesn’t have anything that makes it “stand out” so I’m still looking for that little something. If anyone has any ideas let me know.