douya2433 2017-08-02 10:16
浏览 23
已采纳

如何在Prestashop中获取所有附件列表

I'm working with custom module which should show latest (defined count) added attachments. I see this piece of code in 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.'
        )'
    );
}

So far i know i dont need this $include check so i need to make custom static function like getAllattachments or new class? Any ideas much appreciated.

In module i have this

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());
}

and in tpl this one

{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}

This all from blockmanufacturer module which was duplicated with changed GetManufacturer function.

  • 写回答

1条回答 默认 最新

  • dspv70887 2017-08-02 11:08
    关注

    It depends on language, but try this:

    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 : '')
        );
    }
    

    Then in your module you don't need to get all attachments:

    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());
    }
    

    And your 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}
    

    Btw, i don't think there is the link_rewrite in attachments.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?