ArtiGrid
v1.9

Bulk Custom Action

This example demonstrates how to create a custom bulk action in ArtiGrid. Users can select multiple records from the grid and execute a custom PHP script by clicking the Generate Invoice button.

The selected record IDs are sent automatically via AJAX to invoice.php, where you can perform any server-side task, such as generating PDF invoices, exporting data, sending emails, updating records, or integrating with third-party APIs.

In this demo, the selected products are retrieved from the database using Queryfy, formatted into an HTML email, and sent using ArtiGrid's built-in sendMail() method.


<?php
    $grid = new ArtiGrid();
    $grid->table('products')
        ->template('bootstrap5')
        ->formFields(['productName','productLine'])
        ->modal()
        ->addBulkButton(
            "Generate Invoice",
            "invoice.php",
            "fa fa-file-invoice",
            "btn btn-warning",
            [],
            [
                'title' => 'Generate an invoice?',
                'text'  => 'An invoice will be generated for the selected records.',
                'icon'  => 'question'
            ]
        );
    echo $grid->render();
?>


// invoice.php
<?php

require __DIR__ . '/../../artigrid/ArtiGrid.php';

$ids = json_decode($_POST['ids'] ?? '[]', true);
$table = $_POST['table'] ?? '';
$grid_id = $_POST['grid_id'] ?? '';

if (empty($ids)) {
    echo json_encode([
        'success' => false,
        'message' => 'No records selected'
    ]);
    exit;
}

$db = DB::connect();
$q = new Queryfy($db);

$products = $q->table('products')
    ->where('id', $ids, 'IN')
    ->get();

$body = '
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body style="margin:0;padding:30px;background:#f5f5f5;font-family:Arial,Helvetica,sans-serif;">
<div style="max-width:700px;margin:auto;background:#ffffff;border:1px solid #ddd;border-radius:8px;overflow:hidden;">
    <div style="background:#0d6efd;color:#fff;padding:20px;">
        <h2 style="margin:0;">ArtiGrid Demo</h2>
        <p style="margin:5px 0 0;">Selected Products</p>
    </div>
    <div style="padding:25px;">
        <p style="margin-top:0;">
            The following products were selected from the grid:
        </p>
        <table width="100%" cellpadding="10" cellspacing="0" style="border-collapse:collapse;">
            <thead>
                <tr style="background:#f8f9fa;">
                    <th align="left" style="border:1px solid #dee2e6;">ID</th>
                    <th align="left" style="border:1px solid #dee2e6;">Product</th>
                    <th align="left" style="border:1px solid #dee2e6;">Category</th>
                </tr>
            </thead>
            <tbody>
';
foreach ($products as $product) {
    $body .= '
         <tr>
             <td style="border:1px solid #dee2e6;">'.$product['id'].' </td>
             <td style="border:1px solid #dee2e6;">'.$product['productName'].' </td>
             <td style="border:1px solid #dee2e6;">'.$product['productLine'].' </td>
         </tr>';
}
$body .= '
        </tbody>
        </table>
        <br>
        <p style="margin-bottom:0;">
            This email was generated automatically by
            <strong>ArtiGrid Custom Bulk Action Demo</strong>.
        </p>
    </div>
    <div style="background:#f8f9fa;padding:15px;text-align:center;font-size:12px;color:#666;border-top:1px solid #dee2e6;">
        © '.date('Y').' ArtiGrid. All rights reserved.
    </div>
</div>
</body>
</html>';

$grid = new ArtiGrid();
$result = $grid->sendMail(
    'daniel.telematico@gmail.com',
    'Selected products from ArtiGrid',
    $body
);

echo json_encode([
    'success' => (bool)$result,
    'message' => $result
        ? 'Email sent successfully'
        : 'The email could not be sent'
]);

?>

id productCode productName productLine productScale productVendor productDescription quantityInStock buyPrice MSRP Actions