<?php

function policia_notificaciones_por_estado_render_historic() {
    $form = drupal_get_form('policia_notificaciones_por_estado_render_historic_form_filter');
    $output = render($form);
    $limit = 20;
    // List of table columns ("field" is the matching database column from the sql query)
    $header = [
        ['data' => t('Name'), 'field' => 'name'],
        ['data' => t('Datetime publish'), 'field' => 'datetime_publish'],
        ['data' => t('Datetime unpublish'), 'field' => 'datetime_unpublish'],
        ['data' => t('Act type'), 'field' => 'act_type'],
        ['data' => t('User upload'), 'field' => 'user_upload'],
        ['data' => t('Link file')],
    ];
    // Select table
    $query = db_select("status_notification_history", "snh");
    // Select fields
    $query->fields("snh", array("name", "datetime_publish", "datetime_unpublish", "act_type", "user_upload", "link_file"));
    if (isset($_GET['keyword'])) {
        $query->condition('name', '%' . str_replace(' ', '%', $_GET['keyword']) . '%', 'LIKE');
    }
    // For pagination
    $query = $query->extend('TableSort')->extend('PagerDefault')->limit($limit);
    $query = $query->extend('TableSort')->orderByHeader($header);
    // Execute query
    $result = $query->execute();
    $rows = array();
    // Looping for filling the table rows
    while($data = $result->fetchObject()){
        // Fill the table rows
        $rows[] = array(
            $data->name,
            $data->datetime_publish,
            $data->datetime_unpublish,
            $data->act_type,
            $data->user_upload,
            $data->link_file
        );
    }
    $output .= theme_table(
        [
            "header" => $header,
            "rows" => $rows,
            "attributes" => array(),
            "sticky" => true, // Table header will be sticky
            "caption" => "",
            "colgroups" => array(),
            "empty" => t("No historic records.") // The message to be displayed if table is empty
        ]
    ).theme("pager");
    return $output;
}

function policia_notificaciones_por_estado_render_historic_form_filter($form, &$form_state) {
    $form['filter'] = [
        '#type' => 'fieldset',
        '#title' => t("Filters")
    ];

    $form['filter']['name'] = [
        '#type' => 'textfield',
        '#title' => t('Name'),
        '#size' => 45,
        '#default_value' => $_GET['keyword'] ?? ''
    ];
    $form['filter']['submit'] = [
        '#type' => 'submit',
        '#value' => t("Find"),  
    ];
    $form['filter']['reset'] = [
        '#type' => 'submit',
        '#value' => t('Reset'),
        '#submit' => ['policia_notificaciones_por_estado_render_historic_form_filter_reset'],
    ];
    return $form;
}

function policia_notificaciones_por_estado_render_historic_form_filter_reset($form, &$form_state) {
    $query = [
        'order' => $_GET['order'],
        'sort' => $_GET['sort'],
        'page' => $_GET['page']
    ];
    drupal_goto('admin/config/administration/notificaciones-por-estado-history', array('query' => $query));
}

function policia_notificaciones_por_estado_render_historic_form_filter_submit($form, &$form_state) {
    $_GET['keyword'] = &$form_state['values']['name'];
    $query = [
        'order' => $_GET['order'],
        'sort' => $_GET['sort'],
        'page' => $_GET['page'],
        'keyword' => $_GET['keyword']
    ];
    drupal_goto('admin/config/administration/notificaciones-por-estado-history', array('query' => $query));
    return $form;
}