<?php

/*************************************************************************/
/* DOCEBO CMS - Content Management System                                */
/* ============================================                          */
/*                                                                       */
/* Copyright (c) 2004 by Giovanni Derks                                  */
/* http://www.docebocms.com                                              */
/*                                                                       */
/* This program is free software. You can redistribute it and/or modify  */
/* it under the terms of the GNU General Public License as published by  */
/* the Free Software Foundation; either version 2 of the License.        */
/*************************************************************************/

// ---------------------------------------------------------------------------
if (!defined("IN_DOCEBO")) die ("You can't access this file directly...");
check_feedback_perm($GLOBALS["pb"]);
// ---------------------------------------------------------------------------


$css=getModuleCss($GLOBALS["pb"]);
$GLOBALS["page"]->add("<div class=\"".$css."\">\n", "content");
$GLOBALS["page"]->add(getModuleBlockTitle($GLOBALS["pb"]), "content");


//$backurl="javascript:history.go(-1);";
$backurl="index.php?special=changearea&amp;newArea=".$GLOBALS["area_id"];

$GLOBALS["page"]->add("<div style=\"text-align: right\">\n", "content");
$GLOBALS["page"]->add("<a class=\"back_link\" href=\"$backurl\">".def("_BACK")."</a></div>\n", "content");


$op=importVar("op");
switch ($op) {

		default : {
			$opt=loadBlockOption($GLOBALS["pb"]);
			show_feedback_mask($pb, $opt["form_id"]);
		};break;

		case "sendform" : {
			send_message($GLOBALS["pb"]);
		};break;

}


$GLOBALS["page"]->add("<div style=\"text-align: right\">\n", "content");
$GLOBALS["page"]->add("<a class=\"back_link\" href=\"$backurl\">".def("_BACK")."</a></div>\n", "content");
$GLOBALS["page"]->add("</div>\n", "content");



function send_message($pb) {

	require_once($GLOBALS["where_framework"]."/lib/lib.field.php");

	$fields=new FieldList();

	$out=& $GLOBALS['page'];
	$out->setWorkingZone('content');
	$lang=& DoceboLanguage::createInstance('feedback', 'cms');


	$form_id=(int)$_POST["form_id"];
	$fromemail=$_POST["fromemail"];

	$field_info=getFormFieldInfo($form_id);


	$mandatory_only=array();
	foreach($field_info["mandatory"] as $key=>$val) {
		if ($val)
			$mandatory_only[]=$key;
	}

	$all_mandatory=$fields->isFilledSpecFields($mandatory_only);

	if (($all_mandatory) && (!empty($_POST["fromemail"]))) {

		$form_info=getFormInfo($form_id);

		if ($form_info !== FALSE) {
			$filled_val=$fields->getFilledSpecVal($field_info["fields"], false, true);
			// print_r($filled_val);


			$msg="\n";

			/* $extra_fields=getExtraFormFields($form_info["form_type"]);

			foreach($extra_fields as $field_name=>$field_attr) {
				$msg.=$field_attr["label"].": ".$_POST[$field_name]."\n";
			} */

			$arr_fields=array();
			foreach($filled_val as $field_id=>$val) {

				$msg.=$val["description"].": ".$val["value"]."\n\n";
				$arr_fields[$field_id]=$val["value"];

			}


			// -- Fields database storage ---------------------------------------------

			if ($form_info["storeinfo"]) {
			/*
				$send_id=getFeedbackSendId($fromemail); echo $send_id;
				$fields->setFieldEntryTable($GLOBALS["prefix_cms"]."_form_storage");
				$fields->storeDirectFieldsForUser($send_id, $arr_fields, TRUE, FALSE);
			*/

				storeFormInfo($fromemail, $form_info, $arr_fields);

			} //die("<br /><br />- debug -");
			// ------------------------------------------------------------------------


			$email_list=explode("\n", $form_info["email"]);
			$sub=$form_info["title"];

			$sub=stripslashes(translateChr($sub, getTranslateTable(), true));
			$msg=stripslashes(translateChr($msg, getTranslateTable(), true));

			foreach((array)$email_list as $key=>$val) {
				if ($val != "") {
					mail($val, $sub, $msg,
						"From: $fromemail\r\n".
						"Reply-To: $fromemail\r\n".
						"Content-Type: text/plain; charset=".getUnicode().""."\n".
						"X-Mailer: DoceboFramework ".$GLOBALS["core_version"]);
				}
			}

			$out->add(getResultUi($lang->def("_FEEDBACK_SUCCESS")));
		}
	}
	else {
		$out->add(getErrorUi($lang->def("_FEEDBACK_FIELDREQ")));
	}

	//print_r($_POST);

}


?>