Jump to content

Custom filed in AdminProductsController


petermein

Recommended Posts

Hi there,

 

Im trying to create an override for AdminProductsController from my module.

The overide works perfectly but i want to create an extra column the table.

 

 


class AdminProductsController extends AdminProductsControllerCore {

public function __construct() {

  $this->fields_list['Test'] = array(
'title' => 'Test',
'align' => 'left',
'width' => 80
);

 parent::__construct();

}
}

 

The above code does the job of adding a new column, however instead of getting the data from the product table itself. I want to define a function or a fixed value to this column.

 

Does anyone know if this is possible ?

Edited by petermein (see edit history)
Link to comment
Share on other sites

Check out AdminProductsControllerCore::getList(). The data is contained in the array $this->_list after parent::getList() is called. Down at the bottom of the method, the little loop:

 for ($i = 0; $this->_list && $i < $nb; $i++)
 {
  $this->_list[$i]['price_final'] = $this->_list[$i]['price_tmp'];
  unset($this->_list[$i]['price_tmp']);
 }

is already walking down the display list data for you. Wedge your code / function in there and you should be good. Without changing your display field , I think you'll need to name it exactly 'Test':

 for ($i = 0; $this->_list && $i < $nb; $i++)
 {
  $this->_list[$i]['price_final'] = $this->_list[$i]['price_tmp'];
  unset($this->_list[$i]['price_tmp']);
  $this->_list[ $i ]['Test'] = 'Foo';
 }

HTH,

Link to comment
Share on other sites

Thanks,

 

But the problem is that the parent of getList ( AdminController::getList ) builds a query to get the data from the product table.

 

'.($this->_tmpTableFilter ? ' * FROM (SELECT ' : '');

 if ($this->explicitSelect)
 {
  foreach ($this->fields_list as $key => $array_value)
  {
   // Add it only if it is not already in $this->_select
   if (isset($this->_select) && preg_match('/[\s]`?'.preg_quote($key, '/').'`?\s*,/', $this->_select))
 continue;

   if (isset($array_value['filter_key']))
 $this->_listsql .= str_replace('!', '.', $array_value['filter_key']).' as '.$key.',';
   elseif ($key == 'id_'.$this->table)
 $this->_listsql .= 'a.`'.bqSQL($key).'`,';
   elseif ($key != 'image' && !preg_match('/'.preg_quote($key, '/').'/i', $this->_select))
 $this->_listsql .= '`'.bqSQL($key).'`,';
  }
  $this->_listsql = rtrim($this->_listsql, ',');
 }
 else
  $this->_listsql .= ($this->lang ? 'b.*,' : '').' a.*';

 

Ofcourse i can edit this part to exclude my variable but i dont like doing core upgrades for 1 module.

Link to comment
Share on other sites

Ok i let go of adding a column to the product controller and moved to the AdminOrdersControllers. Here there is a call back functionality on the list_fields and it works like a charm.

 

For anyone interested here is my code with a little explaination:

 

<?php
class AdminOrdersController extends AdminOrdersControllerCore
{
public function __construct(){

 parent::__construct();


/* override the existing select query */
 $this->_select = '
 a.id_currency,
 a.id_order AS id_pdf,
 a.id_order AS id_test,   /* Added to have the field name available */
 CONCAT(LEFT(c.`firstname`, 1), \'. \', c.`lastname`) AS `customer`,
 osl.`name` AS `osname`,
 os.`color`,
 IF((SELECT COUNT(so.id_order) FROM `'._DB_PREFIX_.'orders` so WHERE so.id_customer = a.id_customer) > 1, 0, 1) as new';

 $this->fields_list['id_test'] = array(
  'title' => $this->l('test'),
  'width' => 35,
  'align' => 'center',
  'callback' => 'testCallback',  /* define callback function to get a value for this field */
  'orderby' => false,
  'search' => false,
  'remove_onclick' => true);
}

public function testCallback($id_test, $tr){
/* Get information from database that you want to be displayed */
 $sql = "SELECT state FROM ". _DB_PREFIX_ ."test WHERE order_id = ". $id_test ."";
 $row = Db::getInstance()->getRow($sql);

/* Assign some smarty variables */
 $this->context->smarty->assign(array(
  'test' => $row['variable']
 ));


/* Because youre overriding an AdminController youre in a rather wierd path so i hard coded my smarty template in a string */
 $tpl = '<span style="width:20px; margin-right:5px;">
{if ($test== 1)}
 <img src="../img/exact/1.png" alt="success" />
{elseif ($test= 0)}
 <img src="../img/exact/2.png" alt="failed" />
{else}
 <img src="../img/exact/3.png" alt="todo" />
{/if}
</span>';

/* Fetch the string with the resource prefix string: */
 return $this->context->smarty->fetch('string:'. $tpl);
}
}

 

Good luck with the code if you can use it.

Edited by petermein (see edit history)
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...