<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 7.1.06
// Randem Systems: https://randemsystems.support
// BotBanish Screen Input Common Subroutines
// Date: 04/07/2026
// Usage:
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function get_last_day($month, $year) {
	
	$last_day = 28;
	while (checkdate($month, $last_day, $year)) { $last_day++; }
	return $last_day-1;
}

function formatSeconds($seconds) {
	
	$hours = (int)($seconds/3600);
	$seconds -= $hours * 3600;
	$minutes = (int)($seconds/60);
	$seconds -= $minutes * 60;

	return $hours . 'h ' . $minutes . 'm';
}

function day_button($name, $today=0, $onchange='') {
	
	if (!$today)
		$today = date("d");

	$js = !empty($onchange) ? $onchange : '';

	$i = 0;
	$str = '<select name="' . $name . '" id="' . $name . '" '. $js.'>'."\n";

	while ($i < 31) {
		$i++;
		switch($i) {
		case $today:
			$str .= "<option value=\"$i\" selected>$i";
			break;
		default:
			$str .= "<option value=\"$i\">$i";
		}
		$str .= '</option>' . "\n";
	}
	$str .= "</select>

	";
	return $str;
}

function day_button_2($name, $today=0, $onchange='') {

	if (!$today)
		$today = date("d");

	$js = !empty($onchange) ? $onchange : '';

	$i = 0;
	$str = '<select name="' . $name . '" id="' . $name . '" '. $js.'>'."\n";

	$str .= "<option value=1 selected>1";
	$str .= "<option value=16>16";

	$str .= '</option>' . "\n";

	$str .= "</select>

	";
	return $str;
}

function month_button ($name, $month=0, $onchange='') {
	
	if(!$month)
		$month = date("m");

	$js = !empty($onchange) ? $onchange : '';

	$i = 1;
	$str = '<select name="' . $name . '" id="' . $name . '" ' .$js.'>'."\n";
	while ($i <= 12) {
		switch($i) {
		case $month:
			$str .= "<option value=\"$i\" selected>" . date("M",mktime(0,0,0,$i,1,1999));
		break;
		default:
			$str .= "<option value=\"$i\">". date("M",mktime(0,0,0,$i,1,1999));
		}
		$str .= '</option>' . "\n";
		$i++;
	}
	$str .= "</select>

	";
	return $str;
}

function year_button ($name, $year=0, $onchange='') {
	
	if(!$year)
		$year = date("Y");

	$js = !empty($onchange) ? $onchange : '';

	$i = $year;
	$str = '<select name="' . $name . '" id="' . $name . '" ' . $js . '>' . "\n";
	while ($i <= $year+10) {
		switch($i) {
		case $year:
			$str .= "<option value=\"$i\" selected>$i";
			break;
		default:
			$str .= "<option value=\"$i\">$i";
		}
		$str .= '</option>' . "\n";
		$i++;
 }
 $str .= "</select>

 ";
 return $str;
}


function proj_status_list($name, $status='') {

		$str = PHP_EOL . '
		<SELECT NAME='.$name . '>' .PHP_EOL .
		'<OPTION VALUE="Pending"';
			if ($status == "Pending") $str .= 'SELECTED';
			$str .= '>Pending</OPTION>' . PHP_EOL;
			$str .= '<OPTION VALUE="Started" ';
			if ($status == "Started") $str .= 'SELECTED';
				$str .= '>Started</OPTION>' . PHP_EOL;
			$str .= '<OPTION VALUE="Suspended" ';
			if ($status == "Suspended") $str .= 'SELECTED';
				$str .= '>Suspended</OPTION>' . PHP_EOL;
			$str .= '<OPTION VALUE="Complete" ';
			if ($status == "Complete") $str .= 'SELECTED';
				$str .= '>Complete</OPTION>' . PHP_EOL;
		$str .= '</SELECT>' . PHP_EOL;

		return $str;
	}

//reverses the effects of htmlentities (see PHP manual)
function unhtmlentities($str) {
	
	$trans = get_html_translation_table(HTML_ENTITIES);
	$trans = array_flip($trans);
	return strtr($str, $trans);
}

function errorPage($message, $from_popup = false) {
	
	$targetWindowLocation = "error.php?errormsg=$message";

	if (!$from_popup)
		Header("Location: $targetWindowLocation");
	else
		loadMainPageAndCloseWindow($targetWindowLocation);
	exit;
}

