LoginSignup
1
1

More than 5 years have passed since last update.

concrete5 でファイルのファイズサイズを表示して小数点の調整をする

Last updated at Posted at 2016-06-09

concrete5 のページリスト、ファイルブロックなどで、ファイルをダウンロードするようにカスタマイズできます。

<?php
defined('C5_EXECUTE') or die("Access Denied.");
$th = Core::make('helper/text');
$c = Page::getCurrentPage();
$dh = Core::make('helper/date'); /* @var $dh \Concrete\Core\Localization\Service\Date */
?>
<div class="ccm-block-page-list-wrapper">
    <?php
    foreach ($pages as $page)
    {
        $title = $th->entities($page->getCollectionName());
        $url = $nh->getLinkToCollection($page);
        $target = ($page->getCollectionPointerExternalLink() != '' && $page->openCollectionPointerExternalLinkInNewWindow()) ? '_blank' : $page->getAttribute('nav_target');
        $target = empty($target) ? '_self' : $target;
       $date = $dh->formatDateTime($page->getCollectionDatePublic(), true);
        $f = $page->getAttribute('pdf'); //ページ属性の場合
        $fileTitle="";
        if (is_object($f) && (!$has_detail))
        {
            $fv = $f->getVersion();
            $size = $fv->getFullSize(); // Get the file size
            $size = Core::make('helper/number')->formatSize($size);
           $type = strtoupper($fv->getExtension()); // get the file extension
            $url = $fv->getRelativePath();
            $pass= $this->getThemePath();
            $fileTitle= ' ('.$type.':'.$size.')';
            $target = '_blank';
        }
        ?>
        <div class="<?php echo $entryClasses?>">
            <a href="<?php echo $url?>" class="<?php echo $buttonClasses?>"><?php echo $buttonLinkText?></a>
            <div class="ccm-block-page-list-date"><?php echo $date?></div>
            <div class="ccm-block-page-list-title"><?php echo $title ?><?php echo $fileTitle ?></div>
        </div>
    } ?>
</div>

上記サンプルの

        $f = $page->getAttribute('pdf'); //ページ属性の場合
        $fileTitle="";
        if (is_object($f) && (!$has_detail))
        {
            $fv = $f->getVersion();
            $size = $fv->getFullSize(); // Get the file size
            $size = Core::make('helper/number')->formatSize($size);
           $type = strtoupper($fv->getExtension()); // get the file extension
            $url = $fv->getRelativePath();
            $pass= $this->getThemePath();
            $fileTitle= ' ('.$type.':'.$size.')';
            $target = '_blank';
        }

の部分で、ページ属性にファイルが登録されていれば、ページリストは、ページへのリンクではなく、ファイルへのリンクをページリストに表示するようになり、ファイルのタイプやサイズを表示してくれます。

ただし、デフォルトでは、ファイルサイズの表示が小数点以下の2桁が表示されます。

例:100.12KB

少数点が要らなかったり、少数点を2桁にしたい場合のハッキング方法です。

Number ヘルパーの formatSize() をコアから上書きするためのファイルを作成

  • /application/src/Utility/Service/Number.php テキストファイルを作成

下記のソースコードを流し込み

Number.php
<?php
namespace Application\Src\Utility\Service;

use Concrete\Core\Utility\Service\Number as CoreNumber;

class Number extends CoreNumber
{
    /**
     * Formats a size (measured in bytes, KB, MB, ...).
     * @param number $size The size to be formatted, in bytes.
     * @param string $forceUnit = '' Set to 'bytes', 'KB', 'MB', 'GB' or 'TB' if you want to force the unit, leave empty to automatically determine the unit.
     * @return string|mixed If $size is not numeric, the function returns $size (untouched), otherwise it returns the size with the correct usits (GB, MB, ...) and formatted following the locale rules.
     * @example formatSize(0) returns '0 bytes'
     * @example formatSize(1) returns '1 byte'
     * @example formatSize(1000) returns '1,000 bytes'
     * @example formatSize(1024) returns '1.00 KB'
     * @example formatSize(1024, 'bytes') returns '1024 bytes'
     * @example formatSize(1024, 'GB') returns '0.00 GB'
     * @example formatSize(2000000) returns '1.91 MB'
     * @example formatSize(-5000) returns '-4.88 KB'
     * @example formatSize('hello') returns 'hello'
     */
    public function formatSize($size, $forceUnit = '')
    {
        if (!is_numeric($size)) {
            return $size;
        }
        if (strlen($forceUnit) && array_search($forceUnit, array('bytes', 'KB', 'MB', 'GB', 'TB')) === false) {
            $forceUnit = '';
        }
        if ($forceUnit === 'bytes' || (abs($size) < 1024 && (!strlen($forceUnit)))) {
            return t2(/*i18n %s is a number */'%s byte', '%s bytes', $size, $this->format($size, 0));
        }
        $size /= 1024;
        if ($forceUnit === 'KB' || (abs($size) < 1024 && (!strlen($forceUnit)))) {
            return t(/*i18n %s is a number, KB means Kilobyte */'%s KB', $this->format($size, 1));
        }
        $size /= 1024;
        if ($forceUnit === 'MB' || (abs($size) < 1024 && (!strlen($forceUnit)))) {
            return t(/*i18n %s is a number, MB means Megabyte */'%s MB', $this->format($size, 1));
        }
        $size /= 1024;
        if ($forceUnit === 'GB' || (abs($size) < 1024 && (!strlen($forceUnit)))) {
            return t(/*i18n %s is a number, GB means Gigabyte */'%s GB', $this->format($size, 1));
        }

        return t(/*i18n %s is a number, TB means Terabyte */'%s TB', $this->format($size, 1));
    }
}

上記のコードをご覧いただけると、ファイルサイズに応じて、ほどよく bytes, KB, MB, GB に変換してくれています。そこの($size, 1)というところが、小数点を何桁表示するか指定している箇所です。このサンプル場合はコアの2ケタから1ケタを表示するように変更しました。

Number ヘルパーの上書きの指示

コア関連のファイルは、ファイルを作っただけでは、上書きされません。
下記のファイルに、ヘルパーが呼びだされたら違うファイルを読み込むことを宣言します。

  • application/bootstrap/app.php に下記のコードを <?phpの直後に挿入。
app.php
<?php
Core::bind('helper/number', function() {
     return new \Application\Src\Utility\Service\Number();
});

以上

参考
- 【完全保存版】 concrete5.7.x テーマスニペット集

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1