This repository has been archived by the owner on Jan 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
savebranchexportpreset.php
87 lines (69 loc) · 2.86 KB
/
savebranchexportpreset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace BlasiusSecundus\WebtreesModules\BranchExport;
use Fisharebest\Webtrees\Filter;
use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Auth;
require_once('branchexportutils.php');
/**
* Defined in session.php
*
* @global Tree $WT_TREE
*/
global $WT_TREE;
define('WT_SCRIPT_NAME', 'modules_v3/branch_export/savebranchexportpreset.php');
require '../../includes/session.php';
header('Content-Type: text/json; charset=UTF-8');
if(!Filter::checkCsrf()){
http_response_code(406);
return;
}
$tree_id = $WT_TREE->getTreeId();
$pivot = Filter::escapeHtml(Filter::post("pivot"));
$name = Filter::escapeHtml(Filter::post("name"));
$cutoff = Filter::post("cutoff");
$rename = Filter::escapeHtml(Filter::post("rename"));
$preset_to_rename = Filter::escapeHtml(Filter::post("preset_to_rename"));
$member = Auth::isMember($WT_TREE);
try{
//checking if a preset with the same name exists
if(!$member)
throw new \Exception(sprintf(I18N::translate("The current user is not authorized to modify presets belonging to '%s'"), $WT_TREE->getName()));
if(!BranchExportUtils::validatePivot($pivot)){
throw new \Exception("Cannot save preset. Bad pivot XREF provided: $pivot");
}
$cutoff_array = is_array($cutoff) ? $cutoff : explode(",",$cutoff);
$cutoff_string = Filter::escapeHtml(is_string($cutoff) ? $cutoff : implode(",", $cutoff));
if(!BranchExportUtils::validateCutoffArray($cutoff_array)){
throw new \Exception("Cannot save preset. Bad cutoff XREF(s) provided.");
}
$preset_id = null;
if($rename)
{
$preset_id = $preset_to_rename;
}
else
{
$search_existing = Database::prepare("SELECT preset_id FROM ##branch_export_presets WHERE name = :name AND tree_id = :tree_id")->execute(["name"=>$name,"tree_id"=>$tree_id])->fetchOneRow();
$preset_id = $search_existing ? $search_existing->preset_id : NULL;
}
if($preset_id){
//update
$num_udapted = Database::prepare("UPDATE ##branch_export_presets SET pivot = :pivot, cutoff = :cutoff, name = :name WHERE preset_id =:preset_id")->execute(["name"=>$name,"pivot"=>$pivot,"cutoff"=>$cutoff_string,"preset_id"=>$preset_id])->rowCount();
}
else{
//insert new entry
$new_added = Database::prepare("INSERT INTO ##branch_export_presets SET tree_id = :tree_id, name=:name, pivot = :pivot, cutoff = :cutoff")->execute(["tree_id"=>$tree_id,"name"=>$name,"pivot"=>$pivot,"cutoff"=>$cutoff_string])->rowCount();
}
//now getting the updated list of presets
$presets = Database::prepare("SELECT * FROM ##branch_export_presets WHERE tree_id = :tree_id ORDER BY name")->execute(["tree_id"=>$tree_id])->fetchAll(\PDO::FETCH_ASSOC);
echo json_encode( ["presets"=>$presets,"selected"=>$name]);
}
catch(\Exception $ex)
{
echo json_encode([
"error"=>[
"message" => $ex->getMessage()
]
]);
}