summaryrefslogtreecommitdiffstats
path: root/apps
diff options
context:
space:
mode:
authorThomas Tanghus <thomas@tanghus.net>2012-07-04 22:12:56 +0200
committerThomas Tanghus <thomas@tanghus.net>2012-07-04 22:13:36 +0200
commit706a13544ba1aa022b1642baac53d8070c268cc4 (patch)
tree17c346af0892d770872b178955ba7dd6fc513985 /apps
parent280c59e91482660fb9a6f3ccf3f4b6df97883f5c (diff)
downloadnextcloud-server-706a13544ba1aa022b1642baac53d8070c268cc4.tar.gz
nextcloud-server-706a13544ba1aa022b1642baac53d8070c268cc4.zip
Remove ugly activation hack.
Diffstat (limited to 'apps')
-rw-r--r--apps/contacts/appinfo/database.xml8
-rw-r--r--apps/contacts/appinfo/version2
-rw-r--r--apps/contacts/js/contacts.js19
-rw-r--r--apps/contacts/lib/addressbook.php157
4 files changed, 78 insertions, 108 deletions
diff --git a/apps/contacts/appinfo/database.xml b/apps/contacts/appinfo/database.xml
index 7c8268d71f5..dc469ca0a0c 100644
--- a/apps/contacts/appinfo/database.xml
+++ b/apps/contacts/appinfo/database.xml
@@ -62,6 +62,14 @@
<length>4</length>
</field>
+ <field>
+ <name>active</name>
+ <type>integer</type>
+ <default>0</default>
+ <notnull>true</notnull>
+ <length>4</length>
+ </field>
+
</declaration>
</table>
diff --git a/apps/contacts/appinfo/version b/apps/contacts/appinfo/version
index 2f4536184bc..7dff5b89211 100644
--- a/apps/contacts/appinfo/version
+++ b/apps/contacts/appinfo/version
@@ -1 +1 @@
-0.2 \ No newline at end of file
+0.2.1 \ No newline at end of file
diff --git a/apps/contacts/js/contacts.js b/apps/contacts/js/contacts.js
index 5be1fa61292..d4b3ef588ba 100644
--- a/apps/contacts/js/contacts.js
+++ b/apps/contacts/js/contacts.js
@@ -1359,13 +1359,18 @@ Contacts={
}
return false;
},
- activation:function(checkbox, bookid)
- {
- $.post(OC.filePath('contacts', 'ajax', 'activation.php'), { bookid: bookid, active: checkbox.checked?1:0 },
- function(data) {
- if (data.status == 'success'){
- checkbox.checked = data.active == 1;
- Contacts.UI.Contacts.update();
+ activation:function(checkbox, bookid){
+ var active = checkbox.checked;
+ $.post(OC.filePath('contacts', 'ajax', 'activation.php'), {bookid: bookid, active: (active?1:0)}, function(jsondata) {
+ if (jsondata.status == 'success'){
+ if(!active) {
+ $('#contacts h3[data-id="'+bookid+'"],#contacts ul[data-id="'+bookid+'"]').remove();
+ } else {
+ Contacts.UI.Contacts.update();
+ }
+ } else {
+ OC.dialogs.alert(jsondata.data.message, t('contacts', 'Error'));
+ checkbox.checked = !active;
}
});
},
diff --git a/apps/contacts/lib/addressbook.php b/apps/contacts/lib/addressbook.php
index 86a41b18cf3..f33f4a204e7 100644
--- a/apps/contacts/lib/addressbook.php
+++ b/apps/contacts/lib/addressbook.php
@@ -41,28 +41,63 @@ class OC_Contacts_Addressbook{
/**
* @brief Returns the list of addressbooks for a specific user.
* @param string $uid
+ * @param boolean $active Only return calendars with this $active state, default(=false) is don't care
* @return array or false.
*/
- public static function all($uid){
+ public static function all($uid, $active=false){
+ $values = array($uid);
+ $active_where = '';
+ if ($active){
+ $active_where = ' AND active = ?';
+ $values[] = 1;
+ }
try {
- $stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ? ORDER BY displayname' );
- $result = $stmt->execute(array($uid));
+ $stmt = OCP\DB::prepare( 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE userid = ? ' . $active_where . ' ORDER BY displayname' );
+ $result = $stmt->execute($values);
} catch(Exception $e) {
OCP\Util::writeLog('contacts',__CLASS__.'::'.__METHOD__.' exception: '.$e->getMessage(),OCP\Util::ERROR);
OCP\Util::writeLog('contacts',__CLASS__.'::'.__METHOD__.' uid: '.$uid,OCP\Util::DEBUG);
return false;
}
-
$addressbooks = array();
while( $row = $result->fetchRow()){
$addressbooks[] = $row;
}
+ if(!$active && !count($addressbooks)) {
+ self::addDefault($uid);
+ }
return $addressbooks;
}
/**
+ * @brief Get active addressbook IDs for a user.
+ * @param integer $uid User id. If null current user will be used.
+ * @return array
+ */
+ public static function activeIds($uid = null){
+ if(is_null($uid)){
+ $uid = OCP\USER::getUser();
+ }
+ $activeaddressbooks = self::all($uid, true);
+ $ids = array();
+ foreach($activeaddressbooks as $addressbook) {
+ $ids[] = $addressbook['userid'];
+ }
+ return $ids;
+ }
+
+ /**
+ * @brief Returns the list of active addressbooks for a specific user.
+ * @param string $uid
+ * @return array
+ */
+ public static function active($uid){
+ return self::all($uid, true);
+ }
+
+ /**
* @brief Returns the list of addressbooks for a principal (DAV term of user)
* @param string $principaluri
* @return array
@@ -186,77 +221,6 @@ class OC_Contacts_Addressbook{
return true;
}
- public static function cleanArray($array, $remove_null_number = true){
- $new_array = array();
-
- $null_exceptions = array();
-
- foreach ($array as $key => $value){
- $value = trim($value);
-
- if($remove_null_number){
- $null_exceptions[] = '0';
- }
-
- if(!in_array($value, $null_exceptions) && $value != "") {
- $new_array[] = $value;
- }
- }
- return $new_array;
- }
-
- /**
- * @brief Get active addressbooks for a user.
- * @param integer $uid User id. If null current user will be used.
- * @return array
- */
- public static function activeIds($uid = null){
- if(is_null($uid)){
- $uid = OCP\USER::getUser();
- }
- $prefbooks = OCP\Config::getUserValue($uid,'contacts','openaddressbooks',null);
- if(!$prefbooks){
- $addressbooks = OC_Contacts_Addressbook::all($uid);
- if(count($addressbooks) == 0){
- self::addDefault($uid);
- }
- }
- $prefbooks = OCP\Config::getUserValue($uid,'contacts','openaddressbooks',null);
- return explode(';',$prefbooks);
- }
-
- /**
- * @brief Returns the list of active addressbooks for a specific user.
- * @param string $uid
- * @return array
- */
- public static function active($uid){
- if(is_null($uid)){
- $uid = OCP\USER::getUser();
- }
- $active = self::activeIds($uid);
- $addressbooks = array();
- $ids_sql = join(',', array_fill(0, count($active), '?'));
- $prep = 'SELECT * FROM *PREFIX*contacts_addressbooks WHERE id IN ('.$ids_sql.') ORDER BY displayname';
- try {
- $stmt = OCP\DB::prepare( $prep );
- $result = $stmt->execute($active);
- } catch(Exception $e) {
- OCP\Util::writeLog('contacts',__CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(),OCP\Util::ERROR);
- OCP\Util::writeLog('contacts',__CLASS__.'::'.__METHOD__.', uid: '.$uid,OCP\Util::DEBUG);
- OCP\Util::writeLog('contacts',__CLASS__.'::'.__METHOD__.', ids: '.join(',', $active),OCP\Util::DEBUG);
- OCP\Util::writeLog('contacts',__CLASS__.'::'.__METHOD__.', SQL:'.$prep,OCP\Util::DEBUG);
- }
-
- while( $row = $result->fetchRow()){
- $addressbooks[] = $row;
- }
- if(!count($addressbooks)) {
- self::addDefault($uid);
- }
- return $addressbooks;
- }
-
/**
* @brief Activates an addressbook
* @param integer $id
@@ -264,30 +228,16 @@ class OC_Contacts_Addressbook{
* @return boolean
*/
public static function setActive($id,$active){
- // Need these ones for checking uri
- //$addressbook = self::find($id);
-
- if(is_null($id)){
- $id = 0;
- }
-
- $openaddressbooks = self::activeIds();
- if($active) {
- if(!in_array($id, $openaddressbooks)) {
- $openaddressbooks[] = $id;
- }
- } else {
- if(in_array($id, $openaddressbooks)) {
- unset($openaddressbooks[array_search($id, $openaddressbooks)]);
- }
+ $sql = 'UPDATE *PREFIX*contacts_addressbooks SET active = ? WHERE id = ?';
+ OCP\Util::writeLog('contacts',__CLASS__.'::'.__METHOD__.', id: '.$id.', active: '.intval($active),OCP\Util::ERROR);
+ try {
+ $stmt = OCP\DB::prepare($sql);
+ $stmt->execute(array(intval($active), $id));
+ return true;
+ } catch(Exception $e) {
+ OCP\Util::writeLog('contacts',__CLASS__.'::'.__METHOD__.', exception for '.$id.': '.$e->getMessage(),OCP\Util::ERROR);
+ return false;
}
- // NOTE: Ugly hack...
- $openaddressbooks = self::cleanArray($openaddressbooks, false);
- sort($openaddressbooks, SORT_NUMERIC);
- // FIXME: I alway end up with a ';' prepending when imploding the array..?
- OCP\Config::setUserValue(OCP\USER::getUser(),'contacts','openaddressbooks',implode(';', $openaddressbooks));
-
- return true;
}
/**
@@ -296,8 +246,15 @@ class OC_Contacts_Addressbook{
* @return boolean
*/
public static function isActive($id){
- //OCP\Util::writeLog('contacts','OC_Contacts_Addressbook::isActive('.$id.'):'.in_array($id, self::activeIds()), OCP\Util::DEBUG);
- return in_array($id, self::activeIds());
+ $sql = 'SELECT active FROM *PREFIX*contacts_addressbooks WHERE id = ?';
+ try {
+ $stmt = OCP\DB::prepare( $sql );
+ $result = $stmt->execute(array($id));
+ $row = $result->fetchRow();
+ return (bool)$row['active'];
+ } catch(Exception $e) {
+ OCP\Util::writeLog('contacts',__CLASS__.'::'.__METHOD__.', exception: '.$e->getMessage(),OCP\Util::ERROR);
+ }
}
/**