Skip to content

Commit

Permalink
Dev: added a delete button to delete survey group.
Browse files Browse the repository at this point in the history
  • Loading branch information
LouisGac committed Jul 24, 2017
1 parent 40c146d commit 7c681c5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
22 changes: 17 additions & 5 deletions application/controllers/admin/SurveysGroupsController.php
Expand Up @@ -75,7 +75,7 @@ public function update($id)
}
$aData['model'] = $model;
$oSurveySearch = new Survey('search');
$oSurveySearch->gsid = $model->gsid;
$oSurveySearch->gsid = $model->gsid;

$aData['oSurveySearch'] = $oSurveySearch;
$this->_renderWrappedTemplate('surveysgroups', 'update', $aData);
Expand All @@ -88,11 +88,23 @@ public function update($id)
*/
public function delete($id)
{
$this->loadModel($id)->delete();
$oGroupToDelete = $this->loadModel($id);
$sGroupTitle = $oGroupToDelete->title;

if (! $oGroupToDelete->hasSurveys){
$oGroupToDelete->delete();

// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax'])){
Yii::app()->setFlashMessage( $sGroupTitle .' '. gT("was deleted."),'success');
$this->getController()->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin/survey/sa/listsurveys '));
}

}else{
Yii::app()->setFlashMessage(gT("You can't delete a group if it's not empty!"),'error');
$this->getController()->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin/survey/sa/listsurveys '));
}

// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}

/**
Expand Down
23 changes: 22 additions & 1 deletion application/models/SurveysGroups.php
Expand Up @@ -40,7 +40,7 @@ public function rules()
array('description, created, modified', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('gsid, name, title, description, order, owner_uid, parent_id, created, modified, created_by', 'safe', 'on'=>'search'),
array('gsid, name, title, description, owner_uid, parent_id, created, modified, created_by', 'safe', 'on'=>'search'),
);
}

Expand Down Expand Up @@ -120,6 +120,27 @@ public function getParentTitle()
}
}

public function getHasSurveys()
{
$nbSurvey = Survey::model()->countByAttributes(array("gsid"=>$this->gsid));
return $nbSurvey > 0;
}

/**
* @return string
*/
public function getButtons()
{
$sDeleteUrl = App()->createUrl("admin/surveysgroups/sa/delete", array("id"=>$this->gsid));
$button = '';

if (! $this->hasSurveys){
$button .= '<a class="btn btn-default" href="'.$sDeleteUrl.'" role="button" data-toggle="tooltip" title="'.gT('Delete survey group').'"><span class="fa fa-trash text-danger " ></span><span class="sr-only">'.gT('Delete survey group').'</span></a>';
}

return $button;
}

/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
Expand Down
10 changes: 10 additions & 0 deletions application/views/admin/survey/listSurveys_view.php
Expand Up @@ -86,6 +86,16 @@
'htmlOptions' => array('class' => 'hidden-xs has-link'),
),


array(
'header' => gT('Order'),
'name' => 'order',
'type' => 'raw',
'value'=> '$data->buttons',
'headerHtmlOptions'=>array('class' => 'hidden-xs'),
'htmlOptions' => array('class' => 'hidden-xs has-link'),
),

),

));
Expand Down

0 comments on commit 7c681c5

Please sign in to comment.