[TomatoCart]Add checkboxes in attribute groups window of admin panel

One community user report following problem:

If admin needs to remove many attributes groups it shall be able to remove them fast. Now admin can delete attributes groups only one by one

If you face the same problem, please improve the module as follow.

Step 1. Open admin > includes > extmodules > products_attributes > products_attributes_groups_grid.php with your favorite text editor.

Step 2. Replace all of code in it with following code:

<?php
/*
  $Id: products_attributes_groups_grid.php $
  TomatoCart Open Source Shopping Cart Solutions
  http://www.tomatocart.com

  Copyright (c) 2009 Wuxi Elootec Technology Co., Ltd

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License v2 (1991)
  as published by the Free Software Foundation.
*/

?>
Toc.products_attributes.AttributeGroupsGrid = function(config) {

  config = config || {};
  
  config.region = 'center';
  config.border = false;
  config.viewConfig = {emptyText: TocLanguage.gridNoRecords}; 
  
  config.ds = new Ext.data.Store({
    url: Toc.CONF.CONN_URL,
    baseParams: {
      module: 'products_attributes',
      action: 'list_products_attributes'        
    },
    reader: new Ext.data.JsonReader({
      root: Toc.CONF.JSON_READER_ROOT,
      totalProperty: Toc.CONF.JSON_READER_TOTAL_PROPERTY,
      id: 'products_attributes_groups_id'
    },[
      'products_attributes_groups_id',
      'products_attributes_groups_name',
      'total_entries'
    ]),
    autoLoad: true
  });
  
  config.rowActions = new Ext.ux.grid.RowActions({
    actions: [
     {iconCls: 'icon-edit-record', qtip: TocLanguage.tipEdit},
     {iconCls: 'icon-delete-record', qtip: TocLanguage.tipDelete}],
    widthIntercept: Ext.isSafari ? 4: 2
  });
  config.rowActions.on('action', this.onRowAction, this);    
  config.plugins = config.rowActions;  
     
  config.sm = new Ext.grid.CheckboxSelectionModel();
  config.cm = new Ext.grid.ColumnModel([
  	config.sm,
    {id: 'products_attributes_groups_name', header: '<?php echo $osC_Language->get('table_heading_attributes_groups');?>', dataIndex: 'products_attributes_groups_name'},
    {header: '<?php echo $osC_Language->get('table_heading_total_entries');?>', dataIndex: 'total_entries', width: 100, align: 'right'},
    config.rowActions
  ]);
  config.selModel = new Ext.grid.RowSelectionModel({singleSelect: true});
  config.autoExpandColumn = 'products_attributes_groups_name';
   
  config.listeners = {
    'rowclick' : this.onGrdRowClick
  };

  config.tbar = [
    {
      text: TocLanguage.btnAdd,
      iconCls: 'add',
      handler: this.onAdd,
      scope: this
    },
    '-',
    {
      text: TocLanguage.btnDelete,
      iconCls: 'remove',
      handler: this.onBatchDelete,
      scope: this
    },
    '-',
    { 
      text: TocLanguage.btnRefresh,
      iconCls:'refresh',
      handler: this.onRefresh,
      scope: this
    }];
    
  config.bbar = new Ext.PagingToolbar({
    pageSize: Toc.CONF.GRID_PAGE_SIZE,
    store: config.ds,
    iconCls: 'icon-grid',
    displayInfo: true,
    displayMsg: TocLanguage.displayMsg,
    emptyMsg: TocLanguage.emptyMsg
  });
  
  this.addEvents({'selectchange' : true});  
  
  Toc.products_attributes.AttributeGroupsGrid.superclass.constructor.call(this, config);
};

