]> source.dussan.org Git - nextcloud-server.git/commitdiff
Contacts: refactor common ajax functions to OC_Contacts_App
authorBart Visscher <bartv@thisnet.nl>
Tue, 6 Dec 2011 21:31:04 +0000 (22:31 +0100)
committerBart Visscher <bartv@thisnet.nl>
Wed, 7 Dec 2011 19:40:59 +0000 (20:40 +0100)
12 files changed:
apps/contacts/ajax/addcard.php
apps/contacts/ajax/addproperty.php
apps/contacts/ajax/deletebook.php
apps/contacts/ajax/deletecard.php
apps/contacts/ajax/deleteproperty.php
apps/contacts/ajax/getdetails.php
apps/contacts/ajax/setproperty.php
apps/contacts/ajax/showaddcard.php
apps/contacts/ajax/showaddproperty.php
apps/contacts/ajax/showsetproperty.php
apps/contacts/index.php
apps/contacts/lib/app.php

index 9a52b26ed596f928dd6f6950ba79cb8a8eb0d356..9d782246a0acec2fbcb657e94493be959a136827 100644 (file)
@@ -28,11 +28,7 @@ OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
 $aid = $_POST['id'];
-$addressbook = OC_Contacts_Addressbook::find( $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();
-}
+$addressbook = OC_Contacts_App::getAddressbook( $aid );
 
 $fn = $_POST['fn'];
 $values = $_POST['value'];
index e59a91e4a58fdbf1c68170986cc3edb920338e64..1ca4a85515baee5702542eea95a2fc74b6bb428b 100644 (file)
 // Init owncloud
 require_once('../../../lib/base.php');
 
-$id = $_POST['id'];
-$l10n = new OC_L10N('contacts');
-
 // Check if we are a user
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
-$card = OC_Contacts_VCard::find( $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();
-}
+$id = $_POST['id'];
+$vcard = OC_Contacts_App::getContactVCard( $id );
 
 $name = $_POST['name'];
 $value = $_POST['value'];
@@ -56,7 +37,6 @@ $parameters = isset($_POST['parameteres'])?$_POST['parameters']:array();
 $property = $vcard->addProperty($name, $value, $parameters);
 
 $line = count($vcard->children) - 1;
-$checksum = md5($property->serialize());
 
 OC_Contacts_VCard::edit($id,$vcard->serialize());
 
index c13217ef2e22e6aa719cde9037c6819de735db65..3ede17ab88647880b717ee294d1dee245a6b95c5 100644 (file)
 // Init owncloud
 require_once('../../../lib/base.php');
 
-$id = $_GET['id'];
-
-$l10n = new OC_L10N('contacts');
-
 // Check if we are a user
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
-$addressbook = OC_Contacts_Addressbook::find( $id );
-if( $addressbook === false || $addressbook['userid'] != OC_USER::getUser()){
-       OC_JSON::error(array('data' => array( 'message' => $l10n->t('This is not your contact.'))));
-       exit();
-}
+$id = $_GET['id'];
+$addressbook = OC_Contacts_App::getAddressbook( $id );
 
 OC_Contacts_Addressbook::delete($id);
 OC_JSON::success(array('data' => array( 'id' => $id )));
index a0a6b8c3ea8d352dc313d32162eb1bb1d88b7da1..e26dfd6ebfebc20fc2f498b029cb37803cc59284 100644 (file)
 // Init owncloud
 require_once('../../../lib/base.php');
 
-$id = $_GET['id'];
-
-$l10n = new OC_L10N('contacts');
-
 // Check if we are a user
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
-$card = OC_Contacts_VCard::find( $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();
-}
+$id = $_GET['id'];
+$card = OC_Contacts_App::getContactObject( $id );
 
 OC_Contacts_VCard::delete($id);
 OC_JSON::success(array('data' => array( 'id' => $id )));
index be352cea616b236bad4f4449c99c848119aa8a54..f69735e61c69459a17d86058a8e0eb72c66701c0 100644 (file)
 // Init owncloud
 require_once('../../../lib/base.php');
 
-$id = $_GET['id'];
-$checksum = $_GET['checksum'];
-
-
-$l10n = new OC_L10N('contacts');
-
 // Check if we are a user
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
-$card = OC_Contacts_VCard::find( $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();
-}
+$id = $_GET['id'];
+$checksum = $_GET['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' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
-       exit();
-}
+$vcard = OC_Contacts_App::getContactVCard( $id );
+$line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
 
 unset($vcard->children[$line]);
 
index e480bce4cb49044c1d4c14f937ed245bb3e6cb70..8cc0f9cbb0fe2f1e91eb8af6a6a94a713fe7b6ab 100644 (file)
@@ -28,23 +28,6 @@ OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
 $id = $_GET['id'];
-$card = OC_Contacts_VCard::find( $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();
-}
+$vcard = OC_Contacts_App::getContactVCard( $id );
 
 OC_Contacts_App::renderDetails($id, $vcard);
index e636cc5c1bdf4ddf1d1fc88212e4f4c1a6b210a5..bcc4c161cc04c0e1af30e67cd5bcf5f1ba9991af 100644 (file)
 // Init owncloud
 require_once('../../../lib/base.php');
 
