2011-12-05 20:51:25 +00:00
< ? php
/**
* Copyright ( c ) 2011 Bart Visscher bartv @ thisnet . nl
* This file is licensed under the Affero General Public License version 3 or
* later .
* See the COPYING - README file .
*/
/**
* This class manages our app actions
*/
OC_Contacts_App :: $l10n = new OC_L10N ( 'contacts' );
class OC_Contacts_App {
public static $l10n ;
/**
* Render templates / part . details to json output
* @ param int $id of contact
* @ param Sabre_VObject_Component $vcard to render
*/
public static function renderDetails ( $id , $vcard ){
2011-12-06 21:31:04 +00:00
$property_types = self :: getAddPropertyOptions ();
$adr_types = self :: getTypesOfProperty ( 'ADR' );
$phone_types = self :: getTypesOfProperty ( 'TEL' );
2011-12-05 20:51:25 +00:00
$details = OC_Contacts_VCard :: structureContact ( $vcard );
$name = $details [ 'FN' ][ 0 ][ 'value' ];
$tmpl = new OC_Template ( 'contacts' , 'part.details' );
$tmpl -> assign ( 'details' , $details );
$tmpl -> assign ( 'id' , $id );
$tmpl -> assign ( 'property_types' , $property_types );
$tmpl -> assign ( 'adr_types' , $adr_types );
$tmpl -> assign ( 'phone_types' , $phone_types );
$page = $tmpl -> fetchPage ();
OC_JSON :: success ( array ( 'data' => array ( 'id' => $id , 'name' => $name , 'page' => $page )));
}
2011-12-06 21:31:04 +00:00
public static function getAddressbook ( $id ){
$addressbook = OC_Contacts_Addressbook :: find ( $id );
2011-12-18 22:04:45 +00:00
if ( $addressbook === false || $addressbook [ 'userid' ] != OC_User :: getUser ()){
2011-12-06 21:31:04 +00:00
OC_JSON :: error ( array ( 'data' => array ( 'message' => self :: $l10n -> t ( 'This is not your addressbook.' )))); // Same here (as with the contact error). Could this error be improved?
exit ();
}
return $addressbook ;
}
public static function getContactObject ( $id ){
$card = OC_Contacts_VCard :: find ( $id );
if ( $card === false ){
OC_JSON :: error ( array ( 'data' => array ( 'message' => self :: $l10n -> t ( 'Contact could not be found.' ))));
exit ();
}
self :: getAddressbook ( $card [ 'addressbookid' ] );
return $card ;
}
public static function getContactVCard ( $id ){
$card = self :: getContactObject ( $id );
$vcard = OC_VObject :: parse ( $card [ 'carddata' ]);
// Check if the card is valid
if ( is_null ( $vcard )){
OC_JSON :: error ( array ( 'data' => array ( 'message' => self :: $l10n -> t ( 'vCard could not be read.' ))));
exit ();
}
return $vcard ;
}
public static function getPropertyLineByChecksum ( $vcard , $checksum ){
$line = null ;
for ( $i = 0 ; $i < count ( $vcard -> children ); $i ++ ){
if ( md5 ( $vcard -> children [ $i ] -> serialize ()) == $checksum ){
$line = $i ;
2012-01-11 02:56:53 +00:00
break ;
2011-12-06 21:31:04 +00:00
}
}
if ( is_null ( $line )){
OC_JSON :: error ( array ( 'data' => array ( 'message' => self :: $l10n -> t ( 'Information about vCard is incorrect. Please reload the page.' ))));
exit ();
}
return $line ;
}
2011-12-05 20:51:25 +00:00
/**
* @ return array of vcard prop => label
*/
2011-12-06 21:31:04 +00:00
public static function getAddPropertyOptions (){
$l10n = self :: $l10n ;
2011-12-05 20:51:25 +00:00
return array (
'ADR' => $l10n -> t ( 'Address' ),
'TEL' => $l10n -> t ( 'Telephone' ),
'EMAIL' => $l10n -> t ( 'Email' ),
'ORG' => $l10n -> t ( 'Organization' ),
);
}
/**
* @ return types for property $prop
*/
2011-12-06 21:31:04 +00:00
public static function getTypesOfProperty ( $prop ){
$l = self :: $l10n ;
2011-12-05 20:51:25 +00:00
switch ( $prop ){
case 'ADR' :
return array (
'WORK' => $l -> t ( 'Work' ),
'HOME' => $l -> t ( 'Home' ),
);
case 'TEL' :
return array (
'HOME' => $l -> t ( 'Home' ),
'CELL' => $l -> t ( 'Mobile' ),
'WORK' => $l -> t ( 'Work' ),
'TEXT' => $l -> t ( 'Text' ),
'VOICE' => $l -> t ( 'Voice' ),
'FAX' => $l -> t ( 'Fax' ),
'VIDEO' => $l -> t ( 'Video' ),
'PAGER' => $l -> t ( 'Pager' ),
);
}
}
}