Skip to content

Commit

Permalink
New feature #11797: allow update HTML of core message and core form i…
Browse files Browse the repository at this point in the history
…n template

Dev: Adding 2 pstpl file for form and message
Dev: adding a lot of views : some call another, can be improved
Dev: the 2 pstpl file can be easily added (from default) when update (BD)
Dev: or added when import template
  • Loading branch information
Shnoulle committed Oct 17, 2016
1 parent dfcd0be commit 3d21e0b
Show file tree
Hide file tree
Showing 39 changed files with 442 additions and 351 deletions.
98 changes: 53 additions & 45 deletions application/controllers/RegisterController.php
Expand Up @@ -21,7 +21,16 @@
*/
class RegisterController extends LSYii_Controller {

public $layout = 'bare';
/* @var string : Default layout when using render : leave at bare actually : just send content */
public $layout= 'survey';
/* @var string the template name to be used when using layout */
public $sTemplate= 'default';
/* @var string[] Replacement data when use templatereplace function in layout, @see templatereplace $replacements */
public $aReplacementData= array();
/* @var array Global data when use templatereplace function in layout, @see templatereplace $redata */
public $aGlobalData= array();
/* @var boolean did we need survey.pstpl when using layout */
public $bStartSurvey= true;

/**
* The array of errors to be displayed
Expand Down Expand Up @@ -179,9 +188,9 @@ public function getRegisterForm($iSurveyId)
App()->getPluginManager()->dispatchEvent($event);
// Allow adding error or replace error with plugin ?
$this->aRegisterErrors=$event->get('aRegistersErrors');
if(!is_null($event->get('registerForm')))
if(!is_null($event->get('registerForm'))){
return $event->get('registerForm');

}
$aFieldValue=$this->getFieldValue($iSurveyId);
$aRegisterAttributes=$this->getExtraAttributeInfo($iSurveyId);
$aData['iSurveyId'] = $iSurveyId;
Expand All @@ -191,27 +200,20 @@ public function getRegisterForm($iSurveyId)
$aData['sEmail'] = $aFieldValue['sEmail'];
$aData['aAttribute'] = $aFieldValue['aAttribute'];
$aData['aExtraAttributes']=$aRegisterAttributes;
$aData['urlAction']=App()->createUrl('register/index',array('sid'=>$iSurveyId));
$aData['bCaptcha'] = function_exists("ImageCreate") && isCaptchaEnabled('registrationscreen', $aSurveyInfo['usecaptcha']);
$aReplacement['REGISTERFORM']=$this->renderPartial('registerForm',$aData,true);
$aReplacement['REGISTERFORM'] = CHtml::form(App()->createUrl('register/index',array('sid'=>$iSurveyId)),'post',array('id'=>'limesurvey', 'role' => 'form', 'class' => 'ls-form'));
$aReplacement['REGISTERFORM'].= $this->renderPartial('/survey/frontpage/registerForm/form',$aData,true);
$aReplacement['REGISTERFORM'].= CHtml::endForm();
if(is_array($this->aRegisterErrors))
{
$sRegisterError="<div class='alert alert-danger' role='alert'>"
.implode('<br />',$this->aRegisterErrors)
."</div>";
$aReplacement['REGISTERERROR']=$this->renderPartial("/survey/frontpage/registerForm/error",array('aErrors'=>$this->aRegisterErrors),true);
}
else
{
$sRegisterError='';
$aReplacement['REGISTERERROR']='';
}

$aReplacement['REGISTERERROR'] = $sRegisterError;
$aReplacement['REGISTERMESSAGE1'] = gT("You must be registered to complete this survey");
if($sStartDate=$this->getStartDate($iSurveyId))
$aReplacement['REGISTERMESSAGE2'] = sprintf(gT("You may register for this survey but you have to wait for the %s before starting the survey."),$sStartDate)."<br />\n".gT("Enter your details below, and an email containing the link to participate in this survey will be sent immediately.");
else
$aReplacement['REGISTERMESSAGE2'] = gT("You may register for this survey if you wish to take part.")."<br />\n".gT("Enter your details below, and an email containing the link to participate in this survey will be sent immediately.");

$aReplacement['REGISTERMESSAGE1']=$this->renderPartial("/survey/frontpage/registerForm/heading",array(),true);
$aReplacement['REGISTERMESSAGE2']=$this->renderPartial("/survey/frontpage/registerForm/message",array('sStartDate'=>$this->getStartDate($iSurveyId)),true);
$aData['thissurvey'] = $aSurveyInfo;
Yii::app()->setConfig('surveyID',$iSurveyId);//Needed for languagechanger
$aData['languagechanger'] = makeLanguageChangerSurvey(App()->language);
Expand Down Expand Up @@ -318,19 +320,18 @@ public function sendRegistrationEmail($iSurveyId,$iTokenId){
$today = dateShift(date("Y-m-d H:i:s"), "Y-m-d H:i", Yii::app()->getConfig('timeadjust'));
$oToken->sent=$today;
$oToken->save();
$this->sMessage="<div id='wrapper' class='message tokenmessage'>"
. "<p>".gT("Thank you for registering to participate in this survey.")."</p>\n"
. "<p>{$this->sMailMessage}</p>\n"
. "<p>".sprintf(gT("Survey administrator %s (%s)"),$aSurveyInfo['adminname'],$aSurveyInfo['adminemail'])."</p>"
. "</div>\n";
$aMessage=array();
$aMessage['mail-thanks']=gT("Thank you for registering to participate in this survey.");
$aMessage['mail-message']=$this->sMailMessage;
$aMessage['mail-contact']=sprintf(gT("Survey administrator %s (%s)"),$aSurveyInfo['adminname'],$aSurveyInfo['adminemail']);
$this->sMessage=$this->renderPartial('/survey/system/message',array('aMessage'=>$aMessage),true);
}
else
{
$this->sMessage="<div id='wrapper' class='message tokenmessage'>"
. "<p>".gT("Thank you for registering to participate in this survey.")."</p>\n"
. "<p>".gT("You are registered but an error happened when trying to send the email - please contact the survey administrator.")."</p>\n"
. "<p>".sprintf(gT("Survey administrator %s (%s)"),$aSurveyInfo['adminname'],$aSurveyInfo['adminemail'])."</p>"
. "</div>\n";
$aMessage['mail-thanks']=gT("Thank you for registering to participate in this survey.");
$aMessage['mail-message-error']=gT("You are registered but an error happened when trying to send the email - please contact the survey administrator.");
$aMessage['mail-contact']=sprintf(gT("Survey administrator %s (%s)"),$aSurveyInfo['adminname'],$aSurveyInfo['adminemail']);
$this->sMessage=$this->renderPartial('/survey/system/message',array('aMessage'=>$aMessage),true);
}
// Allways return true : if we come here, we allways trye to send an email
return true;
Expand Down Expand Up @@ -461,39 +462,46 @@ public function getStartDate($iSurveyId){
private function display($iSurveyId)
{
$sLanguage=Yii::app()->language;
$aData['surveyid']=$surveyid=$iSurveyId;
$aData['thissurvey']=getSurveyInfo($iSurveyId,$sLanguage);
$sTemplate=getTemplatePath($aData['thissurvey']['template']);
$this->aGlobalData['surveyid']=$surveyid=$iSurveyId;
$this->aGlobalData['thissurvey']=getSurveyInfo($iSurveyId,$sLanguage);
Yii::app()->setConfig('surveyID',$iSurveyId);//Needed for languagechanger
$aData['sitename']=Yii::app()->getConfig('sitename');
$aData['aRegisterErrors']=$this->aRegisterErrors;
$aData['sMessage']=$this->sMessage;
$this->aReplacementData['sitename']=Yii::app()->getConfig('sitename');
$this->aReplacementData['aRegisterErrors']=$this->aRegisterErrors;
$this->aReplacementData['sMessage']=$this->sMessage;

$oTemplate = Template::model()->getInstance('', $iSurveyId);
Yii::app()->clientScript->registerPackage( 'survey-template' );
sendCacheHeaders();
doHeader();
$aViewData['sTemplate']=$sTemplate;
$oTemplate = Template::model()->getInstance('', $iSurveyId);
Yii::app()->clientScript->registerPackage( 'survey-template' );

$this->sTemplate=$oTemplate->name;
if(!$this->sMessage){
$aData['languagechanger']=makeLanguageChangerSurvey($sLanguage); // Only show language changer shown the form is shown, not after submission
$aViewData['content']=self::getRegisterForm($iSurveyId);
$this->aGlobalData['languagechanger']=makeLanguageChangerSurvey($sLanguage); // Only show language changer shown the form is shown, not after submission
$this->aReplacementData['content']=self::getRegisterForm($iSurveyId);
}else{
$aViewData['content']=templatereplace($this->sMessage);
// Must use message.pstpl
$this->aReplacementData['content']=templatereplace(file_get_contents($oTemplate->viewPath . "/message.pstpl"),
array(
'MESSAGEID'=>'register-message',
'ERROR'=>'',
'MESSAGE'=>$this->sMessage,
)
,$this->aGlobalData
);

}
$aViewData['aData']=$aData;
// Test if we come from index or from register
if(empty(App()->clientScript->scripts)){
/* This surely can be moved to layout , but remove iot from surveyController before */
$oTemplate = Template::model()->getInstance('', $iSurveyId);
Yii::app()->clientScript->registerPackage( 'survey-template' );
App()->getClientScript()->registerPackage('jqueryui');
App()->getClientScript()->registerPackage('jquery-touch-punch');
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('generalscripts')."survey_runtime.js");
useFirebug();
$this->render('/register/display',$aViewData);
$this->layout='survey';
$this->render('/survey/system/display',$this->aReplacementData);
}else{
// Survey/index need renderPartial
echo $this->renderPartial('/register/display',$aViewData, true, true);
echo $this->render('/survey/system/display',$this->aReplacementData, true, true);
}
doFooter();
}
}
85 changes: 42 additions & 43 deletions application/helpers/frontend_helper.php
Expand Up @@ -265,17 +265,6 @@ function makeLanguageChanger($sSelectedLanguage)
'sClass' => $sClass ,
);
$sHTMLCode = Yii::app()->getController()->renderPartial('/surveys/LanguageChangerForm', $languageChangerDatas, true);

