<?php

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

/**
 * CRUD model
 * 
 * @author CIEXT skeleton
 */
class crud_newsletter_email extends \Ciext\Model\AbstractEventModel
{

    protected $crud;

    public function __construct()
    {
        parent::__construct();
        $this->load->library('rtgrid/rtgrid_api');
        $this->crud = new rtgrid_api();

        // Acceess check
        if (!$this->rt_user->isAllowed('newsletter', 'view_newsletter')) {
            $this->admin_lib->show_denied();
        }
        if (!$this->rt_user->isAllowed('newsletter', 'edit_newsletter')) {
            $this->crud->unset_action('edit');
            $this->crud->unset_action('delete');
            $this->crud->unset_action('add');
        }
    }

    public function render()
    {
        $this->crud->set_table('newsletter_email');
        $this->crud->columns('email, date');
        $this->crud->fields('email, date');

        $tags = $this->uri->get_next_segment('tags');
        if (!empty($tags)) {
            $this->crud->where('tags', $tags);
        }

        $this->callbacks()->display_as()->observers();
        
        // Notify
        $this->notify(__FUNCTION__, ['crud' => $this->crud]);

        return $this->get_export_link() . $this->crud->render();
    }

    protected function callbacks()
    {
        $this->crud->callback('email', 'rtgc_edit_link');
        $this->crud->callback('date', 'datetime');

        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('email', $this->lang->line('Email'));
        $this->crud->display_as('date', $this->lang->line('Date'));

        return $this;
    }

    protected function observers()
    {
        $this->load->helper('rtgrid/rtgrid_obs');
        $action = 'edit_newsletter';
        $moreBtn = ' <a href="{link}">>>></a>';

        $this->crud->attach(make_rtgrid_add_obs('newsletter', $action, $this->lang->line("Record was added") . $moreBtn));
        $this->crud->attach(make_rtgrid_edit_obs('newsletter', $action, $this->lang->line("Record was edited") . $moreBtn));
        $this->crud->attach(make_rtgrid_delete_obs('newsletter', $action, $this->lang->line("Record was deleted")));

        return $this;
    }

    public function get_export_link()
    {
        $this->config->load('newsletter/config');
        $def = $this->config->item('export_res')['default'];
        $html = ' <div class="btn-group" style="float:right; margin-left:5px;">' .
                '<a href="/' .
                $this->config->item('admin_page') . '/mconfig/index/newsletter/export?type=' .
                ($this->config->item('export_res')['resource'][$def]['method']) .
                '" class="btn" download="download"><i class="fa fa-cloud-download" aria-hidden="true"></i> ' .
                $this->lang->line('Export') . '</a>' .
                '<button class="btn dropdown-toggle" data-toggle="dropdown">' .
                '    <span class="caret"></span>' .
                '</button>' .
                '<ul class="dropdown-menu" style="min-width: 110px;">';

        foreach ($this->config->item('export_res')['resource'] as $res) {
            $html .= '<li>' .
                    '   <a href="/' .
                    $this->config->item('admin_page') . '/mconfig/index/newsletter/export?type=' .
                    $res['method'] . '&tags=' . $this->uri->get_next_segment('tags') .
                    '" download="download">' . $this->lang->line('To') . ' ' . $res['title'] . '</a>' .
                    '</li>';
        }

        $html .= '</ul>' .
                '</div>';

        return $html;
    }

}
