Здравствуйте!
Решил проверить бот на написание кода. Задал вопрос: напиши плагин связывания зависимых атрибутов со свободными атрибутами в JoomShopping.
Ответ:
<?php
defined('_JEXEC') or die('Restricted access');
class plgSystemJoomshoppingdependentattributes extends JPlugin {
function onAfterDisplayAttribute($product, $attribute) {
$template = $this->params->get('template', ''); // получаем шаблон атрибута из настроек плагина
$dependent_attributes = array();
foreach ($product->getDependentAttributes() as $attr) {
$dependent_attributes[] = $attr->attr_id;
}
foreach (explode(',', $template) as $attr_id) {
$attr_id = trim($attr_id);
if (!in_array($attr_id, $dependent_attributes)) {
$attr_name = JTable::getInstance('attribute', 'jshop');
$attr_name->load($attr_id);
$product->attributes[] = array(
'id' => $attr_id,
'name' => $attr_name->getAttributeLang('name', $_SESSION['languages_id']),
'value' => '',
'input' => $this->renderInput($product, $attr_id),
'type' => $attr_name->type
);
}
}
return $attribute;
}
function renderInput($product, $attribute_id) {
$value = '';
foreach ($product->attributes as $attr) {
if ($attr['id'] == $attribute_id) {
$value = $attr['value'];
break;
}
}
$input = '';
$attr = JTable::getInstance('attribute', 'jshop');
$attr->load($attribute_id);
switch ($attr->type) {
case 'input':
$input = '<input type="text" name="attr['.$attribute_id.']" value="'.$value.'" />';
break;
case 'textarea':
$input = '<textarea name="attr['.$attribute_id.']">'.$value.'</textarea>';
break;
case 'select':
$options = '';
$attr_vals = $attr->getAttributeValues($_SESSION['languages_id']);
foreach ($attr_vals as $val) {
$options .= '<option value="'.$val->id.'" '.($val->id==$value?'selected="selected"':'').'>'.$val->getAttributeValueLang('name').'</option>';
}
$input = '<select name="attr['.$attribute_id.']">'.$options.'</select>';
break;
case 'radio':
$options = '';
$attr_vals = $attr->getAttributeValues($_SESSION['languages_id']);
foreach ($attr_vals as $val) {
$options .= '<input type="radio" name="attr['.$attribute_id.']" value="'.$val->id.'" '.($val->id==$value?'checked="checked"':'').'/> '.$val->getAttributeValueLang('name').' ';
}
$input = $options;
break;
case 'checkbox':
$options = '';
$attr_vals = $attr->getAttributeValues($_SESSION['languages_id']);
$value_arr = explode(',', $value);
foreach ($attr_vals as $val) {
$options .= '<input type="checkbox" name="attr['.$attribute_id.'][]" value="'.$val->id.'" '.(in_array($val->id, $value_arr)?'checked="checked"':'').'/> '.$val->getAttributeValueLang('name').' ';
}
$input = $options;
break;
default:
break;
}
return $input;
}
}
Комментарий бота: Данный плагин будет связывать зависимые атрибуты со свободными атрибутами на странице товара в магазине, в соответствии с шаблоном из настроек плагина. При выборе зависимого атрибута будет автоматически обновляться соответствующий свободный атрибут.
Прошу спецов прокомментировать