Function to check status of gravityforms wordpress - wordpress

I am trying to make a function to check if the gform has some entries, has all entries or is empty and return $status according to status.
I have looped through the entries and checked if they are infact shown as an empty string if empty but I only ever get partial or empty.
function set_form_status($form_id) {
$entries = GFAPI::get_entries( $form_id, entry_search_criteria());
$status = '';
if (count($entries) > 0) {
foreach($entries as $entry) {
$keys = array_keys($entry);
foreach($keys as $key) {
if ($entry[$key] === '') {
$status = 'partial';
}
if ($entry[$key] !== '') {
$status = 'filled';
}
}
}
} else {
$status = 'empty';
}
return $status;
}

I'd suggest running it like below and seeing what gets returned. I did remove the entry_search_criteria function but other than that I'm just echoing the key/value.
What I found was even if I'm not using purchasing in a form, there are fields created for it and they are empty. They don't show on the back end entry view even if I select show empty fields. You may have a similar scenario.
function set_form_status($form_id) {
$entries = GFAPI::get_entries( $form_id);
$status = '';
if (count($entries) > 0) {
foreach($entries as $entry) {
$keys = array_keys($entry);
foreach($keys as $key) {
echo $key . ' - ' . $entry[$key] . '<br>';
if ($entry[$key] === '') {
$status = 'partial';
}
if ($entry[$key] !== '') {
$status = 'filled';
}
}
}
} else {
$status = 'empty';
}
return $status;
}

Did some refactoring and debugging together with a co-worker, there was HTML fields on all of the forms and all of them returned an empty string. This is the working version.
function set_form_status($form) {
define('FORM_EMPTY', 0);
define('FORM_PARTIAL', 1);
define('FORM_FILLED', 2);
//checks if the form is my special kind of form
if (is_eligible_form($form)) {
$earlier_entry = get_last_entry($form['id']);
if ($earlier_entry === false) {
return STATUS_EMPTY;
} else {
foreach($form['fields'] as $key => $field) {
if ($earlier_entry[$field->id] === '' && $field->type !== 'html') {
return FORM_PARTIAL;
}
}
}
}
return FORM_FILLED;
}

Related

How to assign Woocommerce order by custom fields?

My Woocommerce store with 5 store managers, I use the Local Pickup plugin, it automatically generates custom fields.
How do I correctly assign orders to them and make them invisible to each other's orders?
I have found a similar function, but can't work for custom fields.
function before_checkout_create_order($order, $data) {
$idp = get_post_meta( $order_id, 'pickup_name', true );
$store_manager_id = '';
$belgium_region = ['3'];
$czech_region = ['2'];
$uk_region = ['1'];
if (in_array($idp, $belgium_region)) {
// Manually assigning the _store_manager_id using the user id, yours will differ
$store_manager_id = 7;
} else if (in_array($idp, $czech_region)) {
$store_manager_id = 3;
} else if (in_array($idp, $uk_region)) {
$store_manager_id = 2;
} else {
$store_manager_id = 1;
}
$order->update_meta_data('_store_manager_id', $store_manager_id);
}
add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);
function custom_admin_shop_manager_orders($query) {
global $pagenow;
$qv = &$query->query_vars;
$currentUserRoles = wp_get_current_user()->roles;
$user_id = get_current_user_id();
if (in_array('shop_manager', $currentUserRoles)) {
if ( $pagenow == 'edit.php' &&
isset($qv['post_type']) && $qv['post_type'] == 'shop_order' ) {
// I use the meta key from step 1 as a second parameter here
$query->set('meta_key', '_store_manager_id');
// The value we want to find is the $user_id defined above
$query->set('meta_value', $user_id);
}
}
return $query;
}
add_filter('pre_get_posts', 'custom_admin_shop_manager_orders');

Problem removing sub category from permalink URL of blog post in Wordpress

