Exemplo n.º 1
0
/**
 * Retrieve a collection of beans tha are related to the specified bean.
 * As of 4.5.1c, all combinations of related modules are supported
 *
 * @param String $session -- Session ID returned by a previous call to login.
 * @param String $module_name -- The name of the module that the primary record is from.  This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method)..
 * @param String $module_id -- The ID of the bean in the specified module
 * @param String $related_module -- The name of the related module to return records from.  This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method)..
 * @param String $related_module_query -- A portion of the where clause of the SQL statement to find the related items.  The SQL query will already be filtered to only include the beans that are related to the specified bean.
 * @param Number $deleted -- false if deleted records should not be include, true if deleted records should be included.
 * @return unknown
 */
function get_relationships($session, $module_name, $module_id, $related_module, $related_module_query, $deleted)
{
    $error = new SoapError();
    $ids = array();
    if (!validate_authenticated($session)) {
        $error->set_error('invalid_login');
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    }
    global $beanList, $beanFiles;
    $error = new SoapError();
    if (empty($beanList[$module_name]) || empty($beanList[$related_module])) {
        $error->set_error('no_module');
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    }
    $class_name = $beanList[$module_name];
    require_once $beanFiles[$class_name];
    $mod = new $class_name();
    $mod->retrieve($module_id);
    if (!$mod->ACLAccess('DetailView')) {
        $error->set_error('no_access');
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    }
    require_once 'include/SugarSQLValidate.php';
    $valid = new SugarSQLValidate();
    if (!$valid->validateQueryClauses($related_module_query)) {
        $GLOBALS['log']->error("Bad query: {$related_module_query}");
        $error->set_error('no_access');
        return array('result_count' => -1, 'error' => $error->get_soap_array());
    }
    $id_list = get_linked_records($related_module, $module_name, $module_id);
    if ($id_list === FALSE) {
        $error->set_error('no_relationship_support');
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    } elseif (count($id_list) == 0) {
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    }
    $list = array();
    $in = "'" . implode("', '", $id_list) . "'";
    $related_class_name = $beanList[$related_module];
    require_once $beanFiles[$related_class_name];
    $related_mod = new $related_class_name();
    $sql = "SELECT {$related_mod->table_name}.id FROM {$related_mod->table_name} ";
    if (isset($related_mod->custom_fields)) {
        $customJoin = $related_mod->custom_fields->getJOIN();
        $sql .= $customJoin ? $customJoin['join'] : '';
    }
    $sql .= " WHERE {$related_mod->table_name}.id IN ({$in}) ";
    if (!empty($related_module_query)) {
        $sql .= " AND ( {$related_module_query} )";
    }
    $result = $related_mod->db->query($sql);
    while ($row = $related_mod->db->fetchByAssoc($result)) {
        $list[] = $row['id'];
    }
    $return_list = array();
    foreach ($list as $id) {
        $related_class_name = $beanList[$related_module];
        $related_mod = new $related_class_name();
        $related_mod->retrieve($id);
        $return_list[] = array('id' => $id, 'date_modified' => $related_mod->date_modified, 'deleted' => $related_mod->deleted);
    }
    return array('ids' => $return_list, 'error' => $error->get_soap_array());
}
Exemplo n.º 2
0
/**
 * Retrieve a collection of beans tha are related to the specified bean.
 * As of 4.5.1c, all combinations of related modules are supported
 *
 * @param String $session -- Session ID returned by a previous call to login.
 * @param String $module_name -- The name of the module that the primary record is from.  This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method)..
 * @param String $module_id -- The ID of the bean in the specified module
 * @param String $related_module -- The name of the related module to return records from.  This name should be the name the module was developed under (changing a tab name is studio does not affect the name that should be passed into this method)..
 * @param String $related_module_query -- A portion of the where clause of the SQL statement to find the related items.  The SQL query will already be filtered to only include the beans that are related to the specified bean.
 * @param Number $deleted -- false if deleted records should not be include, true if deleted records should be included.
 * @return unknown
 */
function get_relationships($session, $module_name, $module_id, $related_module, $related_module_query, $deleted)
{
    $error = new SoapError();
    $ids = array();
    if (!validate_authenticated($session)) {
        $error->set_error('invalid_login');
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    }
    global $beanList, $beanFiles;
    $error = new SoapError();
    if (empty($beanList[$module_name]) || empty($beanList[$related_module])) {
        $error->set_error('no_module');
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    }
    $class_name = $beanList[$module_name];
    require_once $beanFiles[$class_name];
    $mod = new $class_name();
    $mod->retrieve($module_id);
    if (!$mod->ACLAccess('DetailView')) {
        $error->set_error('no_access');
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    }
    $id_list = get_linked_records($related_module, $module_name, $module_id);
    if ($id_list === FALSE) {
        $error->set_error('no_relationship_support');
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    } elseif (count($id_list) == 0) {
        return array('ids' => $ids, 'error' => $error->get_soap_array());
    }
    $list = array();
    $id_list_quoted = array_map(create_function('$str', 'return "\'" . $str . "\'";'), $id_list);
    $in = implode(", ", $id_list_quoted);
    $related_class_name = $beanList[$related_module];
    require_once $beanFiles[$related_class_name];
    $related_mod = new $related_class_name();
    $sql = "SELECT {$related_mod->table_name}.id FROM {$related_mod->table_name} ";
    $sql .= " WHERE {$related_mod->table_name}.id IN ({$in}) ";
    if (!empty($related_module_query)) {
        $sql .= " AND ( {$related_module_query} )";
    }
    $result = $related_mod->db->query($sql);
    while ($row = $related_mod->db->fetchByAssoc($result)) {
        $list[] = $row['id'];
    }
    $return_list = array();
    foreach ($list as $id) {
        $related_class_name = $beanList[$related_module];
        $related_mod = new $related_class_name();
        $related_mod->retrieve($id);
        $return_list[] = array('id' => $id, 'date_modified' => $related_mod->date_modified, 'deleted' => $related_mod->deleted);
    }
    return array('ids' => $return_list, 'error' => $error->get_soap_array());
}