첨부파일 목록에서 이미지 파일 등을 출력하지 않을 때 첨부파일 개수에서도 감산하는 방법
(링크 프리뷰 모듈 작업하다가 잠깐 딴짓으로 팁 하나 정리해봅니다ㅎㅎ)
스케치북 게시판 등을 이용하시면 익숙한 기능 중 하나가 이미지, 비디오, 오디오 파일 등이 첨부파일로 있을 때 해당 파일을 목록에서 숨기는 것입니다.
이를테면 이미지 파일이 본문 삽입되어 있으면 굳이 첨부파일 목록에 노출시키고 싶지 않을 때가 있으니까요.
그런데 이때 첨부파일 개수와 목록의 항목 개수가 불일치하는 경우가 생깁니다.
첨부파일 개수는 $oDocument->get('uploaded_count')으로 고정되어 있는데, 파일 목록의 개수는 유동적으로 줄어들기 때문입니다.
가령 이런 식으로요. 파일이 4개 있다고 나오는데 정작 다운로드할 수 있는 파일은 2개만 출력되는 거죠.
이럴 때 첨부파일 개수도 2로 출력을 할 수 있다면 게시판이 훨씬 더 깔끔하겠죠?
스케치북 스킨을 중심으로 설명해보겠습니다.
라이믹스 개발진에서 관할하는 스케치북 스킨에는 이렇게 되어 있는데요.
https://github.com/rhymix/rhymix-sketchbook/blob/master/_read_files.html
여기서도 파일 개수가 $oDocument->get('uploaded_count')로 고정되어 있는 걸 볼 수 있습니다.
이걸 다음과 같이 바꾸면,
<div id="files_{$oDocument->document_srl}" class="rd_fnt rd_file<!--@if(!$mi->show_files)--> hide_file<!--@end-->">
<table class="bd_tb">
<caption class="blind">Atachment</caption>
<!--@if(!$mi->files_type)-->
<tr>
<th scope="row" class="ui_font"><strong>{$lang->uploaded_file}</strong> <span class="fnt_count">'<b>{$oDocument->get('uploaded_count')}</b>'</span></th>
<td>
<ul>
<li loop="$oDocument->getUploadedFiles()=>$key,$file"><a class="bubble" href="{getUrl('')}{$file->download_url}" title="[File Size:{FileHandler::filesize($file->file_size)}/Download:{number_format($file->download_count)}]">{$file->source_filename}</a><span class="comma">,</span></li>
</ul>
</td>
</tr>
<!--@elseif($mi->files_type)-->
<tr>
{@
$file_count = $oDocument->get('uploaded_count');
$is_printing = false;
$source_code = '<ul>';
foreach ( $oDocument->getUploadedFiles() as $key => $file ):
$ext = substr($file->source_filename, -4);
$ext = strtolower($ext);
$ext_img = in_array($ext,array('.jpg','jpeg','.gif','.png'));
$ext_video = in_array($ext,array('.mpg','mpeg','.avi','.wmv','.mp4','.mov','.mkv','.swf','.flv','.ogv','webm'));
$ext_audio = in_array($ext,array('.mp3','.ogg','.wma','.wav','.ape','flac','.mid'));
if ( ( !isset($mi->files_img) && $ext_img )
|| ( !isset($mi->files_video) && $ext_video )
|| ( !isset($mi->files_audio) && $ext_audio )
|| ( !isset($mi->files_etc) && (!$ext_img && !$ext_video && !$ext_audio) ) ):
$is_printing = true;
else:
$is_printing = false;
endif;
if ( $is_printing ):
$source_code .= '<li><a class="bubble" href="'. getUrl('') . $file->download_url .'" title="[File Size:'. FileHandler::filesize($file->file_size). '/Download:'. number_format($file->download_count) .']">'. $file->source_filename .'</a><span class="comma">,</span></li>';
else:
$file_count--;
endif;
endforeach;
$source_code .= '</ul>';
}
<th scope="row" class="ui_font"><strong>{$lang->uploaded_file}</strong> <span class="fnt_count">'<b>{$file_count}</b>'</span></th>
<td>{$source_code}</td>
</tr>
<!--@end-->
</table>
</div>
<table class="bd_tb">
<caption class="blind">Atachment</caption>
<!--@if(!$mi->files_type)-->
<tr>
<th scope="row" class="ui_font"><strong>{$lang->uploaded_file}</strong> <span class="fnt_count">'<b>{$oDocument->get('uploaded_count')}</b>'</span></th>
<td>
<ul>
<li loop="$oDocument->getUploadedFiles()=>$key,$file"><a class="bubble" href="{getUrl('')}{$file->download_url}" title="[File Size:{FileHandler::filesize($file->file_size)}/Download:{number_format($file->download_count)}]">{$file->source_filename}</a><span class="comma">,</span></li>
</ul>
</td>
</tr>
<!--@elseif($mi->files_type)-->
<tr>
{@
$file_count = $oDocument->get('uploaded_count');
$is_printing = false;
$source_code = '<ul>';
foreach ( $oDocument->getUploadedFiles() as $key => $file ):
$ext = substr($file->source_filename, -4);
$ext = strtolower($ext);
$ext_img = in_array($ext,array('.jpg','jpeg','.gif','.png'));
$ext_video = in_array($ext,array('.mpg','mpeg','.avi','.wmv','.mp4','.mov','.mkv','.swf','.flv','.ogv','webm'));
$ext_audio = in_array($ext,array('.mp3','.ogg','.wma','.wav','.ape','flac','.mid'));
if ( ( !isset($mi->files_img) && $ext_img )
|| ( !isset($mi->files_video) && $ext_video )
|| ( !isset($mi->files_audio) && $ext_audio )
|| ( !isset($mi->files_etc) && (!$ext_img && !$ext_video && !$ext_audio) ) ):
$is_printing = true;
else:
$is_printing = false;
endif;
if ( $is_printing ):
$source_code .= '<li><a class="bubble" href="'. getUrl('') . $file->download_url .'" title="[File Size:'. FileHandler::filesize($file->file_size). '/Download:'. number_format($file->download_count) .']">'. $file->source_filename .'</a><span class="comma">,</span></li>';
else:
$file_count--;
endif;
endforeach;
$source_code .= '</ul>';
}
<th scope="row" class="ui_font"><strong>{$lang->uploaded_file}</strong> <span class="fnt_count">'<b>{$file_count}</b>'</span></th>
<td>{$source_code}</td>
</tr>
<!--@end-->
</table>
</div>
다음과 같이 목록의 파일개수가 첨부파일 숫자에도 반영됩니다.
관심 있는 분들은 한번 적용해보세요 :)

윤삼
Lv. 19
아무래도 중급 초반 수준의 코딩 오타쿠인 것 같습니다.
댓글 0