Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 7.77 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. /**
  4.  * Author: David T Baker
  5.  * Web: http://dtbaker.com.au
  6.  * Email: dtbaker@gmail.com
  7.  * Created: 2009-02-26
  8.  *
  9.  * File: massupdate.php
  10.  * Provides:
  11.  *  Ability to perform mass updates on products..
  12.  *
  13.  */
  14. class MassUpdate extends Module
  15. {
  16.         /* @var boolean error */
  17.         protected $error = false;
  18.        
  19.        
  20.         function __construct()
  21.         {
  22.                 $this->name = 'massupdate';
  23.                 $this->tab = 'Products';
  24.                 $this->version = '1.0';
  25.  
  26.                 parent::__construct();
  27.  
  28.                 /* The parent construct is required for translations */
  29.                 $this->page = basename(__FILE__, '.php');
  30.         $this->displayName = $this->l('Mass Update');
  31.         $this->description = $this->l('Perform mass updates on all your products (price, weight, and features)');
  32.                 $this->confirmUninstall = $this->l('Aww... are you sure you want to delete me?');
  33.         }
  34.        
  35.         function install($try_again=true)
  36.         {
  37.                 //if (!$this->addMassUpdateHook() || !parent::install() || !$this->registerHook('shippingCalculate') || !$this->registerHook('shippingCalculateDays')){
  38.                 if (!parent::install()){
  39.                         return false;
  40.                 }
  41.                
  42.                 return (Configuration::updateValue('PS_MASSUPDATE_TITLE', array('1' => 'Mass Update')) );
  43.         }
  44.        
  45.         function uninstall()
  46.         {
  47.                 if (parent::uninstall() == false)
  48.                         return false;
  49.                
  50.                        
  51.                 return (Configuration::deleteByName('PS_MASSUPDATE_TITLE'));
  52.         }
  53.        
  54.  
  55.         function getConfig()
  56.         {
  57.                
  58.                 $weight_units = strtolower(Configuration::get('PS_WEIGHT_UNIT'));
  59.                
  60.                 $main_product_fields = array(
  61.                         "price"=>array(
  62.                                 "db_field"=>"price",
  63.                                 "friendly"=>"Price",
  64.                                 "prefix"=>'$',
  65.                         ),
  66.                         "weight"=>array(
  67.                                 "db_field"=>"weight",
  68.                                 "friendly"=>"Weight ($weight_units)",
  69.                         ),
  70.                         "quantity"=>array(
  71.                                 "db_field"=>"quantity",
  72.                                 "friendly"=>"Quantity",
  73.                         ),
  74.                         //add more, eg: quantity, here.
  75.                 );
  76.                 return $main_product_fields;
  77.         }
  78.        
  79.        
  80.        
  81.         function updateProducts()
  82.         {
  83.                 $product_settings = $_POST['mup'];
  84.                
  85.                 if(!is_array($product_settings)){
  86.                         return false;
  87.                 }
  88.                
  89.                 $main_fields = $this->getConfig();
  90.                
  91.                 foreach($product_settings as $product_id => $data){
  92.                         $product_id = (int)$product_id;
  93.                         if(!$product_id)continue;
  94.                         $sql = "UPDATE "._DB_PREFIX_."product SET date_upd = NOW() ";
  95.                         $do_update = false;
  96.                         // we're updating the product id. check and update the main fields.
  97.                         foreach($main_fields as $field){
  98.                                 $update_value = trim(pSQL($data[$field['db_field']]));
  99.                                 if($update_value){
  100.                                         // we've found a main field to update!
  101.                                         $do_update = true;
  102.                                         $sql .= ", `".$field['db_field']."` = '$update_value' ";
  103.                                 }
  104.                         }
  105.                         if($do_update){
  106.                                 $sql .= " WHERE `id_product` = '$product_id' LIMIT 1";
  107.                                 //echo $sql ." <br>\n";
  108.                                 if(!Db::getInstance()->Execute($sql)){
  109.                                         // yer yer i know - dodgey - but this should never happen, all input is sanatised.
  110.                                         // a "just in case" so we dont go and bork all our products if there's a coding error.
  111.                                         echo "FAILED TO UPDATE: $sql ";
  112.                                         echo mysql_error();
  113.                                         exit;
  114.                                 }
  115.                         }
  116.                         // now we have to check the product features! trickeyness..
  117.                         // we grab a list of their current features, if any...
  118.                         $product_features = Product::getFeaturesStatic($product_id);
  119.                         //print_r($product_features);
  120.                         $new_features = array();
  121.                         $do_feature_update = false;
  122.                         if(isset($data['ff']) && is_array($data['ff'])){
  123.                                 foreach($data['ff'] as $feature_id => $feature_value){
  124.                                         $update_value = trim(pSQL($feature_value));
  125.                                         if($update_value){
  126.                                                 //YEY! we can update this products feature value...
  127.                                                 $do_feature_update = true;
  128.                                                 $new_features[$feature_id] = $update_value;
  129.                                         }
  130.                                 }
  131.                         }
  132.                         $language_id = 1;
  133.                         //print_r($new_features);
  134.                         if($do_feature_update){
  135.                                 $product = new Product($product_id);
  136.                                 $product->deleteFeatures();
  137.                                 foreach($new_features as $feature_id => $feature_value){
  138.                                         // add our new custom feature:
  139.                                         //echo "Adding $feature_id as $feature_value <br>\n";
  140.                                         $id_value = $product->addFeaturesToDB($feature_id, 0, true, $language_id);
  141.                                         $product->addFeaturesCustomToDB($id_value, $language_id, $feature_value);
  142.                                 }
  143.                         }
  144.                 }
  145.                
  146.                
  147.                 /*
  148.                 [product_id] => array(
  149.                         "price" => 123.45,
  150.                         "weight" => 3,
  151.                         "ff"=> array(
  152.                                 [feature_id] => "value",
  153.                                 [feature_id] => "value",
  154.                                 [feature_id] => "value",
  155.                         )
  156.                 )
  157.                 */
  158.                
  159.                 return true;
  160.         }
  161.        
  162.        
  163.        
  164.         function getContent()
  165.     {
  166.         $this->_html = '<h2>'.$this->displayName.'</h2>';
  167.  
  168.         /* Update the settings */
  169.         if (isset($_POST['mu_doupdate']))
  170.         {
  171.                 if (!$this->updateProducts())
  172.                         $this->_html .= $this->displayError($this->l('An error occurred during product updating, please try saving again.'));
  173.                 else
  174.                         $this->_html .= $this->displayConfirmation($this->l('Your products have been successfully updated!'));
  175.         }
  176.        
  177.  
  178.         $this->_html .= '
  179.                 <fieldset>
  180.                         <legend><img src="'.$this->_path.'add.png" alt="" title="" /> '.$this->l('Mass Update Your Products').'</legend>
  181.                         <form method="post" action="'.$_SERVER['REQUEST_URI'].'">';
  182.        
  183.         $sql = 'SELECT *
  184.                                 FROM `'._DB_PREFIX_.'feature` f
  185.                                 LEFT JOIN `'._DB_PREFIX_.'feature_lang` fl ON ( f.`id_feature` = fl.`id_feature` AND fl.`id_lang` = 1)';
  186.                 $feature_results = Db::getInstance()->ExecuteS($sql);
  187.                
  188.                
  189.                 // all products
  190.                 $sql = 'SELECT *
  191.                                 FROM `'._DB_PREFIX_.'product` p
  192.                                 LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`) WHERE pl.`id_lang` = 1';
  193.                 $all_products = Db::getInstance()->ExecuteS($sql);
  194.                 foreach($all_products as &$product)$product['db_price']=$product['price'];
  195.                 //$all_products = Product::getProducts(1);
  196. //$all_products = Product::getProductsProperties(1,$all_products);
  197.                
  198.                 $main_fields = $this->getConfig();
  199.                
  200.         ob_start();
  201.        
  202.         ?>
  203.         <input type="hidden" name="mu_doupdate" value="go">
  204.        
  205.                 <input type="submit" class="button" name="go" value="Save All My Products" onclick="this.value='Updating.... Please wait....'; this.disabled=true;" />
  206.                 <br><br>
  207.         <table cellspacing="0" cellpadding="0" class="table space" width="98%" align="center">
  208.                         <tbody>
  209.                                 <tr>
  210.                                         <th>Product</th>
  211.                                         <? foreach($main_fields as $m=>$f){ ?>
  212.                                         <th><?=$f['friendly'];?></th>
  213.                                         <? } ?>
  214.                                         <? foreach($feature_results as $k=>$v){ ?>
  215.                                         <th><?=$v['name'];?></th>
  216.                                         <? } ?>
  217.                                 </tr>
  218.                                 <?php foreach($all_products as $product){
  219.                                         $product['price'] = $product['db_price']; // price hack
  220.                                         ?>  <tr> <td><a href="?tab=AdminCatalog&id_product=<?=$product['id_product'];?>&updateproduct&token=<?=Tools::getAdminToken('AdminCatalog11');?>"><?=$product['ean13'];?>&nbsp;&nbsp;&nbsp;&nbsp;<?=$product['name'];?></a></td> <? foreach($main_fields as $m=>$f){ ?>
  221.                                                 <td><?=$f['prefix'];?><input type="text" style="width: 40px;" value="<?=$product[$f['db_field']];?>" name="mup[<?=$product['id_product'];?>][<?=$f['db_field'];?>]"/><?=$f['suffix'];?></td> <? }  foreach($feature_results as $k=>$v){
  222.                                                         // what value is in this feature?
  223.                                                         $feature_value = '';
  224.                                                         foreach($product['features'] as $f){
  225.                                                                 if($f['id_feature']==$v['id_feature']){
  226.                                                                         $feature_value = $f['value'];
  227.                                                                 }
  228.                                                         }
  229.                                                         ?> <td><input type="text" style="width: 35px;" value="<?=$feature_value;?>" name="mup[<?=$product['id_product'];?>][ff][<?=$v['id_feature'];?>]"/></td> <?php
  230.                                                 }
  231.                                                 ?>      </tr> <?
  232.                                 }
  233.                                 ?>
  234.                 </tbody>
  235.         </table>
  236.         <br><br>
  237.                 <input type="submit" class="button" name="go" value="Save All My Products" onclick="this.value='Updating.... Please wait....'; this.disabled=true;" />
  238.                 <br><br>
  239.         <?
  240.        
  241.        
  242.         $this->_html .= ob_get_clean();
  243.        
  244.                         $this->_html .= '
  245.                                
  246.                        
  247.                         </form>
  248.                 </fieldset>';
  249.                        
  250.        
  251.  
  252.         return $this->_html;
  253.     }
  254.        
  255.        
  256. }
  257. ?>