Commit 5fd36917 authored by Chenu Denis's avatar Chenu Denis

[feature] Add mail reminder for group admin

parent e0c359aa
......@@ -232,32 +232,41 @@ $(document).on('click','[data-manageusers]',function(event) {
$("#modal-manageusers").modal('show');
});
$(document).on('click','#modal-manageusers [data-token]',function(event) {
console.log("click");
event.preventDefault();
$(this).closest("tr").find("[data-input]").each(function(e) {
var inputName = $(this).data('input');
var inputValue = $(this).text().trim();
$("#modal-manageusers form input#"+inputName).val(inputValue);
$("#modal-manageusers #update-user-form input#"+inputName).val(inputValue);
});
$("#updatetoken").val($(this).data('token'));
$("#modal-manageusers form").removeClass("hidden");
$("#modal-manageusers #update-user-form").removeClass("hidden");
$('#modal-manageusers .modal-body').animate({
scrollTop: $("#modal-manageusers .modal-body #update-user-form").position().top
}, 500);
});
$(document).on('click','#modal-manageusers [data-mail]',function(event) {
console.log("click");
event.preventDefault();
$("#mailtoken").val($(this).data('mail'));
$("#modal-manageusers #mail-user-form").removeClass("hidden");
$('#modal-manageusers .modal-body').animate({
scrollTop: $("#modal-manageusers .modal-body form").position().top
scrollTop: $("#modal-manageusers .modal-body #mail-user-form").position().top
}, 500);
});
$(document).on("click","#modal-manageusers button:submit",function(event,data){
$(document).on("click","#update-user-form button:submit",function(event,data){
data = $.extend({source:null}, data);
if(data.source == 'control') {
return;
}
event.preventDefault();
var $form = $("#modal-manageusers form")[0];
var $form = $("#update-user-form")[0];
if (!$form.checkValidity()) {
$('#modal-manageusers button:submit').trigger('click',{source:'control'});
$('#update-user-form button:submit').trigger('click',{source:'control'});
return;
}
var url = $("#modal-manageusers form").attr("action");
var params = $("#modal-manageusers form").serializeArray();
var url = $("#update-user-form").attr("action");
var params = $("#update-user-form").serializeArray();
$.ajax({
url : url,
type : 'POST',
......@@ -267,7 +276,7 @@ $(document).on("click","#modal-manageusers button:submit",function(event,data){
.done(function(data) {
data = $.extend({}, {status:'warning'}, data);
if(data.status == "success") {
$("#modal-manageusers form").addClass("hidden");
$("#update-user-form").addClass("hidden");
var htmlAlert = "<div class='alert alert-success'>"+data.html+"</div>";
$("#manageusers-alert").html(htmlAlert);
$('#modal-manageusers .modal-body').animate({
......@@ -277,8 +286,66 @@ $(document).on("click","#modal-manageusers button:submit",function(event,data){
if(htmlTable) {
$("#table-manage-user").replaceWith(htmlTable);
}
$("#modal-manageusers form input.form-control").attr('value','');
$("#modal-manageusers form #updatetoken").attr('value','');
$("#modal-manageusers #update-user-form input.form-control").attr('value','');
$("#modal-manageusers #update-user-form #updatetoken").attr('value','');
return;
}
if(data.html) {
var className = data.status;
if(data.status == 'error') {
className = 'danger';
}
var htmlAlert = "<div class='alert alert-"+className+"'>"+data.html+"</div>";
$("#manageusers-alert").html(htmlAlert);
$('#modal-manageusers .modal-body').animate({
scrollTop: $("#manageusers-alert").position().top
}, 500);
return;
}
})
.fail(function( jqXHR, textStatus ) {
//console.log([jqXHR,textStatus]);
$("#manageusers-alert").html(jqXHR.responseText);
});
});
$(document).on("click","#mail-user-form button:submit",function(event,data){
data = $.extend({source:null}, data);
if(data.source == 'control') {
return;
}
event.preventDefault();
var $form = $("#mail-user-form")[0];
if (!$form.checkValidity()) {
$('#mail-user-form button:submit').trigger('click',{source:'control'});
return;
}
var url = $("#mail-user-form").attr("action");
var params = $("#mail-user-form").serializeArray();
$.ajax({
url : url,
type : 'POST',
data : params,
dataType : 'json'
})
.done(function(data) {
data = $.extend({}, {status:'warning'}, data);
if(data.status == "success") {
$("#mail-user-form").addClass("hidden");
var htmlAlert = "<div class='alert alert-success'>"+data.html+"</div>";
$("#manageusers-alert").html(htmlAlert);
$("#mail-user-form").find("input[type='email'],input:text,textarea").each(function(){
$(this).val("");
if($(this).data("default")) {
$(this).val($(this).data("default"));
}
});
if($("#remindmessage").data("wysihtml5")) {
$("#remindmessage").data("wysihtml5").editor.setValue($("#remindmessage").data("default"));
}
$('#modal-manageusers .modal-body').animate({
scrollTop: $("#manageusers-alert").position().top
}, 500);
return;
}
if(data.html) {
......@@ -299,6 +366,8 @@ $(document).on("click","#modal-manageusers button:submit",function(event,data){
$("#manageusers-alert").html(jqXHR.responseText);
});
});
/* Extra survey system */
$(document).on('click','[target="extra-survey"][data-direct]',function(event) {
event.preventDefault();
......
......@@ -811,12 +811,15 @@ class oecdMenu extends PluginBase
$surveyId=$this->api->getRequest()->getParam('sid');
$token=$this->api->getRequest()->getParam('token');
$this->_exportInitiatives($surveyId,$token);
die("toto");
App()->end(); // Not needed but more clear
case 'adduser' :
$surveyId=$this->api->getRequest()->getParam('sid');
$this->_addUserForSurvey($surveyId);
App()->end(); // Not needed but more clear
case 'mailuser' :
$surveyId=$this->api->getRequest()->getParam('sid');
$this->_emailUserForSurvey($surveyId);
App()->end(); // Not needed but more clear
case 'manageusers' :
$surveyId=$this->api->getRequest()->getParam('sid');
$this->_manageUserForSurvey($surveyId);
......@@ -1420,6 +1423,14 @@ class oecdMenu extends PluginBase
}
}
$manageUsers['attributes'] = $aRegisterAttributes;
/* Mail part */
$manageUsers['actionmail'] = Yii::app()->createUrl("plugins/direct", array('plugin' => get_class(),'sid'=>$surveyId,'currenttoken'=>$oToken->token,'function'=>'mailuser'));
$manageUsers['email'] = array(
'subject' => '',
'message' => '',
'help' => '',
'html' => $oSurvey->htmlemail == "Y",
);
return Yii::app()->controller->renderPartial('oecdMenu.views.modalManageUsers',array('manageUsers'=>$manageUsers,'oUsers'=>$oUsers),1);
}
......@@ -1467,12 +1478,10 @@ class oecdMenu extends PluginBase
/**
* send the email with replacing SURVEYURL by the manage url
*/
private function _sendMail($surveyId,$oToken,$sSubject="",$sBody="",$sMessage="")
private function _sendMail($surveyId,$oToken,$sMessage="", $emailType = 'register', $sSubject="",$sBody="")
{
$emailType = 'register';
$sLanguage = App()->language;
$aSurveyInfo = getSurveyInfo($surveyId, $sLanguage);
$aMail = array();
$aMail['subject'] = $sSubject;
if(trim($sSubject) == "") {
......@@ -1518,7 +1527,14 @@ class oecdMenu extends PluginBase
if(!empty($aReplacementFields["{ADMINNAME}"])) {
$sFrom = $aReplacementFields["{ADMINNAME}"]."<".$aReplacementFields["{ADMINEMAIL}"].">";
}
if(empty($aMail['subject'])) {
$this->mailError ="Subject of message is empty";
return false;
}
if(empty($aMail['message'])) {
$this->mailError ="Body of message is empty";
return false;
}
$sBounce = getBounceEmail($surveyId);
$sTo = $oToken->email;
$sitename = Yii::app()->getConfig('sitename');
......@@ -1642,7 +1658,7 @@ class oecdMenu extends PluginBase
$oToken->generateToken();
$oToken->save();
if(!$this->_sendMail($surveyId,$oToken,App()->getRequest()->getParam('emailsubject'),App()->getRequest()->getParam('emailbody'),App()->getRequest()->getParam('emailmessage'))) {
if(!$this->_sendMail($surveyId,$oToken,App()->getRequest()->getParam('emailmessage'))) {
$html = sprintf("Token created but unable to send the email, token code is %s",CHtml::tag('code',array(),$oToken->token));
$html.= CHtml::tag("hr");
$html.= CHtml::tag("strong",array('class'=>'block'),'Error return by mailer:');
......@@ -1722,6 +1738,74 @@ class oecdMenu extends PluginBase
'tableUser'=> $this->_getTableManageUsers($surveyId,$oCurrentToken),
));
}
/**
* Send a mail to a token user
*/
private function _emailUserForSurvey($surveyId) {
$oSurvey=Survey::model()->findByPk($surveyId);
$aResult = array(
'status' => null,
);
if(!$oSurvey) {
throw new CHttpException(404, 'Invalid survey id.');
}
if(!$this->get('initiativeSurvey', 'Survey', $surveyId)){
throw new CHttpException(403, 'Reminder is disable for this survey.');
return;
}
$tokenAttributeGroup = self::attributeGroup;
$tokenAttributeGroupManager = self::attributeGroupManager;
$token = App()->getRequest()->getParam('currenttoken');
$tokenGroup = null;
$tokenAdmin = null;
$isAdmin = false;
if($token) {
$oToken = Token::model($surveyId)->find("token = :token",array(":token"=>$token));
$tokenGroup = (!empty($tokenAttributeGroup) && !empty($oToken->$tokenAttributeGroup)) ? $oToken->$tokenAttributeGroup : null;
$tokenAdmin = (!empty($tokenAttributeGroupManager) && !empty($oToken->$tokenAttributeGroupManager)) ? $oToken->$tokenAttributeGroupManager : null;
$isAdmin = ((bool) $tokenAdmin) && trim($tokenAdmin) !== '' && trim($tokenAdmin) !== '0';
}
if(!Permission::model()->hasSurveyPermission($surveyId, 'token', 'update')) {
if(!$isAdmin) {
throw new CHttpException(403,'No right to send email in this survey.');
}
}
$tokenExist = false;
$token = Yii::app()->getRequest()->getPost('mailtoken');
if(empty($token)) {
throw new CHttpException(400, 'No user set.');
return;
}
$oToken = null;
if(Permission::model()->hasSurveyPermission($surveyId, 'token', 'update')) {
$oToken = Token::model($surveyId)->find("token = :token",array(":token"=>$token));
}
if($token) {
$oToken = Token::model($surveyId)->find("token = :token AND $tokenAttributeGroup = :group",array(":token"=>$token,":group"=>$tokenGroup));
}
if(empty($oToken)) {
throw new CHttpException(400, 'Invalid user.');
return;
}
if(!$this->_sendMail($surveyId,$oToken,App()->getRequest()->getParam('remindmessage'),'remind')) {
$html = sprintf("Token exist but unable to send the email");
$html.= CHtml::tag("hr");
$html.= CHtml::tag("strong",array('class'=>'block'),'Error return by mailer:');
$html.= CHtml::tag("div",array(),$this->mailError);
$this->_returnJson(array(
'status'=>'warning',
'html' => $html,
));
}
$this->_returnJson(array(
'status'=>'success',
'html' => "We send the message with success",
));
}
private function _updateMyAccount($surveyId) {
$oSurvey=Survey::model()->findByPk($surveyId);
$aResult = array(
......
......@@ -59,7 +59,7 @@
?></li>-->
<li class="form-group">
<?php
$textArea = Chtml::textArea('email[message]',$addUser['email']['message'],array('rows'=>10,'class'=> 'form-control','data-default'=>$addUser['email']['message']));
$textArea = Chtml::textArea('emailmessage',$addUser['email']['message'],array('rows'=>10,'class'=> 'form-control','data-default'=>$addUser['email']['message']));
if($addUser['email']['html']) {
$textArea = Yii::app()->getController()->widget('yiiwheels.widgets.html5editor.WhHtml5Editor', array(
'name' => 'emailmessage',
......
......@@ -20,7 +20,10 @@
<tbody>
<?php foreach($oUsers as $oUser) { ?>
<tr>
<td><a href="#" data-token="<?php echo $oUser->token; ?>"><i class="fa fa-pencil-square hidden-print" aria-hidden="true" title="Edit this user"></i></td>
<td>
<a href="#" data-token="<?php echo $oUser->token; ?>"><i class="fa fa-pencil-square hidden-print" aria-hidden="true" title="Edit this user"></i></a>
<a href="#" data-mail="<?php echo $oUser->token; ?>"><i class="fa fa-envelope-o hidden-print" aria-hidden="true" title="Mail this user"></i></a>
</td>
<td data-input="tokenattribute_firstname"><?php echo CHtml::encode($oUser->firstname); ?></td>
<td data-input="tokenattribute_lastname"><?php echo CHtml::encode($oUser->lastname); ?></td>
<td data-input="tokenattribute_email"><?php echo CHtml::encode($oUser->email); ?></td>
......@@ -31,7 +34,7 @@
<?php } ?>
</tbody>
</table>
<?php echo CHtml::beginForm($manageUsers['action'],'post',array('class'=>'form-horizontal hidden')); ?>
<?php echo CHtml::beginForm($manageUsers['action'],'post',array('class'=>'form-horizontal hidden','id'=>'update-user-form')); ?>
<?php $mandatory = "<i class='fa fa-asterisk text-warning' aria-hidden='true'></i><span class='sr-only'>(Mandatory)</span> "; ?>
<fieldset>
<legend>Manage users</legend>
......@@ -56,6 +59,55 @@
</div>
</div>
<?php echo CHtml::endForm(); ?>
<?php echo CHtml::beginForm($manageUsers['actionmail'],'post',array('class'=>'form-horizontal hidden','id'=>'mail-user-form')); ?>
<fieldset>
<legend>Message</legend>
<input type='hidden' name='mailtoken' id="mailtoken" value=''>
<ul class='list-unstyled'>
<!--<li class="form-group"><?php
echo CHtml::label(gT("Subject"),'remindsubject',array('class'=>"col-sm-4 control-label"))
. CHtml::tag("div",
array('class'=>"col-sm-7"),
CHtml::textField('remindsubject',$manageUsers['email']['subject'],array('class'=>'form-control','data-default'=>$manageUsers['email']['subject']))
);
?></li>-->
<li class="form-group">
<?php
$textArea = Chtml::textArea('remindmessage',$manageUsers['email']['message'],array('rows'=>10,'class'=> 'form-control','data-default'=>$manageUsers['email']['message']));
if($manageUsers['email']['html']) {
$textArea = Yii::app()->getController()->widget('yiiwheels.widgets.html5editor.WhHtml5Editor', array(
'name' => 'remindmessage',
'value' => $manageUsers['email']['message'],
'pluginOptions' => array(
'html' => true,
'lists' => false,
'image'=>false,
'link'=>false,
'useLineBreaks'=>false,// False broke reading …
'autoLink'=>true,
'parserRules'=>'js:wysihtml5ParserRules',
),
'htmlOptions' => array(
'class'=> 'form-control',
'data-default'=>$manageUsers['email']['message'],
'height' => '10em',
),
),true);
}
echo CHtml::label(gT("Message"),'remindmessage',array('class'=>"col-sm-4 control-label")) .
CHtml::tag("div",array('class'=>"col-sm-7"),
$textArea . "<p class='help-block'>".$manageUsers['email']['help']."</p>");
?>
</li>
</ul>
</fieldset>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-8">
<?php echo CHtml::htmlButton("Send",array('type'=>'submit','class'=>"btn btn-primary")); ?>
</div>
</div>
<?php echo CHtml::endForm(); ?>
</div>
<div class="modal-footer">
<?php
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment