Projekt: Hesla Jednoty bratrské/2009/functions.php

Z Wikiverzity

Soubor funkcí, volaný ze souboru index.php

<?php
/*
verze 10.01 ze dne : 2009-03-13
licence: GFDL + CC-BY-SA
autor: sl
spoluautor: http://cs.wikiversity.org/wiki/User:Kychot
*/
/*
sirka hlavniho ramecku se nastavuje v souboru styl.css #stranka-obal - radek 23
*/

/* Doplní se dle přístupových údajů: */
define("SERVER", "server");
define("USER", "uživatel");
define("PASSWORD", "přístupové heslo");
define("DB_NAME", "jméno databáze");

define("DEFAULT_DATE", "2009-07-01"); /* Například pro editora, který edituje druhé pololetí */

// width of losung textarea in cols
$losung_width = 60;


$dny = array(1 => "pondělí", "úterý", "středa", "čtvrtek", "pátek", "sobota", "neděle");
$mesice = array(1 => "leden", "únor", "březen", "duben", "květen", "červen", "červenec", "srpen", "září", "říjen", "listopad", "prosinec");

function connectDB() {
	$spojeni = MySQL_Connect(SERVER, USER, PASSWORD)
	or die ("Nepodařilo se připojit k databázovému serveru: ".MySQL_error());

	if(!MySQL_Select_DB(DB_NAME)) {
		echo "Nepodařilo se připojit k databázi.\n";
		exit;
	}
	mysql_query("set names 'utf8'");

	return $spojeni;
}
connectDB();

function get_losung($title, $date, $which, $transl, $edit=true) {
	global $losung_width;
	$date = date("Y-m-d", $date);

	$query = "
	SELECT * FROM `losung`
	WHERE `date` = '$date'
		AND `which` = '$which'
		AND `transl` = '$transl'
	";
	$result = mysql_query($query);
	if (mysql_num_rows($result) > 0) { // záznam je v databázi
		$row = mysql_fetch_array($result);

		if ($edit) { // text je má být editovatelný
		  if ($row["sel"] == 1) { // text je vybrán -  má zobrazit
			  echo <<<END
			    <table summary="heslo">
			      <tr>
							<td><input class="intro" type='text' value='{$row["intro"]}' name='losung_{$row["date"]}_{$row["which"]}_{$row["transl"]}_intro' /></td>
							<td rowspan="3"><textarea rows='4' cols='{$losung_width}' name='losung_{$row["date"]}_{$row["which"]}_{$row["transl"]}_text'>{$row['text']}</textarea></td>
						</tr>
			      <tr><td><strong>$title </strong><a href="{$_SERVER['PHP_SELF']}?date=$date&sel=losung_{$row["date"]}_{$row["which"]}_{$row["transl"]}_0"><small>(zavřít)</small></a></td></tr>
			      <tr><td><input class="source" type='text' value='{$row["source"]}'name='losung_{$row["date"]}_{$row["which"]}_{$row["transl"]}_source' /></td></tr>
					</table>
END;
			}
			else { // text není vybrán - nezobrazíse. pouze nabídka na zobrazení
				echo "$title<a href=\"{$_SERVER['PHP_SELF']}?date=$date&sel=losung_{$row["date"]}_{$row["which"]}_{$row["transl"]}_1\" class='deselected'><small>(přidat)</small></a><br />";
			}
		}
		else { // text nemá být editovatelný
			echo (!empty($row["intro"]) ? "<em>{$row["intro"]}</em>" : "") . " {$row['text']}<div class='source'>{$row['source']}</div>\n";
		}
	}
	else { // záznam není v databází - zobrazit tlačítko pro přidání
		if ($transl != "los") // pro originál nezobrazovat
			echo "{$title}<a href=\"{$_SERVER['PHP_SELF']}?date={$date}&sel=losung_{$date}_{$which}_{$transl}_new\" class='deselected'><small>(přidat)</small></a><br />";
	}
}
	
function get_day($title, $date, $which) {
	$date = date("Y-m-d", $date);
	$query = "
	SELECT * FROM `day`
	WHERE `date` = '$date'
		AND `which` = '$which'
	";
	$result = mysql_query($query);

	if (mysql_num_rows($result) > 0) {
		$row = mysql_fetch_array($result);
		echo "<strong>$title: </strong>\n";
		if ($row["sel"] == 1) { // text je vybrán -  má zobrazit
			echo "<input size='50' type='text'  value='{$row["text"]}' name='day_{$row["date"]}_{$row["which"]}_{$row["lang"]}_text' />\n";
			if ($which != "sunday")
				echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&sel=day_{$date}_{$which}_cs_0\"><small>(odebrat)</small></a><br />";
			echo "(<input size='50' type='text' value='{$row["meaning"]}' name='day_{$row["date"]}_{$row["which"]}_{$row["lang"]}_meaning' />)\n";
	  	echo " zdroj: <input type='text' value='{$row["src"]}' name='day_{$row["date"]}_{$row["which"]}_{$row["lang"]}_src' /><br />\n";
			echo "<br />";
			get_losung("", strtotime($date), $row["which"], "los", false);
			echo "<br />";
			get_losung("ekumenický", strtotime($date), $row["which"], "cep");
			get_losung("kralický", strtotime($date), $row["which"], "kra");
			get_losung("jiný", strtotime($date), $row["which"], "oth");
		} else {
				echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&sel=day_{$date}_{$which}_cs_1\"><small>(přidat)</small></a><br />";
			
		}
		
	}
}

