diff --git a/app/common/locales/en.json b/app/common/locales/en.json index d183fb2fda..181b30e2d5 100644 --- a/app/common/locales/en.json +++ b/app/common/locales/en.json @@ -11,6 +11,7 @@ "add_field_instructions_info": "These instructions will be presented as a tooltip to a user when filling in this field.", "add_field_instructions_placeholder": "Instructions for field", "add_new_step": "Add New Step", + "field_allowed_relation_post_type": "Allowed post types", "currently_disabled": "This post type is currently disabled.", "custom_structure": "Custom Structure", "default_date_placeholder": "Default date", @@ -148,6 +149,7 @@ "role" : "Role", "roles" : "Roles", "saved_searches" : "Saved Searches", + "search" : "Search", "select_all" : "Select All", "settings" : "Settings", "tags" : "Categories", diff --git a/app/post/directives/post-editor-directive.js b/app/post/directives/post-editor-directive.js index e42068cbb2..444436e07f 100644 --- a/app/post/directives/post-editor-directive.js +++ b/app/post/directives/post-editor-directive.js @@ -281,6 +281,9 @@ function ( $scope.isRadio = function (attr) { return attr.input === 'radio'; }; + $scope.isRelation = function (attr) { + return attr.input === 'relation'; + }; // Can more values be added for this attribute? $scope.canAddValue = function (attr) { diff --git a/app/post/directives/post-relation-directive.js b/app/post/directives/post-relation-directive.js new file mode 100644 index 0000000000..20dafb99bc --- /dev/null +++ b/app/post/directives/post-relation-directive.js @@ -0,0 +1,55 @@ +module.exports = [ + 'PostEndpoint', + '_', +function ( + PostEndpoint, + _ +) { + + return { + restrict: 'E', + replace: true, + scope: { + id: '@', + name: '@', + model: '=', + required: '=', + attribute: '=' + }, + templateUrl: 'templates/posts/relation.html', + link: function ($scope) { + $scope.$watch(function () { + return $scope.model; + }, function (newValue, oldValue) { + if (!$scope.selectedPost || $scope.selectedPost.id !== newValue) { + $scope.selectedPost = PostEndpoint.get({ id: $scope.model }); + } + }); + + $scope.search = function (event) { + event.preventDefault(); + var query = { q : $scope.searchTerm }; + + if ($scope.attribute.config.input && $scope.attribute.config.input.form) { + query.form = $scope.attribute.config.input.form.join(','); + } + + PostEndpoint.query(query).$promise.then(function (response) { + $scope.results = response.results; + }); + }; + + $scope.selectPost = function (post) { + $scope.model = post.id; + $scope.selectedPost = post; + $scope.results = null; + }; + + $scope.clearPost = function () { + $scope.model = null + $scope.selectedPost = null; + }; + } + }; + +}]; diff --git a/app/post/directives/post-value-directive.js b/app/post/directives/post-value-directive.js index 6427c346c7..12aa347303 100644 --- a/app/post/directives/post-value-directive.js +++ b/app/post/directives/post-value-directive.js @@ -1,4 +1,4 @@ -module.exports = [function () { +module.exports = ['PostEndpoint', function (PostEndpoint) { return { restrict: 'E', replace: true, @@ -7,6 +7,13 @@ module.exports = [function () { value: '=', attribute: '=' }, - templateUrl: 'templates/partials/post-detail-value.html' + templateUrl: 'templates/partials/post-detail-value.html', + link: function ($scope) { + if ($scope.attribute.type === 'relation') { + $scope.value = $scope.value.map(function (entry) { + return PostEndpoint.get({ id : entry }); + }); + } + } }; }]; diff --git a/app/post/post-module.js b/app/post/post-module.js index e1842573e1..a965ac90d7 100644 --- a/app/post/post-module.js +++ b/app/post/post-module.js @@ -5,6 +5,7 @@ angular.module('ushahidi.posts', []) .directive('postPreview', require('./directives/post-preview-directive.js')) .directive('postValue', require('./directives/post-value-directive.js')) .directive('postLocation', require('./directives/post-location-directive.js')) +.directive('postRelation', require('./directives/post-relation-directive.js')) // Post editing workflows .directive('postEditor', require('./directives/post-editor-directive.js')) .directive('postChooseForm', require('./directives/post-choose-form-directive.js')) diff --git a/app/post/services/endpoints/post-endpoint.js b/app/post/services/endpoints/post-endpoint.js index 615dd9d964..63bf1f11eb 100644 --- a/app/post/services/endpoints/post-endpoint.js +++ b/app/post/services/endpoints/post-endpoint.js @@ -2,10 +2,12 @@ module.exports = [ '$resource', '$rootScope', 'Util', + '_', function ( $resource, $rootScope, - Util + Util, + _ ) { var PostEndpoint = $resource(Util.apiUrl('/posts/:id/:extra'), { @@ -13,9 +15,17 @@ function ( }, { query: { method: 'GET', - isArray: false, + isArray: false + }, + get: { + method: 'GET', transformResponse: function (data /*, header*/) { - return angular.fromJson(data); + data = angular.fromJson(data); + // Ensure values is always an object + if (_.isArray(data.values)) { + data.values = _.object(data.values); + } + return data; } }, update: { @@ -27,18 +37,12 @@ function ( geojson: { method: 'GET', url: Util.apiUrl('/posts/:id/geojson'), - isArray: false, - transformResponse: function (data /*, header*/) { - return angular.fromJson(data); - } + isArray: false }, stats: { method: 'GET', url: Util.apiUrl('/posts/:id/stats'), - isArray: false, - transformResponse: function (data /*, header*/) { - return angular.fromJson(data); - } + isArray: false } }); diff --git a/app/setting/directives/setting-form-stage-editor-directive.js b/app/setting/directives/setting-form-stage-editor-directive.js index d29a18967f..28df1cab82 100644 --- a/app/setting/directives/setting-form-stage-editor-directive.js +++ b/app/setting/directives/setting-form-stage-editor-directive.js @@ -52,6 +52,12 @@ function ( $scope.currentStageId = currentStageId; }); + // Load available forms for relation fields + $scope.availableForms = FormEndpoint.query(); + $scope.filterNotCurrentForm = function (form) { + return form.id !== $attrs.formId; + }; + // Manage stage settings $scope.isSettingsOpen = false; $scope.openSettings = function () { @@ -150,6 +156,11 @@ function ( label: 'Checkbox', type: 'varchar', input: 'checkbox' + }, + { + label: 'Related Post', + type: 'relation', + input: 'relation' } ]; $scope.isNewAttributeOpen = false; @@ -166,6 +177,7 @@ function ( label: 'New field', required: false, options: [], + config: {}, form_stage_id: $scope.currentStageId }; diff --git a/server/www/templates/partials/form-stage-editor.html b/server/www/templates/partials/form-stage-editor.html index 90f48306b0..a7e47da9ce 100644 --- a/server/www/templates/partials/form-stage-editor.html +++ b/server/www/templates/partials/form-stage-editor.html @@ -66,6 +66,17 @@

{{ attribute.label }}

+
+ + +
+ +