<?php
global $gdb;

$grid = new \grid( 'pp_scontainers' );
$grid -> gdb_opt      = $gdb;
$grid -> sql          = 'SELECT *'
                            . 'FROM ( '
                              . 'SELECT '
                                . 'id, status, '
                                . '( SELECT title FROM pp_scontainers_langs AS psl, pp_langs AS pl WHERE lang_id = pl.id AND container_id = ps.id AND title != \'\' ORDER BY o ASC LIMIT 1 ) AS title '
                              . 'FROM '
                                . 'pp_scontainers AS ps '
                              . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] '
                          . 'ORDER BY '
                            . '[order_p1] [order_p2]';
$grid -> sql_count    = 'SELECT '
                            . 'COUNT(0) FROM ( '
                              . 'SELECT '
                                . 'id, status, '
                                . '( SELECT title FROM pp_scontainers_langs AS psl, pp_langs AS pl WHERE lang_id = pl.id AND container_id = ps.id AND title != \'\' ORDER BY o ASC LIMIT 1 ) AS title '
                              . 'FROM '
                                . 'pp_scontainers AS ps '
                            . ') AS q1 '
                          . 'WHERE '
                            . '1=1 [where] ';
$grid -> debug        = true;
$grid -> order        = [ 'column' => 'title', 'type' => 'ASC' ];
$grid -> search       = [
                            [ 'name' => 'Tytuł', 'db' => 'title', 'type' => 'text' ],
                            [ 'name' => 'Aktywny', 'db' => 'status', 'type' => 'select', 'replace' => [ 'array' => [ 0 => 'nie', 1 => 'tak' ] ] ]
                        ];
$grid -> columns_view = [
                          [
                            'name' => 'Lp.',
                            'th' => [ 'class' => 'g-lp' ],
                            'td' => [ 'class' => 'g-center' ],
                            'autoincrement' => true
                          ],
                          [
                            'name'  => 'Tytuł',
                            'db'    => 'title',
                            'sort'  => true
                          ],
                          [
                            'name' => 'Kod',
                            'php' => 'echo "[KONTENER:[id]]";'
                          ],
                          [
                            'name' => 'Aktywny',
                            'db' => 'status',
                            'replace' => [ 'array' => [ 0 => '<span style="color: #FF0000;">nie</span>', 1 => 'tak' ] ],
                            'th' => [ 'class' => 'g-center', 'style' => 'width: 150px;' ],
                            'td' => [ 'class' => 'g-center' ]
                          ],
                          [
                            'name'  => 'Akcja',
                            'db'    => 'id',
                            'td'    => [ 'class' => 'g-center' ],
                            'th'    => [ 'class' => 'g-center', 'style' => 'width: 120px;' ],
                            'php'   => 'echo "<a href=\'#\' class=\'button\' text=\'[KONTENER:[id]]\'>wybierz</a>";'
                          ]
                        ];
$grid -> buttons      = [
                          [
                            'label' => 'Dodaj kontener',
                            'url'   => '/admin/scontainers/container_edit/',
                            'icon'  => 'fa-plus-circle',
                            'class' => 'btn-success'
                          ]
                        ];
echo $grid -> draw();
?>
<style type="text/css">
  body.sb-top.sb-top-sm .navbar.navbar-fixed-top + #sidebar_left + #content_wrapper {
    padding-top: 0;
  }
</style>
<script type="text/javascript">
  function getUrlParam(paramName)
  {
    var reParam = new RegExp('(?:[\?&]|&amp;)' + paramName + '=([^&]+)', 'i');
    var match = window.location.search.match(reParam);

    return (match && match.length > 1) ? match[1] : '';
  }

  $( function()
  {
    $( '#sidebar_left, .navbar-fixed-top' ).hide();

    var funcNum = getUrlParam('CKEditorFuncNum');

    $( 'body' ).on( 'click', '.button', function()
    {
       window.opener.CKEDITOR.tools.callFunction(funcNum, $( this ).attr( 'text' ) );
       window.close();
    });
  });
</script>