Front-End Editor and "record_postsave" Hook

By Perchpole - October 18, 2014

Hello, All -

I wonder if someone could help me with a small problem...

I've created a simple front-end admin page for a client which lets them control user access to their web site. It's nothing more than a form with a couple of fields and a submit button. It allows my client to enable/disable membership to the site. This is important because when a user first registers their account is disabled until my client activates it.

So far - so good. The only trouble is the notification email...

When the account is activated an email is sent to the user telling them they are now able to log in to the members area. This works fine if the user's account is updated in CMSB - but not using my front-end admin page. It does not trigger the "record_postsave" plugin hook which would fire the notification email.

How can I fix this?

My front-end page updates data directly on the database. It therefore does not need to intereact with CMSB. Is there a way for my front-end page to instruct CMSB to fire the "record_postsave" hook - which would then send out the email?

Thanks,

Perchpole

By gregThomas - October 20, 2014

Hi Perch,

The simplest solution would be to call the function that the postsave action calls from your front end page, this will be the second variable in the postsave addAction function. You'll need to look at what variables the function needs to receive, and ensure you pass those into the function as well. 

Is the plugin that calls postsave a custom one? I had a look through the website membership plugin, but couldn't find a function that gets called after a records is saved.

Thanks,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Perchpole - October 20, 2014

Hi, greg -

Thanks for your input. The plugin is "emailOnApproved" - which is avalable on the forum somewhere. It works well - appart from in my instance!

:0)

Perchpole

By Perchpole - October 20, 2014

Greg -

The emailOnApproved plugin has the following code at the top

addAction('record_postsave', 'emailOnApproved_sendPassword', null, 4);

//
function emailOnApproved_sendPassword($tableName, $isNewRecord, $oldRecord, $recordNum) {
  global $CURRENT_USER, $SETTINGS;
  $fieldname = 'disabled';

  // error checking
  if ($tableName != 'accounts') { return; }
  if (!array_key_exists($fieldname, $CURRENT_USER)) {
    die(__FUNCTION__ .": You must create an accounts fields called '$fieldname'!");
  }

When I save an account using my front-end editor it uses a file called save.php. The file ends with the following code:

...
$colsToValues['disabled']         = (@$_REQUEST['disabled'] == "Yes")? '1' : '0' ;
mysql_update($tablename, $recordNum, $where, $colsToValues);

Presumably I need to put the addAction code at the end of my file - somewhere?!

:0S

Perchpole

By gregThomas - October 20, 2014

Hi Perch,

You're close, you actually need to put emailOnApproved_sendPassword function into the page you've created. You'll need to ensure that you pass the four variables ($tableName, $isNewRecord, $oldRecord, $recordNum) into the function, and should find that the user will be e-mailed. 

If you want I can help you with setting up the variables that need passing into the function, but you'll need to post the code you've currently got that adds the user to the database.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By gregThomas - October 20, 2014

Hi Perch,

I think you need to do something like this:

  if(@$_REQUEST['uNum']) { //SAVE
  
      $tablename                        = 'accounts';
      $recordNum                        = @$_REQUEST['uNum'];
      $where                            = null;
      $colsToValues                     = array();
      $colsToValues['updatedDate=']     = 'NOW()';
      $colsToValues['updatedByUserNum'] = @$_REQUEST['currentUser'];
      $colsToValues['notes']            = @$_REQUEST['notes'];
      $colsToValues['disabled']         = (@$_REQUEST['disabled'] == "Yes")? '1' : '0' ;
      mysql_update($tablename, $recordNum, $where, $colsToValues);
      
      $status = array('msg' => 'success', 'id' => $recordNum);

      //Get old record
      $oldRecord = mysql_get($tableName, $recordNum);

      //Send update e-mails
      emailOnApproved_sendPassword($tableName, false, $oldRecord, $recordNum);
      
      echo json_encode($status);
        
      $_REQUEST = array();
  }

This is just example code, so you might have to make a few changes to get it working.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Perchpole - October 20, 2014

Thanks Greg -

Unfortunately it doesn't seem to like this line:

emailOnApproved_sendPassword($tableName, false, $oldRecord, $recordNum);

It's throwing an "Uncaught SyntaxError: Unexpected token W" error.

:0/

Perchpole

By gregThomas - October 27, 2014

Hi Perch, 

Would it be possible to send me the full error message? That should help me track down what could be causing the issue.

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Perchpole - October 27, 2014

Hi, Greg -

The message in the earlier post is how the problem is reported in Chrome.

In Firefox it says:

SyntaxError: JSON.parse: unexpected character at line 2 column 1 of the JSON data
var status = JSON.parse(data);

NB: The "emailOnApproved" plugin does not fire.

Feel free to contact me at info at perchpolemedia dot com and I'll let you have a look at the thing.

:0/

Perch