Add support for multiple address books, various bug fixes
authorAndreas Boehler <andreas@aboehler.at>
Tue, 21 Feb 2017 20:47:08 +0100
changeset 18 ff3d73d2e467
parent 17 8c73076d5dcb
child 19 7ddcc8b498be
Add support for multiple address books, various bug fixes
action/ajax.php
helper.php
lang/en/lang.php
plugin.info.txt
script.js
syntax/book.php
syntax/card.php
--- a/action/ajax.php	Tue Dec 13 05:35:23 2016 +0100
+++ b/action/ajax.php	Tue Feb 21 20:47:08 2017 +0100
@@ -50,7 +50,7 @@
       $data['result'] = false;
       $data['html'] = $this->getLang('unknown_error');
       
-      $acl = auth_quickaclcheck($id);
+      $acl = $this->hlp->checkAddressbookPermission($id);
       if($acl > AUTH_READ)
       {
           $write = true;
--- a/helper.php	Tue Dec 13 05:35:23 2016 +0100
+++ b/helper.php	Tue Feb 21 20:47:08 2017 +0100
@@ -35,7 +35,7 @@
             msg('This plugin requires the sqlite plugin. Please install it.', -1);
             return false;
         }
-        if(!$this->sqlite->init('dvacard', DOKU_PLUGIN.'davcard/db/'))
+        if(!$this->sqlite->init('davcard', DOKU_PLUGIN.'davcard/db/'))
         {
             $this->sqlite = null;
             dbglog('Error initialising the SQLite DB for davcard');
@@ -826,5 +826,37 @@
       $res = $sqlite->query($query, $currentToken, $addressbookid);
       return ($res !== false);
   }
+  
+    /**
+   * Check the permission of a user for a given addressbook ID
+   * 
+   * @param string $id The addressbook ID to check
+   * @return int AUTH_* constants
+   */
+  public function checkAddressbookPermission($id)
+  {
+      if(strpos($id, 'webdav://') === 0)
+      {
+          $wdc =& plugin_load('helper', 'webdavclient');
+          if(is_null($wdc))
+            return AUTH_NONE;
+          $connectionId = str_replace('webdav://', '', $id);
+          $settings = $wdc->getConnection($connectionId);
+          if($settings === false)
+            return AUTH_NONE;
+          if($settings['write'] === '1')
+            return AUTH_CREATE;
+          return AUTH_READ;
+      }
+      else
+      {
+          $addr = $this->getAddressbookIdForPage($id);
+          // We return AUTH_READ if the calendar does not exist. This makes
+          // davcard happy when there are just included addressbooks
+          if($addr === false)
+            return AUTH_READ;
+          return auth_quickaclcheck($id);
+      }
+  }
  
 }
--- a/lang/en/lang.php	Tue Dec 13 05:35:23 2016 +0100
+++ b/lang/en/lang.php	Tue Feb 21 20:47:08 2017 +0100
@@ -15,7 +15,7 @@
 $lang['error_editing'] = 'Error editing contact';
 $lang['error_deleting'] = 'Error deleting contact';
 $lang['invalid_options'] = 'invalid options given';
-$lang['no_permission'] = 'You do not have permission to read this address book';
+$lang['no_permission'] = 'You do not have permission for this address book';
 $lang['telvoice'] = 'Voice';
 $lang['telhome'] = 'Home';
 $lang['telmsg'] = 'Message';
@@ -78,3 +78,5 @@
 $lang['js']['home'] = 'Home';
 $lang['js']['add_address'] = 'Add address';
 $lang['js']['other_address'] = 'Other Address';
+$lang['js']['addressbook'] = 'Addressbook';
+
--- a/plugin.info.txt	Tue Dec 13 05:35:23 2016 +0100
+++ b/plugin.info.txt	Tue Feb 21 20:47:08 2017 +0100
@@ -1,7 +1,7 @@
 base	davcard
 author  Andreas Boehler
 email   dev@aboehler.at
-date    2016-12-13
+date    2017-02-21
 name    Addressbook PlugIn with CardDAV client support
 desc    Show contact information from a CardDAV address book (needs webdavclient)
 url     http://www.dokuwiki.org/plugin:davcard
--- a/script.js	Tue Dec 13 05:35:23 2016 +0100
+++ b/script.js	Tue Feb 21 20:47:08 2017 +0100
@@ -136,6 +136,7 @@
                 var addrArray = new Array();
                 var mailArray = new Array();
                 var phoneArray = new Array();
