Trace: » unzip-in-media
การเพิ่มปุ่ม unzip ใน Media Manager
โดยปกติ Media Manager จะอนุญาตให้โอนแฟ้มข้อมูลได้เพียงครั้งละ 1 แฟ้ม แต่ก็สามารถปรับให้โอนได้มากขึ้นโดยการโอน zip ไฟล์ แล้วสร้างปุ่ม unzip เพิ่มใน Media Manager ดังนี้
1) เปิดไฟล์ view.html.php จาก administrator/component/com_media/views/media/view.html.php แล้วเพิ่มชุดคำสั่งต่อไปนี้ในบรรทัดที่ 106 เป็นต้นไป เพื่อสร้างปุ่ม Unzip ให้กับ Media Manager
/* (...) */
// Add a Unzip button
$title = JText::_('Unzip');
$dhtml = "<a href=\"#\" onclick=\"MediaManager.submit('folder.unzip')\"
class=\"toolbar\">
<span class=\"icon-32-unarchive\" title=\"$title\"
type=\"Custom\"></span>
$title</a>";
$bar->appendButton( 'Custom', $dhtml, 'unzip' );
/* (...) */
2) จากนั้นเพิ่มชุดคำสั่ง unzip เอกสารโดยเปิดไฟล์ folder.php จาก administrator/component/com_media/controllers/ แล้วเพิ่มชุดคำสั่งต่อไปนี้ต่อจากฟังก์ชัน delete
/* (...) */
function unzip() {
global $mainframe;
// Set FTP credentials, if given
jimport('joomla.client.helper');
JClientHelper::setCredentialsFromRequest('ftp');
// Get some data from the request
$tmpl = JRequest::getCmd( 'tmpl' );
$paths = JRequest::getVar( 'rm', array(), '', 'array' );
$folder = JRequest::getVar( 'folder', '', '', 'path');
// Initialize variables
$msg = array();
$ret = true;
if (count($paths)) {
foreach ($paths as $path)
{
if ($path !== JFilterInput::clean($path, 'path')) {
JError::raiseWarning(100, JText::_('Impossible de
decompresser:').htmlspecialchars($path, ENT_COMPAT,
'UTF-8').' '.JText::_('WARNDIRNAME'));
continue;
}
$fullPath = JPath::clean(COM_MEDIA_BASE.DS.$folder.DS.$path);
if (is_file($fullPath)) {
$ext = JFile::getExt(strtolower($fullPath));
$pathdir = $fullPath;
if($ext != 'gz') {
$pathdir = str_replace( ".".$ext, "",$pathdir);
}
else {
$pathdir = str_replace( ".".$ext, "",$pathdir);
$pathdir = str_replace( ".tar", "",$pathdir);
}
jimport('joomla.filesystem.*');
jimport('joomla.filesystem.archive');
JFolder::create($pathdir);
JFile::write($pathdir.DS."index.html", "<html>\n<body
bgcolor=\"#FFFFFF\">\n</body>\n</html>");
JArchive::extract($fullPath, $pathdir);
} else if (is_dir($fullPath)) {
JError::raiseWarning(100, JText::_('Imossible de
decompresser:').$fullPath.' '.JText::_('Pas un fichier
ZIP'));
}
}
}
if ($tmpl == 'component') {
// We are inside the iframe
$mainframe->redirect('index.php?option=com_media&view=mediaList&
folder='.$folder.'&tmpl=component');
} else {
$mainframe->redirect('index.php?option=com_media&folder='.$folder);
}
}
/* (...) */
3) เมื่อแก้ไขไฟล์ทั้ง 2 แล้วจะต้องไปกำหนดให้ joomla ยอมรับการโอนไฟล์ .zip เข้าสู่ระบบ โดยเลือก Site, Global Configuration, System, Media Settings แล้วปรับค่า Legal Extensions (File Types) โดยเพิ่ม .zip ต่อท้าย ดังนี้
เพียงเท่านี้ก็ได้ปุ่ม unzip และพร้อมทำงานกับ Zip file ได้แล้วครับ
ผู้เขียน
- นายบุญเลิศ อรุณพิบูลย์ นักวิชาการ ศูนย์บริการความรู้ทางวิทยาศาสตร์และเทคโนโลยี

