Every business application is a CRM

Over the last year I’ve probably looked over 50 business applications across a variety of categories and platform. During that time a pattern has emerged. Every business application is a CRM.

Alright, that might be a slight exaggeration. There are certainly some applications that offer unique functionality. But the bulk of them, regardless of platform or target audience, seem to offer the same basically functionality that looks strikingly similar to a CRM.

  1. Dashboard
  2. Customer management
  3. Calendar/Scheduling
  4. Inventory
  5. Invoicing, Payments and Customer Accounts
  6. Maps
  7. Sales automation (email and/or SMS)
  8. Reporting

Throw a bit of wrapping around this an you have a recipe for a generic application that could be targeted at any market.

Starting out with Maven

Until recently I’ve really only used Java for private projects. This allowed me to get away with selecting the right project type from Eclipse or NetBeans and manually dealing with dependencies. A few months ago that changed and I’m now building more projects using Java.

Anyone who has worked on a project with multiple developers knows that you’ll go crazy without a build script and dependency management. I’d previously used Ant for build scripts but everyone I spoke to said I should use Maven instead because it also handled dependency management.

Starting a new project with Maven was reasonable painless. At first I was a little hesitant that I would need to create complicated XML files and directory structures from scratch but turns out that Maven comes with a generator that creates the relevant directories and files. for you. To start a new project you just run the following command line:

mvn archetype:generate -D archetypeGroupId=org.apache.maven.archetypes -DgroupId=com.example.app -DartifactId=example

You’ll need to replace com.example.app with the namespace for your project and example with the name of the project. Maven will create a new directory with the project name and then generate the relevant directory structure and files below that.

One thing that initially confused me after running this command is that Maven displays hundreds of lines of output and asks you enter a number. What it’s asking you is which archetype (template) to use when generating the application skeleton. Something I found useful is that you can type in a string and it will display a list of just those archetypes that contained that contain it.

For example: If I wanted to build a gwt application I’d do this.

Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : gwt
Choose archetype:
1: remote -> com.dyuproject.protostuff.archetype:basic-gwt-webapp (webapp archetype using protostuff, json and gwt)
2: remote -> com.dyuproject.protostuff.archetype:simple-gwt-webapp (webapp archetype using protobuf, json and gwt)
3: remote -> net.kindleit:gae-archetype-gwt (Archetype for creating maven-gae projects that uses GWT for the view)
4: remote -> net.sf.mgp:maven-archetype-gwt (An archetype which contains a sample Maven GWT project.)
5: remote -> org.codehaus.mojo:gwt-maven-plugin (Maven plugin for the Google Web Toolkit.)
6: remote -> org.codehaus.sonar.archetypes:sonar-gwt-plugin-archetype (Maven archetype to create a Sonar plugin including GWT pages)
7: remote -> org.geomajas:geomajas-gwt-archetype (Geomajas GWT application archetype)
8: remote -> org.ops4j.pax.web.archetypes:wab-gwt-archetype (-)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): :

Once you have selected your archetype there are a few other straight forward questions and it will generate the new project for you. You can compile your new project immediately by changing into the project directory and typing:

mvn compile

All of the dependencies are automatically download for you. While I’ve still got a lot to learn about Maven it was a lot easier than I was expecting.

Cleaning the blog

I started this blog 5 years ago (January 30, 2007). Back then it was full of technical ramblings, mostly about Linux and PHP, which reflected where my life was at the time. While I still use both technologies that’s less reflective of who I am today.

Professionally I’ve moved from developer to CTO. Personally I’ve found an interest in business, startups, marketing and making money online. If you’re familiar with Rob Walling then you’ll understand the word micropreneur. For everyone else think lifestyle business or 4 hour work week.

Earlier today I realised that while I’ve only posted once in the last 18 months the blog is still getting a reasonable amount of traffic to seriously out of date content. In fairness to those people I’ve decided to pull most of the content from this site immediately while I decide if I want to keep it going. Google should figure this out shortly and stop the traffic completely.

jQuery UI sortable

Recently I’ve been playing with a list of lists using the jQuery UI sortable component. Graphically it looks something like:

  • List 1
    • Item 1
    • Item 2
    • Item 3
  • List 2
    • Item 5
    • Item 6
    • Item 7
  • List 3
    • Item 7
    • Item 8
    • Item 9

The HTML for this is pretty simple

<ul  class="lists">
    <li>
        <h1>List 1</h1>
        <ul id="list_1" class="list">
            <li id="item_1">Item 1</li>
            <li id="item_2">Item 2</li>
           <li id="item_3">Item 3</li>
        </ul>
    </li>
    <li>
        <h1>List 2</h1>
        <ul id="list_2" class="list">
            <li id="item_4">Item 4</li>
            <li id="item_5">Item 5</li>
            <li id="item_6">Item 6</li>
        </ul>
    </li>
    <li>
        <h1>List 3</h1>
        <ul id="list_3" class="list">
            <li id="item_7">Item 7</li>
            <li id="item_8">Item 8</li>
            <li id="item_9">Item 9</li>
        </ul>
    </li>