+                var pageid = jQuery("#dw_davcard__addressbook option:selected").val();
 
                 jQuery("tr.dw_davcard__editentry_phone_row").each(function() {
                     var type = jQuery(this).children("td.dw_davcard__editentry_phone_row_type").data('type');
@@ -191,7 +192,7 @@
                     DOKU_BASE + 'lib/exe/ajax.php',
                     {
                         call: 'plugin_davcard',
-                        id: dw_davcard__modals.id,
+                        id: pageid,
                         page: dw_davcard__modals.page,
                         action: 'editContact',
                         params: postArray,
@@ -235,6 +236,7 @@
                 var addrArray = new Array();
                 var mailArray = new Array();
                 var phoneArray = new Array();
+                var pageid = jQuery("#dw_davcard__addressbook option:selected").val();
 
                 jQuery("tr.dw_davcard__editentry_phone_row").each(function() {
                     var type = jQuery(this).children("td.dw_davcard__editentry_phone_row_type").data('type');
@@ -290,7 +292,7 @@
                     DOKU_BASE + 'lib/exe/ajax.php',
                     {
                         call: 'plugin_davcard',
-                        id: dw_davcard__modals.id,
+                        id: pageid,
                         page: dw_davcard__modals.page,
                         action: 'newContact',
                         params: postArray,
@@ -336,7 +338,7 @@
        })
        .html(
             '<div><table>' + 
-            // FIXME: '<tr><td>' + LANG.plugins.davcard['calendar'] + '</td><td><select id="dw_davcal__editevent_calendar"></select></td></tr>' +
+            '<tr><td>' + LANG.plugins.davcard['addressbook'] + '</td><td><select id="dw_davcard__addressbook"></select></td></tr>' +
             '<tr><td>' + LANG.plugins.davcard['firstname'] + '</td><td><input type="text" id="dw_davcard__firstname_edit" name="firstname" class="dw_davcard__editcontact"></td></tr>' +
             '<tr><td>' + LANG.plugins.davcard['lastname'] + '</td><td><input type="text" id="dw_davcard__lastname_edit" name="lastname" class="dw_davcard__editcontact"></td></tr>' +
 
@@ -378,6 +380,24 @@
        $dropdown = jQuery('#dw_davcard__editentry_addresses_select');
        $dropdown.append('<option value="work">' + dw_davcard__modals.addressTypeToString('work') + '</option>');
        $dropdown.append('<option value="home">' + dw_davcard__modals.addressTypeToString('home') + '</option>');
+       
+       $dropdown = jQuery('#dw_davcard__addressbook');
+       jQuery('#davcardAddressbookList option').each(function() {
+           var sel = '';
+           if(edit && dw_davcard__modals.id == jQuery(this).val())
+               sel = ' selected="selected"';
+           if(edit || (!edit && (jQuery(this).data('write') == "1" || jQuery(this).data('write') == "true")))
+           {
+               $dropdown.append('<option value="' + jQuery(this).val() + '"' + sel + '>' + jQuery(this).text() + '</option>');
+           }
+       });
+       
+       if(edit)
+       {
+           $dropdown.prop('disabled', true);
+       }
+       
+       
         
        dw_davcard__modals.attachPhoneDeleteHandlers();
        dw_davcard__modals.attachMailDeleteHandlers();
--- a/syntax/book.php	Tue Dec 13 05:35:23 2016 +0100
+++ b/syntax/book.php	Tue Feb 21 20:47:08 2017 +0100
@@ -126,12 +126,10 @@
         global $ID;
         if($format !== 'xhtml')
             return false;
-        if(in_array($ID, $data['id']))
-        {        
-            $R->doc .= '<div class="davcardAddressbookAddNew"><a href="#" class="davcardAddressbookAddNew">'.$this->getLang('add_new').'</a></div>';
-        }        
-        // FIXME: Add new is not yet permission checked and does not support
-        // included address books!
+        
+        $addressbooklist = array();
+                
+        $R->doc .= '<div class="davcardAddressbookAddNew"><a href="#" class="davcardAddressbookAddNew">'.$this->getLang('add_new').'</a></div>';     
         
         $R->doc .= '<div id="davcardAddressbookList" data-addressbookpage="'.$ID.'">';
         $R->doc .= '<table class="davcardAddressbookTable">';
@@ -149,7 +147,6 @@
                 }
                 $connectionId = str_replace('webdav://', '', $id);
                 $settings = $wdc->getConnection($connectionId);
-        
                 if($settings === false)
                 {
                     echo $this->getLang('settings_not_found');
@@ -160,6 +157,7 @@
                     echo $this->getLang('wrong_type');
                     continue;
                 }
+                $name = $settings['displayname'];
                 $entries = $wdc->getAddressbookEntries($connectionId);
                 $write = $settings['write'];
             }
@@ -179,8 +177,13 @@
                     $write = false;
                 }
                 $addressbookid = $this->hlp->getAddressbookIdForPage($id);
+                $name = $this->hlp->getAddressBookSettings($addressbookid);
+                $name = $name['displayname'];
                 $entries = $this->hlp->getAddressbookEntries($addressbookid);
             }
+            
+            $addressbooklist[] = array('id' => $id, 'name' => $name, 'write' => $write);
+            
             foreach($entries as $entry)
             {
                 $contactdata = $this->hlp->parseVcard($entry['contactdata'], $entry['uri'], $write);
@@ -244,6 +247,13 @@
             }
         }
         $R->doc .= '</table>';
+        $R->doc .= '<div id="davcardAddressbookList" class="davcardAddressbookList" style="display:none">';
+        $R->doc .= '<select id="davcardAddressbookDropdown">';
+        foreach($addressbooklist as $addrbk)
+        {
+            $R->doc .= '<option value="'.$addrbk['id'].'" data-write="'.$addrbk['write'].'">'.$addrbk['name'].'</option>';
+        }
+        $R->doc .= '</select></div>';
         $R->doc .= '</div>';
         
     }
--- a/syntax/card.php	Tue Dec 13 05:35:23 2016 +0100
+++ b/syntax/card.php	Tue Feb 21 20:47:08 2017 +0100
@@ -90,14 +90,6 @@
                     $data[$key] = $val;
             }
         }
-        // FIXME: This is nonsense
-        if($data['id'] === '')
-        {
-            if(($data['name'] === '') || (($data['firstname'] === '') && ($data['lastname'] === '')))
-            {
-                msg($this->getLang('id_name_not_set'), -1);
-            }
-        }
         return $data;
     }