Custom Zend Filter: Excerpt

So you are familiar with the WordPress template tag called the_excert() right? If not the tag takes a post and trims it up to display 55 or so words and then adds ellipses to the end. I find this very useful when you just want to display a teaser. In the current Zend Framework project then I am working on for a client I needed just this. So I had to create the custom filter which is really easy. You can find a vast amount of info for Zend Filters here.

Custom Filter – Excerpt

// To call the filter use
$excerpt = new My_Filter_Excerpt();
$excerpt->filter($str); //$str is the string of text

//Put this in your library
class My_Filter_Excerpt implements Zend_Filter_Interface
{
	protected $_wordCount = 55;

	public function filter($data)
	{
		$sentance = explode(" ", $data);
		if (count($sentance) > $this->_wordCount) {
			$sentance = array_splice($sentance, 0, $this->_wordCount);
			$ellipsis = "…";
		}
		$excerpt = implode(" ", $sentance);
		echo $excerpt . $ellipsis;
	}
}

Gravatar Zend Framework View Helper

I have worked on a Zend Framework these past couple of days and needed the ability to display a users avatar. This is easy by using Automattic’s gravatar project. The easiest way to add a users avatar is to use a (this) view helper. Basically in your view you just pass the user’s email address and a desired size and your all set. Make sure in the view helper you specify the default image if the user doesn’t have an avatar to display.

You can download the source here. Comments and improvements are always welcome.

<?php
/**
 *
 * @author Shaun Farrell
 * @version 1.0
 */
require_once 'Zend/View/Interface.php';

/**
 * Gravatar helper
 *
 * @uses viewHelper Zend_View_Helper
 */
class Zend_View_Helper_Gravatar {

	/**
	 * @var Zend_View_Interface
	 */
	public $view;
	/**
	 *
	 * @var unknown_type
	 */
	protected $_gravatarUrl = "http://www.gravatar.com/avatar.php?";
	/**
	 *
	 * @var unknown_type
	 */
	protected $_defaultImage = "http://everquest.allakhazam.com/pgfx/item_576.png";

	/**
	 *
	 * @param $emailAddress
	 * @param $size
	 * @param $imgAlt
	 */
	public function gravatar($emailAddress, $size = "40", $imgAlt = "")
	{
		$gravatarId = "gravatar_id=" . md5(strtolower(trim($emailAddress)));
		$url = $this->_gravatarUrl . $gravatarId . "&default=" . urlencode($this->_defaultImage) .
			"&size=" . $size;

		echo "<img src=\"$url\" alt=\"$imgAlt\" />";
		return null;
	}

	/**
	 * Sets the view field
	 * @param $view Zend_View_Interface
	 */
	public function setView(Zend_View_Interface $view) {
		$this->view = $view;
	}
}

After the Deadline (AtD)

Yesterday I had the great opportunity to here Raphael Mudge the creator of AtD present After the Deadline at DC PHP.  Below is a recording of the talk.  I hope to write some more about AtD after I play around with it more.

Zend_Db Firebug Profiler via .ini File

So a couple of days ago I posted a way to get the DB profiler working via your bootstrap but an easier way to get things working is via your configuration .ini file.

Once again you need Firefox, FireBug, and FirePHP. Then in your .ini file add the following lines;

resources.db.isdefaulttableadapter = true
resources.db.params.profiler.enabled = true
resources.db.params.profiler.class = Zend_Db_Profiler_Firebug

The best part about this is that you can add this code only to load based on the environment set in your .htaccess or index.php file in your public folder.

Surviving The Deep End: Chapter 3 Models

Just got down reading chapter 3 of the free Zend Framework book called “Surviving The Deep End.“  Chapter 3 was about models and was very informative.  Many developers have different views on what the model should do. I am in total agreement with this description of a model in the book.  I love the topic of “Skinny Controllers, Fat Models” Vs. the “Fat Stupid Ugly Controller.”  When developing, keep your controllers small and models big.  Models are the business logic, and the controller and views are part of the presentation layer (MVC).  Another thing I didn’t realize was in Zend Framework you can call the model from the view.  There is an example in Chapter 3 on why you would do this and it is very logical.  If the controller doesn’t use the data why push it through another layer?  There is no Data police!

Now its off to read Appendix A “Performance Optimisation For Zend Framework Applications

You can read my review of Chapter 1 & 2 here.