controllers.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. 'use strict';
  2. /* Controllers; their method are available where specified with the ng-controller
  3. * directive or for a given route/state (see app.js). They use some services to
  4. * connect to the backend (see services.js). */
  5. var eventManControllers = angular.module('eventManControllers', []);
  6. /* A controller that can be used to navigate. */
  7. eventManControllers.controller('NavigationCtrl', ['$scope', '$rootScope', '$location', 'Setting', '$state',
  8. function ($scope, $rootScope, $location, Setting, $state) {
  9. $scope.logo = {};
  10. $scope.getLocation = function() {
  11. return $location.absUrl();
  12. };
  13. $scope.go = function(url) {
  14. $location.url(url);
  15. };
  16. Setting.query({setting: 'logo'}, function(data) {
  17. if (data && data.length) {
  18. $scope.logo = data[0];
  19. }
  20. });
  21. $scope.isActive = function(view) {
  22. if (view === $location.path()) {
  23. return true;
  24. }
  25. if (view[view.length-1] !== '/') {
  26. view = view + '/';
  27. }
  28. return $location.path().indexOf(view) == 0;
  29. };
  30. }]
  31. );
  32. /* Controller for a group of date and time pickers. */
  33. eventManControllers.controller('DatetimePickerCtrl', ['$scope',
  34. function ($scope) {
  35. $scope.open = function($event) {
  36. $event.preventDefault();
  37. $event.stopPropagation();
  38. $scope.opened = true;
  39. };
  40. }]
  41. );
  42. /* Controller for modals. */
  43. eventManControllers.controller('ModalConfirmInstanceCtrl', ['$scope', '$uibModalInstance', 'message',
  44. function ($scope, $uibModalInstance, message) {
  45. $scope.message = message;
  46. $scope.ok = function () {
  47. $uibModalInstance.close($scope);
  48. };
  49. $scope.cancel = function () {
  50. $uibModalInstance.dismiss('cancel');
  51. };
  52. }]
  53. );
  54. eventManControllers.controller('EventsListCtrl', ['$scope', 'Event', '$uibModal', '$log', '$translate', '$rootScope', '$state', '$filter',
  55. function ($scope, Event, $uibModal, $log, $translate, $rootScope, $state, $filter) {
  56. $scope.tickets = [];
  57. $scope.events = Event.all(function(events) {
  58. if (events && $state.is('tickets')) {
  59. angular.forEach(events, function(evt, idx) {
  60. var evt_tickets = (evt.tickets || []).slice(0);
  61. angular.forEach(evt_tickets, function(obj, obj_idx) {
  62. obj.event_title = evt.title;
  63. obj.event_id = evt._id;
  64. });
  65. $scope.tickets.push.apply($scope.tickets, evt_tickets || []);
  66. });
  67. $scope.filterTickets();
  68. }
  69. });
  70. $scope.eventsOrderProp = "-begin_date";
  71. $scope.ticketsOrderProp = ["name", "surname"];
  72. $scope.shownItems = [];
  73. $scope.currentPage = 1;
  74. $scope.itemsPerPage = 20;
  75. $scope.filteredLength = 0;
  76. $scope.maxPaginationSize = 5;
  77. $scope.filterTickets = function() {
  78. var tickets = $scope.tickets || [];
  79. tickets = $filter('splittedFilter')(tickets, $scope['query-tickets']);
  80. tickets = $filter('orderBy')(tickets, $scope.ticketsOrderProp);
  81. $scope.filteredLength = tickets.length;
  82. tickets = $filter('pagination')(tickets, $scope.currentPage, $scope.itemsPerPage);
  83. $scope.shownItems = tickets;
  84. };
  85. $scope.$watch('query-tickets', function() {
  86. $scope.filterTickets();
  87. });
  88. $scope.$watch('currentPage + itemsPerPage', function() {
  89. $scope.filterTickets();
  90. });
  91. $scope.confirm_delete = 'Do you really want to delete this event?';
  92. $rootScope.$on('$translateChangeSuccess', function () {
  93. $translate('Do you really want to delete this event?').then(function (translation) {
  94. $scope.confirm_delete = translation;
  95. });
  96. });
  97. $scope.deleteEvent = function(_id) {
  98. var modalInstance = $uibModal.open({
  99. scope: $scope,
  100. templateUrl: 'modal-confirm-action.html',
  101. controller: 'ModalConfirmInstanceCtrl',
  102. resolve: {
  103. message: function() { return $scope.confirm_delete; }
  104. }
  105. });
  106. modalInstance.result.then(function() {
  107. Event.delete({'id': _id}, function() {
  108. $scope.events = Event.all();
  109. });
  110. });
  111. };
  112. $scope.updateOrded = function(key) {
  113. var new_order = [key];
  114. var inv_key;
  115. if (key && key[0] === '-') {
  116. inv_key = key.substring(1);
  117. } else {
  118. inv_key = '-' + key;
  119. }
  120. angular.forEach($scope.ticketsOrderProp,
  121. function(value, idx) {
  122. if (value !== key && value !== inv_key) {
  123. new_order.push(value);
  124. }
  125. }
  126. );
  127. $scope.ticketsOrderProp = new_order;
  128. $scope.filterTickets();
  129. };
  130. }]
  131. );
  132. eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', '$log', '$translate', '$rootScope',
  133. function ($scope, $state, Event, $log, $translate, $rootScope) {
  134. $scope.event = {};
  135. $scope.event.tickets = [];
  136. $scope.event.formSchema = {};
  137. $scope.eventFormDisabled = false;
  138. if ($state.params.id) {
  139. $scope.event = Event.get($state.params);
  140. if ($state.is('event.view') || !$rootScope.hasPermission('event|update')) {
  141. $scope.eventFormDisabled = true;
  142. }
  143. }
  144. // store a new Event or update an existing one
  145. $scope.save = function() {
  146. // avoid override of event.tickets list.
  147. var this_event = angular.copy($scope.event);
  148. if (this_event.tickets) {
  149. delete this_event.tickets;
  150. }
  151. if (this_event._id === undefined) {
  152. $scope.event = Event.save(this_event);
  153. } else {
  154. $scope.event = Event.update(this_event);
  155. }
  156. $scope.eventForm.$setPristine(false);
  157. };
  158. $scope.saveForm = function(easyFormGeneratorModel) {
  159. $scope.event.formSchema = easyFormGeneratorModel;
  160. $scope.save();
  161. };
  162. }]
  163. );
  164. eventManControllers.controller('EventTicketsCtrl', ['$scope', '$state', 'Event', 'EventTicket', 'Setting', '$log', '$translate', '$rootScope', 'EventUpdates', '$uibModal', '$filter',
  165. function ($scope, $state, Event, EventTicket, Setting, $log, $translate, $rootScope, EventUpdates, $uibModal, $filter) {
  166. $scope.ticketsOrder = ["name", "surname"];
  167. $scope.countAttendees = 0;
  168. $scope.message = {};
  169. $scope.query = '';
  170. $scope.event = {};
  171. $scope.event.tickets = [];
  172. $scope.shownItems = [];
  173. $scope.ticket = {}; // current ticket, for the event.ticket.* states
  174. $scope.tickets = []; // list of all tickets, for the 'tickets' state
  175. $scope.formSchema = {};
  176. $scope.formData = {};
  177. $scope.guiOptions = {dangerousActionsEnabled: false};
  178. $scope.customFields = Setting.query({setting: 'ticket_custom_field', in_event_details: true});
  179. $scope.registeredFilterOptions = {all: false};
  180. $scope.currentPage = 1;
  181. $scope.itemsPerPage = 20;
  182. $scope.filteredLength = 0;
  183. $scope.maxPaginationSize = 5;
  184. $scope.filterTickets = function() {
  185. var tickets = $scope.event.tickets || [];
  186. tickets = $filter('splittedFilter')(tickets, $scope.query);
  187. tickets = $filter('registeredFilter')(tickets, $scope.registeredFilterOptions);
  188. tickets = $filter('orderBy')(tickets, $scope.ticketsOrder);
  189. $scope.filteredLength = tickets.length;
  190. tickets = $filter('pagination')(tickets, $scope.currentPage, $scope.itemsPerPage);
  191. $scope.shownItems = tickets;
  192. };
  193. $scope.$watch('query', function() {
  194. $scope.filterTickets();
  195. });
  196. $scope.$watch('currentPage + itemsPerPage', function() {
  197. $scope.filterTickets();
  198. });
  199. $scope.formFieldsMap = {};
  200. $scope.formFieldsMapRev = {};
  201. if ($state.params.id) {
  202. $scope.event = Event.get({id: $state.params.id}, function(data) {
  203. $scope.$watchCollection(function() {
  204. return $scope.event.tickets;
  205. }, function(new_collection, old_collection) {
  206. $scope.calcAttendees();
  207. $scope.filterTickets();
  208. }
  209. );
  210. if (!(data && data.formSchema)) {
  211. return;
  212. }
  213. $scope.formSchema = data.formSchema.edaFieldsModel;
  214. $scope.extractFormFields(data.formSchema.formlyFieldsModel);
  215. // Editing an existing ticket
  216. if ($state.params.ticket_id) {
  217. EventTicket.get({id: $state.params.id, ticket_id: $state.params.ticket_id}, function(data) {
  218. $scope.ticket = data;
  219. angular.forEach(data, function(value, key) {
  220. if (!$scope.formFieldsMapRev[key]) {
  221. return;
  222. }
  223. $scope.formData[$scope.formFieldsMapRev[key]] = value;
  224. });
  225. });
  226. }
  227. });
  228. // Managing the list of tickets.
  229. if ($state.is('event.tickets')) {
  230. $scope.allPersons = Event.group_persons({id: $state.params.id});
  231. // Handle WebSocket connection used to update the list of tickets.
  232. $scope.EventUpdates = EventUpdates;
  233. $scope.EventUpdates.open();
  234. $scope.$watchCollection(function() {
  235. return $scope.EventUpdates.data;
  236. }, function(new_collection, old_collection) {
  237. if (!($scope.EventUpdates.data && $scope.EventUpdates.data.update)) {
  238. $log.debug('no data received from the WebSocket');
  239. return;
  240. }
  241. var data = $scope.EventUpdates.data.update;
  242. $log.debug('received ' + data.action + ' action from websocket source ' + data.uuid + ' . Full data:');
  243. $log.debug(data);
  244. if ($rootScope.app_uuid == data.uuid) {
  245. $log.debug('do not process our own message');
  246. return false;
  247. }
  248. if (!$scope.event.tickets) {
  249. $scope.event.tickets = [];
  250. }
  251. var ticket_id = data._id || (data.ticket && data.ticket._id);
  252. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  253. return ticket_id && (ticket_id == el._id);
  254. });
  255. if (ticket_idx != -1) {
  256. $log.debug('_id ' + data._id + ' found');
  257. } else {
  258. $log.debug('_id ' + data._id + ' not found');
  259. }
  260. if (data.action == 'update' && ticket_idx != -1 && $scope.event.tickets[ticket_idx] != data.ticket) {
  261. // if we're updating the 'attended' key and the action came from us (same user, possibly on
  262. // a different station), also show a message.
  263. if (data.ticket.attended != $scope.event.tickets[ticket_idx].attended &&
  264. $scope.info.user.username == data.username) {
  265. $scope.showAttendedMessage(data.ticket, data.ticket.attended);
  266. }
  267. $scope.event.tickets[ticket_idx] = data.ticket;
  268. } else if (data.action == 'add' && ticket_idx == -1) {
  269. $scope._localAddTicket(data.ticket);
  270. } else if (data.action == 'delete' && ticket_idx != -1) {
  271. $scope._localRemoveTicket({_id: data._id});
  272. }
  273. }
  274. );
  275. }
  276. } else if ($state.is('tickets')) {
  277. $scope.tickets = EventTicket.all();
  278. }
  279. $scope.calcAttendees = function() {
  280. if (!($scope.event && $scope.event.tickets)) {
  281. $scope.countAttendees = 0;
  282. return;
  283. }
  284. var attendees = 0;
  285. angular.forEach($scope.event.tickets, function(value, key) {
  286. if (value.attended && !value.cancelled) {
  287. attendees += 1;
  288. }
  289. });
  290. $scope.countAttendees = attendees;
  291. };
  292. /* Stuff to do when a ticket is added, modified or removed locally. */
  293. $scope._localAddTicket = function(ticket, original_ticket) {
  294. if (!$state.is('event.tickets')) {
  295. return true;
  296. }
  297. var ret = true;
  298. if (!$scope.event.tickets) {
  299. $scope.event.tickets = [];
  300. }
  301. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  302. return ticket._id == el._id;
  303. });
  304. if (ticket_idx != -1) {
  305. $log.warn('ticket already present: not added');
  306. ret = false;
  307. } else {
  308. $scope.event.tickets.push(ticket);
  309. }
  310. // Try to remove this person from the allPersons list using ID of the original entry or email.
  311. var field = null;
  312. var field_value = null;
  313. if (original_ticket && original_ticket._id) {
  314. field = '_id';
  315. field_value = original_ticket._id;
  316. } else if (ticket.email) {
  317. field = 'email';
  318. field_value = ticket.email;
  319. }
  320. if (field) {
  321. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  322. return field_value == el[field];
  323. });
  324. if (all_person_idx != -1) {
  325. $scope.allPersons.splice(all_person_idx, 1);
  326. }
  327. }
  328. return ret;
  329. };
  330. $scope._localUpdateTicket = function(ticket) {
  331. if (!$state.is('event.tickets')) {
  332. return;
  333. }
  334. if (!$scope.event.tickets) {
  335. $scope.event.tickets = [];
  336. }
  337. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  338. return ticket._id == el._id;
  339. });
  340. if (ticket_idx == -1) {
  341. $log.warn('ticket not present: not updated');
  342. return false;
  343. }
  344. $scope.event.tickets[ticket_idx] = ticket;
  345. };
  346. $scope._localRemoveTicket = function(ticket) {
  347. if (!(ticket && ticket._id && $scope.event.tickets)) {
  348. return;
  349. }
  350. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  351. return ticket._id == el._id;
  352. });
  353. if (ticket_idx == -1) {
  354. $log.warn('unable to find and delete ticket _id ' + ticket._id);
  355. return;
  356. }
  357. var removed_person = $scope.event.tickets.splice(ticket_idx, 1);
  358. // to be used to populate allPersons, if needed.
  359. var person = null;
  360. if (removed_person.length) {
  361. person = removed_person[0];
  362. } else {
  363. return;
  364. }
  365. if (!$scope.allPersons) {
  366. $scope.allPersons = [];
  367. }
  368. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  369. return person._id == el._id;
  370. });
  371. if (all_person_idx == -1 && person._id) {
  372. $scope.allPersons.push(person);
  373. }
  374. };
  375. $scope.buildTicketLabel = function(ticket) {
  376. var name = ticket.name || '';
  377. if (ticket.surname) {
  378. if (name) {
  379. name = name + ' ';
  380. }
  381. name = name + ticket.surname;
  382. }
  383. if (!name && ticket.email) {
  384. name = ticket.email;
  385. }
  386. if (!name) {
  387. name = 'ticket';
  388. }
  389. return name;
  390. };
  391. $scope.setTicketAttribute = function(ticket, key, value, callback, hideMessage) {
  392. $log.debug('setTicketAttribute for _id ' + ticket._id + ' key: ' + key + ' value: ' + value);
  393. var newData = {event_id: $state.params.id, _id: ticket._id};
  394. newData[key] = value;
  395. EventTicket.update(newData, function(data) {
  396. if (!(data && data._id && data.ticket)) {
  397. return;
  398. }
  399. if (callback) {
  400. callback(data);
  401. }
  402. if (!$state.is('event.tickets')) {
  403. return;
  404. }
  405. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  406. return data._id == el._id;
  407. });
  408. if (ticket_idx == -1) {
  409. $log.warn('unable to find ticket _id ' + data._id);
  410. return;
  411. }
  412. if ($scope.event.tickets[ticket_idx] != data.ticket) {
  413. $scope.event.tickets[ticket_idx] = data.ticket;
  414. }
  415. if (key === 'attended' && !hideMessage) {
  416. $scope.showAttendedMessage(data.ticket, value);
  417. }
  418. });
  419. };
  420. $scope.showAttendedMessage = function(ticket, attends) {
  421. var msg = {};
  422. var name = $scope.buildTicketLabel(ticket);
  423. if (attends) {
  424. msg.message = name + ' successfully added to event ' + $scope.event.title;
  425. } else {
  426. msg.message = name + ' successfully removed from event ' + $scope.event.title;
  427. msg.isError = true;
  428. }
  429. $scope.showMessage(msg);
  430. };
  431. $scope.setTicketAttributeAndRefocus = function(ticket, key, value) {
  432. $scope.setTicketAttribute(ticket, key, value);
  433. $scope.query = '';
  434. };
  435. $scope._setAttended = function(ticket) {
  436. $scope.setTicketAttribute(ticket, 'attended', true, null, true);
  437. };
  438. $scope.deleteTicket = function(ticket) {
  439. EventTicket.delete({
  440. event_id: $state.params.id,
  441. ticket_id: ticket._id
  442. }, function() {
  443. $scope._localRemoveTicket(ticket);
  444. });
  445. };
  446. /* event listners; needed because otherwise, adding a ticket with the Quick add form,
  447. * we'd be changing the $scope outside of the AngularJS's $digest. */
  448. $rootScope.$on('event:ticket:new', function(evt, ticket, callback) {
  449. $scope._localAddTicket(ticket);
  450. if (callback) {
  451. callback(ticket);
  452. }
  453. });
  454. $rootScope.$on('event:ticket:update', function(evt, ticket) {
  455. if (!$scope.event.tickets) {
  456. $scope.event.tickets = [];
  457. }
  458. var ticket_idx = $scope.event.tickets.findIndex(function(el, idx, array) {
  459. return ticket._id == el._id;
  460. });
  461. if (ticket_idx == -1) {
  462. $log.debug('ticket not present: not updated');
  463. return false;
  464. }
  465. $scope.event.tickets[ticket_idx] = ticket;
  466. });
  467. $rootScope.$on('event:ticket:set-attr', function(evt, ticket, key, value, callback, hideMessage) {
  468. $scope.setTicketAttribute(ticket, key, value, callback, hideMessage);
  469. });
  470. $scope.addTicket = function(ticket, cb) {
  471. ticket.event_id = $state.params.id;
  472. EventTicket.add(ticket, function(ret_ticket) {
  473. $log.debug('addTicket');
  474. $log.debug(ret_ticket);
  475. $rootScope.$emit('event:ticket:new', ret_ticket, function() {
  476. $rootScope.$emit('event:ticket:set-attr', ret_ticket, 'attended', true, null, false);
  477. });
  478. if (cb) {
  479. cb(ticket);
  480. }
  481. if (!$state.is('event.tickets')) {
  482. $state.go('event.ticket.edit', {id: $scope.event._id, ticket_id: ret_ticket._id});
  483. } else {
  484. $scope.query = '';
  485. if ($scope.$close) {
  486. // Close the Quick ticket modal.
  487. $scope.$close();
  488. }
  489. }
  490. });
  491. };
  492. $scope.updateTicket = function(ticket, cb) {
  493. ticket.event_id = $state.params.id;
  494. EventTicket.update(ticket, function(t) {
  495. $rootScope.$emit('event:ticket:update', t.ticket);
  496. if (cb) {
  497. cb(t);
  498. }
  499. });
  500. };
  501. $scope.toggleCancelledTicket = function() {
  502. if (!$scope.ticket._id) {
  503. return;
  504. }
  505. $scope.ticket.cancelled = !$scope.ticket.cancelled;
  506. $scope.setTicketAttribute($scope.ticket, 'cancelled', $scope.ticket.cancelled, function() {
  507. $scope.guiOptions.dangerousActionsEnabled = false;
  508. });
  509. };
  510. $scope.openQuickAddTicket = function(_id) {
  511. var modalInstance = $uibModal.open({
  512. templateUrl: 'modal-quick-add-ticket.html',
  513. controller: 'EventTicketsCtrl'
  514. });
  515. modalInstance.result.then(function() {});
  516. };
  517. $scope.submitForm = function(dataModelSubmitted) {
  518. angular.forEach(dataModelSubmitted, function(value, key) {
  519. key = $scope.formFieldsMap[key] || key;
  520. $scope.ticket[key] = value;
  521. });
  522. if ($state.is('event.ticket.edit')) {
  523. $scope.updateTicket($scope.ticket, function() {
  524. $scope.showMessage({message: 'ticket successfully updated'});
  525. });
  526. } else {
  527. $scope.addTicket($scope.ticket);
  528. }
  529. };
  530. $scope.cancelForm = function() {
  531. if (!$state.is('event.tickets')) {
  532. $state.go('events');
  533. } else if ($scope.$close) {
  534. $scope.$close();
  535. }
  536. };
  537. $scope.extractFormFields = function(formlyFieldsModel) {
  538. if (!formlyFieldsModel) {
  539. return;
  540. }
  541. angular.forEach(formlyFieldsModel, function(row, idx) {
  542. if (!row.className == 'row') {
  543. return;
  544. }
  545. angular.forEach(row.fieldGroup || [], function(item, idx) {
  546. if (!(item.key && item.templateOptions && item.templateOptions.label)) {
  547. return;
  548. }
  549. var value = item.templateOptions.label.toLowerCase();
  550. $scope.formFieldsMap[item.key] = value;
  551. $scope.formFieldsMapRev[value] = item.key;
  552. });
  553. });
  554. };
  555. $scope.updateOrded = function(key) {
  556. var new_order = [key];
  557. var inv_key;
  558. if (key && key[0] === '-') {
  559. inv_key = key.substring(1);
  560. } else {
  561. inv_key = '-' + key;
  562. }
  563. angular.forEach($scope.ticketsOrder,
  564. function(value, idx) {
  565. if (value !== key && value !== inv_key) {
  566. new_order.push(value);
  567. }
  568. }
  569. );
  570. $scope.ticketsOrder = new_order;
  571. };
  572. $scope.showMessage = function(cfg) {
  573. $scope.message && $scope.message.show && $scope.message.show(cfg);
  574. };
  575. $scope.$on('$destroy', function() {
  576. $scope.EventUpdates && $scope.EventUpdates.close();
  577. });
  578. }]
  579. );
  580. eventManControllers.controller('UsersCtrl', ['$scope', '$rootScope', '$state', '$log', 'User', '$uibModal',
  581. function ($scope, $rootScope, $state, $log, User, $uibModal) {
  582. $scope.loginData = {};
  583. $scope.user = {};
  584. $scope.updateUserInfo = {};
  585. $scope.users = [];
  586. $scope.usersOrderProp = 'username';
  587. $scope.ticketsOrderProp = 'title';
  588. $scope.confirm_delete = 'Do you really want to delete this user?';
  589. $rootScope.$on('$translateChangeSuccess', function () {
  590. $translate('Do you really want to delete this user?').then(function (translation) {
  591. $scope.confirm_delete = translation;
  592. });
  593. });
  594. $scope.updateUsersList = function() {
  595. if ($state.is('users')) {
  596. $scope.users = User.all();
  597. }
  598. };
  599. $scope.updateUsersList();
  600. if ($state.is('user.edit') && $state.params.id) {
  601. $scope.user = User.get({id: $state.params.id}, function() {
  602. $scope.updateUserInfo = $scope.user;
  603. });
  604. }
  605. $scope.updateUser = function() {
  606. User.update($scope.updateUserInfo);
  607. };
  608. $scope.deleteUser = function(user_id) {
  609. var modalInstance = $uibModal.open({
  610. scope: $scope,
  611. templateUrl: 'modal-confirm-action.html',
  612. controller: 'ModalConfirmInstanceCtrl',
  613. resolve: {
  614. message: function() { return $scope.confirm_delete; }
  615. }
  616. });
  617. modalInstance.result.then(function() {
  618. User.delete({id: user_id}, $scope.updateUsersList);
  619. });
  620. };
  621. $scope.register = function() {
  622. User.add($scope.newUser, function(data) {
  623. $scope.login($scope.newUser);
  624. });
  625. };
  626. $scope.login = function(loginData) {
  627. if (!loginData) {
  628. loginData = $scope.loginData;
  629. }
  630. User.login(loginData, function(data) {
  631. if (!data.error) {
  632. $rootScope.readInfo(function(info) {
  633. $log.debug('logged in user: ' + $scope.info.user.username);
  634. $rootScope.clearError();
  635. $state.go('events');
  636. });
  637. }
  638. });
  639. };
  640. $scope.logout = function() {
  641. User.logout({}, function(data) {
  642. if (!data.error) {
  643. $rootScope.readInfo(function() {
  644. $log.debug('logged out user');
  645. $state.go('login');
  646. });
  647. }
  648. });
  649. };
  650. }]
  651. );
  652. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  653. function ($scope, $log, $upload, Event) {
  654. $scope.file = null;
  655. $scope.reply = {};
  656. $scope.events = Event.all();
  657. $scope.upload = function(file, url) {
  658. $log.debug("FileUploadCtrl.upload");
  659. $upload.upload({
  660. url: url,
  661. file: file,
  662. fields: {targetEvent: $scope.targetEvent}
  663. }).progress(function(evt) {
  664. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  665. $log.debug('progress: ' + progressPercentage + '%');
  666. }).success(function(data, status, headers, config) {
  667. $scope.file = null;
  668. $scope.reply = angular.fromJson(data);
  669. });
  670. };
  671. }]
  672. );