<?php defined('SYSPATH') OR die('No direct access allowed.');

/*
* This is a helper class for product management (going to be used in kalite-1)
*/
class prod {
    
    /**
     * @param   $props_array an associated array of item properties and values of that
     * properties (props: name, image, comment, price, id, [submit_name]), if it has an element with 
     * the key "submit_name", then it's value will be assigned to the submit button..
     * @return  string form of generated html
     */
    public static function item_for_motd( $props_array )
    {
        $return_str = "<div>" . 
        
        form::checkbox("checked_" . $props_array["id"], "", false) .
        "<h2 style=\"margin:0px; padding:0px\">" . $props_array["name"] . "</h2>" . 
        
        html::image($src = $props_array["image"]) .
        "<p>" . $props_array["comment"] . "</p>" .
        "<span>" . $props_array["price"] . " </span>" .
        "<span>" . form::input("qty", "1", array("size"=>"5","type"=>"text")) . "</span>";
        
        $return_str .= "</div>";
            
        return $return_str;
    }
    
    /**
     * @param   $props_array an associated array of item properties and values of that
     * properties (props: name, image, comment, price, id, [submit_name]), if it has an element with 
     * the key "submit_name", then it's value will be assigned to the submit button..
     * @return  string form of generated html
     */
    public static function item_box( $props_array )
    {
        $return_str = "<div class=\"item\">".
                        
                        form::open("#", array("method"=>"post")) .
                        form::input("id", $props_array["id"], array("type"=>"hidden")) .
                        
                        "<h2>" . $props_array["name"] . "</h2>" . 
                        html::image($src = $props_array["image"]) .
                        "<p>" . $props_array["comment"] . "</p>" .
                        "<span>" . $props_array["price"] . " </span>" .
                        "<span>" . form::input("qty", "1", array("size"=>"5","type"=>"text")) . "</span>";
        
        if(isset($props_array["submit_name"]))
        {
            $return_str .= "<span>" . form::submit("item_add", $props_array["submit_name"]) . "</span>";
        }
        else
        {
            $return_str .= "<span>" . form::submit("item_add", "Sepete ekle") . "</span>";
        }
        
        $return_str .= form::close() . "</div>";
            
        return $return_str;
    }
    
    /**
     * @param   $props_array an associated array of item properties and values of that
     * properties (props: name, image, comment, price, id)
     * @param   $categories an associated array where keys are the 
     * @return  string form of generated html
     */
    public static function item_edit( $props_array, $categories, $url_to_submit = "#" )
    {
        $rtn_str = "<div class=\"item_edit\">" .
                form::open($url_to_submit, array("method"=>"post", "enctype"=>"multipart/form-data")) .
                "İsim: " . form::input("name", $props_array["name"] , array("type"=>"text")) . 
                "<p>" . html::image($src = $props_array["image"]) . "</p>" .
                "<p>Fotoyu Değiştir: " . form::input("file", NULL , array("type"=>"file", "id"=>"file")) . "</p>" .
                "<p>Kategorileri</p>";
        
        foreach($categories as $cat_name => $is_checked)
        {
            $rtn_str .= $cat_name;
            $rtn_str .= form::checkbox($cat_name, $cat_name, $is_checked==1) . "<br>";
        }
                       
        $rtn_str .= "<p>";
        $rtn_str .= "<div>Yorum: ";
        $rtn_str .= form::input("item_comment",$props_array["comment"]);
        $rtn_str .= "</div>";
        $rtn_str .= "<div>Fiyat: ";
        $rtn_str .= form::input("item_price",$props_array["price"]);
        $rtn_str .= " TL</div>";
        $rtn_str .= "</p>";
        
        $rtn_str .= form::input("item_id", $props_array["id"], array("type" => "hidden"));
        $rtn_str .= form::submit("item_edit", "Tamam");
        
        $rtn_str .= "</div>";
        
        return $rtn_str;
    }
    
    /**
     * @param   $url_to_submit the place to submit form data..
     * @return  string form of generated html
     */
    public static function item_new( $categories, $url_to_submit = "#" )
    {
        $rtn_str = "<div class=\"item_new\">" .
                form::open($url_to_submit, array("method"=>"post", "enctype"=>"multipart/form-data")) .
                "İsim: " . form::input("item_name", "", array("type"=>"text")) . 
                "<p>Resim: " . form::input("file", "" , array("type"=>"file", "id"=>"file")) . "</p>" .
                "<p>Kategorileri</p>";
        
        foreach($categories as $category)
        {
            $rtn_str .= $category;
            $rtn_str .= form::checkbox($category, $category) . "<br>";
        }

        $rtn_str .= "<div>Yorum: ";
        $rtn_str .= form::input("item_comment","");
        $rtn_str .= "</div>";
        $rtn_str .= "<div>Fiyat: ";
        $rtn_str .= form::input("item_price","");
        $rtn_str .= " TL</div>";

        $rtn_str .= form::input("item_new", "", array("type" => "hidden"));        
        $rtn_str .= form::submit("item_edit", "Tamam");
        
        $rtn_str .= "</div>";
        
        return $rtn_str;
    }
    
    /**
    * Find the extension of the given filename
    * 
     * @param   $filename the name of the file..
     * @return  string representing the extension of the file
     */
    public static function file_extension($filename)
    {
        $filename = strtolower($filename) ;
        $exts = explode(".", $filename) ;
        $n = count($exts)-1;
        
        return ($n > 0) ? $exts[$n] : "";
    }
}
