Remember the code? http://code.neox.net a place where I store my secret powerful coding snippets, or just to remember how to do things Fri, 19 Jun 2009 11:36:19 +0000 http://wordpress.org/?v=2.8 en hourly 1 Open Flash Chart http://code.neox.net/2009/06/18/open-flash-chart/ http://code.neox.net/2009/06/18/open-flash-chart/#comments Thu, 18 Jun 2009 22:38:42 +0000 HanaDaddy http://code.neox.net/?p=42 Today I found this website Open Flash Chart project.

It’s a FREE flash chart component and they are very beautiful! They have line, pie, and bar charts and more. I am going to try this today later, but I am just make a quick post so I can review back again.


This image is not actual flash, by the way. I just don’t have time to set it up yet.

Here is the authors comments why this beautiful commercial quality flash component is FREE.

And it’s really free?!

Yes. Once upon a time I had to deal with a company who sell flash charting components, their component had a bug that I needed fixing, so I emailed them about it asking when it’d be fixed. (Remember that I had paid real money for this software.) They were so incompetent, rude and obnoxious that after three or four weeks of emails I thought to myself “I could learn Flash and Actionscript and write my own charting component, release it as Open Source, host it on sourceforge and build up a community of helpful coders faster than they can fix a single bug.” And that is what I did. And that is why it is free. I guess the moral of the lesson is: don’t piss off your customers.

Only if I had that passion , knowledge, and the time to do this kind work. I envy him. I would like to create my own version of flash movie player, you know. But I am too lazy. :)

Website: Open Flash Chart project.
Open Flash Chart version 2

Thank you for visiting Remember the code?

]]>
http://code.neox.net/2009/06/18/open-flash-chart/feed/ 2
Eclipse PDT Code Completion and Zend Framework http://code.neox.net/2009/03/10/eclipse-pdt-code-completion-and-zend-framework/ http://code.neox.net/2009/03/10/eclipse-pdt-code-completion-and-zend-framework/#comments Tue, 10 Mar 2009 04:22:34 +0000 HanaDaddy http://code.neox.net/?p=38

Hello, Thank you for visiting my remember the code blog.

I am currently trying to learn how to use Zend Framework. It is a very good and flexible framework and easier to understand compared to CakePHP or Symfony frameworks. Well I was trying some example codes with the Eclipse 3.4 & PDT 2.0 package and found some issues that code completion does not work as I want it to.

One of the biggest problem was that the code completion does not work with Zend_Regsitry. When you retrieve a object from the registry and try to execute the code completion (you can force it by Ctrl + Space), it doesn’t work, since Eclipse doesn’t know what type of data it is.

I really like Eclipse but with partially working code completion, it is very annoying and I was really hoping for the better working code completion function.

So I was searching for the various website for any quick solutions and here they are. I spent the whole day yesterday, and it was very difficult for me to gather all these information. So please enjoy and leave some comments if you can. Thanks.

1. Use PHPDoc Style definition to define data type of your custom functions arguments and return value.

//Here is the PHPDoc defined by myself right above the custom function
/**
 *
 * @param  mixed $config Some configuration information for the DB connection 
 * @param  CustomClass $object Special Custom Class  
 * @return Zend_Db_Adapter_Abstract
 * @throws Zend_Db_Exception
 */
function getDbAdapter($config,$object)
{
	//In the below line, Code Completion will work since Eclipse knows the type from the 
	//PHPDoc @param entry.
	$object->  
 
	return $db;
}
 
 
$db=getDbAdapter($config,$object);    
//Code Completion will also work with $db variable below since the return type was defined 
// in the getDbAdapter function's PHPDoc.
$db->

2. Providing code completion hints for the Class member variable.
Still PHPDoc is the answer for the code completion to work with class member variables.

class HelloClass
{	
	/**
	 *  @var Zend_Db_Adapter_Abstract
	 */
	public $db2;
 
