Skip to content

Commit

Permalink
Default Settings: Complete copy method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nate committed Dec 19, 2019
1 parent fb05e4b commit aeeca3a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 121 deletions.
17 changes: 10 additions & 7 deletions core/default_settings/default_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
if (is_array($_POST['default_settings'])) {
$action = $_POST['action'];
$search = $_POST['search'];
$domain_uuid = $_POST['domain_uuid'];
$default_settings = $_POST['default_settings'];
}

Expand All @@ -55,6 +56,7 @@
case 'copy':
if (permission_exists('default_setting_add')) {
$obj = new default_settings;
$obj->domain_uuid = $domain_uuid;
$obj->copy($default_settings);
}
break;
Expand Down Expand Up @@ -161,21 +163,21 @@
echo "<div class='action_bar' id='action_bar'>\n";
echo " <div class='heading'><b>".$text['title-default_settings']." (".$num_rows.")</b></div>\n";
echo " <div class='actions'>\n";
echo button::create(['label'=>$text['button-reload'],'icon'=>$_SESSION['theme']['button_icon_reset'],'type'=>'button','id'=>'button_reload','link'=>'default_settings_reload.php'.($search != '' ? '?search='.urlencode($search) : null),'style'=>'margin-right: 15px;']);
if (permission_exists('default_setting_add')) {
echo button::create(['type'=>'button','label'=>$text['button-add'],'icon'=>$_SESSION['theme']['button_icon_add'],'link'=>'default_setting_edit.php']);
}
if (permission_exists('default_setting_add') && $default_settings) {
//echo button::create(['type'=>'button','label'=>$text['button-copy'],'icon'=>$_SESSION['theme']['button_icon_copy'],'onclick'=>"if (confirm('".$text['confirm-copy']."')) { list_action_set('copy'); list_form_submit('form_list'); } else { this.blur(); return false; }"]);
if (permission_exists("domain_select") && permission_exists("domain_setting_add") && count($_SESSION['domains']) > 1) {
echo " <input type='button' class='btn' id='button_copy' alt='".$text['button-copy']."' onclick='show_domains();' value='".$text['button-copy']."'>";
echo " <input type='button' class='btn' style='display: none;' id='button_back' alt='".$text['button-back']."' onclick='hide_domains();' value='".$text['button-back']."'> ";
echo " <select class='formfld' style='display: none; width: auto;' name='target_domain_uuid' id='target_domain_uuid'>\n";
echo " <option value=''>Select Domain...</option>\n";
echo button::create(['type'=>'button','label'=>$text['button-copy'],'id'=>'button_copy','icon'=>$_SESSION['theme']['button_icon_copy'],'onclick'=>'show_domains();']);
echo button::create(['type'=>'button','label'=>$text['button-back'],'id'=>'button_back','icon'=>$_SESSION['theme']['button_icon_refresh'],'style'=>'display: none;','onclick'=>'hide_domains();']);
echo "<select class='formfld' style='display: none; width: auto;' id='target_domain_uuid' onchange=\"document.getElementById('domain_uuid').value = this.options[this.selectedIndex].value;\">\n";
echo " <option value=''>".$text['label-domain']."...</option>\n";
foreach ($_SESSION['domains'] as $domain) {
echo " <option value='".escape($domain["domain_uuid"])."'>".escape($domain["domain_name"])."</option>\n";
}
echo " </select>\n";
echo " <input type='button' class='btn' id='button_paste' style='display: none;' alt='".$text['button-paste']."' value='".$text['button-paste']."' onclick=\"$('#frm').attr('action', 'default_settings.php?search='+$('#default_setting_search').val()).submit();\">";
echo " </select>";
echo button::create(['type'=>'button','label'=>$text['button-paste'],'id'=>'button_paste','icon'=>'paste','style'=>'display: none;','onclick'=>"if (confirm('".$text['confirm-copy']."')) { list_action_set('copy'); list_form_submit('form_list'); } else { this.blur(); return false; }"]);
}
}
if (permission_exists('default_setting_edit') && $default_settings) {
Expand All @@ -202,6 +204,7 @@
echo "<form id='form_list' method='post'>\n";
echo "<input type='hidden' id='action' name='action' value=''>\n";
echo "<input type='hidden' name='search' value=\"".escape($search)."\">\n";
echo "<input type='hidden' name='domain_uuid' id='domain_uuid'>";

echo "<table class='list'>\n";
if (is_array($default_settings) && @sizeof($default_settings) != 0) {
Expand Down
197 changes: 83 additions & 114 deletions core/default_settings/resources/classes/default_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ class default_settings {
private $toggle_field;
private $toggle_values;
private $location;
public $domain_uuid;

/**
* called when the object is created
*/
public function __construct() {
//assign the variables
$this->app_name = 'default_settings';
$this->app_uuid = '';
$this->app_uuid = '2c2453c0-1bea-4475-9f44-4d969650de09';
$this->name = 'default_setting';
$this->table = 'default_settings';
$this->toggle_field = 'default_setting_enabled';
Expand Down Expand Up @@ -209,134 +210,102 @@ public function copy($records) {
//get checked records
foreach($records as $record) {
if ($record['checked'] == 'true' && is_uuid($record['uuid'])) {
$uuids[] = "'".$record['uuid']."'";
$uuids[] = $record['uuid'];
}
}

/*
$target_domain_uuid = $_POST["target_domain_uuid"];
if (is_uuid($target_domain_uuid) && is_array($default_setting_uuids) && sizeof($default_setting_uuids) > 0) {
$settings_copied = 0;
foreach ($default_setting_uuids as $default_setting_uuid) {
// get default setting from db
$sql = "select * from v_default_settings ";
$sql .= "where default_setting_uuid = :default_setting_uuid ";
$parameters['default_setting_uuid'] = $default_setting_uuid;
$database = new database;
$row = $database->select($sql, $parameters, 'row');
if (is_array($row) && sizeof($row) != 0) {
$default_setting_category = $row["default_setting_category"];
$default_setting_subcategory = $row["default_setting_subcategory"];
$default_setting_name = $row["default_setting_name"];
$default_setting_value = $row["default_setting_value"];
$default_setting_order = $row["default_setting_order"];
$default_setting_enabled = $row["default_setting_enabled"];
$default_setting_description = $row["default_setting_description"];
}
unset($sql, $parameters, $row);
//set a random password for http_auth_password
if ($default_setting_subcategory == "http_auth_password") {
$default_setting_value = generate_password();
}
//copy settings
if (is_uuid($this->domain_uuid) && is_array($uuids) && sizeof($uuids) > 0) {
$settings_copied = 0;
foreach ($uuids as $x => $uuid) {

// check if exists
$sql = "select domain_setting_uuid from v_domain_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and domain_setting_category = :domain_setting_category ";
$sql .= "and domain_setting_subcategory = :domain_setting_subcategory ";
$sql .= "and domain_setting_name = :domain_setting_name ";
$sql .= "and domain_setting_name <> 'array' ";
$parameters['domain_uuid'] = $target_domain_uuid;
$parameters['domain_setting_category'] = $default_setting_category;
$parameters['domain_setting_subcategory'] = $default_setting_subcategory;
$parameters['domain_setting_name'] = $default_setting_name;
$database = new database;
$target_domain_setting_uuid = $database->select($sql, $parameters, 'column');
$message = $database->message;
$action = is_uuid($target_domain_setting_uuid) ? 'update' : 'add';
unset($sql, $parameters);
// fix null
$default_setting_order = $default_setting_order != '' ? $default_setting_order : null;
//begin array
$array['domain_settings'][0]['domain_uuid'] = $target_domain_uuid;
$array['domain_settings'][0]['domain_setting_category'] = $default_setting_category;
$array['domain_settings'][0]['domain_setting_subcategory'] = $default_setting_subcategory;
$array['domain_settings'][0]['domain_setting_name'] = $default_setting_name;
$array['domain_settings'][0]['domain_setting_value'] = $default_setting_value;
$array['domain_settings'][0]['domain_setting_order'] = $default_setting_order;
$array['domain_settings'][0]['domain_setting_enabled'] = $default_setting_enabled;
$array['domain_settings'][0]['domain_setting_description'] = $default_setting_description;
//insert
if ($action == "add" && permission_exists("domain_select") && permission_exists("domain_setting_add") && count($_SESSION['domains']) > 1) {
$array['domain_settings'][0]['domain_setting_uuid'] = uuid();
}
//update
if ($action == "update" && permission_exists('domain_setting_edit')) {
$array['domain_settings'][0]['domain_setting_uuid'] = $target_domain_setting_uuid;
}
//execute
if (is_uuid($array['domain_settings'][0]['domain_setting_uuid'])) {
$database = new database;
$database->app_name = 'default_settings';
$database->app_uuid = '2c2453c0-1bea-4475-9f44-4d969650de09';
$database->save($array);
$message = $database->message;
unset($array);
// get default setting from db
$sql = "select * from v_default_settings ";
$sql .= "where default_setting_uuid = :default_setting_uuid ";
$parameters['default_setting_uuid'] = $uuid;
$database = new database;
$row = $database->select($sql, $parameters, 'row');
if (is_array($row) && sizeof($row) != 0) {
$default_setting_category = $row["default_setting_category"];
$default_setting_subcategory = $row["default_setting_subcategory"];
$default_setting_name = $row["default_setting_name"];
$default_setting_value = $row["default_setting_value"];
$default_setting_order = $row["default_setting_order"];
$default_setting_enabled = $row["default_setting_enabled"];
$default_setting_description = $row["default_setting_description"];
}
unset($sql, $parameters, $row);

$settings_copied++;
}
//set a random password for http_auth_password
if ($default_setting_subcategory == "http_auth_password") {
$default_setting_value = generate_password();
}

} // foreach
*/
//create the array from existing data
if (is_array($uuids) && @sizeof($uuids) != 0) {
$sql = "select * from v_".$this->table." ";
$sql .= "where ".$this->name."_uuid in (".implode(', ', $uuids).") ";
$database = new database;
$rows = $database->select($sql, $parameters, 'all');
if (is_array($rows) && @sizeof($rows) != 0) {
$x = 0;
foreach ($rows as $row) {
//copy data
$array[$this->table][$x] = $row;
// check if exists
$sql = "select domain_setting_uuid from v_domain_settings ";
$sql .= "where domain_uuid = :domain_uuid ";
$sql .= "and domain_setting_category = :domain_setting_category ";
$sql .= "and domain_setting_subcategory = :domain_setting_subcategory ";
$sql .= "and domain_setting_name = :domain_setting_name ";
$sql .= "and domain_setting_name <> 'array' ";
$parameters['domain_uuid'] = $this->domain_uuid;
$parameters['domain_setting_category'] = $default_setting_category;
$parameters['domain_setting_subcategory'] = $default_setting_subcategory;
$parameters['domain_setting_name'] = $default_setting_name;
$database = new database;
$target_domain_setting_uuid = $database->select($sql, $parameters, 'column');
$message = $database->message;

$action = is_uuid($target_domain_setting_uuid) ? 'update' : 'add';
unset($sql, $parameters);

// fix null
$default_setting_order = $default_setting_order != '' ? $default_setting_order : null;

//begin array
$array['domain_settings'][$x]['domain_uuid'] = $this->domain_uuid;
$array['domain_settings'][$x]['domain_setting_category'] = $default_setting_category;
$array['domain_settings'][$x]['domain_setting_subcategory'] = $default_setting_subcategory;
$array['domain_settings'][$x]['domain_setting_name'] = $default_setting_name;
$array['domain_settings'][$x]['domain_setting_value'] = $default_setting_value;
$array['domain_settings'][$x]['domain_setting_order'] = $default_setting_order;
$array['domain_settings'][$x]['domain_setting_enabled'] = $default_setting_enabled;
$array['domain_settings'][$x]['domain_setting_description'] = $default_setting_description;

//insert
if ($action == "add" && permission_exists("domain_select") && permission_exists("domain_setting_add") && count($_SESSION['domains']) > 1) {
$array['domain_settings'][$x]['domain_setting_uuid'] = uuid();
}
//update
if ($action == "update" && permission_exists('domain_setting_edit')) {
$array['domain_settings'][$x]['domain_setting_uuid'] = $target_domain_setting_uuid;
}

//add copy to the description
$array[$this->table][$x][$this->name.'_uuid'] = uuid();
$array[$this->table][$x][$this->name.'_description'] = trim($row[$this->name.'_description']).' ('.$text['label-copy'].')';
//execute
if (is_uuid($array['domain_settings'][$x]['domain_setting_uuid'])) {
$database = new database;
$database->app_name = $this->table;
$database->app_uuid = $this->app_uuid;
$database->save($array);
$message = $database->message;
unset($array);

//increment the id
$x++;
$settings_copied++;
}
}
unset($sql, $parameters, $rows, $row);
}

//save the changes and set the message
if (is_array($array) && @sizeof($array) != 0) {
//save the array
$database = new database;
$database->app_name = $this->app_name;
$database->app_uuid = $this->app_uuid;
$database->save($array);
unset($array);
} // foreach
}

//set message
message::add($text['message-copy']);
//set message
if ($settings_copied != 0) {
message::add($text['message-copy']);
}
unset($records);
}
}
}
} //method

}
} //class
}

?>
?>

0 comments on commit aeeca3a

Please sign in to comment.