Merge pull request #24115 from kcristiano/5.52-token
[civicrm-core.git] / ext / afform / core / ang / af / afForm.component.js
1 (function(angular, $, _) {
2 // Example usage: <af-form ctrl="afform">
3 angular.module('af').component('afForm', {
4 bindings: {
5 ctrl: '@'
6 },
7 controller: function($scope, $element, $timeout, crmApi4, crmStatus, $window, $location, FileUploader) {
8 var schema = {},
9 data = {},
10 status,
11 args,
12 ctrl = this;
13
14 this.$onInit = function() {
15 // This component has no template. It makes its controller available within it by adding it to the parent scope.
16 $scope.$parent[this.ctrl] = this;
17
18 $timeout(ctrl.loadData);
19 };
20
21 this.registerEntity = function registerEntity(entity) {
22 schema[entity.modelName] = entity;
23 data[entity.modelName] = [];
24 };
25 this.getEntity = function getEntity(name) {
26 return schema[name];
27 };
28 // Returns field values for a given entity
29 this.getData = function getData(name) {
30 return data[name];
31 };
32 this.getSchema = function getSchema(name) {
33 return schema[name];
34 };
35 // Returns the 'meta' record ('name', 'description', etc) of the active form.
36 this.getFormMeta = function getFormMeta() {
37 return $scope.$parent.meta;
38 };
39 this.loadData = function() {
40 var toLoad = 0;
41 args = _.assign({}, $scope.$parent.routeParams || {}, $scope.$parent.options || {});
42 _.each(schema, function(entity, entityName) {
43 if (args[entityName] || entity.autofill) {
44 toLoad++;
45 }
46 });
47 if (toLoad) {
48 crmApi4('Afform', 'prefill', {name: ctrl.getFormMeta().name, args: args})
49 .then(function(result) {
50 _.each(result, function(item) {
51 data[item.name] = data[item.name] || {};
52 _.extend(data[item.name], item.values, schema[item.name].data || {});
53 });
54 });
55 }
56 };
57
58 // Used when submitting file fields
59 this.fileUploader = new FileUploader({
60 url: CRM.url('civicrm/ajax/api4/Afform/submitFile'),
61 headers: {'X-Requested-With': 'XMLHttpRequest'},
62 onCompleteAll: postProcess,
63 onBeforeUploadItem: function(item) {
64 status.resolve();
65 status = CRM.status({start: ts('Uploading %1', {1: item.file.name})});
66 }
67 });
68
69 // Called after form is submitted and files are uploaded
70 function postProcess() {
71 var metaData = ctrl.getFormMeta(),
72 dialog = $element.closest('.ui-dialog-content');
73
74 $element.trigger('crmFormSuccess', {
75 afform: metaData,
76 data: data
77 });
78
79 status.resolve();
80 $element.unblock();
81
82 if (dialog.length) {
83 dialog.dialog('close');
84 }
85
86 else if (metaData.redirect) {
87 var url = metaData.redirect;
88 if (url.indexOf('civicrm/') === 0) {
89 url = CRM.url(url);
90 } else if (url.indexOf('/') === 0) {
91 url = $location.protocol() + '://' + $location.host() + url;
92 }
93 $window.location.href = url;
94 }
95 }
96
97 this.submit = function() {
98 status = CRM.status({});
99 $element.block();
100
101 crmApi4('Afform', 'submit', {
102 name: ctrl.getFormMeta().name,
103 args: args,
104 values: data}
105 ).then(function(response) {
106 if (ctrl.fileUploader.getNotUploadedItems().length) {
107 _.each(ctrl.fileUploader.getNotUploadedItems(), function(file) {
108 file.formData.push({
109 params: JSON.stringify(_.extend({
110 token: response[0].token,
111 name: ctrl.getFormMeta().name
112 }, file.crmApiParams()))
113 });
114 });
115 ctrl.fileUploader.uploadAll();
116 } else {
117 postProcess();
118 }
119 });
120 };
121 }
122 });
123 })(angular, CRM.$, CRM._);