Contacts: refactor common ajax functions to OC_Contacts_App

This commit is contained in:
Bart Visscher 2011-12-06 22:31:04 +01:00
parent 247146f703
commit 7e990a8e5c
12 changed files with 84 additions and 195 deletions

View File

@ -28,11 +28,7 @@ OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$aid = $_POST['id']; $aid = $_POST['id'];
$addressbook = OC_Contacts_Addressbook::find( $aid ); $addressbook = OC_Contacts_App::getAddressbook( $aid );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
OC_JSON::error(array('data' => array( 'message' => OC_Contacts_App::$l10n->t('This is not your addressbook.')))); // Same here (as with the contact error). Could this error be improved?
exit();
}
$fn = $_POST['fn']; $fn = $_POST['fn'];
$values = $_POST['value']; $values = $_POST['value'];

View File

@ -23,31 +23,12 @@
// Init owncloud // Init owncloud
require_once('../../../lib/base.php'); require_once('../../../lib/base.php');
$id = $_POST['id'];
$l10n = new OC_L10N('contacts');
// Check if we are a user // Check if we are a user
OC_JSON::checkLoggedIn(); OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$card = OC_Contacts_VCard::find( $id ); $id = $_POST['id'];
if( $card === false ){ $vcard = OC_Contacts_App::getContactVCard( $id );
OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
exit();
}
$vcard = OC_VObject::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
exit();
}
$name = $_POST['name']; $name = $_POST['name'];
$value = $_POST['value']; $value = $_POST['value'];
@ -56,7 +37,6 @@ $parameters = isset($_POST['parameteres'])?$_POST['parameters']:array();
$property = $vcard->addProperty($name, $value, $parameters); $property = $vcard->addProperty($name, $value, $parameters);
$line = count($vcard->children) - 1; $line = count($vcard->children) - 1;
$checksum = md5($property->serialize());
OC_Contacts_VCard::edit($id,$vcard->serialize()); OC_Contacts_VCard::edit($id,$vcard->serialize());

View File

@ -23,19 +23,12 @@
// Init owncloud // Init owncloud
require_once('../../../lib/base.php'); require_once('../../../lib/base.php');
$id = $_GET['id'];
$l10n = new OC_L10N('contacts');
// Check if we are a user // Check if we are a user
OC_JSON::checkLoggedIn(); OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$addressbook = OC_Contacts_Addressbook::find( $id ); $id = $_GET['id'];
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){ $addressbook = OC_Contacts_App::getAddressbook( $id );
OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
exit();
}
OC_Contacts_Addressbook::delete($id); OC_Contacts_Addressbook::delete($id);
OC_JSON::success(array('data' => array( 'id' => $id ))); OC_JSON::success(array('data' => array( 'id' => $id )));

View File

@ -23,25 +23,12 @@
// Init owncloud // Init owncloud
require_once('../../../lib/base.php'); require_once('../../../lib/base.php');
$id = $_GET['id'];
$l10n = new OC_L10N('contacts');
// Check if we are a user // Check if we are a user
OC_JSON::checkLoggedIn(); OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$card = OC_Contacts_VCard::find( $id ); $id = $_GET['id'];
if( $card === false ){ $card = OC_Contacts_App::getContactObject( $id );
OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
exit();
}
OC_Contacts_VCard::delete($id); OC_Contacts_VCard::delete($id);
OC_JSON::success(array('data' => array( 'id' => $id ))); OC_JSON::success(array('data' => array( 'id' => $id )));

View File

@ -23,45 +23,15 @@
// Init owncloud // Init owncloud
require_once('../../../lib/base.php'); require_once('../../../lib/base.php');
$id = $_GET['id'];
$checksum = $_GET['checksum'];
$l10n = new OC_L10N('contacts');
// Check if we are a user // Check if we are a user
OC_JSON::checkLoggedIn(); OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$card = OC_Contacts_VCard::find( $id ); $id = $_GET['id'];
if( $card === false ){ $checksum = $_GET['checksum'];
OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] ); $vcard = OC_Contacts_App::getContactVCard( $id );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){ $line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
exit();
}
$vcard = OC_VObject::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
exit();
}
$line = null;
for($i=0;$i<count($vcard->children);$i++){
if(md5($vcard->children[$i]->serialize()) == $checksum ){
$line = $i;
}
}
if(is_null($line)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
exit();
}
unset($vcard->children[$line]); unset($vcard->children[$line]);

