Dear all,
After I've learnt how to use you framework, build up simple modules is very very simple.
However I'm facing another problem: I'm trying to develop a module to manage Items Lent to customers and I would like to specify a "limit date" in order to be notified when lending ends with an alert. I have also a employees field in order to select who will be notified.
This is my code about that
Custom_LendItemInstall.php
public function install() {
Utils_CommonDataCommon::new_array('custom/lenditem/status', array( '0'=>_M('Items Lent'), '1'=>_M('Returned Partially'), '2'=>_M('Returned Completely'), '3'=>_M('Items Sold') ), true, true); //This created the Common Data for the Status Field
Utils_CommonDataCommon::new_array('custom/lenditem/priority', array( '0'=>_M('Low'), '1'=>_M('Medium'), '2'=>_M('High')), true, true); //This created the Common Data for the Priority Field
Base_ThemeCommon::install_default_theme('Custom/LendItem');
Utils_RecordBrowserCommon::install_new_recordset('custom_lenditem', array(
array(
'name' => _M('Title'),
'type'=>'text',
'required'=>true,
'param'=>'255',
'extra'=>false,
'visible'=>true,
'display_callback'=>array('Custom_LendItemCommon','display_title')
),
array(
'name' => _M('Customer'), //Name of the Field
'type' => 'crm_company_contact', //Type of Field
'visible' => true, //shows on the Table View
'filter' => false, //Can you Filter it?
'param'=>array('field_type'=>'select'),
'required' => true
),
array(
'name' => _M('Items'), //Name of the Field
'type' => 'long text', //Type of Field
'visible' => false, //shows on the Table View
'filter' => false, //Can you Filter it?
'required' => true
),
array(
'name' => _M('Status'), //Name of the Field
'type' => 'commondata', //Type of Field
'visible' => true, //shows on the Table View
'filter' => false, //Can you Filter it?
'param' => array('order_by_key'=>true, 'custom/lenditem/status'),
'required' => true
),
array(
'name' => _M('Priority'), //Name of the Field
'type' => 'commondata', //Type of Field
'visible' => true, //shows on the Table View
'filter' => false, //Can you Filter it?
'param' => array('order_by_key'=>true, 'custom/lenditem/priority')
),
array(
'name' => _M('Limit Date'), //Name of the Field
'type' => 'date', //Type of Field
'visible' => true, //shows on the Table View
'filter' => true, //Can you Filter it?
'required' => true
),
array(
'name' => _M('Employees'),
'type'=>'crm_contact',
'param'=>array(
'field_type'=>'multiselect',
'crits'=>array('Custom_LendItemCommon','employees_crits'),
'format'=>array('CRM_ContactsCommon','contact_format_no_company')
),
'display_callback'=>array('Custom_LendItemCommon','display_employees'),
'required'=>true,
'extra'=>false,
'visible'=>true,
'filter'=>true
)
)
);
Utils_RecordBrowserCommon::set_caption('custom_lenditem', _M('Lend Item')); //Creates the Name of the module for users to see
Utils_RecordBrowserCommon::set_icon('custom_lenditem', Base_ThemeCommon::get_template_filename('Custom/LendItem', 'icon.png'));
Utils_RecordBrowserCommon::register_processing_callback('custom_lenditem', 'Custom_LendItemCommon::process_request'); // method called on insert to generate alert
Utils_RecordBrowserCommon::enable_watchdog('custom_lenditem', array('Custom_LendItemCommon','watchdog_label')); //Watchdog for those Managers to track changes
Utils_RecordBrowserCommon::add_access('custom_lenditem', 'view', 'ACCESS:employee', array('(!permission'=>2, '|employees'=>'USER'));
Utils_RecordBrowserCommon::add_access('custom_lenditem', 'add', 'ACCESS:employee');
Utils_RecordBrowserCommon::add_access('custom_lenditem', 'edit', 'ACCESS:employee', array('(permission'=>0, '|employees'=>'USER', '|customers'=>'USER'));
Utils_RecordBrowserCommon::add_access('custom_lenditem', 'delete', 'ACCESS:employee', array(':Created_by'=>'USER_ID'));
Utils_RecordBrowserCommon::add_access('custom_lenditem', 'delete', array('ACCESS:employee','ACCESS:manager'));
return true; //False on failure
}
Custom_LendItemCommon
// display module on menu
public static function menu() {
return array(__('CRM')=>array('__submenu__'=>1,__('Lend Item')=>array()));
}
// function called after insert new element in order to add autogenerated rma_id based on year of request and id
public static function process_request($data, $mode) {
switch($mode) {
case 'adding':
$data['active']=true;
break;
case 'added':
// automatically add alerts for selected employees
$message = $data['title'].': '.$data['customer'].$data['items'];
$start = strtotime($data['limit_date']);
$eee = array();
foreach($data['employees'] as $v) {
$c = CRM_ContactsCommon::get_contact($v);
if(isset($c['login']))
$eee[] = $c['login'];
}
Utils_MessengerCommon::add('Custom_LendItem_Event:'.$data['id'],'Custom_LendItem', $message, $start, array('Custom_LendItemCommon','get_alarm'),array($data['id']),$eee);
break;
default:
break;
}
return $data;
}
public static function display_title($record, $nolink=false) {
$ret = Utils_RecordBrowserCommon::create_linked_label_r('custom_lenditem', 'Title', $record, $nolink);
if (isset($record['items']) && $record['items']!='') $ret = '<span '.Utils_TooltipCommon::open_tag_attrs(Utils_RecordBrowserCommon::format_long_text($record['items']), false).'>'.$ret.'</span>';
return $ret;
}
// label watchdog
public static function watchdog_label($rid = null, $events = array(), $details = true) {
return Utils_RecordBrowserCommon::watchdog_label(
'custom_lenditem',
_M('Lend Item'),
$rid,
$events,
array('Custom_LendItemCommon','watchdog_label_format'),
$details
);
}
public static function watchdog_label_format($r) {
return $r['title'];
}
public static function get_alarm($id) {
$a = self::get_lend($id);
if (!$a) return __('Private record');
$date = __('Timeless event: %s',array(Base_RegionalSettingsCommon::time2reg($a['date'],false)));
return $date."\n".__('Title: %s',array($a['title']));
}
public static function get_lend($id) {
return Utils_RecordBrowserCommon::get_record('custom_lenditem', $id);
}
public static function employees_crits(){
return array('(company_name'=>CRM_ContactsCommon::get_main_company(),'|related_companies'=>array(CRM_ContactsCommon::get_main_company()));
}
public static function display_employees($record, $nolink, $desc) {
return CRM_ContactsCommon::display_contacts_with_notification('custom_lenditem', $record, $nolink, $desc);
}
The method process_request is called after each actin on item and should add an alert.
However this not happends.
Any suggestion?
Thank you