function loadMainPageAndCloseWindow($targetWindowLocation) {
	//now close this window, and open the target page in the main window
	//(passing it all the parms it needs)

	echo "
		<html>
			<head>
				<script language='javascript'>
					function loadAndClose() {
						if (window.opener.closed) {
							//create a new window
							window.open(" . $targetWindowLocation . ", 'newMainWindow');
						}
						else {
							//get the main window's location and store it as the destination
							var targetWindowLocation = " . $targetWindowLocation .";
							var mainWindowLocation = window.opener.location.href;
							var questionPos = mainWindowLocation.indexOf('?');
							if (questionPos != -1)
								mainWindowLocation = mainWindowLocation.substring(0,questionPos);
							var destinationPos = targetWindowLocation.indexOf('destination=');
							if (destinationPos == -1)
								//just append it
								targetWindowLocation += '&desination=' + mainWindowLocation;
							else {
								var nextAmpPos = targetWindowLocation.indexOf('&', destinationPos);
								if (nextAmpPos == -1) {
									targetWindowLocation =
										targetWindowLocation.substring(destinationPos, targetWindowLocation.length);
								}
								else {
									var myRegex = new RegExp('destination=(.+?)&', 'g')
									targetWindowLocation = targetWindowLocation.replace(myRegex, 'destination=' + mainWindowLocation + '&');
								}
							}
							window.opener.location=targetWindowLocation;
						}

						//close the popup window
						window.close();
					}
				</script>
			</head>
			<body onLoad='javascript:loadAndClose();'>
				javascript may not be enabled. Javascript is required for BotBanish.php
			</body>
		</html>";

	exit;
}


function getWeekStartDay() {
	
	include("table_names.php");
	list($qhq, $numq) = dbQuery("select weekstartday from $CONFIG_TABLE where config_set_id = '1'");
	$configData = dbResult($qhq);
	return $configData["weekstartday"];
}

function getFirstUser() {
	
	include("table_names.php");
	list($qh, $num) = dbQuery("SELECT username from $USER_TABLE ");
	if ($num == 0)
		return 0;

	//get the first result
	$data = dbResult($qh);
	return $data["username"];
}

function getTimeFormat() {
	
		include("table_names.php");
		list($qhq, $numq) = dbQuery("SELECT timeformat FROM $CONFIG_TABLE WHERE config_set_id = '1'");
		$configData = dbResult($qhq);
		return $configData["timeformat"];
}

function setReportDate(&$year, &$month, &$day, &$next_week, &$prev_week, &$next_month, &$prev_month, &$time, &$time_middle_month) {
	// Current date
	if (!$month) $month = $todayMonth;
	if (!$year) $year = $todayYear;
	if (!$day) $day = $todayDay;
	$time = mktime( 0, 0, 0, $month, $day, $year);
	if (date('D',$time) != "Mon") $time = strtotime("last Monday", $time);
	$time_middle_month = mktime( 0, 0, 0, $month, 15, $year);
	$day = date('d', $time);

	// Calculate the previous and next month and week.
	$next_week =  strtotime("+1 week",$time);
	$prev_week = strtotime("-1 week",$time);
	$next_month =  strtotime("+1 month",$time_middle_month);
	$prev_month = strtotime("-1 month",$time_middle_month);
}