	public function __construct()
	{
		$this->db2 = Zend_Registry::get('dbAdapter');
 
		//Code Completion will work since @var PHPDoc was defined with the necessary class type 
		// in the class member definition above.
		$this->db2->
	}
}

3. Make code completion working with any variable
In this case, use standard comment format with @var. This method should be used only when the code completion does not automatically work.

/* @var $variable_name ClassName */

Working Example

$registry = Zend_Registry::getInstance();
 
/* @var $db3 Zend_Db_Adapter_Abstract */
$db3=$registry->dbAdapter;  //before using the variable for the code completion, the variable must be initialized.
$db3->

One thing to be careful with the third convention is that you need to initialize the variable first before testing code completion. Most of the time, it works wonderfully, but I found that it is not working with the direct call of Zend_Registry’s get method.

/* @var $db3 Zend_Db_Adapter_Abstract */
$db3 = Zend_Registry::get('dbadapter');
$db3-> //code completion would not work because...

The reason is that the the method called had PHPDoc with @return value ‘mixed’ or ‘unknown_type’. Here is the get method from the Registry.php. As you can see, the @return value is ‘mixed’.

/**
 * getter method, basically same as offsetGet().
 *
 * This method can be called from an object of type Zend_Registry, or it
 * can be called statically.  In the latter case, it uses the default
 * static instance stored in the class.
 *
 * @param string $index - get the value associated with $index
 * @return mixed
 * @throws Zend_Exception if no entry is registerd for $index.
 */
public static function get($index)
{
    $instance = self::getInstance();
 
    if (!$instance->offsetExists($index)) {
        require_once 'Zend/Exception.php';
        throw new Zend_Exception("No entry is registered for key '$index'");
    }
 
    return $instance->offsetGet($index);
}

Another non-working case is when the variable is assigned again after the first assignment.

/* @var $db3 Zend_Db_Adapter_Abstract */
$db3 = $abc;
$dbx-> //Code completion is working here.
$db3 = $def;
$db3-> //code completion is not working here because $db3 was assigned more than once

Then what do you do if you want to use Zend_Registry::get() without @var comment?
You can create a custom function or class method with PHPDoc with proper @return type defined. Inside the function, the Zend_Registry::get() is called .

class GlobalUtil
{
	/**
	 * @return Zend_Db_Adapter_Abstract
	 */
	public static function getDbAdapter($index='')
	{
		if ($index == '') $index='dbadapter';
		return Zend_Registry::get($index);
	}
}
 
 
$db=GlobalUtil::getDbAdapter();
$db-> //Code completion is working here.

There will not be much cases of using the third convention since most cases, the Eclipse will be able to handle the code completion automatically. However it’s good to know how to make it work.

Here is my last comment on the code completion. In order for the code completion to work properly, you have to have the Zend Framework files inside the project folder.

Zend Framework data files are about 20MB and I didn’t want to make duplicated copies for different php project, so I made a central location folder to store Zend library data and shared it with other PHP project by adding this folder to each project’s include path.

This idea seemed OK at first, but I realized that for some cases, the code completion didn’t work even if it was working OK when the Zend Library was located within the project folder!

The solution is very simple. You can just create a new folder within the project folder and use the advanced button to enable the ‘Link to folder in the file system’ option. Then you can browse and set the actual library folder where the actual Zend framework files are located. This will result the same effect as if the external folder is located within the project folder. Now we can have the desired code completion fully working.

So if you follow my above code completion guide , I believe that you will be able to enjoy the code completion for the most of the time. If you find the cases that the code completion is still not working , leave me a comment! I will gladly help you to resolve the problem.

]]>
http://code.neox.net/2009/03/10/eclipse-pdt-code-completion-and-zend-framework/feed/ 13
hosting migration – moving email data from Mbox to Maildir http://code.neox.net/2008/06/25/hosting-server-migration-moving-email-data/ http://code.neox.net/2008/06/25/hosting-server-migration-moving-email-data/#comments Wed, 25 Jun 2008 17:11:47 +0000 HanaDaddy http://code.neox.net/?p=36 Recently I moved to a new hosting provider . It’s much better than the previous one. I used to use a dedicated server but I have no time to take care the server and softwares and it was much expensive anyway. This new server provides cpanel and I like it . Its easy to use and provides powerful functions.

