注:我用的SugarCRM是6.5.8的社区版,以下描述的修改,都是基于这个版本的。
SugarmCRM都是基于MVC的模型,需要自己动手开发一个新的模块的话,可以先使用系统管理中的模块生成器构造一个新的模块,将基本的数据字段以及一些关联关系,直接通过拖拽的方式先定义好。
通过以上自定义操作,只能满足一些基本的需求,但是如果需要实现自定义的一些需求,就需要自己动手编写了。
使用自定义的tpl文件来做展现
首先可以将生成的模块直接加载后,那样就可以边编辑边调试。
在modules/模块名/就是默认生成的一些代码,一般会有Dashlets、language、metadata等目录。
为了针对这个模块的某个action做自定义tpl展现,需要在moudules/模块名/下面创建views目录和tpls目录。
以“创建”这个功能来作为示例,首先在tpls目录下,放置一个编辑完成的tpl文件,然后在views下面建一个view.edit.php文件,并且添加如下的代码:
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
require_once('include/MVC/View/views/view.edit.php');
// 此处必须是模块名称+ViewEdit,我在这个名称上面折腾了1个多小时才发现此问题
class GD_GongdanViewEdit extends ViewEdit
{
public function __construct()
{
parent::ViewEdit();
}
public function preDisplay()
{
// 此处指向用于展示的tpl文件
$metadataFile = $this->getMetaDataFile();
$this->ev = $this->getEditView();
$this->ev->ss =& $this->ss;
$this->ev->setup($this->module, $this->bean, $metadataFile, get_custom_file_if_exists('modules/GD_gongdan/tpls/EditView.tpl'));
}
public function display(){
parent::display();
}
}
上面通过重载preDisplay来重新指定显示的tpl文件,这样模块的SugarBean继承类在运行处理完后,会自动用你指定的tpl文件来进行展示。
如何获取tpl文件所需要的数据
既然要用自定义的tpl文件来展示,有时候需要额外的获取一些数据给tpl文件,此时可以在modules/模块名/模块名_sugar.php进行修改,此类一般都继承自SugarBean。如下代码展示了,在初始化的时候获取额外的客户信息数据:
function GD_gongdan_sugar(){
parent::Basic();
if(!empty($_GET["tel"]))
{
$query = "SELECT * FROM accounts where phone_office = '".$_GET["tel"]."' and deleted='0'";
$result = $this->db->query($query,true," Error filling in additional detail fields: ");
$row = $this->db->fetchByAssoc($result);
if($row != null)
{
$this->sz_account_name = $row["name"];
}
else
{
$this->sz_account_name = "";
}
}
else
{
$this->sz_account_name = "";
}
}