-$id = $_POST['id'];
-$checksum = $_POST['checksum'];
-$l10n = new OC_L10N('contacts');
-
 // Check if we are a user
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
-$card = OC_Contacts_VCard::find( $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();
-}
+$id = $_POST['id'];
+$checksum = $_POST['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' => $l10n->t('Information about vCard is incorrect. Please reload the page.'))));
-       exit();
-}
+$vcard = OC_Contacts_App::getContactVCard( $id );
+$line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
 
 // Set the value
 $value = $_POST['value'];
@@ -104,8 +76,8 @@ $checksum = md5($vcard->children[$line]->serialize());
 
 OC_Contacts_VCard::edit($id,$vcard->serialize());
 
-$adr_types = OC_Contacts_App::getTypesOfProperty($l10n, 'ADR');
-$phone_types = OC_Contacts_App::getTypesOfProperty($l10n, 'TEL');
+$adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
+$phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
 
 if ($vcard->children[$line]->name == 'FN'){
        $tmpl = new OC_Template('contacts','part.property.FN');
index a2a9398bde51b74b2b32aa1bae2f6263a636bada..54592c89c0d835d0212488d75642fbb4832ef030 100644 (file)
@@ -27,8 +27,8 @@ require_once('../../../lib/base.php');
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
-$adr_types = OC_Contacts_App::getTypesOfProperty(OC_Contacts_App::$l10n, 'ADR');
-$phone_types = OC_Contacts_App::getTypesOfProperty(OC_Contacts_App::$l10n, 'TEL');
+$adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
+$phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
 
 $addressbooks = OC_Contacts_Addressbook::all(OC_USER::getUser());
 $tmpl = new OC_Template('contacts','part.addcardform');
index f87cd05359bdd8aca03a17b4fb106e2461f63cdb..30eb7634f8041076f3f1d0fcbe88b7010e2df897 100644 (file)
 // Init owncloud
 require_once('../../../lib/base.php');
 
-$id = $_GET['id'];
-$l10n = new OC_L10N('contacts');
-
 // Check if we are a user
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
-$card = OC_Contacts_VCard::find( $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();
-}
+$id = $_GET['id'];
+$card = OC_Contacts_App::getContactObject( $id );
 
 $tmpl = new OC_Template('contacts','part.addpropertyform');
 $tmpl->assign('id',$id);
index 30d0f5d817c61e19b5b262c2f7c248ad35a64621..e23fa21c56ba32812d32c948a64e89d421c00492 100644 (file)
 // Init owncloud
 require_once('../../../lib/base.php');
 
-$id = $_GET['id'];
-$checksum = $_GET['checksum'];
-$l10n = new OC_L10N('contacts');
-
 // Check if we are a user
 OC_JSON::checkLoggedIn();
 OC_JSON::checkAppEnabled('contacts');
 
-$card = OC_Contacts_VCard::find( $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();
-}
+$id = $_GET['id'];
+$checksum = $_GET['checksum'];
 
-$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();
-}
+$vcard = OC_Contacts_App::getContactVCard( $id );
 
-$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();
-}
+$line = OC_Contacts_App::getPropertyLineByChecksum($vcard, $checksum);
 
-$adr_types = OC_Contacts_App::getTypesOfProperty($l10n, 'ADR');
-$phone_types = OC_Contacts_App::getTypesOfProperty($l10n, 'TEL');
+$adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
+$phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
 
 $tmpl = new OC_Template('contacts','part.setpropertyform');
 $tmpl->assign('id',$id);
index 0a847f5a672fd7e36f9949e4851c682809bf7894..e5098bb7a8bc4f3c8cc2e5275663667e742ce50b 100644 (file)
@@ -70,13 +70,12 @@ $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']);
+       $vcard = OC_Contacts_App::getContactVCard($id);
        $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');
+$adr_types = OC_Contacts_App::getTypesOfProperty('ADR');
+$phone_types = OC_Contacts_App::getTypesOfProperty('TEL');
 
 // Process the template
 $tmpl = new OC_Template( 'contacts', 'index', 'user' );
index 327ee6f1ca2cefc68515d8ed1fc72fa522aabf03..ba086e4aca8902e1517edb77e40806de2be05876 100644 (file)
@@ -19,9 +19,9 @@ class OC_Contacts_App{
        * @param Sabre_VObject_Component $vcard to render
        */
        public static function renderDetails($id, $vcard){
-               $property_types = self::getAddPropertyOptions(self::$l10n);
-               $adr_types = self::getTypesOfProperty(self::$l10n, 'ADR');
-               $phone_types = self::getTypesOfProperty(self::$l10n, 'TEL');
+               $property_types = self::getAddPropertyOptions();
+               $adr_types = self::getTypesOfProperty('ADR');
+               $phone_types = self::getTypesOfProperty('TEL');
 
                $details = OC_Contacts_VCard::structureContact($vcard);
                $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 )));
        }
 
+       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
         */
-       public static function getAddPropertyOptions($l10n){
+       public static function getAddPropertyOptions(){
+               $l10n = self::$l10n;
                return array(
                                'ADR'   => $l10n->t('Address'),
                                'TEL'   => $l10n->t('Telephone'),
@@ -51,7 +98,8 @@ class OC_Contacts_App{
        /**
         * @return types for property $prop
         */
-       public static function getTypesOfProperty($l, $prop){
+       public static function getTypesOfProperty($prop){
+               $l = self::$l10n;
                switch($prop){
                case 'ADR':
                        return array(