<?php
    /* These functions belong to class BaseStandardResultsList that extends BaseResultsList
    author: Sydney D'Silva

    Usage: Instantiate the object BaseStandardResultsList
        $bsrList = new  BaseStandardResultsList;
        bsrList->Show_AW();
        bsrList->Show_pdf();
        bsrList->Show_xls();
        bsrList->Show_csv();

    display_AW() helps display a page that provides active sorting on the data.
    html2pdf() converts the displayed table to pdf
        The Catalog number is hyperreferenced to the record.
        The pdf file is rendered close to the html output.
    html2xls() converts the displayed table to Excel spreadsheet
        The Catalog number is hyperreferenced to the record.
    html2csv() converts the displayed table to CSV
    */
//**************************************************************************************************
        function
        Show_AW ()
        {
                $this->sourceStrings();

                if ($this->Where == '' && $this->QueryName == '')
                {
                        WebDie('QueryName is not set',
                                'base_results_list - Show()');
                }

                // Query and display
                $this->doQuery();
                $test = count($this->records);

                if (count($this->records) < 1)
                {
                        $this->displayNoResults();
                }
                else
                {
                        $this->display_AW();
                }
        }

        function
        display_AW()
        {
            require_once("../../../ActiveWidgets/examples/php/active_custom_widgets.php") ;

            $A_W = new active_widget;
            $this->get_Data();
            //Data loading
            $data=$this->op_data;//$pdf->LoadData('tmp_pdf.inc'); //loads the data
            $urls=$this->urls;//$pdf->LoadUrls('tmp_pdf.ref'); //loads the urls
            // grid object name 
            $name = "obj";

                $records =& $this->records;

                $widthAttrib = '';
                $backgroundColorAttrib = '';
                $highlightColorAttrib = '';
                if ($this->Width != '')
                        $widthAttrib    = 'width="' . $this->Width . '"' ;
                if ($this->BodyColor != '')
                        $backgroundColorAttrib  = 'bgcolor="' . $this->BodyColor . '"';
                if ($this->HighlightColor != '')
                        $highlightColorAttrib   = 'bgcolor="' . $this->HighlightColor . '"';
                else
                        $highlightColorAttrib   = 'bgcolor="' . $this->BodyColor . '"';
                if ($this->HeaderColor != '')
                        $headerColorAttrib= 'bgcolor="' . $this->HeaderColor . '"';
                if ($this->BorderColor != '')
                        $borderColorAttrib= 'bgcolor="' . $this->BorderColor . '"';

                // Find out how many coloums (one extra for image coloum)
                $numcol = count($this->Fields) + 1;

            // add grid to the page

                //Print Table header
                print "<!--START OBJECT-->\n";
                //print "<table width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"2\">\n";
                print <<<END
                <table style="width:90%; margin-left: auto; margin-right: auto;">
END;
                print "<tr><td width=\"35%\" align=\"left\" nowrap=\"nowrap\">";
                $this->printBackLink();
                if ($this->ContactSheetPage != '')
                {
                        $this->printRedisplay($this->ContactSheetPage,
                                $this->_STRINGS["CONTACT_SHEET"]);
                }
                $pdf_query =  $this->RedirectLink($this->ResultsListpdfPage, 0);
                $xls_query =  $this->RedirectLink($this->ResultsListExcelPage, 0);
                $csv_query =  $this->RedirectLink($this->ResultsListCSVPage, 0);
                print <<<END
                | <a href="$pdf_query"><font face="Arial" size="2" color="#013567">pdf</font></a>
END;
                print <<<END
                | <a href="$xls_query"><font face="Arial" size="2" color="#013567">Excel</font></a>
END;
                print <<<END
                | <a href="$csv_query"><font face="Arial" size="2" color="#013567">CSV</font></a>
END;
                print "</td>";
                print "<td width=\"65%\" align=\"right\">\n";
                $this->printNavLinks();
                print "</td></tr>";
                print "<tr><td colspan=\"2\">\n";

                print "</td></tr>";
                print "</table>\n";
                print "<!--END OBJECT-->\n";
            echo <<<END
            <table style="width:90%; height:45%; margin-left: auto; margin-right: auto;">
            <tr><td>
END;

                //Active Widget
                echo $A_W->activewidgets_grid($name, $urls, $data);// Active Widget
    echo "
    </td></tr>
    </table>
    ";

}

