summaryrefslogtreecommitdiffstats
path: root/apps/contacts/index.php
blob: 0a847f5a672fd7e36f9949e4851c682809bf7894 (plain)
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
<?php
/**
 * ownCloud - Addressbook
 *
 * @author Jakob Sack
 * @copyright 2011 Jakob Sack mail@jakobsack.de
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

function contacts_namesort($a,$b){
	return strcmp($a['name'],$b['name']);
}

// Init owncloud
require_once('../../lib/base.php');

// Check if we are a user
OC_Util::checkLoggedIn();
OC_Util::checkAppEnabled('contacts');

// Check if the user has an addressbook
$addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
if( count($addressbooks) == 0){
	OC_Contacts_Addressbook::add(OC_User::getUser(),'default','Default Address Book');
	$addressbooks = OC_Contacts_Addressbook::all(OC_User::getUser());
}
$prefbooks = OC_Preferences::getValue(OC_User::getUser(),'contacts','openaddressbooks',null);
if(is_null($prefbooks)){
	$prefbooks = $addressbooks[0]['id'];
	OC_Preferences::setValue(OC_User::getUser(),'contacts','openaddressbooks',$prefbooks);
}

// Load the files we need
OC_App::setActiveNavigationEntry( 'contacts_index' );

// Load a specific user?
$id = isset( $_GET['id'] ) ? $_GET['id'] : null;

// sort addressbooks  (use contactsort)
usort($addressbooks,'contacts_namesort');
// Addressbooks to load
$openaddressbooks = explode(';',$prefbooks);

$contacts = array();
foreach( $openaddressbooks as $addressbook ){
	$addressbookcontacts = OC_Contacts_VCard::all($addressbook);
	foreach( $addressbookcontacts as $contact ){
		if(is_null($contact['fullname'])){
			continue;
		}
		$contacts[] = array( 'name' => $contact['fullname'], 'id' => $contact['id'] );
	}
}

usort($contacts,'contacts_namesort');
$details = array();

if( !is_null($id) || count($contacts)){
	if(is_null($id)) $id = $contacts[0]['id'];
	$contact = OC_Contacts_VCard::find($id);
	$vcard = OC_VObject::parse($contact['carddata']);
	$details = OC_Contacts_VCard::structureContact($vcard);
}

$adr_types = OC_Contacts_App::getTypesOfProperty(OC_Contacts_App::$l10n, 'ADR');
$phone_types = OC_Contacts_App::getTypesOfProperty(OC_Contacts_App::$l10n, 'TEL');

// Process the template
$tmpl = new OC_Template( 'contacts', 'index', 'user' );
$tmpl->assign('adr_types',$adr_types);
$tmpl->assign('phone_types',$phone_types);
$tmpl->assign('addressbooks', $addressbooks);
$tmpl->assign('contacts', $contacts);
$tmpl->assign('details', $details );
$tmpl->assign('id',$id);
$tmpl->printPage();