function get_reading($title, $date, $which) {
	$date = date("Y-m-d", $date);
	$query = "
	SELECT * FROM `reading`
	WHERE `date` = '$date'
		AND `which` = '$which'
	";
	$result = mysql_query($query);

	if (mysql_num_rows($result) > 0) {
		$row = mysql_fetch_array($result);
		echo "<label for='reading_{$row["date"]}_{$row["which"]}_{$row["lang"]}_source'>$title </label>";
    echo "<input type='text'  value='{$row["source"]}' name='reading_{$row["date"]}_{$row["which"]}_{$row["lang"]}_source' id='reading_{$row["date"]}_{$row["which"]}_{$row["lang"]}_source' />\n";
    $row = mysql_fetch_array($result);
    echo " <em>{$row["source"]}</em><br />\n";
	}

}

function get_song($date, $which, $lang) {
	$date = date("Y-m-d", $date);
  $query = "
	SELECT * FROM `song`
	WHERE `date` = '$date'
		AND `which` = '$which'
		AND `lang` = '$lang'
	";
	$result = mysql_query($query);
	
  if (mysql_num_rows($result) == 0) {
		echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&sel=song_{$date}_Ld_cs_new\"><small>(přidat)</small></a><br />";
	} else {
	  $row = mysql_fetch_array($result);
	  
	  if($row['sel'] == 0) { // nezobrazit - pouze tlačítko pro zobrazení
	   	echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&sel=song_{$date}_Ld_cs_1\"><small>(přidat)</small></a><br />";
	  } else { // zobrazit celé
	    	echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&sel=song_{$date}_Ld_cs_0\"><small>(odebrat)</small></a><br />";
			echo <<<END
			  <textarea cols='40' rows='10' name="song_{$date}_{$which}_cs_text" id="song_{$date}_{$which}_cs_text">{$row["text"]}</textarea><br />
			  <label for="song_{$date}_{$which}_cs_book">jiný zpěvník </label>
			  <input type='text' name="song_{$date}_{$which}_cs_book" id="song_{$date}_{$which}_cs_book" value="{$row['book']}" /><br />
				<label for="song_{$date}_{$which}_cs_nr">číslo písně </label>
			  <input type='text' name="song_{$date}_{$which}_cs_nr" id="song_{$date}_{$which}_cs_nr" value="{$row['nr']}" /><br />
			  <label for="song_{$date}_{$which}_cs_strophe">sloka </label>
			  <input type='text' name="song_{$date}_{$which}_cs_strophe" id="song_{$date}_{$which}_cs_strophe" value="{$row['strophe']}" /><br />
			  <label for="song_{$date}_{$which}_cs_strophe2">další sloka </label>
			  <input type='text' name="song_{$date}_{$which}_cs_strophe2" id="song_{$date}_{$which}_cs_strophe2" value="{$row['strophe2']}" />
END;
		}
	}
}

function get_comment($date) {
	$date = date("Y-m-d", $date);
  $query = "
	SELECT * FROM `comment`
	WHERE `date` = '$date'
	AND `lang` = 'cs'
	ORDER BY `lang` DESC
	";
	$result = mysql_query($query);

  if (mysql_num_rows($result) == 0) {
		echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&sel=comment_{$date}_x_cs_new\"><small>(přidat)</small></a><br />";
	} else {
	  $row = mysql_fetch_array($result);

	  if($row['sel'] == 0) {
	   	echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&sel=comment_{$date}_x_cs_1\"><small>(přidat)</small></a><br />";
	  } else {
	    	echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&sel=comment_{$date}_x_cs_0\"><small>(odebrat)</small></a><br />";
			echo <<<END
			  <input type='text' size='4' name="comment_{$date}_cs_histdatetxt" value="{$row['histdatetxt']}" />
			  <input type='text' size='50' name="comment_{$date}_cs_text" value="{$row['text']}" />
END;
		}
	}
}

