Skip to content
Snippets Groups Projects
Commit 25b95a9e authored by Thomas Tanghus's avatar Thomas Tanghus
Browse files

Improve checking for active addressbooks and creating default addressbook.

parent 554cb2d3
No related branches found
No related tags found
No related merge requests found
......@@ -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);
......
......@@ -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)) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment