Skip to content

Commit

Permalink
New feature: Purge survey files possibly left from erroneous delete
Browse files Browse the repository at this point in the history
  • Loading branch information
olleharstedt committed Sep 29, 2017
1 parent 3737415 commit 5712030
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
3 changes: 2 additions & 1 deletion application/controllers/admin/globalsettings.php
Expand Up @@ -160,7 +160,8 @@ protected function getSurveyFolderStorage($uploaddir, $decimals)
'sizeInBytes' => $size,
'size' => humanFilesize($size, $decimals),
'name' => empty($surveyinfo['name']) ? '(' . gT('deleted') . ')' : $surveyinfo['name'],
'showPurgeButton' => Permission::model()->hasGlobalPermission('superadmin','delete')
'deleted' => empty($surveyinfo['name']),

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Sep 29, 2017

Collaborator

Ou no :). $surveyinfo['name'] can be empty but exist (0 for example but surveyls_title is not mandatory by object: TSV import allo empty string here if i remind . More via object ?

This comment has been minimized.

Copy link
@olleharstedt

olleharstedt Sep 29, 2017

Author Contributor

Really? If you name your survey "0"?

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Sep 29, 2017

Collaborator

See http://php.net/manual/en/function.empty.php «"0" (0 as a string)» :). Maybe there are some user who use 0, i didn't know or use it myself.

BUT if survey didn't exist : $surveyinfo return false then best is to use empty($surveyinfo) (then empty array, null when we move to model or false here :)

This comment has been minimized.

Copy link
@olleharstedt

olleharstedt Sep 29, 2017

Author Contributor

Fixed here: 353521f

This comment has been minimized.

Copy link
@olleharstedt

olleharstedt Sep 29, 2017

Author Contributor

NB: Purge files is a GET request...

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Sep 29, 2017

Collaborator

put a 👎 about GET request ;). Except : if you validate the survey really don't exist ?

OK : not really an issue : 5712030#diff-ae6ef646cdcd6b65eca67ba2a3b15a27R791 :)

This comment has been minimized.

Copy link
@olleharstedt

olleharstedt Sep 29, 2017

Author Contributor

If there's an easy way to make it a POST, feel free to change it. Can't think of anything from the top of my head.

This comment has been minimized.

Copy link
@Shnoulle

Shnoulle Sep 30, 2017

Collaborator

Yep, it's really not an issue :). removing directory that must not exist seems OK :). Then no need post :)

'showPurgeButton' => Permission::model()->hasGlobalPermission('superadmin', 'delete')
&& empty($surveyinfo['name']),
'sid' => $surveyId
);
Expand Down
35 changes: 35 additions & 0 deletions application/controllers/admin/surveyadmin.php
Expand Up @@ -767,6 +767,41 @@ public function delete($iSurveyID)
$this->_renderWrappedTemplate('survey', $aViewUrls, $aData);
}

/**
* Remove files not deleted properly.
* Purge is only available for surveys that were already deleted but for some reason
* left files behind.
* @param int $purge_sid
* @return void
*/
public function purge($purge_sid)
{
$purge_sid = (int) $purge_sid;
if (Permission::model()->hasGlobalPermission('superadmin', 'delete')) {
$survey = Survey::model()->findByPk($purge_sid);
if (empty($survey)) {
$result = rmdirr(Yii::app()->getConfig('uploaddir') . '/surveys/' . $purge_sid);
if ($result) {
Yii::app()->user->setFlash('success', gT('Survey files deleted.'));
} else {
Yii::app()->user->setFlash('error', gT('Error: Could not delete survey files.'));
}
} else {
// Should not be possible.
Yii::app()->user->setFlash(
'error',
gT('Error: Cannot purge files for a survey that is not deleted. Please delete the survey normally in the survey view.')
);
}
} else {
Yii::app()->user->setFlash('error', gT('Access denied'));
}

$this->getController()->redirect(
$this->getController()->createUrl('admin/globalsettings')
);
}

/**
* Takes the edit call from the detailed survey view, which either deletes the survey information
*/
Expand Down
9 changes: 7 additions & 2 deletions application/views/admin/global_settings/_storage_ajax.php
Expand Up @@ -39,15 +39,20 @@
<tr>
<td style='width: 70%;'>
<?php echo $survey['name']; ?>
(<a href="<?php echo $this->createUrl('admin/survey', array('sa' => 'view', 'surveyid' => $survey['sid'])); ?>"><?php echo $survey['sid']; ?></a>)
<?php if ($survey['deleted']): ?>
(<?php echo $survey['sid']; ?>)
<?php else: ?>
(<a href="<?php echo $this->createUrl('admin/survey', array('sa' => 'view', 'surveyid' => $survey['sid'])); ?>"><?php echo $survey['sid']; ?></a>)
<?php endif; ?>
</td>
<td>
<?php echo $survey['size']; ?>
<?php if ($survey['showPurgeButton']): ?>
<span
class='fa fa-trash pull-right btn btn-danger btn-xs'
data-toggle='tooltip'
title='<?php eT('Purge survey'); ?>'
onclick='window.location = "<?php echo $this->createUrl('admin/survey', array('sa' => 'purge', 'purge_sid' => $survey['sid'])); ?>"'
title='<?php eT('Delete survey files'); ?>'
>
</span>
<?php endif; ?>
Expand Down

0 comments on commit 5712030

Please sign in to comment.