Hello All,
I have two parts to my view. The first part is a CGridView that lists uploaded files, and the second part is a few lines of text that give a user info about the total size of all those files, how much storage space they have on the server etc. When I use the default delete button from CButtonColumn it removes a row from the table no problem, I would like it to also refresh the lines of text at the bottom of the page as well.
Is this possible?
My code for actionDelete:
public function actionDelete($id)
{
// we only allow deletion via POST request
if(Yii::app()->request->isPostRequest)
{
$filename = $this->loadModel($id)->file_name;
$f_name = substr($filename, 0, strpos($filename, '.'));
$ext = substr($filename, strpos($filename, '.'));
$file_name_hashed = md5($f_name);
$file_hashed = $file_name_hashed.$ext;
error_log("Trying to delete ".$file_hashed);
$user_id = Yii::app()->user->selectedUser->id;
$folders = scandir(Yii::app()->basePath . '/../assets/Files/');
if(in_array($user_id, $folders)){
$files = scandir(Yii::app()->basePath.'/../assets/Files/'.$user_id);
if(in_array($file_hashed, $files)){
error_log("unlinking");
unlink(Yii::app()->basePath.'/../assets/Files/'.$user_id.'/'.$file_hashed);
}else{
error_log('that file does not exist');
error_log(Yii::app()->basePath.'/../assets/Files/'.$user_id.'/'.$file_hashed);
}
}else{
error_log('user does not have a folder');
}
//this deletes the db entry
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax'])){
error_log("It is an AJAX request!!!!!");
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
}
else
throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
$dataProvider=new CActiveDataProvider('File');
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
My View Code:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'htmlOptions'=>array("width"=>"10"),
'columns'=>array(
array(
'name'=>'File Name',
'value'=>function($data,$row){
return $data->file_name;
},
'type'=>'raw',
'htmlOptions'=>array("width"=>"35%", "align"=>"center"),
),
array(
'name'=>'URL',
'value'=>function($data,$row){
return "<a href= http://$data->file_url>".$data->file_url .'</a>';
},
'type'=>'raw',
'htmlOptions'=>array("width"=>"55%", "align"=>"center")
),
array(
'class'=>'CButtonColumn',
'template'=>'{delete}',
//'footer'=>"the narwhal bacons at midnight",
'deleteButtonImageUrl'=>Yii::app()->request->baseUrl."/images/btn_small_del.png",
'htmlOptions'=>array("width"=>'5%', "align"=>"center"),
),
),
)
);
?>
<?php
$user_id = Yii::app()->user->selectedUser->id;
$remaining_bytes = File::get_remaining_bytes($user_id, Yii::app()->db);
$total_mbytes = Storage::model()->find("storage_user_id=$user_id")->storage_size;
$used_bytes = ($total_mbytes*1024*1024) - $remaining_bytes;
echo "USED: ".round(($used_bytes/1024/1024),1)." MB</br>";
echo "FREE: ".round(($remaining_bytes/1024/1024),1)." MB</br>";
echo "TOTAL: ".$total_mbytes." MB</br>";
$percent_used = round((($used_bytes/($total_mbytes*1024*1024))*100),1);
echo "You have used ". $percent_used . "% of your total file space";
?>
Thank you.