No external resources are needed. The only requirement is that the cache/images directory is writable and that you have a basic understanding of Sugar.
There are cases where it would be helpful to be able to add a photo to a SugarCRM contact record in order to put a face with the name. However, there isn’t a way to do this out of the box in SugarCRM. Thankfully with SugarCRM’s customizable structure we can create that functionality on our own. Why stop with just contacts? After this tutorial we could add a photo to a lead, account, or any other module.
<img src='cache/images/{$fields.{{$displayParams.id}}.value}_{{sugarvar key='value'}}' height='100'> {/if}
<input type="hidden" id="old_{{sugarvar key='name'}}" name="old_{{sugarvar key='name'}}" value="{$fields[{{sugarvar key='name' stringFormat=true}}].value}"/> <input id="{{sugarvar key='name'}}" name="{{sugarvar key='name'}}" type="file" title='{{$vardef.help}}' size="{{$displayParams.size|default:30}}" {{if !empty($vardef.len)}}maxlength='{{$vardef.len}}'{{elseif !empty($displayParams.maxlength)}}maxlength="{{$displayParams.maxlength}}"{{else}}maxlength="255"{{/if}} value="{$fields[{{sugarvar key='name' stringFormat=true}}].value}" {{$displayParams.field}}> <img src='cache/images/{$fields.{{$displayParams.id}}.value}_{{sugarvar key='value'}}' height='100'> {/if}
<?php require_once('include/SugarFields/Fields/Base/SugarFieldBase.php'); class SugarFieldPhoto extends SugarFieldBase { function getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) { global $app_strings; $error = $app_strings['ERR_SMARTY_MISSING_DISPLAY_PARAMS'] . 'id'; $GLOBALS['log']->error($error); $this->ss->trigger_error($error); return; } $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex); return $this->fetch('include/SugarFields/Fields/Photo/DetailView.tpl'); } } ?>
.... 'name' => 'contact_photo_c', 'label' => 'LBL_CONTACT_PHOTO', 'type' => 'photo', 'displayParams' => array ( 'id' => 'id', ), ....
.... 'name' => 'contact_photo_c', 'label' => 'LBL_CONTACT_PHOTO', 'type' => 'photo', 'displayParams' => array ( 'id' => 'id', ), ....
<?php class ContactPhoto { function uploadPhoto(&$bean, $event, $arguments) { require_once('modules/Contacts/Contact.php'); require_once('include/utils.php'); $GLOBALS['log']->debug("ContactPhoto->uploadPhoto"); //we need to manually set the id if it is not already set //so that we can name the file appropriately $bean->id = create_guid(); $bean->new_with_id = true; } //this is the name of the field that is created in studio for the photo $field_name = 'contact_photo_c'; global $sugar_config; //if a previous photo has been uploaded then remove it now // create a non UTF-8 name encoding // 176 + 36 char guid = windows' maximum filename length $old_file_name = $_REQUEST['old_'.$field_name]; $old_photo = $sugar_config['cache_dir'].'images/'.$bean->id.'_'.$old_file_name; $GLOBALS['log']->debug("ContactPhoto->uploadPhoto: Deleting old photo: ".$old_photo); } $file_name = $bean->id.'_'.$_FILES[$field_name]['name']; //save the file name to the database $bean->contact_photo_c = $_FILES[$field_name]['name']; //return false; } elseif($_FILES[$this->field_name]['size'] > $sugar_config['upload_maxsize']) { } // create a non UTF-8 name encoding // 176 + 36 char guid = windows' maximum filename length $destination = $sugar_config['cache_dir'].'images/'.$stored_file_name; } //$destination = clean_path($this->get_upload_path($bean_id)); die("ERROR: can't move_uploaded_file to $destination. You should try making the directory writable by the webserver"); } } } } ?>
<?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; // position, file, function $hook_array['before_save'][] = Array(1, 'uploadPhoto', 'custom/modules/Contacts/ContactPhoto.php','ContactPhoto', 'uploadPhoto'); ?>
Jul 01, 2009
great piece of work, works as it should.
One happy customer here.