//**************************************************************************************************

        function
        Show_csv ()
        {
                $this->sourceStrings();

                if ($this->Where == '' && $this->QueryName == '')
                {
                        WebDie('QueryName is not set',
                                'base_results_list - Show()');
                }

                // Query and display
                $this->doQuery();
                $test = count($this->records);

                if (count($this->records) < 1)
                {
                        $this->displayNoResults();
                }
                else
                {
                        $this->get_Data();
                }
                $this->html2csv();
        }


        function
        html2csv()
        {

            $n_row=count($this->op_data);
            $n_col=count($this->op_data[0]);
            $file = "nmnh.csv";
            // Netscape is not happy
            //$char_count=0;
            //for($i_row=0;$i_row<$n_row;$i_row++)$char_count += strlen(implode(",",$this->op_data[$i_row]));

            global $HTTP_SERVER_VARS;
            if(isset($HTTP_SERVER_VARS['SERVER_NAME']))
            {
                header("Content-Type: application/text");
                //header('Content-Length:'.$char_count);
                header('Content-Disposition: inline; filename='.$file);
                for($i_row=0;$i_row<$n_row;$i_row++)
                {
                    print implode(",",$this->op_data[$i_row]);
                    print "\n";
                }
            }

        }


        function
        Show_xls ()
        {
                $this->sourceStrings();

                if ($this->Where == '' && $this->QueryName == '')
                {
                        WebDie('QueryName is not set',
                                'base_results_list - Show()');
                }

                // Query and display
                $this->doQuery();
                $test = count($this->records);

                if (count($this->records) < 1)
                {
                        $this->displayNoResults();
                }
                else
                {
                        $this->get_Data();
                }
                $this->html2xls();
        }

//**************************************************************************************************

        function
        html2xls()
        {
            require_once '../../../HTML2EXCEL/html2rgb.php';
            $n_row=count($this->op_data);
            $n_col=count($this->op_data[0]);

            // Creating a workbook
            $workbook = new Spreadsheet_Excel_Writer();

            // Custom Color
            $rgb=html2rgb("#FFFFFF");
            $rgb=html2rgb($this->HighlightColor);
            $even_row_color=9; //white
            $workbook->setCustomColor($even_row_color, $rgb[0], $rgb[1], $rgb[2]);
            $rgb=html2rgb("#FFF3DE");
            $rgb=html2rgb($this->BodyColor);
            $odd_row_color=10;
            $workbook->setCustomColor($odd_row_color, $rgb[0], $rgb[1], $rgb[2]);
            $rgb=html2rgb($this->HeaderColor);
            $header_color =11;
            $workbook->setCustomColor($header_color, $rgb[0], $rgb[1], $rgb[2]);
            $rgb=html2rgb($this->BorderColor);
            $border_color =12;
            $workbook->setCustomColor($border_color, $rgb[0], $rgb[1], $rgb[2]);
            $rgb=html2rgb("#AAC1C0");
            $bg_color =13;
            $workbook->setCustomColor($bg_color, $rgb[0], $rgb[1], $rgb[2]);
            $header_text_color='white';
            $odd_text_color   =$header_color;
            $even_text_color  =$header_color;

            $format_headings =& $workbook->addFormat(array('Align' => 'center',
                                                           'Color' => $header_text_color,
                                                           'Bold'  => 1,
                                                           'Border'  => 1,
                                                           'BorderColor'  => $header_color,
                                                           'Pattern' => 1,
                                                           'FontFamily' => 'Arial',
                                                           'FgColor' => $header_color));

            $format_footer   =& $workbook->addFormat(array('Align' => 'merge',
                                                           'Color' => $header_color,
                                                           'Bold'  => 0,
                                                           'Border'  => 1,
                                                           'BorderColor'  => $bg_color,
                                                           'Pattern' => 1,
                                                           'FontFamily' => 'Arial',
                                                           'FgColor' => $bg_color));

            $format_odd_row  =& $workbook->addFormat(array('Align' => 'left',
                                                           'Color' => $odd_text_color,
                                                           'Bold'  => 0,
                                                           'Border'  => 1,
                                                           'BorderColor'  => $border_color,
                                                           'Pattern' => 1,
                                                           'FontFamily' => 'Arial',
                                                           'FgColor' => $odd_row_color,
                                                           'TextWrap' => 1));

            $format_even_row =& $workbook->addFormat();
            $format_even_row->setAlign('left');
            $format_even_row->setColor($even_text_color);
            $format_even_row->setBorder(1);
            $format_even_row->setBorderColor($border_color);
            $format_even_row->setPattern(0);
            $format_even_row->setFontFamily('Arial');
            $format_even_row->setFgColor($even_row_color);
            $format_even_row->setTextWrap(1);

            // Creating a worksheet
            $worksheet =& $workbook->addWorksheet('NMNH');
            $col_width = array(10,50,10,12,30,10,15,10,10,10,10,10,10,10);
            for($i_col=0;$i_col<$n_col;$i_col++)
            {
                $worksheet->setColumn($i_col,$i_col,$col_width[$i_col]);        // Width of columns
            }

             for($i_row=0;$i_row<$n_row;$i_row++)
             {
                 for($i_col=0;$i_col<$n_col;$i_col++)
                 {
                     $worksheet->setRow($i_row,30);
                     $url  = $this->urls[$i_row][$i_col];
                     //Header
                     if($i_row == 0)
                     {
                         $worksheet->write($i_row, $i_col, $this->op_data[$i_row][$i_col], $format_headings);
                     }
                     else
                     {
                         switch ("$i_col") {
                             case "0":                          //First Column written with urls
                                 if($i_row % 2 == 0)            //Even Rows
                                 {
                                     $worksheet->writeUrl($i_row, $i_col, $url, $this->op_data[$i_row][$i_col], $format_even_row);
                                 }
                                 else                           //Odd Rows
                                 {
                                     $worksheet->writeUrl($i_row, $i_col, $url, $this->op_data[$i_row][$i_col], $format_odd_row);
                                 }
                                 break;
                             default:
                                 if($i_row % 2 == 0)            //Even Rows
                                 {
                                     $worksheet->write($i_row, $i_col, $this->op_data[$i_row][$i_col], $format_even_row);
                                 }
                                 else                           //Odd Rows
                                 {
                                     $worksheet->write($i_row, $i_col, $this->op_data[$i_row][$i_col], $format_odd_row);
                                 }
                                 break;
                         }
                     }
                 }
             }
             $str = "Copyright @ 2001 Smithsonian Institution | Data Access Policy";
             $worksheet->setRow($i_row,40);
             $worksheet->writeUrl($i_row, 0, "http://www.mnh.si.edu/rc/db/2data_access_policy.html", $str, $format_footer);
             for($i_col=1;$i_col<6;$i_col++)
             $worksheet->write($i_row, $i_col, '', $format_footer);

             $worksheet->write($i_row, $i_col, '', $format_footer);

            // Send the file
            // sending HTTP headers
            //$worksheet->setFooter("Copyright @ 2001 Smithsonian Institution | Data Access Policy");
            //$workbook->send('nmnh.xls');
            $workbook->close();

        }