//~ $sHTMLCode= CHtml::beginForm(App()->createUrl('surveys/publiclist'),'get', array('class' => 'form-horizontal'));
//~ $sHTMLCode = Yii::app()->getController()->renderPartial('/survey/system/LanguageChanger/LanguageChanger', $languageChangerDatas, true);
//~ $sHTMLCode.=CHtml::label(gT("Language:"), 'lang',array('class'=>'control-label col-xs-4 col-sm-8'));
//~ $sHTMLCode .= "<div class='col-xs-7 col-sm-2'>";
//~ $sHTMLCode.= CHtml::dropDownList('lang', $sSelected,$aListLang,array('class'=>$sClass));
//~ $sHTMLCode .= "</div>";
//~ $sHTMLCode .= "<div class='col-xs-1 col-sm-2'>";
//~ $sHTMLCode.="<button class='changelang jshide' value='changelang' id='changelangbtn' type='submit'>".gT("Change the language")."</button>";
//~ $sHTMLCode .= "</div>";
//~ $sHTMLCode.= CHtml::endForm();
return $sHTMLCode;
}
else
Expand Down Expand Up @@ -905,6 +894,7 @@ function buildsurveysession($surveyid,$preview=false)
$sTemplatePath = $_SESSION['survey_'.$surveyid]['templatepath'];