But there was no function to migrate or import existing mails into the newly created email account.

So I have searched the Internet and found mb2md.pl . Mbox to Maildir migration perl utility. You can download and save it into PATH registered folder and rename it to mb2md and chmod 755 mb2md to execute it.

Inbox import : Importing source_mbox_file into current maildir account. Note that /home/userid/target_dir is the root folder of your maildir account. You will need to use full path for both source file and target dir.

mb2md -s  /home/userid/source_mbox_file -d /home/userid/target_dir

Multi files in a folder : multi mbox files are located in /home/userid/source_dir and the /home/userid/target_dir is the root of your maildir account. Also you need to use full path for source and target dir.

mb2md -s /home/userid/source_dir -R -d /home/userid/target_dir

After importing mutliple mboxes , you want to login to your Squirrelmail or Roundcube webmail and subscribe mailboxes to make them showing in the mailbox list.

]]>
http://code.neox.net/2008/06/25/hosting-server-migration-moving-email-data/feed/ 1
SarahAjax Javascript Ajax Class http://code.neox.net/2008/06/03/sarahajax-javascript-ajax-class/ http://code.neox.net/2008/06/03/sarahajax-javascript-ajax-class/#comments Wed, 04 Jun 2008 03:17:17 +0000 HanaDaddy http://code.neox.net/?p=34 Download sarahajax.zip

Okay, here is my SarahAjax Javascript Class. You can use it without hassle!

You can either inherit and ‘action’ function or by prototyping a function named ‘action’. ‘action’ function will be called when the request to the resource is successful.

This is SarahAjax Class.

// 6/2/2008 HanaDaddy  hanadaddy at gmail.com
 
/* SarahAjax is a very simple ajax class.
 * url: target url to call. It is usally dynamic script or a simple page return xml or text.
 */
function SarahAjax(url){
	this.url=url;
	this.reqobj ; 
	this.method="GET";
	this.async= true; //if false , the connection will be synchronous ( you have to wait for the reply)
	//this.action = function (obj) { }  // Success reply handler. : youre responsibilty 
	//this.error  = function (obj) { }  // Error reply handler : your responsiblity to implement.	
 
	this.sendRequest = function() {
		if(this.reqobj == null){
    			if (window.XMLHttpRequest) {
				this.reqobj = new XMLHttpRequest();
    			} else if (window.ActiveXObject) {
    				this.reqobj = new ActiveXObject("Microsoft.XMLHTTP");
    			}
		}
 
		if (!this.action){
			alert("SaraAjax:Action member function is not defined! We need the reply handler!");
			return;
		}
		this.reqobj.open(this.method, this.url, this.async);
 
		var sajaxreq = this; // this is necessary to access myself in the callback function
		this.reqobj.onreadystatechange = function() { sajaxreq.callback(); }
 
		this.reqobj.send(null);
	}
 
	this.callback = function () {
 
		if (this.reqobj.readyState == 4) {
			if (this.reqobj.status == 200) {
				//alert("reply is back?"+this.reqobj.responseText);
				this.action(this.reqobj);
			}else{
			 	//alert("There was a problem retrieving data:\n" + this.reqobj.statusText);
			 	if (this.error){
			 		this.error(this.reqobj);
			 	}
			}
		}		
	}	
}


