Archive for December, 2010

Design Patterns Reference: Template

Thursday, December 30th, 2010

“The Template Design Pattern creates an abstract object that enforces a set of methods and functionality that will be used in common by child classes as a template for their own design.”

 

abstract class SaleItemTemplate
{
public $price = 0;

 

public final function setPriceAdjustments()
{
$this->price += $this->taxAddition();
$this->price += $this->oversizedAddition();
}

 

protected function oversizedAddition()
{
return 0;
}

 

abstract protected function taxAddition();
}

 

class CD extends SaleItemTemplate
{
public $band;
public $title;

 

public function __construct($band, $title, $price)
{
$this->band = $band;
$this->title = $title;
$this->price = $price;
}

 

protected function taxAddition()
{
return round($this->price * .05, 2);
}
}

 

class BandEndorsedCaseOfCerial extends SaleItemTemplate
{
public $band;

 

public function __construct($band, $price)
{
$this->band = $band;
$this->price = $price;
}

 

protected function taxAddition()
{
return 0;
}

 

protected function oversizedAddition()
{
return round($this->price * .20, 2);
}
}

 

// use
$externalTitle = “Waste of a Rib”;
$externalBand = “Never Again”;
$externalCDPrice = 12.99;
$externalCerealPrice = 90;
$cd = new CD($externalBand, $exteralTitle, $externalCDPrice);
$cd->setPriceAdjustments();

 

print ‘The total cost for CD item is: $’ . $cd->price;

 

$cereal = new BandEndorsedCaseOfCerial($externalBand, $externalCerealPrice);
$cereal->setPriceAdjustments();

 

print ‘The total cost for the Cereal case is: $’ . $cereal->price;

 

“When creating an object where the general steps of a design are defined but the actual logic is left to be detailed by a child class, using the Template Design Pattern is best practice.”

 

source: http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470496703,descCd-DOWNLOAD.html

 

 

Design Patterns Reference: Strategy

Wednesday, December 29th, 2010

“The Strategy Design Pattern helps architect an object that can make use of algorithms in other objects on demand in lieu of containing the logic itself.”

 

class CDusesStrategy
{
public $title = ”;
public $band = ”;

 

protected $_strategy;

 

public function __construct($title, $band)
{
$this->title = $title;
$this->band = $band;
}

 

public function setStrategyContext($strategyObject)
{
$this->_strategy = $strategyObject;
}

 

public function get()
{
return $this->_strategy->get($this);
}
}

 

class CDAsXMLStrategy
{
public function get(CDusesStrategy $cd)
{
$doc = new DomDocument();
$root = $doc->createElement(‘CD’);
$root = $doc->appendChild($root);
$title = $doc->createElement(‘TITLE’, $cd->title);
$title = $root->appendChild($title);
$band = $root->createElement(‘BAND’, $cd->band);
$band = $root->appendChild($band);

 

return $doc->saveXML();
}
}

 

class CDAsJSONStrategy
{
public function get(CDusesStrategy $cd)
{
$json = array();
$json[‘CD’][‘title’] = $cd->title;
$json[‘CD’][‘band’] = $cd->band;

 

return json_encode($json);
}
}

 

// usage
$cd = new CDusesStrategy($externalTitle, $externalBand);

 

// xml output
$cd->setStrategyContext(new CDAsXMLStrategy());
print $cd->get();

 

// json output
$cd->setStrategyContext(new CDAsJSONStrategy());
print $cd->get($cd);

 

“When it’s possible to create interchangeable objects made of self-contained algorithms to be applied to a base object, it is best practice to use the Strategy Design Pattern”

 

source: http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470496703,descCd-DOWNLOAD.html

 

 

Snippets: HTML object swf embed within a WordPress post snag it n’ publish it code excerpt

Wednesday, December 29th, 2010

<object height=”24″ width=”500″ codebase=”http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0″ classid=”clsid:D27CDB6E-AE6D-11cf-96B8-444553540000″><param value=”file=http://www.site.net/track.mp3&amp;image=http://www.site.net/trackImage.jpg” name=”flashvars”><param value=”http://www.site.com/swf/player.swf” name=”movie”><embed height=”24″ width=”500″ flashvars=”file=http://www.site.net/track.mp3″ pluginspage=”http://www.macromedia.com/go/getflashplayer” type=”application/x-shockwave-flash” src=”http://www.site.com/swf/player.swf”></object>

output XML with PHP (here loaded by Flash)

Tuesday, December 28th, 2010

using something like this in Flash AS perhaps?

