Deprecated: Assigning the return value of new by reference is deprecated in /home/bluestat/public_html/source/index.php on line 477
Kalens - Blob - ViewGit - Blue Static
<?php
/*=====================================================================*\
|| ###################################################################
|| # Kalens [#]version[#]
|| # Copyright ©2002-[#]year[#] Iris Studios, Inc.
|| #
|| # 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; version [#]gpl[#] of the License.
|| #
|| # This program is distributed in the hope that it will be useful, but
|| # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|| # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|| # more details.
|| #
|| # You should have received a copy of the GNU General Public License along
|| # with this program; if not, write to the Free Software Foundation, Inc.,
|| # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|| ###################################################################
\*=====================================================================*/

define('SVN', '$Id$');

require_once('./global.php');
require_once('./includes/class_template.php');

require_once('./includes/api_error.php');
APIError(array('APIErrorHandler', 'AdminError'));

// ###################################################################

if ($_POST['do'] == 'killall')
{
	$kalens->input_clean('styleid', TYPE_INT);

	if ($kalens->in['styleid'] != -1)
	{
		$db->query("DELETE FROM " . TABLE_PREFIX . "template WHERE styleid = " . $kalens->in['styleid']);
	}

	$print->redirect("templates.php?do=modify&styleid=" . $kalens->in['styleid']);
}

// ###################################################################

if ($_REQUEST['do'] == 'deleteall')
{
	$print->page_confirm($lang->string('Are you sure you want to revert all of this styles templates to their default state?'), 'templates.php', 'killall', array('styleid', $kalens->in['styleid']));
}

// ###################################################################

if ($_POST['do'] == 'kill')
{
	$tpl = new TemplateAPI($kalens);
	$tpl->set('templateid',		$kalens->in['templateid']);
	$tpl->set_condition();
	$tpl->delete();

	// Send the user on their merry way
	$print->redirect("templates.php?do=modify&styleid=" . $tpl->objdata['styleid']);
}

// ###################################################################

if ($_REQUEST['do'] == 'delete')
{
	$print->page_confirm(sprintf($lang->string('Are you sure you want to remove the template <strong>%1$s</strong> (id: %2$s)?'), $kalens->in['template'], $kalens->in['id']), 'templates.php', 'kill', array('templateid' => $kalens->in['templateid']));
}

// ###################################################################

if ($_POST['do'] == 'insert')
{
	$tpl = new TemplateAPI($kalens);
	$tpl->set('templatename',	$kalens->in['templatename']);
	$tpl->set('styleid',		$kalens->in['styleid']);
	$tpl->set('template',		$_POST['template']);
	$tpl->insert();

	$print->redirect('templates.php?do=modify&styleid=' . $kalens->in['styleid']);
}

// ###################################################################

if ($_REQUEST['do'] == 'add')
{
	$print->page_start($lang->string('New Template'));

	$print->form_start('templates.php', 'insert');
	$print->form_hidden_field('styleid', $kalens->input_clean('styleid', TYPE_INT));

	$print->table_start();
	$print->table_head($lang->string('New Template'));

	$print->row_input($lang->string('Name'), 'templatename');
	$print->row_textarea($lang->string('Data'), 'template', '', 2, 30, 75, true, 'width: 98%');

	$print->row_submit();

	$print->table_end();
	$print->form_end();

	$print->page_end();
}

// ###################################################################

if ($_POST['do'] == 'update')
{
	$kalens->input_clean('styleid', TYPE_INT);
	$templateid = $kalens->input_clean('templateid', TYPE_INT);

	// This is a brand-new customization
	if ($kalens->in['templateid'] == -1)
	{
		if ($kalens->in['styleid'] != -1)
		{
			$tpl = new TemplateAPI($kalens);
			$tpl->set('templatename',	$kalens->in['templatename']);
			$tpl->set('styleid',		$kalens->in['styleid']);
			$tpl->set('template',		$_POST['template']);
			$tpl->insert();

			$templateid = $tpl->insertid;
		}
	}
	// Just a boring old update
	else
	{
		$tpl = new TemplateAPI($kalens);
		$tpl->set('templateid',		$kalens->in['templateid']);
		$tpl->set_condition();

		$tpl->set('templatename',	$kalens->in['templatename']);
		$tpl->set('template',		$_POST['template']);
		$tpl->set('dateline',		TIMENOW);

		$tpl->update();
	}

	if ($kalens->in['reload'])
	{
		$print->redirect("templates.php?do=edit&id=" . $templateid);
	}
	else
	{
		$print->redirect("templates.php?do=modify&styleid=" . $kalens->in['styleid']);
	}
}