I'm using this solution to remove sub-categories from the url on my blog posts:
Remove sub category slug from permalink URL of blog post and custom post type in WordPress
add_filter('post_link','custom_post_type_link',10,3);
function custom_post_type_link($permalink, $post, $leavename) {
if (!gettype($post) == 'post') {
return $permalink;
}
switch ($post->post_type) {
case 'post':
//$permalink = get_home_url() . '/' . $post->post_name . '/';
$cats = get_the_category($post->ID);
$subcats = array();
foreach( $cats as $cat ) {
$cat = get_category($cat->term_id);
if($cat->parent) { $subcats[] = sanitize_title($cat->name); }
}
if($subcats) {
foreach($subcats as $subcat) {
$subcat = $subcat.'/';
$permalink = str_replace($subcat, "", $permalink);
}
}
break;
}
return $permalink;}
It's working fine but there is still a little problem with the code.
If I have an URL like this:
www.myblog.com/parentcategory/nameofchildcategory/slugpost
I will get this:
www.myblog.com/parentcategory/slugpost
Which is exactly what I want.
But if I have an URL like this:
www.myblog.com/parentcategory/nameofchildcategory/slugpost-with-nameofchildcategory
I will get this:
www.myblog.com/parentcategory/slugpost-with- (mssing the end of the slug)
...and a 404 page for every link to this article.
So the problem is that when the child category text appears in the slug's post, the code will also remove this part of the URL.
Anyone have an idea on how to solve this?
Thanks in advance!
I have found this solution, adding two functions. I'm not sure the code is optimal but it works perfectly.
add_filter('post_link','custom_post_type_link',10,3);
//function to see if URL ends with...
function endsWith($string, $endString) {
$len = strlen($endString);
if ($len == 0) {
return true;
}
return (substr($string, -$len) === $endString);
}
//function to replace only first match
function str_replace_first($from, $to, $content)
{
$from = '/'.preg_quote($from, '/').'/';
return preg_replace($from, $to, $content, 1);
}
function custom_post_type_link($permalink, $post, $leavename) {
if (!gettype($post) == 'post') {
return $permalink;
}
switch ($post->post_type) {
case 'post':
//$permalink = get_home_url() . '/' . $post->post_name . '/';
$cats = get_the_category($post->ID);
$subcats = array();
foreach( $cats as $cat ) {
$cat = get_category($cat->term_id);
if($cat->parent) { $subcats[] = sanitize_title($cat->name); }
}
if($subcats) {
foreach($subcats as $subcat) {
$subcat = $subcat.'/';
//If URL ends with category name
if(endsWith($permalink,$subcat)){
//And if the category name appears more than once
if(substr_count($permalink,$subcat)>1){
// Remove only first match
$permalink = str_replace_first($subcat, "", $permalink);
}else{
//do nothing
}
}
// If URL does NOT end with category name
else{
$permalink = str_replace($subcat, "", $permalink);
}
}
}
break;
}
return $permalink;}
Try delimiting both ends of the slug, like:
foreach ($subcats as $subcat) {
$permalink = str_replace('/'.$subcat.'/', '/', $permalink);
}

How can i convert a yml,json or xml to csv

I'm using the bundle "JMSSerializerBundle" for export retrieved entity data. I can export the results as json, xml or yml successfully.
But i need also a csv export for this results. This bundle can't handle csv export. But i'm also not sure how can i convert json,xml or yml to csv, because csv is a flat file.
Have anyone solve this issue before?
Update
Now i create an solution for my problem, i'm partially satisfied with my solution, because it is not recursive. I will show it.
if ($format == 'csv') {
$json = $serializer->serialize($query, 'json', SerializationContext::create()->enableMaxDepthChecks());
return $this->toCsv(json_decode($json, true));
}
/**
* #todo write an recursive function for deeper levels
*
* #param $data
* #return array
*/
protected function toCsv($data)
{
$headers = array();
$outerCounter = 0;
foreach ($data as $key => $value) {
foreach ($value as $i => $j) {
if (!is_array($j)) {
$headers[] = $i;
$result[$outerCounter][$i] = $j;
} else {
foreach ($j as $k => $m) {
if (!is_array($m)) {
$headers[] = $i. '_'.$k;
$result[$outerCounter][$i. '_'.$k] = $m;
} else {
foreach ($m as $n => $l) {
if (!is_array($l)) {
$headers[] = $i.'_'.$k.'_'.$n;
$result[$outerCounter][$i.'_'.$k.'_'.$n] = $l;
}
}
}
}
}
}
$outerCounter++;
}
asort($headers);
$headers = array_unique($headers);
return array('headers' => $headers, 'data' => $result);
}

