mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-20 05:42:40 +01:00
Pholio - robustify submission errors to retain image edits
Summary: we should build all the image stuff on every post and use that posted image data if there's an error. this diff makes that so. Fixes T4380. Test Plan: made a mock with no title, tried to save it, and was delighted to see my images still there. edited a mock - removing the title and adding images - verified edits showed up after erroneous submission. added a title and submitted and changes were saved. Reviewers: epriestley Reviewed By: epriestley CC: Korvin, epriestley, aran Maniphest Tasks: T4380 Differential Revision: https://secure.phabricator.com/D8197
This commit is contained in:
parent
305fb3fbd9
commit
fec5ea8f9d
1 changed files with 111 additions and 99 deletions
|
@ -50,6 +50,7 @@ final class PholioMockEditController extends PholioController {
|
||||||
$e_name = true;
|
$e_name = true;
|
||||||
$e_images = true;
|
$e_images = true;
|
||||||
$errors = array();
|
$errors = array();
|
||||||
|
$posted_mock_images = array();
|
||||||
|
|
||||||
$v_name = $mock->getName();
|
$v_name = $mock->getName();
|
||||||
$v_desc = $mock->getDescription();
|
$v_desc = $mock->getDescription();
|
||||||
|
@ -100,94 +101,99 @@ final class PholioMockEditController extends PholioController {
|
||||||
$mock->setCoverPHID(head($files)->getPHID());
|
$mock->setCoverPHID(head($files)->getPHID());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$errors) {
|
foreach ($mock_xactions as $type => $value) {
|
||||||
foreach ($mock_xactions as $type => $value) {
|
$xactions[$type] = id(new PholioTransaction())
|
||||||
$xactions[$type] = id(new PholioTransaction())
|
->setTransactionType($type)
|
||||||
->setTransactionType($type)
|
->setNewValue($value);
|
||||||
->setNewValue($value);
|
}
|
||||||
|
|
||||||
|
$order = $request->getStrList('imageOrder');
|
||||||
|
$sequence_map = array_flip($order);
|
||||||
|
$replaces = $request->getArr('replaces');
|
||||||
|
$replaces_map = array_flip($replaces);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Foreach file posted, check to see whether we are replacing an image,
|
||||||
|
* adding an image, or simply updating image metadata. Create
|
||||||
|
* transactions for these cases as appropos.
|
||||||
|
*/
|
||||||
|
foreach ($files as $file_phid => $file) {
|
||||||
|
$replaces_image_phid = null;
|
||||||
|
if (isset($replaces_map[$file_phid])) {
|
||||||
|
$old_file_phid = $replaces_map[$file_phid];
|
||||||
|
$old_image = idx($mock_images, $old_file_phid);
|
||||||
|
if ($old_image) {
|
||||||
|
$replaces_image_phid = $old_image->getPHID();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$order = $request->getStrList('imageOrder');
|
$existing_image = idx($mock_images, $file_phid);
|
||||||
$sequence_map = array_flip($order);
|
|
||||||
$replaces = $request->getArr('replaces');
|
|
||||||
$replaces_map = array_flip($replaces);
|
|
||||||
|
|
||||||
/**
|
$title = (string)$request->getStr('title_'.$file_phid);
|
||||||
* Foreach file posted, check to see whether we are replacing an image,
|
$description = (string)$request->getStr('description_'.$file_phid);
|
||||||
* adding an image, or simply updating image metadata. Create
|
$sequence = $sequence_map[$file_phid];
|
||||||
* transactions for these cases as appropos.
|
|
||||||
*/
|
|
||||||
foreach ($files as $file_phid => $file) {
|
|
||||||
$replaces_image_phid = null;
|
|
||||||
if (isset($replaces_map[$file_phid])) {
|
|
||||||
$old_file_phid = $replaces_map[$file_phid];
|
|
||||||
$old_image = idx($mock_images, $old_file_phid);
|
|
||||||
if ($old_image) {
|
|
||||||
$replaces_image_phid = $old_image->getPHID();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$existing_image = idx($mock_images, $file_phid);
|
if ($replaces_image_phid) {
|
||||||
|
$replace_image = id(new PholioImage())
|
||||||
$title = (string)$request->getStr('title_'.$file_phid);
|
->setReplacesImagePHID($replaces_image_phid)
|
||||||
$description = (string)$request->getStr('description_'.$file_phid);
|
->setFilePhid($file_phid)
|
||||||
$sequence = $sequence_map[$file_phid];
|
->attachFile($file)
|
||||||
|
->setName(strlen($title) ? $title : $file->getName())
|
||||||
if ($replaces_image_phid) {
|
->setDescription($description)
|
||||||
$replace_image = id(new PholioImage())
|
->setSequence($sequence);
|
||||||
->setReplacesImagePHID($replaces_image_phid)
|
$xactions[] = id(new PholioTransaction())
|
||||||
->setFilePhid($file_phid)
|
->setTransactionType(
|
||||||
->setName(strlen($title) ? $title : $file->getName())
|
PholioTransactionType::TYPE_IMAGE_REPLACE)
|
||||||
->setDescription($description)
|
|
||||||
->setSequence($sequence);
|
|
||||||
$xactions[] = id(new PholioTransaction())
|
|
||||||
->setTransactionType(
|
|
||||||
PholioTransactionType::TYPE_IMAGE_REPLACE)
|
|
||||||
->setNewValue($replace_image);
|
->setNewValue($replace_image);
|
||||||
} else if (!$existing_image) { // this is an add
|
$posted_mock_images[] = $replace_image;
|
||||||
$add_image = id(new PholioImage())
|
} else if (!$existing_image) { // this is an add
|
||||||
->setFilePhid($file_phid)
|
$add_image = id(new PholioImage())
|
||||||
->setName(strlen($title) ? $title : $file->getName())
|
->setFilePhid($file_phid)
|
||||||
->setDescription($description)
|
->attachFile($file)
|
||||||
->setSequence($sequence);
|
->setName(strlen($title) ? $title : $file->getName())
|
||||||
$xactions[] = id(new PholioTransaction())
|
->setDescription($description)
|
||||||
->setTransactionType(PholioTransactionType::TYPE_IMAGE_FILE)
|
->setSequence($sequence);
|
||||||
|
$xactions[] = id(new PholioTransaction())
|
||||||
|
->setTransactionType(PholioTransactionType::TYPE_IMAGE_FILE)
|
||||||
|
->setNewValue(
|
||||||
|
array('+' => array($add_image)));
|
||||||
|
$posted_mock_images[] = $add_image;
|
||||||
|
} else {
|
||||||
|
$xactions[] = id(new PholioTransaction())
|
||||||
|
->setTransactionType(PholioTransactionType::TYPE_IMAGE_NAME)
|
||||||
|
->setNewValue(
|
||||||
|
array($existing_image->getPHID() => $title));
|
||||||
|
$xactions[] = id(new PholioTransaction())
|
||||||
|
->setTransactionType(
|
||||||
|
PholioTransactionType::TYPE_IMAGE_DESCRIPTION)
|
||||||
->setNewValue(
|
->setNewValue(
|
||||||
array('+' => array($add_image)));
|
array($existing_image->getPHID() => $description));
|
||||||
} else {
|
$xactions[] = id(new PholioTransaction())
|
||||||
$xactions[] = id(new PholioTransaction())
|
->setTransactionType(
|
||||||
->setTransactionType(PholioTransactionType::TYPE_IMAGE_NAME)
|
PholioTransactionType::TYPE_IMAGE_SEQUENCE)
|
||||||
->setNewValue(
|
->setNewValue(
|
||||||
array($existing_image->getPHID() => $title));
|
array($existing_image->getPHID() => $sequence));
|
||||||
$xactions[] = id(new PholioTransaction())
|
$posted_mock_images[] = $existing_image;
|
||||||
->setTransactionType(
|
|
||||||
PholioTransactionType::TYPE_IMAGE_DESCRIPTION)
|
|
||||||
->setNewValue(
|
|
||||||
array($existing_image->getPHID() => $description));
|
|
||||||
$xactions[] = id(new PholioTransaction())
|
|
||||||
->setTransactionType(
|
|
||||||
PholioTransactionType::TYPE_IMAGE_SEQUENCE)
|
|
||||||
->setNewValue(
|
|
||||||
array($existing_image->getPHID() => $sequence));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
foreach ($mock_images as $file_phid => $mock_image) {
|
}
|
||||||
if (!isset($files[$file_phid]) && !isset($replaces[$file_phid])) {
|
foreach ($mock_images as $file_phid => $mock_image) {
|
||||||
// this is an outright delete
|
if (!isset($files[$file_phid]) && !isset($replaces[$file_phid])) {
|
||||||
$xactions[] = id(new PholioTransaction())
|
// this is an outright delete
|
||||||
->setTransactionType(PholioTransactionType::TYPE_IMAGE_FILE)
|
$xactions[] = id(new PholioTransaction())
|
||||||
->setNewValue(
|
->setTransactionType(PholioTransactionType::TYPE_IMAGE_FILE)
|
||||||
array('-' => array($mock_image)));
|
->setNewValue(
|
||||||
}
|
array('-' => array($mock_image)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$errors) {
|
||||||
$mock->openTransaction();
|
$mock->openTransaction();
|
||||||
$editor = id(new PholioMockEditor())
|
$editor = id(new PholioMockEditor())
|
||||||
->setContentSourceFromRequest($request)
|
->setContentSourceFromRequest($request)
|
||||||
->setContinueOnNoEffect(true)
|
->setContinueOnNoEffect(true)
|
||||||
->setActor($user);
|
->setActor($user);
|
||||||
|
|
||||||
$xactions = $editor->applyTransactions($mock, $xactions);
|
$xactions = $editor->applyTransactions($mock, $xactions);
|
||||||
|
|
||||||
$mock->saveTransaction();
|
$mock->saveTransaction();
|
||||||
|
|
||||||
|
@ -220,10 +226,16 @@ final class PholioMockEditController extends PholioController {
|
||||||
->execute();
|
->execute();
|
||||||
|
|
||||||
$image_elements = array();
|
$image_elements = array();
|
||||||
foreach ($mock_images as $mock_image) {
|
if ($posted_mock_images) {
|
||||||
|
$display_mock_images = $posted_mock_images;
|
||||||
|
} else {
|
||||||
|
$display_mock_images = $mock_images;
|
||||||
|
}
|
||||||
|
foreach ($display_mock_images as $mock_image) {
|
||||||
$image_elements[] = id(new PholioUploadedImageView())
|
$image_elements[] = id(new PholioUploadedImageView())
|
||||||
->setUser($user)
|
->setUser($user)
|
||||||
->setImage($mock_image);
|
->setImage($mock_image)
|
||||||
|
->setReplacesPHID($mock_image->getReplacesImagePHID());
|
||||||
}
|
}
|
||||||
|
|
||||||
$list_id = celerity_generate_unique_node_id();
|
$list_id = celerity_generate_unique_node_id();
|
||||||
|
@ -276,37 +288,37 @@ final class PholioMockEditController extends PholioController {
|
||||||
->appendChild($order_control)
|
->appendChild($order_control)
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormTextControl())
|
id(new AphrontFormTextControl())
|
||||||
->setName('name')
|
->setName('name')
|
||||||
->setValue($v_name)
|
->setValue($v_name)
|
||||||
->setLabel(pht('Name'))
|
->setLabel(pht('Name'))
|
||||||
->setError($e_name))
|
->setError($e_name))
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new PhabricatorRemarkupControl())
|
id(new PhabricatorRemarkupControl())
|
||||||
->setName('description')
|
->setName('description')
|
||||||
->setValue($v_desc)
|
->setValue($v_desc)
|
||||||
->setLabel(pht('Description'))
|
->setLabel(pht('Description'))
|
||||||
->setUser($user))
|
->setUser($user))
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormTokenizerControl())
|
id(new AphrontFormTokenizerControl())
|
||||||
->setLabel(pht('CC'))
|
->setLabel(pht('CC'))
|
||||||
->setName('cc')
|
->setName('cc')
|
||||||
->setValue($handles)
|
->setValue($handles)
|
||||||
->setUser($user)
|
->setUser($user)
|
||||||
->setDatasource('/typeahead/common/mailable/'))
|
->setDatasource('/typeahead/common/mailable/'))
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormPolicyControl())
|
id(new AphrontFormPolicyControl())
|
||||||
->setUser($user)
|
->setUser($user)
|
||||||
->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
|
->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
|
||||||
->setPolicyObject($mock)
|
->setPolicyObject($mock)
|
||||||
->setPolicies($policies)
|
->setPolicies($policies)
|
||||||
->setName('can_view'))
|
->setName('can_view'))
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormMarkupControl())
|
id(new AphrontFormMarkupControl())
|
||||||
->setValue($list_control))
|
->setValue($list_control))
|
||||||
->appendChild(
|
->appendChild(
|
||||||
id(new AphrontFormMarkupControl())
|
id(new AphrontFormMarkupControl())
|
||||||
->setValue($drop_control)
|
->setValue($drop_control)
|
||||||
->setError($e_images))
|
->setError($e_images))
|
||||||
->appendChild($submit);
|
->appendChild($submit);
|
||||||
|
|
||||||
$form_box = id(new PHUIObjectBoxView())
|
$form_box = id(new PHUIObjectBoxView())
|
||||||
|
|
Loading…
Reference in a new issue