playlist.load(“http://www.site.com/playlist.xml”);

wish it were this instead?

playlist.load(“http://www.site.com/playlist.php”);

instead of something like this in xml thus that must be updated…:

<songs><song file=’http://www.site.com/songs/1.mp3′  />
<song file=’http://www.site.com/songs/2.mp3′ /></songs>

you could have something like this, that updates the output xml file each time a new file is added to the songs directory:

< ? p h p

header(‘Content-type:text/xml; charset=utf-8’);
echo “<songs>”;

if ($handle = opendir(‘../songs’)) {
while (false !== ($file = readdir($handle))) {
if ($file != “.” && $file != “..”) {
//echo “$file\n”;
$file_no_extension = implode(‘.’, explode(‘.’, $file, -1));
echo “<song name=’ ” . $file_no_extension . ” ‘ file=’http://www.site.com/songs/” . $file . “‘ />”;
}
}
closedir($handle);
}

echo “</songs>”;
? >

WordPress RSS feed exclude categories or only serve one particular category

Monday, December 27th, 2010

// This will make the site feed ONLY contain category id 9.

add to wp-content/themes/your-active-theme/function.php

function myFilter($query) {
if ($query->is_feed) {
$query->set(‘cat’,’9′);
}
return $query;
}

add_filter(‘pre_get_posts’,’myFilter‘);

// To EXCLUDE categories 9 and 11:

function myFilter($query) {
if ($query->is_feed) {
$query->set(‘cat’,’-9, -11‘);
}
return $query;
}

add_filter(‘pre_get_posts’,’myFilter‘);

odd annoying Dreamweaver errors

Tuesday, December 21st, 2010

Find the Dreamweaver cache file and delete it:

C:\Documents and Settings\\Application Data\Macromedia\Dreamweaver\Configuration\WinFileCache-*.dat

done

source: http://www.gthelp.com/showthread.php?t=80230

anchor tag mailto subject body link example

Tuesday, December 14th, 2010

<a href=”mailto:?subject=Whatever you want the subject to be goes here&body=Whatever you want the body text to be goes here%0Aurl: http://www.alexyz.com/”>

WordPress Custom Structure URL Permalinks

Monday, December 13th, 2010

Admin

Settings

Permalinks

Custom Structure radio text entry field

use this:

/%postname%/

so that your posts appear as they do in this blog, here:

http://www.sitename.com/post-name

httpd.conf or .htaccess for apache WordPress redirect

Monday, December 13th, 2010

This is simply a note to save this block of code should I need it again.

The server in question is one of many cloud instances, and local development machines, in an environment using PHP, JAVA, Surf, among other technologies.

In short, it’s a pretty complex implementation/configuration…

Some URLs are being rewritten, others forwarded, directory crawling is off, but…

a legacy WordPress blog is being converted to clean/pretty URLs for SEO purposes

(which “look” like directory folder URLs)

(rather than WordPress relying on URL parameters by default)

so, along with editing the WordPress Setting for this,

the following was added to the httpd.conf file:

<IfModule modrewrite.c>

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /podcastInQuestionFolder/index.php [L]

</IfModule>

Of course, quite similar code in the .htaccess within that folder would accomplish a similar aim.

WordPress limit search form to a category

Thursday, December 9th, 2010

example:

 

<script>
function submitter()
{
track_it(‘/podcast/searchFormSubmit/’);
document.searchform.submit();
}

</script>

 

<form method=”get” name=”searchform” id=”searchform” action=”<?php bloginfo(‘url’); ?>/”>

 

<input type=”text” value=”<?php the_search_query(); ?>” name=”s” id=”s” onclick=”this.value=””/>

 

This is the important new line, 5 is an example category id

 

<input type=”hidden” name=”cat” value=”5″ />

 

<a href=”javascript:submitter()” style=”text-decoration:none;font-size:11px;margin:0 0 0 5px;position:absolute;top:0px;”><img src=”wp-content/themes/New/images/go.png” onmouseover=”this.src=’wp-content/themes/New/images/go-over.png'” onmouseout=”this.src=’wp-content/themes/New/images/go.png'” border=”0″ />

 

</a>
</form>

 

source: http://wpgarage.com/code-snippets/how-to-hack-the-wordpress-search-function-search-categories-and-child-categories/

 

WordPress Visual editor inactive

Thursday, December 9th, 2010

All of a sudden you can’t click on your visual editor tab when adding a post?
Did you recently install a new plug-in?
Disable it and try again.
It isn’t compatible with your current version…
Just a short note.

Design Patterns Reference: Singleton

Wednesday, December 8th, 2010

“The singleton Design Pattern is used to restrict the number of times a specific object can be created to a single time by providing access to a shared instance of itself.”

 

“The constructor of the object should be a protected method. This will not allow anything but the class itself to create an instance of it. Then it can make a public method to actually create, store, and provide that instance.”

 

Most common use: database connection

 

More interesting possible use, food for thought: configuration option management…

 

class InventoryConnection
{
protected static $_instance = NULL;
protected $_handle = NULL;
public static function getInstance()
{
if(!self::$_instance instanceof self){
self::$_instance = new self;
}
return self::$_instance;
}

 

protected function __construct()
{
$this->_handle = mysql_connect(‘localhost’,’user’,’pass’);
mysql_select_db(‘CD’, $this->_handle);
}

 

public function updateQuantity($band, $title, $number)
{
$query = “update CDS set amount = amount+” . intval($number);
$query .= ” where band=‘ ” . mysql_real_escape_string($band) . ” ‘ “;
$query .= ” and title=‘ ” . mysql_real_escape_string($title) . ” ‘ “;
mysql_query($query, $this->_handle);
}
}

 

class CD
{
protected $_title = ‘ ‘;
protected $_band = ‘ ‘;
public function __construct($title, $band)
{
$this->_title = $title;
$this->_band = $band;
}

 

public function buy()
{
$inventory = InventoryConnection::getInstance();
$inventory->updateQuantity($this->_band, $this->_title, -1);
}
}

 

$boughtCDs = array();
$boughtCDs[] = array(‘band’=>’Never Again’, ‘Waste of a Rib’);
$boughtCDs[] = array(‘band’=>’Therapee’, ‘Long Road’);

 

foreach($boughtCDs as $boughtCD){
$cd = new CD($boughtCD[‘title’], $boughtCD[‘band’]);
$cd->buy();

 

source: http://www.wiley.com/WileyCDA/WileyTitle/productCd-0470496703,descCd-DOWNLOAD.html