Where statement in editor causes emails to stop

Where statement in editor causes emails to stop

davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0

I got my emails working when an edit is made but now when I add in the where clause it stops sending the emails lol. 1 step forward 1 step back.

  ->where( 'completed', 0, '=' )
 
        ->on('postEdit', function ($editor, $id, $values, $row) {
            $emailAddress = $row['requester'];
            $mail = new PHPMailer;
            $mail->setFrom('support@mysite.net', 'RMA Request');
            $mail->addReplyTo('noreply@mysite.net', 'No Reply');
            #$mail->addAddress(strval($emailAddress));
            $mail->addCC('davidm@mysite.net', 'David Morin');
            $mail->Subject = 'RMA Request Information';
            $mail->msgHTML('<b>Sku Request completed');
            if ($row['completed'] == '1') {
 
 
                $mail->Body = "
                <html>
                 <head>
                        <style>
                             .alnright { text-align: right; }
                        </style>
                 </head>
                 <body>
                        <img src='https://dev.mysite.net/assets/logo.png'><br />
                        <p>
                        SKU Request Complete
                        </p>
                        <p>
                        Item: ".$row['item_name']."<br />
                        Vendor: ".$row['vendor']."<br />
                        Barcode: ".$row['barcode']."
                 </body>
            </html>";
 
 
            $mail->send();
}
 
                        })
 
 
        ->process( $_POST )
        ->json();

I tried putting it at the end of the file as well but that did not help. What am I missing?

Answers

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    Looks like you are using the wrong event. The updated record will not be read back from the server because it probably doesn't comply with the WHERE condition any longer. Since it isn't read back the event can't be triggered.

    Are you setting the record to "completed" when editing?

    I would use either writeEdit or even validatedEdit depending on your specific requirements. On writeEdit the db update has been made. On validatedEdit the db update will be made because validation hasn't produced any errors.

    https://editor.datatables.net/manual/php/events

  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0

    @rf1234 I tried your recommendation but then it gives me an error on submit of update.

  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0
    edited August 2020

    Here is my whole script

    <?php
    /*
    # @Author: David Morin
    # @Date:   11-06-2020
    # @Filename: table.php
    # @Last modified by:   David Morin
    # @Copyright: IM Wireless - 2020
    */
    
    
    use PHPMailer\PHPMailer\PHPMailer;
    
    
    
    require '../../../vendor/autoload.php';
    include( "lib/DataTables.php" );
    
    use
        DataTables\Editor,
        DataTables\Editor\Field,
        DataTables\Editor\Format,
        DataTables\Editor\Mjoin,
        DataTables\Editor\Options,
        DataTables\Editor\Upload,
        DataTables\Editor\Validate,
        DataTables\Editor\ValidateOptions;
    
        Editor::inst( $db, 'sku_request' )
            ->fields(
                Field::inst( 'ID' ),
                Field::inst( 'dateAdded' )
                    ->validator( Validate::dateFormat( 'Y-m-d H:i:s' ) )
                    ->getFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) )
                    ->setFormatter( Format::datetime( 'Y-m-d H:i:s', 'Y-m-d H:i:s' ) ),
                Field::inst( 'ID' ),
                Field::inst( 'requester' ),
                Field::inst( 'district' ),
                Field::inst( 'item_name' ),
                Field::inst( 'vendor_cost' ),
                Field::inst( 'default_price' ),
                Field::inst( 'vendor' ),
                Field::inst( 'vendor_sku' ),
                Field::inst( 'manufacturer' ),
                Field::inst( 'man_sku' ),
                Field::inst( 'barcode' ),
                Field::inst( 'category' ),
                Field::inst( 'device_man' ),
                Field::inst( 'device_model' ),
                Field::inst( 'color' ),
                Field::inst( 'other_vendor' ),
                Field::inst( 'new_man' ),
                Field::inst( 'amazon_asin' ),
                Field::inst( 'update_sku' ),
                Field::inst( 'completed' ),
                Field::inst( 'update_new' ),
                Field::inst( 'dateAdded' ),
                Field::inst( 'date_modified' ),
    
            )
        ->where( 'completed', 0, '=' )
    
            ->on('postEdit', function ($editor, $id, $values, $row) {
                $emailAddress = $row['requester'];
                $mail = new PHPMailer;
                $mail->setFrom('support@.net', 'SKU Request');
                $mail->addReplyTo('noreply@.net', 'No Reply');
                #$mail->addAddress(strval($emailAddress));
                $mail->addCC('davidm@.net', 'David Morin');
                $mail->Subject = 'SKU Request Completed';
                $mail->msgHTML('<b>Sku Request completed');
                if ($row['completed'] == '1') {
    
    
                    $mail->Body = "
                    <html>
                     <head>
                            <style>
                                 .alnright { text-align: right; }
                            </style>
                     </head>
                     <body>
                            <img src='https://dev..net/assets/logo.png'><br />
                            <p>
                            SKU Request Complete
                            </p>
                            <p>
                            Item: ".$row['item_name']."<br />
                            Vendor: ".$row['vendor']."<br />
                            Barcode: ".$row['barcode']."
                     </body>
                </html>";
    
    
                $mail->send();
    }
    
                            })
    
        ->debug( true )
            ->process( $_POST )
            ->json();
    
    
  • tangerinetangerine Posts: 3,348Questions: 36Answers: 394

    it gives me an error

    Don't tell us what the error says. Guessing is much more fun.

  • rf1234rf1234 Posts: 2,806Questions: 85Answers: 406

    @tangerine is right. This is my last attempt on guessing for tonight :smile:

    What you post is your old code. It is not going to work in case you update "completed" to be no longer zero after the update. Again: Are you doing this or not?

    If so you need to replace your event with the new event (e.g. writeEdit) and you also need to consider that there is no record read back from the server in that case.

    Which means: No $row gets passed in the event handler (that would have been the values read from the server) but you will need to deal with $values (the values submitted to the server).

  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0
    edited August 2020

    Thank you. I got what you mean now. It is now working. Thank you very much.

This discussion has been closed.