/**
  * get_modified_relationships
  *
  * Get a list of the relationship records that have a date_modified value set within a specified date range.  This is used to
  * help facilitate sync operations.  The module_name should be "Users" and the related_module one of "Meetings", "Calls" and
  * "Contacts".
  *
  * @param xsd:string $session String of the session id
  * @param xsd:string $module_name String value of the primary module to retrieve relationship against
  * @param xsd:string $related_module String value of the related module to retrieve records off of
  * @param xsd:string $from_date String value in YYYY-MM-DD HH:MM:SS format of date_start range (required)
  * @param xsd:string $to_date String value in YYYY-MM-DD HH:MM:SS format of ending date_start range (required)
  * @param xsd:int $offset Integer value of the offset to begin returning records from
  * @param xsd:int $max_results Integer value of the max_results to return; -99 for unlimited
  * @param xsd:int $deleted Integer value indicating deleted column value search (defaults to 0).  Set to 1 to find deleted records
  * @param xsd:string $module_user_id String value of the user id (optional, but defaults to SOAP session user id anyway)  The module_user_id value
  * here ought to be the user id of the user initiating the SOAP session
  * @param tns:select_fields $select_fields Array value of fields to select and return as name/value pairs
  * @param xsd:string $relationship_name String value of the relationship name to search on
  * @param xsd:string $deletion_date String value in YYYY-MM-DD HH:MM:SS format for filtering on deleted records whose date_modified falls within range
  * this allows deleted records to be returned as well
  *
  * @return Array records that match search criteria
  */
 function get_modified_relationships($session, $module_name, $related_module, $from_date, $to_date, $offset, $max_results, $deleted = 0, $module_user_id = '', $select_fields = array(), $relationship_name = '', $deletion_date = '')
 {
     global $beanList, $beanFiles, $current_user;
     $error = new SoapError();
     $output_list = array();
     if (empty($from_date)) {
         $error->set_error('invalid_call_error, missing from_date');
         return array('result_count' => 0, 'next_offset' => 0, 'field_list' => $select_fields, 'entry_list' => array(), 'error' => $error->get_soap_array());
     }
     if (empty($to_date)) {
         $error->set_error('invalid_call_error, missing to_date');
         return array('result_count' => 0, 'next_offset' => 0, 'field_list' => $select_fields, 'entry_list' => array(), 'error' => $error->get_soap_array());
     }
     self::$helperObject = new SugarWebServiceUtilv4_1();
     if (!self::$helperObject->checkSessionAndModuleAccess($session, 'invalid_session', $module_name, 'read', 'no_access', $error)) {
         Log::info('End: SugarWebServiceImpl->get_modified_relationships');
         return;
     }
     // if
     if (empty($beanList[$module_name]) || empty($beanList[$related_module])) {
         $error->set_error('no_module');
         return array('result_count' => 0, 'next_offset' => 0, 'field_list' => $select_fields, 'entry_list' => array(), 'error' => $error->get_soap_array());
     }
     global $current_user;
     if (!self::$helperObject->check_modules_access($current_user, $module_name, 'read') || !self::$helperObject->check_modules_access($current_user, $related_module, 'read')) {
         $error->set_error('no_access');
         return array('result_count' => 0, 'next_offset' => 0, 'field_list' => $select_fields, 'entry_list' => array(), 'error' => $error->get_soap_array());
     }
     if ($max_results > 0 || $max_results == '-99') {
         global $sugar_config;
         $sugar_config['list_max_entries_per_page'] = $max_results;
     }
     // Cast to integer
     $deleted = (int) $deleted;
     $query = "(m1.date_modified > " . db_convert("'" . $GLOBALS['db']->quote($from_date) . "'", 'datetime') . " AND m1.date_modified <= " . db_convert("'" . $GLOBALS['db']->quote($to_date) . "'", 'datetime') . " AND {0}.deleted = {$deleted})";
     if (isset($deletion_date) && !empty($deletion_date)) {
         $query .= " OR ({0}.date_modified > " . db_convert("'" . $GLOBALS['db']->quote($deletion_date) . "'", 'datetime') . " AND {0}.date_modified <= " . db_convert("'" . $GLOBALS['db']->quote($to_date) . "'", 'datetime') . " AND {0}.deleted = 1)";
     }
     if (!empty($current_user->id)) {
         $query .= " AND m2.id = '" . $GLOBALS['db']->quote($current_user->id) . "'";
     }
     //if($related_module == 'Meetings' || $related_module == 'Calls' || $related_module = 'Contacts'){
     $query = string_format($query, array('m1'));
     //}
     require_once 'soap/SoapRelationshipHelper.php';
     $results = retrieve_modified_relationships($module_name, $related_module, $query, $deleted, $offset, $max_results, $select_fields, $relationship_name);
     $list = $results['result'];
     foreach ($list as $value) {
         $output_list[] = self::$helperObject->array_get_return_value($value, $results['table_name']);
     }
     $next_offset = $offset + count($output_list);
     return array('result_count' => count($output_list), 'next_offset' => $next_offset, 'entry_list' => $output_list, 'error' => $error->get_soap_array());
 }