// ###################################################################

if ($_REQUEST['do'] == 'edit')
{
	$kalens->input_clean_array(array(
		'id'		=> TYPE_INT,
		'styleid'	=> TYPE_INT
	));

	if ($kalens->in['name'])
	{
		$template = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "template WHERE templatename = '" . $kalens->input_escape('name') . "' AND styleid = -1");
	}
	else
	{
		$template = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "template WHERE templateid = " . $kalens->in['id']);
	}

	$print->page_start($lang->string('Edit Template'));

	$print->form_start('templates.php', 'update');
	$print->form_hidden_field('templateid', ($kalens->in['name'] ? -1 : $kalens->in['id']));
	$print->form_hidden_field('styleid', (($template['styleid'] == -1 AND $kalens->in['name']) ? $kalens->in['styleid'] : $template['styleid']));

	$print->table_start();
	$print->table_head(sprintf($lang->string('Edit Template - %1$s (id: %2$d)'), $template['templatename'], $template['templateid']));

	if ($template['styleid'] == -1)
	{
		$print->row_input($lang->string('Name'), 'templatename', $template['templatename']);
	}
	else
	{
		$print->form_hidden_field('templatename', $template['templatename']);
		$print->row_text($lang->string('Name'), $template['templatename']);
	}
	$print->row_textarea($lang->string('Data'), 'template', $kalens->entity_encode($template['template']), 2, 30, 75, true, 'width: 98%');
	$print->row_submit('<input type="checkbox" class="button" name="reload" value="1" checked="checked" accesskey="e" />' . $lang->string('Reload After Save?'));
	$print->table_end();
	$print->form_end();

	$print->page_end();
}

// ###################################################################

if ($_REQUEST['do'] == 'view')
{
	$tpl = new TemplateAPI($kalens);
	$tpl->dorelations = array();
	$tpl->set('templatename',	$kalens->in['template']);
	$tpl->set('styleid',		-1);
	$tpl->set_condition(array('templatename', 'styleid'));
	$tpl->fetch();

	$print->page_start($lang->string('View Original'));

	$print->table_start();
	$print->table_head($lang->string('View Original'));
	$print->row_input($lang->string('Name'), 'templatename', $tpl->objdata['templatename']);
	$print->row_textarea($lang->string('Data'), 'template', $kalens->entity_encode($tpl->objdata['template']), 2, 30, 75, true, 'width: 98%');
	$print->table_end();

	$print->page_end();
}

// ###################################################################

