<?php

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

/**
 * Action class for rtgrid_model
 */
class rtgrid_top_action {

    protected $name;
    protected $callback;
    protected $template;
    protected $text;
    protected $message = '';

    public function __construct($name = '', $text = '', $callback = '', $message = '') {

        $this->name = $name;
        $this->text = $text;
        $this->callback = $callback;
        $this->message = htmlspecialchars($message);

        if (empty($callback)) {

            $this->template = $this->text;
        }
    }

    public function get_name() {

        return $this->name;
    }

    public function get_callback() {

        return $this->callback;
    }

    public function get_template() {
        if (empty($this->template)) {

            return '<a href="#" class="btn" onclick="doAction(\'' . $this->name . '\', \'' . $this->message . '\', $(this), true);return false;">' . $this->text . '</a>';
        } else {

            return $this->_prepare_template();
        }
    }

    protected function _prepare_template($row = array()) {
        $template = $this->template;
        if (!empty($row)) {
            $row['primery_key'] = reset($row);
        }
        foreach ($row as $key => $value) {
            $template = str_replace('{' . $key . '}', $value, $template);
        }

        return $template;
    }

    public function set_name($name = '') {

        $this->name = $name;
    }

    public function set_callback($callback = '') {

        $this->callback = $callback;
    }

    public function set_template($template) {

        $this->template = $template;
    }

}

/**
 * Action class for rtgrid_model
 */
class rtgrid_action extends rtgrid_top_action {

    public function get_template($row = array()) {
        if (empty($this->template)) {

            return '<a href="#" onclick="doAction(\'' . $this->name . '\', \'' . $this->message . '\', $(this), false);return false;">' . $this->text . '</a>';
        } else {

            return $this->_prepare_template($row);
        }
    }

}

/**
 * Action class for rtgrid_edit_model
 */
class rtgrid_edit_action extends rtgrid_top_action {

    public function __construct($name = '', $text = '', $callback = '', $message = '', $template = '') {
        $this->name = (!empty($name)) ? 'rtgrid_action_' . $name : '';
        $this->text = $text;
        $this->callback = $callback;
        $this->message = htmlspecialchars($message);
        $this->template = $template;
    }

    public function get_template() {
        if (empty($this->template)) {
            $this->template = '<input type="submit" class="btn" name="{action_name}" value="{action_text}" />';
        }

        return $this->_prepare_template();
    }

    protected function _prepare_template($row = array()) {
        $template = $this->template;
        $row = array(
            'action_name' => $this->name,
            'action_text' => $this->text
        );
        foreach ($row as $key => $value) {
            $template = str_replace('{' . $key . '}', $value, $template);
        }

        return $template;
    }

}

class multilang {

    protected $_data = array();

    public function __construct($field = '', $lang_table = '', $lang_table_field = '', $callback_field = NULL) {
        $this->set_field($field);
        $this->set_lang_table($lang_table);
        $this->set_lang_table_field($lang_table_field);
        $this->set_callback_field($callback_field);
    }

    public function set_field($field = '') {

        $this->_data['field'] = $field;
    }

    public function set_lang_table($lang_table = '') {

        $this->_data['lang_table'] = $lang_table;
    }

    public function set_lang_table_field($lang_table_field = '') {

        $this->_data['lang_table_field'] = $lang_table_field;
    }

    public function set_callback_field($callback_field = NULL) {

        $this->_data['callback_field'] = $callback_field;
    }

    public function get_data($key = NULL) {
        if (is_null($key)) {

            return $this->_data;
        }

        return (isset($this->_data[$key])) ? $this->_data[$key] : FALSE;
    }

}

class photogallery {

    /**
     * Set to TRUE if need create table.
     * @var boolean
     */
    public $create_tables_when_run = FALSE;
    protected $table;
    protected $photo_path = '/assets/uploads/photogallery';
    protected $photo_width;
    protected $photo_height;
    protected $lang_db_data = array();
    protected $label = '';

    public function __construct($table = '', $photo_path = '', $photo_width = FALSE, $photo_height = FALSE) {
        $this->set_table($table);
        if (!empty($photo_path)) {
            $this->set_photo_path($photo_path);
        }
        $this->set_photo_width($photo_width);
        $this->set_photo_height($photo_height);
    }

    public function set_table($table) {

        $this->table = $table;
    }
    
    public function set_photo_path($photo_path) {
        
        $this->photo_path = $photo_path;
    }

    public function set_photo_width($photo_width) {

        $this->photo_width = $photo_width;
    }

    public function set_photo_height($photo_height) {

        $this->photo_height = $photo_height;
    }
    
    public function set_translate_field($field, $length = 0) {
        
        $this->lang_db_data[$field] = $length;
    }
    
    public function set_tab_label($label) {
        
        $this->label = $label;
    }

    public function get_table() {

        return $this->table;
    }
    
    public function get_photo_path() {
        
        return $this->photo_path;
    }

    public function get_photo_width() {

        return $this->photo_width;
    }

    public function get_photo_height() {

        return $this->photo_height;
    }
    
    public function get_translate_fields() {
        
        return $this->lang_db_data;
    }
    
    public function get_tab_label() {
        
        return $this->label;
    }

}

class image_upload {
    
    protected $field;
    protected $path = '/assets/uploads/multimedia/images';
    protected $width;
    protected $height;
    
    public function __construct($field = '', $path = '', $width = 0, $height = 0) {
        
        $this->set_field($field);
        
        if (!empty($path)) {
            $this->set_path($path);
        }
        
        $this->set_width($width);
        $this->set_height($height);
    }
    
    public function set_field($field) {
        
        $this->field = $field;
    }
    
    public function set_path($path) {
        
        $this->path = '/' . trim($path, '/');
    }
    
    public function set_width($width) {
        
        $this->width = $width;
    }
    
    public function set_height($height) {
        
        $this->height = $height;
    }
    
    public function get_field() {
        
        return $this->field;
    }
    
    public function get_path() {
        
        return $this->path;
    }
    
    public function get_width() {
        
        return $this->width;
    }
    
    public function get_height() {
        
        return $this->height;
    }
    
}
