File: /home/httpd/html/baretube.com.new/admin/edit_field.php
<?
require "db.php";
if (!is_numeric($_GET['id'])) {
exit();
}
$id = (int) $_GET['id'];
$row = dbRow("SELECT * FROM `fields` WHERE `record_num` = '$id'");
if (!is_array($row)) {
setMessage("Field ID $id does not exist!", 'error');
pageNotFound(true);
}
if (isset($_POST['formSubmit'])) {
$_POST = array_map_array('trim', $_POST);
if (!getMessages(false, 'error')) {
if (is_array($_POST['keys']) && count($_POST['keys']) > 0) {
dbQuery("DELETE FROM `fields_values` WHERE `field_id` = '$id' AND `record_num` NOT IN ('" . implode("','", $_POST['ids']) . "')");
foreach ($_POST['keys'] as $value_id => $value) {
if ($value == '' && $_POST['values'][$value_id] == '') {
continue;
}
if (isset($_POST['new'][$value_id])) {
$insert_id = dbInsert('fields_values', array(
'field_id' => $id,
'key' => $_POST['keys'][$value_id],
'value' => $_POST['values'][$value_id],
'weight' => is_numeric($_POST['weight'][$value_id]) ? $_POST['weight'][$value_id] : dbValue("SELECT MAX(`weight`) AS `weight` FROM `fields_values` WHERE `field_id` = '$id'", 'weight'),
), true);
setMessage($insert_id);
} else {
dbUpdate('fields_values', array(
'key' => $_POST['keys'][$value_id],
'value' => $_POST['values'][$value_id],
'weight' => is_numeric($_POST['weight'][$value_id]) ? $_POST['weight'][$value_id] : dbValue("SELECT MAX(`weight`) AS `weight` FROM `fields_values` WHERE `field_id` = '$id'", 'weight'),
'record_num' => $value_id,
));
}
}
}
setMessage('Field values have been saved. <a href="' . $basehttp . '/admin/fields.php"><b>Click here to return to Fields</b></a>');
header("Location: $_SERVER[REQUEST_URI]");
exit;
}
}
$_POST += $row;
$field_values = dbQuery("SELECT * FROM `fields_values` WHERE `field_id` = '$id' ORDER BY `weight`", false);
entities_walk($_POST);
entities_walk($field_values);
?>
<? include "header.php"; ?>
<div class="content-page">
<div class="header-area">
<div class="breadcrumbs">
<a href="index.php">Admin Home</a>
<span><a href="fields.php">Content Fields</a></span>
</div>
</div>
<div class="content-outer">
<h2>Edit<strong>Field</strong>: <? echo $_POST['name']; ?></h2>
<div class="content-inner">
<? if (!getMessages(false)) { ?>
<div class="notification info">After changes (sorting, removing rows, adding new rows) you must "Save" field settings.</div>
<div class="notification alert">WARNING: Changing "key" can break your current storage (e.g. profile settings for existing models). Setup "keys" only before the field is pushed LIVE.</div>
<? } else { ?>
<? echo getMessages(); ?>
<? } ?>
<form action="" method="POST" enctype="multipart/form-data" class="form" autocomplete="off" novalidate>
<table class="pagetable sortable">
<thead>
<tr>
<th style="min-width:0;width:40px">Sort</th>
<th width="25%">Key (optional)</th>
<th>Value</th>
<th class="options"></th>
</tr>
</thead>
<tbody>
<? if (is_array($field_values)) { ?>
<? foreach ($field_values as $item) { ?>
<tr data-id="<? echo $item['record_num']; ?>">
<td style="min-width:0;width:40px" class="large"><i class="icn ion ion-arrow-move move-position"></i></td>
<td><input type="text" name="keys[<? echo $item['record_num']; ?>]" value="<? echo $item['key']; ?>"></td>
<td><input type="text" name="values[<? echo $item['record_num']; ?>]" value="<? echo $item['value']; ?>"></td>
<td class="options">
<a href="#" class="btn btn-xs btn-red" data-confirm-action="delete-field-value"><i class="icn ion ion-trash-a"></i></a>
<input type="hidden" name="weight[<? echo $item['record_num']; ?>]" value="<? echo $item['weight']; ?>" class="value-weight">
<input type="hidden" name="ids[]" value="<? echo $item['record_num']; ?>">
</td>
</tr>
<? } ?>
<? } else { ?>
<tr class="error-message"><td colspan="4"><? echo setMessage('No field values existing yet.', 'error', true); ?></td></tr>
<? } ?>
<tr class="item submit">
<td colspan="4">
<a href="#" class="btn btn-default btn-red pull-left" id="field-add-value">Add New Row</a>
<input type="hidden" name="id" value="<? echo $id; ?>">
<input type="hidden" name="formSubmit" value="1">
<button type="submit" class="btn action-save">Save</button>
</td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
</div>
<script type="text/template" id="field-row-template">
<tr data-id="{%id%}">
<td style="min-width:0;width:40px" class="large"><i class="icn ion ion-arrow-move move-position"></i></td>
<td><input type="text" name="keys[{%id%}]" value=""></td>
<td><input type="text" name="values[{%id%}]" value=""></td>
<td class="options">
<a href="#" class="btn btn-xs btn-red" data-confirm-action="delete-field-value"><i class="icn ion ion-trash-a"></i></a>
<input type="hidden" name="weight[{%id%}]" value="{%weight%}" class="value-weight">
<input type="hidden" name="new[{%id%}]" value="1">
</td>
</tr>
</script>
<script>
var field_id, max_id = 0, max_weight = 0;
$().ready(function () {
$('a[data-confirm-action="delete-field-value"]').on('click', function (e) {
e.preventDefault();
if (confirm('Are you sure to delete this item?') === true) {
$(this).parents('tr').slideUp(300, function () {
$(this).remove();
});
}
});
$('table.sortable').sortable({
handle: '.move-position',
helper: 'clone',
items: 'tbody > tr',
placeholder: 'sortable-placeholder',
forcePlaceholderSize: true,
forceHelperSize: true,
cursor: 'move',
update: function (event, ui) {
var order = new Array();
$(this).find('tbody > tr').each(function (index) {
field_id = $(this).attr('data-id');
order[$(this).attr('data-id')] = index;
$(this).find('input[name="weight[' + field_id + ']"]').val(index);
});
}
});
$('#field-add-value').on('click', function (e) {
e.preventDefault();
$('tr.error-message').remove();
$('table.sortable tbody tr').each(function () {
if ($(this).attr('data-id') !== undefined) {
max_id = Math.max(parseInt($(this).attr('data-id')), max_id);
max_weight = Math.max(parseInt($(this).find('.value-weight').val()), max_weight);
}
});
max_id++;
max_weight++;
$('table.sortable tbody tr[data-id]').last().after($('#field-row-template').html().replace(/\{\%id\%\}/g, max_id).replace(/\{\%weight\%\}/g, max_weight));
});
});
</script>
<? require "footer.php"; ?>