Click here for Usage Example1: Prototyping
How to use:

  1. Include the script sarahajax.js file in your html file.
  2. Implement ‘action‘ function by prototyping. This function is only called when request reply is OK.
  3. Optionally add ‘error‘ function. It is called by SarahAjax object when there was error occured or the request reply is not normal.
  4. Initialize a SarahAjax object with Target URL to send request to.
  5. Call the object’s ‘sendRequest()‘ method to send request to the URL
<html>
<head><title>SarahAjax Testing 1 using prototyping</title>
 
 
<script type="text/javascript" src="sarahajax.js"></script>
<script type="text/javascript">
 
//The action function must have the  argument for XMLHttpRequest object
SarahAjax.prototype.action= function (obj) {
	//obj.responseText : Regualar string
	//obj.statusText : Showing Requst Result Status
	//obj.responseXML : XML string
	var div = document.getElementById("result");
	if (div)
		div.innerHTML = obj.responseText; 
	else
		alert("hey where should I show the reply text?");
 
}
 
//error functino is optional, but it's a good idea to create one.
SarahAjax.prototype.error = function(obj){
	alert(obj.statusText);
 
}
 
//Now create an object. The request will be sent to target_app.php
var sajax = new SarahAjax("target_app.php");
 
 
</script>
 
<style type="text/css">
#result {
	border:1px dashed black;
	width:300px;
	height:30px;
}
</style></head>
<body>
<h2>This is SarahAjax Testing Page</h2>
<div id="result">
This is where the result should show.
</div>
 
<a href='javascript:sajax.sendRequest();'>Send Request</a>
 
</body>
</html>

Click here for Usage Example2: inherit SarahAjax class
You can also use the class by creating a subclass. Please Click on the title to see the source in action.

Download sarahajax.zip

]]>
http://code.neox.net/2008/06/03/sarahajax-javascript-ajax-class/feed/ 0
Javascript Class HowTo http://code.neox.net/2008/06/03/javascript-class-howto/ http://code.neox.net/2008/06/03/javascript-class-howto/#comments Tue, 03 Jun 2008 04:29:18 +0000 HanaDaddy http://code.neox.net/?p=33 Great Tutorial on Javascript Classes

function Person (firstname,lastname,age){
 
	this.firstname=firstname;
	this.lastname=lastname;
	this.age=age;
 
	this.introduction = function (type) {
		if (type=='simple'){
			alert("Hi, I am " + this.firstname +".");
		}else{
 
			alert("Hello, my name is "+this.firstname + " " + this.lastname+"." +
			 	" I am " + this.age + " years old.");
		}	
	}
 
}
 
var p = new Person("Hana","Daddy",30);
 
p.introduction("simple");
p.introduction();

Creating a Javascript class is just like creating a regular function. Two things to remember.

  • Use 'this' to access member variables.
  • How to define member functions

The above will show alert message dialog.
First, “Hi, I am Hana”.
Second, “Hello, my name is Hana Daddy. I am 30 years old.”.

Now by using prototyping, you can add additional functions to the predefined classes. Also all the initialized class objects instantly have access to the new function! That is very convenient.

//Prototyping : add 
Person.prototype.addAge = function (year) {
	if (year)
		this.age += year;
	else
		this.age +=1;
}
p.addAge(5);
p.introduction();

The new addAge function was called from the previously created Person object p. And it’s working without problem. The Dialog says “Hello, my name is Hana Daddy. I am 35 years old.”. 5 years more aged. Time surely flies.

Finally, inheritance is very simple, too. See below example.

// new Person inherited class
function GoodPerson(firstname,lastname,age){
	this.fromClass = Person;
	this.fromClass(firstname,lastname,age);
 
	this.characteristic="Good";
	this.doSomethingGood =  function (){
		alert("help others");
	}
}
 
var a = new GoodPerson("Good","Person",20);
a.introduction();

Thats about a all for the quick HOWTO on Javascript class. I will post my very very simple Ajax Javascript Class next time.

]]>
http://code.neox.net/2008/06/03/javascript-class-howto/feed/ 0