<?php
global $gdb;

$grid = new \grid( 'globelus_adverts_answers' );
$grid -> gdb_opt = $gdb;
$grid -> sql = 'SELECT *'
                . 'FROM ( '
                  . 'SELECT '
                    . 'gaa.id, gaa.date_add, CONCAT( gaa.name, \' \', gaa.surname ) AS username, gaa.email, gaa.phone, gaa.text, title, advert_id, gaa.cv, gaa.user_id, gaa.displayed '
                  . 'FROM '
                    . 'globelus_adverts_answers AS gaa '
                    . 'INNER JOIN globelus_adverts AS ga ON ga.id = gaa.advert_id '
                  . ') AS q1 '
              . 'WHERE '
                . '1=1 [where] '
              . 'ORDER BY '
                . '[order_p1] [order_p2]';
$grid -> sql_count = 'SELECT '
                      . 'COUNT(0) FROM ( '
                        . 'SELECT '
                          . 'gaa.id, gaa.date_add, CONCAT( gaa.name, \' \', gaa.surname ) AS username, gaa.email, gaa.phone, gaa.text, title, advert_id, gaa.cv, gaa.user_id, gaa.displayed '
                        . 'FROM '
                          . 'globelus_adverts_answers AS gaa '
                          . 'INNER JOIN globelus_adverts AS ga ON ga.id = gaa.advert_id '
                      . ') AS q1 '
                    . 'WHERE '
                      . '1=1 [where] ';
$grid -> debug        = true;
$grid -> order        = [ 'column' => 'date_add', 'type' => 'DESC' ];
$grid -> search       = [
                            [ 'name' => 'Imię i nazwisko', 'db' => 'username', 'type' => 'text' ]
                        ];
$grid -> columns_view = [
                          [ 
                            'name' => 'Lp.',
                            'th' => [ 'class' => 'g-lp' ],
                            'td' => [ 'class' => 'g-center' ],
                            'autoincrement' => true 
                          ], [
                            'name' => 'Data dodania',
                            'db' => 'date_add',
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 130px;' ],
                            'th' => [ 'class' => 'g-center' ],
                            'php' => 'echo date( "Y-m-d H:i", strtotime( "[date_add]" ) );',
                            'sort' => true
                          ], [
                            'name'  => 'Ogłoszenie',
                            'db'    => 'title',
                            'sort'  => true,
                            'td' => [ 'style' => 'min-width: 200px; white-space: nowrap;' ],
                            'php' => 
                                  'echo "<a href=\'/admin/globelus_adverts/advert_preview/id=[advert_id]\' target=\'_blank\' style=\'color:"; if ( [displayed] ) echo "#e72f34;"; else echo "#8c8c8c;"; echo "\'>"; '
                                    . 'echo addslashes( mb_substr( \'[title]\', 0, 25, \'UTF-8\' ) ); '
                                    . 'if ( strlen( \'[title]\' ) > 25 ) echo "..."; '  
                                  . 'echo "</a>"; '
                          ], [
                            'name' => 'Data dodania',
                            'db' => 'date_add',
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 130px;' ],
                            'th' => [ 'class' => 'g-center' ],
                            'php' => 'echo date( "Y-m-d H:i", strtotime( "[date_add]" ) );',
                            'sort' => true
                          ], [
                            'name' => 'Imię i nazwisko',
                            'db' => 'username',
                            'sort' => true
                          ], [
                            'name' => 'Email',
                            'db' => 'email',
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 130px;' ],
                            'th' => [ 'class' => 'g-center' ]
                          ], [
                            'name' => 'Telefon',
                            'db' => 'phone',
                            'td' => [ 'class' => 'g-center', 'style' => 'width: 130px;' ],
                            'th' => [ 'class' => 'g-center' ]
                          ], [
                            'name' => 'Tekst',
                            'db' => 'text'
                          ], [
                            'name' => 'CV',
                            'db' => 'cv',
                            'td' => [ 'style' => 'white-space: nowrap;' ],
                            'php' => 
                                  'if ( ( "[cv]" and file_exists( "../[cv]" ) ) || ( !"[cv]" and [user_id] and file_exists( "../" . \front\factory\GlobelusCandidates::cv_url( [user_id] ) ) ) ) { '
                                    . 'if ( !"[cv]" and [user_id] and file_exists( "../" . \front\factory\GlobelusCandidates::cv_url( [user_id] ) ) ) '
                                      . '$cv_url = "/cv/[user_id]/" . \S::get_token() . "/"; '
                                    . 'else if ( "[cv]" and file_exists( "../[cv]" ) ) '
                                      . '$cv_url = "/cv-tmp/[id]/" . \S::get_token() . "/"; '
                                    . 'echo "<a href=\'" . $cv_url . "\' target=\'_blank\'>"; '
                                      . 'echo "<i class=\'fa fa-file-text-o\'></i> pobierz CV"; '
                                    . 'echo "</a>"; '
                                  . '} '
                          ]
                        ];
echo $grid -> draw();
?>