<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// ------------------------------------------------------------------------

/**
 * Common Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Common Methods
 * @author		Harshada Kakade
 */
class Common {

	 /*
	 * upload files/images
	 *
	 * @access	public
     * @param	string , array , array ( fieldname,configArr,thumbnail config arr )
     * @return	array 
     * @author	Harshada Kakade
	 */
	 public function upload( $fieldName = "" , $configArr = array() , $thumbnailArr = array() , $resize = "0")
	{
		$CI_OBJ = & get_instance();
		$CI_OBJ->load->library('image_lib');
		$CI_OBJ->image_lib->clear();
		$CI_OBJ->load->library('upload');
		$CI_OBJ->upload->initialize($configArr);
		
		if($CI_OBJ->upload->do_upload( $fieldName) )
		{
			$image_data = $CI_OBJ->upload->data();
						
			if( $thumbnailArr['isThumb'] || ($resize))
			{
				if($thumbnailArr['width'] == "")
					$thumbnailArr['width'] = 150;
					
				if($thumbnailArr['height'] == "")
					$thumbnailArr['height'] = 150;
					
				$thumbConfigArr = array(
						'source_image' => $image_data['full_path'],
						'new_image' => $thumbnailArr['thumbFilePath'],
						'maintain_ratio' => $thumbnailArr['maintain_ratio'],
						'width' => $thumbnailArr['width'],
						'height' => $thumbnailArr['height']
				);
				$CI_OBJ->image_lib->initialize($thumbConfigArr);
			
				if ( $CI_OBJ->image_lib->resize())
					$error = array('error' =>  $CI_OBJ->image_lib->display_errors());
				
					
			}
			return $image_data["file_name"];
		}
		else
		{
			
			$error = array('error' => $CI_OBJ->upload->display_errors());
			return $error;
			
		}
				
	}
	
	/**
	 * remove_image
	 *
	 * deletes images
	 * @author	Harshada Kakade
	 * @access	public
	 * @param   string
	 * @return	null
	 */
	 
	 public function remove_image( $image_name = "")
	 {
		 if( file_exists($image_name ) )
		 	unlink($image_name); 
						
	 }
	 
	  /**
     * getPDFReport_common
     *
     * Function to generate pdf report
     *
     * @access    public
     * @param string $arrData
     * @param string $filename
     * @param string $location
     * @return    none
     * @author    Harshada Kakade
     */
    function getPDFReport_common($arrData = '', $filename = '', $location = '') {
        $CI_OBJ = & get_instance();
		
		$CI_OBJ->load->library('MPDF56/mpdf');
        ob_clean();
		
		//$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13); 
		$mpdf = new mPDF();
		$mpdf->SetDisplayMode('fullpage');
		
		$mpdf->list_indent_first_level = 0;	// 1 or 0 - whether to indent the first level of a list
		
		// LOAD a stylesheet
		//$stylesheet = file_get_contents('./MPDF56/mpdfstyletables.css');
		#$stylesheet = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/application/libraries/MPDF56/mpdf.css');
		$mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text
        $arrSample['arrData'] = $arrData;
		$html = $CI_OBJ->load->view($location, $arrSample, true);
        $mpdf->WriteHTML($html,2);
		$strFileName = "subscription-management-system".time().".pdf";
		$mpdf->Output($strFileName, 'D');


		header("Content-Type: application/pdf");
		header("Cache-Control: max-age=0");
		header("Accept-Ranges: none");
		header("Content-Disposition: attachment; filename=\"$filename\"");

        //echo $CI_OBJ->load->view($location, $arrData, true);
        exit;
    }

    function getPDFReport_commonWithoutHTML($arrHtmlData = '', $filename = '') {
        $CI_OBJ = & get_instance();

        $CI_OBJ->load->library('MPDF56/mpdf');
        ob_clean();

        //$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13);
        $mpdf = new mPDF('utf-8', 'A4', 0, '', 0, 0, 0, 0, 0, 0);
        $mpdf->SetDisplayMode('fullpage');
        //$mpdf->SetAutoFont(AUTOFONT_THAIVIET);
        $mpdf->list_indent_first_level = 0;	// 1 or 0 - whether to indent the first level of a list

        // LOAD a stylesheet
        //$stylesheet = file_get_contents('./MPDF56/mpdfstyletables.css');
        #$stylesheet = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/application/libraries/MPDF56/mpdf.css');
        $mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text
        $html = $arrHtmlData;
        $mpdf->WriteHTML($html,2);
        $strFileName = "subscription-management-system".time().".pdf";
        $mpdf->Output($strFileName, 'D');


        header("Content-Type: application/pdf");
        header("Cache-Control: max-age=0");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename=\"$filename\"");

        //echo $CI_OBJ->load->view($location, $arrData, true);
        exit;
    }



    /**
     * getPDFReport_common
     *
     * Function to generate pdf report
     *
     * @access    public
     * @param string $arrData
     * @param string $filename
     * @param string $location
     * @return    none
     * @author    Harshada Kakade
     */
    function getPDFReport_special($arrData = '', $filename = '', $location = '') {
        $CI_OBJ = & get_instance();

        $CI_OBJ->load->library('MPDF56/mpdf');
        ob_clean();

        //$mpdf=new mPDF('c','A4','','',32,25,27,25,16,13);
        $mpdf = new mPDF();
        $mpdf->SetDisplayMode('fullpage');

        $mpdf->list_indent_first_level = 0;	// 1 or 0 - whether to indent the first level of a list

        // LOAD a stylesheet
        //$stylesheet = file_get_contents('./MPDF56/mpdfstyletables.css');
        #$stylesheet = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/application/libraries/MPDF56/mpdf.css');
        $mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text
        $arrSample['arrData'] = $arrData;
        $html = $location;
        $mpdf->WriteHTML($html,2);
        $strFileName = "subscription-management-system".time().".pdf";
        $mpdf->Output($strFileName, 'D');


        header("Content-Type: application/pdf");
        header("Cache-Control: max-age=0");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename=\"$filename\"");

        //echo $CI_OBJ->load->view($location, $arrData, true);
        exit;
    }
	
	

 }
?>