ResultManager/subs/part/contests_backend.php
2019-08-08 16:58:29 +03:00

178 lines
7.2 KiB
PHP

<?php
/**
* /subs/contests_backend.php
* @version 1.0
* @desc backend for contests
* @author Fándly Gergő Zoltán (fandlygergo@gmail.hu, systemtest.tk)
* @copy 2017 Fándly Gergő Zoltán
* License:
Result Manager for managing results of students in bilingual school systems.
Copyright (C) 2017 Fándly Gergő Zoltán
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, either version 3 of the License, or
(at your option) any later version.
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, see <https://www.gnu.org/licenses/>.
**/
try{
if(isset($_GET['list'])){
$filter="WHERE c.id<>0 and c.schoolyear=?";
$filter_array=array($_GET['list']);
if(isset($_POST['filter'])){
if(isset($_POST['f_search'])){
if($_POST['f_search']!=""){
$filter.=" and (c.name_1 LIKE ? or c.name_2 LIKE ? or s.name_1 LIKE ? or s.name_2 LIKE ? or c.description LIKE ?)";
array_push($filter_array, "%".$_POST['f_search']."%", "%".$_POST['f_search']."%", "%".$_POST['f_search']."%", "%".$_POST['f_search']."%", "%".$_POST['f_search']."%");
}
}
if(isset($_POST['f_subject'])){
for($i=0; $i<count($_POST['f_subject']); $i++){
if($i==0){
$filter.=" and (";
}
else{
$filter.=" or ";
}
$filter.="c.subject=?";
array_push($filter_array, $_POST['f_subject'][$i]);
}
$filter.=")";
}
if(isset($_POST['f_ministry'])){
for($i=0; $i<count($_POST['f_ministry']); $i++){
if($i==0){
$filter.=" and (";
}
else{
$filter.=" or ";
}
$filter.="c.ministry_support=?";
array_push($filter_array, $_POST['f_ministry'][$i]);
}
$filter.=")";
}
}
$sql=$db->prepare("SELECT c.id, c.name_1, c.name_2, s.name_1 AS subject_1, s.name_2 AS subject_2, c.description, c.ministry_support, c.ministry_place FROM contests AS c INNER JOIN subjects AS s ON (s.id=c.subject) ".$filter." ORDER BY c.name_1 ASC, c.name_2 ASC");
$sql->execute($filter_array);
echo "
<table class=\"table\">
<thead>
<tr>
<th data-breakpoints=\"xs sm md\">".$lang['id']."</th>
<th>".$lang['name_1']."</th>
<th>".$lang['name_2']."</th>
<th data-breakpoints=\"xs sm\">".$lang['subject_1']."</th>
<th data-breakpoints=\"xs sm\">".$lang['subject_2']."</th>
<th data-breakpoints=\"xs sm md\">".$lang['description']."</th>
<th data-breakpoints=\"xs sm\">".$lang['ministry_support']."</th>
<th data-breakpoints=\"xs sm\">".$lang['ministry_place']."</th>
<th data-breakpoints=\"xs sm md\">".$lang['tools']."</th>
</tr>
</thead>
<tbody>
";
while($row=$sql->fetch(PDO::FETCH_ASSOC)){
echo "
<tr>
<td>".$row['id']."</td>
<td>".$row['name_1']."</td>
<td>".$row['name_2']."</td>
<td>".$row['subject_1']."</td>
<td>".$row['subject_2']."</td>
<td>".str_replace(array("\n"), array("<br>"), $row['description'])."</td>
<td>".$lang['ministry_'.$row['ministry_support']]."</td>
<td>".($row['ministry_support']>0?$row['ministry_place']:"-")."</td>
<td>
<button type=\"button\" onclick=\"contestsEdit(".$row['id'].")\">".$lang['edit']."</button>
<button type=\"button\" onclick=\"contestsDelete(".$row['id'].", this)\">".$lang['delete']."</button>
</td>
</tr>
";
}
echo "
</tbody>
</table>
";
}
if(isset($_POST['new'])){
$sql=$db->prepare("SELECT COUNT(id) AS count FROM contests WHERE name_1=:name_1 or name_2=:name_2");
$sql->execute(array(":name_1"=>$_POST['name_1'], ":name_2"=>$_POST['name_2']));
$row=$sql->fetch(PDO::FETCH_ASSOC);
if($row['count']>0){
functions::setError(8);
}
else{
$sql=$db->prepare("INSERT INTO contests (name_1, name_2, subject, description, ministry_support, ministry_place, schoolyear) VALUES (:n1, :n2, :subj, :desc, :ms, :mp, :sy)");
$sql->execute(array(":n1"=>$_POST['name_1'], ":n2"=>$_POST['name_2'], ":subj"=>$_POST['subject'], ":desc"=>$_POST['description'], ":ms"=>$_POST['ministry_support'], ":mp"=>$_POST['ministry_place'], ":sy"=>$schoolyear));
$res=$sql->rowCount();
if($res<1){
functions::setError(4);
}
else{
functions::setMessage(3);
}
}
}
if(isset($_POST['delete'])){
$sql=$db->prepare("DELETE FROM contests WHERE id=:id");
$sql->execute(array(":id"=>$_POST['delete']));
$res=$sql->rowCount();
if($res<1){
functions::setError(4);
}
else{
functions::setMessage(4);
}
}
if(isset($_GET['getdata'])){
$sql=$db->prepare("SELECT COUNT(id) AS count, id, name_1, name_2, subject, description, ministry_support, ministry_place FROM contests WHERE id=:id");
$sql->execute(array(":id"=>$_GET['getdata']));
$res=$sql->fetch(PDO::FETCH_ASSOC);
if($res['count']<1){
functions::setError(6);
}
else{
echo json_encode($res);
}
}
if(isset($_POST['edit'])){
$sql=$db->prepare("SELECT COUNT(id) AS count FROM contests WHERE id=:id");
$sql->execute(array(":id"=>$_POST['edit']));
$res=$sql->fetch(PDO::FETCH_ASSOC);
if($res['count']<1){
functions::setError(6);
}
else{
$sql=$db->prepare("UPDATE contests SET name_1=:n1, name_2=:n2, subject=:subj, description=:desc, ministry_support=:ms, ministry_place=:mp WHERE id=:id");
$sql->execute(array(":n1"=>$_POST['name_1'], ":n2"=>$_POST['name_2'], ":subj"=>$_POST['subject'], ":desc"=>$_POST['description'], ":ms"=>$_POST['ministry_support'], ":mp"=>$_POST['ministry_place'], ":id"=>$_POST['edit']));
$res=$sql->rowCount();
if($res>0){
functions::setMessage(5);
}
else{
functions::setError(4);
}
}
}
}
catch(Exception $e){
functions::setError(500);
error_log($e);
}