controllers.js 28 KB

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