How to prevent duplicate entries in Gravity Forms within specific dates - php

I am trying to prevent duplicate entries for 2 of my Gravity Forms. I need to prevent these entries only within a certain timeframe and based on certain fields like 'email' or 'address'. So if they have an entry with the same 'email' prior to the start_date, they can still submit the form without issue. If they have an entry with the same 'email' after the start_date and before the end_date then they won't be able to submit the form. The code below is what I currently have in place, but it isn't working. Any help is greatly appreciated.
add_filter( 'gform_is_duplicate', 'custom_duplicate_entry_rules', 10, 4 );
function custom_duplicate_entry_rules( $count, $form_id, $field, $value ) {
$form_id = array( 6,61 );
$search_criteria = array(
'start_date' => 2021-02-01, // Get entries from date after last event
'end_date' => 2022-01-20, // Up to right before next event
'field_filters' => array( // which fields to search
array(
'key' => 'email',
)
)
);
$search_past_entries = GFAPI::count_entries($form_id, $search_criteria);
if ( $search_past_entries >= '1' ) {
echo 'You have already signed up.';
}
}

I managed to get this figured out with the help of a fellow developer...
add_filter( 'gform_is_duplicate_6', 'custom_duplicate_entry_rules', 10, 4 );
add_filter( 'gform_is_duplicate_61', 'custom_duplicate_entry_rules', 10, 4 );
function custom_duplicate_entry_rules( $count, $form_id, $field, $value ) {
$search_criteria = array(
'start_date' => '2021-02-01', // Get entries from date after last event
'end_date' => '2022-01-20', // Up to right before next event
'field_filters' => array( // which fields to search
array(
'key' => 2,
'value' => $value,
)
)
);
$count = GFAPI::count_entries( $form_id, $search_criteria );
if( $count >= 1 ) {
return $count;
} else {
return 0;
}
}
I needed to zero in on the specific entry meta in field_filters which in this case ended up being 2, for the email address (this is the field ID in Gravity Forms). Lastly, I changed the if statement to return $count or 0. Gravity Forms does all the heavy lifting beyond that.

Related

Make a php array load faster

I successfully edited the checkout page of the WooCommerce city field in order to be a dropdown field with a lot of cities in it around 13k values the problem is that on mobile and some low-end devices this field isn't performing well how I can make this array load faster is a DB call worth it in this situation, is there any way stop the dropdown and make the field only searchable is it worth splitting the array or loading from a text file?
The code I made
<?php
// Add custom checkout select fields
add_filter('woocommerce_checkout_fields', 'add_custom_checkout_select_fields');
function add_custom_checkout_select_fields($fields)
{
$arr = array(); // very big array arround 13k values
// Define HERE in the array, your desired cities
$cities = $arr;
// Format in the right way the options array of cities
$options = array(
'' => __('Choose City', 'woocommerce') . '…'
);
foreach ($cities as $city)
{
$options[$city] = $city;
}
// Adding 2 custom select fields
$fields['billing']['billing_city2'] = $fields['shipping']['shipping_city2'] = array(
'type' => 'select',
'required' => true,
'options' => $options,
'autocomplete' => 'address-level2',
'input_class' => array(
'wc-enhanced-select',
)
);
// Copying data from WooCommerce city fields
$fields['billing']['billing_city2']['class'] = array_merge($fields['billing']['billing_city']['class'], array(
'hidden'
));
$fields['shipping']['shipping_city2']['class'] = array_merge($fields['shipping']['shipping_city']['class'], array(
'hidden'
));
$fields['billing']['billing_city2']['label'] = $fields['billing']['billing_city']['label'];
$fields['shipping']['shipping_city2']['label'] = $fields['shipping']['shipping_city']['label'];
$fields['billing']['billing_city2']['priority'] = $fields['billing']['billing_city']['priority'] + 5;
$fields['shipping']['shipping_city2']['priority'] = $fields['shipping']['shipping_city']['priority'] + 5;
wc_enqueue_js("
jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {
var select2_args = { minimumResultsForSearch: 1 };
jQuery( this ).select2( select2_args ).addClass( 'enhanced' );
});");
return $fields;
}

How to add nested data into MySQL tables using PHP

