I am working on vTiger 6.5 and I am trying to figure a way to see if a record exists in a custom module of mine. I want to check whether the 'policynumber' is new before saving, here is my code so far. For some reason it seems to act randomly depending on my module number chosen.
class isaHandler extends VTEventHandler {
function handleEvent($eventName, $entityData) {
global $adb;
$moduleName = $entityData->getModuleName();
if($moduleName=='isa'){
if($eventName == 'vtiger.entity.beforesave.modifiable') {
$isNew = $entityData->isNew('policynumber');
if ($isNew == false) {
echo "Duplicate policy number";
exit;
}
}
if($eventName == 'vtiger.entity.beforesave') {}}
if($eventName == 'vtiger.entity.beforesave.final') {
$price = $entityData->get('currentamount');
if($price > 20000){
echo "Please go back and enter less than 20000";
exit;
}
if($eventName == 'vtiger.entity.aftersave') {}
}
}
At the moment I am currently using an echo just to see the result. But later on I will perform more than this.
isNew()
Returns true if new record is being created, false otherwise.
More info is here
you should write a custom query to check policynumber already exist or not in your function:
if($eventName == 'vtiger.entity.beforesave.modifiable') {
global $adb;
$result = $adb->pquery("SELECT your-field-name FROM table_name WHERE policynumber=?", array($policynumbervalue));
if($result && $adb->num_rows($result)) {
echo "This policy number exist";
die();
}else{
// write your overwrite code
}
} //end if($eventName == 'vtiger.entity.beforesave.modifiable')
Update:
I am assuming there is field i.e. policynumber in your form, you enter some value in this field and submit the form. so you will get entered policy number value from this:
$policynumbervalue = $entityData->get('policynumber'); //this is vtiger standard way
if this does not work, you can simply use php global variable $_REQUEST['policynumber'] but I is not a good practice.
Hope this will help.
This is the update to my answer, I simply done an if statement on the number of rows displayed.
if($eventName == 'vtiger.entity.beforesave.modifiable') {
$policynumbervalue = $entityData->get('policynumber');
$sql = $adb->pquery("SELECT policynumber FROM vtiger_isa WHERE policynumber=?",array($policynumbervalue));
$nrows = $adb->num_rows($sql);
if($nrows > 0){
echo "<script type=\"text/javascript\">window.alert('ISA policy number already exists, you will be redirected to the updata module.');
window.location.href = '/vtigercrm/index.php?module=isa&view=List';</script>";
exit;
}
Related
I have a login form currently setup that confuses my users.
The way i handle errors is like this;
if (!($result->total > 0)) {
$err[] = "License key is not in our system.";
}
if ($claimed == 1) {
err[] = 'License key has been claimed already.';
}
if ($userID > 0) {
$err[] = 'License key is already connected to a user.';
}
if ($banned == 1) {
$err[] = 'License key is banned';
}
so for example, if one of my users would input a invalid license key instead of showing that it is not in our system it would show banned(creating confusion). Because i'm not exiting the code and letting it run.
I'm wondering how to go on about error handling when my functions are set up like this.
update -
Forgot to show how i'm displaying the error.. my fault!
if (empty($err)) {
//no errors
} else {
echo $err; //this will show the last error instead of the first error generated
}
OK Bob,
It would be helpful if you were showing us how you are presenting your errors, as what you are explaining would suggest that your $err array would then contain two values, not just (the last) one.
However, what I think is going on here is that your $banned condition will always be met; unless you add another = to your if statement, like this:
if (!($result->total > 0)) {
$err[] = "License key is not in our system.";
}
if ($claimed == 1) {
err[] = 'License key has been claimed already.';
}
if ($userID > 0) {
$err[] = 'License key is already connected to a user.';
}
if ($banned == 1) { # <-- Here
$err[] = 'License key is banned';
}
Then for testing purposes you can view the array of errors:
if(isset($err) && !empty($err)){
print_r($err);
}
If you want to loop through each potential error:
if(isset($err) && !empty($err)){
foreach($err as $error){
echo "Error because: {$error}".PHP_EOL;
}
}
So you are adding all the errors to the array err.
To display the first item in an array, just use [0] to access the first index.
if (empty($err)) {
//no errors
} else {
echo $arr[0];
}
I am checking data if its not exist than insert. so i am using if else condition.
take a glance on code.
if($_POST['save_appointment']){
if(is_user_logged_in()){
$user_appontment_sql = "select * from ".$wpdb->prefix."table where status=1 AND event_id=1 AND user_id=2";
$get_user_data_row = $wpdb->get_row($user_appontment_sql);
if(isset($get_user_data_row)){
echo '<div class="saved_thing">'.__('You have already filled form for appointment for this date').'</div>';
}
else{
$push_form_data=array();
if($_POST['username']){
$user_data=array('username'=>$_POST['username'],'usernamelabel'=>$_POST['usernamelabel']);
$user_name=array('name_data'=>$user_data);
array_push($push_form_data,$user_name);
}
$form_data_all=json_encode($push_form_data);
$done = add_appointment($_POST,$form_data_all);
if($done==true)
{
echo '<div class="saved_thing">'.__('Mange tak. Du hører fra os snarest !').'</div>';
$user_status_mail = "select * from ".$wpdb->prefix."volunteer_app_setting where meta_key='admin_email' OR meta_key='email_send_status' OR meta_key='email_template_accept'";
$get_appointment_send_mail = $wpdb->get_results($user_status_mail);
$event_metadata=unserialize($event_datas_count[0]->pstdata_value);
if($event_metadata['notification_mail']=='1')
{
require_once(dirname(dirname(dirname(dirname(__FILE__))))."/volunteer-appointment/mail/send_mail.php");
$selected_template=$event_meta_data['mail_temp_form_submission'];
$from=$get_appointment_send_mail[0]->meta_value;
$data=array('first_name'=>$_POST['username'],'send_to_admin'=>1,'event_date'=>get_post_meta($_POST['event_id'],'event-date',true),'event_name'=>get_the_title());
$current_user = wp_get_current_user();
$send_mail=mail_user($current_user->user_email,$selected_template,$from,$data);
if($send_mail){
echo '<div class="saved_thing">'.__('E-mail notifikation afsendt til administrator').'</div>';
}
else{
//echo "<script>alert('Mail not send');</script>";
}
}
}
else
{
echo '<div class="saved_thing">'.__('Fejl!!!').'</div>';
}
//}
return;
}
}
}
first we check the data, if its not exist than go to else part.
its is going in if part all the time even data is not exists but saving data . In else part add_appointment() is the issue. this function is saving the data.
I debug
code is checking the condition and if data is not exist in database than go to else part, save data using the function add_appointment() and after executaion of the function again its going to else condition and showing message You have already filled form for appointment for this date
Change your if condition to
from -> if(isset($get_user_data_row))
to -> if(!empty($get_user_data_row))
If no rows are found, it will return 0
if($get_user_data_row > 0) {
// Some code here
} else {
// Some functions here
}
I am retrieving the details from the database if the condition is satisfied.
But when the condition is not satisfied, empty result is retrived.
So how to have a condition if status is empty
$sq = mysqli_query($link, $query);
$ro = mysqli_fetch_array($sq);
$status = $ro['status'];
if ($status == 1) {
header("location: paymentPage.php");
} elseif ($status == 0) {
header("location:login.php");
} elseif ($status == '\0') {
header("location:SignUp.php");
}
But I am not able to redirect to signup page if the status is empty.Is status=='\0' is correct to go for signup page.
If your column status has integer type in DB then it can't have value '\0' because '\0' is a string.
Maybe you should use is_null function?
For example:
else if(is_null($status))
{
header("location:SignUp.php");
}
In all case, mysqli_fetch_array function will return array|null|false types. It means that, if your connection didn't establish or data is empty, your response will return empty array or empty string|boolean item. Therefore, you can check if is exits by any case like that:
if( !($row = mysqli_fetch_array($sq)))
{
// Data doesn't exists
header("location:SignUp.php");
}else{
$status = $ro['status'];
echo 'My other validations';
}
To read documentation about function look at: https://www.php.net/manual/tr/mysqli-result.fetch-array.php
I have this code:
function saveField($field, $id, $module, $value)
{
$bean = BeanFactory::getBean($module, $id);
if (is_object($bean) && $bean->id != "") {
if ($bean->field_defs[$field]['type'] == "multienum") {
$bean->$field = encodeMultienumValue($value);
}else if ($bean->field_defs[$field]['type'] == "relate" || $bean->field_defs[$field]['type'] == 'parent'){
$save_field = $bean->field_defs[$field]['id_name'];
$bean->$save_field = $value;
if ($bean->field_defs[$field]['type'] == 'parent') {
$bean->parent_type = $_REQUEST['parent_type'];
$bean->fill_in_additional_parent_fields(); // get up to date parent info as need it to display name
}
}else{
$bean->$field = $value;
}
//return here will work
$bean->save(); //this works
//nothing works here
return getDisplayValue($bean, $field);
} else {
return false;
}
}
The problem here is that anything under
$bean->save()
will not work. But I know that save is working as the values are being updated. So how can I debug this problem?
I already tried:
return var_dump($bean->save());
return print_r($bean->save());
if($bean->save()){
return "1";
}else{
return "2";
}
And none of those in the above worked I still get nothing in my return.
There is likely something such as an after_save logic hook that is executing and either causing a fatal error or doing an exit.
Try using xdebug, it should allow you to investigate further into the save method that fails.
This may help a few people using Aicontactsafe with joomla as the core component does not have it currently as an option. Essentially i need to change the module php file to allow different redirect urls depending on what dropdown option is selected in the "Studio" Combobox field.
Perfectly honest with you i do not know if i am typing in the correct fields or code values:
The test site you can see here - http://www.datumcreative.com/wellness/ and the form is 'book my taster class' on the right.
Below is where i believe code needs adding in the original file:
if (!array_key_exists('return_to', $parameters)) {
// initialize the database
$db = &JFactory::getDBO();
$query = 'SELECT redirect_on_success FROM #__aicontactsafe_profiles WHERE id = ' . $pf;
$db->setQuery( $query );
$redirect_on_success = $db->loadResult();
// set the return link to the current url or the one setup into the profile
if (strlen(trim($redirect_on_success)) == 0) {
$postData['return_to'] = $currentUrl;
} else {
$postData['return_to'] = $redirect_on_success;
}
}
To create the other redirects i added the following custom lines, below the line "$redirect_on_success = $db->loadResult();":
$redirect_to_wgc = "http://www.google.com";
$redirect_to_harp = "http://www.yahoo.co.uk";
I then needed to add the "if" statements **.
**if ($field_values['aics_studio'] == "Welwyn Garden City") {
$postData['return_to'] = $redirect_to_wgc;
}
elseif ($field_values['$aics_studio'] == "Harpenden") {
$postData['return_to'] = $redirect_to_harp;
}**
elseif (strlen(trim($redirect_on_success)) == 0) {
$postData['return_to'] = $currentUrl;
} else {
$postData['return_to'] = $redirect_on_success;
}
No luck so far, any suggestions?