Create Formidable entries from outside using nusoap web service

Nusoap makes designing web services in PHP easy. My requirement was to use Nusoap to design a web service that an external system can use to post entries in Formidable.
Nusoap can be downloaded here. Once downloaded, explode the zip file and upload *lib* folder to the project directory. We will be creating two files: postdata.php and createfpentry.php. The first file will act as the client to post requests. The latter will act as the web service server to handle post requests and insert formidable entries.
postdata.php
<?php
require_once ('lib/nusoap.php'); //imports the nusoap library
//set the post parameter
$param = array( 'fpformfieldslist' => array( 'v1' => trim($_GET['<var1>']),
'v2' => trim($_GET['<var2>']),
'v3' => trim($_GET['<var3>'])
)
);
$client = new nusoap_client('<hostname>/createfpentry.php'); //new object referring web service server
$response = $client->call('insert_fp_entry',$param); //invoke insert_fp_entry method from the server
//Process result
if($client->fault) {
//echo fault code and fault string: $client->faultcode and $client->faultstring
}
else {
echo $response; //echo response on successful web service call
}
?>

createfpentry.php
<?php
require_once ('lib/nusoap.php'); //import nusoap library
require_once( $_SERVER['DOCUMENT_ROOT'] . '/path/to/wp-load.php' ); //load WordPress from outside
$server = new soap_server; //server object
$server->register('insert_fp_entry'); //register insert_fp_entry method
function insert_fp_entry($fpformfieldslist) {
global $frm_entry, $frm_entry_meta;
$isCreated = $frm_entry->create(array(
'form_id' => 25, //replace 25 with your form id
'item_meta' => array(
152 => $fpformfieldslist['v1'], //replace 152 with your field id. You can set other fields with values equal to the different GET parameters
),
));
$result = 'Entry successfully created'; //you can make conditional edits to the response
return $result;
}
$server->service($HTTP_RAW_POST_DATA);
exit();
?>

The URL to access the web service is http://<host>/postdata.php?v1=test1&v2=test2&v3=test3. This will create an entry in formidable backend. The parameter values should be escaped for special characters.

Hope this helped! I shall be posting more on this subject in future posts. Please share your experiences and let me know if there is a better way to achieve similar results.

WordPress with Ultimate Client Manager

Ultimate Client Manager (UCM) is a Project Management tool designed for managing tasks and invoicing. This product offers the flexibility of customization to suit individual business needs.
I got an opportunity to work on a proof of concept where the intent was to establish the theory into practice. After studying their documentation, I chose to write a simple script to create a job inside of UCM, set the status to ‘New’ and Start Date as ‘Today’.
<?php
chdir('/path/to/ucm/');
include('init.php');
$job['name'] = 'Demo';
$job['status'] = 'New';
$job['date_start'] = date('Y-m-d');
$new_job_id = module_job::save_job('new',$job);
$new_job = module_job::get_job($new_job_id); //get values from newly created job
echo 'new job id:'.$new_job_id.' created with job name:'.$new_job['name'];
?>

Executing this code will insert a new job in UCM and the results will be echoed to the browser. I will be scaling this script as per my project requirements and most likely hook this to WordPress. But this can be used with any other platform.
Hope this helps! I will be writing more on this in future.

HTML table sorting with DataTables

I had this opportunity to use the brilliant DataTables jQuery plugin to sort an html table in a formidable pro view to display data. Primary benefits to using this plugin are:
a) Lightweight.
b) Easy to install and initialize.
c) Adds dynamic search and pagination features by default. However, this can be altered by passing parameters during plugin instantiation.

However, I would like to get an opinion if there are better and more efficient ways to sort html tables. I did some research but finally came back to DataTables to implement my custom solution. Please leave me a comment and let me know.

Autocomplete in Formidable Pro

Formidable Pro offers a fine feature called ‘enable autocomplete‘ for drop-down fields, the team making an extremely intelligent and efficient use of Chosen to drive this new field option.
However, one demerit of this feature is the lack of mobile support, ‘overkill’ being cited as one of the reasons for desktop only support and I respect their decision and whoever voted in favor of the cause.
This made me decide to implement my own custom solution with plain old jQuery and a text field (instead of drop-down). My requirement is to drive a ‘Data From Entries‘ field with ‘enable autocomplete’ checked for users to look up the values as they type. Also, users should be able to submit the data even if they enter a value not present in the drop-down, the new value finally getting added to the drop-down list.
As a first step to solve the problem, I added a text field to the form and bounded it to jQuery autocomplete. Second step was to write some PHP to make it the remote data source for autocomplete script. I used Formidable API to collect and return all field values from the form that was supposed to be the source for my data from entries field. This made my text field behave as a drop-down and also enabled users to add new values to the list. On form submission, I used Formidable hook to add the new entry to the list of entries.
A small demo is attached, the values you see are Formidable entries. Hope this helps! Please leave me a comment and let me know if there is a better way to achieve the same result.

Secure Page behind WP log in

