name = 'massupdate';
$this->tab = 'Products';
$this->version = '1.1';
parent::__construct();
/* The parent construct is required for translations */
$this->page = basename(__FILE__, '.php');
$this->displayName = $this->l('Mass Update');
$this->description = $this->l('Perform mass updates on all your products (price, weight, and features)');
$this->confirmUninstall = $this->l('Aww... are you sure you want to delete me?');
}
function install($try_again=true)
{
//if (!$this->addMassUpdateHook() || !parent::install() || !$this->registerHook('shippingCalculate') || !$this->registerHook('shippingCalculateDays')){
if (!parent::install()){
return false;
}
return (Configuration::updateValue('PS_MASSUPDATE_TITLE', array('1' => 'Mass Update')) );
}
function uninstall()
{
if (parent::uninstall() == false)
return false;
return (Configuration::deleteByName('PS_MASSUPDATE_TITLE'));
}
function getConfig()
{
$currencies = Currency::getCurrencies();
$default_currency = Configuration::get('PS_CURRENCY_DEFAULT');
foreach ($currencies as $currency) {
if ($currency['id_currency'] == $default_currency)
$sign = $currency['sign'];
}
$weight_units = strtolower(Configuration::get('PS_WEIGHT_UNIT'));
$main_product_fields = array(
"price"=>array(
"db_field"=>"price",
"friendly"=>$this->l('Price'),
"prefix"=> '(' . $sign . ')',
),
"weight"=>array(
"db_field"=>"weight",
"friendly"=>$this->l('Weight') . " ($weight_units)",
),
"quantity"=>array(
"db_field"=>"quantity",
"friendly"=>$this->l('Quantity'),
),
//add more, eg: quantity, here.
);
return $main_product_fields;
}
function updateProducts()
{
$language_id = Configuration::get('PS_LANG_DEFAULT');
$product_settings = $_POST['mup'];
if(!is_array($product_settings)){
return false;
}
$main_fields = $this->getConfig();
foreach($product_settings as $product_id => $data){
$product_id = (int)$product_id;
if(!$product_id)continue;
$sql = "UPDATE "._DB_PREFIX_."product SET date_upd = NOW() ";
$do_update = false;
// we're updating the product id. check and update the main fields.
foreach($main_fields as $field){
$update_value = trim(pSQL($data[$field['db_field']]));
if($update_value){
// we've found a main field to update!
$do_update = true;
$sql .= ", `".$field['db_field']."` = '$update_value' ";
}
}
if($do_update){
$sql .= " WHERE `id_product` = '$product_id' LIMIT 1";
//echo $sql ."
\n";
if(!Db::getInstance()->Execute($sql)){
// yer yer i know - dodgey - but this should never happen, all input is sanatised.
// a "just in case" so we dont go and bork all our products if there's a coding error.
echo "FAILED TO UPDATE: $sql ";
echo mysql_error();
exit;
}
}
// now we have to check the product features! trickeyness..
// we grab a list of their current features, if any...
$product_features = Product::getFeaturesStatic($product_id);
//print_r($product_features);
$new_features = array();
$do_feature_update = false;
if(isset($data['ff']) && is_array($data['ff'])){
foreach($data['ff'] as $feature_id => $feature_value){
$update_value = trim(pSQL($feature_value));
if($update_value){
//YEY! we can update this products feature value...
$do_feature_update = true;
$new_features[$feature_id] = $update_value;
}
}
}
//print_r($new_features);
if($do_feature_update){
$product = new Product($product_id);
$product->deleteFeatures();
foreach($new_features as $feature_id => $feature_value){
// add our new custom feature:
//echo "Adding $feature_id as $feature_value
\n";
$id_value = $product->addFeaturesToDB($feature_id, 0, true, $language_id);
$product->addFeaturesCustomToDB($id_value, $language_id, $feature_value);
}
}
}
return true;
}
function getContent()
{
$language_id = Configuration::get('PS_LANG_DEFAULT');
$this->_html = '