<?php
global $gdb;

$grid = new \grid( 'globelus_candidates_unregistered' );
$grid -> gdb_opt      = $gdb;
$grid -> sql          = 'SELECT *'
                            . 'FROM ( '
                              . 'SELECT '
                                . 'gaa.id, CONCAT( surname, \' \', name ) AS candidate_name, phone, email, date_add, cv, cv_extension '
                              . 'FROM '
                                . 'globelus_adverts_answers AS gaa '
                              . 'WHERE '
                                  . 'user_id IS NULL '
                                . 'AND '
                                  . 'cv IS NOT NULL '
                              . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] '
                          . 'ORDER BY '
                            . '[order_p1] [order_p2]';
$grid -> sql_count    = 'SELECT '
                            . 'COUNT(0) FROM ( '
                              . 'SELECT '
                                . 'gaa.id, CONCAT( surname, \' \', name ) AS candidate_name, phone, email, cv, cv_extension '
                              . 'FROM '
                                . 'globelus_adverts_answers AS gaa '
                              . 'WHERE '
                                  . 'user_id IS NULL '
                                . 'AND '
                                  . 'cv IS NOT NULL '
                            . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] ';
$grid -> debug        = true;
$grid -> order        = [ 'column' => 'date_add', 'type' => 'DESC' ];
$grid -> search       = [
                            [ 'name' => 'Imię i nazwisko', 'db' => 'candidate_name', 'type' => 'text' ],
                            [ 'name' => 'Telefon', 'db' => 'phone', 'type' => 'text' ],
                            [ 'name' => 'Email', 'db' => 'email', 'type' => 'text' ],
                            [ 'name' => 'Data', 'db' => 'register_date', 'type' => 'date_range' ]
                        ];
$grid -> columns_view = [
                          [ 
                            'name' => 'Lp.',
                            'th' => [ 'class' => 'g-lp' ],
                            'td' => [ 'class' => 'g-center' ],
                            'autoincrement' => true 
                          ],
                          [
                            'name'  => 'Data',
                            'db'    => 'register_date',
                            'td'    => [ 'class' => 'g-center', 'style' => 'width: 200px;' ],
                            'th'    => [ 'class' => 'g-center' ],
                            'php'   => 'echo date( "Y-m-d H:i", strtotime( "[date_add]" ) );',
                            'sort' => true
                          ],
                          [
                            'name'  => 'Email',
                            'db'    => 'email',
                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 300px;' ],
                            'sort'  => true
                          ],
                          [
                            'name'  => 'CV',
                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 300px;' ],
                            'php'   => 'if ( "[cv]" and ( file_exists( "../[cv]" ) or file_exists( "../../[cv]" ) ) ) echo "<a href=\'/cv-tmp/[id]/" . \S::get_token() . "\' target=\'blank\'>pobierz cv</a>";'
                          ],
                          [
                            'name'  => 'Telefon',
                            'db'    => 'phone',
                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 300px;' ],
                            'th'    => [ 'class' => 'g-center' ]
                          ],
                          [
                            'name'  => 'Imię i nazwisko',
                            'db'    => 'candidate_name',
                            'sort'  => true
                          ]
                        ];
echo $grid -> draw();