Skip to content

Porting Existing IS4C Code to Office

Andy Theuninck edited this page Apr 6, 2015 · 1 revision

Got something that already works and what to start integrating it into CORE's Office back end? Great! Get started here. There are a few key tasks to make your code get along with Office and behave nicely for others as well as your self.

  • Interact with the Office environment
    • Avoid absolute paths
  • Be include-safe
  • Access the database
  • Use Office's look & feel

Interact with the environment

Office provides a global configuration file for storing settings. It lives in the root directory and is aptly named config.php. Including this file is almost always the first thing you should do in a script. It defines a bunch of helpful global variables. The second thing you'll usually do is include Office's API classes via the FannieAPI.php file.

Since the software is meant to exist at many locations with various configurations, we cannot make any assumptions about file paths. If your code is in /var/www/fannie/my-new-feature/index.php, don't write this:

include('/var/www/fannie/config.php');

Write this:

include(dirname(__FILE__).'/../config.php');

Be Include-Safe

One important rule in Office is your file should not do anything when include()ed or require()ed. This may sound counter intuitive at first but it's essential to class discovery and unit testing. Your script should only run when called directly by a browser (or command line). This is a sound, path-safe, include-safe beginning for a file:

include(include(dirname(__FILE__).'/../config.php');
if (!class_exists('FannieAPI')) {
   include(dirname(__FILE__) . '/../classlib2.0/FannieAPI.php');
}
if (basename(__FILE__) != basename($_SERVER['PHP_SELF'])) {
    return;
}
// your stuff follows

The configuration include is path-safe. The API include is path-safe and include-safe. The class_exists check means your fill never trigger a duplicate-class-definition error when included. The second if block checks whether your file has been called directly and if not returns without executing.

Accessing the Database

The database is accessed using the API class FannieDB.

$dbc = FannieDB::get($FANNIE_OP_DB);

Office's three default database names are stored in $FANNIE_OP_DB, $FANNIE_TRANS_DB, and $FANNIE_ARCHIVE_DB. Note that the value returned is a SQLManager object rather than a low-level, driver-specific link. This means you cannot use it with functions like mysql_query. See Common Architecture for more information on using SQLManager.

Use Office's Look & Feel

To use Office's look & feel, you need to wrap your display code in a class that inherits from FanniePage. The Plugin section describes using the page class.

Clone this wiki locally