Improve checking for active addressbooks and creating default addressbook.

This commit is contained in:
Thomas Tanghus 2012-07-08 15:41:38 +02:00
parent 554cb2d3ab
commit 25b95a9ec7
2 changed files with 29 additions and 10 deletions

View File

@ -169,16 +169,26 @@ class OC_Contacts_Addressbook{
$uid = OCP\USER::getUser(); $uid = OCP\USER::getUser();
} }
$prefbooks = OCP\Config::getUserValue($uid,'contacts','openaddressbooks',null); $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){ if(!$prefbooks){
OCP\Util::writeLog('contacts','OC_Contacts_Addressbook:activeIds:, No active addressbooks',OCP\Util::DEBUG);
$addressbooks = OC_Contacts_Addressbook::all($uid); $addressbooks = OC_Contacts_Addressbook::all($uid);
if(count($addressbooks) == 0){ 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); $addressbooks = OC_Contacts_Addressbook::all($uid);
} }
$prefbooks = $addressbooks[0]['id']; $prefbooks[] = $addressbooks[0]['id'];
OCP\Config::setUserValue($uid,'contacts','openaddressbooks',$prefbooks); 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){ public static function active($uid){
$active = self::activeIds($uid); $active = self::activeIds($uid);
$addressbooks = array(); $addressbooks = array();
if(!$active) {
return $addressbooks;
}
$ids_sql = join(',', array_fill(0, count($active), '?')); $ids_sql = join(',', array_fill(0, count($active), '?'));
$prep = 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id IN ('.$ids_sql.') ORDER BY displayname'; $prep = 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id IN ('.$ids_sql.') ORDER BY displayname';
try { try {
@ -213,7 +226,7 @@ class OC_Contacts_Addressbook{
* @param integer $name * @param integer $name
* @return boolean * @return boolean
*/ */
public static function setActive($id,$active){ public static function setActive($id,$active=true){
// Need these ones for checking uri // Need these ones for checking uri
//$addressbook = self::find($id); //$addressbook = self::find($id);

View File

@ -49,25 +49,31 @@ class OC_Contacts_VCard{
*/ */
public static function all($id){ public static function all($id){
$result = null; $result = null;
if(is_array($id)) { if(is_array($id) && count($id) > 1) {
$id_sql = join(',', array_fill(0, count($id), '?')); $id_sql = join(',', array_fill(0, count($id), '?'));
$prep = 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid IN ('.$id_sql.') ORDER BY fullname'; $prep = 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid IN ('.$id_sql.') ORDER BY fullname';
try { try {
$stmt = OCP\DB::prepare( $prep ); $stmt = OCP\DB::prepare( $prep );
$result = $stmt->execute($id); $result = $stmt->execute($id);
} catch(Exception $e) { } 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:, exception: '.$e->getMessage(),OCP\Util::ERROR);
OCP\Util::writeLog('contacts','OC_Contacts_VCard:all, ids: '.join(',', $id),OCP\Util::DEBUG); 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); OCP\Util::writeLog('contacts','SQL:'.$prep,OCP\Util::DEBUG);
} }
} elseif($id) { } elseif($id) {
if(is_array($id)) {
$id = $id[0];
}
try { try {
$stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? ORDER BY fullname' ); $stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_cards WHERE addressbookid = ? ORDER BY fullname' );
$result = $stmt->execute(array($id)); $result = $stmt->execute(array($id));
} catch(Exception $e) { } 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:, exception: '.$e->getMessage(),OCP\Util::ERROR);
OCP\Util::writeLog('contacts','OC_Contacts_VCard:all, ids: '. $id,OCP\Util::DEBUG); 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(); $cards = array();
if(!is_null($result)) { if(!is_null($result)) {