//**************************************************************************************************
        function
        Show_pdf ()
        {
                $this->sourceStrings();

                if ($this->Where == '' && $this->QueryName == '')
                {
                        WebDie('QueryName is not set',
                                'base_results_list - Show()');
                }

                // Query and display
                $this->doQuery();
                $test = count($this->records);

                if (count($this->records) < 1)
                {
                        $this->displayNoResults();
                }
                else
                {
                        $this->get_Data();
                }
                $this->html2pdf();
        }

        function
        html2pdf ()
        {
           define('FPDF_FONTPATH','../../../FPDF/fpdf152/font/');
           require('../../../FPDF/fpdf152/mc_table.php');
           $pagemode = 'P';
           $pdf=new PDF_MC_Table();
           $pdf->SetTitle('NMNH');
           $pdf->SetSubject('IZ');
           $pdf->SetAuthor('Sydney D\'Silva');
           $pdf->SetKeywords('iz');
           $pdf->SetCreator('PHP');
//           $pdf->SetDisplayMode('fullpage');

           //Data loading
           $data=$this->op_data;//$pdf->LoadData('tmp_pdf.inc'); //loads the data
           $urls=$this->urls;//$pdf->LoadUrls('tmp_pdf.ref'); //loads the urls

           //
           $pdf->AddPage($pagemode);       //Portrait 
           $pdf->font_family='Arial';// Font Family,style,size
           $pdf->font_style='';      // Font style
           $pdf->font_size=7;        // Font size

           //Background Color
           $rgb=html2rgb("#AAC1C0");    //Converts from hex to dec
           $pdf->SetFillColor($rgb[0],$rgb[1],$rgb[2]); //rgb color
           $pdf->Rect(0,0,210,500,"F");  //Draws the bgcolor
           $pdf->page_height=11.69/2.54*72;     //in points
           $pdf->page_width =8.27/2.54*72;
           if($pagemode == 'L')
           {
               $pdf->page_height=8.27/2.54*72;
               $pdf->page_width =11.69/2.54*72;
           }

           $pdf->Rect(0,0,$pdf->page_width,$pdf->page_height,"F");  //Draws the bgcolor

           //Incorporate the banner image
           $pdf->Image('../../pages/nmnh/images/nmnh_mcs_banner.jpg',10,2,110,0,'','http://goode.si.edu/webnew');
           $pdf->SetX(10);
           $pdf->SetY(20);
           //Table with 20 rows and 7 columns
           $n_col = count($data[0]);

           $col_widths = array();
           $col_width = 100./($n_col+6.);
           $width_ratio = array(1.,4.7,1.3,1.3,1.3,1.4,2.);
           for($i_col=0;$i_col<$n_col;$i_col++)
           {
               if($i_col < 7) // Varying widths for the first 7 columns
               {
                   array_push($col_widths , $col_width*$width_ratio[$i_col]);
               }
               else           // Uniform width for the rest of the columns
               {
                   array_push($col_widths , $col_width);
               }
           }
           //$pdf->SetWidths(array(7.89474,36.8421,10.5263,10.5263,10.5263,10.5263,15.7895));//Set column widths
           $pdf->SetWidths($col_widths);//Set column widths
           $pdf->fill='DF'; //Fill the cells
           $pdf->cell_frame_color=$this->HeaderColor; //Cell Frame Color
           $pdf->cell_frame_width=0.3; //Cell Frame line width


           $i_row=0;
           foreach($data as $row)
           {
               switch ("$i_row") {
                   case "0": //header
                       $pdf->header_height=7;      //cell height
                       $pdf->fill_color=$this->HeaderColor; //blue cells
                       $pdf->text_color=$this->HighlightColor; //text color white
                       $pdf->font_family='Arial';  // Font Family = Arial
                       $pdf->font_style='B';       // Font style=Bold
                       $pdf->font_size=7;          // Font size = 7
                       $pdf->Row($row,$urls[$i_row],"1");
                       break;
                   default:
                       $pdf->text_color=$this->HeaderColor;
                       $pdf->font_family='Arial';  // Font Family,style,size
                       $pdf->font_style='';        // Font style
                       $pdf->font_size=7;          // Font size
                       if($i_row % 2 == 0) //even rows
                       {
                           $pdf->fill_color=$this->BodyColor; //orange cells
                       }
                       else                 //odd rows
                       {
                           $pdf->fill_color=$this->HighlightColor; //white cells
                       }

                       $pdf->Row($row,$urls[$i_row],"0");
                       break;
                   }
               $i_row++;
           }
           //$pdf->Output();
           $pdf->AliasNbPages();
           $pdf->Output('NMNH.pdf','I');
       }
   ?>