Skip to content
Snippets Groups Projects
Commit e4e0be50 authored by Tim Repke's avatar Tim Repke
Browse files

registration via form works againgit status

parent bf312166
No related branches found
No related tags found
No related merge requests found
...@@ -56,8 +56,13 @@ class Environment { ...@@ -56,8 +56,13 @@ class Environment {
* 0 = registration open (slots available) * 0 = registration open (slots available)
* 1 = all slots taken -> waitlist open * 1 = all slots taken -> waitlist open
* 2 = registration closed! * 2 = registration closed!
*
* @param $fid (optional) to pass this parameter
* @returns int the state of the trip (see above)
*/ */
public function getRegistrationState($fid) { public function getRegistrationState($fid = NULL) {
if(is_null($fid)) $fid = $this->getSelectedTripId();
comm_verbose(3,"checking if fid ". $fid . " is open"); comm_verbose(3,"checking if fid ". $fid . " is open");
$open = $this->database->has('fahrten', ['AND' => ['fahrt_id'=>$fid, 'regopen'=>1]]); $open = $this->database->has('fahrten', ['AND' => ['fahrt_id'=>$fid, 'regopen'=>1]]);
if(!$open) if(!$open)
...@@ -84,7 +89,7 @@ class Environment { ...@@ -84,7 +89,7 @@ class Environment {
*/ */
public function getSelectedTripId() { public function getSelectedTripId() {
if(isset($_REQUEST['fid'])) if(isset($_REQUEST['fid']))
return $_REQUEST['fid']; return (int) $_REQUEST['fid'];
else else
return null; return null;
} }
...@@ -160,4 +165,69 @@ class Environment { ...@@ -160,4 +165,69 @@ class Environment {
'studityp' => "", 'comment'=>"" 'studityp' => "", 'comment'=>""
]; ];
} }
/**
* @param $data
* @return bool
*/
public function sendBachelorToDB($data) {
// === prepare data to insert ===
$data['anm_time'] = time();
$data['anday'] = date('Y-m-d', DateTime::createFromFormat('d.m.Y',$data['anday'])->getTimestamp());
$data['abday'] = date('Y-m-d', DateTime::createFromFormat('d.m.Y',$data['abday'])->getTimestamp());
if($this->isInWaitlistMode()){
if($this->getRegistrationState($data['fahrt_id'])==1){
// === prepare data to insert ===
$data['waitlist_id'] = comm_generate_key($this->database,
["bachelor" => "bachelor_id", "waitlist" => "waitlist_id"],
['fahrt_id'=>$data['fahrt_id']]);
// === insert data ===
$this->database->insert("waitlist", $data);
// === notify success ===
$this->feedbackHelper("lang_waitmail", $data['mehl'], $data['waitlist_id'], $data['fahrt_id']);
}
else return false;
} else {
// === prepare data to insert ===
$data['version'] = 1;
$data['bachelor_id'] = comm_generate_key($this->database,
["bachelor" => "bachelor_id"],
['fahrt_id'=>$data['fahrt_id']]);
// === check regstration full ===
$res = $this->database->get("fahrten",
["regopen", "max_bachelor"],
["fahrt_id" => $data['fahrt_id']]);
if (!$res || $res['regopen'] != "1")
return false;
$this->database->exec("LOCK TABLES fahrten, bachelor WRITE"); // count should not be calculated in two scripts at once
$insertOk = $this->isRegistrationOpen($data['fahrt_id']);
if ($insertOk)
$this->database->insert("bachelor", $data);
$this->database->exec("UNLOCK TABLES"); // insert is done now, count may be recalculated in other threads
if (!$insertOk)
return false; // full
// === notify success ===
$this->feedbackHelper("lang_regmail", $data['mehl'], $data['bachelor_id'], $data['fahrt_id']);
}
return true;
}
private function feedbackHelper($mail_lang, $to, $hash, $fid) {
global $config_baseurl;
$from = $this->database->get("fahrten", ["kontakt","leiter"], ["fahrt_id"=>$fid]);
$mail = comm_get_lang($mail_lang, array( "{{url}}" => $config_baseurl."status.php?hash=".$hash,
"{{organisator}}" => $from['leiter']));
comm_send_mail($this->database, $to, $mail, $from['kontakt']);
}
} }
\ No newline at end of file
...@@ -19,10 +19,9 @@ class soft_protect ...@@ -19,10 +19,9 @@ class soft_protect
{ {
array_push($elems, "'".addslashes($protectkey)."'"); array_push($elems, "'".addslashes($protectkey)."'");
} }
array_push($lines, "soft_protect([".implode(",", $elems)."], ".$element[1].");"); array_push($lines, "FAPI.attachSoftProtector([".implode(",", $elems)."], ".$element[1].");");
} }
array_push($lines, "</script>"); array_push($lines, "</script>");
return implode("\r\n", $lines); return implode("\r\n", $lines);
} }
} }
?> \ No newline at end of file
...@@ -66,6 +66,10 @@ function index_get_dependencies_helper($dependency_function_name) { ...@@ -66,6 +66,10 @@ function index_get_dependencies_helper($dependency_function_name) {
} }
function index_get_additional_headers() { function index_get_additional_headers() {
$signup_method = SignupMethods::getInstance();
if($signup_method->signupMethodExists()){
return $signup_method->getActiveMethod()->getAdditionalHeader();
}
return ''; return '';
} }
...@@ -135,15 +139,16 @@ function index_show_signup() { ...@@ -135,15 +139,16 @@ function index_show_signup() {
$sub = $signup_method->validateSubmission(); $sub = $signup_method->validateSubmission();
if ($sub['valid']) { if ($sub['valid']) {
if (index_form_to_db($sub['data'])) if ($environment->sendBachelorToDB($sub['data']))
header("Location: ?fid=".$fid."&success"); header("Location: ?fid=".$fid."&success");
else else
header("Location: ?fid=".$fid."&full"); header("Location: ?fid=".$fid."&full");
die(); die();
} else { } else {
//TODO include that behaviour: //TODO include that behaviour:
// index_show_errors($errors); index_show_errors($sub['errors']);
// index_show_formular($fid, NULL, $data); $environment->setDanglingFormData($sub['data']);
$signup_method->getFallbackMethod()->showInlineHTML();
} }
} }
...@@ -189,7 +194,17 @@ function index_show_signup() { ...@@ -189,7 +194,17 @@ function index_show_signup() {
echo '</div>'; // close signup-container echo '</div>'; // close signup-container
} }
/**
* puts out a list of all errors
* @param $errors
*/
function index_show_errors($errors){
echo '<div class="message error"><ul>';
foreach($errors as $e){
echo '<li>'.$e.'</li>';
}
echo'</ul></div>';
}
......
...@@ -21,9 +21,11 @@ describe("Bachelor prototype behaviour", function() { ...@@ -21,9 +21,11 @@ describe("Bachelor prototype behaviour", function() {
it("should handle single property setting/getting", function() { it("should handle single property setting/getting", function() {
expect(bachelor.getValue('forname')).toBe(null); expect(bachelor.getValue('forname')).toBe(null);
expect(bachelor.isSet('forname')).toBe(false);
bachelor.setValue('forname', 'Franz'); bachelor.setValue('forname', 'Franz');
expect(bachelor.getValue('forname')).toBe('Franz'); expect(bachelor.getValue('forname')).toBe('Franz');
expect(bachelor.isSet('forname')).toBe(true);
expect(bachelor.getProperties()).toEqual({ expect(bachelor.getProperties()).toEqual({
'forname': 'Franz', 'forname': 'Franz',
...@@ -65,6 +67,22 @@ describe("Bachelor prototype behaviour", function() { ...@@ -65,6 +67,22 @@ describe("Bachelor prototype behaviour", function() {
'studityp':null, 'studityp':null,
'comment': null 'comment': null
}); });
expect(bachelor.isComplete()).toBe(false);
bachelor.setValues({
'anday': 'bla',
'abday': 'bla',
'antyp': 'bla',
'abtyp': 'bla',
'pseudo': 'bla',
'mehl': 'bla',
'essen': 'bla',
'public': 'bla',
'studityp':'bla',
'comment': 'bla'
});
expect(bachelor.isComplete()).toBe(true);
}); });
it("should revoke setting/getting invalid attributes", function() { it("should revoke setting/getting invalid attributes", function() {
...@@ -76,6 +94,10 @@ describe("Bachelor prototype behaviour", function() { ...@@ -76,6 +94,10 @@ describe("Bachelor prototype behaviour", function() {
bachelor.resetValue('illegalAttribute') bachelor.resetValue('illegalAttribute')
}).toThrow(); }).toThrow();
expect(function () {
bachelor.isSet('illegalAttribute')
}).toThrow();
expect(function () { expect(function () {
bachelor.setValues({ bachelor.setValues({
illegalAttribute: 'bla', illegalAttribute: 'bla',
......
...@@ -21,6 +21,10 @@ var UrlComponents = { ...@@ -21,6 +21,10 @@ var UrlComponents = {
})(q.split("&")); })(q.split("&"));
} }
}, },
isSet: function(param) {
var tmp = this.getParams();
return tmp && param in tmp;
},
getValueOf: function(param) { getValueOf: function(param) {
return this.getParams()[param]; return this.getParams()[param];
} }
...@@ -53,7 +57,15 @@ function Bachelor() { ...@@ -53,7 +57,15 @@ function Bachelor() {
}; };
this.isSet = function (attribute) { this.isSet = function (attribute) {
return !!properties[attribute]; if(! (attribute in properties)) throw new Error("Attribute does not exist!");
return properties[attribute] !== null;
};
this.isComplete = function () {
for (var key in properties) {
if(!this.isSet(key)) return false;
}
return true;
}; };
this.setValue = function (attribute, value) { this.setValue = function (attribute, value) {
...@@ -85,6 +97,8 @@ function Bachelor() { ...@@ -85,6 +97,8 @@ function Bachelor() {
function FAPI() { function FAPI() {
this.data = new Bachelor(); this.data = new Bachelor();
this.captcha = '';
this.methodBasepath = 'view/signups/' + UrlComponents.getValueOf('method') + '/'; this.methodBasepath = 'view/signups/' + UrlComponents.getValueOf('method') + '/';
} }
...@@ -93,63 +107,49 @@ FAPI.prototype.resolvePath = function(file) { ...@@ -93,63 +107,49 @@ FAPI.prototype.resolvePath = function(file) {
}; };
/** /**
* params to send have to be set earlier (see the Bachelor) * Submit the registration send-and-pray style!
* *
* callback is called, once the request was sent to the server. * All error handling will be done by the simple form (if needed).
* It'll transmit an object containing the
* -> state (0 = successful, 1 = error, 2 = ?)
* -> main message (string)
* -> errors (null, if non; array of strings with messages else)
* @param callback
*/ */
FAPI.prototype.submitSignup = function(callback) { FAPI.prototype.submitSignup = function() {
// TODO evaluate params and send them
callback({
state: 0,
messages: 'Successful signup',
errors: null
})
};
/**
* Helper function for FAPI.prototype.submitSignup
* it creates a hidden form to be submitted.
*/
FAPI.prototype.prepareSubmission = function () {
var formWrapper = $('<div style="display:none"/>'); var formWrapper = $('<div style="display:none"/>');
var form = $('<form name="storySubmitForm" method="POST"/>'); var form = $('<form name="storySubmitForm" method="POST"/>');
formWrapper.append(form); formWrapper.append(form);
$('#signup-container').append(formWrapper); $('#signup-container').append(formWrapper);
function formAppendText (name, value) {
form.append('<input name="' + name + '" value="' + value.replace(/[\r\n]/g, "<br/>").replace(/&/g, "&amp;").replace(/"/g, "&quot;") + '"/>'); var data = this.data.getProperties();
for (var key in data) {
var value = data[key];
var leaveOut = false;
switch (key) {
case 'public':
if(value === true) value = 'public';
else leaveOut = true;
break;
default: /* does nothing */
}
if(!leaveOut) {
value = value.replace(/[\r\n]/g, "<br/>").replace(/&/g, "&amp;").replace(/"/g, "&quot;");
addToForm(key, value);
}
} }
if(window.location.pathname.search("waitlist")>0) addToForm('captcha', this.captcha);
formAppendText("waitlist", "waitlist"); addToForm('storySubmit', 'storySubmit');
formAppendText('forname', story.form_variables.forname); if(UrlComponents.isSet('waitlist'))
formAppendText('sirname', story.form_variables.name); addToForm('waitlist', 'waitlist');
formAppendText('pseudo', story.form_variables.anzeig);
formAppendText('mehl', story.form_variables.mehl);
formAppendText('studityp', $('#story_summary_studityp').val());
formAppendText('virgin', Story.ageMap[story.form_variables.age]);
formAppendText('essen', Story.eatMapPhp[Story.eatMap[story.form_variables.eat]]);
formAppendText('anday', story.form_variables.travelStartDate);
formAppendText('antyp', Story.travelMapPhp[Story.travelMap[story.form_variables.travelStartType]]);
formAppendText('abday', story.form_variables.travelEndDate);
formAppendText('abtyp', Story.travelMapPhp[Story.travelMap[story.form_variables.travelEndType]]);
formAppendText('comment', $('#story_summary_comment').val());
if ($('#story_summary_public').is(':checked'))
formAppendText('public', 'public');
formAppendText('captcha', $('#story_summary_captcha').val());
formAppendText('storySubmit', 'storySubmit');
form.submit(); form.submit();
function addToForm(key, value) {
form.append('<input name="' + key + '" value="' + value + '"/>');
}
}; };
FAPI.prototype.attachSoftProtector = function (elementIds, regex) { FAPI.attachSoftProtector = function (elementIds, regex) {
for(var i = 0; i < elementIds.length; ++i) { for(var i = 0; i < elementIds.length; ++i) {
$('#'+elementIds[i]).keyup(function(event) { $('#'+elementIds[i]).keyup(function(event) {
if (!event.target.value.match(regex)) if (!event.target.value.match(regex))
......
...@@ -52,9 +52,9 @@ class FormSignupMethod extends SignupMethod { ...@@ -52,9 +52,9 @@ class FormSignupMethod extends SignupMethod {
echo '<div id="stylized" class="myform"> echo '<div id="stylized" class="myform">
<form id="form" name="form" method="post" action="index.php' . $link_params . '">'; <form id="form" name="form" method="post" action="index.php' . $link_params . '">';
$this->show_formular_helper_input("Vorname", "forname", $bachelor["forname"], ""); $this->show_formular_helper_input("Vorname", "forname", $bachelor["forname"], "");
$this->show_formular_helper_input("Nachname", "sirname", $bachelor["sirname"], ""); $this->show_formular_helper_input("Nachname", "sirname", $bachelor["sirname"], "");
$this->show_formular_helper_input("Anzeigename", "pseudo", $bachelor["pseudo"], ""); $this->show_formular_helper_input("Anzeigename", "pseudo", $bachelor["pseudo"], "");
echo $soft_prot->add(array('forname', 'sirname', 'pseudo'), $environment->config['invalidChars'])->write(); echo $soft_prot->add(array('forname', 'sirname', 'pseudo'), $environment->config['invalidChars'])->write();
$this->show_formular_helper_input("E-Mail-Adresse", "mehl", $bachelor["mehl"], "regelmäßig lesen!"); $this->show_formular_helper_input("E-Mail-Adresse", "mehl", $bachelor["mehl"], "regelmäßig lesen!");
......
...@@ -52,7 +52,7 @@ ...@@ -52,7 +52,7 @@
margin-left:150px; margin-left:150px;
width:125px; width:125px;
height:31px; height:31px;
background:#666666 url(img/button.png) no-repeat; background:#666666;
text-align:center; text-align:center;
line-height:31px; line-height:31px;
color:#FFFFFF; color:#FFFFFF;
......
...@@ -6,6 +6,7 @@ class SignupMethods { ...@@ -6,6 +6,7 @@ class SignupMethods {
private static $__instance = NULL; private static $__instance = NULL;
private $signup_methods = []; private $signup_methods = [];
private $fallback_method = 'form';
public static function getInstance() { public static function getInstance() {
if(self::$__instance == NULL) self::$__instance = new SignupMethods(); if(self::$__instance == NULL) self::$__instance = new SignupMethods();
...@@ -49,6 +50,11 @@ class SignupMethods { ...@@ -49,6 +50,11 @@ class SignupMethods {
} }
} }
public function getFallbackMethod() {
$method = $this->getMethodObj($this->fallback_method);
return new $method['class']();
}
/** /**
* @return class (instantiated) of the active signup method * @return class (instantiated) of the active signup method
* @throws ErrorException when $_GET["method"] is missing or not available in the list * @throws ErrorException when $_GET["method"] is missing or not available in the list
...@@ -73,9 +79,11 @@ class SignupMethods { ...@@ -73,9 +79,11 @@ class SignupMethods {
*/ */
private function getActiveMethodObj() { private function getActiveMethodObj() {
if(!isset($_REQUEST["method"])) throw new ErrorException("No signup-method selected!"); if(!isset($_REQUEST["method"])) throw new ErrorException("No signup-method selected!");
$mode = $_REQUEST["method"]; return $this->getMethodObj($_REQUEST["method"]);
if(!isset($this->signup_methods[$mode])) throw new ErrorException("Signup-method does not exist!"); }
private function getMethodObj($mode) {
if(!isset($this->signup_methods[$mode])) throw new ErrorException("Signup-method does not exist!");
return [ 'id' => $mode, 'class' => $this->signup_methods[$mode]['class']]; return [ 'id' => $mode, 'class' => $this->signup_methods[$mode]['class']];
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment