Archive for the ‘Scripts’ Category

Keyboard Shortcuts JavaScript Library

Saturday, July 21st, 2007

I have created the second version of the JavaScript Shortcut Library. It is one of my more popular scripts. This script eases the work involved in making shortcuts in JavaScript.

Despite the many JavaScript libraries that are available today, I cannot find one that makes it easy to add keyboard shortcuts(or accelerators) to your javascript app. This is because keyboard shortcuts where only used in JavaScript games - no serious web application used keyboard shortcuts to navigate around its interface. But Google apps like Google Reader and Gmail changed that. So, I have created a function to make adding shortcuts to your application much easier.

Changelog

  • The single function method was abandoned for an object with two functions
  • Shortcut Remove function added
  • New option to disable shortcuts in textarea, input fields.

If you are using this script in any of your projects, please switch to the latest version.

Previous Version Documentation

Web Installer: The Code

Wednesday, July 11th, 2007

The last two posts on web installer did not include any code. I wanted to dump all code into one post - this is it. Please note that this is what I did - you don’t have to copy my code as it is. Just look at the code and modify it according to your needs.

Getting Database Details

The Form/Frontend

<form action="" method="post">
<h1>Installation : Step 1</h1>
Please provide the database connection details...
<fieldset>
<legend>Database Details</legend>

<label for='host'>Database Host</label><input type='text' name='host' value='localhost' /><br />
<label for='db_user'>Database User</label><input type='text' name='db_user' value='root' /><br />
<label for='password'>Database Password</label><input type='text' name='password' /><br />
<label for='database'>Database</label><input type='text' name='database' value='nexty' /><br />
</fieldset>

<input type="hidden" name="step" value="2" /><br />
<input type="submit" name="action" value="Continue >>" /><br />
</form>

Backend

Make sure that the given database details are correct.

// The First step is Setting up Database connection
//					   (the '2' is NOT a typo)
if($_REQUEST['step'] == 2) {
	//Save the data to the Session
	if(isset($_REQUEST['host'])) $_SESSION['host'] = $_REQUEST['host'];
	if(isset($_REQUEST['db_user'])) $_SESSION['db_user'] = $_REQUEST['db_user'];
	if(isset($_REQUEST['password'])) $_SESSION['password'] = $_REQUEST['password'];
	if(isset($_REQUEST['database'])) $_SESSION['database'] = $_REQUEST['database'];
	if(isset($_REQUEST['url'])) $_SESSION['url'] = $_REQUEST['url'];

	if(mysql_connect($_SESSION['host'],$_SESSION['db_user'],$_SESSION['password'])) { //Try to connect to the DB.
		$QUERY['success'][] = 'Connection to Database server successful';

		if(mysql_select_db($_SESSION['database'])) {//Select the provided database.
			$QUERY['success'][] = "Database '$_SESSION[database]' selected";

		} else {
			$QUERY['error'][] = 'The given database('.$_SESSION['database'].') does not exist. Please povide a valid database.';
			$_REQUEST['step'] = 1;
		}
	} else {
		$QUERY['error'][] = 'Unable to connect to the database. Make sure that the entered details are correct';
		$_REQUEST['step'] = 1;
	}
}

Notice the over use of $_SESSION? That will come in use if I decide to add a Back button that enables the users to modify the data entered previously.

Database Creation

//Create the database only if it does not exist
//See if the database exists
$tables_sql = mysql_query("SHOW TABLES") or die(mysql_error());
$necessary_tables = array('Context','Project','Reminder','Setting','Task','TaskContext','User');
while($table = mysql_fetch_row($tables_sql)) {
	$necessary_tables = array_diff($necessary_tables,array($table[0])); //Remove the table from the array if it exists
}

//If there are no tables in the array that means that the all the necessary tables are present in the Database
//If some tables are missing, that means we have to create those tables...
if($necessary_tables) {
	$quries = <<<END
-- Insert all the SQL to create the necessary tables here...
END;

	//Execute all the queries
	$all_quires = explode(";",$quries);
	$query_count = 0;
	foreach($all_quires as $query) {
		$query = trim($query);
		if($query) {
			@mysql_query($query);
			$query_count++;
		}
	}
	$QUERY['success'][] = "Database Populated.";
} else {
	$QUERY['error'][] = "Tables already in Database - I did not overwrite it. If you want to remove the old data, please delete the tables and run the installer script agian.";
}

Saving the Database connection details


