Custom Button to Email a Group

SeanCrandall

New member
I know this is super basic, but I just don't know much about JavaScript. (I'm an old hand at C/C++ and I've taught myself some PHP, definitely not a seasoned web developer).

My application helps me manage my docket (I'm a patent attorney). I have a form that has "Cases" with a list of one or more Inventors linked to each Case. My database includes the inventor email addresses. I have a custom PHP function that takes a $case_id, and returns a link like <a href="mailto: [inventor_list]?subject=[subject]">Email Inventors</a>. This function works great on my custom dashboard, which shows me items currently due.

I want to add a custom button to my Cases form, "Email Inventors," so I can auto-generate an email to the inventors if the case isn't currently on my dashboard. I have added the button, and I can use a PHP callback to get the link, but then I'm not sure what to do with it. I think what I need to do is use a JavaScript callback instead to open the link. But looking at the documentation, the JavaScript callback doesn't seem to get the $case_id like the PHP callback.

Again, I am a total moron with JavaScripts, so maybe I'm missing something obvious. Is there a way for the JavaScript callback to get the $case_id, call the PHP function that returns the link, and then open the link?


Your current DaDaBIK version​


You are using DaDaBIK version 11.9-Elba enterprise, installed on 05-15-2023 (installation code: 1811062e2f139af96e), the latest version of DaDaBIK is 11.9-Elba released on 03-23-2023

You are running the latest release of DaDaBIK

In case you want to upgrade to a more powerful edition (from Pro to Enterprise/Platinum, from Enterprise to Platinum) please contact us.


System info​


PHP Version: 8.1.19


mysql version: 10.8.8-MariaDB-1:10.8.8+maria~ubu2204


Web server: Apache/2.4.56 (Debian)


Client: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0
 

SeanCrandall

New member
I couldn't figure out how to make JavaScript work. As far as I could tell, I couldn't even get the button to call the JavaScript function (I even stripped it down to
Code:
alert("You clicked the email button");
... and nothing happened).

But I found a solution. It's not a good solution, but it gets there, if inelegantly.

At the end of the PHP callback, I simply added:

Code:
$link = [call to my PHP function to build the link];
echo $link.

This overwrites the whole page, so that the only thing displayed is a blank page with a hyperlink that says "Email Inventors." If I click that link, it opens my email client with the right addresses, subject, etc.

Not pretty, not elegant, not "production ready," but functional.

If anybody has a better solution, I'd still love to hear it.
 

SeanCrandall

New member
I figured it out.

I don't know how, but I finally got it to call the correct JavaScript callback. (Maybe it was a caching issue or something).

This is the JavaScript callback that the button calls:
Code:
function dadabik_js_email_inventors()
{
var xhr = new XMLHttpRequest();

var case_id = document.getElementsByName('id_case')[0].value;
console.log("Email inventors for case ID " + case_id);

    var url = "index.php?function=show_static_page&id_static_page=1";
    url += "&_cp_case_id=" + case_id;
xhr.onload = function(){
        target = this.responseText;
        console.log("Full response was: " + target);
        console.log("Target is: " + target);
window.location.href = target;
}

xhr.open("GET",url,true);
xhr.send();

}

(One node: "id_case" is the primary index in my Cases table. It auto-increments and I had the field disabled on my forms in the Permissions, but I had to make it "Yes, But Disabled" so that it was visible. It's the only way I could find to pass that value to my JavaScript callback function.)

The URL is just the link to my Dashboard, which is my homepage, but it can actually be a call to any page on my site, because I put the PHP handler into header.php. I needed the code in the header so I could put it first, call it, and then get out without printing a bunch of other HTML as my output.

I put the actual code into a file called handle_cp_codes.php, and added the following lines to header.php, at the top (but within the <?php tag):

Code:
require_once('handle_cp_codes.php');
_cp_handle_get($_GET);
(For those who are clueless like me, $_GET is a global variable in all PHP code that is an associative array of GET codes in the URL).

handle_cp_codes.php has the following function:
Code:
function _cp_handle_get($get)
{
    $getcode = "_cp_case_id";

    $_cp_response = null;

    if (array_key_exists($getcode, $get)) {
        $case_id = htmlspecialchars($get[$getcode]);

        if ($case_id == null) {
            $_SESSION['Error'] = "Malformed request. Requested emails, but no Case ID provided.";
            exit("Malformed Request. No Case Id provided.");
        } else {
            $_cp_response = _dm_get_email_target($case_id); //my utility function that gets an email list by case_id
        }
 
        exit(  $_cp_response ); //write out the target, and exit. Kills the rest of the page.
    } // end of process GET codes

}

Since this is at the top of the header, if any call to the page has the GET code, this function processes the GET code, and then exits, using $_cp_repsonse as the output of the page. Since we exit, there's no additional code on the page, and we just get the email list I wanted.

Not only is this useful for my specific use case, but (after spending way too long tinkering with this), I finally figured out how to do AJAX on Dadabik. If I later have more get codes, I can break them down. Like I can do ?_need_cp=true&_cp_function=email_inventors&_cp_case_id=109

Then I can check for _need_cp, and if it's found use a case statement to decide which function we're doing, and use other GET codes to provide any necessary parameters.

Now I have a button on my Cases edit and details screens that says "Email Inventors." If I click on the button, it launches an email in Outlook addressed to all the inventors with a subject line that has the correct docket numbers and is marked attorney-client privileged and confidential. Exactly what I wanted.
 
Last edited:
Top