I have 3 tables and I'm working with wordpress too: wp7s_g_bookings, wp7s_g_tests and wp7s_g_clients. This is a study project, not to sell.
I want to be able to save 1 booking with X clients and Y tests.
I read about LAST_INSERT_ID() but I don't think it will work in this case and I'm lost.
Possible scenarios:
You are a partner, then the data is beeing pulled, so we have the ID because it exist already;
In this case we are adding other users data inside repeatable fields like name="client_birth[]" ... // client_vat only show/exist if the partner don't exist.
You are a normal client, in this case you fill the same client data plus client_vat (so I'm sure when adding into database that only the guy that have client_vat is the booking_client)
if (!empty($_POST)) {
$wpdb->query('START TRANSACTION');
//All this fields are repeatable inputs, except client_vat that can only be the 1st record. I think it's missing a foreach
$default = array(
'client_name' => $_POST['client_name'],
'client_birth' => $_POST['client_birth'],
'client_sns' => $_POST['client_sns'],
'client_vat' => $_POST['client_vat'],
'client_city' => $_POST['client_city'],
'client_email' => $_POST['client_email'],
'client_phone' => $_POST['client_phone']
);
$item = shortcode_atts( $default, $_REQUEST );
$addClients = $wpdb->insert( 'wp7s_g_clients', $item );
$default = array(
'test_client' => ???, //ID of previous client that have client_vat filled [0]
);
$item = shortcode_atts( $default, $_REQUEST );
$addTests = $wpdb->insert( 'wp7s_g_tests', $item );
$collectDate = date('Y-m-d H:i:s', strtotime($_POST['booking_collect_date']));
$default = array(
'booking_status' => 1,
'booking_partner' => $_POST['booking_partner'], //ID of the partner, if not empty
'booking_client' => ???, //If partner don't exist -> ID of client that have client_vat filled
'booking_type' => $_POST['booking_type'],
'booking_clients' => ???, //Array of all IDs previously added in wp7s_g_clients table
'booking_city' => $_POST['booking_city'],
'booking_address' => $_POST['booking_address'],
'booking_collect_date' => $collectDate,
'booking_payment' => $_POST['booking_payment'],
'booking_amount' => $_POST['booking_amount'],
'booking_obs' => nl2br($_POST['booking_obs']),
);
$item = shortcode_atts( $default, $_REQUEST );
$addBookings = $wpdb->insert( 'wp7s_g_bookings', $item );
if($addClients && $addTests && $addBookings) {
$wpdb->query('COMMIT');
$msg = "Success";
wp_redirect( esc_url( get_page_link( 6 ) ) );
exit;
}
else {
$wpdb->query('ROLLBACK');
$msg = "Error";
}
}
My issues are adding this properly into database the repeatable fields and using a previous created ID(s).
I tried to explain everything and comment so it's better to understand.
After you perform a query with $wpdb you can access the $wpdb->insert_id to get the last inserted id. You can find more details on the documentation page of wpdb
If you need more ids because u have more $wpdb->insert all you need to do is store $wpdb->insert_id after each insert, like:
...
$item = shortcode_atts( $default, $_REQUEST );
$addClients = $wpdb->insert( 'wp7s_g_clients', $item );
$clientId = $wpdb->insert_id;
....
$default = array(
'test_client' => ???, //ID of previous client that have client_vat filled [0]
);
$item = shortcode_atts( $default, $_REQUEST );
$addTests = $wpdb->insert( 'wp7s_g_tests', $item );
$testId = $wpdb->insert_id;
...
rest of your code

How to update the quantity of all items in the cart with respect to database?

I am doing a shopping cart using code-igniter. While I can do the add cart functions, I'm somewhat confused with how to update them with regards to database. All I want is when I change the item quantity in the cart and click update ,both the cart and database must be updated for that particular item.I tried to do it but couldn't get it.Can someone please enlighten me what to do?
controller code
$this->load->model('user_m');
$result['query'] = $this->user_m->get($id); // inserts coresponing item
foreach ($result['query'] as $row)
$id = $row->pid;
$qty = $a;
$quan=$row->quantity;
$price = $row->pprice;
$name = $row->pname;
$q=$quan-$a; // for remainig stock i.e total qty-user qty
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'stock' =>$q
);
$this->cart->insert($data);
$this->load->model('user_m');
$result['qry'] = $this->user_m->up_cart($id,$q);
redirect($_SERVER['HTTP_REFERER']);
}
just tell me how to update pls!
when ever you add any product in cart, it will generate an uniq row ID and that will be unique for each and every product and hence you need to update quantity by that row ID
$data = array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
);
$this->cart->update($data);
// Or a multi-dimensional array
$data = array(
array(
'rowid' => 'b99ccdf16028f015540f341130b6d8ec',
'qty' => 3
),
array(
'rowid' => 'xw82g9q3r495893iajdh473990rikw23',
'qty' => 4
),
array(
'rowid' => 'fh4kdkkkaoe30njgoe92rkdkkobec333',
'qty' => 2
)
);
$this->cart->update($data);
Now you want to know about how you will get row id.
$cart=$this->cart->contents();
it will return whole array of cart and when you listing it on webpage, please associate row id with quantity text box to update that particular product.
please check following url:
https://ellislab.com/codeigniter/user-guide/libraries/cart.html

PHP. How to take data from 2 mysql tables instead of 1

