Skip to content

Instantly share code, notes, and snippets.

@esimonetti
Last active August 25, 2017 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esimonetti/543d834a75be5fc64e050fffb92a772f to your computer and use it in GitHub Desktop.
Save esimonetti/543d834a75be5fc64e050fffb92a772f to your computer and use it in GitHub Desktop.
Sugar 7.9.1.0 example of how to extend the RelateApi to display on the Account's Notes subpanel all its Notes and all its first level members Accounts Notes
<?php
// Enrico Simonetti
// enricosimonetti.com
//
// 2017-08-25 on Sugar 7.9.1.0
// filename: custom/modules/Accounts/clients/base/api/CustomAccountsRelateApi.php
//
// Display on the Account's Notes subpanel all its Notes and all its first level members Accounts Notes
class CustomAccountsRelateApi extends AccountsRelateApi // that extends RelateApi
{
public function registerApiRest()
{
// make sure all the api definitison are returned, as we want to override RelateApi, but gaining also AccountsRelateApi
$relate = array(
'filterRelatedRecords' => array(
'reqType' => 'GET',
'path' => array('Accounts', '?', 'link', '?', 'filter'),
'pathVars' => array('module', 'record', '', 'link_name', ''),
'jsonParams' => array('filter'),
'method' => 'filterRelated',
'shortHelp' => 'Lists related filtered records.',
'longHelp' => 'include/api/help/module_record_link_link_name_filter_get_help.html',
),
'filterRelatedRecordsCount' => array(
'reqType' => 'GET',
'path' => array('Accounts', '?', 'link', '?', 'filter', 'count'),
'pathVars' => array('module', 'record', '', 'link_name', '', ''),
'jsonParams' => array('filter'),
'method' => 'filterRelatedCount',
'shortHelp' => 'Lists related filtered records.',
'longHelp' => 'include/api/help/module_record_link_link_name_filter_get_help.html',
),
'listRelatedRecords' => array(
'reqType' => 'GET',
'path' => array('Accounts', '?', 'link', '?'),
'pathVars' => array('module', 'record', '', 'link_name'),
'jsonParams' => array('filter'),
'method' => 'filterRelated',
'shortHelp' => 'Lists related records.',
'longHelp' => 'include/api/help/module_record_link_link_name_filter_get_help.html',
),
'listRelatedRecordsCount' => array(
'reqType' => 'GET',
'path' => array('Accounts', '?', 'link', '?', 'count'),
'pathVars' => array('module', 'record', '', 'link_name', ''),
'jsonParams' => array('filter'),
'method' => 'filterRelatedCount',
'shortHelp' => 'Counts all filtered related records.',
'longHelp' => 'include/api/help/module_record_link_link_name_filter_get_help.html',
),
);
return array_merge(parent::registerApiRest(), $relate);
}
public function filterRelatedSetup(ServiceBase $api, array $args)
{
if($args['link_name'] != 'notes') {
return parent::filterRelatedSetup($api, $args);
} else {
// only for Account's notes. use custom implementation from the current 7.9.1.0 RelateApi method as a base
// might need update in the future to be in line with additional functionality added
// Load the parent bean.
$record = BeanFactory::retrieveBean($args['module'], $args['record']);
if (empty($record)) {
throw new SugarApiExceptionNotFound(
sprintf(
'Could not find parent record %s in module: %s',
$args['record'],
$args['module']
)
);
}
// Load the relationship.
$linkName = $args['link_name'];
if (!$record->load_relationship($linkName)) {
// The relationship did not load.
throw new SugarApiExceptionNotFound('Could not find a relationship named: ' . $args['link_name']);
}
$linkModuleName = $record->$linkName->getRelatedModuleName();
$linkSeed = BeanFactory::newBean($linkModuleName);
if (!$linkSeed->ACLAccess('list')) {
throw new SugarApiExceptionNotAuthorized('No access to list records for module: ' . $linkModuleName);
}
$options = $this->parseArguments($api, $args, $linkSeed);
// If they don't have fields selected we need to include any link fields
// for this relationship
if (empty($args['fields']) && is_array($linkSeed->field_defs)) {
$relatedLinkName = $record->$linkName->getRelatedModuleLinkName();
$options['linkDataFields'] = array();
foreach ($linkSeed->field_defs as $field => $def) {
if (empty($def['rname_link']) || empty($def['link'])) {
continue;
}
if ($def['link'] != $relatedLinkName) {
continue;
}
// It's a match
$options['linkDataFields'][] = $field;
$options['select'][] = $field;
}
}
// In case the view parameter is set, reflect those fields in the
// fields argument as well so formatBean only takes those fields
// into account instead of every bean property.
if (!empty($args['view'])) {
$args['fields'] = $options['select'];
} elseif (!empty($args['fields'])) {
$args['fields'] = $this->normalizeFields($args['fields'], $options['displayParams']);
}
$q = self::getQueryObject($linkSeed, $options);
// Some relationships want the role column ignored
if (!empty($args['ignore_role'])) {
$ignoreRole = true;
} else {
$ignoreRole = false;
}
// START - CUSTOM CODE
//$q->joinSubpanel($record, $linkName, array('joinType' => 'INNER', 'ignoreRole' => $ignoreRole));
//$q->setJoinOn(array('baseBeanId' => $record->id));
// get all child accounts
$record->load_relationship('members');
$members = $record->members->get();
$q->where()->queryAnd()->equals('notes.parent_type', 'Accounts');
$orCondition = $q->where()->queryOr();
$orCondition->equals('notes.parent_id', $record->id);
// add all children id as an or query condition
if(!empty($members)) {
foreach($members as $member) {
$orCondition->equals('notes.parent_id', $member);
}
}
// END - CUSTOM CODE
if (!isset($args['filter']) || !is_array($args['filter'])) {
$args['filter'] = array();
}
self::addFilters($args['filter'], $q->where(), $q);
if (!sizeof($q->order_by)) {
self::addOrderBy($q, $this->defaultOrderBy);
}
return array($args, $q, $options, $linkSeed);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment