Table of Contents Previous Next

Sugar Developer Guide

Version 5.1


Chapter 4 Customizing Sugar : Module Builder : Adding Custom Logic using Code

Adding Custom Logic using Code
While the key benefit of the Module Builder is that the user is able to create entirely new modules without the need to write code, there are still some tasks that require writing PHP code. For instance, adding custom logic or making a call to an external system via a Web Service. This can be done in one of two methods.
Logic Hooks
One way is by writing PHP code that leverages the event handlers or “logic hooks” available in Sugar. In order to accomplish this, the developer must create the custom code and then add it to the manifest file for the “Media Inquiry” package.
Here is some sample code for a simple example of using the logic hooks. This example adds a time stamp to the description field of the Media Inquiry every time the record is saved.
First, create the file AddTimeStamp.php with the following contents.
<?php
//prevents directly accessing this file from a web browser
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
 
class AddTimeStamp {
function StampIt(& $focus, $event){
global $current_user;
$focus->description .= “\nSaved on ”. date(“Y-m-d g:i a”). “ by ”. $current_user->user_name;
}
}
?>
 
Next register this custom function by creating the file logic_hooks.php with the following contents.
<?php
// Do not store anything in this file that is not part of the array or the hook version. This file will
// be automatically rebuilt in the future.
$hook_version = 1;
$hook_array = Array();
// position, file, function
$hook_array['before_save'] = Array();
$hook_array['before_save'][] = Array(1, 'custom', 'custom/modules/Media/AddTimeStamp.php ','AddTimeStamp', 'StampIt');
?>
 
Now add these two files to the Media Inquiries zip file you just saved. Create a directory called “SugarModules/custom/” in the zip file and add the two files there. Then modify the manifest.php in the zip file to include the following definition in the $install_defs[‘copy’] array.
array (
'from' => '<basepath>/SugarModules/custom',
'to' => 'custom/modules/jsche_Media',
),
Custom Bean files
Another method is to add code directly to the custom bean. This is the somewhat more complicated approach as it requires understanding the SugarBean class, but is the far more flexible and powerful approach.
First you must “build” your module. This can be done by either deploying your module or pressing the publish button. Module Builder will then generate a folder for your package in “custom/modulebuilder/builds/”. Inside that folder is where Module Builder will have placed the bean files for your new module(s). In this case we want
custom/modulebuilder/builds/MediaTracking/SugarModules/modules/jsche_mediarequest”
Inside you will find two files of interest. The first one is {module_name}_sugar.php. This file is generated by Module Builder and may be erased by further changes in module builder or upgrades to the sugar application. You should not make any changes to this file. The second is {module_name}.php. This is the file where you should make any changes you would like to the logic of your module. To add our timestamp, we would add the following code to jsche_mediarequest.php
function save($check_notify = FALSE) {
global $current_user;
$this->description .= "\nSaved on " . date("Y-m-d g:i a"). " by "
. $current_user->user_name;
parent::save($check_notify);
}
The call to the parent::save function is critical as this will call on the out of box SugarBean to handle the regular save functionality. To finish, simply re-deploy or re-publish your package from ModuleBuilder.
You can now upload this module extended with custom code logic into your Sugar application using the Module Loader as described earlier.

Table of Contents Previous Next

Copyright 2004-2008 SugarCRM Inc.
Product License