if ($_REQUEST['do'] == 'modify')
{
	$print->page_start($lang->string('Template Manager'));

	$kalens->input_clean('styleid', TYPE_INT);

	if ($kalens->in['styleid'] == -1)
	{
		$style['styleid'] = -1;
	}
	else
	{
		$style = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "style WHERE styleid = " . $kalens->in['styleid']);
	}

	if ($style['styleid'])
	{
		// Get the master style's templates to use as a base
		$master_style_get = $db->query("SELECT * FROM " . TABLE_PREFIX . "template WHERE styleid = -1 ORDER BY templatename ASC");
		while ($master = $db->fetch_array($master_style_get))
		{
			$master['ismaster'] = 1;
			$templates["$master[templatename]"] = $master;
		}
		$db->free_result($master_style_get);

		// Get the customized templates and replace them over the masters
		$get_templates = $db->query("SELECT * FROM " . TABLE_PREFIX . "template WHERE styleid = $style[styleid] ORDER BY templatename ASC");
		while ($template = $db->fetch_array($get_templates))
		{
			$template['ismaster'] = 0;
			$templates["$template[templatename]"] = $template;
		}
		$db->free_result($get_templates);

		// unset special templates
		if (!DEVDEBUG)
		{
			unset($templates['STYLE_VARS']);
		}

		// Go through each item in the array and break the name apart via underscores
		// then, group them by the element before the first underscore
		// Go through the grouped items and merge any upper case groups with the lower case ones -- keeping the upper case as the key
		foreach ($templates AS $name => $array)
		{
			$group = explode('_', $name);
			$group = $group[0];
			$groupup = strtoupper($group);

			// If there is a template group with an upper case name of a lower case name
			if (is_array($templatelist["$groupup"]))
			{
				// Go through the items in the old case-sensitive groups and reassign them to the all upper-case groups
				foreach ($templatelist["$groupup"] AS $_value)
				{
					$templatelist["$groupup"][] = $_value;
					unset($templatelist["$group"]["$_value"]);
				}
			}
			// There is no group for this template, so just put it in the list
			else
			{
				$templatelist["$group"][] = $array;
			}
		}
		unset($templates);

		// Construct depth mark on all of the templates
		require_once('./includes/date.php');
		foreach ($templatelist AS $group => $subtemplates)
		{
			if (count($subtemplates) > 1)
			{
				sort($subtemplates);

				unset($orderlist);
				foreach ($subtemplates AS $_key => $_value)
				{
					$orderlist["$_value[templatename]"] = $_key;
				}

				ksort($orderlist);

				$optlist[] = "\t<optgroup class=\"manager_group\" label=\"" . sprintf($lang->string('%1$s Group'), $group) . "\">";
				foreach ($orderlist AS $key => $value)
				{
					$_value = &$subtemplates["$value"];

					$templateid = ($style['styleid'] != -1 ? ($_value['ismaster'] ? '^' : $_value['templateid']) : $_value['templateid']);

					$date = pcdate('standard', $_value['dateline']);
					$optlist[] = "\t\t<option class=\"manager_group_item\" tid=\"$templateid\" date=\"$date\" tname=\"$_value[templatename]\">&nbsp;-- $_value[templatename]</option>";
				}

				$optlist[] = "\t</optgroup>";
			}
			else
			{
				$templateid = ($style['styleid'] != -1 ? ($subtemplates[0]['ismaster'] ? '^' : $subtemplates[0]['templateid']) : $subtemplates[0]['templateid']);
				$date = pcdate('standard', $subtemplates[0]['dateline']);
				$optlist[] = "\t<option tid=\"$templateid\" date=\"$date\" tname=\"{$subtemplates[0][templatename]}\">{$subtemplates[0][templatename]}</option>";
			}
		}

		$jsout_style = $style['styleid'];

		unset($templates);

		$jslang = js_safe_language(array(
			'templateinfo'				=> $lang->string('Welcome to the ProCal template system. Click on a template\'s name to view it\'s information here. You can double click it to edit it or you can use one of the links above.'),
			'templateinfo_div_nochange'	=> $lang->string('<div align="center"><strong>%1$s</strong> has not been modified.</div>
<br />
To edit this template, use the button above.'),
			'templateinfo_div_changed'	=> $lang->string('<div align="center"><strong>%1$s</strong> has been modified</div>
<br />
If you want to revert this template to its original state, you can do so with the button above.<br />
<br />
<strong>Modified on:</strong> %2$s'),
			'[UNDEFINED: %1$s]'
		));

		require('./admin/js.php');
		$print->page_code($js_template_manager);

		// Render the select menu
		$print->table_start();
		$print->table_head(sprintf($lang->string('%1$s Manager'), $lang->string('Template')). ' - ' . $style['stylename'] . ' (styleid: ' . $style['styleid'] . ')');
		$print->form_start('', '', false, 'templatemanager');

$control_box =
"<table cellpadding=\"4\" cellspacing=\"0\" border=\"0\" align=\"center\" width=\"250\" class=\"tborder\">
<tr>
	<td class=\"tcat\" align=\"center\" colspan=\"1\">
		<input type=\"button\" onclick=\"click_handler('edit');\" class=\"button\" disabled=\"disabled\" name=\"editbutton\" value=\"" . $lang->string('Edit') . "\" />&nbsp;
		<input type=\"button\" onclick=\"click_handler('revert');\" class=\"button\" disabled=\"disabled\" name=\"revertbutton\" value=\"" . $lang->string('Revert') . "\" />&nbsp;
		<input type=\"button\" onclick=\"click_handler('view');\" class=\"button\" disabled=\"disabled\" name=\"vieworigbutton\" value=\"" . $lang->string('View Original') . "\" />
	</td>
</tr>
<tr>
	<td class=\"nav_item\" colspan=\"1\" align=\"center\">
		<fieldset style=\"color:#FFFFFF; border:2px ridge; padding:5px; margin:10px; text-align:left;\">
			<div id=\"templateinfo\">" . $lang->string('Welcome to the ProCal template system. Click on a template\'s name to view it\'s information here. You can double click it to edit it or you can use one of the links above.') . "</div>
		</fieldset>" . ($style['styleid'] != -1 ? "
		<a href=\"templates.php?do=resync&styleid=$style[styleid]\">" . $lang->string('Resync All Templates') . "</a>
		<br />
		<br />" : '') . "
	</td>
</tr>
</table>";

		$print->row_text("\n<select name=\"manager\" size=\"25\" onchange=\"change_handler();\" onclick=\"change_handler();\" ondblclick=\"click_handler('edit');\" class=\"manager_body\" style=\"width: 350px;\">\n" . implode("\n", $optlist) . "\n</select>\n", $control_box);

		unset($optlist);

		$print->form_end();
		$print->row_span('<a href="templates.php?do=add&amp;styleid=' . $style['styleid'] . '">' . sprintf($lang->string('[%1$s]'), $lang->string('New')) . '</a>', 'tfoot', 'center');

		$print->form_start('templates.php', 'styleopts');
		$print->form_hidden_field('styleid', $style['styleid']);
		$print->table_head($lang->string('StyleVars <span style="float:right" class="smallfont">Leave StyleVars blank to have them revert to their defaults.</span>'));

		// -------------------------------------------------------------------
		// handle style vars
		// -------------------------------------------------------------------

		// get master stylevars
		$masterset = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "template WHERE templatename = 'STYLE_VARS' AND styleid = -1");

		// get current active stylevars
		$currentactive = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "template WHERE templatename = 'STYLE_VARS' AND styleid = $style[styleid]");

		// evaluate the master set
		eval($masterset['template']);
		$mastervars = $stylevar;

		// evaluate the second set
		if (trim($currentactive['template']))
		{
			eval($currentactive['template']);
		}
		else
		{
			$currentactive = array();
		}

		// print out stylevars
		foreach ($mastervars AS $varname => $var)
		{
			$print->row_input(AdminStrings::fetchStyleVar($varname) . ($var != $stylevar["$varname"] ? "<span class=\"smallfont\"><strong>" . $lang->string('Modified') . "</strong></span>" : ''), "stylevar[$varname]", htmlspecialchars(stripslashes($stylevar["$varname"])));
		}

		$print->row_submit();
		$print->form_end();

		$print->table_end();
	}

	$print->page_end();
}

