Commit 05d11634 authored by Karamel's avatar Karamel
Browse files

Handle payment mode value image when editing a payment mode.

parent 079fde7e
......@@ -38,6 +38,21 @@ class PaymentmodeAPI extends APIRefHelper
$this->supportOrDie($data);
$arrayArgs = is_array($data);
$data = ($arrayArgs) ? $data : array($data);
// Keep track of PaymentModeValue images to restore them after
$images = [];
foreach ($data as $d) {
if ($d->getId() == null) {
// New payment mode, it cannot have an image.
continue;
}
$snapPm = $this->dao->readSnapshot(PaymentMode::class, $d->getId());
foreach ($snapPm->getValues() as $pmv) {
$images[$d->getId()] = [];
if ($pmv->hasImage()) {
$images[$d->getId()][] = $pmv->getValue();
}
}
}
// Delete previously registered return and values
$returnsHelper = new SubclassHelper($this->dao,
PaymentModeReturn::class, 'returns', 'paymentMode');
......@@ -45,6 +60,38 @@ class PaymentmodeAPI extends APIRefHelper
$valuesHelper = new SubclassHelper($this->dao,
PaymentModeValue::class, 'values', 'paymentMode');
$valuesHelper->deleteSubrecords($data);
/* Delete unlinked value images:
* remove from $images all those which still exists,
* all the remaining are orphaned images. */
foreach($images as $pmId => $values) {
foreach ($data as $d) {
if ($pmId == $d->getId()) {
foreach ($d->getValues() as $pmv) {
for($i = 0; $i < count($values); $i++) {
if ($pmv->getValue() == $values[$i]) {
if (!$pmv->hasImage()) {
$pmv->setHasImage(true);
}
array_splice($images[$pmId], $i, 1);
break;
}
}
}
}
}
}
$deleteImgIds = [];
foreach ($images as $pmId => $pmvs) {
foreach ($pmvs as $pmv) {
$deleteImgIds[] = ['model' => 'paymentmodevalue',
'modelId' => json_encode(['paymentMode' => $pmId, 'value' => $pmv])];
}
}
if (count($deleteImgIds) > 0) {
$imgApi = new ImageAPI(null, $this->dao);
$imgApi->delete($deleteImgIds);
}
// Write and commit PaymentModes
return parent::write(($arrayArgs) ? $data : $data[0]);
}
......
......@@ -21,7 +21,9 @@
// along with Pasteque. If not, see <http://www.gnu.org/licenses/>.
namespace Pasteque\Server;
use \Pasteque\Server\API\ImageAPI;
use \Pasteque\Server\API\PaymentmodeAPI;
use \Pasteque\Server\Model\Image;
use \Pasteque\Server\Model\PaymentMode;
use \Pasteque\Server\Model\PaymentModeReturn;
use \Pasteque\Server\Model\PaymentModeValue;
......@@ -38,10 +40,14 @@ class PaymentModeAPITest extends TestCase
protected function setUp() {
global $dbInfo;
$this->dao = DAOFactory::getDAO($dbInfo, ['debug' => true]);
$this->api = new PaymentmodeAPI($this->dao);
$this->api = new PaymentModeAPI($this->dao);
}
protected function tearDown() {
$imgs = $this->dao->search(Image::class);
foreach ($imgs as $img) {
$this->dao->delete($img);
}
$returns = $this->dao->search(PaymentModeReturn::class);
foreach ($returns as $record) {
$this->dao->delete($record);
......@@ -270,4 +276,65 @@ class PaymentModeAPITest extends TestCase
$this->assertEquals($pm2Id, $read->getReturns()->get(0)->getReturnMode()->getId());
$this->assertEquals(0.0, $read->getReturns()->get(0)->getMinAmount());
}
public function testPMVImageKeep() {
// Write an image for a payment mode value
// Update the payment mode but keep the value
// The image must still be there and linked
$pm = new PaymentMode();
$pm->setReference('mode');
$pm->setLabel('Mode');
$value = new PaymentModeValue();
$value->setValue(10.0);
$pm->addValue($value);
$this->api->write($pm);
$imgJsId = json_encode($value->getId());
$img = new Image();
$img->setImage(file_get_contents(__DIR__ . '/../res/image.png'));
$img->setMimeType('image/png');
$img->setModel(Image::MODEL_PAYMENTMODE_VALUE);
$img->setModelId($imgJsId);
$imgApi = new ImageAPI(null, $this->dao);
$imgApi->write($img);
// Update payment mode
$value2 = new PaymentModeValue();
$value2->setValue(5.0);
$pm->addValue($value2);
$this->api->write($pm);
$readImg = $this->dao->readSnapshot(Image::class,
['model' => Image::MODEL_PAYMENTMODE_VALUE, 'modelId' => $imgJsId]);
$this->assertNotNull($readImg);
$readValue = $this->dao->readSnapshot(PaymentModeValue::class, $value->getId());
$this->assertTrue($readValue->hasImage());
}
public function testPMVImageOrphaned() {
// Write an image for a payment mode value
// Update the payment mode with a new value
// The image must be deleted
$pm = new PaymentMode();
$pm->setReference('mode');
$pm->setLabel('Mode');
$value = new PaymentModeValue();
$value->setValue(10.0);
$pm->addValue($value);
$this->api->write($pm);
$imgJsId = json_encode($value->getId());
$img = new Image();
$img->setImage(file_get_contents(__DIR__ . '/../res/image.png'));
$img->setMimeType('image/png');
$img->setModel(Image::MODEL_PAYMENTMODE_VALUE);
$img->setModelId($imgJsId);
$imgApi = new ImageAPI(null, $this->dao);
$imgApi->write($img);
// Update payment mode
$pm->clearValues();
$value2 = new PaymentModeValue();
$value2->setValue(5.0);
$pm->addValue($value2);
$this->api->write($pm);
$readImg = $this->dao->readSnapshot(Image::class,
['model' => Image::MODEL_PAYMENTMODE_VALUE, 'modelId' => $imgJsId]);
$this->assertNull($readImg);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment