Přeskočit na obsah

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

Z Wikiverzity

functions.php

[editovat]

PHP funkce pro webový formulář Projekt: Hesla Jednoty bratrské/2018/index_php. Verze z 2017-03-09:

<?php
/* sirka hlavniho ramecku se nastavuje v souboru styl.css #stranka-obal - radek 23 */
/*
verze 2017.01 ze dne : 2017-03-09	
licence: GFDL + CC-BY-SA 3.0
autor původního PHP kódu pro Hesla na rok 2009: Slávek Heřman
struktura databáze + úpravy PHP kódu pro další roky: Petr Heřman aka http://cs.wikiversity.org/wiki/User:Kychot
*/

define("SERVER", "localhost");
define("PASSWORD", "********");

/* LOCALHOST */
//define("USER", "petr");
//define("DB_NAME", "hes18");

/* DULOS */
define("USER", "dulos7146");
//define("DB_NAME", "dulos191");   //neevidována
//define("DB_NAME", "dulos933");   //neevidována
//define("DB_NAME", "dulos8551");  //2016mirek
//define("DB_NAME", "dulos9715");  //2016petr
//define("DB_NAME", "dulos1398");  //2016rfc
//define("DB_NAME", "dulos1152");  //2017mirek
//define("DB_NAME", "dulos8482");  //2017rfc
//define("DB_NAME", "dulos2827");  //2018mirek
define("DB_NAME", "dulos2831");  //2018rfc
//define("DB_NAME", "dulos4978");  //2018honza

define("DEFAULT_DATE", "2018-01-01"); /* Začátek roku */

// width of losung textarea in cols
$losung_width = 60;
// $losung_width = 65;
// $losung_width = 70;
$quote_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 <tt>", DB_NAME, "</tt>\n";
		exit;
	}
	mysql_query("set names 'utf8'");

	return $spojeni;
}
connectDB();

function get_losung($title, $date, $which, $lang, $transl, $edit=true) {
	global $losung_width;
	$date = date("Y-m-d", $date);
	printf ("\t\t<!-- get_losung(title='%s', date='%s', which='%s', lang='%s', transl='%s', edit='%s' -->\n",
	                                    $title, $date, $which, $lang, $transl, $edit);
	$query = "
	SELECT * FROM `losung`
	WHERE `date` = '$date'
		AND `which`  = '$which'
		AND `lang`   = '$lang'
	";
	if ($transl) {
                $query .= "		AND `transl` = '$transl'
	";
	}
	$result = mysql_query($query);
	printf ("\t\t<!-- mysql_num_rows(%s) = %d -->\n",  $result, mysql_num_rows($result));
	if (mysql_num_rows($result) > 0) { 	// záznam je v databázi
		$row = mysql_fetch_array($result);

		if ($edit) { 			// text má být editovatelný
			if ($row["sel"] == 1) { 	// text je vybrán -  má se 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' /><i>:</i></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&amp;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, jen nabídka na zobrazení
				echo "$title <a href=\"{$_SERVER['PHP_SELF']}?date=$date&amp;sel=losung_{$row["date"]}_{$row["which"]}_{$row["transl"]}_1\" class='deselected'>\n";
				echo "\t<small>(přidat)</small></a><br />";
			}
		}
		else { 				// text nemá být editovatelný
			echo (!empty($row["intro"]) ? "<em><font color='#A04000'>{$row["intro"]}</font></em>" : "")."\n{$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 "\t{$title} <a href=\"{$_SERVER['PHP_SELF']}?date={$date}&amp;sel=losung_{$date}_{$which}_{$transl}_new\" class='deselected'>\n";
			echo "\t<small>(přidat)</small></a><br />";
	}
}
	
function get_quote($title, $date, $which, $lang, $transl, $edit=true) {
	global $quote_width;
	$date = date("Y-m-d", $date);
	printf ("\t\t<!-- get_quote(date='%s', which='%s', lang='%s', transl='%s', edit='%s' -->\n",
		$title, $date, $which, $lang, $transl, $edit);
	$query = "
	SELECT * FROM `quote`
	WHERE `date` = '$date'
		AND `which`  = '$which'
		AND `lang`   = '$lang'
	";
	if ($transl) {
                $query .= "		AND `transl` = '$transl'
	";
	}
	$result = mysql_query($query);
	printf ("\t\t<!-- mysql_num_rows(%s) = %d -->\n",  $result, mysql_num_rows($result));
	if (mysql_num_rows($result) > 0) { 	// záznam je v databázi
		$row = mysql_fetch_array($result);

		if ($edit) { 			// text má být editovatelný
			if ($row["sel"] == 1) { 	// text je vybrán -  má se zobrazit
				echo <<<END
	<table summary="quote">
		<tr>
			<td><strong>{$title}:</strong></td>
			<td rowspan="2"><textarea rows='3' cols='{$quote_width}' name='quote_{$row["date"]}_{$row["which"]}_{$row["transl"]}_text'>
{$row['text']}</textarea></td>
		</tr>
		<tr>
			<td><input class="source" type='text' value='{$row["source"]}'name='quote_{$row["date"]}_{$row["which"]}_{$row["transl"]}_source' /></td>
		</tr>
	</table>

END;
			}
		}
	}
}
	
function get_day($title, $date, $which, $lang) {
	$date = date("Y-m-d", $date);
	printf ("\t\t<!-- get_day(title='%s', date='%s', which='%s', lang='%s' -->\n", $title, $date, $which, $lang);
	$query = "
	SELECT * FROM `day`
	WHERE `date` = '$date'
		AND `which` = '$which'
		AND `lang`  = '$lang'
	";
	$result = mysql_query($query);
	printf ("\t\t\t<!-- mysql_num_rows(%s) = %d -->\n", $result, mysql_num_rows($result));
	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&amp;sel=day_{$date}_{$which}_cs_0\"><small>(odebrat)</small></a><br />";
			echo "<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<input size='50' type='text' value='{$row["meaning"]}' name='day_{$row["date"]}_{$row["which"]}_{$row["lang"]}_meaning' />\n";
	  		echo " odkaz: <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"], 'de', false, false);
			#echo "<br />";
			get_losung("ekumenický", strtotime($date),$row["which"], 'cs', "cep");
			get_losung("kralický", strtotime($date),  $row["which"], 'cs', "kra");
			get_losung("jiný", strtotime($date),      $row["which"], 'cs', "oth");
			get_quote("Citát", strtotime($date),      $row["which"], 'cs', "MM");
		} else {		// text není vybrán - možnost přidat jej
			echo "\t<a href=\"{$_SERVER['PHP_SELF']}?date=$date&amp;sel=day_{$date}_{$which}_cs_1\"><small>(přidat)</small></a>\n";
			echo "\t<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 display_wochenlied($nr, $text) {
  $link_name = explode(']', $text);
  $link = $link_name[0];
  $name = $link_name[1];
  $link = str_replace('[', '', $link);
  $link = "http://www.liederdatenbank.de/song/$link";
  echo "$hdr <b>EG $nr</b> <a href=\"$link\" target=\"_blank\">$name</a><br />
";
}

function wochenlied($date) {
  $date = date("Y-m-d", $date);
  printf ("\t<!-- display_wochenlied(date='%s') -->", $date);
  $query = "
	SELECT * FROM `song`
	WHERE `date` = '$date'
		AND `which` = 'WL'
		AND `lang` = 'de'
	";
  $result = mysql_query($query);
  printf ("\t<!-- mysql_num_rows(%s) = %d -->\n",  $result, mysql_num_rows($result));
  if (mysql_num_rows($result) > 0) {
    $row = mysql_fetch_array($result);
    $nr = $row['nr'];
    $text=$row['text'];
    $nr12 = explode('|', $nr);
    $text12 = explode('|', $text);
    echo "Wochenlied: ";
    display_wochenlied($nr12[0], $text12[0]);
    if($nr12[1]) {
      echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nebo: ";
      display_wochenlied($nr12[1], $text12[1]);
    }
  }
} 

function get_song($date, $which, $lang) {
  $date = date("Y-m-d", $date);
  printf ("\t<!-- get_song(date='%s') -->", $date);
  $query = "
	SELECT * FROM `song`
	WHERE `date` = '$date'
		AND `which` = '$which'
		AND `lang` = '$lang'
	";
  $result = mysql_query($query);
  printf ("\t<!-- mysql_num_rows(%s) = %d -->\n",  $result, mysql_num_rows($result));
	
  if (mysql_num_rows($result) == 0) {
    echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&amp;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&amp;sel=song_{$date}_Ld_cs_1\"><small>(přidat)</small></a><br />";
    } else { // zobrazit celé
      echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&amp;sel=song_{$date}_Ld_cs_0\"><small>(odebrat)</small></a><br />";
      echo <<<END
			  <textarea cols='45' 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);
  printf ("\t<!-- get_comment(date='%s') -->", $date);
  $query = "
	SELECT * FROM `comment`
	WHERE `date` = '$date'
	AND `lang` = 'cs'
	ORDER BY `lang` DESC
	";
  $result = mysql_query($query);
  printf ("\t<!-- mysql_num_rows(%s) = %d -->\n",  $result, mysql_num_rows($result));

  if (mysql_num_rows($result) == 0) {
    echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&amp;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&amp;sel=comment_{$date}_x_cs_1\"><small>(přidat)</small></a><br />";
    } else {
      echo "<a href=\"{$_SERVER['PHP_SELF']}?date=$date&amp;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);
	
	// losungen
	$whiches = array('YEAR', 'MONTH', 'NT', 'OT', 'sunday', 'holiday', 'important', 'week');
	$transls = array('cep', 'kra', 'oth');	
	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);
	    }
	  }
	}
	
	// quotes
	$whiches = array('YEAR', 'MONTH', 'NT', 'OT', 'sunday', 'holiday', 'important', 'week');
	$transls = array('MM');	
	foreach ($whiches as $which) {
	  foreach ($transls as $transl) {
	    if (isset($_POST["quote_{$date}_{$which}_{$transl}_text"])) {
				$query = "
				UPDATE `quote`
				SET
					`text`='".$_POST["quote_{$date}_{$which}_{$transl}_text"]."',
					`source`='".$_POST["quote_{$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', 'Pr');
	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);
}