Joomla DJ Catalog 2-用于属性的customfields插件的问题

时间:2019-02-22 12:26:54

标签: php joomla

DJ Catalog2组件具有用于在产品说明中插入自定义字段的插件。 在标准customfileds表中-在产品描述下-在customfield多色中选择-这是

<span class="djc_cartattr_color">
<span class="djc_cartattr_color-bg" style="background-image: url('/media/djcatalog2/images/colors/image.png');"></span> 
<span class="djc_cartattr_color-name">Name</span></span>

这是用于显示属性的文件代码:

<?php
/**
 * @package DJ-Catalog2
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 */

defined ('_JEXEC') or die('Restricted access'); 
?>
<?php

$item = $this->item_cursor;

$attribute = $this->attribute_cursor; 
$attributeName = '_ef_'.$attribute->alias; 
?>
<?php if (isset($this->item_cursor->$attributeName) && (is_array($this->item_cursor->$attributeName) || (is_scalar($this->item_cursor->$attributeName) && trim($this->item_cursor->$attributeName) != ''))) { ?>
<tr class="djc_attribute djc<?php echo $attributeName; ?>">
    <td class="djc_label">
    <?php 
        if ($attribute->imagelabel != '') {
            echo '<img class="djc_attribute-imglabel" alt="'.htmlspecialchars($attribute->name).'" src="'.JURI::base().$attribute->imagelabel.'" />';
            echo '<span class="djc_attribute-label">'.htmlspecialchars($attribute->name).'</span>';
        } else {
            echo '<span class="djc_attribute-label">'.htmlspecialchars($attribute->name).'</span>';
        } 
    ?>
    </td>
    <td  class="djc_value">
    <?php 
        if (is_array($this->item_cursor->$attributeName) && $attribute->type != 'multicolor' && $attribute->type != 'color'){
            //$this->item_cursor->$attributeName = implode(', ', $this->item_cursor->$attributeName);
            if (count($this->item_cursor->$attributeName) > 1) {
                $this->item_cursor->$attributeName = '<ul><li>'.implode('</li><li>', $this->item_cursor->$attributeName).'</li></ul>';
            } else {
                $this->item_cursor->$attributeName = implode(' ', $this->item_cursor->$attributeName);
            }
            echo $this->item_cursor->$attributeName;
        }
        else if ($attribute->type == 'textarea' || $attribute->type == 'text'){
            $value = nl2br(htmlspecialchars($this->item_cursor->$attributeName));
            // convert URLs
            $value = preg_replace('#([\w]+://)([^\s()<>]+)#iS', '<a target="_blank" rel="noopener noreferrer" href="$1$2">$2</a>', $value);
            // convert emails
            $value = preg_replace('#([\w.-]+(\+[\w.-]+)*@[\w.-]+)#i', '<a target="_blank" rel="noopener noreferrer" href="mailto:$1">$1</a>', $value);
            echo $value;
        }
        else if ($attribute->type == 'html') {
            echo $this->item_cursor->$attributeName;
        }
        else if ($attribute->type == 'calendar') {
            echo JHtml::_('date', $this->item_cursor->$attributeName, JText::_('DATE_FORMAT_LC4'));
        }
        else if ($attribute->type == 'multicolor' || $attribute->type == 'color') {
            $optionParamsName = '_efp_' . $attribute->alias;
            if (!empty($item->$optionParamsName)) { ?>
                <?php foreach ($item->$optionParamsName as $optId => $optionParams) {?>
                    <?php 
                    $style = '';
                    $optionName = '';
                    foreach ($item->$attributeName as $optId2 => $optName) {
                        if ($optId2 == $optId) {
                            $optionName = $optName;
                            break;
                        }
                    }
                    if ($optionParams->get('hexcode')) {
                        $style .= 'background-color: ' . $optionParams->get('hexcode').';';
                    }
                    if ($optionParams->get('file_name')) {
                        $style .= 'background-image: url(\''.JUri::base(true) .'/media/djcatalog2/images/colors/'. $optionParams->get('file_name').'\');';
                    }
                    if ($style == '') {
                        $style = 'background-color: #ccc; background-image: url(\''.JUri::base(true) .'/components/com_djcatalog2/themes/'.$this->params->get('theme', 'default').'/images/icon-no-color.png\')';
                    }
                    ?>
                    <span class="djc_cartattr_color">
                        <span class="djc_cartattr_color-bg" style="<?php echo $style; ?>">
                        </span> 
                        <span class="djc_cartattr_color-name"><?php echo $optionName; ?></span>
                    </span>
                <?php } ?>
            <?php } else {
                echo htmlspecialchars(implode(', ', $item->$attributeName));
            }
        }
        else {
            echo htmlspecialchars($this->item_cursor->$attributeName);
        }   
    ?>
    </td>
</tr>
<?php } ?>

