I have a site which I'm running gravity forms on and I have created a form to upload images to my NextGEN gallery which all works great by using the following code in my functions.php file
<?php
// Gravity Forms changing the upload location to my sort it out folder
add_filter("gform_upload_path", "change_upload_path", 10, 2);
function change_upload_path($path_info, $form_id){
if($form_id == 3){
//NOTE: replace the form_id with the gravity form id number
$path_info["path"] = WP_CONTENT_DIR . "/gallery/sort-it-out/" ;
$path_info["url"] = WP_CONTENT_URL . "/gallery/sort-it-out/";
}
return $path_info;
}
?>
This code works and when I upload an image using it the image appears in my album fine albeit I have to scan for new images which isn't an issue but if someone knows how to automate that I would be greatful for the code.
The issue I need help with is I'm also now running the NextGEN plugin called ngg custom fields where I have 4 custom fields of image owner, location,year, and description. I gather this information and the image from the gravity form and need it to update the album in the nextGEN gallery with this information so I can manage my gallery. I have the following code which seems to partly work apart from the information not populating the fields. The reason I say it partly works is because if I change the style of date or ngg_type number etc the mysql database and the nextgen gallery album shows the differnet changes but it's just the users inputs from the form ie the required details aren't being populated into the nextgen gallery custom fields from the gravity form.
Can anyone help because I can't see why not and I'm starting to tear my hair out with this problem, one thing I have noticed is the the wp_ngg_picture table has the filename as "kenny.jpg" and the filename in the gravity form (wp_rg_lead_detail) is the full site url/gallery/sort-it-out/kenny.jpg and then at the end it has this |:||:||:|
Here's the code I'm trying to get working
<?php
/* The script below is possibly going to add form details to ngg gallery */
add_action('ngg_after_new_images_added','my_function',10,2);
function my_function($galleryID,$image_ids){
global $wpdb;
$date = date('Y-m-d H:i:s');
foreach($image_ids as $imgID){
$wpdb->query("UPDATE $wpdb->nggpictures SET exclude = '1' WHERE pid = '$imgID' ");
$filename = $wpdb->get_var( "SELECT filename FROM wp_ngg_pictures WHERE pid='$imgID' ");
$leadid = $wpdb->get_var( "SELECT lead_id FROM wp_rg_lead_detail WHERE form_id='3' AND field_number='1' AND value='$filename' " );
$owner = $wpdb->get_var("SELECT value FROM wp_rg_lead_detail WHERE form_id='3' AND field_number='2' AND lead_id='$leadid' ");
$wpdb->insert("wp_nggcf_field_values", array("pid" => $imgID, "fid" => '3' , "field_value" => $owner, "ngg_type" => '1' ,'dateadded' => $date), array("%d","%d","%s", "%d" , "%s"));
$year = $wpdb->get_var("SELECT value FROM wp_rg_lead_detail WHERE field_number='3' AND lead_id='$leadid' ");
$wpdb->insert("wp_nggcf_field_values", array("pid" => $imgID, "fid" => '5' , "field_value" => $year, "ngg_type" => '1' ,'dateadded' => $date), array("%d","%d","%s", "%d" , "%s"));
$locat = $wpdb->get_var("SELECT value FROM wp_rg_lead_detail WHERE field_number='4' AND lead_id='$leadid' ");
$wpdb->insert("wp_nggcf_field_values", array("pid" => $imgID, "fid" => '6' , "field_value" => $locat, "ngg_type" => '1' ,'dateadded' => $date), array("%d","%d","%s", "%d" , "%s"));
$descr = $wpdb->get_var("SELECT value FROM wp_rg_lead_detail WHERE field_number='5' AND lead_id='$leadid' ");
$wpdb->insert("wp_nggcf_field_values", array("pid" => $imgID, "fid" => '4' , "field_value" => $descr, "ngg_type" => '1' ,'dateadded' => $date), array("%d","%d","%s", "%d" , "%s"));
}
}
?>
Please can anyone help me solve this problem.
Arnav Joy answers:
so for the second problem of full filename , use this
$filename = $wpdb->get_var( "SELECT filename FROM wp_ngg_pictures WHERE pid='$imgID' ");
$filename = substr($filename,strrpos($filename,'/',0)+1,strlen($filename));
$leadid = $wpdb->get_var( "SELECT lead_id FROM wp_rg_lead_detail WHERE form_id='3' AND field_number='1' AND value='$filename' " );
and it will correct the problem of full url and will provide you just the name of the file.
for the first problem , I am little confused about your problem , can you explain it more
Arnav Joy comments:
in the above code I have added just one this line of code
$filename = substr($filename,strrpos($filename,'/',0)+1,strlen($filename));
and where you have to place this code, after this
foreach($image_ids as $imgID){
$wpdb->query("UPDATE $wpdb->nggpictures SET exclude = '1' WHERE pid = '$imgID' ");
so it will become something like
foreach($image_ids as $imgID){
$wpdb->query("UPDATE $wpdb->nggpictures SET exclude = '1' WHERE pid = '$imgID' ");
$filename = substr($filename,strrpos($filename,'/',0)+1,strlen($filename));
let me know if you have any problem.
Kennyboy7 comments:
Hi Arnav,
Thanks for answering, I've tried the extra line you provided and I still have the same issue the form processes and I get the image ok in my gallery but I still have no custom field information. I can give you access if you require it because I remember you have helped someone else with something similar.
Dbranes answers:
Hi, you could try to profile your code to see where it might fail:
fx. writing the sql queries and the results into a logger.txt file
<?php
/* The script below is possibly going to add form details to ngg gallery */
add_action('ngg_after_new_images_added','my_function',10,2);
function my_function($galleryID,$image_ids){
global $wpdb;
$date = date('Y-m-d H:i:s');
$nr=0;
$exceptions=array();
$sql=array();
foreach($image_ids as $imgID){
try {
$wpdb->query("UPDATE $wpdb->nggpictures SET exclude = '1' WHERE pid = '$imgID' ");
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
$filename = $wpdb->get_var( "SELECT filename FROM wp_ngg_pictures WHERE pid='$imgID' ");
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
$leadid = $wpdb->get_var( "SELECT lead_id FROM wp_rg_lead_detail WHERE form_id='3' AND field_number='1' AND value='$filename' " );
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
$owner = $wpdb->get_var("SELECT value FROM wp_rg_lead_detail WHERE form_id='3' AND field_number='2' AND lead_id='$leadid' ");
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
$wpdb->insert("wp_nggcf_field_values", array("pid" => $imgID, "fid" => '3' , "field_value" => $owner, "ngg_type" => '1' ,'dateadded' => $date), array("%d","%d","%s", "%d" , "%s"));
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
$year = $wpdb->get_var("SELECT value FROM wp_rg_lead_detail WHERE field_number='3' AND lead_id='$leadid' ");
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
$wpdb->insert("wp_nggcf_field_values", array("pid" => $imgID, "fid" => '5' , "field_value" => $year, "ngg_type" => '1' ,'dateadded' => $date), array("%d","%d","%s", "%d" , "%s"));
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
$locat = $wpdb->get_var("SELECT value FROM wp_rg_lead_detail WHERE field_number='4' AND lead_id='$leadid' ");
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
$wpdb->insert("wp_nggcf_field_values", array("pid" => $imgID, "fid" => '6' , "field_value" => $locat, "ngg_type" => '1' ,'dateadded' => $date), array("%d","%d","%s", "%d" , "%s"));
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
$descr = $wpdb->get_var("SELECT value FROM wp_rg_lead_detail WHERE field_number='5' AND lead_id='$leadid' ");
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
$wpdb->insert("wp_nggcf_field_values", array("pid" => $imgID, "fid" => '4' , "field_value" => $descr, "ngg_type" => '1' ,'dateadded' => $date), array("%d","%d","%s", "%d" , "%s"));
$sql[]=$wpdb->last_query." ERROR:" . $wpdb->last_error;
}catch (Exception $e) {
$exceptions[]= "Exception: ".$e->getMessage();
}
$nr++;
$debuginfo=array(
"nr" =>$nr,
"filename"=>$filename,
"leadid"=>$leadid,
"year"=>$year,
"locat"=>$locat,
"descr"=>$descr,
"exceptions"=>$exceptions
);
my_logger($debuginfo);
}
}
function my_logger($var){
file_put_contents('/tmp/logger.txt', date("Y-m-d: H:I:s",time())." --- ".print_r($var, true), FILE_APPEND);
}
?>
Kennyboy7 comments:
Hi Dbranes
I tried this code but I can't find where it's outputting the logger.txt file so at the moment I'm unable to provide you with any more info but thanks for trying to solve this issue I'm having.
Dbranes comments:
ok, you have to change this:
'/tmp/logger.txt'
to your own custom path and make logger.txt writeable by your webserver.
(see fx: http://codex.wordpress.org/Changing_File_Permissions)