function employee_select_droplist_pp($name, $onChange='', $employee_id='', $startDate='', $endDate='') {

	global $BotBanishSettings, $BotBanishSession;
	
	$prefix = $BotBanishSettings['BOTBANISH_DB_PREFIX'] ;
	
	$change = '';
	if ($onChange !== '')
		$change = 'onChange="' .$onChange . '"';
	
	if ($startDate != '' && $endDate != '')

		$query = 'SELECT DISTINCT `' . $prefix . 'employee`.`emp_id`, `last`, `first`
					FROM `' . $prefix . 'employee`, `' . $prefix . 'hours`
//						WHERE `salary` = 6 AND `' . $prefix . 'employee`.`company_id` = ' . $BotBanishSession->getSessionKey('company_id') . '
						WHERE `salary` = 6 AND `' . $prefix . 'employee`.`company_id` = ' . $_SESSION['BotBanish']['company_id'] . '
							AND `' . $prefix . 'hours`.`emp_id` = `' . $prefix . 'employee`.`emp_id`
							AND `' . $prefix . 'hours`.`date` >= "' . date_format($startDate, 'Y-m-d') . '"
							AND `' . $prefix . 'hours`.`date` <= "' . date_format($endDate, 'Y-m-d') . '"
							AND `' . $prefix . 'employee`.`active` = 1
							AND `' . $prefix . 'employee`.`deleted` = 0
								ORDER BY `last`';
	else

		$query = 'SELECT `emp_id`, `last`, `first` from `' . $BotBanishSettings['BOTBANISH_DB_PREFIX'] . 'employee`
					WHERE `salary` = 6
						AND `company_id` = ' . $BotBanishSession->getSessionKey('company_id') . '
//						AND `company_id` = ' . $_SESSION['BotBanish']['company_id'] . '
						AND `active` = 1
						AND `deleted` = 0
							ORDER BY `last`';

	$rows = BotBanishExecuteSQL($query, true);
/*
	if (!is_array($rows) || count($rows) == 0){

		$row = array();
		$rows[] = array('last' => 'There are no records for the selected time period', 'first' => '', 'emp_id' => 0);
		$employee_id = 0;
	}
*/
	$str = '<select name="' . $name . '" id="' . $name . '" style="width: 25%;" '.$change;
	$str .= ">\n";
	$str .= "<option value=0></option>\n";

	foreach ($rows as $row) {

		// Don't include the admin account

		if (($row['last'] == 'admin') && ($BotBanishSession->getSessionKey('login_user') != 'admin'))
//		if (($row['last'] == 'admin') && ($_SESSION['BotBanish']['login_user'] != 'admin'))
			continue;

		$last = $row['last'];
		$first = $row['first'];
		$emp_id = $row['emp_id'];
		$str .= "<option value=\"$emp_id\"";
		$str .= (!empty($employee_id) && ($employee_id == $emp_id)) ? ' selected' : '';
		$str .= ">$last, $first</option>\n";
	}

	$str .= "</select>";
	return $str;
}

function employee_select_droplist($name, $onChange='', $employee_id='') {

	global $BotBanishSettings, $BotBanishSession;
	
	$change = '';
	if ($onChange !== '')
		$change = 'onChange="' .$onChange . '"';
	
	$sql = 'SELECT `emp_id`, `last`, `first`, `salary`
				FROM `' . $BotBanishSettings['BOTBANISH_DB_PREFIX'] . 'employee`
					WHERE `company_id` = ' . $BotBanishSession->getSessionKey('company_id') . '
//					WHERE `company_id` = ' . $_SESSION['BotBanish']['company_id'] . '
						AND `active` = 1
						AND `deleted` = 0
							ORDER BY `last`';

	$rows = BotBanishExecuteSQL($sql, true);

	$str = '<select name="' . $name . '" id="' . $name . '" style="width: 50%;" '.$change;
	$str .= ">\n";
	
	if (!is_array($rows) || count($rows) == 0){

		$row = array();
		$rows[] = array('last' => 'There are no employee records', 'first' => '', 'emp_id' => 0);
		$employee_id = 0;
		
	} else {
	
		$str .= "<option value=0></option>\n";
	}

	foreach ($rows as $row) {

		// Don't include the admin account

		if (($row['last'] == 'admin') && ($BotBanishSession->getSessionKey('login_user') != 'admin'))
			continue;

		$last = $row['last'];
		$first = $row['first'];
		$emp = !empty($row['last']) ? $row['last'] . ', ' . $row['first'] : '';
		$emp_id = $row['emp_id'];
		$str .= "<option value=\"$emp_id\"";
		$str .= (!empty($employee_id) && ($employee_id == $emp_id)) ? ' selected' : '';
		$str .= ">$emp</option>\n";
	}

	$str .= "</select>";
	return $str;
}

