Example #1
0
    <?php 
} else {
    midcom::get()->disable_limits();
    // Import product images
    $qb = org_openpsa_products_product_dba::new_query_builder();
    $qb->add_constraint('code', '<>', '');
    $products = $qb->execute();
    $schemadb = midcom_baseclasses_components_configuration::get('org.openpsa.products', 'config')->get('schemadb_product');
    $schema = midcom_helper_datamanager2_schema::load_database($schema);
    $datamanager = new midcom_helper_datamanager2_datamanager($schema);
    foreach ($products as $product) {
        // Get old image
        $image = file_get_contents(str_replace('__PRODUCT_CODE__', $product->code, $_POST['address']));
        if (empty($image)) {
            continue;
        }
        // Save image to a temp file
        $tmp_name = tempnam($GLOBALS['midcom_config']['midcom_tempdir'], 'org_openpsa_products_product_oldimage_');
        $fp = fopen($tmp_name, 'w');
        if (!fwrite($fp, $image)) {
            //Could not write, clean up and continue
            echo "Error when writing file {$tmp_name}";
            fclose($fp);
            continue;
        }
        fclose($fp);
        $datamanager->autoset_storage($product);
        $datamanager->types['image']->set_image("{$product->code}.jpg", $tmp_name, $product->title);
        $datamanager->save();
    }
}