Give Phame blogs mutable interact policies
[phabricator.git] / src / applications / phame / controller / blog / PhameBlogHeaderPictureController.php
blob0106b9571d40d06891cf32f83d9d76e9fbe8eb97
1 <?php
3 final class PhameBlogHeaderPictureController
4 extends PhameBlogController {
6 public function handleRequest(AphrontRequest $request) {
7 $viewer = $request->getViewer();
8 $id = $request->getURIData('id');
10 $blog = id(new PhameBlogQuery())
11 ->setViewer($viewer)
12 ->withIDs(array($id))
13 ->needHeaderImage(true)
14 ->requireCapabilities(
15 array(
16 PhabricatorPolicyCapability::CAN_VIEW,
17 PhabricatorPolicyCapability::CAN_EDIT,
19 ->executeOne();
20 if (!$blog) {
21 return new Aphront404Response();
24 $blog_uri = '/phame/blog/manage/'.$id;
26 $supported_formats = PhabricatorFile::getTransformableImageFormats();
27 $e_file = true;
28 $errors = array();
29 $delete_header = ($request->getInt('delete') == 1);
31 if ($request->isFormPost()) {
32 if ($request->getFileExists('header')) {
33 $file = PhabricatorFile::newFromPHPUpload(
34 $_FILES['header'],
35 array(
36 'authorPHID' => $viewer->getPHID(),
37 'canCDN' => true,
38 ));
39 } else if (!$delete_header) {
40 $e_file = pht('Required');
41 $errors[] = pht(
42 'You must choose a file when uploading a new blog header.');
45 if (!$errors && !$delete_header) {
46 if (!$file->isTransformableImage()) {
47 $e_file = pht('Not Supported');
48 $errors[] = pht(
49 'This server only supports these image formats: %s.',
50 implode(', ', $supported_formats));
54 if (!$errors) {
55 if ($delete_header) {
56 $new_value = null;
57 } else {
58 $file->attachToObject($blog->getPHID());
59 $new_value = $file->getPHID();
62 $xactions = array();
63 $xactions[] = id(new PhameBlogTransaction())
64 ->setTransactionType(PhameBlogHeaderImageTransaction::TRANSACTIONTYPE)
65 ->setNewValue($new_value);
67 $editor = id(new PhameBlogEditor())
68 ->setActor($viewer)
69 ->setContentSourceFromRequest($request)
70 ->setContinueOnMissingFields(true)
71 ->setContinueOnNoEffect(true);
73 $editor->applyTransactions($blog, $xactions);
75 return id(new AphrontRedirectResponse())->setURI($blog_uri);
79 $title = pht('Edit Blog Header');
81 $upload_form = id(new AphrontFormView())
82 ->setUser($viewer)
83 ->setEncType('multipart/form-data')
84 ->appendChild(
85 id(new AphrontFormFileControl())
86 ->setName('header')
87 ->setLabel(pht('Upload Header'))
88 ->setError($e_file)
89 ->setCaption(
90 pht('Supported formats: %s', implode(', ', $supported_formats))))
91 ->appendChild(
92 id(new AphrontFormCheckboxControl())
93 ->setName('delete')
94 ->setLabel(pht('Delete Header'))
95 ->addCheckbox(
96 'delete',
98 null,
99 null))
100 ->appendChild(
101 id(new AphrontFormSubmitControl())
102 ->addCancelButton($blog_uri)
103 ->setValue(pht('Upload Header')));
105 $upload_box = id(new PHUIObjectBoxView())
106 ->setHeaderText(pht('Upload New Header'))
107 ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY)
108 ->setForm($upload_form);
110 $crumbs = $this->buildApplicationCrumbs();
111 $crumbs->addTextCrumb(
112 pht('Blogs'),
113 $this->getApplicationURI('blog/'));
114 $crumbs->addTextCrumb(
115 $blog->getName(),
116 $this->getApplicationURI('blog/view/'.$id));
117 $crumbs->addTextCrumb(pht('Blog Header'));
118 $crumbs->setBorder(true);
120 $header = id(new PHUIHeaderView())
121 ->setHeader(pht('Edit Blog Header'))
122 ->setHeaderIcon('fa-camera');
124 $view = id(new PHUITwoColumnView())
125 ->setHeader($header)
126 ->setFooter(array(
127 $upload_box,
130 return $this->newPage()
131 ->setTitle($title)
132 ->setCrumbs($crumbs)
133 ->appendChild(
134 array(
135 $view,