public function installSampleData() { global $jlistConfig; jimport('joomla.filesystem.folder'); jimport('joomla.filesystem.file'); $db = JFactory::getDBO(); $query = $db->getQuery(true); $user = JFactory::getUser(); $error = false; $model = $this->getModel('category', '', array()); $model_download = $this->getModel('download', '', array()); // check user access right if (JFactory::getUser()->authorise('edit.config', 'com_jdownloads')) { $root_dir = $jlistConfig['files.uploaddir']; if (JFolder::exists($root_dir)) { if (is_writable($root_dir)) { // create it only when the folder for the sample category still not exists if (!JFolder::exists($root_dir . '/' . JText::_('COM_JDOWNLOADS_SAMPLE_DATA_CAT_NAME_ROOT'))) { // create root cat $create_result = $model->createCategory(JText::_('COM_JDOWNLOADS_SAMPLE_DATA_CAT_NAME_ROOT'), '1', '', JText::_('COM_JDOWNLOADS_SAMPLE_DATA_CAT_NAME_TEXT'), 1); if (!$create_result) { // error message: can not rebuild tree in DB $error = true; JError::raiseWarning(100, JText::_('COM_JDOWNLOADS_SAMPLE_DATA_REBUILD_ERROR')); } else { // create sub category $create_result_sub = $model->createCategory(JText::_('COM_JDOWNLOADS_SAMPLE_DATA_CAT_NAME_SUB'), $create_result->id, '', JText::_('COM_JDOWNLOADS_SAMPLE_DATA_CAT_NAME_TEXT')); if (!$create_result_sub) { // error message: can not rebuild tree in DB $error = true; JError::raiseWarning(100, JText::_('COM_JDOWNLOADS_SAMPLE_DATA_REBUILD_ERROR')); } else { // create download example $filename = 'example_data_file.zip'; // copy first the file to the target folder $source_path = JPATH_COMPONENT_ADMINISTRATOR . '/assets/example_data_file.zip'; $dest_path = $root_dir . '/' . $create_result_sub->cat_dir_parent . '/' . $create_result_sub->cat_dir . '/example_data_file.zip'; $filesize = JDownloadsHelper::fsize($source_path); $result = JFile::copy($source_path, $dest_path); $new_download = $model_download->createDownload(JText::_('COM_JDOWNLOADS_SAMPLE_DATA_FILE_NAME'), $create_result_sub->id, '', JText::_('COM_JDOWNLOADS_SAMPLE_DATA_FILE_NAME_TEXT'), $filename, $filesize); if (!$new_download) { $error = true; JError::raiseWarning(100, JText::_('COM_JDOWNLOADS_SAMPLE_DATA_REBUILD_ERROR')); } } } } else { // error message: upload folder not writeable $error = true; JError::raiseWarning(100, JText::_('COM_JDOWNLOADS_SAMPLE_DATA_EXISTS')); } } else { // error message: upload folder not writeable $error = true; JError::raiseWarning(100, JText::_('COM_JDOWNLOADS_SAMPLE_DATA_CREATE_ERROR')); } } else { // error message: upload folder not found $error = true; JError::raiseWarning(100, JText::_('COM_JDOWNLOADS_SAMPLE_DATA_CREATE_ERROR')); } if (!$error) { // result successful JFactory::getApplication()->enqueueMessage(JText::_('COM_JDOWNLOADS_TOOLS_RESET_RESULT_OKAY_MSG')); } } $this->setRedirect(JRoute::_('index.php?option=com_jdownloads&view=tools', false)); }
/** * Method to load files data in array * * @access public * @return array An array of results. */ public function getItems() { global $jlistConfig; jimport('joomla.filesystem.folder'); jimport('joomla.filesystem.file'); require_once JPATH_COMPONENT . '/helpers/jdownloadshelper.php'; jimport('joomla.html.pagination'); $app = JFactory::getApplication('administrator'); $mainframe = JFactory::getApplication(); $option = 'com_jdownloads'; // Lets load the file data if it doesn't already exist if (empty($this->_data)) { // get all file names from upload root dir $files_dir = $jlistConfig['files.uploaddir'] . DS; $filenames = JFolder::files($jlistConfig['files.uploaddir'], $filter = '.', $recurse = false, $fullpath = false, $exclude = array('index.htm', 'index.html', '.htaccess')); $files_info = array(); // build data array for files list for ($i = 0; $i < count($filenames); $i++) { $files_info[$i]['id'] = $i + 1; $files_info[$i]['name'] = $filenames[$i]; $date_format = JDownloadsHelper::getDateFormat(); $files_info[$i]['date'] = date($date_format['long'], filemtime($files_dir . $filenames[$i])); $files_info[$i]['size'] = JDownloadsHelper::fsize($files_dir . $filenames[$i]); } // search in file names $search = $this->getState('filter.search'); if ($search) { $search_result = JDownloadsHelper::arrayRegexSearch('/' . $search . '/i', $files_info, TRUE, TRUE); foreach ($search_result as $result) { $files_info_result[] = $files_info[$result]; } $files_info = $files_info_result; } // build pagination data $limitstart = $this->getState('list.start'); $limit = $this->getState('list.limit'); $pageNav = new JPagination(count($files_info), $limitstart, $limit); $this->_pagination = $pageNav; $items = array_splice($files_info, $limitstart, $limit); $this->_data = $items; } return $this->_data; }