if ($form_view == 'recommendations') {
    // get all the form data with recommendations, sorted with the most recommended at the top
    // TODO: replace with more efficient SQL
    $all_entities = get_entities_from_annotations("object", "form_data", "form:recommendation", "", 0, 0, 5000);
    if ($all_entities) {
        $entity_list = array();
        foreach ($all_entities as $entity) {
            if ($entity->form_id == $form_id) {
                $count = count_annotations($entity->getGUID(), "object", "form_data", "form:recommendation");
                $item = new StdClass();
                $item->entity = $entity;
                $item->count = $count;
                $entity_list[] = $item;
            }
        }
        $sorted = form_vsort($entity_list, 'count', true);
        foreach ($sorted as $item) {
            $entities[] = $item->entity;
        }
        $entities = array_slice($entities, $offset, $limit);
    }
} else {
    if ($form_view == 'mine') {
        $user_guid = $user->getGUID();
    } else {
        if ($form_view == 'friends') {
            // handles up to 5000 friends
            $friends = $user->getFriends("", 5000);
            $user_guid = array();
            if ($friends) {
                foreach ($friends as $friend) {
function form_get_data_with_search_conditions($conditions, $sd, $limit, $offset)
{
    global $CONFIG;
    $search_order_field = trim($sd->search_order);
    $form = get_entity($sd->form_id);
    // will return at most 500 search results
    if ($form->profile == 1) {
        // this is a profile form, so get the data from users
        $entities = get_entities_from_metadata_multi($conditions, 'user', '', 0, 500);
    } else {
        if ($form->profile == 2) {
            // this is a profile form, so get the data from groups
            // if a profile category is set for this form, restrict the search
            // to groups of that category
            if ($form->profile_category) {
                $conditions['group_profile_category'] = $form->profile_category;
            }
            $entities = get_entities_from_metadata_multi($conditions, 'group', '', 0, 500);
        } else {
            if ($conditions) {
                $conditions['form_id'] = $sd->form_id;
                $entities = get_entities_from_metadata_multi($conditions, 'object', 'form_data', 0, 500);
            } else {
                // if no conditions, return everything from the relevant form
                $entities = get_entities_from_metadata('form_id', $sd->form_id, 'object', 'form_data', 0, 500);
            }
        }
    }
    if ($entities) {
        if (!$form->profile) {
            // filter by language if appropriate
            $new_results2 = array();
            $view_languages = array();
            if (isloggedin()) {
                $key = 'form:view_content_languages';
                if (!empty($_SESSION['user']->{$key})) {
                    $view_languages = explode(',', $_SESSION['user']->{$key});
                }
            }
            // we can always see content in the current language
            $view_languages[] = $CONFIG->language;
            foreach ($entities as $entity) {
                if (empty($entity->{$_language}) || in_array($entity->{$_language}, $view_languages)) {
                    $new_results2[] = $entity;
                }
            }
        } else {
            $new_results2 = $entities;
        }
        // sort by search order if required
        if ($search_order_field) {
            $new_results3 = form_vsort($new_results2, $search_order_field);
        } else {
            $new_results3 = $new_results2;
        }
        // now slice the results using limit and offset
        $count = count($new_results3);
        return array($count, array_slice($new_results3, $offset, $limit));
    } else {
        return array(0, $entities);
    }
    //    } else {
    //        // no special handling required
    //        return get_entities_from_metadata_multi($conditions, 'object', 'form_data', 0, $limit, $offset, "", 0, false);
    //    }
}