如何获取Prestashop

时间:2017-08-02 10:17:00

标签: php prestashop

我正在使用自定义模块,该模块应显示最新(已定义计数)添加的附件。 我在 class / Attachment.php

中看到了这段代码
public static function getAttachments($id_lang, $id_product, $include = true)
{
    return Db::getInstance()->executeS('
        SELECT *
        FROM '._DB_PREFIX_.'attachment a
        LEFT JOIN '._DB_PREFIX_.'attachment_lang al
            ON (a.id_attachment = al.id_attachment AND al.id_lang = '.(int)$id_lang.')
        WHERE a.id_attachment '.($include ? 'IN' : 'NOT IN').' (
            SELECT pa.id_attachment
            FROM '._DB_PREFIX_.'product_attachment pa
            WHERE id_product = '.(int)$id_product.'
        )'
    );
}

到目前为止,我知道我不需要这个$ include支票,所以我需要制作自定义静态函数,如getAllattachments或新类?任何想法都非常赞赏。

在模块中我有这个

public function hookLeftColumn($params)
{
    if (!$this->isCached('blockattachment.tpl', $this->getCacheId()))
    {
        $attachments = Attachment::getAttachments();
        foreach ($attachments as &$attachment)
        {
            $attachment['image'] = $this->context->language->iso_code.'-default';
            if (file_exists(_PS_MANU_IMG_DIR_.$attachment['id_manufacturer'].'-'.ImageType::getFormatedName('medium').'.jpg'))
                $attachment['image'] = $attachment['id_manufacturer'];
        }

        $this->smarty->assign(array(
            'attachments' => $attachments,
            'text_list' => Configuration::get('ATTACHMENT_DISPLAY_TEXT'),
            'text_list_nb' => Configuration::get('ATTACHMENT_DISPLAY_TEXT_NB'),
            'form_list' => Configuration::get('ATTACHMENT_DISPLAY_FORM'),
            'display_link_manufacturer' => Configuration::get('PS_DISPLAY_SUPPLIERS'),
        ));
    }
    return $this->display(__FILE__, 'blockattachment.tpl', $this->getCacheId());
}

并在tpl这个

{if $attachments}
        {if $text_list}
            <ul class="bullet">
                {foreach from=$attachments item=attachment name=attachment_list}
                    {if $smarty.foreach.attachment_list.iteration <= $text_list_nb}
                    <li><a href="{$link->getAttachmentLink($attachment.id_attachment, $attachment.link_rewrite)|escape:'html'}" title="{l s='More about %s' sprintf=[$attachment.name] mod='blockattachment'}">{$attachment.name|escape:'html':'UTF-8'}</a></li>
                    {/if}
                {/foreach}
            </ul>
        {/if}
    {else}
        <p>{l s='No new attachments' mod='blockattachment'}</p>
    {/if}

这一切都来自blockmanufacturer模块,该模块与更改的GetManufacturer功能重复。

1 个答案:

答案 0 :(得分:1)

这取决于语言,但试试这个:

public static function getLatestAttachments($id_lang, $n = 5)
{
    return Db::getInstance()->executeS('
        SELECT DISTINCT (a.`id_attachment`), a.`file`,  a.`file_name`, a.`file_size`, al.`name`
        FROM '._DB_PREFIX_.'attachment a
        LEFT JOIN '._DB_PREFIX_.'attachment_lang al
            ON (a.id_attachment = al.id_attachment AND al.id_lang = '.(int)$id_lang.')
        ORDER BY a.`id_attachment` DESC'.
        ($n > 0 ? ' LIMIT '.$n : '')
    );
}

然后在您的模块中,您不需要获取所有附件:

public function hookLeftColumn($params)
{
    if (!$this->isCached('blockattachment.tpl', $this->getCacheId()))
    {
        $attachments = Attachment::getLatestAttachments($this->context->language->id, Configuration::get('ATTACHMENT_DISPLAY_TEXT_NB'));
        foreach ($attachments as &$attachment)
        {
            $attachment['image'] = $this->context->language->iso_code.'-default';
            if (file_exists(_PS_MANU_IMG_DIR_.$attachment['id_manufacturer'].'-'.ImageType::getFormatedName('medium').'.jpg'))
                $attachment['image'] = $attachment['id_manufacturer'];
        }

        $this->smarty->assign(array(
            'attachments' => $attachments,
            'text_list' => Configuration::get('ATTACHMENT_DISPLAY_TEXT'),
            'form_list' => Configuration::get('ATTACHMENT_DISPLAY_FORM'),
            'display_link_manufacturer' => Configuration::get('PS_DISPLAY_SUPPLIERS'),
        ));
    }
    return $this->display(__FILE__, 'blockattachment.tpl', $this->getCacheId());
}

你的tpl:

{if $attachments}
        {if $text_list}
            <ul class="bullet">
                {foreach from=$attachments item=attachment name=attachment_list}
                    <li><a href="{$link->getAttachmentLink($attachment.id_attachment, $attachment.name)|escape:'html'}" title="{l s='More about %s' sprintf=[$attachment.name] mod='blockattachment'}">{$attachment.name|escape:'html':'UTF-8'}</a></li>
                {/foreach}
            </ul>
        {/if}
    {else}
        <p>{l s='No new attachments' mod='blockattachment'}</p>
    {/if}
是的,我不认为附件中有link_rewrite。