Skip to content

Commit

Permalink
Merge pull request #206 from rajnish-dargan/release-1.4.3
Browse files Browse the repository at this point in the history
Task #205 feat : File download functionality from backend
  • Loading branch information
ankush-maherwal authored Dec 3, 2019
2 parents 21eb04f + eee21de commit 27b90ef
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 208 deletions.
1 change: 1 addition & 0 deletions admin_language/en-GB/en-GB.com_tjfields.ini
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,4 @@ COM_TJFIELDS_PERMISSION_VIEW_OWN_FIELD_VALUE_DESC="Users in the group to view ow
COM_TJFIELDS_FORM_OPTIONS_NAME_LABEL="Text"
COM_TJFIELDS_FORM_OPTIONS_VALUE_LABEL="Value"
COM_TJFIELDS_FORM_OPTIONS_OPTIONID_LABEL="Option Id"
COM_TJFIELDS_FILE_DELETE_SUCCESS="File deleted successfully."
2 changes: 1 addition & 1 deletion administrator/assets/js/tjfields.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jQuery(document).ready(function(){
}

jQuery.ajax({
url: Joomla.getOptions('system.paths').base + "/index.php?option=com_tjfields&task=fields.deleteFile&format=json",
url: Joomla.getOptions('system.paths').base + "/index.php?option=com_tjfields&task=fields.deleteFile&format=json&" + Joomla.getOptions('csrf.token') + '=1',
type: 'POST',
data:{
fileName: fileName,
Expand Down
115 changes: 112 additions & 3 deletions administrator/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,38 @@

// No direct access
defined('_JEXEC') or die;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Factory;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Language\Text;
use Joomla\CMS\MVC\Controller\BaseController;
use Joomla\CMS\Uri\Uri;

/**
* TJ Fields Controller
*
* @since 2.5
*/
class TjfieldsController extends JControllerLegacy
class TjfieldsController extends BaseController
{
/**
* The return URL.
*
* @var mixed
*/
protected $returnURL;

/**
* Constructor
*
*/
public function __construct()
{
$this->returnURL = Uri::base();

parent::__construct();
}

/**
* Method to display a view.
*
Expand All @@ -31,11 +55,96 @@ public function display($cachable = false, $urlparams = false)
{
require_once JPATH_COMPONENT . '/helpers/tjfields.php';

$view = JFactory::getApplication()->input->getCmd('view', 'fields');
JFactory::getApplication()->input->set('view', $view);
$view = Factory::getApplication()->input->getCmd('view', 'fields');
Factory::getApplication()->input->set('view', $view);

parent::display($cachable, $urlparams);

return $this;
}

/**
* Fuction to get download media file
*
* @return object
*/
public function getMediaFile()
{
(JSession::checkToken() or JSession::checkToken('get')) or jexit(JText::_('JINVALID_TOKEN'));
JLoader::import("/techjoomla/media/storage/local", JPATH_LIBRARIES);
$app = Factory::getApplication();
$jinput = $app->input;
$mediaLocal = TJMediaStorageLocal::getInstance();

// Here, fpht means file encoded name
$encodedFileName = $jinput->get('fpht', '', 'STRING');
$decodedFileName = base64_decode($encodedFileName);

// Subform File field Id for checking authorization for specific field under subform
$subformFileFieldId = $jinput->get('subFormFileFieldId', '', 'INT');

// Get media storage path
JLoader::import('components.com_tjfields.models.fields', JPATH_SITE);
$fieldsModel = BaseDatabaseModel::getInstance('Fields', 'TjfieldsModel', array('ignore_request' => true));
$data = $fieldsModel->getMediaStoragePath($jinput->get('id', '', 'INT'), $subformFileFieldId);

if ($data->tjFieldFieldTable->type == "file")
{
$extraFieldParams = json_decode($data->tjFieldFieldTable->params);
$storagePath = $extraFieldParams->uploadpath;
$decodedPath = $storagePath . '/' . $decodedFileName;
}
else
{
$fieldType = $data->tjFieldFieldTable->type;
$decodedPath = JPATH_SITE . '/' . $fieldType . 's/tjmedia/' . str_replace(".", "/", $data->tjFieldFieldTable->client) . '/' . $decodedFileName;
}

if ($data->tjFieldFieldTable->fieldValueId)
{
$user = Factory::getUser();

if ($subformFileFieldId)
{
$canView = $user->authorise('core.field.viewfieldvalue', 'com_tjfields.field.' . $subformFileFieldId);
}
else
{
$canView = $user->authorise('core.field.viewfieldvalue', 'com_tjfields.field.' . $data->tjFieldFieldTable->field_id);
}

$canDownload = 0;

// Allow to view own data
if ($data->tjFieldFieldTable->user_id != null && ($user->id == $data->tjFieldFieldTable->user_id))
{
$canDownload = true;
}

if ($canView || $canDownload)
{
$down_status = $mediaLocal->downloadMedia($decodedPath, '', '', 0);

if ($down_status === 2)
{
$app->enqueueMessage(Text::_('COM_TJFIELDS_FILE_NOT_FOUND'), 'error');
$app->redirect($this->returnURL);
}

return;
}
else
{
$app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'error');
$app->redirect($this->returnURL);
}
}
else
{
$app->enqueueMessage(Text::_('JERROR_ALERTNOAUTHOR'), 'error');
$app->redirect($this->returnURL);
}

jexit();
}
}
38 changes: 25 additions & 13 deletions administrator/controllers/fields.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
jimport('joomla.filesystem.file');

use Joomla\CMS\MVC\Controller\FormController;
use Joomla\CMS\MVC\Model\BaseDatabaseModel;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Response\JsonResponse;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Factory;

/**
* Item controller class.
Expand All @@ -31,29 +36,36 @@ class TjfieldsControllerFields extends FormController
public function deleteFile()
{
// Check for request forgeries.
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
$app = JFactory::getApplication();
Session::checkToken('get') or Session::checkToken() or jexit(Text::_('JINVALID_TOKEN'));
$app = Factory::getApplication();
$jinput = $app->input;

$data = array();

// Here, fpht means file encoded path
$data['filePath'] = base64_decode($jinput->get('filePath', '', 'BASE64'));
$data['fileName'] = base64_decode($jinput->get('fileName', '', 'BASE64'));
$data['valueId'] = base64_decode($jinput->get('valueId', '', 'BASE64'));
$data['subformFileFieldId'] = $jinput->get('subformFileFieldId');
$data['isSubformField'] = $jinput->get('isSubformField');
$data['client'] = $jinput->get('client', '', 'STRING');

$client = explode('.', $data['client']);
// Get media storage path
JLoader::import('components.com_tjfields.models.fields', JPATH_SITE);
$fieldsModel = BaseDatabaseModel::getInstance('Fields', 'TjfieldsModel', array('ignore_request' => true));
$fieldData = $fieldsModel->getMediaStoragePath($data['valueId'], $data['subformFileFieldId']);

$tjFieldFieldTableParamData = json_decode($fieldData->tjFieldFieldTable->params);
$client = $fieldData->tjFieldFieldTable->client;
$type = $fieldData->tjFieldFieldTable->type;
$uploadPath = isset($tjFieldFieldTableParamData->uploadpath) ? $tjFieldFieldTableParamData->uploadpath : '';
$data['storagePath'] = ($uploadPath != '') ? $uploadPath : JPATH_SITE . '/' . $type . 's/tjmedia/' . str_replace(".", "/", $client . '/');
$data['storagePath'] = str_replace('/', DIRECTORY_SEPARATOR, $data['storagePath']);
$data['client'] = $client;

$data['storagePath'] = '/media/' . $client[0] . '/' . $client[1];
require_once JPATH_ADMINISTRATOR . '/components/com_tjfields/helpers/tjfields.php';

$tjFieldsHelper = new TjfieldsHelper;
$returnValue = $tjFieldsHelper->deleteFile($data);
$msg = $returnValue ? JText::_('COM_TJFIELDS_FILE_DELETE_SUCCESS') : JText::_('COM_TJFIELDS_FILE_DELETE_ERROR');
$msg = $returnValue ? Text::_('COM_TJFIELDS_FILE_DELETE_SUCCESS') : Text::_('COM_TJFIELDS_FILE_DELETE_ERROR');

echo new JResponseJson($returnValue, $msg);
echo new JsonResponse($returnValue, $msg);
}

/**
Expand All @@ -66,9 +78,9 @@ public function deleteFile()
public function getFields()
{
// Check for request forgeries.
(JSession::checkToken() or JSession::checkToken('get')) or jexit(JText::_('JINVALID_TOKEN'));
(Session::checkToken() or Session::checkToken('get')) or jexit(Text::_('JINVALID_TOKEN'));

$app = JFactory::getApplication('administrator');
$app = Factory::getApplication('administrator');
$client = $app->input->get('client', '', 'STRING');

$fieldsModel = parent::getModel("Fields", "TjfieldsModel", array('ignore_request' => true));
Expand All @@ -82,6 +94,6 @@ public function getFields()

$result = $fieldsModel->getItems();

echo new JResponseJson($result);
echo new JsonResponse($result);
}
}
Loading

0 comments on commit 27b90ef

Please sign in to comment.