Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly import image positions in media_gallery #463

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,30 @@ public function resetGallery($pid, $storeid, $attid)
{
$tgv = $this->tablename('catalog_product_entity_media_gallery_value');
$tg = $this->tablename('catalog_product_entity_media_gallery');
$sql = "DELETE emgv,emg FROM `$tgv` as emgv
$sql = "DELETE emgv FROM `$tgv` as emgv
JOIN `$tg` AS emg ON emgv.value_id = emg.value_id AND emgv.store_id=?
WHERE emg.entity_id=? AND emg.attribute_id=?";
$this->delete($sql, array($storeid, $pid, $attid));
}

/**
* Removes images from gallery
* without a corresponding value
* in the current store.
*
* @param int $pid
* : product id
*/
public function removeImagesWithoutValues($pid, $storeid, $attid)
{
$tgv = $this->tablename('catalog_product_entity_media_gallery_value');
$tg = $this->tablename('catalog_product_entity_media_gallery');
$sql = "DELETE emg FROM `$tg` as emg
LEFT JOIN `$tgv` AS emgv ON emgv.value_id = emg.value_id AND emgv.store_id=?
WHERE emg.entity_id=? AND emg.attribute_id=? AND emgv.value_id IS NULL";
$this->delete($sql, array($storeid, $pid, $attid));
}

/**
* adds an image to product image gallery only if not already exists
*
Expand All @@ -290,29 +308,27 @@ public function resetGallery($pid, $storeid, $attid)
* @param string $imgname
* : image file name (relative to /products/media in magento dir)
*/
public function addImageToGallery($pid, $storeid, $attrdesc, $imgname, $targetsids, $imglabel = null, $excluded = false,
$refid = null)
public function addImageToGallery($pid, $storeid, $attrdesc, $imgname, $targetsids, $imglabel = null, $excluded = false, $refid = null)
{
$gal_attinfo = $this->getAttrInfo("media_gallery");
$tg = $this->tablename('catalog_product_entity_media_gallery');
$tgv = $this->tablename('catalog_product_entity_media_gallery_value');
$vid = $this->getImageId($pid, $gal_attinfo["attribute_id"], $imgname, $refid, $storeid);
$vid = $this->getImageId($pid, $gal_attinfo["attribute_id"], $imgname, $refid);
if ($vid != null) {

// et maximum current position in the product gallery
$sql = "SELECT MAX( position ) as maxpos
FROM $tgv AS emgv
JOIN $tg AS emg ON emg.value_id = emgv.value_id AND emg.entity_id = ?
WHERE emgv.store_id=?
GROUP BY emg.entity_id";
$pos = $this->selectone($sql, array($pid, $storeid), 'maxpos');
$pos = ($pos == null ? 0 : $pos + 1);
// nsert new value (ingnore duplicates)
// Insert new value (ingnore duplicates)

$vinserts = array();
$data = array();

foreach ($targetsids as $tsid) {
// Get maximum current position in the product gallery
$sql = "SELECT MAX( position ) as maxpos
FROM $tgv AS emgv
JOIN $tg AS emg ON emg.value_id = emgv.value_id AND emg.entity_id = ?
WHERE emgv.store_id=?
GROUP BY emg.entity_id";
$pos = $this->selectone($sql, array($pid, $tsid), 'maxpos');
$pos = ($pos == null ? 0 : $pos + 1);
$vinserts[] = "(?,?,?,?," . ($imglabel == null ? "NULL" : "?") . ")";
$data = array_merge($data, array($vid, $tsid, $pos, $excluded ? 1 : 0));
if ($imglabel != null) {
Expand Down Expand Up @@ -432,10 +448,10 @@ public function copyImageFile($imgfile, &$item, $extra)
//handle amazon specific
if (is_remote_path($imgfile)) {
// Amazon images patch , remove SLXXXX part
if (preg_match('|amazon\..*?/images/I|', $imgfile)) {
$pattern = '/\bSL[0-9]+\./i';
$imgfile = preg_replace($pattern, '', $imgfile);
}
if (preg_match('|amazon\..*?/images/I|', $imgfile)) {
$pattern = '/\bSL[0-9]+\./i';
$imgfile = preg_replace($pattern, '', $imgfile);
}
}

$source = $this->findImageFile($imgfile);
Expand Down Expand Up @@ -566,8 +582,7 @@ public function processItemAfterId(&$item, $params = null)
if (isset($item[$attrcode . "_label"]) && !isset($item[$attrcode])) {
// force label update
$attrdesc = $this->getAttrInfo($attrcode);
$this->updateLabel($attrdesc, $pid, $this->getItemStoreIds($item, $attrdesc["is_global"]),
$item[$attrcode . "_label"]);
$this->updateLabel($attrdesc, $pid, $this->getItemStoreIds($item, $attrdesc["is_global"]), $item[$attrcode . "_label"]);
unset($attrdesc);
}
}
Expand All @@ -577,7 +592,7 @@ public function processItemAfterId(&$item, $params = null)

if ((isset($item["media_gallery"]) && $galreset) || $forcereset) {
$gattrdesc = $this->getAttrInfo("media_gallery");
$sids = $this->getItemStoreIds($item, $gattrdesc["is_global"]);
$sids = $this->getItemStoreIds($item);
foreach ($sids as $sid) {
$this->resetGallery($pid, $sid, $gattrdesc["attribute_id"]);
}
Expand All @@ -588,10 +603,31 @@ public function processItemAfterId(&$item, $params = null)
return true;
}

public function processItemAfterImport(&$item, $params = null)
{
if (!$this->_active) {
return true;
}
$this->_newitem = $params["new"];
$pid = $params["product_id"];
// Reset media_gallery
$galreset = !(isset($item["media_gallery_reset"])) || $item["media_gallery_reset"] == 1;
$forcereset = (isset($item["media_gallery_reset"])) && $item["media_gallery_reset"] == 1;

if ((isset($item["media_gallery"]) && $galreset) || $forcereset) {
$gattrdesc = $this->getAttrInfo("media_gallery");
$sids = $this->getItemStoreIds($item);
foreach ($sids as $sid) {
$this->removeImagesWithoutValues($pid, $sid, $gattrdesc["attribute_id"]);
}
}
return true;
}

public function processColumnList(&$cols, $params = null)
{
// automatically add modified attributes if not found in datasource

// automatically add media_gallery for attributes to handle
$imgattrs = array_intersect(array_merge($this->_img_baseattrs, array('media_gallery', 'image_remove')), $cols);
if (count($imgattrs) > 0) {
Expand Down