<?php
global $gdb;

$grid = new \grid( 'pp_dictionaries_units' );
$grid -> gdb_opt      = $gdb;
$grid -> sql          = 'SELECT *'
                            . 'FROM ( '
                              . 'SELECT '
                                . 'id,'
                                . '( SELECT text FROM pp_units_langs AS psl, pp_langs AS pl WHERE lang_id = pl.id AND unit_id = ps.id AND text != \'\' ORDER BY o ASC LIMIT 1 ) AS text '
                              . 'FROM '
                                . 'pp_units AS ps '
                              . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] '
                          . 'ORDER BY '
                            . '[order_p1] [order_p2]';
$grid -> sql_count    = 'SELECT '
                            . 'COUNT(0) FROM ( '
                              . 'SELECT '
                                . 'id,'
                                . '( SELECT text FROM pp_units_langs AS psl, pp_langs AS pl WHERE lang_id = pl.id AND unit_id = ps.id AND text != \'\' ORDER BY o ASC LIMIT 1 ) AS text '
                              . 'FROM '
                                . 'pp_units AS ps '
                            . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] ';
$grid -> debug        = true;
$grid -> order        = [ 'column' => 'id', 'type' => 'ASC' ];
$grid -> search       = [
                            [ 'name' => 'Tekst', 'db' => 'text', 'type' => 'text' ]
                        ];
$grid -> columns_view = [
                          [
                            'name' => 'Lp.',
                            'th' => [ 'class' => 'g-lp' ],
                            'td' => [ 'class' => 'g-center' ],
                            'autoincrement' => true
                          ],
                          [
                            'name'  => 'Tekst',
                            'db'    => 'text',
                            'php'   => 'echo "<a href=\'/admin/dictionaries/unit_edit/id=[id]\'>[text]</a>";',
                            'sort'  => true
                          ],
                          [
                            'name' => 'Edytuj',
                            'action' => [ 'type' => 'edit', 'url' => '/admin/dictionaries/unit_edit/id=[id]' ],
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 70px;' ],
                            'td' => [ 'class' => 'g-center' ]
                          ],
                          [
                            'name' => 'Usuń',
                            'action' => [ 'type' => 'delete', 'url' => '/admin/dictionaries/unit_delete/id=[id]' ],
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 70px;' ],
                            'td' => [ 'class' => 'g-center' ]
                          ]
                        ];
$grid -> buttons      = [
                          [
                            'label' => 'Dodaj jednostkę miary',
                            'url'   => '/admin/dictionaries/unit_edit/',
                            'icon'  => 'fa-plus-circle',
                            'class' => 'btn-success'
                          ]
                        ];
echo $grid -> draw();