$oTemplate = Template::model()->getInstance('', $surveyid);
App()->getController()->sTemplate=$oTemplate->name;
$sTemplatePath = $oTemplate->path;
$sTemplateViewPath = $oTemplate->viewPath;

Expand Down Expand Up @@ -940,7 +930,6 @@ function buildsurveysession($surveyid,$preview=false)
} else {
$oTokenEntry = Token::model($surveyid)->usable()->incomplete()->findByAttributes(array('token' => $clienttoken));
}

$subscenarios['tokenValid'] = ((!empty($oTokenEntry) && ($clienttoken != "")));
}
else
Expand All @@ -952,21 +941,23 @@ function buildsurveysession($surveyid,$preview=false)
if($scenarios['captchaRequired'])
{
//Check if the Captcha was correct
$loadsecurity = returnGlobal('loadsecurity',true);
$captcha = Yii::app()->getController()->createAction('captcha');
$subscenarios['captchaCorrect'] = $captcha->validate($loadsecurity, false);
$subscenarios['captchaCorrect'] = $captcha->validate(App()->getRequest()->getPost('loadsecurity'), false);
}
else
{
$subscenarios['captchaCorrect'] = true;
$loadsecurity = false;
}


//RenderWay defines which html gets rendered to the user_error
// Possibilities are main,register,correct
$renderCaptcha = "";
$renderToken = "";

/**
* @todo : create 2 new function to create and call form
*/
//Define array to render the partials
$aEnterTokenData = array();
$aEnterTokenData['bNewTest'] = false;
Expand All @@ -985,19 +976,28 @@ function buildsurveysession($surveyid,$preview=false)
$aEnterTokenData['sLoadpass'] = htmlspecialchars($loadpass);
}

$FlashError = "";
$aEnterErrors=array();
// Scenario => Token required
if ($scenarios['tokenRequired'] && !$preview){
//Test if token is valid
list($renderToken, $FlashError) = testIfTokenIsValid($subscenarios, $thissurvey, $aEnterTokenData, $clienttoken);
if(!empty($FlashError)){
$aEnterErrors['token']=$FlashError;
}
}

// Scenario => Captcha required
if($scenarios['captchaRequired'] && !$preview) {
$FlashError = '';

//Apply the captchaEnabled flag to the partial
//Apply the captcYii::app()->getRequest()->getPost($id);haEnabled flag to the partial
$aEnterTokenData['bCaptchaEnabled'] = true;
// IF CAPTCHA ANSWER IS NOT CORRECT OR NOT SET
if (!$subscenarios['captchaCorrect']) {
if ($loadsecurity) {
// was a bad answer
$FlashError.=gT("Your answer to the security question was not correct - please try again.")."<br/>\n";
if(App()->getRequest()->getPost('loadsecurity')){
$aEnterErrors['captcha']=gT("Your answer to the security question was not correct - please try again.");
}elseif(null!==App()->getRequest()->getPost('loadsecurity')){
$aEnterErrors['captcha']=gT("Your must answer to the security question - please try again.");
}
$renderCaptcha='main';
}
Expand All @@ -1007,19 +1007,14 @@ function buildsurveysession($surveyid,$preview=false)
}
}

