Skip to content

Instantly share code, notes, and snippets.

@dmulvi
Last active December 30, 2015 20:19
Show Gist options
  • Save dmulvi/e877804c398a9dd94953 to your computer and use it in GitHub Desktop.
Save dmulvi/e877804c398a9dd94953 to your computer and use it in GitHub Desktop.
SugarCRM 6.x - Replace remove action with a delete action on subpanel
<?php
// you need to add a new entry to this file located in custom/include/MVC/Controller/entry_point_registry.php
$entry_point_registry['subpanel-delete'] = array('file' => 'custom/ninjacode/subpanel-delete.php', 'auth' => true);
<?php
/*
* In this example we'll swap out the remove button from the subpanel for a delete button.
* This will happen in a custom module that is displayed as a subpanel on the Contacts DetailView.
*
* First, you need to find the definition file for the subpanel. It will be located in:
* /custom/modules/SUBPANEL_MODULE/metadata/subpanels/
* It will have a long ugly name, but it should be somewhat obvious which one you want as it will
* contain the name of the parent or DetailView module in the filename. In this case that is the
* Contacts module. On the specific example I did this for the file name was:
* Contact_subpanel_contacts_smm_status_module_1.php
*/
$subpanel_layout['list_fields'] = array (
// removed other metadata for brevity
'remove_button' =>
array (
'vname' => 'LBL_REMOVE',
//'widget_class' => 'SubPanelRemoveButton',
'widget_class' => 'SubPanelDeleteButton',
'module' => 'SMM_Status_Module',
'width' => '5%',
'default' => true,
),
);
if(typeof subpanel_delete_record != 'function'){
window.subpanel_delete_record = function(sp,lf,li,rp) {
$.post( "/index.php?entryPoint=subpanel-delete",
{
id: li
},
function(data) {
showSubPanel('contacts_smm_status_module_1', null, true);
});
};
}
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
global $current_user, $db;
$id = $_POST['id'];
$sql = "UPDATE smm_status_module
SET deleted = 1,
date_modified = NOW(),
modified_user_id = '$current_user->id'
WHERE id = '$id'";
$db->query($sql);
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*
* For this file start by copying the file located in /include/generic/SugarWidgets/SugarWidgetSubPanelRemoveButton.php
* into this new file and place it in /custom/include/generic/SugarWidgets/ (create the directories if needed)
*
* Then make the below updates to the file to enable actually deleting the record that the
* subpanel links to instead of only deleting the relationship and retaining the record.
*/
class SugarWidgetSubPanelDeleteButton extends SugarWidgetField
{
function displayHeaderCell(&$layout_def) {
return '&nbsp;';
}
function displayList(&$layout_def) {
global $app_strings;
global $subpanel_item_count;
// super ugs
echo '<script src="custom/ninjacode/subpanel-delete.js"></script>';
$unique_id = $layout_def['subpanel_id']."_remove_".$subpanel_item_count; //bug 51512
$parent_record_id = $_REQUEST['record'];
$parent_module = $_REQUEST['module'];
$action = 'DeleteRelationship';
$record = $layout_def['fields']['ID'];
$current_module=$layout_def['module'];
//in document revisions subpanel ,users are now allowed to
//delete the latest revsion of a document. this will be tested here
//and if the condition is met delete button will be removed.
$hideremove=false;
if ($current_module=='DocumentRevisions') {
if ($layout_def['fields']['ID']==$layout_def['fields']['LATEST_REVISION_ID']) {
$hideremove=true;
}
}
// Implicit Team-memberships are not "removeable"
elseif ($_REQUEST['module'] == 'Teams' && $current_module == 'Users') {
if($layout_def['fields']['UPLINE'] != translate('LBL_TEAM_UPLINE_EXPLICIT', 'Users')) {
$hideremove = true;
}
//We also cannot remove the user whose private team is set to the parent_record_id value
$user = new User();
$user->retrieve($layout_def['fields']['ID']);
if($parent_record_id == $user->getPrivateTeamID())
{
$hideremove = true;
}
}
$return_module = $_REQUEST['module'];
$return_action = 'SubPanelViewer';
$subpanel = $layout_def['subpanel_id'];
$return_id = $_REQUEST['record'];
if (isset($layout_def['linked_field_set']) && !empty($layout_def['linked_field_set'])) {
$linked_field= $layout_def['linked_field_set'] ;
} else {
$linked_field = $layout_def['linked_field'];
}
$refresh_page = 0;
if(!empty($layout_def['refresh_page'])){
$refresh_page = 1;
}
$return_url = "index.php?module=$return_module&action=$return_action&subpanel=$subpanel&record=$return_id&sugar_body_only=1&inline=1";
//$icon_remove_text = strtolower($app_strings['LBL_ID_FF_REMOVE']);
$icon_remove_text = 'delete';
if($linked_field == 'get_emails_by_assign_or_link')
$linked_field = 'emails';
//based on listview since that lets you select records
if($layout_def['ListView'] && !$hideremove) {
$retStr = "<a href=\"javascript:subpanel_delete_record('$subpanel', '$linked_field', '$record', $refresh_page);\""
. ' class="listViewTdToolsS1"'
. " id=$unique_id"
. " onclick=\"return sp_del_conf();\""
. ">$icon_remove_text</a>";
return $retStr;
}
else {
return '';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment