custom button in cmsb to perform custom action

4 posts by 3 authors in: Forums > CMS Builder
Last Post: March 1, 2013   (RSS)

By zaba - February 25, 2013

Hi Guys,

I have an order table that stores the orders taken from a sagepay cart script, it records the name of the purchaser, address, email, and details of the order after it has been sent to sagepay. What my client has asked for is a "button to press" once they are viewing a particular order, they can press the button that basically fires an email off to the email address saying that the order has been despatched.

This button itself can be basically trigger a handle script that will sit on the server passing to it the num, the script can pull all the data based on the num passed to it and send out the email.

My question is can I add such a button to the cms (its not a question of writing the handle script to do the work) so basically the button action is /performmyscript.php?num=xx and the script would return them back to the cms record they are currently viewing, with some means of saying despatch email sent.

Im not sure this is possible or how I would go about doing it, it only needs to appear on the orders section of the cms.

By gregThomas - February 25, 2013

Hi, 

This is possible, but will require a custom plugin. I've created a very basic plugin that demonstrates how you could do it:

<?php
/*
Plugin Name: Test Plugin - 
Description: test plugin
Version: 1.00
Requires at least: 2.50
*/


//Add button to orders menu
addFilter('listRow_actionLinks',  'dispatch_order',           null, 3);

//Send update on link click
pluginAction_addHandlerAndLink('Send an update e-mail', 'sendUpdate', true);

addAction('record_preedit', 'sendUpdate', null, 0);



// add "create record here" link
function dispatch_order($actionLink, $tableName, $record) {
  global $schema;



  // Add a line with the url for each section
  if (@$tableName == 'orders') { $actionLink .= '<a target="_blank" href="?sendMessage=true&menu='.$tableName.'&action=edit&num='.$record['num'].'" >Test link</a>'; }


  return $actionLink;
}


//Function for sending message
function sendUpdate(){

  if(@$_REQUEST['menu'] = 'orders' && @$_REQUEST['sendMessage'] == 'true'){

    /*
    *
    * YOUR SCRIPT WOULD GO HERE
    *
    *
    */

    alert('Message sent to'.$_REQUEST['num']);

  }
}

This is example code, so you will need to modify it to work with your site.

So this plugin will add a link called 'test link' to the actions area of a section list page. In this example it only adds the link when the table name is orders. 

I've set up the link so that it will edit a record, but also passes the variable sendMessage in the URL as well. 

Then I added another plugin hook called record_preedit. When a record is edited, it will launch the function sendUpdate, this function will check if the sendMessage variable is in the URL, and that we are on the orders menu.

Finally, an alert message will display at the top of the page that says 'Message sent to ' and the record number.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Dave - February 25, 2013

Hi zaba, 

And here's a more low-tech solution as well so you have some options.  Just add a new separator field in the Section Editor and enter this for HTML:<tr>

 <td colspan='2'>
   <?php if (!$GLOBALS['RECORD']): ?>
      Save record first to send email.
    <?php else: ?>
      <a href="/script.php?<?php echo $GLOBALS['RECORD']['num']; ?>">Send email</a>
    <?php endif ?>
  </td>
</tr>

Then either redirect or link people back to the edit page when you are done: 

/cmsAdmin/admin.php?menu=orders&action=edit&num=123

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com