1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8//this script may only be included - so its better to die if called directly.
9if (strpos($_SERVER["SCRIPT_NAME"], basename(__FILE__)) !== false) {
10	header("location: index.php");
11	exit;
12}
13
14class AddressBookLib extends TikiLib
15{
16	function list_address_books($user, $offset = -1, $maxRecords = -1) {
17		$query = "select * from `tiki_address_books` where `user` = ? order by `name`";
18		$bindvars = [$user];
19		return $this->fetchAll($query, $bindvars, $maxRecords, $offset);
20	}
21
22	function update_address_book($addressBookId, $data) {
23		if ($addressBookId) {
24			$update = [];
25			$bindvars = [];
26			foreach ($data as $key => $val) {
27				$update[] = "`$key` = ?";
28				$bindvars[] = $val;
29			}
30			$query = "update `tiki_address_books` set ".implode(', ', $update)." where addressBookId = ?";
31			$bindvars[] = $addressBookId;
32			$this->query($query, $bindvars);
33		} else {
34			$update = [];
35			$bindvars = [];
36			foreach ($data as $key => $val) {
37				$update[] = "`$key` = ?";
38				$bindvars[] = $val;
39			}
40			$query = "insert into `tiki_address_books` set ".implode(', ', $update);
41			$this->query($query, $bindvars);
42			return $this->lastInsertId();
43		}
44	}
45
46	function delete_address_book($addressBookId) {
47		$this->query("delete from `tiki_address_books` where `addressBookId` = ?", [$addressBookId]);
48		$this->query("delete from `tiki_address_cards` where `addressBookId` = ?", [$addressBookId]);
49	}
50
51	function get_address_book($addressBookId) {
52		$result = $this->query("select * from `tiki_address_books` where `addressBookId` = ?", $addressBookId);
53		return $result->fetchRow();
54	}
55
56	function list_cards($addressBookId, $offset = -1, $maxRecords = -1, $cardUris = []) {
57		$query = "select * from `tiki_address_cards` where `addressBookId` = ?";
58		$bindvars = [$addressBookId];
59		if ($cardUris) {
60			$query .= " and `uri` in (".implode(',', array_fill(0, count($cardUris), '?')).")";
61			$bindvars = array_merge($bindvars, $cardUris);
62		}
63		$query .= " order by `addressCardId`";
64		return $this->fetchAll($query, $bindvars, $maxRecords, $offset);
65	}
66
67	function create_card($data) {
68		$update = [];
69		$bindvars = [];
70		foreach ($data as $field => $value) {
71			if (!in_array($field, ['carddata', 'uri', 'addressBookId', 'lastmodified', 'size', 'etag'])) {
72				continue;
73			}
74			$update[] = "`$field` = ?";
75			$bindvars[] = $value;
76		}
77		$query = "insert into `tiki_address_cards` set ".implode(', ', $update);
78		$this->query($query, $bindvars);
79		return $this->lastInsertId();
80	}
81
82	function update_card($addressBookId, $uri, $data) {
83		$update = [];
84		$bindvars = [];
85		foreach ($data as $field => $value) {
86			if (!in_array($field, ['carddata', 'uri', 'addressBookId', 'lastmodified', 'size', 'etag'])) {
87				continue;
88			}
89			$update[] = "`$field` = ?";
90			$bindvars[] = $value;
91		}
92		$query = "update `tiki_address_cards` set ".implode(', ', $update)." where `addressBookId` = ? and `uri` = ?";
93		$bindvars[] = $addressBookId;
94		$bindvars[] = $uri;
95		return $this->query($query, $bindvars);
96	}
97
98	function delete_card($addressBookId, $uri) {
99		$this->query("delete from `tiki_address_cards` where `addressBookId` = ? and `uri` = ?", [$addressBookId, $uri]);
100	}
101}
102