2012-11-22 02:23:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group pholio
|
|
|
|
*/
|
|
|
|
final class PholioMockEditController extends PholioController {
|
|
|
|
|
|
|
|
private $id;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->id = idx($data, 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
|
|
|
if ($this->id) {
|
2012-11-22 02:27:44 +01:00
|
|
|
$mock = id(new PholioMockQuery())
|
|
|
|
->setViewer($user)
|
2013-07-16 22:31:20 +02:00
|
|
|
->needImages(true)
|
2012-11-22 02:27:44 +01:00
|
|
|
->requireCapabilities(
|
|
|
|
array(
|
|
|
|
PhabricatorPolicyCapability::CAN_VIEW,
|
|
|
|
PhabricatorPolicyCapability::CAN_EDIT,
|
|
|
|
))
|
|
|
|
->withIDs(array($this->id))
|
|
|
|
->executeOne();
|
|
|
|
|
|
|
|
if (!$mock) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = pht('Edit Mock');
|
|
|
|
|
|
|
|
$is_new = false;
|
2013-07-16 22:31:20 +02:00
|
|
|
$mock_images = $mock->getImages();
|
|
|
|
$files = mpull($mock_images, 'getFile');
|
|
|
|
$mock_images = mpull($mock_images, null, 'getFilePHID');
|
2012-11-22 02:23:28 +01:00
|
|
|
} else {
|
2014-01-30 20:53:42 +01:00
|
|
|
$mock = PholioMock::initializeNewMock($user);
|
2012-11-22 02:23:28 +01:00
|
|
|
|
|
|
|
$title = pht('Create Mock');
|
2012-11-22 02:27:44 +01:00
|
|
|
|
|
|
|
$is_new = true;
|
2013-07-16 22:31:20 +02:00
|
|
|
$files = array();
|
|
|
|
$mock_images = array();
|
2012-11-22 02:23:28 +01:00
|
|
|
}
|
|
|
|
|
2014-06-07 22:54:40 +02:00
|
|
|
if ($is_new) {
|
|
|
|
$v_projects = array();
|
|
|
|
} else {
|
|
|
|
$v_projects = PhabricatorEdgeQuery::loadDestinationPHIDs(
|
|
|
|
$mock->getPHID(),
|
|
|
|
PhabricatorEdgeConfig::TYPE_OBJECT_HAS_PROJECT);
|
|
|
|
$v_projects = array_reverse($v_projects);
|
|
|
|
}
|
|
|
|
|
2012-11-22 02:23:28 +01:00
|
|
|
$e_name = true;
|
|
|
|
$e_images = true;
|
|
|
|
$errors = array();
|
2014-02-11 20:38:10 +01:00
|
|
|
$posted_mock_images = array();
|
2012-11-22 02:23:28 +01:00
|
|
|
|
2012-11-22 02:27:44 +01:00
|
|
|
$v_name = $mock->getName();
|
|
|
|
$v_desc = $mock->getDescription();
|
2014-05-19 20:33:34 +02:00
|
|
|
$v_status = $mock->getStatus();
|
2012-11-22 02:27:44 +01:00
|
|
|
$v_view = $mock->getViewPolicy();
|
2013-02-22 14:44:46 +01:00
|
|
|
$v_cc = PhabricatorSubscribersQuery::loadSubscribersForPHID(
|
|
|
|
$mock->getPHID());
|
2012-11-22 02:27:44 +01:00
|
|
|
|
2012-11-22 02:23:28 +01:00
|
|
|
if ($request->isFormPost()) {
|
2012-11-22 02:27:44 +01:00
|
|
|
$xactions = array();
|
2012-11-22 02:23:28 +01:00
|
|
|
|
2012-11-22 02:27:44 +01:00
|
|
|
$type_name = PholioTransactionType::TYPE_NAME;
|
|
|
|
$type_desc = PholioTransactionType::TYPE_DESCRIPTION;
|
2014-05-19 20:33:34 +02:00
|
|
|
$type_status = PholioTransactionType::TYPE_STATUS;
|
2012-12-11 22:59:20 +01:00
|
|
|
$type_view = PhabricatorTransactions::TYPE_VIEW_POLICY;
|
2013-02-22 14:44:46 +01:00
|
|
|
$type_cc = PhabricatorTransactions::TYPE_SUBSCRIBERS;
|
2012-11-22 02:23:28 +01:00
|
|
|
|
2012-11-22 02:27:44 +01:00
|
|
|
$v_name = $request->getStr('name');
|
|
|
|
$v_desc = $request->getStr('description');
|
2014-05-19 20:33:34 +02:00
|
|
|
$v_status = $request->getStr('status');
|
2012-11-22 02:27:44 +01:00
|
|
|
$v_view = $request->getStr('can_view');
|
2013-02-22 14:44:46 +01:00
|
|
|
$v_cc = $request->getArr('cc');
|
2014-06-07 22:54:40 +02:00
|
|
|
$v_projects = $request->getArr('projects');
|
2012-11-22 02:23:28 +01:00
|
|
|
|
2013-07-16 22:31:20 +02:00
|
|
|
$mock_xactions = array();
|
|
|
|
$mock_xactions[$type_name] = $v_name;
|
|
|
|
$mock_xactions[$type_desc] = $v_desc;
|
2014-05-19 20:33:34 +02:00
|
|
|
$mock_xactions[$type_status] = $v_status;
|
2013-07-16 22:31:20 +02:00
|
|
|
$mock_xactions[$type_view] = $v_view;
|
|
|
|
$mock_xactions[$type_cc] = array('=' => $v_cc);
|
2012-11-22 02:23:28 +01:00
|
|
|
|
2012-11-22 02:27:44 +01:00
|
|
|
if (!strlen($request->getStr('name'))) {
|
|
|
|
$e_name = 'Required';
|
2013-07-18 23:19:43 +02:00
|
|
|
$errors[] = pht('You must give the mock a name.');
|
2012-11-22 02:23:28 +01:00
|
|
|
}
|
|
|
|
|
2013-07-16 22:31:20 +02:00
|
|
|
$file_phids = $request->getArr('file_phids');
|
|
|
|
if ($file_phids) {
|
|
|
|
$files = id(new PhabricatorFileQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withPHIDs($file_phids)
|
|
|
|
->execute();
|
|
|
|
$files = mpull($files, null, 'getPHID');
|
|
|
|
$files = array_select_keys($files, $file_phids);
|
2013-07-19 00:04:08 +02:00
|
|
|
} else {
|
|
|
|
$files = array();
|
2013-07-16 22:31:20 +02:00
|
|
|
}
|
2012-11-22 02:27:44 +01:00
|
|
|
|
2013-07-16 22:31:20 +02:00
|
|
|
if (!$files) {
|
|
|
|
$e_images = pht('Required');
|
|
|
|
$errors[] = pht('You must add at least one image to the mock.');
|
|
|
|
} else {
|
|
|
|
$mock->setCoverPHID(head($files)->getPHID());
|
2012-11-22 02:23:28 +01:00
|
|
|
}
|
|
|
|
|
2014-02-11 20:38:10 +01:00
|
|
|
foreach ($mock_xactions as $type => $value) {
|
|
|
|
$xactions[$type] = id(new PholioTransaction())
|
|
|
|
->setTransactionType($type)
|
|
|
|
->setNewValue($value);
|
|
|
|
}
|
2012-11-22 02:27:44 +01:00
|
|
|
|
2014-02-11 20:38:10 +01:00
|
|
|
$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();
|
2013-07-26 01:59:25 +02:00
|
|
|
}
|
2014-02-11 20:38:10 +01:00
|
|
|
}
|
2013-07-26 01:59:25 +02:00
|
|
|
|
2014-02-11 20:38:10 +01:00
|
|
|
$existing_image = idx($mock_images, $file_phid);
|
|
|
|
|
|
|
|
$title = (string)$request->getStr('title_'.$file_phid);
|
|
|
|
$description = (string)$request->getStr('description_'.$file_phid);
|
|
|
|
$sequence = $sequence_map[$file_phid];
|
|
|
|
|
|
|
|
if ($replaces_image_phid) {
|
|
|
|
$replace_image = id(new PholioImage())
|
|
|
|
->setReplacesImagePHID($replaces_image_phid)
|
|
|
|
->setFilePhid($file_phid)
|
|
|
|
->attachFile($file)
|
|
|
|
->setName(strlen($title) ? $title : $file->getName())
|
|
|
|
->setDescription($description)
|
|
|
|
->setSequence($sequence);
|
|
|
|
$xactions[] = id(new PholioTransaction())
|
|
|
|
->setTransactionType(
|
|
|
|
PholioTransactionType::TYPE_IMAGE_REPLACE)
|
2013-07-26 01:59:25 +02:00
|
|
|
->setNewValue($replace_image);
|
2014-02-11 20:38:10 +01:00
|
|
|
$posted_mock_images[] = $replace_image;
|
|
|
|
} else if (!$existing_image) { // this is an add
|
|
|
|
$add_image = id(new PholioImage())
|
|
|
|
->setFilePhid($file_phid)
|
|
|
|
->attachFile($file)
|
|
|
|
->setName(strlen($title) ? $title : $file->getName())
|
|
|
|
->setDescription($description)
|
|
|
|
->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)
|
2013-07-16 22:31:20 +02:00
|
|
|
->setNewValue(
|
2014-02-11 20:38:10 +01:00
|
|
|
array($existing_image->getPHID() => $description));
|
|
|
|
$xactions[] = id(new PholioTransaction())
|
|
|
|
->setTransactionType(
|
|
|
|
PholioTransactionType::TYPE_IMAGE_SEQUENCE)
|
2013-07-16 22:31:20 +02:00
|
|
|
->setNewValue(
|
2014-02-11 20:38:10 +01:00
|
|
|
array($existing_image->getPHID() => $sequence));
|
2014-06-07 22:54:40 +02:00
|
|
|
|
2014-02-11 20:38:10 +01:00
|
|
|
$posted_mock_images[] = $existing_image;
|
2013-07-16 22:31:20 +02:00
|
|
|
}
|
2014-02-11 20:38:10 +01:00
|
|
|
}
|
|
|
|
foreach ($mock_images as $file_phid => $mock_image) {
|
|
|
|
if (!isset($files[$file_phid]) && !isset($replaces[$file_phid])) {
|
|
|
|
// this is an outright delete
|
|
|
|
$xactions[] = id(new PholioTransaction())
|
|
|
|
->setTransactionType(PholioTransactionType::TYPE_IMAGE_FILE)
|
|
|
|
->setNewValue(
|
|
|
|
array('-' => array($mock_image)));
|
2013-07-16 22:31:20 +02:00
|
|
|
}
|
2014-02-11 20:38:10 +01:00
|
|
|
}
|
2013-07-16 22:31:20 +02:00
|
|
|
|
2014-02-11 20:38:10 +01:00
|
|
|
if (!$errors) {
|
2014-06-07 22:54:40 +02:00
|
|
|
$proj_edge_type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_PROJECT;
|
|
|
|
$xactions[] = id(new PholioTransaction())
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_EDGE)
|
|
|
|
->setMetadataValue('edge:type', $proj_edge_type)
|
|
|
|
->setNewValue(array('=' => array_fuse($v_projects)));
|
|
|
|
|
2012-11-22 02:23:28 +01:00
|
|
|
$mock->openTransaction();
|
2014-02-11 20:38:10 +01:00
|
|
|
$editor = id(new PholioMockEditor())
|
|
|
|
->setContentSourceFromRequest($request)
|
|
|
|
->setContinueOnNoEffect(true)
|
|
|
|
->setActor($user);
|
2012-11-22 02:27:44 +01:00
|
|
|
|
2014-02-11 20:38:10 +01:00
|
|
|
$xactions = $editor->applyTransactions($mock, $xactions);
|
2012-11-22 02:27:44 +01:00
|
|
|
|
2012-11-22 02:23:28 +01:00
|
|
|
$mock->saveTransaction();
|
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())
|
|
|
|
->setURI('/M'.$mock->getID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->id) {
|
|
|
|
$submit = id(new AphrontFormSubmitControl())
|
|
|
|
->addCancelButton('/M'.$this->id)
|
|
|
|
->setValue(pht('Save'));
|
|
|
|
} else {
|
|
|
|
$submit = id(new AphrontFormSubmitControl())
|
|
|
|
->addCancelButton($this->getApplicationURI())
|
|
|
|
->setValue(pht('Create'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$policies = id(new PhabricatorPolicyQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->setObject($mock)
|
|
|
|
->execute();
|
|
|
|
|
2012-11-22 02:27:44 +01:00
|
|
|
// NOTE: Make this show up correctly on the rendered form.
|
|
|
|
$mock->setViewPolicy($v_view);
|
|
|
|
|
2013-09-11 21:27:28 +02:00
|
|
|
$handles = id(new PhabricatorHandleQuery())
|
2013-02-22 14:44:46 +01:00
|
|
|
->setViewer($user)
|
2013-09-11 21:27:28 +02:00
|
|
|
->withPHIDs($v_cc)
|
|
|
|
->execute();
|
2013-02-22 14:44:46 +01:00
|
|
|
|
2013-07-19 00:04:08 +02:00
|
|
|
$image_elements = array();
|
2014-02-11 20:38:10 +01:00
|
|
|
if ($posted_mock_images) {
|
|
|
|
$display_mock_images = $posted_mock_images;
|
|
|
|
} else {
|
|
|
|
$display_mock_images = $mock_images;
|
|
|
|
}
|
|
|
|
foreach ($display_mock_images as $mock_image) {
|
2013-07-19 00:04:08 +02:00
|
|
|
$image_elements[] = id(new PholioUploadedImageView())
|
|
|
|
->setUser($user)
|
2014-02-11 20:38:10 +01:00
|
|
|
->setImage($mock_image)
|
|
|
|
->setReplacesPHID($mock_image->getReplacesImagePHID());
|
2013-07-19 00:04:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$list_id = celerity_generate_unique_node_id();
|
|
|
|
$drop_id = celerity_generate_unique_node_id();
|
2013-08-12 21:08:54 +02:00
|
|
|
$order_id = celerity_generate_unique_node_id();
|
2013-07-19 00:04:08 +02:00
|
|
|
|
|
|
|
$list_control = phutil_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'id' => $list_id,
|
|
|
|
'class' => 'pholio-edit-list',
|
|
|
|
),
|
|
|
|
$image_elements);
|
|
|
|
|
|
|
|
$drop_control = phutil_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'id' => $drop_id,
|
|
|
|
'class' => 'pholio-edit-drop',
|
|
|
|
),
|
|
|
|
'Drag and drop images here to add them to the mock.');
|
|
|
|
|
2013-08-12 21:08:54 +02:00
|
|
|
$order_control = phutil_tag(
|
|
|
|
'input',
|
|
|
|
array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => 'imageOrder',
|
|
|
|
'id' => $order_id,
|
|
|
|
));
|
|
|
|
|
2013-07-19 00:04:08 +02:00
|
|
|
Javelin::initBehavior(
|
|
|
|
'pholio-mock-edit',
|
|
|
|
array(
|
|
|
|
'listID' => $list_id,
|
|
|
|
'dropID' => $drop_id,
|
2013-08-12 21:08:54 +02:00
|
|
|
'orderID' => $order_id,
|
2013-07-19 00:04:08 +02:00
|
|
|
'uploadURI' => '/file/dropupload/',
|
|
|
|
'renderURI' => $this->getApplicationURI('image/upload/'),
|
|
|
|
'pht' => array(
|
|
|
|
'uploading' => pht('Uploading Image...'),
|
|
|
|
'uploaded' => pht('Upload Complete...'),
|
2013-07-19 17:50:49 +02:00
|
|
|
'undo' => pht('Undo'),
|
|
|
|
'removed' => pht('This image will be removed from the mock.'),
|
2013-07-19 00:04:08 +02:00
|
|
|
),
|
|
|
|
));
|
2013-07-16 22:31:20 +02:00
|
|
|
|
2014-06-07 22:54:40 +02:00
|
|
|
if ($v_projects) {
|
|
|
|
$project_handles = $this->loadViewerHandles($v_projects);
|
|
|
|
} else {
|
|
|
|
$project_handles = array();
|
|
|
|
}
|
|
|
|
|
2013-07-16 22:31:20 +02:00
|
|
|
require_celerity_resource('pholio-edit-css');
|
2012-11-22 02:23:28 +01:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
2013-08-12 21:08:54 +02:00
|
|
|
->appendChild($order_control)
|
2012-11-22 02:23:28 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
2014-02-11 20:38:10 +01:00
|
|
|
->setName('name')
|
|
|
|
->setValue($v_name)
|
|
|
|
->setLabel(pht('Name'))
|
|
|
|
->setError($e_name))
|
2012-11-22 02:23:28 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new PhabricatorRemarkupControl())
|
2014-02-11 20:38:10 +01:00
|
|
|
->setName('description')
|
|
|
|
->setValue($v_desc)
|
|
|
|
->setLabel(pht('Description'))
|
|
|
|
->setUser($user))
|
2014-05-19 20:33:34 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSelectControl())
|
|
|
|
->setLabel(pht('Status'))
|
|
|
|
->setName('status')
|
|
|
|
->setValue($mock->getStatus())
|
|
|
|
->setOptions($mock->getStatuses()))
|
2014-06-07 22:54:40 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTokenizerControl())
|
|
|
|
->setLabel(pht('Projects'))
|
|
|
|
->setName('projects')
|
|
|
|
->setValue($project_handles)
|
|
|
|
->setDatasource('/typeahead/common/projects/'))
|
2013-02-22 14:44:46 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTokenizerControl())
|
2014-02-11 20:38:10 +01:00
|
|
|
->setLabel(pht('CC'))
|
|
|
|
->setName('cc')
|
|
|
|
->setValue($handles)
|
|
|
|
->setUser($user)
|
|
|
|
->setDatasource('/typeahead/common/mailable/'))
|
2012-11-22 02:23:28 +01:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormPolicyControl())
|
2014-02-11 20:38:10 +01:00
|
|
|
->setUser($user)
|
|
|
|
->setCapability(PhabricatorPolicyCapability::CAN_VIEW)
|
|
|
|
->setPolicyObject($mock)
|
|
|
|
->setPolicies($policies)
|
|
|
|
->setName('can_view'))
|
2013-07-19 00:04:08 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormMarkupControl())
|
2014-02-11 20:38:10 +01:00
|
|
|
->setValue($list_control))
|
2013-07-19 00:04:08 +02:00
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormMarkupControl())
|
2014-02-11 20:38:10 +01:00
|
|
|
->setValue($drop_control)
|
|
|
|
->setError($e_images))
|
2012-11-22 02:23:28 +01:00
|
|
|
->appendChild($submit);
|
|
|
|
|
2013-09-25 20:23:29 +02:00
|
|
|
$form_box = id(new PHUIObjectBoxView())
|
2013-08-26 20:53:11 +02:00
|
|
|
->setHeaderText($title)
|
2014-01-10 18:17:37 +01:00
|
|
|
->setFormErrors($errors)
|
2013-08-26 20:53:11 +02:00
|
|
|
->setForm($form);
|
|
|
|
|
2013-07-22 21:21:15 +02:00
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
2013-12-19 02:47:34 +01:00
|
|
|
$crumbs->addTextCrumb($title, $this->getApplicationURI());
|
2012-11-22 02:23:28 +01:00
|
|
|
|
|
|
|
$content = array(
|
2013-03-26 21:15:15 +01:00
|
|
|
$crumbs,
|
2013-08-26 20:53:11 +02:00
|
|
|
$form_box,
|
2012-11-22 02:23:28 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
2013-07-22 21:21:15 +02:00
|
|
|
$content,
|
2012-11-22 02:23:28 +01:00
|
|
|
array(
|
|
|
|
'title' => $title,
|
2013-02-15 19:17:31 +01:00
|
|
|
'device' => true,
|
2012-11-22 02:23:28 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|