<?php
/**
 * PHPExcel
 *
 * Copyright (C) 2006 - 2013 PHPExcel
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category   PHPExcel
 * @package    PHPExcel
 * @copyright  Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
 * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt	LGPL
 * @version    1.7.9, 2013-06-02
 */

/** Error reporting */
//require_once('libconfig.php');
//require_once('libhtml.php');
//require_once('libmsginfo.php');
//require_once('libdefine.php');
//require_once('libdbconn.php');

$keyword=$_REQUEST['keyword'];
$cl=new MY_HTML();
//if($idfakultas){$nmfakultas=$cl->fn_get_fieldvalue("namafakultas","select namafakultas from ".DB_UNDIAN.".tbl_fakultas where id='$idfakultas'");}


error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
	die('This example should only be run from a Web Browser');

/** Include PHPExcel */

require_once 'Classes/PHPExcel.php';
if(preg_match("/^windows/i",strtolower(php_uname()))){PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);}

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// My data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1','TELKOMCEL')
            ->setCellValue('A2','Cells Information List')
            ->setCellValue('A3','Reported By : '.$_SESSION['nik']);

//  set format cell
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);


// Print Header
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A6','No')
            ->setCellValue('B6','Site ID')
            ->setCellValue('C6','Site Name')
            ->setCellValue('D6','Long')
            ->setCellValue('E6','Lat')
            ->setCellValue('F6','2G Cell Name MSC')
            ->setCellValue('G6','3G Cell Name MSC (850)')
            ->setCellValue('H6','3G Cell Name MSC (2100)')
            ->setCellValue('I6','3G Cell Name MSC (2100) 2nd Carrier')
            ->setCellValue('J6','3G Cell Name MSC (2100) 3rd Carrier')
            ->setCellValue('K6','CI 2G')
            ->setCellValue('L6','CI 3G (850)')
            ->setCellValue('M6','CI 3G (2100)')
            ->setCellValue('N6','CI 3G (2100) - 2nd Carrier')
            ->setCellValue('O6','CI 3G (2100) - 3rd Carrier')
            ;

// === Merge Cells
//$objPHPExcel->getActiveSheet()->mergeCells('G5:H5');
//$objPHPExcel->getActiveSheet()->mergeCells('I5:J5');

$objPHPExcel->getActiveSheet()->getStyle('A6:O6')->getFont()->getColor()->setRGB('ffffff');
// Worksheet Name
$objPHPExcel->getActiveSheet()->setTitle('CellsInfo_'.date("Ym"));

// === Set Background Cell
$objPHPExcel->cellColor("A6:O6","938e8e");

// === set Column Width A
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(6);
// === Set auto COlumn width
foreach(range('B','O') as $columnID)
{
    $objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(true);
}

$qry="select c.id,c.idsiteid,s.siteid,s.sitename,s.lng,s.lat,c.cell2g,c.cell3g850,c.cell3g2100A,c.cell3g2100B,c.cell3g2100C,c.ci2g,c.ci3g850,c.ci3g2100A,c.ci3g2100B,c.ci3g2100C
      from ".DB_APORTIL.".tbl_nits_siteslacci s inner join ".DB_APORTIL.".tbl_nits_sitescells c on s.id=c.idsiteid
      where (s.siteid like '%$keyword%' or s.sitename like '%$keyword%' or c.cell2g like '%$keyword%' 
             or c.cell3g850 like '%$keyword%' or c.cell3g2100A like '%$keyword%' or c.cell3g2100B like '%$keyword%' or c.cell3g2100C like '%$keyword%') 
             and c.isdeleted=0
      order by c.id asc";
      
$rsrpt=$cl->selectquery($qry);
if($rsrpt)
{
    $i=7;$j=1;
    while($rw=$cl->data_row_array($rsrpt))
    {
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i,$j)
                                            ->setCellValue('B'.$i,$rw["siteid"])
                                            ->setCellValue('C'.$i,$rw["sitename"])
                                            ->setCellValue('D'.$i,$rw["lng"])
                                            ->setCellValue('E'.$i,$rw["lat"])
                                            ->setCellValue('F'.$i,$rw["cell2g"])
                                            ->setCellValue('G'.$i,$rw["cell3g850"])
                                            ->setCellValue('H'.$i,$rw["cell3g2100A"])
                                            ->setCellValue('I'.$i,$rw["cell3g2100B"])
                                            ->setCellValue('J'.$i,$rw["cell3g2100C"])
                                            ->setCellValue('K'.$i,$rw["ci2g"])
                                            ->setCellValue('L'.$i,$rw["ci3g850"])
                                            ->setCellValue('M'.$i,$rw["ci3g2100A"])
                                            ->setCellValue('N'.$i,$rw["ci3g2100B"])
                                            ->setCellValue('O'.$i,$rw["ci3g2100C"])
                                            ;
        $i+=1;$j+=1;
    }
}
$cl->clearrecordset($rsrpt);
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a web browser (Excel2007)
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="CELLSINFO.xls"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
?>