Steps to quickly secure a page behind WordPress login are:
1) Open the page template you are using to display the page in text editor.
2) Add the code block below just beneath the template name
<?php if(is_user_logged_in()){ //check if user is logged in ?>
3) Go to the end of the template and add the below code
<?php } else { wp_redirect(wp_login_url(get_permalink())); //Redirect to login page} ?>
That’s it. Your page is now secured behind a login. Hope this helps! Please leave a comment and let me know if there is a better way to achieve the same result.

Dynamic drop-downs in Formidable Pro

One quick way to implement dynamic drop-downs (where second drop-down entries are dependent on the selection of first drop-down value) is to use the Dynamic Fields feature of Formidable Pro.
Upside
a) Very quick and easy to implement.
b) Developer can avoid all the fancy ajax and get/post scripts to fetch values from database. This reduces cost of development and increases ease of maintenance.
Downside
a) This is a paid feature.

Hope this helps! Please leave me a comment and let me know if there is any other way to implement dynamic fields in formidable.

Truncate string overflow

I was trying to set acl_name for a document using DFC when I encountered the below exception:
DfAttributeValueException:: THREAD: http-8080-1; MSG: [DFC_OBJECT_BADATTRVALUE] value is too big for attribute 'acl_name'. Value UTF-8 length is 36. Maximum length is 32.; ERRORCODE: ff; NEXT: null
This makes sense as string attributes in Documentum can hold a maximum of 32 characters. Ideally, we shouldn’t try to insert data more than the attribute can handle. However, this sometimes become necessary when dealing with a live application where the data set is already defined and changing production data is not encouraged. In such cases, a quick way to suppress the exception and truncate overflowing characters is to add the below line in dfc.properties file.
dfc.compatibility.truncate_long_values=true
Hope this helps! Please leave me a comment and let me know if you faced similar issue in the past and if there is a better way to achieve the same results.

Restrict WP Admin access

Quick tips to prevent users other than administrators to access WP-admin and the admin toolbar.
add_action( 'admin_init', 'your_method', 1 ); /* blocks WP-admin */
function your_method() {
$user = wp_get_current_user();
if ( ! $user->has_cap('administrator') && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
wp_die( __( 'You are not allowed to access the back end of this site.' ), 'Access Denied', array( 'back_link' => true ) );
}
}

add_action('set_current_user', 'your_method'); /* hides admin toolbar */
function your_method() {
$user = wp_get_current_user();
if ( !$user->has_cap('administrator') ) {
add_filter('show_admin_bar', '__return_false');
}
}

Hope this helps! Please leave me a note and let me know if there is a better way to achieve the same results.

Formidable Pro Nugget 2

I will be discussing about how to fetch values from “Data from Entries” field using Formidable API.

Data from Entries field type is a brilliant offering from Formidable Pro (FP) team, which at the very basic can display field values dynamically (let’s consider drop-down for this example) from other form entries.

Let’s consider two forms: FormA (source) and FormB (target). For this example, let’s assume FormA consists of one field “Name” and has two entries created. FormB has one Data From Entries field “Name” and is configured to display the Names from FormA as a drop-down. Ergo, the Name field in FormB will display two names, let’s assume the names are John and Jane Doe.

On previewing FormB, it will paint the drop-down list with the two names as expected. However, on analyzing the HTML structure in detail (I use Firebug), it is revealed to be somewhat similar as shown below.
<div id="frm_data_field_<field_id>_container">
<select id="field_<field_key>" name="item_meta[<field_id>]">
<option value=""> </option>
<option value="6">John Doe</option>
<option value="11">Jane Doe</option>
</select>
</div>

You’ll notice that option values are integer and not the actual names being displayed. This might be baffling when writing custom code to pull the user selected name from drop-down. However, these integer values are links back to the original form (FormA) entries driving the drop-down values. The numbers 6 & 11 (assumed for this example) are the corresponding item_ids for each entry from FormA.
So, while writing custom business logic to fetch the names from FormB, something as shown below will help. I am using hook frm_after_create_entry against FormB for illustration.
add_action('frm_after_create_entry', 'your_custom_method', 30, 2);
function your_custom_method($entry_id, $form_id) {
global $wpdb, $frm_entry, $frm_entry_meta;
if($form_id == 10) { //assuming FormB_id=10
//assuming Name field_id in FormA=111 & Name drop-down field_id in FormB=150
$name = $wpdb->get_var('SELECT meta_value FROM '.$wpdb->prefix.'frm_item_metas WHERE field_id=111 AND item_id='.$_POST['item_meta'][150]); //this line gets you the name from the original FormA entry based on the drop-down selection in FormB
//once user selected name is pulled from database, you are free to utilize the variable as per requirement
}
}

Hope this helps! Please leave a note and let me know if there is a better way to achieve the same result.

WordPress User Role

A quick way to get currently logged in user role using WordPress API.
$user = wp_get_current_user(); //returns a WP_User object
if($user->has_cap('ccexecutive') || $user->has_cap('administrator')) { //For example, check for a default admin role and a custom role
//custom logic
}

I tested the code and it works like a charm. Hope it helps! Please leave a note and let me know if there is a better way to achieve the same result.