function company_select_droplist($name, $onChange='', $company_id='') {

	global $BotBanishSettings;
	
	$change = '';
	if ($onChange !== '')
		$change = 'onChange="' .$onChange . '"';
	
	$sql = 'SELECT `company_id`, `name`
				FROM `' . $BotBanishSettings['BOTBANISH_DB_PREFIX'] . 'company`
					WHERE `active` = 1
						AND `deleted` = 0
							ORDER BY `name`';

	$rows = BotBanishExecuteSQL($sql, true);

	if (!is_array($rows) || count($rows) == 0) {

		$row = array();
		$rows[] = array('company_id' => 0, 'name' => 'There are no company records');
		$company_id = 0;
	}

	$str = '<select name="' . $name . '" id="' . $name . '" style="width: 50%;" '.$change;
	$str .= ">\n";
	$str .= "<option value=0></option>\n";

	foreach ($rows as $row) {

		$name = $row['name'];
		$comp_id = $row['company_id'];
		$company = !empty($row['name']) ? $row['name'] : '';
		$str .= "<option value=\"$comp_id\"";
		$str .= (!empty($company_id) && ($company_id == $comp_id)) ? ' selected' : '';
		$str .= ">$name</option>\n";
	}

	$str .= "</select>";
	return $str;
}

function period_select_droplist($name, $onChange='', $salary_id=0) {

	global $BotBanishSettings;
	
	$change = '';
	if ($onChange !== '')
		$change = 'onChange="' .$onChange . '"';
	
	$sql = 'SELECT `period_id`, `name`, `description`
				FROM `' . $BotBanishSettings['BOTBANISH_DB_PREFIX'] . 'period`
					ORDER BY `name`';

	$rows = BotBanishExecuteSQL($sql, true);

	if (!is_array($rows) || count($rows) == 0) {

		$row = array();
		$rows[] = array('last' => 'There are no pay period records');
		$salary_id = 0;
	}

	$str = '<select name="' . $name . '" id="' . $name . '" style="width: 25%;" '.$change;
	$str .= ">\n";
	$str .= "<option value=0></option>\n";

	foreach ($rows as $row) {

		$period = $row['name'];
		$desc = $row['description'];
		$period_id = $row['period_id'];
		$str .= "<option value=\"$period_id\"";
		$str .= (!empty($salary_id) && ($period_id == $salary_id)) ? ' selected' : '';
		$str .= ">$period</option>\n";
	}

	$str .= "</select>";
	return $str;
}

function language_select_droplist($name, $onChange='', $language_name='', $pct=50) {

	global $BotBanishSettings;
	
	$Languages = $BotBanishSettings['BOTBANISH_LANGUAGES'];
	
	$change = '';
	if ($onChange !== '')
		$change = 'onChange="' .$onChange . '"';
	
	$str = '<select name="' . $name . '" id="' . $name . '" style="width: ' . $pct . '%;" '. $change;
	$str .= ">\n";
	
	$i = 0;

	foreach ($Languages as $key => $language) {

		$i++;
		$language_id = $i;
		$str .= "<option value=\"$language\"";
		$str .= (!empty($language) && ($language == $language_name)) ? ' selected' : '';
		$str .= ">$language</option>\n";
	}

	$str .= "</select>
	";
	return $str;
}

function timezone_select_droplist($name, $onChange='', $timezone_name='', $pct=100) {
	
	global $BotBanishSettings;
	
	$items = include $BotBanishSettings['BOTBANISH_DATA_DIR'] . 'Language/botbanish_timezones.php';
	
	 if (empty($timezone_name))
		 $timezone_name = date_default_timezone_get();
	 
	$BotBanishTimeZones = array();
	
	foreach ($items as $item)	
		$BotBanishTimeZones[$item['area']] = $item['timezone'];
	
	$change = '';
	if ($onChange !== '')
		$change = 'onChange="' .$onChange . '"';
		
	if (!is_array($BotBanishTimeZones) || count($BotBanishTimeZones) == 0) {

		$row = array();
		$rows[] = array('last' => 'There are no timezone records');
		$timezone_id = 0;
	}

	$str = '<select name="' . $name . '" id="' . $name . '" style="width:' . $pct .'%;'.$change;
	$str .= ">
	";
	
	$i = 0;

	foreach ($BotBanishTimeZones as $locale => $timezone) {

		$gmt = $items[$i++]['gmt'];

		$str .= "<option value=\"$timezone\"";
		$str .= (!empty($timezone) && ($timezone == $timezone_name)) ? ' selected' : '';
		$str .= ">$gmt - $locale</option>\n";
	}

	$str .= "</select>
	";
	return $str;
}

function yes_no_select_droplist($name, $options='', $pct = 35, $yes_no=false) {

	global $BotBanishText;

	$str = '<select name="' . $name . '" id="' . $name . '" style="width:' . $pct . '%" ' . $options;
	$str .= ">\n";

	$str .= "<option value=\"0\"";
	$str .= ($yes_no == false) ? ' selected' : '';
	$str .= ">" . $BotBanishText['no'] . "</option>\n";

	$str .= "<option value=\"1\"";
	$str .= ($yes_no == true) ? ' selected' : '';
	$str .= ">" . $BotBanishText['yes'] . "</option>\n";

	$str .= "</select>
	";
	return $str;
}

