<?php

// -----------------------------------------------------------------------
// This file is part of aroundme
//
// Copyright (C) 2003, 2005 Barnraiser
// http://www.barnraiser.org/
// info@barnraiser.org
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option) any
// later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with program; see the file COPYING. If not, write to the Free
// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
// -----------------------------------------------------------------------

include "inc/header.inc.php";


if (isset($frmdata['adv'])) {
	// Gettings list of friends
	require_once($c_node['dir']['class_path'] . 'SocialNetwork.class.php');
	$sn = new Socialnetwork($me['user_id'], $db, $c_node['node']['node_id'], $c_node['sn']['max_degree']);
	$sn->relation_type_ids = $c_node['user']['relation_type'];
	$sn->status_ids = $c_node['user']['status'];
	$sn->date = $date;
	$output_friend_ids = $sn->getFriends($me['user_id']);
	if (!empty($output_friend_ids)) {
		foreach($output_friend_ids as $key => $i):
			$user->user_ids[] = $i;
		endforeach;
	}
	unset ($user->file);
	$user->orderby = "p.profile_nic";
	$output_friends = $user->getProfiles();
	if (!empty($output_friends)) {
		foreach($output_friends as $key => $i):
			if (empty($i['profile_allow_msg'])) {
			   unset($output_friends[$key]);
			}
		endforeach;
	}

	if (isset($frmdata['rsp_type_id'])) {
		$rsp_type_id = $frmdata['rsp_type_id'];
	}

	if (isset($frmdata['language_id'])) {
		$language_id = $frmdata['language_id'];
	}

	if (isset($frmdata['user_id'])) {

	}
}

if (isset($frmdata['search_text']) && strlen(trim($frmdata['search_text'])) >= 3) {
   $search_text = trim($frmdata['search_text']);

	$file->noimage = $c_node['file']['image']['thumb'][3]['noimage'];
	$file->thumb = $c_node['file']['image']['thumb'][3]['name'];


	// GROUP SEARCH --------------------------------------------------
	require_once($languageArrayPath . 'arr_category.lang.php');
	require_once($languageArrayPath . 'arr_group.lang.php');
	require_once($c_node['dir']['class_path'] . 'Group.class.php');
	$group = new Group($db, $date, $file);
	$group->registration_types = $c_node['group']['registration_type'];
	$group->group_visibility = 1;
	$output_groups = $group->getGroups($search_text);

	
	//get blogs -----------------------------------------------------
	require_once($c_node['dir']['class_path'] . 'Blog.class.php');
	$blog = new Blog($db, $c_node['rsp']['item_type']['blog'], $date, null);
	$blog->rsp_level_id = $c_node['rsp']['rsp_level']['node'];
	$blog->rsp_status_id = $c_node['rsp']['status']['live'];

	if (isset($frmdata['degree_friend'])) {
		$blog->degree_id = $degree_id;
		$blog->rsp_item_type_ids = $c_node['rsp']['rsp_level']['degree'];
		$blog->user_ids = $user->user_ids;
	}

	$output_blogs = $blog->selRspBlogs($search_text);



	//get library items -----------------------------------------------------
	require_once($c_node['dir']['class_path'] . 'Library.class.php');
	$library = new Library($db, $c_node['rsp']['item_type']['library'], $date, null);
	$library->rsp_level_id = $c_node['rsp']['rsp_level']['node'];
	$library->rsp_status_id = $c_node['rsp']['status']['live'];

	if (isset($frmdata['degree_friend'])) {
		$library->degree_id = $degree_id;
		$library->rsp_item_type_ids = $c_node['rsp']['rsp_level']['degree'];
		$library->user_ids = $user->user_ids;
	}

	$output_library_items = $library->selRspLibraryItems($search_text);


	//get polls  -----------------------------------------------------
	require_once($c_node['dir']['class_path'] . 'Poll.class.php');
	$poll = new Poll($db, $c_node['rsp']['item_type']['poll'], $date);
	$poll->order_by = "p.poll_start_datetime desc";
	$poll->rsp_level_id = $c_node['rsp']['rsp_level']['node'];
	$poll->rsp_status_id = $c_node['rsp']['status']['live'];

	if (isset($frmdata['degree_friend'])) {
		$poll->degree_id = $degree_id;
		$poll->rsp_item_type_ids = $c_node['rsp']['rsp_level']['degree'];
		$poll->user_ids = $user->user_ids;
	}

	$output_polls = $poll->selRspPolls($search_text);


	//get subjects  -----------------------------------------------------
	require_once($c_node['dir']['class_path'] . 'Forum.class.php');
	$forum = new Forum($db, $c_node['rsp']['item_type']['forum'], $date, null);
	$forum->rsp_level_id = $c_node['rsp']['rsp_level']['node'];
	$forum->rsp_status_id = $c_node['rsp']['status']['live'];

	if (isset($frmdata['degree_friend'])) {
		$forum->degree_id = $degree_id;
		$forum->rsp_item_type_ids = $c_node['rsp']['rsp_level']['degree'];
		$forum->user_ids = $user->user_ids;
	}

	$output_subjects = $forum->selRspSubjects($search_text);


	// ACTIVITY SEARCH --------------------------------------------------
	require_once($c_node['dir']['class_path'] . 'Schedule.class.php');
	require_once($languageArrayPath . 'arr_schedule.lang.php');
	$schedule = new Schedule($db, $c_node['rsp']['item_type']['schedule'], $date, $file);
	$schedule->rsp_level_id = $c_node['rsp']['rsp_level']['node'];
	$schedule->rsp_status_id = $c_node['rsp']['status']['live'];

	if (isset($frmdata['degree_friend'])) {
		$schedule->degree_id = $degree_id;
		$schedule->rsp_item_type_ids = $c_node['rsp']['rsp_level']['degree'];
		$schedule->user_ids = $user->user_ids;
	}

	$schedule->schedule_type_id = $c_node['schedule']['type']['event'];
	$output_activities = $schedule->selRspScheduleItems($search_text);;
	$schedule->schedule_type_id = $c_node['schedule']['type']['task'];
	$output_tasks = $schedule->selRspScheduleItems($search_text);;

}
elseif (isset($data['search_text']) && strlen(trim($data['search_text'])) < 3) {
	$GLOBALS['am_error_log'][] = array(41, null, null);
}
else {
	//ERROR NO SEARCH TEXT
}






//move to template
if (!empty($output_subjects)) {
    $body->set('subjects', $output_subjects);
}

if (!empty($output_activities)) {
	$body->set('activities', $output_activities);
}

if (!empty($output_tasks)) {
	$body->set('tasks', $output_tasks);
}

if (!empty($output_blogs)) {
    $body->set('blogs', $output_blogs);
}

if (!empty($output_groups)) {
    $body->set('groups', $output_groups);
}

if (!empty($output_library_items)) {
    $body->set('library_items', $output_library_items);
}

if (!empty($output_polls)) {
    $body->set('polls', $output_polls);
}


if (!empty($output_friends)) {
    $body->set('users', $output_friends);
}



if (!empty($search_text)) {
    $body->set('search_text', $search_text);
	$lang['txt_search_results'] = str_replace("SEARCHTEXT", "\"" . $search_text . "\"", $lang['txt_search_results']);
}

$template_pageLink = $lang['txt_search'];
include "inc/footer.inc.php";

?>
