<?php

/**
 * Description of Letter
 *
 * @author Arkadiusz Rychlik <a.rychlik at eppe.com.pl>
 */
class Newsletter_Jqgrid_Letter extends Rtn_JqGrid {

	public $shrinktofit = false;
	public $autowidth = true;
	public $canReorder = false;
	public $name = 'letters';
	public $caption = '';
	public $sortname = 'id';
	public $height = 'auto';
	public $loadonce = false;
	public $scroll = false;
	public $rownum = 20;
	public $beanName = 'viewnlletter';
	public $actions = [
		'add' => null,
		'remove' => null,
		'edit' => null,
		'refresh' => null,
	];

	protected function _renderColmodel() {
		return [
			[
				'name' => 'status',
				'custom_formatter' => 'status_icon',
				'label' => __('Wysyłka'),
				'width' => 80,
				'search' => false
			],
			[
				'name' => 'subject',
				'label' => __('Temat'),
				'width' => 500,
			],
			[
				'name' => 'created',
				'label' => __('Data utworzenia'),
				'formatter' => 'date',
				'formatoptions' => ['srcformat' => 'U', 'newformat' => 'Y-m-d H:i:s']
			],
			[
				'name' => 'sendstarted',
				'label' => __('Data wysyłki'),
				'formatter' => 'date',
				'formatoptions' => ['srcformat' => 'U', 'newformat' => 'Y-m-d H:i:s']
			],
			[
				'name' => 'sendfinished',
				'label' => __('Data ukończenia'),
				'formatter' => 'date',
				'formatoptions' => ['srcformat' => 'U', 'newformat' => 'Y-m-d H:i:s']
			],
		];
	}

	protected function _renderCustomActions() {
		return [
			'status_icon' => [
				[
					'title' => __('Podgląd newslettera'),
					'icon_src' => Core_Module_Module::getInstance()->getAdminImgPath() . 'icons/mail.png',
					'onclick' => [
						'function' => 'admin_indexControllerClass.preview',
						'param' => 'row.id'
					]
				], [
					'condition' => [
						'column' => 'status',
						'op' => '==',
						'value' => Newsletter_Model_Nlletter::STATUS_SENT,
					],
					'title' => __('Wysyłka newslettera zakończona'),
					'icon_src' => Core_Module_Module::getInstance()->getAdminImgPath() . 'icons/checked.png',
				], [
					'condition' => [
						'column' => 'status',
						'op' => '==',
						'value' => Newsletter_Model_Nlletter::STATUS_PENDING,
					],
					'title' => __('Newsletter w trakcie wysyłki'),
					'icon_src' => Core_Module_Module::getInstance()->getAdminImgPath() . 'icons/pending_16.png',
				], [
					'condition' => [
						'column' => 'status',
						'op' => '==',
						'value' => Newsletter_Model_Nlletter::STATUS_NEW,
					],
					'title' => __('Kliknij aby rozpocząć wysyłanie'),
					'icon_src' => Core_Module_Module::getInstance()->getAdminImgPath() . 'icons/inactive_check.png',
					'onclick' => [
						'function' => 'admin_indexControllerClass.setPending',
						'param' => 'row.id'
					]
				]
			],
		];
	}

	protected function _canRowBeEdited($bean) {
		return ($bean['status'] == 'new');
	}

	protected function _canRowBeDeleted($bean) {
		return in_array($bean['status'], ['new', 'sent']);
	}

}
