<?php

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

/**
 * CRUD model
 * 
 * @author CIEXT skeleton
 */
class crud_messeges extends CI_Model
{

    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_message');
        $this->crud->columns('subject, sended');
        $this->crud->fields('subject, text, emails, sended');

        $this->crud->required_fields('subject', 'text');

        $this->callbacks()->display_as()->observers();

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

    protected function callbacks()
    {
        $this->crud->callback('subject', 'rtgc_edit_link');
        $this->crud->callback('text', 'textarea');
        $this->crud->callback('sended', 'active');

        if (!$this->crud->is_grid()) {
            $this->load->library('newsletter/callback/rtgc_set_relation_n_n');
            $this->crud->set_relation_n_n('emails', 'newsletter_message_email_linker', 'newsletter_email', 'email');
            $rtgrid_action = new rtgrid_edit_action('send', $this->lang->line('Send message'), array($this, 'send_message'), '', '<button type="submit" class="btn btn-primary" name="{action_name}"><i class="icon-envelope icon-white"></i> {action_text}</button>');
            $this->crud->add_action($rtgrid_action);
        }

        return $this;
    }

    protected function display_as()
    {
        $this->crud->display_as('subject', $this->lang->line('Email subject'));
        $this->crud->display_as('text', $this->lang->line('Text'));
        $this->crud->display_as('sended', $this->lang->line('Sended'));

        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('Message was added') . $moreBtn));
        $this->crud->attach(make_rtgrid_edit_obs('newsletter', $action, $this->lang->line('Message was edited') . $moreBtn));
        $this->crud->attach(make_rtgrid_delete_obs('newsletter', $action, $this->lang->line('Message was deleted')));

        return $this;
    }

    public function send_message($table, $primery_key, $primery_key_value, $post_data = array())
    {
        $emailsIds = $this->input->post('f5d6d4041');
        if (!empty($emailsIds)) {
            $db = clone $this->db;
            $q = $db->
                            select('email')->
                            where_in('id_newsletter_email', array_map('intval', $emailsIds))->
                            get('newsletter_email')->result_array();
            
            $this->load->library('emailmng/emailmng');
            foreach ($q as $v) {
                $this->emailmng->to($v['email']);
                $this->emailmng->subject($post_data['subject']);
                $this->emailmng->message($post_data['text']);
                $this->emailmng->send();
            }
        }

        $post_data['sended'] = 1;
        $this->lang->language['Saved'] = sprintf($this->lang->line('Message was sended to %s email(s)'), count($q));

        return $this->rtgrid_edit_action_model->save($table, $primery_key, $primery_key_value, $post_data);
    }

}
