Utility Functions

The Best Practice for Formidable to ensure the portability of your code along with Forms and Views requires you to use keys instead of IDs absolutely everywhere.

This is all well and good, but the code examples in the official documentation always seem to reference IDs, and in most cases, the ID of a view, field or form is required in order to do anything useful in code.

The functions below (or something like them) are therefore necessary in order to enable the global use of keys in code. I use them all over the place.

/*
 -----------------------------------------------------------------------------------
 UTILITY FUNCTIONS
 -----------------------------------------------------------------------------------
 The functions below are all descriptively named and each serve a single purpose.
*/

if (!function_exists('bsn_edit_dropdown_from_key')) {
	function bsn_edit_dropdown_from_key($atts) {
		$id = bsn_get_form_id_from_key($atts['form_key']);
		$field = $atts['field_key'];
		$page_id =$atts['page_id'];
		
		return do_shortcode('');
	}
	add_shortcode('bsn_edit_dropdown_from_key','bsn_edit_dropdown_from_key'); // shortcode definition MUST come AFTER function in "if(!function_exists()) {}" for some reason.
}
if (!function_exists('bsn_get_form_id_from_field_key')) {
	function bsn_get_form_id_from_field_key($field_key) {
		// get form id given a field key as imput
		global $frmdb, $wpdb;
		$form_id = $wpdb->get_var("SELECT form_id FROM $frmdb->fields WHERE field_key LIKE '".$field_key."'");
		return $form_id;
	}
}

if (!function_exists('bsn_get_form_id_from_key')) {
	function bsn_get_form_id_from_key($form_key) {
		// get form id given a form key as imput
		if(is_array($form_key)) {
			$form_key = $form_key['key'];
		}
		global $frmdb, $wpdb;
		$form_id = $wpdb->get_var("SELECT id FROM $frmdb->forms WHERE form_key LIKE '".$form_key."'");
		return $form_id;
	}
}

if (!function_exists('bsn_get_field_id_from_key')) {
	function bsn_get_field_id_from_key($field_key) {
		global $frmdb, $wpdb;
		$field_id = $wpdb->get_var("SELECT id FROM $frmdb->fields WHERE field_key LIKE '".$field_key."'");
		return $field_id;
	}
}

if (!function_exists('bsn_get_all_entries_in_form')) {
	function bsn_get_all_entries_in_form($form_id) {
		global $frmdb, $wpdb;
		$entry_ids = $wpdb->get_col("SELECT id FROM $frmdb->entries WHERE form_id=".$form_id);
		return $entry_ids;
	}
}

if (!function_exists('bsn_get_users_array')) {
	function bsn_get_users_array(){
		$args = array(
			'orderby'      => 'ID',
			'order'        => 'ASC',
			'fields'       => 'all',
		);
		$bsn_users = get_users( $args );
		return $bsn_users;
	}
}

/*
 * function bsn_get_child_entry_ids($parent_entry_id,$child_form_id=NULL)
 *
 * returns a one-dimensional array of entry ids of child form entries
 *
 * optionally specify child form id to get entries from a specific child form if there are several
 *
 */
 
if (!function_exists('bsn_get_child_entry_ids')) {
	function bsn_get_child_entry_ids($parent_entry_id,$child_form_id=NULL) {
		global $wpdb,$frmdb;
		if($child_form_id != NULL) {
			$child_entry_ids = $wpdb->get_col("SELECT id FROM $frmdb->entries WHERE parent_item_id=$parent_entry_id AND form_id=$child_form_id ORDER BY id ASC");
		} else { // if no child form id is specified, grab all child form entries willy-nilly
			$child_entry_ids = $wpdb->get_col("SELECT id FROM $frmdb->entries WHERE parent_item_id=$parent_entry_id ORDER BY id ASC");
		}
		return $child_entry_ids;	
	}
}

if (!function_exists('bsn_get_post_id_from_slug')) {
	function bsn_get_post_id_from_slug($slug) {
		global $wpdb;
		$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$slug'");
		return $id;
	}
}

Leave a Reply

Your email address will not be published.

I accept the Privacy Policy


Recent Posts

October 5, 2020
Easy Ways To Automate Your Website - 2 approaches

Isn't technology supposed to make life easier? I recently built a tool for a provider of an ERP system, to allow his clients to automatically publish inventory to their business websites and keep the sites in synch with his system. It got me thinking (again) about automation, and how business can do more with tools they're already using.

June 17, 2020
The Pivot: Making the shift from Commodity to Consultant

A lot of people - even companies - that provide technical services find themselves in the unfortunate position of being viewed as "the techs" and ignored when discussion turns to policy, marketing, or other topics relating to business management and strategy - the domain of consultants. This can be the case whether the tech is a twenty-something fresh out of school, or someone who's been in the industry for decades.
It's difficult to change people's perception once you've been slotted into a role. Far better to set expectations at the beginning of a relationship than try to change mid-stride.

June 4, 2020
An office without walls. A business without borders. There's no need to meet in person.

I should have known better, but I was hit with a wave of panic when the government announced the beginning of the corona virus lock down. I thought that might be it for my business. How would I get new clients? What about the meetings I had already scheduled? What if everybody stopped spending? Even in a digital business it's sometimes difficult to see past traditional expectations.

To my eternal relief, I learned better.

May 29, 2020
Your Website is an Investment, NOT an Expense. Demand ROI

Viewing your website(s) as a necessary expense rather than as an investment that's expected to produce a return is a critical misstep. Business owners often have a difficult time defining the role of their Website in their online presence and marketing. People often confess to feeling overwhelmed by technology, and rather than deal with the complexity, they settle for a site that's less functional than a business card or a billboard.

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

Email sending image

You have Successfully Subscribed!