</ul>

I want the user to be able to re-order the lists, the items in each list and move items from one list to another. 18 lines of Javascript later I had it handling this perfectly and displaying alerts telling me where items were moved to, the list they were in and if the lists were re-ordered.

$(function() {
    $(".lists").sortable({
        forcePlaceholderSized: true,
        stop: function(event, ui) {
            var list = ui.item.children('ul').attr('id').replace('list_', '');
            window.alert('Moving list ' + list + ' to position ' + ui.item.index());
        },
    });
    $(".list").sortable({
        connectWith: ".list",
        forcePlaceholderSized: true,
        stop: function(event, ui) {
            var item = ui.item.attr('id').replace('item_', '');
            var list = ui.item.parent().attr('id').replace('list_', '');
            window.alert('Moving item ' + item + ' to list ' + list + ' position ' + ui.item.index());
        },
    });
});

I love jQuery and jQuery UI.

Auto Increment in SugarCRM

Earlier today I posted about creating a DateTime Picker in SugarCRM. A second problem I had was creating an auto increment field. This turned out to be slightly more difficult than the datetime picker. Again you need to edit your vardefs.php but this time you add the following to your field.

'auto_increment' => true,

You then need to manually alter the table structure so the id field is a unique index instead of a primary key then add your new column to the database as an auto_increment field and the tables primary key. I also found that if you try to install the module on another system it will fail because it can’t create the table properly. You can solve that by creating the table then installing the module.

Date/Time Picker in SugarCRM

Recently I needed to add a date/time field to a custom SugarCRM module. Sadly the module builder doesn’t include support this and the documentation is pretty bad. Eventually I managed to solve my problem and the solution is suprisingly simple.

After adding a date field to the form I edited my vardefs.php file. You’ll find the file in custom/modulebuilder/packages/package_name/modules/module_name/. Find the field and change it’s type to datetime.

Next you need to change your views. They’re located in custom/modulebuilder/packages/package_name/modules/module_name/metadata. In my case I wanted to the edit view to show a datepicker plus a time combo. I found the field and added

'type' => 'Datetimecombo'

After that I deployed the package and my module was now saving as a date/time and in the edit view I could set both the date (using a date picker) and time (using drop down lists).

API equals dollars

Lately I’ve been looking at a number of SaaS providers covering a range of areas. It amazes me how many of them have no API or only a reporting API. If you’re thinking of building a SaaS start-up then you should be thinking about creating an API that allows your customers to do everything they can with your user interface.

Service providers with this API have an obvious advantage when it comes to migration and integration but they also have a more subtle and more important advantage. When reviewing potential providers I looked at those with an API before those without. Your SaaS may be the best but without this API are potential customers even considering you?

Unit tests

I just read a post asking if my unit test take too long. In it the author suggests that 5 minutes is long and asks if anyone has solved this problem. This reminded me of a discussion I had with some developers about 12 months ago about unit testing in which my argument was simply that unit tests need to be comprehensive and not necessarily quick.

There are many projects where the unit tests take several hours to run. This shouldn’t matter during development when you’re probably only interested in a few unit tests as most test tools provide a way to filter the tests that are run. You only need to run the entire test suite prior to committing or during continuous integration.

Having said all of that I can recommend using memory tables if your database supports them. The operations are generally a lot faster as the database doesn’t need to write to disk.

Migrating to the Zend Framework

I don’t usually blog about work but I think this may interest some people. Late last year we trialled the Zend Framework in a small application. A few months ago we begun the process of converting our main PHP application to it. The migration is still in the early stages so some of this may change rapidly.

We decided on the Zend Framework because it was component based. This was important because we have an existing code base with a regular release schedule. Stopping development while we converted the application wasn’t an option so we needed something that could be integrated slowly.

So how are we doing this?

The first task was to replace the applications module structure with auto loading. While this reduced the number of files that needed to be included to process a request the biggest advantage was making the code more visible to the developers. Prior to this classes were hidden 2, 3 or 4 levels deep in modules. The immediate impact was finding a number of classes that were no longer required.

Next I was expecting to replace some of the more discrete components like logging but it didn’t turn out this way. Because of the applications development schedule work instead focused on migrating from our custom MVC to the Zend Framework MVC.

After writing a bootstrap file  we created controllers and actions. The release cycle wasn’t long enough to completely convert the old MVC so the new actions simply call the old code. This approach allowed us convert the front controller (without session management and authentication) in about a day and means that new code can be written using the Zend Framework instead of our old framework.  Custom routing was required to emulate some of the old URLs.

The session management and authentication took about to day to sort out on its own. All of the controllers share a common base class. It’s init() function provides some common functionality:

  1. Initialize the view
  2. Initialize the session (except the API or AJAX controllers)
  3. Set all actions as requiring authentication

Starting sessions is done in the controllers init() function instead of the bootstrap file so we can prevent sessions from being started for API calls. This is important because we get a large number of API calls and none of them requires a session. The other important task for init() is to mark some actions as not requiring authentication.

