events.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var ai1ec = (function(){
  2. var API = {}, json = [], events = [];
  3. //Categorie di eventi
  4. API.cats = {
  5. single: 34,
  6. repeat: 87
  7. };
  8. var getCleanId = function(uid){
  9. var initChar = uid.indexOf('-'),
  10. endChar = uid.indexOf('@');
  11. return uid.substr(initChar+1, uid.length - uid.substr(endChar -1).length - initChar );
  12. };
  13. //Parser di un evento dell'array tornato da getXmlData
  14. API.event = {
  15. getId : function(event) {
  16. return event.properties.uid ? getCleanId(event.properties.uid.text) : null;
  17. },
  18. getTitle : function(event) {
  19. return event.properties.summary.text || null;
  20. },
  21. getDescription : function(event) {
  22. return event.properties.description.text || "";
  23. },
  24. getInitDate : function(event) {
  25. return event.properties.dtstart && event.properties.dtstart["date-time"] ? event.properties.dtstart["date-time"] : "";
  26. },
  27. getEndDate : function(event) {
  28. return event.properties.dtend && event.properties.dtend["date-time"] ? event.properties.dtend["date-time"] : "";
  29. },
  30. getCats : function(event) {
  31. return event.properties.categories.text || "";
  32. },
  33. getTags : function(event) {
  34. return event.properties["x-tags"] && event.properties["x-tags"]._ ? event.properties["x-tags"]._ : "";
  35. },
  36. getUrl : function(event) {
  37. return event.properties.url && event.properties.url.uri && event.properties.url.uri ? event.properties.url.uri : "";
  38. },
  39. getContact : function(event) {
  40. return event.properties.contact || "";
  41. },
  42. getGeo : function(event) {
  43. return event.properties.location.text || "";
  44. },
  45. //Regole per gli eventi ripetitivi
  46. getRule : function(event) {
  47. return event.properties.rrule || "";
  48. },
  49. getImage : function(event){
  50. return event.properties['x-wp-images-url'] && event.properties['x-wp-images-url'].unknown ? event.properties['x-wp-images-url'].unknown : "";
  51. },
  52. };
  53. var getXmlData = function(url, catId, callback) {
  54. $.ajax({
  55. url: url,
  56. dataType: 'xml',
  57. method: 'GET',
  58. success: function(res) {
  59. json[catId] = $.xml2json(res);
  60. events[catId] = json[catId]["#document"].icalendar.vcalendar.components.vevent;
  61. console.log(events[catId]);
  62. return callback(events[catId]);
  63. },
  64. error: function(err){
  65. console.log("Err: status ", err.status);
  66. return callback(events[catId]);
  67. }
  68. });
  69. };
  70. var getDefaultUrl = function(catId){
  71. catId = catId || API.cats.single;
  72. return API.remote.site + API.remote.q + "&" + API.remote.catq + catId + "&" + API.remote.xmlq;
  73. };
  74. //Oggetto che gestisce attività remote
  75. API.remote = {
  76. site : 'http://www.ecn.org/xm24',
  77. q : '/?plugin=all-in-one-event-calendar&controller=ai1ec_exporter_controller&action=export_events',
  78. post: 'ai1ec_post_ids=',
  79. catq : 'ai1ec_cat_ids=',
  80. xmlq : 'xml=true',
  81. win : 'window=true',
  82. //post_ids
  83. //Torna un array con tutti gli eventi organizzati da xm24, un array vuoto se errore.
  84. getAllEvents : function(catId, callback) {
  85. var url = getDefaultUrl(catId);
  86. getXmlData(url, catId, callback);
  87. },
  88. //Torna un array con gli eventi tra -30 e +30 giorni da oggi, un array vuoto se errore.
  89. getLastEvents : function(catId, callback) {
  90. var url = getDefaultUrl(catId) + '&' + API.remote.win;
  91. getXmlData(url, catId, callback);
  92. },
  93. }
  94. return API;
  95. })();