Exemple #2
0
/**
 * Get a list of the relationship records that have been modified within a 
 * specified date range.  This is used to perform a sync with a mobile client.
 * The results are paged.
 *
 * @param xsd:string $session
 * @param xsd:string $module_name
 * @param xsd:string $related_module
 * @param xsd:string $from_date
 * @param xsd:string $to_date
 * @param xsd:int $offset
 * @param xsd:int $max_results
 * @param xsd:int $deleted
 * @param xsd:int $module_id
 * @param tns:select_fields $select_fields
 * @param tns:select_fields $ids
 * @param xsd:string $relationship_name
 * @param xsd:string $deletion_date
 * @param xsd:int $php_serialize
 * @return 
 */
function sync_get_modified_relationships($session, $module_name, $related_module, $from_date, $to_date, $offset, $max_results, $deleted, $module_id = '', $select_fields = array(), $ids = array(), $relationship_name = '', $deletion_date = '', $php_serialize = 1)
{
    global $beanList, $beanFiles;
    $error = new SoapError();
    $output_list = array();
    if (!validate_authenticated($session)) {
        $error->set_error('invalid_login');
        return array('result_count' => -1, 'entry_list' => array(), 'error' => $error->get_soap_array());
    }
    if (empty($beanList[$module_name]) || empty($beanList[$related_module])) {
        $error->set_error('no_module');
        return array('result_count' => -1, 'entry_list' => array(), 'error' => $error->get_soap_array());
    }
    global $current_user;
    if (!check_modules_access($current_user, $module_name, 'read') || !check_modules_access($current_user, $related_module, 'read')) {
        $error->set_error('no_access');
        return array('result_count' => -1, 'entry_list' => array(), 'error' => $error->get_soap_array());
    }
    // Cast to integer
    $deleted = (int) $deleted;
    if ($max_results > 0 || $max_results == '-99') {
        global $sugar_config;
        $sugar_config['list_max_entries_per_page'] = $max_results;
    }
    $date_query = "(m1.date_modified > " . db_convert("'" . $GLOBALS['db']->quote($from_date) . "'", 'datetime') . " AND m1.date_modified <= " . db_convert("'" . $GLOBALS['db']->quote($to_date) . "'", 'datetime') . " AND {0}.deleted = {$deleted})";
    if (isset($deletion_date) && !empty($deletion_date)) {
        $date_query .= " OR ({0}.date_modified > " . db_convert("'" . $GLOBALS['db']->quote($deletion_date) . "'", 'datetime') . " AND {0}.date_modified <= " . db_convert("'" . $GLOBALS['db']->quote($to_date) . "'", 'datetime') . " AND {0}.deleted = 1)";
    }
    $in = '';
    if (isset($ids) && !empty($ids)) {
        foreach ($ids as $value) {
            if (empty($in)) {
                $in .= "('" . $GLOBALS['db']->quote($value) . "'";
            } else {
                $in .= ",'" . $GLOBALS['db']->quote($value) . "'";
            }
        }
        $in .= ')';
    }
    $query = '';
    if (isset($in) && !empty($in)) {
        $query .= "( {$date_query} AND m1.id IN {$in}) OR (m1.id NOT IN {$in} AND {0}.deleted = 0)";
    } else {
        $query .= "( {0}.deleted = 0)";
    }
    if (isset($module_id) && !empty($module_id)) {
        //if(isset($in) && !empty($in)){
        $query .= " AND";
        //}
        $query .= " m2.id = '" . $GLOBALS['db']->quote($module_id) . "'";
    }
    if ($related_module == 'Meetings' || $related_module == 'Calls') {
        $query = string_format($query, array('m1'));
    }
    $results = retrieve_modified_relationships($module_name, $related_module, $query, $deleted, $offset, $max_results, $select_fields, $relationship_name);
    $list = $results['result'];
    $xml = '<?xml version="1.0" encoding="utf-8"?><items>';
    foreach ($list as $value) {
        $val = array_get_return_value($value, $results['table_name']);
        if ($php_serialize == 0) {
            $xml .= get_name_value_xml($val, $module_name);
        }
        $output_list[] = $val;
    }
    $xml .= '</items>';
    $next_offset = $offset + sizeof($output_list);
    if ($php_serialize == 0) {
        $myoutput = base64_encode($xml);
    } else {
        $myoutput = get_encoded($output_list);
    }
    return array('result_count' => sizeof($output_list), 'next_offset' => 0, 'total_count' => sizeof($output_list), 'field_list' => array(), 'entry_list' => $myoutput, 'error' => $error->get_soap_array());
}
 /**
  * testRetrieveModifiedRelationships
  * This test checks to make sure we can correctly retrieve related Meetings and Calls (see bugs 50092 & 50093)
  *
  */
 public function testRetrieveModifiedRelationships()
 {
     if ($GLOBALS['db']->dbType != 'mysql') {
         $this->markTestIncomplete("Currently these queries don't work on non-mysql DBs, skip until query is fixed.");
     }
     foreach ($this->testData as $data) {
         //retrieve_modified_relationships($module_name, $related_module, $relationship_query, $show_deleted, $offset, $max_results, $select_fields = array(), $relationship_name = '')
         $result = retrieve_modified_relationships($data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7]);
         $this->assertEquals($data[8]['id'], $result['result'][0]['id'], 'Ids do not match');
         $this->assertEquals($data[9], $result['total_count'], 'Totals do not match');
         $this->assertEquals($data[10], $result['error'], 'No SOAP Error');
     }
 }