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

/**
 *  MX Stars Field Class for ExpressionEngine2
 *
 * @package		ExpressionEngine
 * @subpackage	Fieldtypes
 * @category	Fieldtypes
 * @author    Max Lazar <max@eec.ms>
 * @copyright Copyright (c) 2011 Max Lazar
 * @license   http://creativecommons.org/licenses/by-sa/3.0/ Attribution-Share Alike 3.0 Unported
 */

class Mx_stars_field_ft extends EE_Fieldtype
{
    /**
     * Fieldtype Info
     * @var array
     */
    var $info = array('name' => 'MX Stars Field', 'version' => '2.5');
    
    // Parser Flag (preparse pairs?)
    var $has_array_data = true;
    
    var $addon_name = 'mx_stars_field';
    
    /**
     * Constructor
     *
     * @access	public
     */
    function Mx_stars_field()
    {
        parent::EE_Fieldtype();
        
        
    }
    
    // --------------------------------------------------------------------
    
    function validate($data)
    {
        $valid = TRUE;
        
    }
    
    // --------------------------------------------------------------------
    
    public function display_field($data)
    {
        if (!isset($this->cache[$this->addon_name]['header'])) {
            $this->EE->cp->add_to_head('<script type="text/javascript" src="' . $this->EE->config->item('theme_folder_url') . 'third_party/mx_stars_field/js/jquery-ui-1.8.5.custom.min.js"></script>');
            $this->EE->cp->add_to_head('<script type="text/javascript" src="' . $this->EE->config->item('theme_folder_url') . 'third_party/mx_stars_field/js/jquery.ui.stars.min.js"></script>');
            $this->EE->cp->add_to_head('<link rel="stylesheet" type="text/css" href="' . $this->EE->config->item('theme_folder_url') . 'third_party/mx_stars_field/css/ui.stars.css" />');
            $this->cache[$this->addon_name]['header'] = true;
        }
        ;
        
        $prefix      = 'mx_stars_field';
        $split       = (isset($this->settings[$prefix . '_split'])) ? $this->settings[$prefix . '_split'] : '1';
        $stars_count = (isset($this->settings[$prefix . '_field_stars'])) ? $this->settings[$prefix . '_field_stars'] : '5';
        
        
        $name = str_replace(array(
            '[',
            ']'
        ), '_', $this->field_name);
        $r    = "";
        $css  = "";
        
        $r = "<p><div id=\"rs_$name\" style='padding-left:10px;'><select name=\"$this->field_name\" class=\"mx_star\">";
        for ($i = 1; $i <= $stars_count * $split; $i++) {
            $selected = ($i == $data) ? " selected=\"true\"" : "";
            $r .= "<option value=\"$i\"$selected>$i</option>";
        }
        
        $r .= "</select>";
        $this->EE->javascript->output('$("#rs_' . $name . '").stars({inputType: "select",   split: ' . $split . '});');
        
        
        $r .= '</div></p>';
        
        
        return $r;
    }
    
    // --------------------------------------------------------------------
    function replace_scale($data)
    {
        $r = $this->settings['mx_stars_field_field_stars'];
        
        return $r;
    }
    
    
    
    function replace_tag($data, $params = '', $tagdata = '')
    {
        if (!empty($tagdata)) {
            $prefix = 'mx_stars_field';
            $split  = (isset($this->settings[$prefix . '_split'])) ? $this->settings[$prefix . '_split'] : '1';
            $size   = ((int) $tagdata != 0) ? (int) $tagdata : 1;
            $r      = ($size / $split) * $data;
        } else {
            $r = $data;
        }
        ;
        
        
        return $r;
    }
    
