Skip to content

Commit

Permalink
Task techjoomla#205 feat : File download and delete functionality fro…
Browse files Browse the repository at this point in the history
…m backend
  • Loading branch information
“Rajnish committed Nov 11, 2019
1 parent fd0afbd commit 8ef667c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 84 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 @@ -515,3 +515,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."
21 changes: 13 additions & 8 deletions administrator/controllers/fields.json.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,28 @@ class TjfieldsControllerFields extends FormController

public function deleteFile()
{
// Check for request forgeries.
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
$app = JFactory::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 = JModelLegacy::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;
Expand Down
95 changes: 19 additions & 76 deletions administrator/helpers/tjfields.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,50 +528,30 @@ public function deleteFile($data)
}

JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_tjfields/tables');
$fieldsValueTable = JTable::getInstance('Fieldsvalue', 'TjfieldsTable');

$fieldsValueTable->load(array('id' => $data['valueId']));
$fieldValueTable = JTable::getInstance('Fieldsvalue', 'TjfieldsTable');
$fieldValueTable->load(array('id' => $data['valueId']));

$subData = new stdClass;
$fieldId = 0;

if ($data['isSubformField'] == 1)
{
$subData = json_decode($fieldsValueTable->value);

foreach ($subData as $value)
{
$subformData = (array) $value;

if (in_array($data['filePath'], $subformData))
{
$fileUser = $fieldsValueTable->user_id;
}
}

// Check for file field is of subform or ucmsubform
if ($data['subformFileFieldId'])
{
$fieldId = $data['subformFileFieldId'];
}
else
{
$fieldId = $fieldsValueTable->field_id;
}
$fileUser = $fieldValueTable->user_id;
$fieldId = $fieldValueTable->field_id;
}
else
{
if ($data['filePath'] === $fieldsValueTable->value)
if ($data['fileName'] === $fieldValueTable->value)
{
$fileUser = $fieldsValueTable->user_id;
$fieldId = $fieldsValueTable->field_id;
$fileUser = $fieldValueTable->user_id;
$fieldId = $fieldValueTable->field_id;
}
}

$fileExtension = StringHelper::strtolower(StringHelper::substr(strrchr($data['filePath'], "."), 1));
$file_extension = strtolower(substr(strrchr($data['fileName'], "."), 1));
$localGetMime = TJMediaStorageLocal::getInstance();

$ctype = $localGetMime->getMime($fileExtension);
$ctype = $localGetMime->getMime($file_extension);

if (!empty($fileUser))
{
Expand All @@ -586,68 +566,31 @@ public function deleteFile($data)
if ($type[0] === 'image')
{
$deleteData = array();
$deleteData[] = JPATH_ROOT . $data['storagePath'] . '/' . $type[0] . '/' . $data['filePath'];

$deleteData[] = JPATH_ROOT . $data['storagePath'] . '/' . $type[0] . '/S_' . $data['filePath'];
$deleteData[] = JPATH_ROOT . $data['storagePath'] . '/' . $type[0] . '/M_' . $data['filePath'];
$deleteData[] = JPATH_ROOT . $data['storagePath'] . '/' . $type[0] . '/L_' . $data['filePath'];
$deleteData[] = $data['storagePath'] . '/' . $data['fileName'];
$deleteData[] = $data['storagePath'] . '/S_' . $data['fileName'];
$deleteData[] = $data['storagePath'] . '/M_' . $data['fileName'];
$deleteData[] = $data['storagePath'] . '/L_' . $data['fileName'];

foreach ($deleteData as $image)
{
if (JFile::exists($image))
{
JFile::delete($image);
if (!JFile::delete($image))
{
return false;
}
}
}

$deleted = 1;
}
else
{
if (!JFile::delete(JPATH_ROOT . $data['storagePath'] . '/' . $type[0] . '/' . $data['filePath']))
if (!JFile::delete($data['storagePath'] . '/' . $data['fileName']))
{
return false;
}
else
{
$deleted = 1;
}
}

if ($deleted == 1)
{
$db = JFactory::getDbo();
$fields_obj = new stdClass;

// Making value object if the field is under subform form subfrom
if ($data['isSubformField'] == 1)
{
foreach ($subData as $subformName => $value)
{
foreach ($value as $k => $v)
{
// Finding the particular index and making it null
if ($v === $data['filePath'])
{
$subData->$subformName->$k = '';
}
}
}

$fields_obj->value = json_encode($subData);
}
else
{
$fields_obj->value = '';
}

$fields_obj->id = $fieldsValueTable->id;
$db->updateObject('#__tjfields_fields_value', $fields_obj, 'id');

return true;
}

return false;
return $fieldValueTable->delete();
}

return false;
Expand Down

0 comments on commit 8ef667c

Please sign in to comment.