Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
add_action('frm_after_update_entry', 'populate_additional_workflow', 10, 2);
function populate_additional_workflow( $entry_id, $form_id ) {
/* get the project form id */
$project_form = FrmForm::get_id_by_key('project_with_workflow');
if( $form_id == $project_form ) {
/* initialize variables */
global $wpdb, $user_ID;
$table_prefix = $wpdb->prefix;
/* get the value of the 'Service Needed' and 'Project Status' radio buttons */
$service = ( isset( $_POST['item_meta'][636] ) ) ? $_POST['item_meta'][636] : '' ;
$project_status = ( isset( $_POST['item_meta'][653] ) ) ? $_POST['item_meta'][653] : '';
if ( empty($service) || empty($project_status) || ( !empty($project_status) && $project_status == 'In Consideration' ) ) {
return;
}
/* we use keys to retrieve IDs so code is transportable
* see https://formidable-masterminds.com/writing-transportable-code-keys-vs-ids/ for details
*/
/* get the form IDs */
$process_step_form = FrmForm::get_id_by_key('process_steps');
$process_flow = FrmForm::get_id_by_key('process_flow');
/* get the field IDs */
$field_id1 = FrmField::get_id_by_key('process_step_service');
$field_id2 = FrmField::get_id_by_key('process_step_phase');
$field_id3 = FrmField::get_id_by_key('process_step_sequence');
/* generate and process the SQL */
/* retrieve a list of entry ids for the steps that match the project's status */
$sql = "SELECT DISTINCT(t1.id) FROM `" . $table_prefix . "frm_items` t1 LEFT JOIN `" . $table_prefix . "frm_item_metas` t2 ON t2.item_id = t1.id LEFT JOIN `" . $table_prefix . "frm_item_metas` t3 ON t3.item_id = t1.id LEFT JOIN `" . $table_prefix . "frm_item_metas` t4 ON t4.item_id = t1.id where t1.form_id = " . $process_step_form . " AND ( t2.field_id = '" . $field_id1 . "' AND t2.meta_value LIKE '%" . $service . "%' ) AND ( t3.field_id = '" . $field_id2 . "' AND t3.meta_value = '" . $project_status . "' ) AND t4.field_id = '" . $field_id3 . "' ORDER BY t4.meta_value";
/* execute the query and return the results */
$results = $wpdb->get_results( $sql );
/* process the results and insert new rows into the process flow form for this project */
foreach($results as $item) {
/* get the values we need for the process flow */
$entry = FrmEntry::getOne($item->id, true);
$process_name = $entry->metas['618'];
$assigned_to = '';
$due_date = '';
$date_completed = '';
$consideration = ( isset( $entry->metas['659'] ) ) ? $entry->metas['659'] : '';
$next_action = ( isset( $entry->metas['622'] ) ) ? $entry->metas['622'] : '';
$prior_action = ( isset( $entry->metas['621'] ) ) ? $entry->metas['621'] : '';
$assigned_to_email = '';
$assigned_to_userid = '';
$assigned_to_user_error = '';
$sequence = ( isset( $entry->metas['619'] ) ) ? $entry->metas['619'] : '';
$phase = ( isset( $entry->metas['657'] ) ) ? $entry->metas['657'] : '';
/* create workflow record for each process step */
$data_values = array(
'form_id' => $process_flow,
'item_key' => FrmAppHelper::get_unique_key( '', $table_prefix . 'frm_items', 'item_key', 0, 7 ),
'name' => '',
'frm_user_id' => $user_ID,
'item_meta' => array(
660 => $entry_id, // project id
661 => $process_name, // name
667 => $assigned_to, // assigned to
665 => $due_date, // due date
670 => $date_completed, // date completed
662 => $consideration, // consideration
335 => $next_action, // next action
336 => $prior_action, // prior action
663 => $user_ID, // user id
668 => $assigned_to_email, // assigned to email
669 => $assigned_to_userid, // assigned to userid
671 => $assigned_to_user_error, // assign to user error
672 => $sequence, // process sequence
673 => $phase, // process step phase
),
);
$success = FrmEntry::create( $data_values );
}
}
}
<?php
add_action('frm_after_create_entry', 'populate_initial_workflow', 30, 2);
function populate_initial_workflow( $entry_id, $form_id ) {
/* get the project form id */
$project_form = FrmForm::get_id_by_key('project_with_workflow');
if( $form_id == $project_form ) {
/* initialize variables */
global $wpdb, $user_ID;
$table_prefix = $wpdb->prefix;
/* get the value of the 'Service Needed' radio button */
$service = ( isset( $_POST['item_meta'][636] ) ) ? $_POST['item_meta'][636] : '' ;
if ( empty($service) ) {
return;
}
/* we use keys to retrieve IDs so code is transportable
* see https://formidable-masterminds.com/writing-transportable-code-keys-vs-ids/ for details
*/
/* get the form IDs */
$process_step_form = FrmForm::get_id_by_key('process_steps');
$process_flow = FrmForm::get_id_by_key('process_flow');
/* get the field IDs */
$field_id1 = FrmField::get_id_by_key('process_step_service');
$field_id2 = FrmField::get_id_by_key('process_step_phase');
$field_id3 = FrmField::get_id_by_key('process_step_sequence');
/* generate and process the SQL */
/* retrieve a list of entry ids for the steps that match the project's initial state */
$sql = "SELECT DISTINCT(t1.id) FROM `" . $table_prefix . "frm_items` t1 LEFT JOIN `" . $table_prefix . "frm_item_metas` t2 ON t2.item_id = t1.id LEFT JOIN `" . $table_prefix . "frm_item_metas` t3 ON t3.item_id = t1.id LEFT JOIN `" . $table_prefix . "frm_item_metas` t4 ON t4.item_id = t1.id where t1.form_id = " . $process_step_form . " AND ( t2.field_id = '" . $field_id1 . "' AND t2.meta_value LIKE '%" . $service . "%' ) AND ( t3.field_id = '" . $field_id2 . "' AND t3.meta_value = 'Init') AND t4.field_id = '" . $field_id3 . "' ORDER BY t4.meta_value";
/* execute the query and return the results */
$results = $wpdb->get_results( $sql );
/* process the results and insert new rows into the process flow form for this project */
foreach($results as $item) {
/* get the values we need for the process flow */
$entry = FrmEntry::getOne($item->id, true);
$process_name = $entry->metas['618'];
$assigned_to = '';
$due_date = '';
$date_completed = '';
$consideration = ( isset( $entry->metas['659'] ) ) ? $entry->metas['659'] : '';
$next_action = ( isset( $entry->metas['622'] ) ) ? $entry->metas['622'] : '';
$prior_action = ( isset( $entry->metas['621'] ) ) ? $entry->metas['621'] : '';
$assigned_to_email = '';
$assigned_to_userid = '';
$assigned_to_user_error = '';
$sequence = ( isset( $entry->metas['619'] ) ) ? $entry->metas['619'] : '';
$phase = ( isset( $entry->metas['657'] ) ) ? $entry->metas['657'] : '';
/* create workflow record for each process step */
$data_values = array(
'form_id' => $process_flow,
'item_key' => FrmAppHelper::get_unique_key( '', $table_prefix . 'frm_items', 'item_key', 0, 7 ),
'name' => '',
'frm_user_id' => $user_ID,
'item_meta' => array(
660 => $entry_id, // project id
661 => $process_name, // name
667 => $assigned_to, // assigned to
665 => $due_date, // due date
670 => $date_completed, // date completed
662 => $consideration, // consideration
335 => $next_action, // next action
336 => $prior_action, // prior action
663 => $user_ID, // user id
668 => $assigned_to_email, // assigned to email
669 => $assigned_to_userid, // assigned to userid
671 => $assigned_to_user_error, // assign to user error
672 => $sequence, // process sequence
673 => $phase, // process step phase
),
);
$success = FrmEntry::create( $data_values );
}
}
}
jQuery(document).ready(function($) {
"use strict";
function execute_assigned_to_email_ajax( fullname, form_key) {
/* Note: Something weird was going here. The "Assigned To" field is a
* lookup drop down that display the assignee's fullname as
* lastname, firstname
* For some unknown reason, when this gets passed to the PHP function, it is received as:
* lastname, +firstname
* We have no idea where the + plus sign is coming from or how it's being added to the
* passed parameter. To deal with this, we used the split() function to
* break the fullname up into its component parts. It was just easier for us
* this way. If anyone has any ideas about where the + sign is coming from, we'd
* love to hear from you.
*/
var name_parts = fullname.split(", ");
$.ajax({
type: 'POST',
url: hixxa.ajax_url,
async: false,
data: {
action: 'update_assigned_to_fields',
last_name: name_parts[0],
first_name: name_parts[1],
},
}).done(function (response) {
var values = JSON.parse(response);
if (form_key = 'project_with_workflow') {
$("#field_project_assigned_to_email").val(values.email);
$("#field_project_assigned_to_user_id").val(values.userid);
$("#field_project_assigned_to_user_error").val(values.error);
} else {
$("#field_process_flow_assigned_to_email").val(values.email);
$("#field_process_flow_assigned_to_user_id").val(values.userid);
$("#field_process_flow_assigned_to_user_error").val(values.error);
}
});
}
$(function() {
if ( "#field_project_assigned_to_email").length > 0 ) {
var form_key = $('input[name="form_key"]').val();
if ( form_key = 'project_with_workflow' ) {
var assigned_to_email = $("#field_project_assigned_to_email").val();
if ( assigned_to_email == '' ) {
var fullname = $("#field_project_assigned_to").val();
execute_assigned_to_email_ajax( fullname, form_key);
}
}
}
});
$("#field_process_flow_assigned_to, #field_project_assigned_to").on("change", function() {
var fullname = $(this).val(),
name_parts = fullname.split(", "),
form_key = $('input[name="form_key"]').val();
execute_assigned_to_email_ajax( fullname, form_key);
});
});
<?php
/* load the required jQuery */
/* if you are editing the entries on the backend,
* you need to load the jQuery in the WordPress admin
*/
add_action('admin_enqueue_scripts','process_flow_script');
add_action( 'wp_enqueue_scripts', 'process_flow_script' );
function process_flow_script() {
wp_enqueue_script('assigned-to-update', get_bloginfo( 'stylesheet_directory' ) .'/js/update_assigned_to_fields.js', array('jquery'), '1.0.0', false);
/* allow script to pass variables to PHP */
wp_localize_script( 'assigned-to-update', 'myajaxurl', array('ajax_url' => admin_url( 'admin-ajax.php')));
}
/* setup the Ajax callback in WordPress */
add_action( 'wp_ajax_update_assigned_to_fields', 'update_assigned_to_fields' );
add_action( 'wp_ajax_nopriv_update_assigned_to_fields', 'update_assigned_to_fields' );
function update_assigned_to_fields() {
global $wpdb;
$table_prefix = $wpdb->prefix;
$item_metas = $table_prefix . 'frm_item_metas';
/* get the parameters */
$lastname = $_POST['last_name'];
$firstname = $_POST['first_name'];
/* we use keys to retrieve IDs so code is transportable
* see https://formidable-masterminds.com/writing-transportable-code-keys-vs-ids/ for details
*/
/* get the field ids */
$employee_lastname = FrmField::get_id_by_key('employee_last_name');
$employee_firstname = FrmField::get_id_by_key('employee_first_name');
$employee_email_address = FrmField::get_id_by_key('employee_email_address');
/* retrieve the item id so we can get the email address */
$sql = "SELECT t1.item_id FROM `" . $item_metas . "` t1 LEFT JOIN `" . $item_metas . "` t2 ON t1.item_id = t2.item_id WHERE t1.field_id = '" . $employee_lastname . "' AND t1.meta_value = '" . $lastname . "' AND t2.field_id = '" . $employee_firstname . "' AND t2.meta_value = '" . $firstname . "'";
$item_id = $wpdb->get_var( $sql );
/* now get the email address */
$sql = "SELECT meta_value FROM `" . $item_metas . "` WHERE field_id = '" . $employee_email_address . "' AND item_id = '" . $item_id . "'";
$email = $wpdb->get_var( $sql );
/* get the WordPress user object */
$user = get_user_by( 'email', $email );
if( !$user ) {
$error_msg = 'Unable to retrieve user id. No user record found.';
$userid = '0';
} else {
$error_msg = 'Success!';
$userid = $user->data->ID;
}
/* populate an array with the retrieved values */
$values = array( 'email' => $email, 'userid' => $userid, 'error' => $error_msg, );
/* send the JSON to the browser */
echo json_encode($values);
/* end further processing and trigger the Ajax done event
* without this step, a 0 will be added to the end of the JSON string
* that will cause a decoding error when the response is returned to the browser
*/
wp_die();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment