/**
* Import files from selected folder
*
* @param string $strPath
*/
protected function importFromPath($strPath)
{
$arrFiles = scan(TL_ROOT . '/' . $strPath);
if (empty($arrFiles)) {
Message::addError($GLOBALS['TL_LANG']['MSC']['noFilesInFolder']);
Controller::reload();
}
$blnEmpty = true;
$arrDelete = array();
$objProducts = \Database::getInstance()->prepare("SELECT * FROM tl_iso_product WHERE pid=0")->execute();
while ($objProducts->next()) {
$arrImageNames = array();
$arrImages = deserialize($objProducts->images);
if (!is_array($arrImages)) {
$arrImages = array();
} else {
foreach ($arrImages as $row) {
if ($row['src']) {
$arrImageNames[] = $row['src'];
}
}
}
$arrPattern = array();
$arrPattern[] = $objProducts->alias ? standardize($objProducts->alias) : null;
$arrPattern[] = $objProducts->sku ? $objProducts->sku : null;
$arrPattern[] = $objProducts->sku ? standardize($objProducts->sku) : null;
$arrPattern[] = !empty($arrImageNames) ? implode('|', $arrImageNames) : null;
// !HOOK: add custom import regex patterns
if (isset($GLOBALS['ISO_HOOKS']['addAssetImportRegexp']) && is_array($GLOBALS['ISO_HOOKS']['addAssetImportRegexp'])) {
foreach ($GLOBALS['ISO_HOOKS']['addAssetImportRegexp'] as $callback) {
$objCallback = System::importStatic($callback[0]);
$arrPattern = $objCallback->{$callback}[1]($arrPattern, $objProducts);
}
}
$strPattern = '@^(' . implode('|', array_filter($arrPattern)) . ')@i';
$arrMatches = preg_grep($strPattern, $arrFiles);
if (!empty($arrMatches)) {
$arrNewImages = array();
foreach ($arrMatches as $file) {
if (is_dir(TL_ROOT . '/' . $strPath . '/' . $file)) {
$arrSubfiles = scan(TL_ROOT . '/' . $strPath . '/' . $file);
if (!empty($arrSubfiles)) {
foreach ($arrSubfiles as $subfile) {
if (is_file($strPath . '/' . $file . '/' . $subfile)) {
$objFile = new File($strPath . '/' . $file . '/' . $subfile);
if ($objFile->isGdImage) {
$arrNewImages[] = $strPath . '/' . $file . '/' . $subfile;
}
}
}
}
} elseif (is_file(TL_ROOT . '/' . $strPath . '/' . $file)) {
$objFile = new \File($strPath . '/' . $file);
if ($objFile->isGdImage) {
$arrNewImages[] = $strPath . '/' . $file;
}
}
}
if (!empty($arrNewImages)) {
foreach ($arrNewImages as $strFile) {
$pathinfo = pathinfo(TL_ROOT . '/' . $strFile);
// Will recursively create the folder
$objFolder = new \Folder('isotope/' . strtolower(substr($pathinfo['filename'], 0, 1)));
$strCacheName = $pathinfo['filename'] . '-' . substr(md5_file(TL_ROOT . '/' . $strFile), 0, 8) . '.' . $pathinfo['extension'];
\Files::getInstance()->copy($strFile, $objFolder->path . '/' . $strCacheName);
$arrImages[] = array('src' => $strCacheName);
$arrDelete[] = $strFile;
Message::addConfirmation(sprintf($GLOBALS['TL_LANG']['MSC']['assetImportConfirmation'], $pathinfo['filename'] . '.' . $pathinfo['extension'], $objProducts->name));
$blnEmpty = false;
}
\Database::getInstance()->prepare("UPDATE tl_iso_product SET images=? WHERE id=?")->execute(serialize($arrImages), $objProducts->id);
}
}
}
if (!empty($arrDelete)) {
$arrDelete = array_unique($arrDelete);
foreach ($arrDelete as $file) {
\Files::getInstance()->delete($file);
}
}
if ($blnEmpty) {
\Message::addInfo($GLOBALS['TL_LANG']['MSC']['assetImportNoFilesFound']);
}
\Controller::reload();
}