Remove product literal strings in "pht()", part 5
[phabricator.git] / src / applications / files / document / PhabricatorImageDocumentEngine.php
bloba20758ede101f6594f03e30133f7ace009817612
1 <?php
3 final class PhabricatorImageDocumentEngine
4 extends PhabricatorDocumentEngine {
6 const ENGINEKEY = 'image';
8 public function getViewAsLabel(PhabricatorDocumentRef $ref) {
9 return pht('View as Image');
12 protected function getDocumentIconIcon(PhabricatorDocumentRef $ref) {
13 return 'fa-file-image-o';
16 protected function getByteLengthLimit() {
17 return (1024 * 1024 * 64);
20 public function canDiffDocuments(
21 PhabricatorDocumentRef $uref = null,
22 PhabricatorDocumentRef $vref = null) {
24 // For now, we can only render a rich image diff if the documents have
25 // their data stored in Files already.
27 if ($uref && !$uref->getFile()) {
28 return false;
31 if ($vref && !$vref->getFile()) {
32 return false;
35 return true;
38 public function newEngineBlocks(
39 PhabricatorDocumentRef $uref = null,
40 PhabricatorDocumentRef $vref = null) {
42 if ($uref) {
43 $u_blocks = $this->newDiffBlocks($uref);
44 } else {
45 $u_blocks = array();
48 if ($vref) {
49 $v_blocks = $this->newDiffBlocks($vref);
50 } else {
51 $v_blocks = array();
54 return id(new PhabricatorDocumentEngineBlocks())
55 ->addBlockList($uref, $u_blocks)
56 ->addBlockList($vref, $v_blocks);
59 public function newBlockDiffViews(
60 PhabricatorDocumentRef $uref,
61 PhabricatorDocumentEngineBlock $ublock,
62 PhabricatorDocumentRef $vref,
63 PhabricatorDocumentEngineBlock $vblock) {
65 $u_content = $this->newBlockContentView($uref, $ublock);
66 $v_content = $this->newBlockContentView($vref, $vblock);
68 return id(new PhabricatorDocumentEngineBlockDiff())
69 ->setOldContent($u_content)
70 ->addOldClass('diff-image-cell')
71 ->setNewContent($v_content)
72 ->addNewClass('diff-image-cell');
76 private function newDiffBlocks(PhabricatorDocumentRef $ref) {
77 $blocks = array();
79 $file = $ref->getFile();
81 $image_view = phutil_tag(
82 'div',
83 array(
84 'class' => 'differential-image-stage',
86 phutil_tag(
87 'img',
88 array(
89 'alt' => $file->getAltText(),
90 'src' => $file->getBestURI(),
91 )));
93 $hash = $file->getContentHash();
95 $blocks[] = id(new PhabricatorDocumentEngineBlock())
96 ->setBlockKey('1')
97 ->setDifferenceHash($hash)
98 ->setContent($image_view);
100 return $blocks;
103 protected function canRenderDocumentType(PhabricatorDocumentRef $ref) {
104 $file = $ref->getFile();
105 if ($file) {
106 return $file->isViewableImage();
109 $viewable_types = PhabricatorEnv::getEnvConfig('files.viewable-mime-types');
110 $viewable_types = array_keys($viewable_types);
112 $image_types = PhabricatorEnv::getEnvConfig('files.image-mime-types');
113 $image_types = array_keys($image_types);
115 return
116 $ref->hasAnyMimeType($viewable_types) &&
117 $ref->hasAnyMimeType($image_types);
120 protected function newDocumentContent(PhabricatorDocumentRef $ref) {
121 $file = $ref->getFile();
122 if ($file) {
123 $source_uri = $file->getViewURI();
124 } else {
125 // We could use a "data:" URI here. It's not yet clear if or when we'll
126 // have a ref but no backing file.
127 throw new PhutilMethodNotImplementedException();
130 $image = phutil_tag(
131 'img',
132 array(
133 'alt' => $file->getAltText(),
134 'src' => $source_uri,
137 $linked_image = phutil_tag(
138 'a',
139 array(
140 'href' => $source_uri,
141 'rel' => 'noreferrer',
143 $image);
145 $container = phutil_tag(
146 'div',
147 array(
148 'class' => 'document-engine-image',
150 $linked_image);
152 return $container;