<?php
///////////////////////////////////////////////////////////////////////////////////////
// PHPizabi 0.848b C1 [ALICIA]                               http://www.phpizabi.net //
///////////////////////////////////////////////////////////////////////////////////////
// Please read the license.txt file before using / modifying this software           //
// Original author:         Claude Desjardins, R - feedback@realitymedias.com        //
// Last modification date:  August 13th 2006                                         //
// Version:                 PHPizabi 0.848b C1                                       //
//                                                                                   //
// (C) 2005, 2006 Real!ty Medias / PHPizabi - All rights reserved                    //
///////////////////////////////////////////////////////////////////////////////////////

	/* Check Structure Availability */
	if (!defined("CORE_STRAP")) die("Out of structure call");
	/* Administrative restriction */
	(!me('is_administrator')&&!me('is_superadministrator')?die("Access restricted"):NULL);
	
	

	$tpl = new template;
	$tpl -> Load("add");
	
	/* Handle post */
	if (isset($_POST["Submit"])) {
		
		/* Handle the uploaded file if there was any */
		if (is_uploaded_file($_FILES["file"]["tmp_name"])) {
			$filename = basename($_FILES["file"]["name"]);
			move_uploaded_file($_FILES["file"]["tmp_name"], "system/cache/images/nudges/{$filename}");
		}
		
		/* Load title and body for all the languages */
		foreach ($_POST as $var => $val) {
			if (strstr($var, "_")) {
				$var = explode("_", $var);
				if ($var[0] == "title") $title[$var[1]] = $val;
				if ($var[0] == "body") $body[$var[1]] = $val;
			}
		}
		
		/* Load the actual nudges database */
		$nArr = unpk(file_get_contents("system/cache/nudges.dat"));
		if (!is_array($nArr)) $nArr = array();
		
		end($nArr);
		$key = key($nArr)+1;
		
		$nArr[$key]["title"] = $title;
		$nArr[$key]["body"] = $body;
		$nArr[$key]["icon"] = (isset($filename)?$filename:NULL);
		
		$handle = fopen("system/cache/nudges.dat", "w");
		fwrite($handle, pk($nArr));
		fclose($handle);
		
	}
	
	
	$i=0;
	foreach(explode(",", $CONF["LOCALE_SITE_LANGUAGES"]) as $lang) {
		$langLoop[$i]["lang"] = $lang;
		$i++;
	}
	$tpl -> Loop("title", $langLoop);
	$tpl -> Loop("body", $langLoop);


	$tpl -> Flush();
	
?>