<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Testscripts extends CI_Controller {

	function __construct()
    {
        parent::__construct(); 
        error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));        

    }
    
    function getproductmasters()
    {
		$arrpricing = array();
		$this->db->select("fld_productid, fld_price, fld_price3bottles, fld_price6bottles, fld_price9bottles, fld_price12bottles, fld_price24bottles");
		$this->db->where("fld_del", "0");
		$this->db->where("fld_price>", "0");
		$results = $this->db->get("tbl_price");
		if($results->num_rows()>0)
		{
			foreach($results->result() as $row)
			{
				$arrpricing[$row->fld_productid] = $row;
			}
		}
		
		$arrproducts = array();
		$this->db->select("fld_id, fld_type, fld_name, fld_noofbottlesincase");
		$this->db->where("fld_del", "0");
		$this->db->order_by("fld_name", "asc");
		$results02 = $this->db->get("tbl_products");
		if($results02->num_rows()>0)
		{
			$filename = "ProductsMaster-". date("YmdHis");
			ob_end_clean();
			// Set headers for file name, MIME type, and caching
			header("Content-type: application/csv");
			header("Content-Disposition: attachment; filename=\"".$filename.".csv\"");
			header("Pragma: no-cache");
			header("Expires: 0");
			$fh = fopen('php://output', 'w');
			$idx = 0;
			while ($row02 = $results02->unbuffered_row())
			{
				$idx++;
				$arrproducts[$row02->fld_id]["ID"] = $row02->fld_id;
				$arrproducts[$row02->fld_id]["Name"] = $row02->fld_name;
				$arrproducts[$row02->fld_id]["Type"] = $row02->fld_type;
				$arrproducts[$row02->fld_id]["NoOfBottlesInCase"] = $row02->fld_noofbottlesincase;
				if($arrpricing[$row02->fld_id])
				{
					$arrproducts[$row02->fld_id]["Price"] = $arrpricing[$row02->fld_id]->fld_price;
					$arrproducts[$row02->fld_id]["3 Bottles Price"] = $arrpricing[$row02->fld_id]->fld_price3bottles;
					$arrproducts[$row02->fld_id]["6 Bottles Price"] = $arrpricing[$row02->fld_id]->fld_price6bottles;
					$arrproducts[$row02->fld_id]["9 Bottles Price"] = $arrpricing[$row02->fld_id]->fld_price9bottles;
					$arrproducts[$row02->fld_id]["12 Bottles Price"] = $arrpricing[$row02->fld_id]->fld_price12bottles;
					$arrproducts[$row02->fld_id]["24 Bottles Price"] = $arrpricing[$row02->fld_id]->fld_price24bottles;
				}
				
				if($idx==1)
				{
					$mycustomheader = array_keys($arrproducts[$row02->fld_id]);
					fputcsv($fh, $mycustomheader);
				}
				fputcsv($fh, $arrproducts[$row02->fld_id]);
			}
			fclose($fh);
		}
	}

	function cachedfolders()
	{
		$addpath = "";
		if($_SERVER['HTTP_HOST']=="localhost") $addpath = "/gtownserver";
		$folderpath = "/home/liquorninja/";
		$openeddir = opendir($folderpath);
		$serial = 0;
		while ($file = readdir($openeddir)) {
			if ($file == '.' || $file == '..') {
				continue;
			}
			$filename = $folderpath . "/" . $file;
			
			$filedatetime = date("d-m-Y H:i:s.", filemtime($filename));
			$filedatetime = strtotime($filedatetime);
			$deletefromdatetime = date("Y-m-d H:i:s");
			$deletefromdatetime = strtotime($deletefromdatetime) - 50000;
			//if($filedatetime <= $deletefromdatetime)
			//{
				$serial++;
				
				$filestartchars = substr($file, 0,15);
				if($filestartchars=="rmDuplicateBill") {
					echo "<br>" . $serial . " => " . $file . " => " . date ("d-m-Y H:i:s.", filemtime($filename))  . " => " . substr($file, 0,15);
					chmod($filename,"0777");
					unlink($filename);
					echo " ==> DELETED";
				}
			//}
			if($serial==10000) exit;
		}
		closedir($dir);
		
	}

	function updatebooksmeta()
	{
        $this->db->select("fld_id, fld_title, fld_author, fld_about_ebook");
        $this->db->where("fld_ebookid", "0");
        /*$this->db->group_start();
        $this->db->where("fld_metatitle is NULL");
        $this->db->or_where("fld_metatitle", "");
        $this->db->group_end();*/
        $query = $this->db->get('tbl_ebooks');
        $index = 0;
        if($query -> num_rows() > 0)
        {
			foreach($query->result() as $row)
			{
				$index++;
				$metatitle = $row->fld_title . " | " . $row->fld_author;
				//$aboutbook = $row->fld_about_ebook;
				//echo "<br><br>" . $aboutbook;
				$metadesc = substr(strip_tags(html_entity_decode($row->fld_about_ebook)), 0, 160) . "...";
				$metakeywords = $metatitle;				
				$newdata = array("fld_metatitle" => $metatitle, "fld_metadesc" => $metadesc, "fld_metakeywords" => $metakeywords);
				$this->db->where("fld_id", $row->fld_id);
				$this->db->update("tbl_ebooks", $newdata);
			}
        }
        echo "DONE - " . $index;

	}	

	
}
