From ef9511c713980faecc65b898b15a50163218cbaa Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Sat, 5 May 2012 14:56:52 -0400 Subject: [PATCH] Refactor sharing UI; bug fixes, code clean-up, and more efficient with less ajax calls --- apps/files_sharing/ajax/getitem.php | 87 ++- apps/files_sharing/ajax/getstatuses.php | 25 + apps/files_sharing/ajax/setpermissions.php | 15 +- apps/files_sharing/ajax/share.php | 44 +- apps/files_sharing/ajax/unshare.php | 13 +- apps/files_sharing/ajax/userautocomplete.php | 10 +- apps/files_sharing/css/sharing.css | 5 +- apps/files_sharing/js/share.js | 530 +++++++++++-------- apps/files_sharing/lib_share.php | 8 +- 9 files changed, 449 insertions(+), 288 deletions(-) create mode 100644 apps/files_sharing/ajax/getstatuses.php diff --git a/apps/files_sharing/ajax/getitem.php b/apps/files_sharing/ajax/getitem.php index d9404f7e3b..6eed040823 100755 --- a/apps/files_sharing/ajax/getitem.php +++ b/apps/files_sharing/ajax/getitem.php @@ -1,36 +1,69 @@ $gid, 'permissions' => $rows[$i]['permissions'], 'users' => OC_Group::usersInGroup($gid), 'parentFolder' => false)); + } else { + $group = array(array('gid' => $gid, 'permissions' => $rows[$i]['permissions'], 'users' => OC_Group::usersInGroup($gid), 'parentFolder' => basename($path))); + } + if (!isset($item['groups'])) { + $item['groups'] = $group; + } else if (is_array($item['groups'])) { + $gidExists = false; + $currentGroups = $item['groups']; + // Check if the group is already included + foreach ($currentGroups as $g) { + if ($g['gid'] == $gid) { + $gidExists = true; + } + } + if (!$gidExists) { + $item['groups'] = array_merge($item['groups'], $group); + } + } + } else { + if ($path == $source) { + $user = array(array('uid' => $uid_shared_with, 'permissions' => $rows[$i]['permissions'], 'parentFolder' => false)); + } else { + $user = array(array('uid' => $uid_shared_with, 'permissions' => $rows[$i]['permissions'], 'parentFolder' => basename($path))); + } + if (!isset($item['users'])) { + $item['users'] = $user; + } else if (is_array($item['users'])) { + $item['users'] = array_merge($item['users'], $user); + } + } } - $parentUsers[basename($source)."-".$i] = $values[$i]; } - $users = array_merge($users, $parentUsers); } - $source = dirname($source); -} -if (!empty($users)) { - OCP\JSON::encodedPrint($users); + $path = dirname($path); } + +OCP\JSON::success(array('data' => $item)); + +?> diff --git a/apps/files_sharing/ajax/getstatuses.php b/apps/files_sharing/ajax/getstatuses.php new file mode 100644 index 0000000000..c1892e7e2a --- /dev/null +++ b/apps/files_sharing/ajax/getstatuses.php @@ -0,0 +1,25 @@ + $items)); + +?> \ No newline at end of file diff --git a/apps/files_sharing/ajax/setpermissions.php b/apps/files_sharing/ajax/setpermissions.php index 73f59d73db..2f4c5da978 100755 --- a/apps/files_sharing/ajax/setpermissions.php +++ b/apps/files_sharing/ajax/setpermissions.php @@ -1,13 +1,14 @@ diff --git a/apps/files_sharing/ajax/share.php b/apps/files_sharing/ajax/share.php index 4863170f57..79ebf9187b 100755 --- a/apps/files_sharing/ajax/share.php +++ b/apps/files_sharing/ajax/share.php @@ -1,31 +1,33 @@ getToken(); + $path = ltrim($source, '/'); + $source = $userDirectory.$source; + // Check if the file exists or if the file is being reshared + if ($source && (OC_FILESYSTEM::file_exists($path) && OC_FILESYSTEM::is_readable($path) || OC_Share::getSource($source))) { + try { + $shared = new OC_Share($source, $uid_shared_with, $permissions); + // If this is a private link, return the token + if ($uid_shared_with == OC_Share::PUBLICLINK) { + OCP\JSON::success(array('data' => $shared->getToken())); + } else { + OCP\JSON::success(); + } + } catch (Exception $exception) { + OCP\Util::writeLog('files_sharing', 'Unexpected Error : '.$exception->getMessage(),OCP\Util::ERROR); + OCP\JSON::error(); } - } catch (Exception $exception) { - OCP\Util::writeLog('files_sharing',"Unexpected Error : ".$exception->getMessage(),OCP\Util::ERROR); - echo "false"; + } else { + OCP\Util::writeLog('files_sharing', 'File does not exist or is not readable :'.$source,OCP\Util::ERROR); + OCP\JSON::error(); } } diff --git a/apps/files_sharing/ajax/unshare.php b/apps/files_sharing/ajax/unshare.php index 5056c59a3d..9088bf4266 100755 --- a/apps/files_sharing/ajax/unshare.php +++ b/apps/files_sharing/ajax/unshare.php @@ -1,12 +1,13 @@ diff --git a/apps/files_sharing/ajax/userautocomplete.php b/apps/files_sharing/ajax/userautocomplete.php index 73b5f12698..99cc81bce0 100755 --- a/apps/files_sharing/ajax/userautocomplete.php +++ b/apps/files_sharing/ajax/userautocomplete.php @@ -1,7 +1,4 @@ "; $groups[] = ""; foreach ($userGroups as $group) { $groupUsers = OC_Group::usersInGroup($group); + $userCount = 0; foreach ($groupUsers as $user) { if ($user != $self) { $users[] = ""; + $userCount++; } } - $groups[] = ""; + // Don't include the group if only the current user is a member of it + if ($userCount > 0) { + $groups[] = ""; + } } $users[] = ""; $groups[] = ""; diff --git a/apps/files_sharing/css/sharing.css b/apps/files_sharing/css/sharing.css index 5acd9af589..c4b4540e9d 100644 --- a/apps/files_sharing/css/sharing.css +++ b/apps/files_sharing/css/sharing.css @@ -6,8 +6,9 @@ -moz-box-shadow:0 1px 1px #777; -webkit-box-shadow:0 1px 1px #777; box-shadow:0 1px 1px #777; -moz-border-radius-bottomleft:1em; -webkit-border-bottom-left-radius:1em; border-bottom-left-radius:1em; -moz-border-radius-bottomright:1em; -webkit-border-bottom-right-radius:1em; border-bottom-right-radius:1em; } -#shared_list { padding:0.5em; list-style-type: none; } -#public { border-top:1px solid #ddd; padding-top:0.5em; } +#sharedWithList { padding:0.5em; list-style-type: none; } +#privateLink { border-top:1px solid #ddd; padding-top:0.5em; } a.unshare { float:right; display:inline; margin:0 .5em; padding:.3em .3em 0 .3em !important; opacity:.5; } a.unshare:hover { opacity:1; } #share_with { width: 16em; } +#privateLink label, .edit { font-weight:normal; } diff --git a/apps/files_sharing/js/share.js b/apps/files_sharing/js/share.js index d9750761fc..65118f5e85 100644 --- a/apps/files_sharing/js/share.js +++ b/apps/files_sharing/js/share.js @@ -1,100 +1,270 @@ -$(document).ready(function() { - var shared_status = {}; - if (typeof FileActions !== 'undefined') { - FileActions.register('all', 'Share', function(filename) { - if (scanFiles.scanning){return;}//workaround to prevent additional http request block scanning feedback - var icon; - if (typeof filename == 'undefined') { - return false; - } - var file = $('#dir').val()+'/'+filename; - if(shared_status[file]) - return shared_status[file].icon; - $.ajax({ - type: 'GET', - url: OC.linkTo('files_sharing', 'ajax/getitem.php'), - dataType: 'json', - data: {source: file}, - async: false, - success: function(users) { - if (users) { - icon = OC.imagePath('core', 'actions/shared'); - $.each(users, function(index, row) { - if (row.uid_shared_with == 'public') { - icon = OC.imagePath('core', 'actions/public'); - } - }); +OC.Share={ + icons:[], + itemUsers:[], + itemGroups:[], + itemPrivateLink:false, + usersAndGroups:[], + loadIcons:function() { + // Cache all icons for shared files + $.getJSON(OC.filePath('files_sharing', 'ajax', 'getstatuses.php'), function(result) { + if (result && result.status === 'success') { + $.each(result.data, function(item, hasPrivateLink) { + if (hasPrivateLink) { + OC.Share.icons[item] = OC.imagePath('core', 'actions/public'); } else { - icon = OC.imagePath('core', 'actions/share'); - } - shared_status[file]= { timestamp: new Date().getTime(), icon: icon }; - } - }); - return icon; - }, function(filename) { - if (($('#dropdown').length > 0)) { - $('#dropdown').hide('blind', function() { - var dropdownFile = $('#dropdown').data('file') - var file = $('#dir').val()+'/'+filename; - $('#dropdown').remove(); - $('tr').removeClass('mouseOver'); - if (dropdownFile != file) { - createDropdown(filename, file); + OC.Share.icons[item] = OC.imagePath('core', 'actions/shared'); } }); + } + }); + }, + loadItem:function(item) { + $.ajax({type: 'GET', url: OC.filePath('files_sharing', 'ajax', 'getitem.php'), data: { item: item }, async: false, success: function(result) { + if (result && result.status === 'success') { + var item = result.data; + OC.Share.itemUsers = item.users; + OC.Share.itemGroups = item.groups; + OC.Share.itemPrivateLink = item.privateLink; + } + }}); + }, + share:function(source, uid_shared_with, permissions, callback) { + $.post(OC.filePath('files_sharing', 'ajax', 'share.php'), { sources: source, uid_shared_with: uid_shared_with, permissions: permissions }, function(result) { + if (result && result.status === 'success') { + if (callback) { + callback(result.data); + } } else { - createDropdown(filename, $('#dir').val()+'/'+filename); + OC.dialogs.alert('Error', 'Error while sharing'); + } + }); + }, + unshare:function(source, uid_shared_with, callback) { + $.post(OC.filePath('files_sharing', 'ajax', 'unshare.php'), { source: source, uid_shared_with: uid_shared_with }, function(result) { + if (result && result.status === 'success') { + if (callback) { + callback(); + } + } else { + OC.dialogs.alert('Error', 'Error while unsharing'); + } + }); + }, + changePermissions:function(source, uid_shared_with, permissions) { + $.post(OC.filePath('files_sharing','ajax','setpermissions.php'), { source: source, uid_shared_with: uid_shared_with, permissions: permissions }, function(result) { + if (!result || result.status !== 'success') { + OC.dialogs.alert('Error', 'Error while changing permissions'); + } + }); + }, + showDropDown:function(item, appendTo) { + OC.Share.loadItem(item); + var html = '