fbpx

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;
	}
}