Learning php and I am losing my mind trying to solve this for days now. Please help.
This is a code which goes thought a table COUPON, take data with a condition met, and download it afterwards. In this table COUPON I have USER_ID as number but I want to have a user name also, which is kept in another table USER.
How can I go to another table (USER) and take names (REALNAME) by this USER_ID which is the same in both tables?
if ( $_POST ) {
$team_id = abs(intval($_POST['team_id']));
$consume = $_POST['consume'];
if (!$team_id || !$consume) die('-ERR ERR_NO_DATA');
$condition = array(
'team_id' => $team_id,
'consume' => $consume,
);
$coupons = DB::LimitQuery('coupon', array(
'condition' => $condition,
));
if (!$coupons) die('-ERR ERR_NO_DATA');
$team = Table::Fetch('team', $team_id);
$name = 'coupon_'.date('Ymd');
$kn = array(
'id' => 'ID',
'secret' => 'Password',
'date' => 'Valid',
'consume' => 'Status',
);
$consume = array(
'Y' => 'Used',
'N' => 'Unused',
);
$ecoupons = array();
foreach( $coupons AS $one ) {
$one['id'] = "#{$one['id']}";
$one['consume'] = $consume[$one['consume']];
$one['date'] = date('Y-m-d', $one['expire_time']);
$ecoupons[] = $one;
}
down_xls($ecoupons, $kn, $name);
After this, I want to try to do the same thing using only SQL queries.
You would need to JOIN the tables in the SQL query
SELECT something FROM coupons as coupons JOIN user as user ON coupons.id=user.id
You should use join when you want to retrieve details from two tables.
Join table COUPON and table USER based on user_id . This should yield results you want.

insert multiple rows in a saveall in cakephp

i'm newbie in Cake and wodering how to insert multiple rows in a single saveall function,
i got this table,
CREATE TABLE IF NOT EXISTS `dates` (
`date` varchar(10) COLLATE utf8_unicode_ci NOT NULL
)
what i'm trying to do is let user select start date and end date using JQuery calander, once submit all the dates between this range will be saved into database, i already got the array of dates eg:
`array(
(int) 0 => '5/8/2013',
(int) 1 => '6/8/2013',
(int) 2 => '7/8/2013',
(int) 3 => '8/8/2013',
)
`
then my controller looks like this:
public function index(){
if ($this->request->is('post')) {
$this->Date->create();
$data = array();
$data['dates']=array();
$startDate = $this->request->data['Date']['from'];
$endDate = $this->request->data['Date']['to'];
$datesBlocked = $this->loopDates($this->request->data['Date']['from'],$this->request->data['Date']['to']);
$data['dates'][] = $this->request->data['Blockdate']['from'];
$data['dates'][] = $this->request->data['Blockdate']['to'];
/*foreach($datesBlocked as $data) {
$data['dates'][] = $data;
}*/
if($this->Date->saveAll($data)) {
$this->Session->setFlash(__('done'));
if ($this->Session->read('UserAuth.User.user_group_id') == 1) {
// $this->redirect("/manages");
}
}
}
public function loopDates($from,$to){
$blockdates = array();
$start = strtotime($from);
$end = strtotime($to);
debug($start);
$counter = 0;
for($t=$start;$t<=$end;$t+=86400) {
$d = getdate($t);
$blockdates[$counter++] = $d['mday'].'/'.$d['mon'].'/'.$d['year'];
}
debug($blockdates);
return $blockdates;
}
issue was i can't get foreach work, if i uncomment the foreach, i got error said Illegal string offset 'dates' , so i commented that and try to only add the start date and end date to the array to see if that works, then i got another error said.
`array(
'dates' => array(
(int) 0 => '08/05/2013',
(int) 1 => '09/05/2013'
)
)
`
Notice (8): Array to string conversion [CORE\Cake\Model\Datasource\DboSource.php, line 1005]Code
cuz i'm trying to insert 2 values into one field...i know it should be sth like
`array(
'dates' => array( (int) 0 => '08/05/2013',
)
'dates' => array((int) 1 => '09/05/2013'
))
`but can't figure out how to do it. Any help would be much appreciate!!!!
The structure you'll want your array to save multiple dates using saveAll() is this:
array(
'Date' => array(
0 => array(
'date' => '08/05/2013',
),
1 => array(
'date' => '09/05/2013',
)
),
)
I know that this is a little late, but to write multiple rows in a loop, you have to proceed the save with a create().
eg:
foreach($items as $lineItem){
$this->Invoice->create();
$this->Invoice->save(array(
'user_id'=>$property['User']['id'],
'invoice_id'=>$invId['Invoices']['id'],
'item_id'=>$lineItem['item_number'],
'quantity'=>$lineItem['quantity'],
'price'=>$lineItem['mc_gross']
);
}
Just thought it was worth mentioning, hopefully it will help someone.

Categories