From 1cca4183c9d33765fb87fbacbdf178301d042747 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Tue, 13 Sep 2016 17:30:39 +0200 Subject: [PATCH] WIP avatar widget: - Implement upload and delete of contacts photo - Add missing cropper stylesheet --- addressbook/inc/class.addressbook_ui.inc.php | 29 +++++++++++++++ api/js/etemplate/et2_widget_image.js | 39 +++++++++++++++++--- api/templates/default/avatar_edit.xet | 5 +-- api/templates/default/etemplate2.css | 2 +- 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/addressbook/inc/class.addressbook_ui.inc.php b/addressbook/inc/class.addressbook_ui.inc.php index bb924143a1..c6115f6994 100644 --- a/addressbook/inc/class.addressbook_ui.inc.php +++ b/addressbook/inc/class.addressbook_ui.inc.php @@ -2936,6 +2936,35 @@ window.egw_LAB.wait(function() { return $this->tmpl->exec('addressbook.addressbook_ui.search',$content,$sel_options,$readonlys,array(),2); } + /** + * Ajax method to update edited avatar photo via + * avatar widget. + * + * @param int $contact_id + * @param file string $file = null null means to delete + */ + function ajax_update_photo ($contact_id, $file= null) + { + $response = Api\Json\Response::get(); + $contact = $this->read($contact_id); + if ($file) + { + $filteredFile=substr($file, strpos($file, ",")+1); + $decoded = base64_decode($filteredFile); + } + $contact ['jpegphoto'] = is_null($file)? $file: $decoded; + + $success = $this->save($contact); + if (!$success) + { + $response->alert($message); + } + else + { + $response->data(true); + } + } + /** * download photo of the given ($_GET['contact_id'] or $_GET['account_id']) contact */ diff --git a/api/js/etemplate/et2_widget_image.js b/api/js/etemplate/et2_widget_image.js index 86f3dd8db7..7eaab5e9e3 100644 --- a/api/js/etemplate/et2_widget_image.js +++ b/api/js/etemplate/et2_widget_image.js @@ -361,8 +361,7 @@ var et2_avatar = (function(){ "use strict"; return et2_image.extend( set_contact_id: function(_contact_id) { var params = { - menuaction: 'addressbook.addressbook_ui.photo', - 'etag' : '' + menuaction: 'addressbook.addressbook_ui.photo' }; var id = 'contact_id'; @@ -457,6 +456,9 @@ var et2_avatar = (function(){ "use strict"; return et2_image.extend( { var canvas = jQuery('#_cropper_image').cropper('getCroppedCanvas'); self.image.attr('src', canvas.toDataURL("image/jpeg", 1.0)); + egw.json('addressbook.addressbook_ui.ajax_update_photo', [self.options.contact_id, canvas.toDataURL('image/jpeg',1.0)], function(res){ + if (res) egw.refresh('Avatar updated.', egw.app_name()); + }).sendRequest(); } }, title: _title||egw.lang('Input required'), @@ -483,7 +485,13 @@ var et2_avatar = (function(){ "use strict"; return et2_image.extend( et2_dialog.show_dialog(function(_btn){ if (_btn == et2_dialog.YES_BUTTON) { - //TODO: send delete message to server + egw.json('addressbook.addressbook_ui.ajax_update_photo', [self.options.contact_id, null], function(res){ + if (res) + { + self.image.attr('src',''); + egw.refresh('Avatar Deleted.', egw.app_name()); + } + }).sendRequest(); } }, egw.lang('Delete this photo?'), 'Delete', null, et2_dialog.BUTTONS_YES_NO); }) @@ -496,7 +504,7 @@ var et2_avatar = (function(){ "use strict"; return et2_image.extend( // bind handler for activating actions on editable mask eml.on({ mouseover:function(){eml.css('opacity','0.9');}, - mouseout: function () {eml.css('opacity','0');} + mouseout: function (){eml.css('opacity','0');} }); }, @@ -523,4 +531,25 @@ var et2_avatar = (function(){ "use strict"; return et2_image.extend( } });}).call(this); -et2_register_widget(et2_avatar, ["avatar"]); \ No newline at end of file +et2_register_widget(et2_avatar, ["avatar"]); + +jQuery.extend(et2_avatar, +{ + /** + * Function runs after uplaod in avatar dialog is finished and it tries to + * update image and cropper container. + * + * @param {type} e + * @returns {undefined} + */ + uploadAvatar_onFinish: function (e){ + var file = e.data.resumable.files[0].file; + var reader = new FileReader(); + reader.onload = function (e) + { + jQuery('#_cropper_image').attr('src', e.target.result); + jQuery('#_cropper_image').cropper('replace',e.target.result); + }; + reader.readAsDataURL(file); + } +}); \ No newline at end of file diff --git a/api/templates/default/avatar_edit.xet b/api/templates/default/avatar_edit.xet index 7387de1aa0..5a041dc4ef 100644 --- a/api/templates/default/avatar_edit.xet +++ b/api/templates/default/avatar_edit.xet @@ -2,16 +2,15 @@ -