]> source.dussan.org Git - nextcloud-server.git/commitdiff
Improve checking for active addressbooks and creating default addressbook.
authorThomas Tanghus <thomas@tanghus.net>
Sun, 8 Jul 2012 13:41:38 +0000 (15:41 +0200)
committerThomas Tanghus <thomas@tanghus.net>
Sun, 8 Jul 2012 13:41:38 +0000 (15:41 +0200)
apps/contacts/lib/addressbook.php
apps/contacts/lib/vcard.php

index 79445ceeee1dabcc6f59d77002181e3d70ee8041..73b30e942fa257c14b435443f2676ef06e352107 100644 (file)
@@ -169,16 +169,26 @@ class OC_Contacts_Addressbook{
                        $uid = OCP\USER::getUser();
                }
                $prefbooks = OCP\Config::getUserValue($uid,'contacts','openaddressbooks',null);
+               $prefbooks = explode(';',$prefbooks);
+               for ($i = 0; $i < count($prefbooks); $i++) {
+                       if(!self::find($prefbooks[$i])) {
+                               unset($prefbooks[$i]);
+                       }
+               }
                if(!$prefbooks){
+                       OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:activeIds:, No active addressbooks',OCP\Util::DEBUG);
                        $addressbooks = OC_Contacts_Addressbook::all($uid);
                        if(count($addressbooks) == 0){
-                               OC_Contacts_Addressbook::add($uid,'default','Default Address Book');
+                               OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:activeIds:, No addressbooks',OCP\Util::DEBUG);
+                               $id = self::add($uid,'default','Default Address Book');
+                               OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:activeIds:, Created addressbook: '.$id,OCP\Util::DEBUG);
+                               self::setActive($id, true);
                                $addressbooks = OC_Contacts_Addressbook::all($uid);
                        }
-                       $prefbooks = $addressbooks[0]['id'];
-                       OCP\Config::setUserValue($uid,'contacts','openaddressbooks',$prefbooks);
+                       $prefbooks[] = $addressbooks[0]['id'];
+                       OCP\Config::setUserValue($uid,'contacts','openaddressbooks',implode(';',$prefbooks));
                }
-               return explode(';',$prefbooks);
+               return $prefbooks;
        }
 
        /**
@@ -189,6 +199,9 @@ class OC_Contacts_Addressbook{
        public static function active($uid){
                $active = self::activeIds($uid);
                $addressbooks = array();
+               if(!$active) {
+                       return $addressbooks;
+               }
                $ids_sql = join(',', array_fill(0, count($active), '?'));
                $prep = 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id IN ('.$ids_sql.') ORDER BY displayname';
                try {
@@ -213,7 +226,7 @@ class OC_Contacts_Addressbook{
         * @param integer $name
         * @return boolean
         */
-       public static function setActive($id,$active){
+       public static function setActive($id,$active=true){
                // Need these ones for checking uri
                //$addressbook = self::find($id);
 
index 71a874d783b5ba46b7fa9b361ec79e9923f8b3cb..2868643387d7c9a9934a52c53916b30565a81ac9 100644 (file)
@@ -49,25 +49,31 @@ class OC_Contacts_VCard{
         */
        public static function all($id){
                $result = null;
-               if(is_array($id)) {
+               if(is_array($id) && count($id) > 1) {
                        $id_sql = join(',', array_fill(0, count($id), '?'));
                        $prep = 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid IN ('.$id_sql.') ORDER BY fullname';
                        try {
                                $stmt = OCP\DB::prepare( $prep );
                                $result = $stmt->execute($id);
                        } catch(Exception $e) {
-                               OCP\Util::writeLog('contacts','OC_Contacts_VCard:all:, exception: '.$e->getMessage(),OCP\Util::DEBUG);
-                               OCP\Util::writeLog('contacts','OC_Contacts_VCard:all, ids: '.join(',', $id),OCP\Util::DEBUG);
+                               OCP\Util::writeLog('contacts','OC_Contacts_VCard:all:, exception: '.$e->getMessage(),OCP\Util::ERROR);
+                               OCP\Util::writeLog('contacts','OC_Contacts_VCard:all, ids: '.count($id).' '.join(',', $id),OCP\Util::DEBUG);
                                OCP\Util::writeLog('contacts','SQL:'.$prep,OCP\Util::DEBUG);
                        }
                } elseif($id) {
+                       if(is_array($id)) {
+                               $id = $id[0];
+                       }
                        try {
                                $stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? ORDER BY fullname' );
                                $result = $stmt->execute(array($id));
                        } catch(Exception $e) {
-                               OCP\Util::writeLog('contacts','OC_Contacts_VCard:all:, exception: '.$e->getMessage(),OCP\Util::DEBUG);
-                               OCP\Util::writeLog('contacts','OC_Contacts_VCard:all, ids: '. $id,OCP\Util::DEBUG);
+                               OCP\Util::writeLog('contacts','OC_Contacts_VCard:all:, exception: '.$e->getMessage(),OCP\Util::ERROR);
+                               OCP\Util::writeLog('contacts','OC_Contacts_VCard:all, id: '. $id,OCP\Util::DEBUG);
                        }
+               } else {
+                       OCP\Util::writeLog('contacts','OC_Contacts_VCard:all: No ID given.',OCP\Util::ERROR);
+                       return array();
                }
                $cards = array();
                if(!is_null($result)) {