4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />1<br />
Best Practices for Creating Custom Apps in Sugar	<br />John Mertic<br />SugarCRM<br />4/14/10<br />©2010 SugarCRM Inc. All...
The golden rules of customizations<br />Use Module Builder/Studio for customizations if possible<br />Otherwise, put your ...
Why do this?<br />Module Builder/Studio changes are well supported and easier to manage<br />Customizations are separated ...
Exceptions to this…<br />New modules need to be under modules/ directory.<br />Bean class customizations to OOTB modules n...
Let’s see what we can do<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />6<br />
MVC Framework<br />MVC stands for Model View Controller<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />7<...
MVC Framework – The anatomy of a request<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />8<br />
MVC Framework – SugarApplication<br />Bootstraps the application<br />Loads many of the default application settings<br />...
MVC Framework - SugarController<br />Manages all the actions of a module<br />Contains logic for handling requests that do...
MVC Framework – SugarController API<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />11<br />
MVC Framework – Controller Example<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />12<br /><?php<br />clas...
MVC Framework – Controller-less mapping<br />If your module doesn’t need to have any controller logic, use action mapping ...
MVC Framework - SugarView<br />Contains the display logic for the action<br />Uses a metadata backend for Edit, Detail, Li...
MVC Framework – SugarView API<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />15<br />
MVC Framework – SugarView API<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />16<br />
MVC Framework – View Example<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />17<br /><?php<br />class View...
MVC Framework – ViewDetail Example<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />18<br /><?php<br />requ...
4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />19<br />Where do I put my customizations?<br />Controllers<br /...
Metadata Layer<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />20<br />
Kinds of metadata<br />editviewdefs.php / detailviewdefs.php / quickcreatedefs.php<br />Wireless variants: wireless.editvi...
editviewdefs.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />22<br /><?php<br />$viewdefs[$module_name...
subpaneldefs.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />23<br /><?php<br />$layout_defs[$module_n...
searchdefs.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />24<br /><?php<br />$searchdefs[$module_name...
SearchFields.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />25<br /><?php<br />$searchFields[$module_...
popupdefs.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />26<br /><?php<br />global $mod_strings;<br /...
Where do I put my customizations?<br />Subpaneldefs<br />custom/Extension/modules/Modulename/Ext/Layoutdefs/NameWhateverYo...
Logic Hooks<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />28<br />
Logic Hook Types<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />29<br />
Logic Hook Types (cont)<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />30<br />
Logic Hook Types (new in 6.0)<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />31<br />
logic_hooks.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />32<br /><?php<br />$hook_version = 1; <br ...
AccountHooks.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />33<br /><?php<br />class AccountHooks<br ...
Where do I put my customizations?<br />Application Level Logic Hooks<br />/custom/modules/<br />Module Level Logic Hooks<b...
Themes<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />35<br />
Theme Directory Layout<br />css/ - contains all css files<br />images/ - contains all images<br />js/ - contains any js fi...
Themes can inherit from other themes<br />Theme Inheritance Model<br />12/30/08<br />©2009 SugarCRM Inc. All rights reserv...
Allow upgrade-safe modifications to themes<br />All themes can be modified by putting the replacement or overriding file i...
Resources<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />39<br />http://developers.sugarcrm.com<br />Buy ...
Thanks for coming!<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />40<br />
Upcoming SlideShare
Loading in …5
×

SugarCon 2010 - Best Practices for Creating Custom Apps in Sugar

6,179 views

Published on

One of the most important features of SugarCRM for developers is the ability to build customizations, even custom apps, to suit your needs. In this talk, John Mertic will detail some of the most common customization use cases from the field and provide guidelines on how to build upgrade-safe customizations and custom apps that will help meet the needs of your given company.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,179
On SlideShare
0
From Embeds
0
Number of Embeds
1,648
Actions
Shares
0
Downloads
104
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

SugarCon 2010 - Best Practices for Creating Custom Apps in Sugar

  1. 1. 4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />1<br />
  2. 2. Best Practices for Creating Custom Apps in Sugar <br />John Mertic<br />SugarCRM<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />2<br />
  3. 3. The golden rules of customizations<br />Use Module Builder/Studio for customizations if possible<br />Otherwise, put your code inside the custom directory<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />3<br />
  4. 4. Why do this?<br />Module Builder/Studio changes are well supported and easier to manage<br />Customizations are separated from the shipped code<br />Allows you to manage your customizations via version control ( SVN, Git, etc )<br />Files in here aren’t disturbed by any Sugar upgrades<br />Exception – sometimes we will try to fix metadata templates on upgrades for new/removed fields. <br />Makes your app “Upgrade Safe”<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />4<br />
  5. 5. Exceptions to this…<br />New modules need to be under modules/ directory.<br />Bean class customizations to OOTB modules need to be on the original bean file.<br />We’ll look at using logic hooks and other techniques to avoid this for many use cases.<br />Will need to manually manage this during upgrades, so best to avoid.<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />5<br />
  6. 6. Let’s see what we can do<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />6<br />
  7. 7. MVC Framework<br />MVC stands for Model View Controller<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />7<br />
  8. 8. MVC Framework – The anatomy of a request<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />8<br />
  9. 9. MVC Framework – SugarApplication<br />Bootstraps the application<br />Loads many of the default application settings<br />Language<br />Theme<br />Session<br />Handles User Authentication<br />Loads the controller<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />9<br />
  10. 10. MVC Framework - SugarController<br />Manages all the actions of a module<br />Contains logic for handling requests that don’t require a view<br />Example: Record saving<br />Maps all other requests to the correct view<br />Logic for mapping an action of one name to a view of another<br />Can detect if we should be using MVC or classic views<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />10<br />
  11. 11. MVC Framework – SugarController API<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />11<br />
  12. 12. MVC Framework – Controller Example<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />12<br /><?php<br />class ExampleController extends SugarController<br />{<br /> public function action_hello()<br /> {<br /> echo "Hello World!";<br /> }<br /> public function action_goodbye()<br /> {<br /> $this->view = 'goodbye';<br /> }<br />}<br />
  13. 13. MVC Framework – Controller-less mapping<br />If your module doesn’t need to have any controller logic, use action mapping insteads.<br />Create file named action_view_map.php with the following contents:<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />13<br /><?php<br />$action_view_map['goodbye'] = 'goodbye';<br />
  14. 14. MVC Framework - SugarView<br />Contains the display logic for the action<br />Uses a metadata backend for Edit, Detail, List, Popup views<br />And Convert Lead in Sugar 6.0<br />For other views, you write the code for the view and put it in modules/modulename/views/view.viewname.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />14<br />
  15. 15. MVC Framework – SugarView API<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />15<br />
  16. 16. MVC Framework – SugarView API<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />16<br />
  17. 17. MVC Framework – View Example<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />17<br /><?php<br />class ViewExample extends SugarView<br />{<br /> public function preDisplay()<br /> {<br /> if ( !is_admin($GLOBALS["current_user"]) )<br />sugar_die("Not an Admin");<br /> }<br /> public function display()<br /> {<br /> echo "Hello Admin!";<br /> }<br />}<br />
  18. 18. MVC Framework – ViewDetail Example<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />18<br /><?php<br />require_once('include/MVC/View/views/view.detail.php');<br />class ContactsViewDetail extends ViewDetail<br />{<br /> public function display() <br /> {<br /> $admin = new Administration();<br /> $admin->retrieveSettings();<br />if(isset($admin->settings['portal_on']) <br /> && $admin->settings['portal_on']) {<br /> $this->ss->assign("PORTAL_ENABLED", true);<br /> }<br />parent::display();<br /> }<br />}<br />
  19. 19. 4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />19<br />Where do I put my customizations?<br />Controllers<br />/custom/modules/Modulename/controller.php<br />class CustomModulenameController extends SugarController<br />Views<br />/custom/modules/Modulename/views/view.viewname.php<br />class CustomModulenameViewViewname extends ModulenameViewViewname<br />If that class doesn’t exist extend from ViewViewname or SugarView<br />
  20. 20. Metadata Layer<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />20<br />
  21. 21. Kinds of metadata<br />editviewdefs.php / detailviewdefs.php / quickcreatedefs.php<br />Wireless variants: wireless.editviewdefs.php, wireless.detailviewdefs.php<br />subpaneldefs.php<br />listviewdefs.php<br />wireless.listviewdefs.php<br />searchdefs.php<br />wireless.searchdefs.php<br />SearchFields.php<br />popupdefs.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />21<br />
  22. 22. editviewdefs.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />22<br /><?php<br />$viewdefs[$module_name]['EditView'] = array(<br /> 'templateMeta' => array(<br /> 'maxColumns' => '2', <br /> 'widths' => array(<br />array('label' => '10', 'field' => '30'), <br />array('label' => '10', 'field' => '30'),<br /> ), <br /> ),<br /> 'panels' =>array (<br /> 'default' => <br /> array (<br /> array (<br /> 'name',<br /> 'assigned_user_name',<br /> ), <br />array(array(<br /> 'name' => 'date_modified',<br /> 'customCode' => '{$fields.date_modified.value} {$APP.LBL_BY} {$fields.modified_by_name.value}',<br /> 'label' => 'LBL_DATE_MODIFIED',<br />)),<br /> ),<br /> array(<br /> array (<br /> 'description',<br /> ),<br /> ),<br /> ), <br />);<br />
  23. 23. subpaneldefs.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />23<br /><?php<br />$layout_defs[$module_name] = array(<br /> 'subpanel_setup' => array(<br /> 'contacts' => array(<br /> 'order' => 20,<br /> 'module' => 'Contacts',<br /> 'sort_by' => 'last_name, first_name',<br /> 'sort_order' => 'asc',<br /> 'subpanel_name' => 'default',<br /> 'get_subpanel_data' => 'contacts',<br /> 'title_key' => 'LBL_CONTACTS_SUBPANEL_TITLE',<br /> 'top_buttons' => array(<br />array('widget_class' => 'SubPanelTopButtonQuickCreate'),<br /> array(<br /> 'widget_class'=>'SubPanelTopSelectButton',<br /> 'mode'=>'MultiSelect’<br /> ),<br /> ),<br /> ),<br /> ),<br />);<br />
  24. 24. searchdefs.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />24<br /><?php<br />$searchdefs[$module_name] = array(<br /> 'templateMeta' => array(<br /> 'maxColumns' => '3',<br /> 'widths' => array('label' => '10', 'field' => '30'),<br /> ),<br /> 'layout' => array(<br /> 'basic_search' => array(<br /> 'name',<br />array('name'=>'current_user_only', 'label'=>'LBL_CURRENT_USER_FILTER', 'type'=>'bool'),<br /> ),<br /> 'advanced_search' => array(<br /> 'name',<br />array('name' => 'phone', 'label' =>'LBL_ANY_PHONE', 'type' => 'name'),<br />array('name' => 'address_city', 'label' =>'LBL_CITY', 'type' => 'name'),<br />array('name' => 'email', 'label' =>'LBL_ANY_EMAIL', 'type' => 'name'),<br />array('name' => 'assigned_user_id', 'type' => 'enum', <br /> 'label' => 'LBL_ASSIGNED_TO', <br /> 'function' => array('name' => 'get_user_array', <br /> 'params' => array(false))),<br /> ),<br /> ),<br />);<br />
  25. 25. SearchFields.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />25<br /><?php<br />$searchFields[$module_name] = array (<br /> 'name' => array( 'query_type'=>'default'),<br /> 'current_user_only'=> array('query_type'=>'default',<br /> 'db_field'=>array('assigned_user_id'),'my_items'=>true, <br /> 'vname' => 'LBL_CURRENT_USER_FILTER', 'type' => 'bool'),<br /> 'assigned_user_id'=> array('query_type'=>'default'),<br />);<br />
  26. 26. popupdefs.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />26<br /><?php<br />global $mod_strings;<br />$popupMeta = array(<br /> 'moduleMain' => 'Account',<br /> 'varName' => 'ACCOUNT',<br /> 'orderBy' => 'name',<br /> 'whereClauses' => array(<br /> 'name' => 'accounts.name', <br /> 'billing_address_city' => 'accounts.billing_address_city',<br /> 'phone_office' => 'accounts.phone_office'),<br /> 'searchInputs' => array('name', 'billing_address_city', 'phone_office'),<br /> 'create' => array(<br /> 'formBase' => 'AccountFormBase.php',<br /> 'formBaseClass' => 'AccountFormBase',<br /> 'getFormBodyParams' => array('','','AccountSave'),<br /> 'createButton' => $mod_strings['LNK_NEW_ACCOUNT’]),<br /> 'listviewdefs' => array(<br /> 'NAME' => array(<br /> 'width' => '40', 'label' => 'LBL_LIST_ACCOUNT_NAME', 'link' => true,'default' => true,), <br /> ),<br /> 'searchdefs' => array(<br /> 'name', <br />array('name' => 'assigned_user_id', 'label'=>'LBL_ASSIGNED_TO', 'type' => 'enum', 'function' => array('name' => 'get_user_array', 'params' => array(false))),<br /> ),);<br />
  27. 27. Where do I put my customizations?<br />Subpaneldefs<br />custom/Extension/modules/Modulename/Ext/Layoutdefs/NameWhateverYouWant.php<br />Need to run ‘Quick Repair and Rebuild’ after changes are made<br />Everything else<br />/custom/modules/Modulename/metadata<br />Watch for Studio blowing out your changes.<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />27<br />
  28. 28. Logic Hooks<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />28<br />
  29. 29. Logic Hook Types<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />29<br />
  30. 30. Logic Hook Types (cont)<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />30<br />
  31. 31. Logic Hook Types (new in 6.0)<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />31<br />
  32. 32. logic_hooks.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />32<br /><?php<br />$hook_version = 1; <br />$hook_array = Array(); <br />$hook_array['before_save'] = Array(); <br />$hook_array['before_save'][] = Array(1, 'AccountHooks', <br />'custom/Accounts/AccountHooks.php','AccountHooks', 'getParentAccountIndustry'); <br />Parameters for Logic Hook Definition<br />Parameter 1 - Sorting index used to sort the arrays of logic hook definitions before they are processed.Parameter 2 - A string value to identify the hookParameter 3 - Path to the PHP file to include which contains your logic hook codeParameter 4 - Name of the PHP class the logic hook method is inParameter 5 - Name of the PHP method to call <br />
  33. 33. AccountHooks.php<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />33<br /><?php<br />class AccountHooks<br />{ <br /> public function getParentAccountIndustry( <br />SugarBean $bean, <br /> $event, <br /> $arguments <br /> ) <br /> { <br /> if ( empty($bean->industry) <br /> && !empty($bean->parent_id) ) { <br /> $parentAccountFocus = new Account(); <br /> $parentAccountFocus->retrieve($bean->parent_id); <br /> if ( !empty($parentAccountFocus->id) ) <br /> $bean->industry = $parentAccountFocus->industry; <br /> } <br /> } <br />} <br />
  34. 34. Where do I put my customizations?<br />Application Level Logic Hooks<br />/custom/modules/<br />Module Level Logic Hooks<br />/custom/modules/Modulename/<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />34<br />
  35. 35. Themes<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />35<br />
  36. 36. Theme Directory Layout<br />css/ - contains all css files<br />images/ - contains all images<br />js/ - contains any js files.<br />tpls/ - smarty templates<br />themedef.php definition file.<br />12/30/08<br />©2009 SugarCRM Inc. All rights reserved.<br />36<br />
  37. 37. Themes can inherit from other themes<br />Theme Inheritance Model<br />12/30/08<br />©2009 SugarCRM Inc. All rights reserved.<br />37<br />
  38. 38. Allow upgrade-safe modifications to themes<br />All themes can be modified by putting the replacement or overriding file in the custom/theme/<themename> directory<br />Image, HTML template file overrides are used as a replacement of the previous file<br />Example: an image custom/theme/<themename>/dog.gif would be used instead of theme/<themename>/dog.gif <br />CSS and Javascript files are combined in order of inheritance<br />Uses cssmin and jsmin to help reduce file size<br />No further code changes are required – changes are picked up automatically when themes cache is rebuilt.<br />12/30/08<br />©2009 SugarCRM Inc. All rights reserved.<br />38<br />
  39. 39. Resources<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />39<br />http://developers.sugarcrm.com<br />Buy my book!<br />
  40. 40. Thanks for coming!<br />4/14/10<br />©2010 SugarCRM Inc. All rights reserved.<br />40<br />

×