Skip to content

Commit

Permalink
Generate place forms if place-types.json is supplied
Browse files Browse the repository at this point in the history
Closes #4
  • Loading branch information
alxndrsn committed Aug 26, 2017
1 parent a29c4a9 commit a3d5353
Show file tree
Hide file tree
Showing 44 changed files with 7,414 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "medic-configurer-beta",
"version": "1.5.5",
"version": "1.5.6",
"description": "Configure Medic Mobile deployments",
"main": "index.js",
"scripts": {
Expand Down
34 changes: 29 additions & 5 deletions src/fn/convert-contact-forms.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
const convertForms = require('../lib/convert-forms');
const fs = require('../lib/sync-fs');

module.exports = (projectDir, couchUrl, extras) => convertForms(projectDir, 'contact', {
enketo: true,
force_data_node: 'data',
forms: extras,
});
module.exports = (projectDir, couchUrl, extras) => {

const dir = `${projectDir}/forms/contact`;
const placeTypesJson = `${dir}/place-types.json`;

let PLACE_TYPES;
if(fs.exists(placeTypesJson)) {
PLACE_TYPES = fs.readJson(placeTypesJson);
Object.keys(PLACE_TYPES)
.forEach(type => {
fs.copy(`${dir}/PLACE_TYPE-create.xlsx`, `${dir}/${type}-create.xlsx`);
fs.copy(`${dir}/PLACE_TYPE-edit.xlsx`, `${dir}/${type}-edit.xlsx`);
});
}

return convertForms(projectDir, 'contact', {
enketo: true,
force_data_node: 'data',
forms: extras,
transformer: (xml, path) => {
const type = path.replace(/.*\/(.*)-(create|edit)\.xml/, '$1');
return !PLACE_TYPES ? xml : xml
.replace(/PLACE_TYPE/g, type)
.replace(/PLACE_NAME/g, PLACE_TYPES[type]);
},
});

};
9 changes: 6 additions & 3 deletions src/lib/convert-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = (projectDir, subDirectory, options) => {
return fs.readdir(formsDir)
.filter(name => name.endsWith('.xlsx'))
.filter(name => !name.startsWith('~$')) // ignore Excel "owner files"
.filter(name => name !== 'PLACE_TYPE-create.xlsx' && name !== 'PLACE_TYPE-edit.xlsx')
.filter(name => !options.forms || options.forms.includes(fs.withoutExtension(name)))
.reduce((promiseChain, xls) => {
const originalSourcePath = `${formsDir}/${xls}`;
Expand All @@ -47,8 +48,7 @@ module.exports = (projectDir, subDirectory, options) => {
.then(() => info('Converting form', originalSourcePath, '…'))
.then(() => xls2xform(sourcePath, targetPath))
.then(() => getHiddenFields(`${fs.withoutExtension(originalSourcePath)}.properties.json`))
.then(hiddenFields => fixXml(targetPath, hiddenFields, options.enketo))
.then(() => options.transformer && fs.write(targetPath, options.transformer(fs.read(targetPath))))
.then(hiddenFields => fixXml(targetPath, hiddenFields, options.transformer, options.enketo))
.then(() => trace('Converted form', originalSourcePath));
},
Promise.resolve());
Expand All @@ -66,7 +66,7 @@ const xls2xform = (sourcePath, targetPath) =>

// FIXME here we fix the form content in arcane ways. Seeing as we have out own
// fork of pyxform, we should probably be doing this fixing there.
const fixXml = (path, hiddenFields, enketo) => {
const fixXml = (path, hiddenFields, transformer, enketo) => {
// TODO This is not how you should modify XML
let xml = fs.read(path)

Expand All @@ -92,9 +92,12 @@ const fixXml = (path, hiddenFields, enketo) => {
xml = xml.replace(r, '<$1 tag="hidden"$2>');
}

if(transformer) xml = transformer(xml, path);

// The ordering of elements in the <model> has an arcane affect on the
// order that docs are saved in the database when processing a form.
// Move the main doc's element down to the bottom.
// For templated PLACE_TYPE forms, shifting must be done _after_ templating.
xml = shiftThingsAroundInTheModel(path, xml);

fs.write(path, xml);
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit a3d5353

Please sign in to comment.