Authentication itself takes place in the controllers preDispatch() function. This provided backwards compatibility with the way the application previously worked.

More recently we’ve begun removing static methods from business logic so that we can use dependency injection to make testing easier. I’ll post about this next time.

Dynamic forms using Zend_Form

While most forms contain fixed fields there are occasions when you need a form to be dynamic and adjust itself based on user input. The adjustment could be as simple as altering the options in a drop down list or as complex as adding/removing fields. In this post I’m going to cover how to create a dynamic form using Zend_Form and jQuery. I’ll use the example of a registration form that prompts the user for their country and state. The requirements are pretty simple:

  1. It should only prompt for a state if the country has states.
  2. The state list should only show states for the selected country.
  3. The form should degrade gracefully so it works without Javascript

To start I’m going to create a World class. This class has two functions. The first returns a list of countries. The second returns a list of states for a specified country or a list of all countries that have states plus the states in those countries. You might like to retrieve this information from a database but for simplicity I’ll hard code the information into the class.

class World
{
    static private $_countries = array(
                       "AU" => "Australia",
                       "NZ" => "New Zealand");

    static private $_states = array(
		        "AU" => array(
		            "ACT" => "Australian Capital Territory",
		            "NSW" => "New South Wales",
		            "NT" => "Northern Territory",
		            "QLD" => "Queensland",
		            "SA" => "South Australia",
		            "TAS" => "Tasmania",
		            "VIC" => "Victoria"));

    public function getCountries()
    {
        return self::$_countries;
    }

    public function getStates($country = null)
    {
        if ($country === null) {
            return self::$_states;
        }
        if (array_key_exists($country, self::$_states)) {
            return self::$_states[$country];
        }
        return null;
    }
}

Next I’ll create a class for the form (RegForm) by extending Zend_Form. This gives our registration form all of the advantages you get from Zend_Form including input filtering and validation. The form elements will be added in the constructor so that creating a new form is all you need to do to use it.

As our form needs to adapt based on user input the constructor needs to accept the user input as one of its parameters. This allows us to adjust the form elements based on the user input. All code using these parameters needs to be extremely careful as the user input has not been filtered or validated yet.

class RegForm extends Zend_Form
{
    public function __construct($world, $params)
    {
        parent::__construct();

        $countries = $world->getCountries();
        $countryKeys = array_keys($countries);
        $thisCountry = isset($params['country']) ? $params['country'] : $countryKeys[0];
        $states = $world->getStates($thisCountry);

        $country = new Zend_Form_Element_Select('country');
        $country->setLabel('Country')
                ->setMultiOptions($countries)
                ->setValue($thisCountry)
                ->setRequired(true);
        $this->addElement($country);

        $state = new Zend_Form_Element_Select('state');
        $state->setLabel('State');
        if ($states !== null) {
            $state->setMultiOptions($states)
                  ->setRequired(true);
        } else {
            $state->setRegisterInArrayValidator(false);
        }
        $this->addElement($state);

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setValue('Add User')
               ->setRequired(false);
        $this->addElement($submit);
    }
}

There are two important things that RegForm does:

  1. The state options are adjusted to match the selected country.
  2. The state is not validated if the country does not contain states.

This means that our form will work exactly the way you expect Zend_Form to work. Without Javascript if you select a country and submit the form then the state list will adjust and display an error that the previously selected state was invalid. After you select a valid country and state the form will validate. If the selected country does not have states then the form with validate regardless of the selected state (providing there are no other errors).

I then need to create the controller.

class AccountController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $params = $this->_getAllParams();
        $world = new World();
        $form = new RegForm($world, $params);
        if ($this->_request->isPost() && $form->isValid($params)) {
            // The form was valid!!
        }
        $this->view = $form;
    }
}

Finally we can add Javascript to alter the form in browser. If you’re using the forms render() function then the Javascript will look something like this.

    var countries = <?php echo json_encode($this->world->getCountries()); ?>;

    var states = <?php echo json_encode($this->world->getStates()); ?>

    function updateStates() {
        var state = $("#state");
        var country = $("#country");
        var hasStates = false;
        jQuery.each(states, function (cc, slist) {
            if (cc == country.val()) {
                hasStates = true;
                state.html('');
                jQuery.each(slist, function (code, name) {
                    state.append('<option value="' +  code + '">' + name + '</option>');
                });
            }
        });
        if (hasStates) {
            $("#state-label").css("display", "block");
            $("#state-element").css("display", "block");
        } else {
            $("#state-label").css("display", "none");
            $("#state-element").css("display", "none");
        }
    }
    $().ready(function () {
        $("#country").change(function () {
            updateStates();
        });
        var state = $("#state");
        if (state.val() == null) {
            $("#state-label").css("display", "none");
            $("#state-element").css("display", "none");
        }
    });

This code takes care of hiding the states if they are not required and adjusting the states based on the selected country without needing to submit the form. With minimal effort I’ve been able to create a dynamic form using Zend_Form to do most of the hard work. Users with Javascript enabled get an A grade experience while those without Javascript can still use the form.