//I don't know how to escape the $ charector in heredocs - so I did this...
$config = '$config';//Heh, Heh ;-)
$system_installed = '$system_installed';
$configuration = <<<END
<?php
//Configuration file for Nexty
$system_installed = true;
$config = array(
	'db_host'		=>	'$_SESSION[host]',
	'db_user'		=>	'$_SESSION[db_user]',
	'db_password'	=>	'$_SESSION[password]',
	'db_database'	=>	'$_SESSION[database]',
	'url'			=>	'$_SESSION[url]',
	'absolute_path'	=>	'$abs'
);

END;
if(is_writable('../configuration.php')) {
        // ...Write the $config text into the configuration.php file...

	$QUERY['success'][] = 'Saved the configuration file. <a href="'.$_REQUEST['url'].'">Go to Nexty</a>';
} else {
	$QUERY['error'][] = 'Configuration file (configuration.php) is not writable. Please copy the configuration code and enter it into the "configuration.php" file. Then press continue.';
}

See the install folder for Nexty in the Subversion server.

3 Simple Steps to create a Web Installer

Friday, July 6th, 2007

Creating a web installer is not hard - as a matter of fact - it is downright easy. You just have to know a few things. Get the database details, create the database, save the details. That’s it!

Getting Database Details

Nexty Installer Screenshot

A simple four field form will suffice. The important thing to remember is that the data must be saved as session variables - as we are using a multi-page form.

As soon as you get the DB details, try to connect to the database and confirm that the DB details is correct. If the connection attempt fails, take the user back to the DB details form. Else go to step two.

Insert Initial Data

This part is easy - just run a bunch of SQL statements to create the necessary table structure and insert the initial data.

Save the Details

This should be the last step of the installation. Just take all the data from the session variables and write it to a file, say ‘configuration.php’. Write the data as PHP code - so all you have to do to retrieve the data is include the file.

The code for the installer in the next post.

What I learned from Nexty

Sunday, May 27th, 2007

Nexty Logo

Remember Nexty? Recently I was able to make the 1.0 version. Currently I am planning for the second version. But before starting on that I want to document the different things I learned when creating Nexty. These are a list of things you can expect on this series…

Client Side

  • CSS
    • Icons
    • Theming using CSS
  • JavaScript
    • Ajax - Success/Failure Pattern

Server Side

  • Installer
  • Framework
  • API
  • Distributed Apps

Before starting, a small notice - I will include links to Nexty’s internal pages in this series. Most of these pages are behind a login. So I would advice that you get an account in Nexty and login into it using the ‘Remember Me’ option enabled. This would make sure that you have the most seamless experience.

iFrame Features

Tuesday, May 22nd, 2007

iFrame Logo

In the previous post I introduced my new PHP framemwork - iFrame. It had the reasons why you should not use the framework. In these post, I will talk of the advantages you get if you use my framemwork. But remember - do not use my framework.

No routing - Fully file based

There are no complicated routing rules - the URL should specify which file should be used. For example…

http://www.example.com/user/create.php

Here, ‘user’ is the controller and ‘create’ is the action.

Folder Structure

Like many other framemworks, iFrame has a rigid folder structure.

/
+-commen.php
+-configeration.php
+-/includes/  #System files
+-index.php
+-/user/
	+-index.php
	+-create.php
+-/templates/
	+-index.php
	+-/user/
		+-index.php
		+--create.php
+-/js/
	+-/user/
		+--create.php
+-/css/
	+-/user/
		+--create.php

Auto inclustion of CSS/JS files

The system will auto include the CSS/JS files with the same file name as the current file - the files colored colored in green in the above ‘Folder Structure’ will be included automatically when ever the create.php(red color) file is called.

Library for Paging/Tagging

These two classes are great time savers. Unfortunatly they have no documentation - yet.

Small/Managable Files

The framemwork forces the code to be broken down into smaller parts. In this framemwork, one action is one file - instead of one controller per file as in other framemworks. I find this easier to manage than the other approch.

Uses OOPs only when it is required

I only use Object Orinted Programming only if there is a clear need to use it. This step made the system much simpler.

Three Layers

This framemwork tries to follow both MVC(server side 3 layer) and Content/Behaviour/Presentation(client side 3 layer) approches.

iFrame - My PHP Framework

Friday, May 18th, 2007

iFrame Logo

I have created a number of Web Applications - Nexty and Jus5 are just a few examples. I used a common code base when creating these sites - but I did not consider it a full fledged framework. However, now the code base has evolved enough to be called a framework. So, I called it a framework - the iFrame framework. I know, lame joke - geek humor ;-)

Before moving further, a word of warning - Do not use this framework for your project. There are plenty of great PHP frameworks out there - use one of those. For example…

These are professional, enterprise level frameworks. They are secure, well designed and documented. Use any of those when creating your site - do not use mine. There are no legal restrictions - the code for iFrame is under BSD License. These are the reasons why you should not use my framework…

Disadvantages

Documentation
Many Users of frameworks complain that there is not enough documentation. My framework has no such problems - it has no documentation whatsoever . They only way to find what a function is supposed to do, is to read the code.
Bugs
This system is developed, used and tested by just one person - me. So, there are many yet undiscovered bugs.
No ORM
Those who are in love with ORM will be disappointed when using iFrame - I have not implemented ORM.
Currently, only MySQL is supported
There is a database abstraction layer - but as of yet, only MySQL is supported.
And a lot more other reasons
The reasons why you should not use this framework is too numerous to list here.

Advantages

Despite all the given disadvantages, I will use this framework - these are the reasons for that…

Lightweight
This is the most lightweight framework that I have ever seen.
Helpful functions that reduce coding time

All my PHP functions are available in the framework - If you know how to use them, these functions save a lot of time. For example…

Encourages clean code
iFrame is an MVC framework(although the ‘Model’ is a bit limited for now). By default, each controller has its own folder and each action has its own file. This will greatly increase the number of files - but will reduce the number of lines in each file. Some might find it hard to use - but I find it more manageable this way.
A lot of cool features
I will talk about these ‘cool features’ in the next post.

Code

What good is a framework without the code…

View the iFrame code online

You can get the code from my subversion server - use this command…

svn checkout http://www.bin-co.com/php/scripts/iframe/code/ iframe

Jus5 - Light Weight CMS

Monday, May 7th, 2007

As promised in my previous post about the release of Nexty, I want to announce my new project - Jus5. It is not exactly ‘new’ - I have been working on it for a couple of weeks. Jus5 is a light weight CMS. It is also a total failure :-(

Download Jus5 (Updated)

See Jus5 In Action

What is Jus5?

Jus5 is perhaps the smallest Content Management System. The system only requires 9 files(less than 100KB) - without the editor. The entire system can be compressed into just 2 files - but my conscience is not letting me do that.

Features

Jus5 is a Lightweight CMS that can be used to create and manage smaller sites. It includes only the most essential features of a CMS tool.

  • Customizable Themes
  • Add/Edit/Delete Pages
  • Add/Edit/Delete Categories
  • mod_rewrite support(User Friendly URLs)
  • Client/Admin Model
  • Single Admin
  • WYSIWYG Editor Support(TinyMCE)
  • Easy to Install/Use
  • And More…

Purpose

The purpose of this software is to let people set up micro sites with the least amount of troubles. My aim is this - the web master copies the files over to the server and calls up the location in the browser and the system is in place. No configuration - no editing config files - no going through wizards to set up this software(ideally).

A good example for this is sourceforge sites - ie. the web hosting support that sourceforge site provides for open source projects. I have some OSS projects in sourceforge - and each of them have a site - nexty and bdir. The problems with this is, the site is too limited to use a professional CMS tool like Drupal or Mambo. The only other options is to manage it manually. This is what I have been doing so far - and it is getting a bit tedious.

Enter Jus5 - just upload the necessary files to the server - and you have the system running!

At least that was the plan.

Problems

If you have been in the Web development field for some time, you will immediately spot the problem when I said that I want the program to have zero configuration. You will know that at the very least, the user have to provide the database connection details.

But there is one method that can be used to overcome this limitation - SQLite. I used this method to create the entire application. The problem is not all servers have SQLite support in PHP. SourceForge don’t!

As a result, I have added MySQL driver support to this system - but now the user must configure the system before running it. Not what I wanted.

With that change I am releasing a ‘working’ version of Jus5. Please remember that this is a beta release - expect broken stuff. Don’t use it in a production system.

Nexty 1.0 Released

Monday, April 30th, 2007

Nexty Logo

Remember Nexty, the easy to use to-Do list manager using GTD principles? Well, that is ready for public release.

Nexty is a easy to use GTD tool created in PHP. It can be installed in a local server or in a online web server. The core idea behind this software is simplicity. I wanted to make a GTD tool that is the most easy to use.

Nexty Links

The next project is ready - watch this space for its announcement.

Subscribe to Feed