// Scenario => Token required
if ($scenarios['tokenRequired'] && !$preview){
//Test if token is valid
list($renderToken, $FlashError) = testIfTokenIsValid($subscenarios, $thissurvey, $aEnterTokenData, $clienttoken);
}

//If there were errors, display through yii->FlashMessage
if($FlashError !== ""){
$aEnterTokenData['errorMessage'] = $FlashError;
}

$aEnterTokenData['aEnterErrors']=$aEnterErrors;
$renderWay = getRenderWay($renderToken, $renderCaptcha);
$redata = compact(array_keys(get_defined_vars()));
/* This funtion end if an form need to be shown */
renderRenderWayForm($renderWay, $redata, $scenarios, $sTemplateViewPath, $aEnterTokenData, $surveyid);

// Reset all the session variables and start again
Expand Down Expand Up @@ -1564,12 +1559,7 @@ function testIfTokenIsValid(array $subscenarios, array $thissurvey, array $aEnte
}
else
{ //token was wrong
$errorMsg= ""
. gT("The token you have provided is either not valid, or has already been used.")."<br /><br />\n"
. sprintf( gT("For further information please contact %s"), $thissurvey['adminname'])
. "(<a href='mailto:".$thissurvey['adminemail']."'>"
. $thissurvey['adminemail']."</a>)";

$errorMsg= gT("The token you have provided is either not valid, or has already been used.");
$FlashError .= $errorMsg;

$renderToken='main';
Expand Down Expand Up @@ -1633,26 +1623,35 @@ function renderRenderWayForm($renderWay, array $redata, array $scenarios, $sTemp
{
switch($renderWay){
case "main": //Token required, maybe Captcha required
sendCacheHeaders();
doHeader();
echo templatereplace(file_get_contents($sTemplateViewPath."startpage.pstpl"),array(),$redata,'frontend_helper[875]');
echo templatereplace(file_get_contents($sTemplateViewPath."survey.pstpl"),array(),$redata,'frontend_helper[877]');

App()->getController()->layout="survey";
App()->getController()->aReplacementData=$aEnterTokenData;
App()->getController()->bStartSurvey=true;
// render token form

if($scenarios['tokenRequired']){
App()->getController()->renderPartial('/survey/frontpage/enterToken', $aEnterTokenData);
$aReplacements['FORMID'] = 'token';
} else {
App()->getController()->renderPartial('/survey/frontpage/enterCaptcha', $aEnterTokenData);
$aReplacements['FORMID'] = 'captcha';
}

echo templatereplace(file_get_contents($sTemplateViewPath."endpage.pstpl"),array(),$redata,'frontend_helper[1645]');
doFooter();
$aReplacements['FORMHEADING'] = App()->getController()->renderPartial("/survey/frontpage/{$aReplacements['FORMID']}Form/heading",$aEnterTokenData,true);
$aReplacements['FORMMESSAGE'] = App()->getController()->renderPartial("/survey/frontpage/{$aReplacements['FORMID']}Form/message",$aEnterTokenData,true);
$aReplacements['FORMERROR'] = App()->getController()->renderPartial("/survey/frontpage/{$aReplacements['FORMID']}Form/error",$aEnterTokenData,true);

$aReplacements['FORM'] = CHtml::beginForm(array("/survey/index","sid"=>$surveyid), 'post',array('id'=>'form-'.$aReplacements['FORMID'],'class'=>'ls-form'));
$aReplacements['FORM'].= App()->getController()->renderPartial("/survey/frontpage/{$aReplacements['FORMID']}Form/form",$aEnterTokenData,true);
/* @ todo : some hidden field must be moved here : in controller */
$aReplacements['FORM'].= CHtml::endForm();
$content = templatereplace(file_get_contents($sTemplateViewPath."form.pstpl"),$aReplacements,$aData);
App()->getController()->render("/survey/system/display",array(
'content'=>$content,
));
Yii::app()->end();
break;
case "register": //Register new user
// Add the event and test if done
Yii::app()->runController("register/index/sid/{$surveyid}");
Yii::app()->end();
/* We never get here */
echo templatereplace(file_get_contents($sTemplateViewPath."register.pstpl"),array(),$redata,'frontend_helper[1751]');
break;
case "correct": //Nothing to hold back, render survey
Expand Down

0 comments on commit 3d21e0b

Please sign in to comment.