function select_droplist($name, $items, $options='', $default_item='=', $pct=100) {
	
	$items = is_array($items) ? $items : array();

	$str = '<select name="' . $name . '" id="' . $name . '" style="width: ' . $pct . '%;" '.$options;
	$str .= ">
	";
	
	$i = 0;

	foreach ($items as $key => $value) {

		$str .= "<option value=\"$key\"";
		$str .= (!empty($default_item) && ($default_item == $key)) ? ' selected' : '';
		$str .= ">" . $value . "</option>\n";
	}

	$str .= "</select>
	";
	return $str;
}

function locale_select_droplist($name, $disabled, $locale) {
	
	$disabled = empty($disabled) ? 'false' : $disabled;
	
	$query = "SELECT locale, location, charset from $LOCALE_TABLE ORDER BY location";

	$str = "<select name=\"$name\" style=\"width: 50%;\"";
	if ($disabled == 'true')
		$str .= " disabled";
	$str .= ">\n";

	list($qh, $num) = dbQuery($query);
	if ($num > 0) {
		while ($return = dbResult($qh)) {
			$current_locale = $return["locale"];
			$current_location = $return["charset"] . " - " . $return["location"];
			$str .= "<option value=\"$current_locale\"";

			if ($current_locale == $locale)
				$str .= " selected";

			$str .= ">$current_location</option>\n";
		}
	}
	$str .= "</select>";
	return $str;
}

function DisplayTextInputField($name, $size = '', $label = '', $value = '', $options = '', $password = false) {
	$str = !empty($label) ? $label .': ': '';
	
	if ($password)
		$str .=  '<input type="password"';
	else
		$str .=  '<input type="text"';
		
	$str .= !empty($size) ? ' size="' . $size . '"' : '';
	$str .= ' name="' . $name . '"';
	$str .= ' id="' . $name . '"';
	$str .= ' value="' . $value . '"';
	$str .= !empty($options) ? ' ' . $options : '';
	$str .= '>';
	return $str;
}

function DisplayNumberInputField($name, $size = '', $label = '', $value = '', $options = '') {
	
	$str = !empty($label) ? $label .': ': '';
	$str .=  '<input type="number"';
		
	$str .= !empty($size) ? ' size="' . $size . '"' : '';
	$str .= ' name="' . $name . '"';
	$str .= ' id="' . $name . '"';
	$str .= ' value="' . $value . '"';
	$str .= !empty($options) ? ' ' . $options : '';
	$str .= '>';
	return $str;
}

function DisplayCommentInputField($name, $rows, $cols, $value, $options = '', $label = '', ) {
	
	$label = !empty($label) ? $label . ':' : $label;
	return $label . '<textarea name="' . $name . '" id="' . $name . '" rows="' . $rows . '" cols="' . $cols . '"'  . $options . '>' . $value . '</textarea>';
}

function DisplayURLButton($name, $url) {
					 				 
	$str = '<button type="button" id="button" onClick=Form_Redirect("' . $url . '")>' . $name . '</button>';
	return $str;
}

function DisplayClickButton($name, $options = '') {

	$str = '<button type="button" id="button" ' . $options . '>' . $name . '</button>';
	return $str;
}

function DisplaySubmitButton($name, $options = '') {

	$str = '<button type="submit" id="button" ' . $options . '>' . $name . '</button>';
	return $str;
}

function DisplayCheckbox($name, $checked = false, $options = '') {

	$options .= !empty($checked) ? ' checked="yes"' : '';
	$str = '<input type="checkbox" id="' . $name . '" name="' . $name . '" ' . $options . '</input>';
	return $str;
}

function BotBanishDrawLine($heading = '', $color = '') {

	$background = !empty($color) ? 'background-color: ' . $color . ';' : '';
	
	$str = '<div style="width: 100%; height: 30px; border-bottom: 2px solid black; text-align: center">
	  <span style="font-size: 25px; ' . $background . ' padding: 0 10px;">
		' . ucwords($heading) . '
	  </span>
	</div>';

	return $str;
}
?>