From 75ed81e499426c7be864868da90c25bb0d1a821f Mon Sep 17 00:00:00 2001 From: Rupesh Manandhar Date: Tue, 19 Sep 2017 12:52:54 +0545 Subject: [PATCH] [#828] Push publication between nations --- app/MyApp/app/Router.js | 215 +++++-- app/MyApp/app/views/NationPublicationTable.js | 595 ++++++++++++++++++ app/MyApp/index.html | 2 + app/nation/app/Router.js | 32 +- app/nation/app/views/listNationView.js | 149 +++++ app/nation/index.html | 6 +- databases/publicationdistribution.js | 7 + init_docs/Text.json | 5 +- 8 files changed, 972 insertions(+), 39 deletions(-) create mode 100644 app/MyApp/app/views/NationPublicationTable.js create mode 100644 app/nation/app/views/listNationView.js diff --git a/app/MyApp/app/Router.js b/app/MyApp/app/Router.js index b39383b84..4b30da7e3 100644 --- a/app/MyApp/app/Router.js +++ b/app/MyApp/app/Router.js @@ -88,6 +88,7 @@ $(function() { 'CompileAppManifest': 'CompileAppManifest', 'communityManage': 'communityManage', 'publications/:community': 'Publications', + 'nationpublications/:community': 'NationPublications', 'surveys/:community': 'Surveys', 'openSurvey/:surveyId/:isSubmitted/:memberId': 'OpenSurvey', 'memberSurveys': 'SurveysForMembers', @@ -153,6 +154,13 @@ $(function() { $('#requestsTable').append('

'+App.languageDict.get('Publications')+'

'); $('#requestsTable').append(PublicationsView.el); }, + NationPublications: function(publicationIdes) { + var PublicationsView = new App.Views.NationPublicationTable(); + PublicationsView.render(); + App.$el.children('.body').html('
'); + $('#requestsTable').append('

'+App.languageDict.get('Publications')+'

'); + $('#requestsTable').append(PublicationsView.el); + }, Surveys: function() { var SurveysView = new App.Views.SurveyTable(); @@ -971,6 +979,7 @@ $(function() { }, getNationVersion: function (dashboard) { + console.log(dashboard) var that = this; var configuration = App.configuration var nationName = configuration.get("nationName") @@ -1158,34 +1167,36 @@ $(function() { newSurveysCount++; } } - } - if (newPublicationsCount > 0 && ($.inArray('Manager', roles) != -1)) { - new_publications_count = newPublicationsCount; - if (newSurveysCount > 0) { - new_surveys_count = newSurveysCount; - dashboard.updateVariables(nation_version, new_publications_count, new_surveys_count); - $("#newSurvey").click(function () { - document.location.href = "#surveys/for-" + currentBellName; - }); - $('#newSurvey').show(); - } else { - dashboard.updateVariables(nation_version, new_publications_count, 0); - } - $("#newPublication").click(function () { - document.location.href = "#publications/for-" + currentBellName; - }); - // $('#newPublication').attr("onclick",document.location.href+"#publications/for-"+currentBellName); - $('#newPublication').show(); - } else { - if (newSurveysCount > 0 && ($.inArray('Manager', roles) != -1)) { - new_surveys_count = newSurveysCount; - dashboard.updateVariables(nation_version, 0, new_surveys_count); - $("#newSurvey").click(function () { - document.location.href = "#surveys/for-" + currentBellName; + } + if(App.configuration.get('type')!='nation'){ + if (newPublicationsCount > 0 && ($.inArray('Manager', roles) != -1)) { + new_publications_count = newPublicationsCount; + if (newSurveysCount > 0) { + new_surveys_count = newSurveysCount; + dashboard.updateVariables(nation_version, new_publications_count, new_surveys_count); + $("#newSurvey").click(function () { + document.location.href = "#surveys/for-" + currentBellName; + }); + $('#newSurvey').show(); + } else { + dashboard.updateVariables(nation_version, new_publications_count, 0); + } + $("#newPublication").click(function () { + document.location.href = "#publications/for-" + currentBellName; }); - $('#newSurvey').show(); + // $('#newPublication').attr("onclick",document.location.href+"#publications/for-"+currentBellName); + $('#newPublication').show(); } else { - dashboard.updateVariables(nation_version, 0, 0); + if (newSurveysCount > 0 && ($.inArray('Manager', roles) != -1)) { + new_surveys_count = newSurveysCount; + dashboard.updateVariables(nation_version, 0, new_surveys_count); + $("#newSurvey").click(function () { + document.location.href = "#surveys/for-" + currentBellName; + }); + $('#newSurvey').show(); + } else { + dashboard.updateVariables(nation_version, 0, 0); + } } } }, @@ -1194,16 +1205,18 @@ $(function() { } }); } else { - if (newPublicationsCount > 0 && ($.inArray('Manager', roles) != -1)) { - new_publications_count = newPublicationsCount; - dashboard.updateVariables(nation_version, new_publications_count, 0); - $("#newPublication").click(function () { - document.location.href = "#publications/for-" + currentBellName; - }); - $('#newPublication').show(); - } - else { - dashboard.updateVariables(nation_version, 0, 0); + if(App.configuration.get('type')!='nation'){ + if (newPublicationsCount > 0 && ($.inArray('Manager', roles) != -1)) { + new_publications_count = newPublicationsCount; + dashboard.updateVariables(nation_version, new_publications_count, 0); + $("#newPublication").click(function () { + document.location.href = "#publications/for-" + currentBellName; + }); + $('#newPublication').show(); + } + else { + dashboard.updateVariables(nation_version, 0, 0); + } } } }, @@ -1243,7 +1256,134 @@ $(function() { timeout: 4000 }); }, + getNationPublication: function (dashboard) { + var centralNationUrl = getCentralNationUrl(); + var newPublicationsCount = 0; + $.ajax({ + url:'http://' + centralNationUrl + '/publicationdistribution/_design/bell/_view/getNationPublications?include_docs=true&key=["' + App.configuration.get('nationName') + '",' + false + ']', + type: 'GET', + dataType: 'jsonp', + async: false, + success: function (json) { + var publicationDistribDocsFromNation = [], + tempKeys = []; + _.each(json.rows, function (row) { + publicationDistribDocsFromNation.push(row.doc); + tempKeys.push(row.doc.publicationId); + }); + // fetch all publications from local/community server to see how many of the publications from nation are new ones + var publicationCollection = new App.Collections.Publication(); + var tempUrl = App.Server + '/publications/_design/bell/_view/allPublication?include_docs=true'; + publicationCollection.setUrl(tempUrl); + publicationCollection.fetch({ + success: function () { + var alreadySyncedPublications = publicationCollection.models; + for (var i in publicationDistribDocsFromNation) { + // if this publication doc exists in the list of docs fetched from nation then ignore it from new publications + // count + var index = alreadySyncedPublications.map(function (element) { + return element.get('_id'); + }).indexOf(publicationDistribDocsFromNation[i].publicationId); + if (index > -1) {//code here + /* var pubId = publicationDistribDocsFromNation[i].publicationId; + + var nationUrl = 'http://' + publicationDistribDocsFromNation[i].createrNationName + ':' + App.password + '@' + publicationDistribDocsFromNation[i].createrNationUrl + + '/publications/' + pubId; + $.ajax({ + url: nationUrl, + type: 'GET', + dataType: 'jsonp', + success: function (publicationDoc) { + if(publicationDoc.downloadedByNation && publicationDoc.downloadedByNation != undefined) { + if(publicationDoc.downloadedByNation.indexOf(currentBellName) == -1) { + publicationDoc.downloadedByNation.push(currentBellName); + console.log(publicationDoc.downloadedByNation) + var courseModel = new App.Models.Publication({ + _id: publicationDoc._id + }) + courseModel.fetch({ + success: function (model) { + model.destroy(); + $.couch.db("temppublication").create({ + success: function (data) { + $.couch.db("temppublication").saveDoc(publicationDoc, { + success: function (response) { + $.couch.replicate("temppublication", "publications"); + $.ajax({ + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json; charset=utf-8' + }, + type: 'POST', + url: '/_replicate', + dataType: 'json', + data: JSON.stringify({ + "source": "temppublication", + "target": 'http://' + App.configuration.get('nationName') + ':oleoleole@' + App.configuration.get('nationUrl') + '/publications', + "doc_ids": [publicationDoc._id] + }), + success: function (response) { + $.couch.db("temppublication").drop({ + success: function(data) { + }, + error: function(status) { + console.log(status); + } + }); + }, + error: function (res) { + console.log(res); + } + }); + }, + error: function (jqXHR, textStatus, errorThrown) { + console.log(errorThrown); + $.couch.db("temppublication").drop({ + success: function(data) { + }, + error: function(status) { + console.log(status); + } + }); + } + }); + } + }); + + }, + async: false + }); + } + } + }, + error: function(jqXHR, status, errorThrown){ + console.log(status); + } + });*/ + // don't increment newPublicationsCount cuz this publicationId already exists in the already synced publications at + // local server + } else { + newPublicationsCount++; + } + } + + if (newPublicationsCount > 0) { + new_publications_count = newPublicationsCount; + dashboard.updateVariables("", new_publications_count, 0); + $("#newPublication").click(function () { + document.location.href = "#nationpublications/for-" + App.configuration.get('nationName'); + }); + // $('#newPublication').attr("onclick",document.location.href+"#publications/for-"+currentBellName); + $('#newPublication').show(); + } }, + error: function (jqXHR, status, errorThrown) { + $('#onlineButton').css({"background-color": "#ff0000"}); + $('#onlineButton').attr("title", App.languageDict.get("Nation_InVisible")); + } + }); + } }) +}, Dashboard: function() { if(App.configuration.get('type')=='nation'){ getAllPendingRequests(); @@ -1338,6 +1478,9 @@ $(function() { } dashboard.$el.length=0; that.getNationVersion(dashboard); + if(App.configuration.get('type')=='nation'){ + that.getNationPublication(dashboard); + } $('#olelogo').remove(); applyCorrectStylingSheet(directionOfLang); }, diff --git a/app/MyApp/app/views/NationPublicationTable.js b/app/MyApp/app/views/NationPublicationTable.js new file mode 100644 index 000000000..0d4e6e44a --- /dev/null +++ b/app/MyApp/app/views/NationPublicationTable.js @@ -0,0 +1,595 @@ + $(function () { + + App.Views.NationPublicationTable = Backbone.View.extend({ + authorName: null, + tagName: "table", + className: "table table-striped", + collectionInfo:[], + add: function (publicationDistribID, model, isAlreadySynced) { + // carry the publication in a variable global to this (PublicationTable) view for use in event handling + this.collectionInfo[model._id]= model; //[model.resources,model.courses,model.IssueNo] + if (isAlreadySynced) { + this.$el.append('' + model.IssueNo+ ''+App.languageDict.attributes.Sync_Publication+''); + } else { + this.$el.append('' + model.IssueNo+ ''+App.languageDict.attributes.Sync_Publication+''); + } + }, + events:{ + "click .synPublication": 'synPublication' + }, + render: function () { + this.$el.html(''+App.languageDict.attributes.IssueNumber+''+App.languageDict.get("action")+''); + var that=this; + var centralNationUrl = getCentralNationUrl(); + var currentNationName = App.configuration.get('nationName'), + nationPassword = App.password; + var currentNationUrl = App.configuration.get('nationUrl') + var DbUrl = 'http://' + centralNationUrl + '/publicationdistribution/_design/bell/_view/getNationPublications?include_docs=true&key=["' + currentNationName + '",' + false + ']'; + //#113 reverse pubs order + // make sure the couchdb that is being requested in this ajax call has its 'allow_jsonp' property set to true in the + // 'httpd' section of couchdb configurations. Otherwise, the server couchdb will not respond as required by jsonp format + // to send publication-distribution records from nation whose 'viewed' property is false + $.ajax({ + url: DbUrl, + type: 'GET', + dataType: 'jsonp', + success: function (json) { + var keys=''; + var publicationToPubDistributionMap = {}; + _.each(json.rows,function(row){ + publicationToPubDistributionMap[row.doc.publicationId] = row.doc._id; + keys += '"' + row.doc.publicationId + '",'; + }); + /*if (keys != '') { + keys = keys.substring(0, keys.length - 1); + console.log(keys)*/ + _.each(json.rows,function(row){ + var pubsForCommunityUrl = 'http://' + row.doc.createrNationName + ':' + nationPassword + '@' + row.doc.createrNationUrl + + '/publications/_all_docs?include_docs=true&keys=["' + row.doc.publicationId + '"]'; + $.ajax({ + url: pubsForCommunityUrl, + type: 'GET', + dataType: 'jsonp', + success: function (jsonNationPublications) { + // fetch all publications from local/community server to see how many of the publications from nation are new ones + var publicationCollection = new App.Collections.Publication(); + var tempUrl = App.Server + '/publications/_design/bell/_view/allPublication?include_docs=true'; + publicationCollection.setUrl(tempUrl); + publicationCollection.fetch({ + success: function () { + var syncedPublication = []; + var newPublication = []; + _.each(jsonNationPublications.rows,function(row){ + var publicationFromNation = row.doc; + var alreadySyncedPublications = publicationCollection.models; + var index = alreadySyncedPublications.map(function(element) { + return element.get('_id'); + }).indexOf(publicationFromNation._id); + var nationPublicationDistributionDocId = publicationToPubDistributionMap[publicationFromNation._id]; + var isAlreadySynced = false; + if (index > -1) { // its a new or yet-to-be-synced publication from nation, so display it as new + isAlreadySynced = true; + var temp = { "pubDistId":nationPublicationDistributionDocId, "pubDoc":publicationFromNation, "isAlreadySynced":isAlreadySynced, "IssueNo":publicationFromNation.IssueNo }; + syncedPublication.push(temp); + } else { // its an already synced publication. display it without the new/unsynced mark + var temp = { "pubDistId":nationPublicationDistributionDocId, "pubDoc":publicationFromNation, "isAlreadySynced":isAlreadySynced, "IssueNo":publicationFromNation.IssueNo }; + newPublication.push(temp); + } + }); + // + newPublication.sort(that.sortByProperty('IssueNo')); + syncedPublication.sort(that.sortByPropertyInDecreasingOrder('IssueNo')); + // + for (var i = 0; i b[property]) { + sortStatus = 1; + } + + return sortStatus; + }; + }, + sortByPropertyInDecreasingOrder: function(property) { + 'use strict'; + return function (a, b) { + var sortStatus = 0; + if (a[property] < b[property]) { + sortStatus = 1; + } else if (a[property] > b[property]) { + sortStatus = -1; + } + return sortStatus; + }; + }, + + synPublication:function(e){ + var that = this; + var pubId = e.currentTarget.name; + var pubDistributionID = $(e.currentTarget).closest('tr').attr('id'); + var publicationToSync = this.collectionInfo[pubId]; + $.couch.allDbs({ + success: function(data) { + if(data.indexOf('tempresources') != -1 ) { + $.couch.db("tempresources").drop({ + success: function(data) { + that.syncCourses(pubDistributionID, publicationToSync); + }, + error: function(status) { + console.log(status); + } + }); + } else { + that.syncCourses(pubDistributionID, publicationToSync); + } + }, + async: false + }); + }, + + syncCourses:function(pubDistributionID, publicationToSync){ + var resourcesIdes = publicationToSync.resources, + courses = publicationToSync.courses, + IssueNo = publicationToSync.IssueNo; + var nationUrl = publicationToSync.createrNationUrl, + nationName = publicationToSync.createrNationName; + // courses contains courseID and stepIDs(which contains stepID and resouceIDs(which contains ids of resources in the step)) + var cumulativeCourseIDs = [], + cumulativeCourseStepIDs = [], + cumulativeResourceIDs = []; + cumulativeQuestionIDs = []; + for (var indexOfCourse in courses) { + var courseInfo = courses[indexOfCourse]; + cumulativeCourseIDs.push(courseInfo['courseID']); + var courseSteps = courseInfo['stepIDs']; + for (var indexOfCourseStep in courseSteps) { + var courseStepInfo = courseSteps[indexOfCourseStep]; + cumulativeCourseStepIDs.push(courseStepInfo['stepID']); + var resourceIDs = courseStepInfo['resourceIDs']; + for (var indexOfResourceID in resourceIDs) { + cumulativeResourceIDs.push(resourceIDs[indexOfResourceID]); + } + var questionIDs = courseStepInfo['questionIDs']; + for (var indexOfQuestionID in questionIDs) { + cumulativeQuestionIDs.push(questionIDs[indexOfQuestionID]); + } + } + } + for (var indexOfNonCourseResourceID in resourcesIdes) { + cumulativeResourceIDs.push(resourcesIdes[indexOfNonCourseResourceID]); + } + App.startActivityIndicator(); + var that=this; + $.couch.login({ + name: App.configuration.get('nationName'), + password: App.password, + success: function(data) { + $.couch.db("tempresources").create({ + success: function(data) { + $.ajax({ + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json; charset=utf-8' + }, + type: 'POST', + url: '/_replicate', + dataType: 'json', + data: JSON.stringify({ + "source": 'http://'+ nationName +':'+App.password+'@'+ nationUrl + '/resources', + "target": 'tempresources', + 'doc_ids': cumulativeResourceIDs + }), + async: false, + success: function (response) { + //Resource Rating work here. + $.ajax({ + url: '/tempresources/_all_docs?include_docs=true', + type: 'GET', + dataType: 'json', + success: function (resResult) { + var result = resResult.rows; + var tempResult = []; + for (var i = 0; i>Community Undefined user created + that.removeLeaderAndMemberDetails(cumulativeCourseIDs); + $.ajax({ + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json; charset=utf-8' + }, + type: 'POST', + url: '/_replicate', + dataType: 'json', + data: JSON.stringify({ + "source": 'http://'+ nationName +':'+App.password+'@'+ nationUrl + '/coursestep', + "target": 'coursestep', + 'doc_ids': cumulativeCourseStepIDs + }), + success: function (response) { + $.ajax({ + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json; charset=utf-8' + }, + type: 'POST', + url: '/_replicate', + dataType: 'json', + data: JSON.stringify({ + "source": 'http://'+ nationName +':'+App.password+'@'+ nationUrl + '/coursequestion', + "target": 'coursequestion', + 'doc_ids': cumulativeQuestionIDs + }), + success: function (response) { + var nation_Url = 'http://' + nationName + ':' + App.password + '@' + nationUrl + + '/publications/' + publicationToSync._id; + + $.ajax({ + url: nation_Url, + type: 'GET', + dataType: 'jsonp', + success: function (publicationDoc) { + //put here + // mark this publication as synced at community couchdb.We are informing the nation about it as well + // so that nations can see which communities have successfully downladed the publication. + //If the publication is already synced then no need to save it again in the db's, that's why assigning + // appropriate value to the isAlreadyExist variable. + console.log(App.configuration.get('name')); + var isAlreadyExist = true; + if(publicationDoc.downloadedByNation && publicationDoc.downloadedByNation != undefined) { + if(publicationDoc.downloadedByNation.indexOf(App.configuration.get('name')) == -1) { + publicationDoc.downloadedByCommunities.push(App.configuration.get('name')); + isAlreadyExist = false; + } + } + //If a publication doc does not contain the downloadedByCommunities field then it means that this + // publication was before Issue#48 implementation, so saving it in the db to maintain the value + // of "synced/not synced" for the older publications too. + if(isAlreadyExist == false || publicationDoc.downloadedByCommunities == undefined) { + $.couch.db("publications").saveDoc(publicationDoc, { + success: function (response) { + console.log("adding publication# " + publicationDoc.IssueNo + " doc at community for bookkeeping"); + $.ajax({ + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json; charset=utf-8' + }, + type: 'POST', + url: '/_replicate', + dataType: 'json', + data: JSON.stringify({ + "source": "publications", + "target": 'http://' + nationName + ':oleoleole@' + nationUrl + '/publications', + "doc_ids": [publicationDoc._id] + }), + success: function (response) { + }, + error: function (res) { + console.log(res); + } + }); + }, + error: function (jqXHR, textStatus, errorThrown) { + console.log(errorThrown); + } + }); + } + //My code for lastPublicationsSyncDate + // Update lastPublicationsSyncDate at Nation's Community Records + /* var communitycode = App.configuration.get('code'); + $.ajax({ + url:'http://' + nationName + ':oleoleole@' +nationUrl= + '/community/_design/bell/_view/getCommunityByCode?_include_docstrue', + type: 'GET', + dataType: 'jsonp', + success: function(result){ + var doc, communityModelId; + for(var i = 0 ; i < result.rows.length ; i++) { + var code; + if(result.rows[i].value.Code != undefined){ + code = result.rows[i].value.Code; + } else { + code = result.rows[i].value.code; + } + if(communitycode == code) { + doc = result.rows[i].value; + } + } + if(doc != undefined) { + communityModelId = doc._id; + } + //Replicate from Nation to Community + $.ajax({ + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json; charset=utf-8' + }, + type: 'POST', + url: '/_replicate', + dataType: 'json', + data: JSON.stringify({ + "source": 'http://' + nationName + ':oleoleole@' + nationUrl + '/community', + "target": "community", + "doc_ids": [communityModelId] + }), + success: function(response){ + var date = new Date(); + var year = date.getFullYear(); + var month = (1 + date.getMonth()).toString(); + month = month.length > 1 ? month : '0' + month; + var day = date.getDate().toString(); + day = day.length > 1 ? day : '0' + day; + var formattedDate = month + '-' + day + '-' + year; + $.ajax({ + url: '/community/_design/bell/_view/getCommunityByCode?_include_docs=true', + type: 'GET', + dataType: 'json', + success: function (res) { + if (res.rows.length > 0) { + var communityModel; + for(var i = 0 ; i < result.rows.length ; i++) { + var code; + if(result.rows[i].value.Code != undefined){ + code = result.rows[i].value.Code; + } else { + code = result.rows[i].value.code; + } + if(communitycode == code) { + communityModel = result.rows[i].value; + } + } + if(communityModel != undefined) { + communityModel.lastPublicationsSyncDate = month + '/' + day + '/' + year; + } + //Update the record in Community db at Community Level + $.ajax({ + + headers: { + 'Accept': 'application/json', + 'Content-Type': 'multipart/form-data' + }, + type: 'PUT', + url: App.Server + '/community/' + communityModelId + '?rev=' + communityModel._rev, + dataType: 'json', + data: JSON.stringify(communityModel), + success: function (response) { + //Replicate from Community to Nation + $.ajax({ + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json; charset=utf-8' + }, + type: 'POST', + url: '/_replicate', + dataType: 'json', + data: JSON.stringify({ + "source": "community", + "target": 'http://' + App.configuration.get('nationName') + ':oleoleole@' + App.configuration.get('nationUrl') + '/community', + "doc_ids": [communityModelId] + }), + success: function(response){ + alert(App.languageDict.attributes.Pubs_Replicated_Success) + App.stopActivityIndicator(); + }, + async: false + }); + }, + + async: false + }); + } + } + }); + }, + async: false + }); + }, + error: function(err){ + console.log(err); + } + });*/ + //End of my code. + }, + error: function(jqXHR, status, errorThrown){ + console.log(status); + } + }); + }, + error: function(jqXHR, status, errorThrown){ + console.log(status); + } + }); + }, + error: function(jqXHR, status, errorThrown){ + console.log(status); + App.stopActivityIndicator(); + alert(App.languageDict.attributes.CourseSteps_Synced_Error); + } + }); + }, + error: function(jqXHR, status, errorThrown){ + console.log(status); + App.stopActivityIndicator(); + alert(App.languageDict.attributes.Courses_Synced_Error); + } + }) + }, + error: function(jqXHR, status, errorThrown){ + console.log(status); + $.couch.db("tempresources").drop({ + success: function(data) { + }, + error: function(status) { + console.log(status); + } + }); + App.stopActivityIndicator(); + alert(App.languageDict.attributes.Resources_Synced_Error); + } + }); + } + }); + }, + error: function(status) { + console.log(status); + } + }); + + }, + removeLeaderAndMemberDetails: function(cumulativeCourseIDs){ + var courseInPubIdes = '', courseData=[]; + _.each(cumulativeCourseIDs, function(item) { + courseInPubIdes += '"' + item + '",' + }) + if (courseInPubIdes != ''){ + courseInPubIdes = courseInPubIdes.substring(0, courseInPubIdes.length - 1); + } + var courseColl = new App.Collections.Courses(); + courseColl.keys = encodeURI(courseInPubIdes) + courseColl.fetch({ + async: false + }); + for(var i = 0 ; i + @@ -566,6 +567,7 @@
<%= languageDict.attributes.Set_Password %>
<% if(type == "nation") {%> + <%} else {%> diff --git a/app/nation/app/Router.js b/app/nation/app/Router.js index 5bbefcdf4..f9e8137bb 100644 --- a/app/nation/app/Router.js +++ b/app/nation/app/Router.js @@ -4284,7 +4284,7 @@ $(function() { var lang = App.Router.getLanguage(loginOfMem); App.languageDictValue=App.Router.loadLanguageDocs(lang); App.$el.children('.body').html('
'); - $('#parentDiv').append('
'+App.languageDictValue.get('IssueNumber')+ ' '+ publicationObject.get('IssueNo') + '
'+App.languageDictValue.get('Add_Course')+' '+App.languageDictValue.get('Add_Resource')+'
') + $('#parentDiv').append('
'+App.languageDictValue.get('IssueNumber')+ ' '+ publicationObject.get('IssueNo') + '
'+App.languageDictValue.get('Add_Course')+' '+App.languageDictValue.get('Add_Resource')+'
') if( publicationObject.attributes.autoPublication == undefined || publicationObject.attributes.autoPublication == false){ $('#parentChildDiv').append(" "+App.languageDictValue.get('Enable_Auto_Sync')+""); }else{ @@ -4477,6 +4477,36 @@ $(function() { } }, + SelectNations: function(pId) { + var centralNationUrl = App.Router.getCentralNationUrl(); + $('#invitationdiv').fadeIn(1000) + var inviteForm = new App.Views.listNationView() + inviteForm.pId = pId + inviteForm.render() + $('#invitationdiv').html(' ') + $('#invitationdiv').append(inviteForm.el) + var nationUrl = App.configuration.get('nationUrl'); + if(navigator.onLine){ //Check if there is a stable internet connection + $.ajax({ + url: 'http://' + centralNationUrl + '/nations/_design/bell/_view/getAllNations?_include_docs=true', + type: 'GET', + dataType: 'jsonp', + async: false, + success: function (json) { + for(var i = 0 ; i < json.rows.length ; i++) { + if(json.rows[i].value.nationurl != nationUrl) + $('#nationSelect').append("") + } + } + }); + } + $('#addQuestion').css('pointer-events','none'); + if(App.languageDictValue.get('directionOfLang').toLowerCase()==="right") + { + $('#invitationdiv').css({"direction":"rtl"}); + } + }, + criteriaList: function(surveyId) { var loginOfMem = $.cookie('Member.login'); var lang = App.Router.getLanguage(loginOfMem); diff --git a/app/nation/app/views/listNationView.js b/app/nation/app/views/listNationView.js new file mode 100644 index 000000000..51036b2f4 --- /dev/null +++ b/app/nation/app/views/listNationView.js @@ -0,0 +1,149 @@ +$(function() { + + App.Views.listNationView = Backbone.View.extend({ + + id: "invitationForm", + + events: { + "click #cancelButton": "hidediv", + "click #formButton": "syncData" + }, + hidediv: function() { + $('#invitationdiv').fadeOut(1000) + + setTimeout(function() { + $('#invitationdiv').hide() + }, 1000); + $('#addQuestion').css('pointer-events','auto'); + }, + render: function() { + var loginOfMem = $.cookie('Member.login'); + var lang = App.Router.getLanguage(loginOfMem); + App.languageDictValue=App.Router.loadLanguageDocs(lang); + var $button = $('
'+App.languageDictValue.get('Select_Nations')+'


'+App.languageDictValue.get('Send')+'') + this.$el.append($button) + this.$el.append('     ') + this.$el.append(''+App.languageDictValue.get('Cancel')+'') + }, + syncData: function() { + var loginOfMem = $.cookie('Member.login'); + var lang = App.Router.getLanguage(loginOfMem); + var languageDictValue=App.Router.loadLanguageDocs(lang); + var selectedValues = $('#nationSelect').val(); + if (!selectedValues) { + alert(languageDictValue.attributes.Prompt_Nation_First) + return + } + var that = this; + if (that.pId != undefined && that.pId != null) { + if (that.pId) { + that.syncPublicationsData(that.pId, selectedValues); + } + } + }, + syncPublicationsData: function(p_id, selectedValues) { + var that = this + var loginOfMem = $.cookie('Member.login'); + var lang = App.Router.getLanguage(loginOfMem); + var languageDictValue=App.Router.loadLanguageDocs(lang); + App.startActivityIndicator() + var sendPub = new Array() + //******if starts******************************************** + if (selectedValues.length > 0) { + //******for loop start************* + var pubResult; + var i; + var selectedNations = []; + for (i = 0; i < selectedValues.length; i++) { + sendPub.splice(0, sendPub.length); + var nName = $("#nationSelect option[value='" + selectedValues[i] + "']").text() + var x = p_id; + //*********************************************************** + //extra code for #100 + $.ajax({ + url: '/publications/_design/bell/_view/publicationById?include_docs=true&key="' + x + '"', + type: 'GET', + dataType: 'json', + success: function(resResult) { + pubResult = resResult.rows[0]; + if(pubResult.doc.nationNames == undefined){ + pubResult.doc.nationNames = [] + } + if (pubResult.doc.nationNames != [] && pubResult.doc.nationNames.length > 0 && pubResult.doc.nationNames.indexOf(nName) > -1) { + alert( languageDictValue.attributes.Already_Pub_Sent+' '+ nName); + } else { + selectedNations.push(nName); + sendPub.push({ + nationName: nName, + publicationId: p_id, + Viewed: false, + createrNationUrl: App.configuration.get('nationUrl'), + createrNationName: App.configuration.get('nationName') + }) + $.couch.db("publicationdistribution").bulkSave({ + "docs": sendPub + }, { + success: function(data) { + var centralNationUrl = App.Router.getCentralNationUrl(); + that.synchPubNationWithCenter(centralNationUrl); + }, + error: function(status) { + console.log(status); + }, + async: false + }); + } + + }, + async: false + }); + } + if(i == selectedValues.length && selectedNations && selectedNations.length > 0) { + pubResult.doc["downloadedByNation"] = []; + pubResult.doc["createrNationUrl"] = App.configuration.get('nationUrl'); + pubResult.doc["createrNationName"] = App.configuration.get('nationName'); + for(var j = 0 ; j < selectedNations.length ; j++) { + pubResult.doc.nationNames.push(selectedNations[j]); + } + $.couch.db("publications").saveDoc(pubResult.doc, { + success: function(data) { + + }, + error: function(status) { + console.log(status); + }, + async: false + }); + } + + //******for loop ends****************************** + $("#list option[value='2']").text() + $('#invitationdiv').fadeOut(1000) + setTimeout(function() { + $('#invitationdiv').hide() + }, 1000); + App.stopActivityIndicator() + } + }, + + synchPubNationWithCenter: function(centralNationUrl) { + $.ajax({ + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json; charset=utf-8' + }, + type: 'POST', + url: '/_replicate', + dataType: 'json', + data: JSON.stringify({ + "source": "publicationdistribution", + "target": 'http://' + centralNationUrl + '/publicationdistribution' + }), + success: function(response) { + }, + async: false + }) + } + }) + +}) diff --git a/app/nation/index.html b/app/nation/index.html index 911abf73d..6c2149646 100644 --- a/app/nation/index.html +++ b/app/nation/index.html @@ -159,6 +159,8 @@ + +