Ext.extend(Toc.products_attributes.AttributeGroupsGrid, Ext.grid.GridPanel, {
  
  onAdd: function() {
    var dlg = this.owner.createAttributeGroupsDialog();
    
    dlg.on('saveSuccess', function() {
      this.onRefresh();
    }, this);
    
    dlg.show();
  },
  
  onEdit: function(record) {
    var dlg = this.owner.createAttributeGroupsDialog();
    dlg.setTitle(record.get('products_attributes_groups_name'));
    
    dlg.on('saveSuccess', function() {
      this.onRefresh();
    }, this);

    dlg.show(record.get('products_attributes_groups_id'));
  },
  
  onDelete: function(record) {
    var groupsId = record.get('products_attributes_groups_id');
    
    Ext.Msg.confirm(
      TocLanguage.msgWarningTitle, 
      TocLanguage.msgDeleteConfirm, 
      function(btn) {
        if(btn == 'yes') { 
          Ext.Ajax.request({
            url: Toc.CONF.CONN_URL,
            params: { 
              module: 'products_attributes',
              action: 'delete_products_attributes',
              products_attributes_groups_id: groupsId                                        
            },
            callback: function(options, success, response) {
              var result = Ext.decode(response.responseText);
              
              if (result.success == true) {
                this.owner.app.showNotification({title: TocLanguage.msgSuccessTitle, html: result.feedback});
                this.getStore().reload();
              } else {
                Ext.MessageBox.alert(TocLanguage.msgErrTitle, result.feedback);
              }
            }, 
            scope: this 
          });
        }
      }, this);                                                               
  },
  
  onBatchDelete: function() {
    var keys = this.selModel.selections.keys;
    
    if(keys.length > 0) {    
      var batch = keys.join(',');
      
      Ext.Msg.confirm(
        TocLanguage.msgWarningTitle,
        TocLanguage.msgDeleteConfirm,
        function(btn) {
          if(btn == 'yes') {      
            Ext.Ajax.request({
              url: Toc.CONF.CONN_URL,
              params: { 
                module: 'products_attributes',
                action: 'delete_products_attributes_groups',
                batch: batch                                        
              },
              callback: function(options, success, response){
                var result = Ext.decode(response.responseText);
              
                if(result.success == true) {
                  this.owner.app.showNotification({title: TocLanguage.msgSuccessTitle, html: result.feedback});
                  this.getStore().reload();
                } else {
                  Ext.MessageBox.alert(TocLanguage.msgErrTitle, result.feedback);
                }
              },
              scope: this                     
            });                
          }                                              
        }, this);     
    } else {
      Ext.MessageBox.alert(TocLanguage.msgInfoTitle, TocLanguage.msgMustSelectOne);
    }
  },
  
  onGrdRowClick: function(grid, rowIndex, e) {
    var record = grid.getStore().getAt(rowIndex);
    this.fireEvent('selectchange', record);
  },
      
  onRefresh: function() {
    this.getStore().reload();
  },  
  
  onRowAction: function(grid, record, action, row, col) {
    switch(action) {
      case 'icon-delete-record':
        this.onDelete(record);
        break;
      
      case 'icon-edit-record':
        this.onEdit(record);
        break;
    }
  }
});

Step 3. Open admin > includes > jsons > products_attributes.php.

Step 4. Find following method in it:

function listProductsAttributesEntries() {
      global $osC_Database, $toC_Json, $osC_Language;
      
      $Qentries = $osC_Database->query('select * from :table_products_attributes_values where products_attributes_groups_id = :products_attributes_groups_id  and language_id = :language_id order by sort_order ');
      $Qentries->bindTable(':table_products_attributes_values', TABLE_PRODUCTS_ATTRIBUTES_VALUES);
      $Qentries->bindInt(':products_attributes_groups_id', $_REQUEST['products_attributes_groups_id']);
      $Qentries->bindInt(':language_id', $osC_Language->getID());
      $Qentries->execute();
      
      $records = array();
      while ($Qentries->next()) {
        $records[] = array('products_attributes_values_id' => $Qentries->ValueInt('products_attributes_values_id'),
                           'products_attributes_groups_id' => $Qentries->ValueInt('products_attributes_groups_id'),
                           'language_id' => $Qentries->ValueInt('language_id'),
                           'status' => $Qentries->Value('status'),
                           'module' => $Qentries->Value('module'),
                           'name' => $Qentries->Value('name'),
                           'value' => $Qentries->Value('value'),
                           'sort_order' => $Qentries->ValueInt('sort_order'));
      }
      
      $response = array(EXT_JSON_READER_ROOT => $records);
                        
      echo $toC_Json->encode($response);
    }

Add following method before it:

 function deleteProductsAttributesGroups() {
    	global $osC_Language, $toC_Json;
    	
    	$error = false;
    	$feedback = array();
    	
    	$batch = explode(',', $_REQUEST['batch']);
    	
    	foreach ($batch as $product_attributes_groups_id) {
    		$data = osC_Products_Attributes_Admin::getData($product_attributes_groups_id);
    		if ( $data['total_products'] > 0 ) {
    			$error = true;
    			$feedback[] = sprintf($osC_Language->get('delete_error_attributes_group_in_use'), $data['total_products']);
    		}
    		break;
    	}
    	
    	if ($error === false) {
    		foreach ($batch as $groups_id) {
    			if ( osC_Products_Attributes_Admin::delete($groups_id) ) {
    				$response = array('success' => true ,'feedback' => $osC_Language->get('ms_success_action_performed'));
    			} else {
    				$response = array('success' => false ,'feedback' => $osC_Language->get('ms_error_action_not_performed'));
    			}
    		}
    	}else {
    		$response = array('success' => false ,'feedback' => implode('<br />', $feedback));
    	}
    	
    	echo $toC_Json->encode($response);
    }

After completing above steps, it will be possible to delete several products attribute groups at the same time.

Snip20150420_1If you need my help, please contact me via support@tomatocart. I will help you as possible as i can.

Tags: , , , , , | Posted under TomatoCart | RSS 2.0

Author Spotlight

Jack Yin

TomatoCart Developer & Co Founder - Arvixe Web Hosting / TomatoCart Community Liaison

2 Comments on [TomatoCart]Add checkboxes in attribute groups window of admin panel

  1. Dartco says:

    Jack, thank you! Will you add this fix to the next version of minor TC?

Leave a Reply

Your email address will not be published. Required fields are marked *