Blame view

sources/apps/contacts/lib/hooks.php 3.72 KB
03e52840d   Kload   Init
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
  <?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/>.
   *
   */
  
  /**
   * The following signals are being emitted:
   *
   * OCA\Contacts\VCard::post_moveToAddressbook(array('aid' => $aid, 'id' => $id))
   * OCA\Contacts\VCard::pre_deleteVCard(array('aid' => $aid, 'id' => $id, 'uri' = $uri)); (NOTE: the values can be null depending on which method emits them)
   * OCA\Contacts\VCard::post_updateVCard($id)
   * OCA\Contacts\VCard::post_createVCard($newid)
   */
  
  namespace OCA\Contacts;
  
  use \Sabre\VObject;
  
  /**
   * This class contains all hooks.
   */
  class Hooks{
  	/**
  	 * @brief Add default Addressbook for a certain user
  	 * @param paramters parameters from postCreateUser-Hook
  	 * @return array
  	 */
  	static public function createUser($parameters) {
  		Addressbook::addDefault($parameters['uid']);
  		return true;
  	}
  
  	/**
  	 * @brief Deletes all Addressbooks of a certain user
  	 * @param paramters parameters from postDeleteUser-Hook
  	 * @return array
  	 */
  	static public function deleteUser($parameters) {
  		$addressbooks = Addressbook::all($parameters['uid']);
  
  		foreach($addressbooks as $addressbook) {
  			if($parameters['uid'] === $addressbook['userid']) {
  				Addressbook::delete($addressbook['id']);
  			}
  		}
  
  		return true;
  	}
  
  	static public function getCalenderSources($parameters) {
  		$base_url = \OCP\Util::linkTo('calendar', 'ajax/events.php').'?calendar_id=';
  		foreach(Addressbook::all(\OCP\USER::getUser()) as $addressbook) {
  			$parameters['sources'][]
  				= array(
  					'url' => $base_url.'birthday_'. $addressbook['id'],
  					'backgroundColor' => '#cccccc',
  					'borderColor' => '#888',
  					'textColor' => 'black',
  					'cache' => true,
  					'editable' => false,
  				);
  		}
  	}
  
  	static public function getBirthdayEvents($parameters) {
  		$name = $parameters['calendar_id'];
  		if (strpos($name, 'birthday_') != 0) {
  			return;
  		}
  		$info = explode('_', $name);
  		$aid = $info[1];
  		Addressbook::find($aid);
  		foreach(VCard::all($aid) as $contact) {
  			try {
  				$vcard = VObject\Reader::read($contact['carddata']);
  			} catch (Exception $e) {
  				continue;
  			}
  			$birthday = $vcard->BDAY;
  			if ((string)$birthday) {
  				$title = str_replace('{name}',
  					strtr((string)$vcard->FN, array('\,' => ',', '\;' => ';')),
  					App::$l10n->t('{name}\'s Birthday'));
  				
  				$date = new \DateTime($birthday);
  				$vevent = VObject\Component::create('VEVENT');
  				//$vevent->setDateTime('LAST-MODIFIED', new DateTime($vcard->REV));
  				$vevent->add('DTSTART');
  				$vevent->DTSTART->setDateTime($date,
  					VObject\Property\DateTime::DATE);
  				$vevent->add('DURATION', 'P1D');
  				$vevent->{'UID'} = substr(md5(rand().time()), 0, 10);
  				// DESCRIPTION?
  				$vevent->{'RRULE'} = 'FREQ=YEARLY';
  				$vevent->{'SUMMARY'} = $title;
  				$parameters['events'][] = array(
  					'id' => 0,//$card['id'],
  					'vevent' => $vevent,
  					'repeating' => true,
  					'summary' => $title,
  					'calendardata' => "BEGIN:VCALENDAR
  VERSION:2.0
  "
  						. "PRODID:ownCloud Contacts "
  						. \OCP\App::getAppVersion('contacts') . "
  "
  						. $vevent->serialize() .  "END:VCALENDAR"
  					);
  			}
  		}
  	}
  }