Changing the administrative part

Sometimes you need to make administrative interface more informative. For example, the display of additional information in a certain list (news, products, etc.) to not extend into each element separately.

In this example, consider the possibility of adding the display mode of delivery in the order list.

During the formation of the list of orders corresponds file modules/shop/admin/shop.admin.order.php.

The list of data to be displayed in a list, stored in the array $variables_list.

Adding a new element delivery_id. As a result, the array will look like this:

Example:

public $variables_list = array (
    
'checkbox' => '',
    
'created' => array(
        
'name' => 'Date and time',
        
'type' => 'datetime',
        
'sql' => true,
        
'no_important' => true,
    ),
    
'name' => array(
        
'name' => 'Order',
        
'variable' => 'id',
        
'text' => '%d ID'
    
),
    
'status_id' => array(
        
'name' => 'Status',
        
'sql' => true,
    ),
    
'delivery_id' => array(
        
'name' => 'Delivery',
        
'sql' => true,
    ),
    
'summ' => array(
        
'name' => 'Sum',
        
'sql' => true,
    ),
    
'user_id' => array(
        
'name' => 'Customer',
        
'sql' => true,
    ),
    
'actions' => array(
        
'trash' => true,
    ),
);

Let's add a method that generates output the data for the new field.

Example:

public function list_variable_delivery_id($row, $var)
{
    
$delivery = DB::query_result("SELECT [name] FROM {shop_delivery} WHERE id=%d", $row['delivery_id']);
    return
'<div>'.$delivery.'</div>';
}

Consider the method in more detail.

Be sure to pay attention to the naming method. It always starts with list_variable_, and further there is the value of the array element key $variables_list. In our case it delivery_id.

The method is passed an array $row that contains a number of parameters of this order, including the right to us $row['delivery_id'].

$row['delivery_id'] — ID is the delivery method table {shop_delivery}. He allows us to choose the name of the method of delivery for a specific order. Save the name of the variable $delivery.

Next, we simply display it in the list.

Our method will be performed for each order from the list. In addition, each time will run SQL-query. Optimizing the code so that in the beginning was carried out one SQL-query with the search for all delivery methods used, then these data are simply to be used.

Please ask for all used delivery methods, and write this information in the variable $this->cache["prepare"]["delivery"].

Example:

// if data is not requested
if(! isset($this->cache["prepare"]["delivery"]))
{
    
// collect ID methods of delivery of orders derived from the list
    
$delivery_ids = array();
    foreach(
$this->diafan->rows as $r)
    {
        if(
$r["delivery_id"] && ! in_array($r["delivery_id"], $delivery_ids))
        {
            
$delivery_ids[] = $r["delivery_id"];
        }
    }
    if(
$delivery_ids)
    {
        
// ask for information found on the delivery methods
        // write the result as an array of keys,
        // where the key will be the id field, and the values of the name field
        
$this->cache["prepare"]["delivery"] = DB::query_fetch_key_value(
            
"SELECT id, [name] FROM {shop_delivery} WHERE id IN (%s)",
            
implode(",", $delivery_ids),
            
"id", "name"
        
);
    }
}

Then simply use the data.

Example:

if($row["delivery_id"] && ! empty($this->cache["prepare"]["delivery"][$row["delivery_id"]]))
{
    echo
$this->cache["prepare"]["delivery"][$row["delivery_id"]];
}

Final code:

Example:

public function list_variable_delivery_id($row)
{
    if(! isset(
$this->cache["prepare"]["delivery"]))
    {
        
$delivery_ids = array();
        foreach(
$this->diafan->rows as $r)
        {
            if(
$r["delivery_id"] && ! in_array($r["delivery_id"], $delivery_ids))
            {
                
$delivery_ids[] = $r["delivery_id"];
            }
        }
        if(
$delivery_ids)
        {
            
$this->cache["prepare"]["delivery"] = DB::query_fetch_key_value(
                
"SELECT id, [name] FROM {shop_delivery} WHERE id IN (%s)",
                
implode(",", $delivery_ids),
                
"id", "name"
            
);
        }
    }
    
$text = '<div>';
    if(
$row["delivery_id"] && ! empty($this->cache["prepare"]["delivery"][$row["delivery_id"]]))
    {
        
$text .= $this->cache["prepare"]["delivery"][$row["delivery_id"]];
    }
    
$text .= '</div>';
    return
$text;
}

This example is universal for all modules. If necessary, you can bring your own additional information in any of the lists.