// ####################### UPDATE STYLE OPTIONS ######################
if ($_POST['do'] == 'styleopts')
{
	$kalens->input_clean('styleid', TYPE_INT);

	$masterset = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "template WHERE templatename = 'STYLE_VARS' AND styleid = -1");
	eval($masterset['template']);

	foreach ($_POST['stylevar'] AS $var => $value)
	{
		$varset[] = "\$stylevar[$var] = '" . (!trim($value) ? ($vars['styleid'] != -1 ? $stylevar["$var"] : '') : addslashes(html_entity_decode($value))) . "';";
	}

	$stylevars = implode("\n\n", $varset);

	if ($kalens->in['styleid'] == -1 OR $stylevars != $masterset['template'])
	{
		if ($db->query_first("SELECT * FROM " . TABLE_PREFIX . "template WHERE templatename = 'STYLE_VARS' AND styleid = $vars[styleid]"))
		{
			$db->query("UPDATE " . TABLE_PREFIX . "template SET template = '" . addslashes($stylevars) . "', dateline = " . TIMENOW . " WHERE templatename = 'STYLE_VARS' AND styleid = " . $kalens->in['styleid']);
		}
		else
		{
			$db->query("INSERT INTO " . TABLE_PREFIX . "template (templatename, styleid, template, dateline) VALUES ('STYLE_VARS', " . $kalens->in['styleid'] . ", '" . addslashes($stylevars) . "', " . TIMENOW . ")");
		}
	}
	else
	{
		$db->query("DELETE FROM " . TABLE_PREFIX . "template WHERE templatename = 'STYLE_VARS' AND styleid = " . $kalens->in['styleid']);
	}
	$print->redirect("templates.php?do=manage&id=" . $kalens->in['styleid']);
}

/*=====================================================================*\
|| ###################################################################
|| # $HeadURL$
|| # $Id$
|| ###################################################################
\*=====================================================================*/
?>