controllers.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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', '$location', 'Setting',
  8. function ($scope, $location, Setting) {
  9. $scope.logo = {};
  10. $scope.go = function(url) {
  11. $location.url(url);
  12. };
  13. Setting.query({setting: 'logo'}, function(data) {
  14. if (data && data.length) {
  15. $scope.logo = data[0];
  16. }
  17. });
  18. $scope.isActive = function(view) {
  19. if (view === $location.path()) {
  20. return true;
  21. }
  22. if (view[view.length-1] !== '/') {
  23. view = view + '/';
  24. }
  25. return $location.path().indexOf(view) == 0;
  26. };
  27. }]
  28. );
  29. /* Controller for a group of date and time pickers. */
  30. eventManControllers.controller('DatetimePickerCtrl', ['$scope',
  31. function ($scope) {
  32. $scope.open = function($event) {
  33. $event.preventDefault();
  34. $event.stopPropagation();
  35. $scope.opened = true;
  36. };
  37. }]
  38. );
  39. /* Controller for modals. */
  40. eventManControllers.controller('ModalConfirmInstanceCtrl', ['$scope', '$modalInstance', 'message',
  41. function ($scope, $modalInstance, message) {
  42. $scope.message = message;
  43. $scope.ok = function () {
  44. $modalInstance.close($scope);
  45. };
  46. $scope.cancel = function () {
  47. $modalInstance.dismiss('cancel');
  48. };
  49. }]
  50. );
  51. eventManControllers.controller('EventsListCtrl', ['$scope', 'Event', '$modal', '$log', '$translate', '$rootScope',
  52. function ($scope, Event, $modal, $log, $translate, $rootScope) {
  53. $scope.events = Event.all();
  54. $scope.personsOrderProp = 'name';
  55. $scope.eventsOrderProp = "-begin_date";
  56. $scope.confirm_delete = 'Do you really want to delete this event?';
  57. $rootScope.$on('$translateChangeSuccess', function () {
  58. $translate('Do you really want to delete this event?').then(function (translation) {
  59. $scope.confirm_delete = translation;
  60. });
  61. });
  62. $scope.remove = function(_id) {
  63. var modalInstance = $modal.open({
  64. scope: $scope,
  65. templateUrl: 'modal-confirm-action.html',
  66. controller: 'ModalConfirmInstanceCtrl',
  67. resolve: {
  68. message: function() { return $scope.confirm_delete; }
  69. }
  70. });
  71. modalInstance.result.then(function() {
  72. console.debug('here');
  73. Event.remove({'id': _id}, function() {
  74. $scope.events = Event.all();
  75. }
  76. );
  77. });
  78. };
  79. }]
  80. );
  81. eventManControllers.controller('EventDetailsCtrl', ['$scope', '$state', 'Event', 'Person', 'EventUpdates', '$stateParams', 'Setting', '$log', '$translate', '$rootScope',
  82. function ($scope, $state, Event, Person, EventUpdates, $stateParams, Setting, $log, $translate, $rootScope) {
  83. $scope.personsOrder = ["name", "surname"];
  84. $scope.countAttendees = 0;
  85. $scope.message = {};
  86. $scope.event = {};
  87. $scope.event.persons = [];
  88. $scope.customFields = Setting.query({setting: 'person_custom_field', in_event_details: true});
  89. if ($stateParams.id) {
  90. $scope.event = Event.get($stateParams, function() {
  91. $scope.$watchCollection(function() {
  92. return $scope.event.persons;
  93. }, function(prev, old) {
  94. $scope.calcAttendees();
  95. }
  96. );
  97. });
  98. $scope.allPersons = Person.all();
  99. if ($state.is('event.info')) {
  100. // Handle WebSocket connection used to update the list of persons.
  101. $scope.EventUpdates = EventUpdates;
  102. $scope.EventUpdates.open();
  103. $scope.$watchCollection(function() {
  104. return $scope.EventUpdates.data;
  105. }, function(prev, old) {
  106. if (!($scope.EventUpdates.data && $scope.EventUpdates.data.update)) {
  107. return;
  108. }
  109. var data = $scope.EventUpdates.data.update;
  110. $log.debug('received ' + data.action + ' action from websocket source ' + data.uuid);
  111. if ($rootScope.app_uuid == data.uuid) {
  112. $log.debug('do not process our own message');
  113. return false;
  114. }
  115. if (!$scope.event.persons) {
  116. $scope.event.persons = [];
  117. }
  118. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  119. return data.person_id == el.person_id;
  120. });
  121. if (person_idx != -1) {
  122. $log.debug('person_id ' + data.person_id + ' found');
  123. } else {
  124. $log.debug('person_id ' + data.person_id + ' not found');
  125. }
  126. if (data.action == 'update' && person_idx != -1 && $scope.event.persons[person_idx] != data.person) {
  127. $scope.event.persons[person_idx] = data.person;
  128. } else if (data.action == 'add' && person_idx == -1) {
  129. $scope._localAddAttendee(data.person, true);
  130. } else if (data.action == 'delete' && person_idx != -1) {
  131. $scope._localRemoveAttendee({person_id: data.person_id});
  132. }
  133. }
  134. );
  135. }
  136. }
  137. $scope.updateOrded = function(key) {
  138. var new_order = [key];
  139. var inv_key;
  140. if (key && key[0] === '-') {
  141. inv_key = key.substring(1);
  142. } else {
  143. inv_key = '-' + key;
  144. }
  145. angular.forEach($scope.personsOrder,
  146. function(value, idx) {
  147. if (value !== key && value !== inv_key) {
  148. new_order.push(value);
  149. }
  150. }
  151. );
  152. $scope.personsOrder = new_order;
  153. };
  154. // store a new Event or update an existing one
  155. $scope.save = function() {
  156. // avoid override of event.persons list.
  157. var this_event = angular.copy($scope.event);
  158. if (this_event.persons) {
  159. delete this_event.persons;
  160. }
  161. if (this_event.id === undefined) {
  162. $scope.event = Event.save(this_event);
  163. } else {
  164. $scope.event = Event.update(this_event);
  165. }
  166. $scope.eventForm.$setPristine(false);
  167. };
  168. $scope.calcAttendees = function() {
  169. if (!($scope.event && $scope.event.persons)) {
  170. $scope.countAttendees = 0;
  171. return;
  172. }
  173. var attendees = 0;
  174. angular.forEach($scope.event.persons, function(value, key) {
  175. if (value.attended) {
  176. attendees += 1;
  177. }
  178. });
  179. $scope.countAttendees = attendees;
  180. };
  181. /* Stuff to do when an attendee is added locally. */
  182. $scope._localAddAttendee = function(person, hideMessage) {
  183. if (!$scope.event.persons) {
  184. $scope.event.persons = [];
  185. }
  186. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  187. return person.person_id == el.person_id;
  188. });
  189. if (person_idx != -1) {
  190. $log.debug('person already present: not added');
  191. return false;
  192. }
  193. $scope.event.persons.push(person);
  194. $scope.setPersonAttribute(person, 'attended', true, function() {
  195. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  196. return person.person_id == el.person_id;
  197. });
  198. if (all_person_idx != -1) {
  199. $scope.allPersons.splice(all_person_idx, 1);
  200. }
  201. }, hideMessage);
  202. };
  203. $scope._addAttendee = function(person) {
  204. person.person_id = person._id;
  205. person._id = $stateParams.id;
  206. Event.addPerson(person, function() {
  207. $scope._localAddAttendee(person);
  208. });
  209. $scope.query = '';
  210. };
  211. $scope.fastAddAttendee = function(person, isNew) {
  212. $log.debug('EventDetailsCtrl.fastAddAttendee.person:');
  213. $log.debug(person);
  214. if (isNew) {
  215. var personObj = new Person(person);
  216. personObj.$save(function(p) {
  217. $scope._addAttendee(angular.copy(p));
  218. $scope.newPerson = {};
  219. });
  220. } else {
  221. $scope._addAttendee(angular.copy(person));
  222. }
  223. };
  224. $scope.setPersonAttribute = function(person, key, value, callback, hideMessage) {
  225. $log.debug('EventDetailsCtrl.setPersonAttribute.event_id: ' + $stateParams.id);
  226. $log.debug('EventDetailsCtrl.setPersonAttribute.person_id: ' + person.person_id);
  227. $log.debug('EventDetailsCtrl.setPersonAttribute.key: ' + key + ' value: ' + value);
  228. var data = {_id: $stateParams.id, person_id: person.person_id};
  229. data[key] = value;
  230. Event.updatePerson(data,
  231. function(data) {
  232. if (!(data && data.person_id && data.person)) {
  233. return;
  234. }
  235. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  236. return data.person_id == el.person_id;
  237. });
  238. if (person_idx == -1) {
  239. $log.warn('unable to find person_id ' + person_id);
  240. return;
  241. }
  242. if ($scope.event.persons[person_idx] != data.person) {
  243. $scope.event.persons[person_idx] = data.person;
  244. }
  245. if (callback) {
  246. callback(data);
  247. }
  248. if (key === 'attended' && !hideMessage) {
  249. var msg = {};
  250. if (value) {
  251. msg.message = '' + person.name + ' ' + person.surname + ' successfully added to event ' + $scope.event.title;
  252. } else {
  253. msg.message = '' + person.name + ' ' + person.surname + ' successfully removed from event ' + $scope.event.title;
  254. msg.isError = true;
  255. }
  256. $scope.showMessage(msg);
  257. }
  258. });
  259. };
  260. $scope.setPersonAttributeAndRefocus = function(person, key, value) {
  261. $scope.setPersonAttribute(person, key, value);
  262. $scope.query = '';
  263. };
  264. /* Stuff to do when an attendee is removed locally. */
  265. $scope._localRemoveAttendee = function(person) {
  266. $log.debug('_localRemoveAttendee');
  267. $log.debug(person);
  268. if (!(person && person.person_id && $scope.event.persons)) {
  269. return;
  270. }
  271. var person_idx = $scope.event.persons.findIndex(function(el, idx, array) {
  272. return person.person_id == el.person_id;
  273. });
  274. if (person_idx == -1) {
  275. $log.warn('unable to find and delete person_id ' + person.person_id);
  276. return;
  277. }
  278. var removed_person = $scope.event.persons.splice(person_idx, 1);
  279. // to be used to populate allPersons, if needed.
  280. if (removed_person.length) {
  281. person = removed_person[0];
  282. }
  283. var all_person_idx = $scope.allPersons.findIndex(function(el, idx, array) {
  284. return person.person_id == el._id;
  285. });
  286. if (all_person_idx == -1 && person.person_id) {
  287. $scope.allPersons.push(person);
  288. }
  289. };
  290. $scope.removeAttendee = function(person) {
  291. Event.deletePerson({
  292. _id: $stateParams.id,
  293. person_id: person.person_id
  294. }, function() {
  295. $scope._localRemoveAttendee(person);
  296. });
  297. };
  298. $scope.showMessage = function(cfg) {
  299. $scope.message.show(cfg);
  300. };
  301. $scope.$on('$destroy', function() {
  302. $scope.EventUpdates && $scope.EventUpdates.close();
  303. });
  304. }]
  305. );
  306. eventManControllers.controller('PersonsListCtrl', ['$scope', 'Person', 'Setting', '$modal', '$translate', '$rootScope',
  307. function ($scope, Person, Setting, $modal, $translate, $rootScope) {
  308. $scope.persons = Person.all();
  309. $scope.personsOrder = ["name", "surname"];
  310. $scope.customFields = Setting.query({setting: 'person_custom_field',
  311. in_persons_list: true});
  312. $scope.confirm_delete = 'Do you really want to delete this person?';
  313. $rootScope.$on('$translateChangeSuccess', function () {
  314. $translate('Do you really want to delete this person?').then(function (translation) {
  315. $scope.confirm_delete = translation;
  316. });
  317. });
  318. $scope.updateOrded = function(key) {
  319. var new_order = [key];
  320. var inv_key;
  321. if (key && key[0] === '-') {
  322. inv_key = key.substring(1);
  323. } else {
  324. inv_key = '-' + key;
  325. }
  326. angular.forEach($scope.personsOrder,
  327. function(value, idx) {
  328. if (value !== key && value !== inv_key) {
  329. new_order.push(value);
  330. }
  331. }
  332. );
  333. $scope.personsOrder = new_order;
  334. };
  335. $scope.setAttribute = function(person, key, value) {
  336. var data = {_id: person._id};
  337. data[key] = value;
  338. Person.update(data, function() {
  339. $scope.persons = Person.all();
  340. });
  341. };
  342. $scope.remove = function(_id) {
  343. var modalInstance = $modal.open({
  344. scope: $scope,
  345. templateUrl: 'modal-confirm-action.html',
  346. controller: 'ModalConfirmInstanceCtrl',
  347. resolve: {
  348. message: function() { return $scope.confirm_delete; }
  349. }
  350. });
  351. modalInstance.result.then(function() {
  352. console.debug('here');
  353. Person.remove({'id': _id}, function() {
  354. $scope.persons = Person.all();
  355. }
  356. );
  357. });
  358. };
  359. }]
  360. );
  361. eventManControllers.controller('PersonDetailsCtrl', ['$scope', '$stateParams', 'Person', 'Event', 'Setting', '$log',
  362. function ($scope, $stateParams, Person, Event, Setting, $log) {
  363. $scope.personsOrderProp = 'name';
  364. $scope.eventsOrderProp = '-begin_date';
  365. $scope.addToEvent = '';
  366. $scope.customFields = Setting.query({setting: 'person_custom_field',
  367. in_persons_list: true});
  368. if ($stateParams.id) {
  369. $scope.person = Person.get($stateParams);
  370. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  371. } else {
  372. $scope.events = Event.all();
  373. }
  374. // store a new Person or update an existing one
  375. $scope.save = function() {
  376. if ($scope.person.id === undefined) {
  377. $scope.person = new Person($scope.person);
  378. $scope.person.$save(function(person) {
  379. if ($scope.addToEvent) {
  380. var data = angular.copy(person);
  381. data.person_id = data._id;
  382. data._id = $scope.addToEvent;
  383. data.attended = false;
  384. Event.addPerson(data);
  385. }
  386. });
  387. } else {
  388. $scope.person = Person.update($scope.person, function(data) {
  389. if ($scope.addToEvent) {
  390. var data = angular.copy($scope.person);
  391. data._id = $scope.addToEvent;
  392. data.person_id = $scope.person._id;
  393. data.attended = false;
  394. Event.addPerson(data);
  395. }
  396. });
  397. }
  398. $scope.personForm.$setPristine(false);
  399. };
  400. $scope.setPersonAttributeAtEvent = function(evnt, key, value) {
  401. var attrs = {_id: evnt._id, person_id: $stateParams.id};
  402. attrs[key] = value;
  403. Event.updatePerson(attrs,
  404. function(data) {
  405. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  406. }
  407. );
  408. };
  409. $scope.switchRegistered = function(evnt, person, add) {
  410. $log.debug('PersonDetailsCtrl.switchRegistered.event_id: ' + evnt._id);
  411. $log.debug('PersonDetailsCtrl.switchRegistered.person_id: ' + person._id);
  412. $log.debug('PersonDetailsCtrl.switchRegistered.add: ' + add);
  413. if (add) {
  414. var data = angular.copy(person);
  415. data._id = evnt._id;
  416. data.person_id = person._id;
  417. data.attended = false;
  418. Event.addPerson(data,
  419. function(data) {
  420. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  421. }
  422. );
  423. } else {
  424. Event.deletePerson({_id: evnt._id, person_id: person._id},
  425. function(data) {
  426. $scope.events = Person.getEvents({_id: $stateParams.id, all: true});
  427. }
  428. );
  429. }
  430. };
  431. }]
  432. );
  433. eventManControllers.controller('FileUploadCtrl', ['$scope', '$log', '$upload', 'Event',
  434. function ($scope, $log, $upload, Event) {
  435. $scope.file = null;
  436. $scope.reply = {};
  437. $scope.events = Event.all();
  438. $scope.upload = function(file, url) {
  439. $log.debug("FileUploadCtrl.upload");
  440. $upload.upload({
  441. url: url,
  442. file: file,
  443. fields: {targetEvent: $scope.targetEvent}
  444. }).progress(function(evt) {
  445. var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
  446. $log.debug('progress: ' + progressPercentage + '%');
  447. }).success(function(data, status, headers, config) {
  448. $scope.file = null;
  449. $scope.reply = angular.fromJson(data);
  450. });
  451. };
  452. }]
  453. );