    /**
     * Displays the cell
     * 
     * @access public
     * @param $data The cell data
     */
    public function display_cell($data)
    {
        if (!isset($this->EE->session->cache[__CLASS__]['mx_stars_js'])) {
            $this->EE->cp->add_to_head('<script type="text/javascript" src="' . $this->EE->config->item('theme_folder_url') . 'third_party/mx_stars_field/js/jquery-ui-1.8.5.custom.min.js"></script>');
            $this->EE->cp->add_to_head('<script type="text/javascript">mx_stars_field = {}; </script>');
            $this->EE->cp->add_to_head('<script type="text/javascript" src="' . $this->EE->config->item('theme_folder_url') . 'third_party/mx_stars_field/js/jquery.ui.stars.min.js"></script>');
            $this->EE->cp->add_to_head('<link rel="stylesheet" type="text/css" href="' . $this->EE->config->item('theme_folder_url') . 'third_party/mx_stars_field/css/ui.stars.css" />');
            $this->EE->cp->add_to_foot('<script type="text/javascript" src="' . $this->EE->config->item('theme_folder_url') . 'third_party/mx_stars_field/js/mx_stars.js"></script>');
            $this->EE->session->cache[__CLASS__]['mx_stars_js'] = true;
        }
        ;
        
        $prefix      = 'mx_stars_field';
        $split       = (isset($this->settings[$prefix . '_split'])) ? $this->settings[$prefix . '_split'] : '1';
        $stars_count = (isset($this->settings[$prefix . '_field_stars'])) ? $this->settings[$prefix . '_field_stars'] : '5';
        
        $name = str_replace(array(
            '[',
            ']'
        ), '_', $this->cell_name);
        $r    = "";
        $css  = "";
        
        $r = "<p><div id=\"rs_\" style='padding-left:10px;'><select name=\"$this->cell_name\" class=\"mx_star\">";
        for ($i = 1; $i <= $stars_count * $split; $i++) {
            $selected = ($i == $data) ? " selected=\"true\"" : "";
            $r .= "<option value=\"$i\"$selected>$i</option>";
        }
        
        $r .= "</select>";
        
        $this->EE->cp->add_to_foot('<script type="text/javascript">mx_stars_field["col_id_' . $this->col_id . '"] = "' . $split . '"</script>');
        
        
        $r .= '</div></p>';
        
        return $r;
    }
    
    /**
     * Display Cell Settings
     * 
     * @access public
     * @param $cell_settings array The cell settings
     * @return array Label and form inputs
     */
    public function display_cell_settings($cell_settings)
    {
        $prefix = 'mx_stars_field';
        
        $field_stars = (empty($cell_settings[$prefix . '_field_stars']) OR $cell_settings[$prefix . '_field_stars'] == '') ? 5 : $cell_settings[$prefix . '_field_stars'];
        $field_split = (empty($cell_settings[$prefix . '_split']) OR $cell_settings[$prefix . '_split'] == '') ? 1 : $cell_settings[$prefix . '_split'];
        $out         = "";
        
        
        $out .= '<table class="matrix-col-settings" border="0" cellpadding="0" cellspacing="0"><tbody><tr class=" matrix-first">';
        $out .= '<th class="matrix-first">Stars</th><td class="matrix-last">' . $this->select_list($prefix . '_field_stars', $field_stars) . '</td></tr>';
        $out .= '<tr class=" matrix-last"><th class="matrix-first">Split</th><td class="matrix-last">' . $this->select_list($prefix . '_split', $field_split) . '</td></tr></tbody></table>		';
        
        return $out;
    }
    // --------------------------------------------------------------------
    
    function display_settings($data)
    {
        $prefix = 'mx_stars_field';
        
        $field_stars = (empty($data[$prefix . '_field_stars']) OR $data[$prefix . '_field_stars'] == '') ? 5 : $data[$prefix . '_field_stars'];
        $field_split = (empty($data[$prefix . '_split']) OR $data[$prefix . '_split'] == '') ? 1 : $data[$prefix . '_split'];
        
        $this->EE->table->add_row('Stars', $this->select_list($prefix . '_field_stars', $field_stars));
        $this->EE->table->add_row('Split', $this->select_list($prefix . '_split', $field_split));
    }
    
    
    function select_list($field_name, $data)
    {
        $r = '<select name="' . $field_name . '" >';
        for ($i = 1; $i <= 30; $i++) {
            $selected = ($i == $data) ? " selected=\"true\"" : "";
            $r .= "<option value=\"$i\"$selected>$i</option>";
        }
        
        $r .= "</select>";
        return $r;
    }
    // --------------------------------------------------------------------
    function install()
    {
        return array(
            'mx_stars_field_field_stars' => '5'
        );
        return array(
            'mx_stars_field_field_split' => '1'
        );
    }
    
    function save_settings($data)
    {
        return array(
            'mx_stars_field_field_stars' => $this->EE->input->post('mx_stars_field_field_stars'),
            'mx_stars_field_split' => $this->EE->input->post('mx_stars_field_split')
        );
    }
    // --------------------------------------------------------------------
    
}

// END mx_stars_field_ft class

/* End of file ft.mx_stars_field.php */
/* Location: ./expressionengine/third_party/mx_stars_field/ft.mx_stars_field.php */