Changeset View
Changeset View
Standalone View
Standalone View
src/applications/pholio/xaction/PholioImageFileTransaction.php
<?php | <?php | ||||
final class PholioImageFileTransaction | final class PholioImageFileTransaction | ||||
extends PholioImageTransactionType { | extends PholioImageTransactionType { | ||||
const TRANSACTIONTYPE = 'image-file'; | const TRANSACTIONTYPE = 'image-file'; | ||||
public function generateOldValue($object) { | public function generateOldValue($object) { | ||||
$images = $object->getActiveImages(); | $images = $object->getActiveImages(); | ||||
return array_values(mpull($images, 'getPHID')); | return array_values(mpull($images, 'getPHID')); | ||||
} | } | ||||
public function generateNewValue($object, $value) { | public function generateNewValue($object, $value) { | ||||
$new_value = array(); | $editor = $this->getEditor(); | ||||
foreach ($value as $key => $images) { | |||||
$new_value[$key] = mpull($images, 'getPHID'); | $old_value = $this->getOldValue(); | ||||
} | $new_value = $value; | ||||
$old = array_fuse($this->getOldValue()); | |||||
return $this->getEditor()->getPHIDList($old, $new_value); | return $editor->getPHIDList($old_value, $new_value); | ||||
} | } | ||||
public function applyInternalEffects($object, $value) { | public function applyExternalEffects($object, $value) { | ||||
$old_map = array_fuse($this->getOldValue()); | $old_map = array_fuse($this->getOldValue()); | ||||
$new_map = array_fuse($this->getNewValue()); | $new_map = array_fuse($this->getNewValue()); | ||||
$obsolete_map = array_diff_key($old_map, $new_map); | $add_map = array_diff_key($new_map, $old_map); | ||||
$images = $object->getActiveImages(); | $rem_map = array_diff_key($old_map, $new_map); | ||||
foreach ($images as $seq => $image) { | |||||
if (isset($obsolete_map[$image->getPHID()])) { | $editor = $this->getEditor(); | ||||
$image->setIsObsolete(1); | |||||
$image->save(); | foreach ($rem_map as $phid) { | ||||
unset($images[$seq]); | $editor->loadPholioImage($object, $phid) | ||||
->setIsObsolete(1) | |||||
->save(); | |||||
} | } | ||||
foreach ($add_map as $phid) { | |||||
$editor->loadPholioImage($object, $phid) | |||||
->setMockPHID($object->getPHID()) | |||||
->save(); | |||||
} | } | ||||
$object->attachImages($images); | |||||
} | } | ||||
public function getTitle() { | public function getTitle() { | ||||
$old = $this->getOldValue(); | $old = $this->getOldValue(); | ||||
$new = $this->getNewValue(); | $new = $this->getNewValue(); | ||||
$add = array_diff($new, $old); | $add = array_diff($new, $old); | ||||
$rem = array_diff($old, $new); | $rem = array_diff($old, $new); | ||||
▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | public function getColor() { | ||||
} else if ($add) { | } else if ($add) { | ||||
return PhabricatorTransactions::COLOR_GREEN; | return PhabricatorTransactions::COLOR_GREEN; | ||||
} else { | } else { | ||||
return PhabricatorTransactions::COLOR_RED; | return PhabricatorTransactions::COLOR_RED; | ||||
} | } | ||||
} | } | ||||
public function extractFilePHIDs($object, $value) { | public function extractFilePHIDs($object, $value) { | ||||
$images = $this->getEditor()->getNewImages(); | $editor = $this->getEditor(); | ||||
$images = mpull($images, null, 'getPHID'); | |||||
// NOTE: This method is a little weird (and includes ALL the file PHIDs, | |||||
// including old file PHIDs) because we currently don't have a storage | |||||
// object when called. This might change at some point. | |||||
$new_phids = $value; | |||||
$file_phids = array(); | $file_phids = array(); | ||||
foreach ($value as $image_phid) { | foreach ($new_phids as $phid) { | ||||
$image = idx($images, $image_phid); | $file_phids[] = $editor->loadPholioImage($object, $phid) | ||||
if (!$image) { | ->getFilePHID(); | ||||
continue; | |||||
} | |||||
$file_phids[] = $image->getFilePHID(); | |||||
} | } | ||||
return $file_phids; | return $file_phids; | ||||
} | } | ||||
public function mergeTransactions( | public function mergeTransactions( | ||||
$object, | $object, | ||||
PhabricatorApplicationTransaction $u, | PhabricatorApplicationTransaction $u, | ||||
PhabricatorApplicationTransaction $v) { | PhabricatorApplicationTransaction $v) { | ||||
return $this->getEditor()->mergePHIDOrEdgeTransactions($u, $v); | return $this->getEditor()->mergePHIDOrEdgeTransactions($u, $v); | ||||
} | } | ||||
} | } |