如果我尝试通过插件(使用{djc2customfield color}代码)向商品说明添加一个属性,则只有

<span class="djc_attribute color_pr__ba">
<span class="djc_attribute-value">atribute value 1, atribute value 2</span>
</span>

未将图像分配给属性值。

在产品说明中显示属性的代码为:

<?php
/**
 * @package DJ-Catalog2
 * @copyright Copyright (C) DJ-Extensions.com, All rights reserved.
 * @license http://www.gnu.org/licenses GNU/GPL
 * @author url: http://dj-extensions.com
 * @author email contact@dj-extensions.com
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.plugin.plugin');
class plgDJCatalog2Customfield extends JPlugin {

    public static $attributes = array();

    public function __construct(& $subject, $config)
    {
        parent::__construct($subject, $config);
        $this->loadLanguage();
    }

    function onPrepareItemDescription( &$row, &$params, $page=0, $context = 'item')
    {
        $app = JFactory::getApplication();
        if (empty(self::$attributes)) {
            $db = JFactory::getDbo();

            $query = $db->getQuery(true);

            $query->select('f.*, group_concat(fo.id separator \'|\') as options');
            $query->from('#__djc2_items_extra_fields as f');
            $query->join('LEFT', '#__djc2_items_extra_fields_options as fo ON fo.field_id=f.id');

            $query->where('f.published = 1');
            $query->group('f.id');
            $query->order('f.group_id asc, f.ordering asc');

            $db->setQuery($query);
            self::$attributes = $db->loadObjectList();
        }

        //echo '<pre>'.print_r(self::$attributes,true).'</pre>';

        //$regex    = '/{djc2customfield\s+(.*?)}/i';
        $regex = '#{djc2customfield\s([a-z0-9_]+?)([^}]*?)}#iU';
        $regexGrp = '#{djc2fieldgroup\s([a-z0-9_]+?)([^}]*?)}#iU';


        $row->_nulledExtrafields = array();
        $nullExtraFields = array();

        preg_match_all($regex, $row->description, $matches, PREG_SET_ORDER);
        preg_match_all($regexGrp, $row->description, $matchesGrp, PREG_SET_ORDER);

        if ($matchesGrp) {
            foreach ($matchesGrp as $matchGrp) {
                $output = null;
                $rows = array();
                $group_id = trim($matchGrp[1]);
                $layout = 'table';
                if (isset($matchGrp[2])) {
                    $attrs = self::parseAttributes(trim($matchGrp[2]));
                    if (isset($attrs['layout'])) {
                        $layout = trim($attrs['layout']);
                    }
                }

                foreach (self::$attributes as $attribute) {
                    if ($attribute->group_id == $group_id) {
                        $item_attrib = '_ef_'.$attribute->alias;

                        if (!empty($row->$item_attrib)) {
                            $attributeData = $row->$item_attrib;
                            if ($layout == 'list') {
                                $rows[] = '<li class="djc_attribute"><span class="djc_label">'.$attribute->name.'</span>: <span class="djc_value">'.$this->renderAttribute($attribute, $attributeData, false).'</span></li>';
                            } else if ($layout == 'divs') {
                                $rows[] = '<div class="djc_attribute"><span class="djc_label">'.$attribute->name.'</span>:   <span class="djc_value">
                                '.$this->renderAttribute($attribute, $attributeData, false).'
                                </span></div>';


                            }  else if ($layout == 'inline') {
                                $rows[] = '<span class="djc_value">'.$this->renderAttribute($attribute, $attributeData, false).'</span>';
                            }
                            else {
                                $rows[] = '<tr class="djc_attribute"><td class="djc_label">'.$attribute->name.'</td><td class="djc_value">'.$this->renderAttribute($attribute, $attributeData, false).'</td></tr>';
                            }
                        }

                        $nullExtraFields[] = $item_attrib;
                    }
                }
                if (!empty($rows)){
                    if ($layout == 'list') {
                        $output .= '<div class="djc_attributes"><ul class="djc_attributes_list">';
                        $output .= implode(' ',$rows);
                        $output .= '</ul></div>';
                    } else if ($layout == 'divs') {
                        $output .= '<div class="djc_attributes">';
                        $output .= implode(' ',$rows);
                        $output .= '</div>';
                    } else if ($layout == 'inline') {
                        $output .= '<div class="djc_attributes">';
                        $output .= implode(', ',$rows);
                        $output .= '</div>';
                    } else {
                        $output .= '<div class="djc_attributes"><table class="table table-condensed">';
                        $output .= implode(' ',$rows);
                        $output .= '</table></div>';
                    }

                    $row->description = preg_replace('/'.preg_quote($matchGrp[0]).'/', $this->escapeValue($output), $row->description, 1);
                }
            }
        }

        // No matches, skip this
        if ($matches) {
            foreach ($matches as $match) {

                $matcheslist =  explode(',',$match[1]);

                if (!array_key_exists(1, $matcheslist)) {
                    $matcheslist[1] = null;
                }

                $attrib = trim($matcheslist[0]);
                $item_attrib = '_ef_'.$attrib;
                $output = null;

                $show_label = true;
                $unset_var = true;

                if (isset($match[2])) {
                    $attrs = self::parseAttributes(trim($match[2]));
                    if (isset($attrs['label']) && $attrs['label'] == '0') {
                        $show_label = false;
                    }
                    if (isset($attrs['unset']) && $attrs['unset'] == '0') {
                        $unset_var = false;
                    }
                }

                if (!empty($row->$item_attrib)) {

                    foreach (self::$attributes as $attribute) {
                        if ($attribute->alias == $attrib) {
                            $attributeData = $row->$item_attrib;
                            $output .= $this->renderAttribute($attribute, $attributeData, $show_label);
                        }
                    }
                    $row->_nulledExtrafields[] = $attrib;
                    $row->_nulledExtrafields[] = $item_attrib;

                    if ($unset_var) {
                        unset($row->$item_attrib);
                    }
                }
                $row->description = preg_replace("|$match[0]|", $this->escapeValue($output), $row->description, 1);
            }
        }

        if (count($nullExtraFields) > 0) {
            foreach($nullExtraFields as $item_attrib) {
                if (isset($row->$item_attrib)) {
                    unset($row->$item_attrib);
                }
            }
        }

        return true;

    }

    public static function parseAttributes($string) {
        $attr = array();
        $retarray = array();

        // Let's grab all the key/value pairs using a regular expression
        preg_match_all('/([\w:-]+)[\s]?=[\s]?"([^"]*)"/i', $string, $attr);

        if (is_array($attr))
        {
            $numPairs = count($attr[1]);

            for ($i = 0; $i < $numPairs; $i++) {
                $retarray[$attr[1][$i]] = $attr[2][$i];
                }
            }

            return $retarray;
    }

    public static function renderAttribute($attribute, $attributeData, $show_label){
        $output = '';   
        if (is_array($attributeData)) {
            $attributeData = implode(', ', $attributeData);
        }
        if ($show_label) {
            $output .= '<div class="atrybut">
            <img class="djc_attribute-imglabel" alt="'.htmlspecialchars($attribute->name).'" src="'.JURI::base().$attribute->imagelabel.'" /><br />

            <span class="djc_attribute '.preg_replace('#[^0-9a-zA-Z\-]#', '_', strtolower(trim($attribute->name))).'"><span class="djc_attribute-label">'.$attribute->name.': </span> <br />

            <span class="djc_attribute-value">'.$attributeData.'</span></span>
            </div>
            ';
        } else {
            $output .= '<span class="djc_attribute '.preg_replace('#[^0-9a-zA-Z\-]#', '_', strtolower(trim($attribute->name))).'"><span class="djc_attribute-value">'.$attributeData.'</span></span>';
        }

        return $output;
    }

    public function escapeValue($value) {
        $value = addcslashes($value, '\\');
        $value = preg_replace('/\$(\d)/', '\\\$$1', $value);
        return $value;
    }

}

我想使用layout =“ divs”。 我想我需要更改代码

else if ($layout == 'divs') {
$rows[] = '<div class="djc_attribute"><span class="djc_label">'.$attribute->name.'</span>:   <span class="djc_value">
'.$this->renderAttribute($attribute, $attributeData, false).'
</span></div>';

但是我不知道如何。有人可以帮我这个吗?

0 个答案:

没有答案