<?php
/**
 * Copyright (C) 2007,2008  Arie Nugraha (dicarve@yahoo.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/* LOAN HISTORY LIST IFRAME CONTENT */

// key to authenticate
define('INDEX_AUTH', '1');
if (!defined('INDEX_AUTH')) {
    define('INDEX_AUTH', '1');
}

// main system configuration
require '../../../sysconfig.inc.php';
// IP based access limitation
require LIB.'ip_based_access.inc.php';
do_checkIP('smc');
do_checkIP('smc-circulation');
// start the session
require SB.'admin/default/session.inc.php';
require SB.'admin/default/session_check.inc.php';

// ========== TAMBAHAN MULTI-LIBRARY ==========
$current_library_id = isset($_SESSION['library_id']) ? (int)$_SESSION['library_id'] : 1;
$is_superadmin = isset($_SESSION['uid']) && $_SESSION['uid'] == 1;
// ========== AKHIR TAMBAHAN ==========

if (!isset($_SESSION['memberID'])) { die(); }

require SIMBIO.'simbio_GUI/table/simbio_table.inc.php';
require SIMBIO.'simbio_GUI/paging/simbio_paging.inc.php';
require SIMBIO.'simbio_DB/datagrid/simbio_dbgrid.inc.php';

// page title
$page_title = 'Member Loan List';

// start the output buffering
ob_start();
// check if there is member ID
if (isset($_SESSION['memberID']) AND !empty($_SESSION['memberID'])) {
    /* LOAN HISTORY LIST */
    $memberID = trim($_SESSION['memberID']);
    
    // ========== TABLE SPEC DENGAN JOIN LIBRARY ==========
    $table_spec = 'loan_history AS lh
        LEFT JOIN mst_library AS lib ON lh.library_id = lib.library_id';
    // ========== AKHIR TABLE SPEC ==========

    // ========== FILTER LIBRARY_ID ==========
    $criteria = 'lh.member_id=\''.$dbs->escape_string($memberID).'\' ';
    if (!$is_superadmin) {
        $criteria .= " AND lh.library_id = " . $current_library_id;
    }
    // ========== AKHIR FILTER ==========
    
    // is there any search
    if (isset($_GET['keywords']) AND $_GET['keywords']) {
        $keyword = $dbs->escape_string($_GET['keywords']);
        $criteria .= " AND (lh.item_code LIKE '%$keyword%' OR lh.title LIKE '%$keyword%')";
    }
    
    // create datagrid
    $datagrid = new simbio_datagrid();
    
    // ========== DATAGRID DENGAN KOLOM LIBRARY ==========
    $datagrid->setSQLColumn(
        'lh.member_id AS \''.__('Member ID').'\'',
        'lh.member_name AS \''.__('Member Name').'\'',
        'lh.member_type_name AS \''.__('Membership Type').'\'',
        'lh.item_code AS \''.__('Item Code').'\'',
        'lh.title AS \''.__('Title').'\'',
        'lib.library_name AS \''.__('Library').'\'',
        'lh.loan_date AS \''.__('Loan Date').'\'',
        'lh.due_date AS \''.__('Due Date').'\'',
        'IF(lh.is_return = 0, \'<i>'.__('On Loan').'</i>\', lh.return_date) AS \''.__('Loan Status').'\'',
        'lh.last_update AS \''.__('Last Update').'\'');
    // ========== AKHIR DATAGRID ==========
    
    $datagrid->setSQLorder("lh.loan_date DESC");
    $datagrid->setSQLCriteria($criteria);

    // set table and table header attributes
    $datagrid->table_attr = 'id="dataList" class="s-table table"';
    $datagrid->table_header_attr = 'class="dataListHeader" style="font-weight: bold;"';
    $datagrid->icon_edit = SWB.'admin/'.$sysconf['admin_template']['dir'].'/'.$sysconf['admin_template']['theme'].'/edit.gif';
    // special properties
    $datagrid->using_AJAX = false;
    $datagrid->column_width = array(
        0 => '8%',   // Member ID
        1 => '10%',  // Member Name
        2 => '10%',  // Membership Type
        3 => '8%',   // Item Code
        4 => '20%',  // Title
        5 => '12%',  // Library
        6 => '8%',   // Loan Date
        7 => '8%',   // Due Date
        8 => '10%',  // Loan Status
        9 => '10%'   // Last Update
    );

    // put the result into variables
    $datagrid_result = $datagrid->createDataGrid($dbs, $table_spec, 20, false);
    if (isset($_GET['keywords']) AND $_GET['keywords']) {
        $msg = str_replace('{result->num_rows}', $datagrid->num_rows, __('Found <strong>{result->num_rows}</strong> from your keywords')); //mfc
        echo '<div class="infoBox">'.$msg.' : "'.$_GET['keywords'].'"</div>';
    }

    echo $datagrid_result;
}

// get the buffered content
$content = ob_get_clean();
// include the page template
require SB.'/admin/'.$sysconf['admin_template']['dir'].'/notemplate_page_tpl.php';