<commit_before><?php

include_once './_common.php';

no_access_if_not_allowed('offers/edit');

bad_request_if(empty($_GET['id']));

$referer = get_referer("offers/view.php?id={$_GET['id']}");

if (!is('post') || empty($_POST['offer'])) go_to($referer);

$oldOffer = fetch_one('SELECT id, number, closedAt FROM offers WHERE id=? LIMIT 1', array(1 => $_GET['id']));

if (empty($oldOffer) || !empty($oldOffer->closedAt)) not_found();

$offer = $_POST['offer'];
$items = empty($_POST['item']) ? array() : $_POST['item'];

$conn = get_conn();

try
{
  $conn->beginTransaction();

  $fields = array('title', 'supplier', 'supplierContact', 'client', 'clientContact', 'intro', 'outro');
  $bindings = array();

  foreach ($fields as $field)
  {
    if (array_key_exists($field, $offer))
    {
      $bindings[$field] = $offer[$field];
    }
  }

  exec_update('offers', $bindings, "id={$oldOffer->id}");
  exec_stmt('DELETE FROM offer_items WHERE offer=?', array(1 => $oldOffer->id));

  foreach ($items as $item)
  {
    $item['offer'] = $oldOffer->id;

    exec_insert('offer_items', $item);
  }

  $conn->commit();

  set_flash(sprintf('Oferta <%s> została zaktualizowana pomyślnie.', $oldOffer->number));
}
catch (Exception $x)
{
  set_flash($x->getMessage(), 'error');
}

go_to($referer);
<commit_msg>offers: fixed item positions not being saved properly on edit.<commit_after><?php

include_once './_common.php';

no_access_if_not_allowed('offers/edit');

bad_request_if(empty($_GET['id']));

$referer = get_referer("offers/view.php?id={$_GET['id']}");

if (!is('post') || empty($_POST['offer'])) go_to($referer);

$oldOffer = fetch_one('SELECT id, number, closedAt FROM offers WHERE id=? LIMIT 1', array(1 => $_GET['id']));

if (empty($oldOffer) || !empty($oldOffer->closedAt)) not_found();

$offer = $_POST['offer'];
$items = empty($_POST['item']) ? array() : $_POST['item'];

$conn = get_conn();

try
{
  $conn->beginTransaction();

  $fields = array('title', 'supplier', 'supplierContact', 'client', 'clientContact', 'intro', 'outro');
  $bindings = array();

  foreach ($fields as $field)
  {
    if (array_key_exists($field, $offer))
    {
      $bindings[$field] = $offer[$field];
    }
  }

  exec_update('offers', $bindings, "id={$oldOffer->id}");
  exec_stmt('DELETE FROM offer_items WHERE offer=?', array(1 => $oldOffer->id));

  foreach ($items as $k => $item)
  {
    $item['offer'] = $oldOffer->id;
    $item['position'] = $k + 1;

    exec_insert('offer_items', $item);
  }

  $conn->commit();

  set_flash(sprintf('Oferta <%s> została zaktualizowana pomyślnie.', $oldOffer->number));
}
catch (Exception $x)
{
  set_flash($x->getMessage(), 'error');
}

go_to($referer);

{'fraction_non_alphanumeric': 0.22288483041371598, 'fraction_numerical': 0.002609019754006709, 'mean_word_length': 5.360189573459715, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6224744', 'n_tokens_mistral': 1015, 'n_tokens_neox': 997, 'n_words': 189}