Change apperance of login modal form module drupal 7

I installed the modal forms module in drupal 7 (https://drupal.org/project/modal_forms) to create a modal window login form. But now, I'm tryig to change its appereance with css but I don't know how to do it.
Anyone knows how to change for example the appereance of input submit button applying one css class?
This is my code:
function get_output_ajax(){
$output = array();
ctools_add_js('ajax-responder');
$output[] = ctools_modal_command_dismiss(t('Login success'));
if (isset($_GET['destination'])) {
$output[] = ctools_ajax_command_redirect($_GET['destination']);
}
elseif(module_exists('login_destination')) {
$destination = login_destination_get_destination('login');
$output[] = ctools_ajax_command_redirect($destination['path']);
}
else {
$output[] = ctools_ajax_command_reload();
}
return $output;
}
function modal_forms_login($js = NULL) {
// Fall back if $js is not set.
if (!$js) {
return drupal_get_form('user_login');
}
$output = get_output_ajax();
}
}
else{
$output = get_output_ajax();
}
print ajax_render($output);
} global $user;
if ($user->uid == 0) {
$output = ctools_modal_form_wrapper('user_login', $form_state);
if (!empty($form_state['executed'])) {
$output = get_output_ajax();
}
}
else{
$output = get_output_ajax();
}
print ajax_render($output);
}
Thanks in advance.
Here my solution, it works for me:
function modal_forms_login($js = NULL) {
// Fall back if $js is not set.
if (!$js) {
return drupal_get_form('user_login');
}
ctools_include('modal');
ctools_include('ajax');
$form_state = array(
// 'title' => t('Log in'),
'ajax' => TRUE,
);
global $user;
if ($user->uid == 0) {
$output = ctools_modal_form_wrapper('user_login', $form_state);
//Necesario para editar el boton del login
$cadena = $output[0]['output'];
$patrón = '/class="form-submit"/';
$sustitución = 'class="form-submit btn btn-primary"';
$output[0]['output'] = preg_replace($patrón, $sustitución, $cadena);
if (!empty($form_state['executed'])) {
$output = get_output_ajax();
}
}
else{
$output = get_output_ajax();
}
print ajax_render($output);
}

Wordpress - Create own admin messages for Custom Post Type

I've created a custom post type called routes and I'd like to be able to return error messages to the screen when something goes wrong during a save/update e.g. The type allows for gpx/kml files to be uploaded and checked that the correct type has been posted. At the moment it just returns if it goes wrong - how can I set an error message?
//Return if file type wrong.
if($file_type != 'application/octet-stream' && $file_type != 'application/gpx+xml' ) {
return;
}
try this
example:
add_admin_message('Please enter valid URL for the project link', true);
add_admin_message('Your custom post type was updated');
source:
<?php
/**
* Messages with the default wordpress classes
*/
function showMessage($message, $errormsg = false)
{
if ($errormsg) {
echo '<div id="message" class="error">';
}
else {
echo '<div id="message" class="updated fade">';
}
echo "<p>$message</p></div>";
}
/**
* Display custom messages
*/
function show_admin_messages()
{
if(isset($_COOKIE['wp-admin-messages-normal'])) {
$messages = strtok($_COOKIE['wp-admin-messages-normal'], "##");
while ($messages !== false) {
showMessage($messages, true);
$messages = strtok("##");
}
setcookie('wp-admin-messages-normal', null);
}
if(isset($_COOKIE['wp-admin-messages-error'])) {
$messages = strtok($_COOKIE['wp-admin-messages-error'], "##");
while ($messages !== false) {
showMessage($messages, true);
$messages = strtok("##");
}
setcookie('wp-admin-messages-error', null);
}
}
/**
* Hook into admin notices
*/
add_action('admin_notices', 'show_admin_messages');
/**
* User Wrapper
*/
function add_admin_message($message, $error = false)
{
if(empty($message)) return false;
if($error) {
setcookie('wp-admin-messages-error', $_COOKIE['wp-admin-messages-error'] . '##' . $message, time()+60);
} else {
setcookie('wp-admin-messages-normal', $_COOKIE['wp-admin-messages-normal'] . '##' . $message, time()+60);
}
}

Resources