Archive for the ‘Web Dev’ Category

How to Debug AJAX, quick and easy


2009
08.15

Debug AJAX with Firebug

Sometimes it can be difficult to debug AJAX applications, because it is asynchronous in nature the response is not normally displayed unless you provide provisions otherwise. Creating debugging systems such as debugging code, places to display the debugging information, etc, can be time consuming and detracts focus from the job at hand.

The method described below does not only apply to AJAX but to Flash and Flex as-well, it can be very helpful any time you are sending and receiving HTTP requests, synchronously or asynchronously.

A really simple and easy way to get debugging information; is to monitor the HTTP requests coming and going when you are sending your AJAX requests. So what’s the easiest way to do this? Why install Firebug of-course!

The NET tab in Firebug allows for monitoring of the coming and going requests in real time. When sending a POST request you can view the post data being sent, you can also view the server response. For instance if your request called hi.php and sent the post data; “say hi” then hi.php replied with the data “Hi from hi.php!”, Firebug would display the “say hi” post data and also the “Hi from hi.php!” response.

Post data in FirebugResponse data in Firebug

Firebug also offers excellent and intricate benchmarking facilities which allow you  to optimize your web pages to a great extent. Anyone who doesnt use Firebug should start to use it right now!

Firebug is a Firefox plug-in so you best get that too.

Singleton Class in PHP


2009
08.13

This is how I create a Singleton class in PHP.

A Singleton class can only be instantiated once. There can only ever be one copy of the class in existence in your program at one time. That is to say, there can only be one object of the type of the Singleton class, in your program.

Static class vs Singleton class

A Singletons member methods or properties can still be accessed via the -> operator, however a static member can only be accessed using the :: operator. The reason behind this is because a Singletons class is still instantiated, the new keyword is still used, with a static class it is never instantiated you just access the methods and properties.

A static class has no initialization, a static class behaves more like a library of methods then an actual object created from a class.

Below is a very simple Singleton class, to instantiate it you call $obj = ClassName::instance() . This will assign the single object to the $obj variable, the instance method returns the object.

You can still initialize data within the class because you can still use the constructor. The instance() method calls the constructor.


	class ClassName
	{
		private $VarName;

		/*
		* The reason the constructor is a private method is to
		* stop a new copy of the class being instantiated.
		*/
		private function __construct()
		{

		}

		/*
		*	Creates a single instance of the class and then returns it
		*  if an instance already exists in the static $instance var then it returns that instead
		*/
		public static function instance()
		{
			/* When a variable is declared as static it will retain its value even after the function
			* where it was defined, goes out of scope.
			*/
			static $instance;

			if ($instance == NULL)
			{
				$instance = new ClassName;
				return $instance;
			}
			else
			{
				return $instance;
			}
		}

		private SomeFunction()
		{

		}
	}

Accordion Menu


2009
03.23

I thought a nice sliding accordion type menu would be nice to use on one of my sites. So I decided to use jQuery seeing as I think jQuery is rather kewl. I checked out a jQuery demo I found (http://jquery.com/blog/2006/11/14/expandable-sidebar-menu-screencast/). I liked what I saw and liked how simple it was, so I decided to base my menu on it.

So here we go, I created the definition list in just the same way, created some CSS for it and pretty much copy n pasted the code, and I was away, I had a working menu.

There was a problem though, I wanted my menu to animate alot faster, I noticed that the opening element and the closing element didn’t open and close at the exact same speed so it had a strange shuddering effect. This was unacceptable. I then decided it would be in my better interest to create my own animation using the setInterval() function, which is what I did.

I still used jQuery for its browser abstraction and ease of use, I just didn’t use animate().

// Variables are declared here so they are available in the function and in document.ready

var fullWidth; // the full width the ’slides’ must be set to (When they are this width they are fully open)
var closedPage; // the slide that is currently close and is going to be opened
var openPage; // the slide that is currently opened and is going to be closed
var int;
var running = 0;
var step = 35; // the amount of pixels to increase or decrease the width of the slides by every milisecond

var fstep;

function slide()
{

// If the width of the slide that is busying opening + the step amount is larger then the full width

// find the difference between the opening page width and the full width and then add that to the opening page width so it equals the full width
if ( (closedPage.width() + fstep) > fullWidth ) // Makes sure the slides width always ends at the exact value of fullWidth
{
diff = fullWidth – closedPage.width();
openPage.css(‘width’, openPage.width()-diff+’px’);
closedPage.css(‘width’, closedPage.width()+diff+’px’);
}
else
{

// The slide that is opening + the step is not larger then the full width so continue adding the step as usual
openPage.css(‘width’, openPage.width()-fstep+’px’);
closedPage.css(‘width’, closedPage.width()+fstep+’px’);
}

// If the opening slides width is larger or equal to the full width, this means the slide is now fully open and the other slide is full closed

// So clear the interval and set running to 0 so the whole process can start again
if (closedPage.width() >= fullWidth)
{
int = clearInterval(int);
running = 0
}
}

$(document).ready(function() {

// Width needs to be calculated depending on the number of children items, I use this code in a CMS solution so the ‘Slides’ are added dynamicly
// and can vary in number

fullWidth = Math.round($(‘.slide-tabs’).width() – ( ($(‘.slide-tabs dt’).width()) * $(‘.slide-tabs dl’).children(‘dt’).length ));
fullWidth = fullWidth – 1; // This makes it all fit nicely

$(‘.slide-tabs dd .dddiv’).css(‘width’, fullWidth+’px’);

$(‘.slide-tabs dd:first’).css(‘width’, fullWidth+’px’); // First ‘Slide’ is open on when page is loaded

$(‘.slide-tabs dt’).bind(‘mouseover’, function() {

if (!running) // Waits for timer to exit before starting a new one
{
// The line below finds the open ‘Slide’ I want this code to be fast and efficient
// so I only want one slide to open and one to close.
openPage = $(‘.slide-tabs dl’).children(‘dd’).filter( function(i) { if ($(this).width() > 0) return 1; } );
closedPage = $(this).next();

fstep = step;

int = self.setInterval(’slide()’,1);
running = 1;
}

});

});

ezPublish: Links to return types


2009
03.22

Sometimes I find using {$node|attribute(’show’)} isn’t really quick enough so here are some links to some common return types.

The object that represents Nodes

( ezcontentobjecttreenode ) (3.10)

( ezcontentobjecttreenode ) (4.x)

The object that represents Objects

( ezcontentobject ) (3.10)

( ezcontentobject ) (4.x)

The object that represents Attributes

( ezcontentobjectattribute ) (3.10)

( ezcontentobjectattribute ) (4.x)

Debug by IP settings for ezPublish


2009
03.11

Here are some Debug settings for ezPublish when you want to see the debug info for a live site without other users seeing it, and without loging in.

These settings go in a site.ini.append.php config file, siteaccess or override doesn’t really matter.

[DebugSettings]
DebugOutput=enabled
DebugByIP=enabled
DebugIPList[]=000.000.000.000
DebugRedirection=disabled

eZpublish Dev Helper


2009
03.10

These are some reminders and notes to make developing extensions abit easier in ezPublish. It should help in coding common tasks and getting common info from ezPublish. Ofcourse the ezPublish community server is also a very valuable resource

To get data about the logged in user:

$currentUser =& eZUser::currentUser();

To get the users name:

$userContentObject =& eZContentObject::fetch( $currentUser->ContentObjectID );
$currentUserName = $userContentObject->Name;

Information about module.php syntax

Policy Functions in module.php
HOWTO REDIRECT

You need to module’s object

$Module =& $Params["Module"];

then you can do a redirect using the redirect method from the module class or you can use the redirect manager which uses the module class neway

eZRedirectManager::redirectTo($Module, false);

Content Object and Node Info

Contact Object class = eZContentObject

Node class = eZContentObjectTreeNode
HOWTO to get user group info when you don’t have access to the user section

$allUsers = eZUser::fetchContentList(); // GET LIST OF ALL USERS

foreach ( $allUsers as $k => $v ) // LOOP THROUGH THE LIST OF ALL THE USERS
{
$groups = eZUser::groups(true, $v['id']); // GET THE GROUPS FOR EACH USER

foreach( $groups as $k2 => $v2 ) // LOOP THROUGH GROUPS
{
if ( $v2->Name == “GroupName” ) // I AM LOOKING FOR USER IN THE GROUP: “GroupName”
{
$inter = ezUser::fetch($v['id']);
// IF THIS USER BELONGS TO THE “GroupName” GROUP ADD IT TO THE MAIN USER ARRAY

$users[] = $inter;
}
}

}

$users is now an array of all the user within the specified group!

ezPublish & Nice-URLS on shared hosting


2009
03.10

Remove index.php and the siteaccess name from your ezPublish URLS while using a shared hosting account, even free hosting. All your need is for .htaccess config to be supported.

First of all you need to tell ezPublish to use virtual host mode for the URLS:

In the override site.ini config file in the [SiteAccessSettings] section add the following

MatchOrder=host
HostMatchType=map
HostMatchMapItems[]=example.com;example_siteaccess
HostMatchMapItems[]=www.example.com;example_siteaccess
HostMatchMapItems[]=admin.example.com;example_siteaccess_admin
ForceVirtualHost=true
RemoveSiteAccessIfDefaultAccess=enabled

The above settings will instruct ezPublish to choose a siteaccess based on the domain name used in the URL, for example;
When the URL is http://example.com or http://www.example.com, ezPublish will display example_siteaccess, which would be your main siteaccess.

However if the URL is http://admin.example.com then ezPublish will display example_siteaccess_admin which would be your admin interface.

Now that ezPublish knows what todo we need the web server to direct anything after the domain name to index.php, for example;
http://www.example.com/products, by default /products would point to a folder called products in your root directory but if we add some rewrite rules in a .htaccess file /products can point to http://www.example.com/index.php/products or http://www.example.com/index.php?/products.

So add the following to a file called .htaccess in your root directory, if one doesnt exist your need to create it.

DirectoryIndex index.php

RewriteEngine On

RewriteCond %{HTTP_HOST} ^webdav\..*
RewriteRule ^(.*) webdav.php [L]

RewriteCond %{HTTP_HOST} ^soap\..*
RewriteRule ^(.*) soap.php [L]

RewriteRule content/treemenu/?$ /index_treemenu.php [L]
RewriteRule ^var/cache/debug.html.* – [L]
RewriteRule ^var/[^/]+/cache/debug.html.* – [L]
Rewriterule ^var/storage/.* – [L]
Rewriterule ^var/[^/]+/storage/.* – [L]
RewriteRule ^var/cache/texttoimage/.* – [L]
RewriteRule ^var/[^/]+/cache/texttoimage/.* – [L]
Rewriterule ^design/[^/]+/(stylesheets|images|javascript)/.* – [L]
Rewriterule ^share/icons/.* – [L]
Rewriterule ^extension/[^/]+/design/[^/]+/(stylesheets|images|javascripts?)/.* – [L]
Rewriterule ^packages/styles/.+/(stylesheets|images|javascript)/[^/]+/.* – [L]
RewriteCond %{SCRIPT_FILENAME} !.+\.pdf$
RewriteRule .* index.php

These Rewrite rules are default ezPublish rules and may be a-bit edited.

Copy/Paste Cache settings


2009
03.09

I have a read a few comments about ezPublish where people say every time you edit something, a template, settings even a page you have to clear the cache (which takes 20-30 seconds) to see your changes. Well guys simply do what’s below and your problems will be over!

To instantly disable all caching on your site for development paste the following into your site.ini.append.php override file

[TemplateSettings]
TemplateCompile=disabled
TemplateCache=disabled

[ContentSettings]
ViewCaching=disabled

[OverrideSettings]
Cache=disabled