<?php
namespace FAAPI;

$path_to_root = "../..";

include_once($path_to_root."/modules/church/includes/group.php");

class MemberGroups {
	// Get Items
	public function get($rest) {
		$req = $rest->request();

		$from = isset($_GET["start"]) ? $_GET["start"] : null;
                $length = isset($_GET["length"]) ? $_GET["length"] : null;
                $term = isset($_GET["term"]) ? $_GET["term"] : '';
		$this->groups_all($from,$length,$term);
	}

	// Get 
	public function getById($id) {
            $sql = "SELECT * FROM " . TB_PREF . "per_groups WHERE id=".db_escape($id);
            $result = db_query($sql, _('Could not get group data.'));
            $row = db_fetch($result);
		if (! $row)
			$row = array();
		api_success_response(json_encode($row));
	}

	

	private function groups_all($from = null,$length = null,$term = '') {
		if ($from == null)
			$from = 0;
                if ($length == null)
			$length = RESULTS_PER_PAGE;
                if ($term == null)
			$term = '';
		$sql = "SELECT * FROM " . TB_PREF . "per_groups WHERE status and name like ".  db_escape("%".$term."%")." LIMIT " . $from . ", " . $length;
                $query = db_query($sql, "error");
                
		$info = array();
		while ($data = db_fetch($query, "error")) {
			$info[] = array(
				'id' => $data['id'],
				'text' => $data['name'],
				'description' => $data['description']
			);
		}
                $response['items'] = $info;
                $response['incomplete_results'] = FALSE;
                $response['total_count'] = 10;
		api_success_response(json_encode($response));
	}
}