View File

@ -28,23 +28,6 @@ OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$id = $_GET['id']; $id = $_GET['id'];
$card = OC_Contacts_VCard::find( $id ); $vcard = OC_Contacts_App::getContactVCard( $id );
if( $card === false ){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
exit();
}
$vcard = OC_VObject::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
exit();
}
OC_Contacts_App::renderDetails($id, $vcard); OC_Contacts_App::renderDetails($id, $vcard);

View File

@ -23,43 +23,15 @@
// Init owncloud // Init owncloud
require_once('../../../lib/base.php'); require_once('../../../lib/base.php');
$id = $_POST['id'];
$checksum = $_POST['checksum'];
$l10n = new OC_L10N('contacts');
// Check if we are a user // Check if we are a user
OC_JSON::checkLoggedIn(); OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$card = OC_Contacts_VCard::find( $id ); $id = $_POST['id'];
if( $card === false ){ $checksum = $_POST['checksum'];
OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] ); $vcard = OC_Contacts_App::getContactVCard( $id );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){ $line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
exit();
}
$vcard = OC_VObject::parse($card['carddata']);
// Check if the card is valid
if(is_null($vcard)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
exit();
}
$line = null;
for($i=0;$i<count($vcard->children);$i++){
if(md5($vcard->children[$i]->serialize()) == $checksum ){
$line = $i;
}
}
if(is_null($line)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
exit();
}
// Set the value // Set the value
$value = $_POST['value']; $value = $_POST['value'];
@ -104,8 +76,8 @@ $checksum = md5($vcard->children[$line]->serialize());
OC_Contacts_VCard::edit($id,$vcard->serialize()); OC_Contacts_VCard::edit($id,$vcard->serialize());
$adr_types = OC_Contacts_App::getTypesOfProperty($l10n, 'ADR'); $adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
$phone_types = OC_Contacts_App::getTypesOfProperty($l10n, 'TEL'); $phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
if ($vcard->children[$line]->name == 'FN'){ if ($vcard->children[$line]->name == 'FN'){
$tmpl = new OC_Template('contacts','part.property.FN'); $tmpl = new OC_Template('contacts','part.property.FN');

View File

@ -27,8 +27,8 @@ require_once('../../../lib/base.php');
OC_JSON::checkLoggedIn(); OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$adr_types = OC_Contacts_App::getTypesOfProperty(OC_Contacts_App::$l10n, 'ADR'); $adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
$phone_types = OC_Contacts_App::getTypesOfProperty(OC_Contacts_App::$l10n, 'TEL'); $phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
$addressbooks = OC_Contacts_Addressbook::all(OC_USER::getUser()); $addressbooks = OC_Contacts_Addressbook::all(OC_USER::getUser());
$tmpl = new OC_Template('contacts','part.addcardform'); $tmpl = new OC_Template('contacts','part.addcardform');

View File

@ -23,24 +23,12 @@
// Init owncloud // Init owncloud
require_once('../../../lib/base.php'); require_once('../../../lib/base.php');
$id = $_GET['id'];
$l10n = new OC_L10N('contacts');
// Check if we are a user // Check if we are a user
OC_JSON::checkLoggedIn(); OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$card = OC_Contacts_VCard::find( $id ); $id = $_GET['id'];
if( $card === false ){ $card = OC_Contacts_App::getContactObject( $id );
OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
exit();
}
$tmpl = new OC_Template('contacts','part.addpropertyform'); $tmpl = new OC_Template('contacts','part.addpropertyform');
$tmpl->assign('id',$id); $tmpl->assign('id',$id);

View File

@ -23,46 +23,19 @@
// Init owncloud // Init owncloud
require_once('../../../lib/base.php'); require_once('../../../lib/base.php');
$id = $_GET['id'];
$checksum = $_GET['checksum'];
$l10n = new OC_L10N('contacts');
// Check if we are a user // Check if we are a user
OC_JSON::checkLoggedIn(); OC_JSON::checkLoggedIn();
OC_JSON::checkAppEnabled('contacts'); OC_JSON::checkAppEnabled('contacts');
$card = OC_Contacts_VCard::find( $id ); $id = $_GET['id'];
if( $card === false ){ $checksum = $_GET['checksum'];
OC_JSON::error(array('data' => array( 'message' => $l10n->t('Contact could not be found.'))));
exit();
}
$addressbook = OC_Contacts_Addressbook::find( $card['addressbookid'] ); $vcard = OC_Contacts_App::getContactVCard( $id );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
exit();
}
$vcard = OC_VObject::parse($card['carddata']); $line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
// Check if the card is valid
if(is_null($vcard)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('vCard could not be read.'))));
exit();
}
$line = null; $adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
for($i=0;$i<count($vcard->children);$i++){ $phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
if(md5($vcard->children[$i]->serialize()) == $checksum ){
$line = $i;
}
}
if(is_null($line)){
OC_JSON::error(array('data' => array( 'message' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
exit();
}
$adr_types = OC_Contacts_App::getTypesOfProperty($l10n, 'ADR');
$phone_types = OC_Contacts_App::getTypesOfProperty($l10n, 'TEL');
$tmpl = new OC_Template('contacts','part.setpropertyform'); $tmpl = new OC_Template('contacts','part.setpropertyform');
$tmpl->assign('id',$id); $tmpl->assign('id',$id);

View File

@ -70,13 +70,12 @@ $details = array();
if( !is_null($id) || count($contacts)){ if( !is_null($id) || count($contacts)){
if(is_null($id)) $id = $contacts[0]['id']; if(is_null($id)) $id = $contacts[0]['id'];
$contact = OC_Contacts_VCard::find($id); $vcard = OC_Contacts_App::getContactVCard($id);
$vcard = OC_VObject::parse($contact['carddata']);
$details = OC_Contacts_VCard::structureContact($vcard); $details = OC_Contacts_VCard::structureContact($vcard);
} }
$adr_types = OC_Contacts_App::getTypesOfProperty(OC_Contacts_App::$l10n, 'ADR'); $adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
$phone_types = OC_Contacts_App::getTypesOfProperty(OC_Contacts_App::$l10n, 'TEL'); $phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
// Process the template // Process the template
$tmpl = new OC_Template( 'contacts', 'index', 'user' ); $tmpl = new OC_Template( 'contacts', 'index', 'user' );

View File

@ -19,9 +19,9 @@ class OC_Contacts_App{
* @param Sabre_VObject_Component $vcard to render * @param Sabre_VObject_Component $vcard to render
*/ */
public static function renderDetails($id, $vcard){ public static function renderDetails($id, $vcard){
$property_types = self::getAddPropertyOptions(self::$l10n); $property_types = self::getAddPropertyOptions();
$adr_types = self::getTypesOfProperty(self::$l10n, 'ADR'); $adr_types = self::getTypesOfProperty('ADR');
$phone_types = self::getTypesOfProperty(self::$l10n, 'TEL'); $phone_types = self::getTypesOfProperty('TEL');
$details = OC_Contacts_VCard::structureContact($vcard); $details = OC_Contacts_VCard::structureContact($vcard);
$name = $details['FN'][0]['value']; $name = $details['FN'][0]['value'];
@ -36,10 +36,57 @@ class OC_Contacts_App{
OC_JSON::success(array('data' => array( 'id' => $id, 'name' => $name, 'page' => $page ))); OC_JSON::success(array('data' => array( 'id' => $id, 'name' => $name, 'page' => $page )));
} }
public static function getAddressbook($id){
$addressbook = OC_Contacts_Addressbook::find( $id );
if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
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;
}
}
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;
}
/** /**
* @return array of vcard prop => label * @return array of vcard prop => label
*/ */
public static function getAddPropertyOptions($l10n){ public static function getAddPropertyOptions(){
$l10n = self::$l10n;
return array( return array(
'ADR' => $l10n->t('Address'), 'ADR' => $l10n->t('Address'),
'TEL' => $l10n->t('Telephone'), 'TEL' => $l10n->t('Telephone'),
@ -51,7 +98,8 @@ class OC_Contacts_App{
/** /**
* @return types for property $prop * @return types for property $prop
*/ */
public static function getTypesOfProperty($l, $prop){ public static function getTypesOfProperty($prop){
$l = self::$l10n;
switch($prop){ switch($prop){
case 'ADR': case 'ADR':
return array( return array(