<?php
/*
* @package LabCMS
* @copyright (c) 2006 Alexander Levin aka Loki (loki_angel@mail.ru)
* @support http://labcms.ru
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

class feedback_main extends ModuleTools
{
	public function show()
	{
		$oForm=Factory::create('feedback.form');
		$form=$oForm->getFormById(LabCMS::i()->id);

		if ($form->validate()) {
			$labels=$oForm->getFormLabels($form);
			$this->_sendEmail($form->getValue(), $labels);
			refresh('Спасибо! Ваше сообщение отпрвлено', _url(), 20);
			return;
		}
		self::_assign('form', $this->_renderForm($form));
		self::_setTpl('form');
	}

	public function edit()
	{
		if (!access('edit')) return $this->error403();
		$oForm=Factory::create('feedback.form');
		self::_assign('items', $oForm->getFormDataById(LabCMS::i()->id));
		self::_setTpl('form_edit');
	}

	public function edit_item()
	{
		if (!access('edit')) return $this->error403();
		$oForm=Factory::create('feedback.form');
		if (!$element=$oForm->getElementById($_GET['var1'])) return $this->_error(_m('элемент не найден'));
		$form=$oForm->getEditForm($element);
		if ($form->validate()) {
			$oForm->updateElement($element['id'], $form->getValue());
			refresh('', _url(null, 'edit'));
		}

		self::_assign('form', $this->_renderForm($form));
		self::_setTpl('form');
	}

	public function delete_item()
	{
		if (!access('edit')) return $this->error403();
		User::checkToken();
		$oForm=Factory::create('feedback.form');
		$oForm->delete($_GET['var1']);
		refresh('', _url(null, 'edit'));
	}

	public function add_item()
	{
		if (!access('edit')) return $this->error403();
		$oForm=Factory::create('feedback.form');
		$form=$oForm->getEditForm();
		if ($form->validate()) {
			$oForm->addElement(LabCMS::i()->id, $form->getValue());
			refresh('', _url(null, 'edit'));
		}

		self::_assign('form', $this->_renderForm($form));
		self::_setTpl('form');
	}

	public function move_up()
	{
		if (!access('edit')) return $this->error403();
		User::checkToken();
		$oForm=Factory::create('feedback.form');
		$oForm->move_up($_GET['var1']);
		refresh('', _url(null, 'edit'));
	}

	public function move_down()
	{
		if (!access('edit')) return $this->error403();
		User::checkToken();
		$oForm=Factory::create('feedback.form');
		$oForm->move_down($_GET['var1']);
		refresh('', _url(null, 'edit'));
	}

	protected function _sendEmail($data, $labels)
	{
		$text='';
		$mail=Factory::create('Mailer', config::get('email')?config::get('email'):config::get('core.admin_mail'));
		$mail->From = "feedback@".config::get('core.domain');
		$mail->FromName = "Обратная связь";
		$mail->WordWrap = 80;                                 // set word wrap to 80 characters
		$mail->Subject = "Обратная связь на ".config::get('core.domain');

		foreach ($labels as $key=>$label) {
			if (is_array($data[$key])) {
				$mail->AddAttachment($data[$key]['tmp_name'], $label.'.'.pathinfo($data[$key]['name'], PATHINFO_EXTENSION));
			} else {
				$text.=$label.': '.$data[$key]."\r\n";
			}
		}
		$mail->Body    = $text;
		return $mail->Send();
	} 
}