<?php

/**
 * 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;
    }

}
