Jump to content

Update vars using ClientEdit hook


dezio

Recommended Posts

I am trying to edit phone number before it gets updated in db but I cannot figure out can I do that.

 

I am using this code:

function hook_filter_clients_data($vars) {
$vars['phonenumber'] = preg_replace("/[^0-9]/", '', $vars['phonenumber']);
return $vars;
}

add_hook("ClientEdit",1,"hook_filter_clients_data");

 

but it doesnt work. I also tried to change $vars to &$vars but still nothing...

 

Is there any other way to do it?

Link to comment
Share on other sites

I am trying to edit phone number before it gets updated in db but I cannot figure out can I do that.

 

I am using this code:

function hook_filter_clients_data($vars) {
$vars['phonenumber'] = preg_replace("/[^0-9]/", '', $vars['phonenumber']);
return $vars;
}

add_hook("ClientEdit",1,"hook_filter_clients_data");

 

but it doesnt work. I also tried to change $vars to &$vars but still nothing...

 

Is there any other way to do it?

 

First off why are you doing this in this first place?

 

It seems like your trying to sanitize it? Or something? Which you wouldnt need to do. Cus whmcs does that im sure.

 

but in case you do need it for some reason:

 

Your problem is you cant send data back into the $vars variable. returning $vars just sends vars back to the function that called the hook. and it will do nothing with it there.

 

so to do this you need to use php mysql or mysqli. I recommend starting using mysql if u havent done any before. You'll have to update tblclients phone number column. based on the userid of the user being edited which you can find in $vars["params"] so its gonna look something like

$userid = $vars["params"]["userid"];

 

 

then your gonna have to do

 

 

UPDATE tblclients SET phonenumber=? WHERE userid=?

 

 

the ? marks are the parameters. So you'll have to look up how to do that. mysqli and mysql do it differently.

Link to comment
Share on other sites

Thanks for reply synik4l.

 

I do it because Resell.biz module doesnt create phone number in a right format when registering new client in their API.

 

So this is solution I tried and it works :)

 

function hook_filter_clients_data($vars) {
$phonenumber = preg_replace( array( "/[^0-9]/", "/^[0]{1}/" ), array( '', '' ), $vars['phonenumber']);

$table = "tblclients";
$update = array( 'phonenumber' => $phonenumber );
$where = array( 'id' => $vars['userid'] );

update_query($table, $update, $where);
}

add_hook("ClientAdd",1,"hook_filter_clients_data");
add_hook("ClientEdit",1,"hook_filter_clients_data");

Link to comment
Share on other sites

Thanks for reply synik4l.

 

I do it because Resell.biz module doesnt create phone number in a right format when registering new client in their API.

 

So this is solution I tried and it works :)

 

function hook_filter_clients_data($vars) {
$phonenumber = preg_replace( array( "/[^0-9]/", "/^[0]{1}/" ), array( '', '' ), $vars['phonenumber']);

$table = "tblclients";
$update = array( 'phonenumber' => $phonenumber );
$where = array( 'id' => $vars['userid'] );

update_query($table, $update, $where);
}

add_hook("ClientAdd",1,"hook_filter_clients_data");
add_hook("ClientEdit",1,"hook_filter_clients_data");

 

Do they really do it wrong? Dang...looks like ill have to fix that too before we go live...Thanks for info. Glad to help

Link to comment
Share on other sites

Depends in which format your customers enter their phone number.. Please note preg_replace function. I wanted to remove leading zero, so you must remove that part for your customers if you dont want leading zero from phone number to be removed.

Edited by dezio
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated