Last active
January 30, 2016 14:06
-
-
Save dannevang/bdb83a08c395e960cb44 to your computer and use it in GitHub Desktop.
Import documents based on CSV into Extensionsmalls Product File Attachment extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sku | store_id | file | label | description | position | only_registred_customers | |
---|---|---|---|---|---|---|---|
test | 0 | http://server.com/link-tofile.pdf | A Name | A description | 0 | 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Product Documents Importer</title> | |
</head> | |
<body> | |
<h1 style="text-align: center; padding: 5%; color: #808080;">Custom Documents Importer</h1> | |
</br></br></br></br> | |
<?php | |
$config = simplexml_load_file(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'etc' . DIRECTORY_SEPARATOR . 'local.xml', 'SimpleXMLElement', LIBXML_NOCDATA); | |
$link = mysqli_connect($config->global->resources->default_setup->connection->host, $config->global->resources->default_setup->connection->username, $config->global->resources->default_setup->connection->password, $config->global->resources->default_setup->connection->dbname); | |
set_time_limit(0); | |
ini_set('memory_limit', '1024M'); | |
umask(0); | |
if (isset($_GET['import'])) { | |
$import = new Import(); | |
$update = $import->Importdocument($link); | |
} | |
class Import | |
{ | |
function make_path($pathname, $is_filename = false) | |
{ | |
if ($is_filename) { | |
$pathname = substr($pathname, 0, strrpos($pathname, '/')); | |
} | |
// Check if directory already exists | |
if (is_dir($pathname) || empty($pathname)) { | |
return true; | |
} | |
// Ensure a file does not already exist with the same name | |
$pathname = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $pathname); | |
if (is_file($pathname)) { | |
trigger_error('mkdirr() File exists', E_USER_WARNING); | |
return false; | |
} | |
// Crawl up the directory tree | |
$mode = 0775; | |
$next_pathname = substr($pathname, 0, strrpos($pathname, DIRECTORY_SEPARATOR)); | |
if ($this->make_path($next_pathname, $mode)) { | |
if (!file_exists($pathname)) { | |
return mkdir($pathname, $mode); | |
} | |
} | |
return false; | |
} | |
public function fileExistOnPage ($target, $filenamedb, $link) { | |
$query = "SELECT 1 FROM `catalog_product_documents` WHERE `product_id`='$target' AND `file`='$filenamedb' LIMIT 1"; | |
$query = mysqli_query($link, $query); | |
$query = mysqli_num_rows($query); | |
return $query; | |
} | |
public function Importdocument($link) | |
{ | |
$no = 0; | |
$counter = 0; | |
$imported = 0; | |
$source = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'documents.csv'; | |
if (($handle = fopen($source, "r")) !== FALSE) { | |
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) { | |
if (!isset($data[6]) || $data[6] == '') { | |
$data[6] = 0; | |
} | |
if ($data[1] == '') { | |
$data[1] = 0; | |
} | |
$stringName = substr($data[2], strripos($data[2], '/') + 1); | |
if ($data[0] !== 'sku' && $data[0] !== '') { | |
$counter++; | |
$querygetid = "SELECT `entity_id`,`type_id` FROM `catalog_product_entity` WHERE `sku` LIKE '$data[0]'"; | |
$dbqueryid = mysqli_query($link, $querygetid); | |
$productid = mysqli_fetch_array($dbqueryid); | |
$target = $productid['entity_id']; | |
if ($target) | |
{ | |
//If this is a simple product check visibility | |
if ($productid['type_id'] == 'simple') { | |
$visiQuery = "SELECT `value` FROM `catalog_product_entity_int` WHERE `entity_id`='$target' AND `attribute_id`=102"; | |
$visiQuery = mysqli_query($link, $visiQuery); | |
$visiQuery = mysqli_fetch_array($visiQuery); | |
} | |
//If visibility == not individually visible get parent ID | |
if ($visiQuery['value'] == 1) { | |
$parent = "SELECT `parent_id` FROM `catalog_product_super_link` WHERE `product_id`='$target'"; | |
$parent = mysqli_query($link, $parent); | |
$parent = mysqli_fetch_array($parent); | |
//Only assign parent if one exist | |
if ($parent['parent_id']) { | |
$target = $parent['parent_id']; | |
} | |
} | |
$copyfrom = $data[2]; | |
$copyto = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'product_documents' . DIRECTORY_SEPARATOR . $stringName[0] . DIRECTORY_SEPARATOR . $stringName[1] . DIRECTORY_SEPARATOR . $stringName; | |
$this->make_path(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR . 'product_documents' . DIRECTORY_SEPARATOR . $stringName[0] . DIRECTORY_SEPARATOR . $stringName[1] . DIRECTORY_SEPARATOR, false); | |
//Don't import the file if it already exist | |
if (!is_file($copyto)) { | |
$copyno = copy($copyfrom, $copyto); | |
$imported++; | |
} else { | |
$copyno = 1; | |
} | |
$filenamedb = '/' . $stringName[0] . '/' . $stringName[1] . '/' . $stringName; | |
$filenamedb = str_replace('DS', "/", $filenamedb); | |
//If this target already has the same file we skip it | |
if (!$this->fileExistOnPage($target,$filenamedb, $link)) | |
{ | |
$filesize = filesize($copyto); | |
//Insert to db | |
if ($copyno == 1) { | |
$queryinsert = "INSERT INTO `catalog_product_documents` (`product_id`, `store_id`, `file`, `size`, `label`, `description`, `position`, `only_registred_customers`, `created_time`, `update_time`) VALUES ('$target', '$data[1]', '$filenamedb', '$filesize', '$data[3]', '$data[4]', '$data[5]', '$data[6]', (CURRENT_TIMESTAMP), (CURRENT_TIMESTAMP))"; | |
$dbquery = mysqli_query($link, $queryinsert); | |
$no = $no + $copyno; | |
} | |
} | |
} | |
} | |
} | |
fclose($handle); | |
} | |
$result = array ($no, $counter, $imported); | |
return $result; | |
} | |
} | |
?> | |
<form style="text-align: center;"> | |
<input type="hidden" name="import" value="1"</input> | |
<input type="submit" value="Run Update"> | |
</form> | |
<?php | |
if (isset($update)) { | |
echo '<h2 style="color: #008000; text-align: center;">You imported ' . $update[2] . ' documents and referenced them to '. $update[0] .' products from ' . $update[1] .' rows in the file</h2>'; | |
} | |
elseif (isset($_GET['import']) && !isset($update)){ | |
echo '<h2 style="color: #FF0000; text-align: center;">Something went wrong!</h2>'; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment