diff --git a/examples/testimonials/modules/@apostrophecms/form/index.js b/examples/testimonials/modules/@apostrophecms/form/index.js new file mode 100644 index 0000000..743199a --- /dev/null +++ b/examples/testimonials/modules/@apostrophecms/form/index.js @@ -0,0 +1,13 @@ +// Extends form object to capture submission events and +// automagically create other piece types +module.exports = { + handlers (self) { + return { + submission: { + async createTestimonial (req, form, data) { + self.apos.modules.testimonial.addTestimony(req, data) + } + } + } + } +} \ No newline at end of file diff --git a/examples/testimonials/modules/testimonial/index.js b/examples/testimonials/modules/testimonial/index.js new file mode 100644 index 0000000..2c1bc7e --- /dev/null +++ b/examples/testimonials/modules/testimonial/index.js @@ -0,0 +1,180 @@ +// modules/article/index.js +module.exports = { + extend: "@apostrophecms/piece-type", + options: { + label: "Testimonial", + pluralLabel: "Testimonials", + }, + fields: { + add: { + names: { + label: "Names", + type: "string", + }, + emailaddress: { + label: "Email address", + type: "string", + }, + phonenumber: { + label: "Phone Number", + type: "string", + }, + testimonial: { + label: "Testimonial", + type: "string", + }, + helpneeded: { + label: "Help needed", + type: "string", + }, + photo: { + // Photos are not uploaded via the website form to avoid upload issues, but can be added later + label: "Photo", + type: "area", + options: { + max: 1, + widgets: { + "@apostrophecms/image": {}, + }, + }, + required: false, + }, + active: { + label: "Active", + type: "boolean", + help: "Active testimonials can be viewed on the public website", + }, + }, + group: { + basics: { + label: "Basics", + fields: [ + "title", + "names", + "emailaddress", + "phonenumber", + "testimonial", + "helpneeded", + ], + }, + advanced: { + label: "Photo", + fields: ["photo"], + }, + utility: { + fields: ["active"], + }, + }, + }, + columns: { + add: { + names: { + label: "Names", + }, + emailaddress: { + label: "Email", + }, + phonenumber: { + label: "Phone", + }, + active: { + label: "Active", + }, + }, + }, + filters: { + add: { + active: { + label: "Active", + inputType: "select", + def: false, + }, + }, + }, + queries(self, query) { + builders: { + return { + builders: { + active: { + // This is our filter to be able to see active or inactive in the manager + def: null, + safeFor: "public", + finalize() { + const active = query.get("active"); + if (active === null) { + return; + } + + if (active) { + query.and({ + active: true, + }); + } else { + query.and({ + active: false, + }); + } + }, + launder(value) { + return self.apos.launder.booleanOrNull(value); + }, + choices() { + return [ + { + value: true, + label: "Active", + }, + { + value: false, + label: "NOT active", + }, + { + value: null, + label: "All items", + }, + ]; + }, + }, + }, + }; + } + }, + handlers(self, options) {}, + components(self) { + return { + // Returning the five most recently created testimonies. + async latest(req, data) { + const articles = await self + .find(req) + .active(true) + .sort({ createdAt: -1 }) + .limit(data.max || 5) + .toArray(); + return { + articles, + }; + }, + }; + }, + methods(self) { + return { + async addTestimony(req, initialInfo) { + // Generate a blank testimony data object. + let newTestimony = self.newInstance(); + // Add our initial information to the object. + newTestimony = { + ...newTestimony, + ...initialInfo, + }; + // Assign some arbitrary title to this piece + newTestimony.title = initialInfo["names"]; + // Set "active" to false - testimonies must be approved before uploading + newTestimony.active = false; + Object.assign(newTestimony, { ...initialInfo }); + // Insert the testimonial with the asynchronous `self.insert` method + const insertResult = await self.insert(req, newTestimony); + return insertResult; + }, + }; + }, +}; diff --git a/examples/testimonials/modules/testimonial/views/latest.html b/examples/testimonials/modules/testimonial/views/latest.html new file mode 100644 index 0000000..7165d2d --- /dev/null +++ b/examples/testimonials/modules/testimonial/views/latest.html @@ -0,0 +1,15 @@ +
{{ testimony.testimonial }}
+