From 8ef667c7b78f50b68073adcca2c13a5d4d146c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9CRajnish?= <“rajnish_d@techjoomla.com”> Date: Mon, 11 Nov 2019 18:28:36 +0530 Subject: [PATCH] Task #205 feat : File download and delete functionality from backend --- admin_language/en-GB/en-GB.com_tjfields.ini | 1 + administrator/controllers/fields.json.php | 21 +++-- administrator/helpers/tjfields.php | 95 +++++---------------- 3 files changed, 33 insertions(+), 84 deletions(-) diff --git a/admin_language/en-GB/en-GB.com_tjfields.ini b/admin_language/en-GB/en-GB.com_tjfields.ini index 8820d0e6..7ccd06ce 100755 --- a/admin_language/en-GB/en-GB.com_tjfields.ini +++ b/admin_language/en-GB/en-GB.com_tjfields.ini @@ -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." diff --git a/administrator/controllers/fields.json.php b/administrator/controllers/fields.json.php index 60cb5a02..e205a8c4 100644 --- a/administrator/controllers/fields.json.php +++ b/administrator/controllers/fields.json.php @@ -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; diff --git a/administrator/helpers/tjfields.php b/administrator/helpers/tjfields.php index 405f2e14..99b44fb6 100755 --- a/administrator/helpers/tjfields.php +++ b/administrator/helpers/tjfields.php @@ -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)) { @@ -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;