Skip to content

Commit

Permalink
Кнопка восстановления удаленной фотографии
Browse files Browse the repository at this point in the history
  • Loading branch information
klimashkin committed Mar 23, 2014
1 parent b251ad3 commit 73054d1
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
55 changes: 54 additions & 1 deletion controllers/photo.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var auth = require('./auth.js'),

msg = {
deny: 'You do not have permission for this action',
notExists: 'Requested photo does not exist',
notExists: 'Запрашиваемая фотография не существует',
notExistsRegion: 'Such region does not exist',
anotherStatus: 'Фотография уже в другом статусе, обновите страницу',
mustCoord: 'Фотография должна иметь координату или быть привязана к региону вручную'
Expand Down Expand Up @@ -80,6 +80,7 @@ var auth = require('./auth.js'),
edit: false,
disable: false,
remove: false,
restore: false,
approve: false,
convert: false
},
Expand All @@ -92,6 +93,7 @@ var auth = require('./auth.js'),

can.edit = canModerate || ownPhoto;
can.remove = canModerate || photo.s < 2 && ownPhoto; //Пока фото новое, её может удалить и владелец
can.restore = user.role > 9; //Восстанавливать может только администратор
if (canModerate) {
can.disable = true;
if (photo.s < 2) {
Expand Down Expand Up @@ -401,6 +403,52 @@ function removePhoto(socket, cid, cb) {
});
}

/**
* Восстановление фотографии
* @param socket Сокет пользователя
* @param cid
* @param cb Коллбэк
*/
function restorePhoto(socket, cid, cb) {
var iAm = socket.handshake.session.user;

if (!iAm || iAm.role < 10) {
return cb({message: msg.deny, error: true});
}
cid = Number(cid);
if (!cid) {
return cb({message: 'Bad params', error: true});
}

findPhoto({cid: cid, s: 9}, {}, iAm, function (err, photo) {
if (err || !photo) {
return cb({message: err && err.message || (msg.notExists + ' в удалёном статусе'), error: true});
}

if (!permissions.getCan(photo, iAm).restore) {
return cb({message: msg.deny, error: true});
}

photo.s = 5;
photo.save(function (err, photoSaved) {
if (err) {
return cb({message: err && err.message, error: true});
}
step(
function () {
changePublicPhotoExternality(socket, photoSaved, iAm, true, this.parallel());
},
function (err) {
if (err) {
return cb({message: 'Restore ok, but: ' + (err && err.message || 'other changes error'), error: true});
}
cb({message: 'ok'});
}
);
});
});
}

//Подтверждаем новую фотографию
function approvePhoto(iAm, cid, cb) {
cid = Number(cid);
Expand Down Expand Up @@ -1616,6 +1664,11 @@ module.exports.loadController = function (app, db, io) {
socket.emit('removePhotoIncCallback', {error: !!err});
});
});
socket.on('restorePhoto', function (data) {
restorePhoto(socket, data, function (resultData) {
socket.emit('restorePhotoCallback', resultData);
});
});

socket.on('approvePhoto', function (data) {
if (hs.session.user && hs.session.user.role > 4) {
Expand Down
1 change: 1 addition & 0 deletions public/history.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* (added) Комментариями новостей могут управлять только администраторы, комменатирями фотографий - также медераторы регионов, которым принадлежит фотография
* (added) Если у фотографии нет координаты, карта устанавливается в домашнее положение этого региона
* (added) Анонимным пользователям над помментариями показывается кнопка "Войдите, чтобы поделиться мнением"
* (added) Кнопка восстановления удаленной фотографии
* (added) Звание Советник
* (fixed) Регистрируемым пользователям по умолчанию присваивается регион Москва
* (fixed) Ближайшие фотографии должны показывать только бубличные фото
Expand Down
67 changes: 67 additions & 0 deletions public/js/module/photo/photo.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ define(['underscore', 'underscore.string', 'Utils', 'socket!', 'Params', 'knocko
edit: false,
disable: false,
remove: false,
restore: false,
approve: false,
convert: false
});
Expand Down Expand Up @@ -826,6 +827,72 @@ define(['underscore', 'underscore.string', 'Utils', 'socket!', 'Params', 'knocko
}
);
},
restore: function (data, event) {
if (!this.can.restore()) {
return false;
}

var that = this;

this.exe(true);
window.noty(
{
text: 'Фотография будет восстановлена и станет публичной<br>Подтвердить операцию?',
type: 'confirm',
layout: 'center',
modal: true,
force: true,
animation: {
open: {height: 'toggle'},
close: {},
easing: 'swing',
speed: 500
},
buttons: [
{addClass: 'btn btn-success', text: 'Да', onClick: function ($noty) {
// this = button element
// $noty = $noty element
if ($noty.$buttons && $noty.$buttons.find) {
$noty.$buttons.find('button').attr('disabled', true);
}

socket.once('restorePhotoCallback', function (data) {
$noty.$buttons.find('.btn-warning').remove();
var okButton = $noty.$buttons.find('button')
.attr('disabled', false)
.off('click');

if (data && !data.error) {
this.p.s(5);
this.originData.s = 5;

$noty.$message.children().html('Фотография успешно восстановлена');

okButton.text('Ok').on('click', function () {
$noty.close();
this.exe(false);
}.bind(this));
ga('send', 'event', 'photo', 'restore', 'photo restore success');
} else {
$noty.$message.children().html(data.message || 'Error occurred');
okButton.text('Close').on('click', function () {
$noty.close();
this.exe(false);
}.bind(this));
ga('send', 'event', 'photo', 'restore', 'photo restore error');
}
}.bind(that));
socket.emit('restorePhoto', that.p.cid());

}},
{addClass: 'btn btn-warning', text: 'Отмена', onClick: function ($noty) {
$noty.close();
that.exe(false);
}}
]
}
);
},
save: function (cb, ctx) {
var target = _.pick(ko_mapping.toJS(this.p), 'geo', 'dir', 'title', 'year', 'year2', 'address', 'author'),
key;
Expand Down
5 changes: 5 additions & 0 deletions views/module/photo/photo.jade
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@
span.glyphicon.glyphicon-trash
span(data-bind="text: !IOwner() && p.s()<2 ? ' Отклонить' : ' Удалить'")
// /ko
//ko if: p.s()===9 && can.restore()
button.btn.btn-success(type="button", data-bind="click: restore, attr: {disabled: exe()}")
span.glyphicon.glyphicon-repeat
span Восстановить
// /ko
| </script>
//-ACTIONS EDIT MODE
Expand Down

0 comments on commit 73054d1

Please sign in to comment.