aboutsummaryrefslogtreecommitdiff
path: root/classes/contact.php
blob: 7741c175c540d7105c6000195084ff92b103ad7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/**
 * Provides the contact class, an interface to the contact table in the database
 *
 * @author Camil Staps
 *
 * BusinessAdmin: administrative software for small companies
 * Copyright (C) 2015 Camil Staps (ViviSoft)
 *
 * 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 3 of the License, 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * An interface to the contact table in the database
 */
class contact {
	/**
	 * @var PDO $pdo                    The PDO class for database communication
	 * @var int $id                     The id of the contact
	 * @var int $clientId               The id of the client the contact is linked to
	 * @var string $name                The name of the contact
	 * @var string $email               The email address of the contact
	 * @var string $address             The first address line (normally street and house number) of the contact
	 * @var string $address_2           The second address line (can be null)
	 * @var string $postal_code         The postal code of the contact
	 * @var string $city                The city of the contact
	 * @var string $country             The country of the contact
	 * @var string $language            The language of the contact
	 */
	protected $pdo, $id, $clientId, $name, $email, $address, $postal_code, $city, $country, $language;

	/**
	 * Create a new instance
	 *
	 * @param PDO $pdo                  The PDO class, to access the database
	 * @param int $id                   The id of the contact to fetch
	 *
	 * @throws PDOException             If something went wrong with the database
	 * @throws Exception                If the contact could not be found
	 */
	public function __construct($pdo, $id) {
		$this->pdo = $pdo;

		$stmt = $this->pdo->prepare("SELECT * FROM `".constants::db_prefix."contact` WHERE `id`=?");
		$stmt->execute(array($id));
		if ($stmt->rowCount() == 0) {
			throw new Exception("The contact with id '$id' could not be found.");
		}
		$contact = $stmt->fetch(PDO::FETCH_ASSOC);

		$this->id = $contact['id'];
		$this->clientId = $contact['clientId'];
		$this->name = $contact['name'];
		$this->email = $contact['email'];
		$this->address = $contact['address'];
		$this->address_2 = $contact['address_2'];
		$this->postal_code = $contact['postal_code'];
		$this->city = $contact['city'];
		$this->country = $contact['country'];
		$this->language = $contact['language'];
	}

	//------------------------------------------------------------------------------
	// Getters and setters
	//------------------------------------------------------------------------------

	/**
	 * Get the ID of the contact
	 *
	 * @return int                      The ID
	 */
	public function getId() {
		return $this->id;
	}

	/**
	 * Get the ID of the client that this contact is linked to
	 *
	 * @return int                      The ID
	 */
	public function getClientId() {
		return $this->clientId;
	}

	/**
	 * Get the client that this contact is linked to
	 *
	 * @return client                   The client
	 */
	public function getClient() {
		return new client($this->pdo, $this->clientId);
	}

	/**
	 * Get the name of the contact
	 *
	 * @return string                   The name
	 */
	public function getName() {
		return $this->name;
	}