function save_all($date) {
	$date = date("Y-m-d", $date);
	
	$whiches = array('YEAR', 'MONTH', 'NT', 'OT', 'sunday', 'holiday', 'important', 'week');
	$transls = array('cep', 'kra', 'oth');
	
	// losungen
	foreach ($whiches as $which) {
    foreach ($transls as $transl) {
      if (isset($_POST["losung_{$date}_{$which}_{$transl}_text"])) {
				$query = "
				UPDATE `losung`
				SET
					`intro`='".$_POST["losung_{$date}_{$which}_{$transl}_intro"]."',
					`text`='".$_POST["losung_{$date}_{$which}_{$transl}_text"]."',
					`source`='".$_POST["losung_{$date}_{$which}_{$transl}_source"]."'
				WHERE `date`='{$date}' AND `which`='{$which}' AND `transl`='{$transl}'";
				mysql_query($query);
	 		}
		}
	}
	
	// days
	$whiches = array('sunday', 'holiday', 'important', 'week');
	foreach ($whiches as $which) {
		if (isset($_POST["day_{$date}_{$which}_cs_text"])) {
			$query = "
			UPDATE `day`
			SET
				`meaning`='".$_POST["day_{$date}_{$which}_cs_meaning"]."',
				`text`='".$_POST["day_{$date}_{$which}_cs_text"]."',
				`src`='".$_POST["day_{$date}_{$which}_cs_src"]."',
				`sel`='1'
			WHERE `date`='{$date}' AND `which`='{$which}' AND `lang`='cs'";
			mysql_query($query);
		}
	}
	
	// readings
	$whiches = array('SR', 'CR', 'Ev', 'Ep', 'Ps');
	foreach ($whiches as $which) {
		if (isset($_POST["reading_{$date}_{$which}_cs_source"])) {
			$query = "
			UPDATE `reading`
			SET
				`source`='".$_POST["reading_{$date}_{$which}_cs_source"]."',
				`sel`='1'
			WHERE `date`='{$date}' AND `which`='{$which}' AND `lang`='cs'";
			mysql_query($query);
		}
	}
	
	// song
	if (isset($_POST["song_{$date}_Ld_cs_text"])) {
		$query = "
		UPDATE `song`
		SET
			`book`='" .$_POST["song_{$date}_Ld_cs_book"]. "',
			`nr`='" .$_POST["song_{$date}_Ld_cs_nr"]. "',
			`strophe`='" .$_POST["song_{$date}_Ld_cs_strophe"]. "',
			`strophe2`='" .$_POST["song_{$date}_Ld_cs_strophe2"]. "',
			`text`='" .$_POST["song_{$date}_Ld_cs_text"]. "'
		WHERE `date`='$date'
		  AND `which`='Ld'
		  AND `lang`='cs'
		";
		mysql_query($query);
	}
	
	// comment
	if (isset($_POST["comment_{$date}_cs_text"])) {
	$query = "
	UPDATE `comment`
	SET
		`text`='".$_POST["comment_{$date}_cs_text"]."',
		`histdatetxt`='".$_POST["comment_{$date}_cs_histdatetxt"]."'
	WHERE `date`='{$date}' AND `lang`='cs'";
	mysql_query($query);
}

}

function sel() {
  list($table, $date, $which, $transl, $value) = explode("_", $_REQUEST['sel']);
	switch($table) {
	  case "losung":
	  	if ($value == "new") {
	  		$query = "INSERT INTO `$table`(`sel`, `date`, `which`, `lang`, `transl`, `source`, `intro`, `text`) VALUES('1', '$date', '$which', 'cs', '$transl', '', '', '')";
	  	}	else {
				$query = "UPDATE `$table`	SET `sel`='$value' WHERE `date`='{$date}' AND `which`='{$which}' AND `transl`='$transl'";
	  	}
			break;
		case "song":
	  	if ($value == "new") {
				$query = "INSERT INTO song(`sel`, `date`, `which`, `lang`) VALUES('1', '$date', '$which', '$transl')";
			} else {
				$query = "UPDATE `$table`	SET `sel`='$value' WHERE `date`='{$date}' AND `which`='{$which}' AND `lang`='$transl'";
			}
			break;
		case "comment":
	  	if ($value == "new") {
				$query = "INSERT INTO comment(`sel`, `date`, `lang`, `text`) VALUES('1', '$date', '$transl', '')";
				echo $query;
			} else {
				$query = "UPDATE `$table`	SET `sel`='$value' WHERE `date`='{$date}' AND `lang`='$transl'";
			}
			break;
			
		case "day":
			$query = "UPDATE `$table`	SET `sel`='$value' WHERE `date`='{$date}' AND `which`='{$which}' AND `lang`='{$transl}'";
			break;
	}
  mysql_query($query);
}