Conditionally Show Form or Edit in Place View in Parent View

Project Requirement

Jeremy Knauff, owner of Spartan Media in Tampa, Florida, is developing an expert SaaS (Software as a Service) business application with Formidable Forms. The yet to be revealed product is designed to nurture relationships between the application's subscribers and its media contacts.

To nurture their relationships, SaaS subscribers designate the type of relationship they have with their media contacts. Relationship types can include any or all of the following checkbox selections: Associate, Collaborator, Friend, and Relationship Target.

Each subscriber can have relationships with many media contacts, but are limited to one relationship record per media contact. Media contacts, on the other hand, can have relationships with many subscribers.

This means that each subscriber has a one-to-one relationship with a media contact, but the media contact has a one-to-many relationship with subscribers. This simple UML use case diagram, visualizes the requirement.

Megaphone Use Case UML diagram
Saas Relationship Use Case

Individual relationships are viewable only by the logged in users that created them and system admins. While anyone can still see the media contacts view without being logged in, to view/edit the relationship status, they must be logged in.

Challenge

These requirements are challenging to implement and raise several questions:

  1. How do we enforce a one-to-many and many-to-one relationship at the same time?
  2. If a subscriber has not created a relationship record with the media contact, how is a new record added from the media contact view?
  3. If a subscriber has created a relationship record, how can that record be edited from the media contact view.
  4. How do we prevent the display of personal relationship records to the general public when not logged in?

All of these challenges must be realized in a single view.

Result

The Media contacts view lists all contacts:

Media Contacts View

This is a standard Formidable View. Clicking on a media contact name results in one of three responses:

Response #1

User has an existing relationship with this media contact

This is the view seen by a subscriber that has defined an existing relationship with this media contact. Clicking the Relationship Type Edit opens an edit in place screen for this field.

Relationship Type Edit in Place

Response #2

The second possibility is that a logged in user has not yet entered a relationship type for this media contact, in which case the user is greeted with the relationship form in add mode:

Response #3

The third possible response is presented to anyone not logged into the system:

Implementation

All three of the expected responses are produced by the Media Contact view using custom code that replaces a placeholder string through the frm_display_entry_content filter. Here is the code.

The string being replaced in the Media Contact view is [maybe_add_relationship]. This is not a shortcode. It is simply text that will be replaced with one of the three possible results depending on the conditions met in the media_contact_relationships() function.

Relationship Form

The relationship form ties the media contact and subscribers together with a single-record->media-contact per subscriber. The form definition is very simple, consisting of four fields:

Relationship Form Definition

Relationship Type is a checkbox field formatted as buttons.

Contact Entry ID is the entry id of the media contact's record from the Media Contacts form

User ID (numeric value for view) is the subscriber's WordPress user ID in numeric format.

User ID (Formidable defined field) is the default Formidable user id field that displays the user name.

Reader Interactions

Leave a Reply

Your email address will not be published.