Good resource for MySQL table types…

March 3, 2010 by Adam · Leave a Comment 

http://www.softwareprojects.com/resources/programming/t-mysql-storage-engines-1470.html

Pet Peeve…

March 1, 2010 by Adam · Leave a Comment 

It’s never, ever, ever “an HTML”. It’s always always always “a HTML”. Read it out loud. “a haych-tee-em-el document” or “a hyper-text markup language document”. The rule is simple. If it sounds like it starts with a vowel, use an, otherwise, it’s a.

If you can’t get your head around this, put an x infront of it “an xHTML document” or “an ex-haych-tee-em-el document”. I’ll stop yelling at you then.

Learn More

Easy Multi Page Forms (or Wizards) with CakePHP

February 16, 2010 by Adam · Leave a Comment 

There is a wizard component available for CakePHP that’s been around for a very long time, however it uses a lot of code to something that doesn’t really need it. Today I needed to spread a single form across multiple pages for an app I am writing.

Here is the very simple controller action I used to achieve it:

<?php
	function add($step = 'account') { // $step is your first page

		$order = array(
			'contact_name' => $this->Auth->user('contact_name'),
			'company_name' => $this->Auth->user('company_name'),
			'account_number' => $this->Auth->user('account_number'),
			'delivery_address' => $this->Auth->user('delivery_address'),
			'invoice_address' => $this->Auth->user('invoice_address'),
			'promotional_discount' => $this->Auth->user('promotional_discount'),
			'extra_ordinary_discount' => $this->Auth->user('extra_ordinary_discount')
		); // your defaults

		if($this->Session->check('order')) {
			$order = Set::merge($order, $this->Session->read('order'));
		}

		if(!empty($this->data)) {
			$this->data['Order'] = Set::merge($order, $this->data['Order']);

			if($step == 'thanks') { // $step is your last step
				if(!$this->Order->save($this->data)) {
					$this->Session->setFlash('There was an error trying to save your order,
						please make sure you completed all steps');
					$step = 'confirm'; // $step is your last-but-one step
				}
			}
		} else {
			$this->data['Order'] = $order;
		}

		$this->Session->write('order', $this->data['Order']);

		$this->render('add' . DS . $step); //put your files in a folder and name them after the matching step, no need for validation, cakephp will 404 on any missing steps

	}
?>

Then in your view, post your form to the next page, so the accounts page posts to /orders/add/range, the range form posts to /orders/add/items and so on. You can then provide user navigation between the steps just by linking to that page.

<div id="page" class="accountinfo">
	<?php
		echo $this->Form->create('Order', array('url' => '/orders/add/range'));
		echo $this->Form->input('contact_name');
		echo $this->Form->input('company_name');
		echo $this->Form->input('account_number');
		echo $this->Form->input('order_number');
		echo $this->Form->input('date_required');
	?>
	<div id="paging">
		<?php echo $this->Html->link('Previous', array('controller' => 'orders', 'action' => 'index')); // first step so previous takes you back to the index ?>
		<?php echo $this->Form->submit('Next'); ?>
	</div>
	<?php echo $this->Form->end(); ?>
</div> <!-- page -->

I don’t think I could make this any simpler!

Do not install Nero 9 lite

February 15, 2010 by Adam · Leave a Comment 

I recently had an issue with an iso that wouldn’t burn using Windows 7’s in-built iso burner, I installed Nero-9 lite to solve the problem (which it did) however trying to uninstall Nero once it gets onto your system is a pain in the ass.

Some quick research shows you need to uninstall it, then use a seperate tool to actually remove it. You can find that here:

http://www.nero.com/enu/tools-utilities.html#tab4

Simple Sluggable Behavior for CakePHP

November 18, 2009 by Adam · Leave a Comment 

For quite a while now pretty much the defacto standard for generating automagic slugs in CakePHP has been the Sluggable Behavior by Mariano Iglesias (mariano) in the Cake Bakery (http://bakery.cakephp.org/articles/view/sluggable-behavior). I’ve never quite gotten on with this behavior, it’s not that it doesn’t do what it says, it just seems like it uses a lot of code to do it, and I prefer all of my code to be as simple as possible.

I’ve been using my own Sluggable Behavior for a while, and today I found myself needing a little more customisation, I made a few changes and now it works in exactly the same way as Mariano Iglesias’s, but using less code (and CakePHP’s built in Inflector::slug). So here is my Sluggable Behavior.

<?php
class SluggableBehavior extends ModelBehavior {
/**
 * Defaults
 *
 * @var array
 * @access protected
 */
	var $_defaults = array(
		'seperator' => '-',
		'label' => 'title',
		'field' => 'slug',
		'extension' => '.html',
		'length' => 100,
		'overwrite' => false
	);
/**
 * Initiate Sluggable behavior
 *
 * @param object $Model instance of model
 * @param array $config array of configuration settings.
 * @return void
 * @access public
 */
    function setup(&$Model, $config = array()) {
        $settings = array_merge($this->_defaults, $config);
        $this->_settings[$Model->alias] = $settings;
	}
/**
 * Before save method. Called before all saves
 *
 * Overriden to transparently manage creating and populating the slug field
 *
 * @param AppModel $Model Model instance
 * @return boolean true to continue, false to abort the save
 * @access public
 */
	function beforeSave(&$Model) {
		extract($this->_settings[$Model->alias]);

		if(!empty($Model->data[$Model->alias][$Model->primaryKey]) && !$overwrite) {
			return true;
		}

		if(!is_array($label) && empty($Model->data[$Model->alias][$label])) {
			return false;
		}

		if(is_array($label)) {
			$slug = '';
			foreach($label as $key) {
				$slug .= ife(!empty($slug), ' ', '') . $Model->data[$Model->alias][$key];
			}
		} else {
			$slug = $Model->data[$Model->alias][$label];
		}

		$slug = Inflector::slug($slug, $seperator);
		$slug = low($slug);

		if(strlen($slug) > $length) {
			$slug = substr($slug, 0, $length-1);
		}

		if(!empty($extension)) {
			$slug = $slug . $extension;
		}

		$i = 1;
		while($Model->find('count', array('conditions' => array($Model->alias . '.' . $field => $slug))) != 0) {
			if (!preg_match ('/'.preg_quote($seperator).'{1}[0-9]+$/', $slug)) {
				if(!empty($extension)) {
					$slug = $slug . $seperator . ++$i . $extension;
				} else {
					$slug = $slug . $seperator . ++$i;
				}
			} else {
				$slug = preg_replace('/[0-9]+$/', ++$i, $slug);
			}
		}

		$Model->data[$Model->alias][$field] = $slug;

		return parent::beforeSave($Model);
	}
}
?>

Usage

	var $actsAs = array(
		'Sluggable' => array(
			'label' => 'name',
			'extension' => false
		)
	);

// or

	var $actsAs = array('Sluggable');

Next Page »