	/**
	 * Set the name of the contact
	 *
	 * @param string $name              The new name for the contact
	 *
	 * @throws PDOException             If something went wrong with the database
	 *
	 * @return bool                     True on succes, false on failure
	 */
	public function setName($name) {
		$stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."contact` SET `name`=? WHERE `id`=?");
		$stmt->execute(array($name, $this->id));
		if ($stmt->rowCount() == 1) {
			$this->name = $name;
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Get the email of the contact
	 *
	 * @return string                   The email
	 */
	public function getEmail() {
		return $this->email;
	}

	/**
	 * Set the email of the contact
	 *
	 * @param string $email             The new email for the contact
	 *
	 * @throws PDOException             If something went wrong with the database
	 *
	 * @return bool                     True on succes, false on failure
	 */
	public function setEmail($email) {
		$stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."contact` SET `email`=? WHERE `id`=?");
		$stmt->execute(array($email, $this->id));
		if ($stmt->rowCount() == 1) {
			$this->email = $email;
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Get the first address line of the contact
	 *
	 * @return string                   The address
	 */
	public function getAddress() {
		return $this->address;
	}

	/**
	 * Set the first address line of the contact
	 *
	 * @param string $address           The new address for the contact
	 *
	 * @throws PDOException             If something went wrong with the database
	 *
	 * @return bool                     True on succes, false on failure
	 */
	public function setAddress($address) {
		$stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."contact` SET `address`=? WHERE `id`=?");
		$stmt->execute(array($address, $this->id));
		if ($stmt->rowCount() == 1) {
			$this->address = $address;
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Get the second address line of the contact
	 *
	 * @return string                   The address
	 */
	public function getAddress_2() {
		return $this->address_2;
	}

	/**
	 * Set the second address line of the contact
	 *
	 * @param string $address_2         The new address for the contact
	 *
	 * @throws PDOException             If something went wrong with the database
	 *
	 * @return bool                     True on succes, false on failure
	 */
	public function setAddress_2($address_2) {
		$stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."contact` SET `address_2`=? WHERE `id`=?");
		$stmt->execute(array($address_2, $this->id));
		if ($stmt->rowCount() == 1) {
			$this->address_2 = $address_2;
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Get the postal_code of the contact
	 *
	 * @return string                   The postal_code
	 */
	public function getPostalCode() {
		return $this->postal_code;
	}

	/**
	 * Set the postal code of the contact
	 *
	 * @param $postal_code string       The new postal code for the contact
	 *
	 * @throws PDOException             If something went wrong with the database
	 *
	 * @return bool                     True on succes, false on failure
	 */
	public function setPostalCode($postal_code) {
		$stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."contact` SET `postal_code`=? WHERE `id`=?");
		$stmt->execute(array($postal_code, $this->id));
		if ($stmt->rowCount() == 1) {
			return true;
			$this->postal_code = $postal_code;
		} else {
			return false;
		}
	}

	/**
	 * Get the city of the contact
	 *
	 * @return string                   The city
	 */
	public function getCity() {
		return $this->city;
	}

	/**
	 * Set the city of the contact
	 *
	 * @param string $city              The new city for the contact
	 *
	 * @throws PDOException             If something went wrong with the database
	 *
	 * @return bool                     True on succes, false on failure
	 */
	public function setCity($city) {
		$stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."contact` SET `city`=? WHERE `id`=?");
		$stmt->execute(array($city, $this->id));
		if ($stmt->rowCount() == 1) {
			$this->city = $city;
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Get the country of the contact
	 *
	 * @return string                   The country
	 */
	public function getCountry() {
		return $this->country;
	}

	/**
	 * Set the country of the contact
	 *
	 * @param string $country           The new country for the contact
	 *
	 * @throws PDOException             If something went wrong with the database
	 *
	 * @return bool                     True on succes, false on failure
	 */
	public function setCountry($country) {
		$stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."contact` SET `country`=? WHERE `id`=?");
		$stmt->execute(array($country, $this->id));
		if ($stmt->rowCount() == 1) {
			$this->country = $country;
			return true;
		} else {
			return false;
		}
	}

	/**
	 * Get the language of the contact
	 *
	 * @return string                   The language
	 */
	public function getLanguage() {
		return $this->language;
	}

	/**
	 * Set the language of the contact
	 *
	 * @see correspondence::LANGUAGES   The available languages
	 *
	 * @param string $language          The new language for the contact
	 *
	 * @throws PDOException             If something went wrong with the database
	 * @throws Exception                If the language is unknown
	 *
	 * @return bool                     True on succes, false on failure
	 */
	public function setLanguage($language) {
		if (!in_array($language, correspondence::LANGUAGES)) {
			throw new Exception("Language $language not available.");
		}
		$stmt = $this->pdo->prepare("UPDATE `".constants::db_prefix."contact` SET `language`=? WHERE `id`=?");
		$stmt->execute(array($language, $this->id));
		if ($stmt->rowCount() == 1) {
			$this->language = $language;
			return true;
		} else {
			return false;
		}
	}

	//------------------------------------------------------------------------------
	// Other functions
	//------------------------------------------------------------------------------

	/**
	 * Remove this contact from the database
	 *
	 * If this doesn't succeed (i.e. false is returned), that means the contact was removed manually or by another instance of this class
	 *
	 * @throws PDOException             If something went wrong with the database
	 *
	 * @return bool                     True on success, false on failure
	 */
	public function delete() {
		$stmt = $this->pdo->prepare("DELETE FROM `".constants::db_prefix."contact` WHERE `id`=?");
		$stmt->execute(array($this->id));
		if ($stmt->rowCount() != 1) {
			return false;
		} else {
			return true;
		}
	}

	/**
	 * Make a new offer for this contact
	 *
	 * @throws PDOException             If something went wrong with the database
	 * @throws Exception                If there was a problem with the input
	 *
	 * @return offer                    A new instance of the offer class containing the new offer
	 */
	public function createOffer() {
		$stmt = $this->pdo->prepare("INSERT INTO `".constants::db_prefix."offer` (`contactId`) VALUES (?)");
		$stmt->execute(array($this->id));
		if ($stmt->rowCount() == 1) {
			return new offer($this->pdo, $this->pdo->lastInsertId());
		} else {
			$error = $stmt->errorInfo();
			throw new Exception($error[2]);
		}
	}
}