Skip to content

Commit

Permalink
#24 #55 Added use limit, Reference number and usage log
Browse files Browse the repository at this point in the history
  • Loading branch information
burnacid committed Oct 27, 2017
1 parent 9ee328e commit a0dc957
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 6 deletions.
3 changes: 3 additions & 0 deletions admin/modules/config/formcreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@

$form_container->output_row($lang->fc_allowed_groups." <em>*</em>", $lang->fc_allowed_groups_desc, $radioboxes . "<br /><br />" . $form->
generate_group_select("allowedgid[]", $formcreator->allowedgid, array("multiple" => true)));
$form_container->output_row($lang->fc_limitusage, $lang->fc_limitusage_desc, $form->generate_numeric_field("limitusage", $formcreator->limitusage));
$form_container->output_row($lang->fc_status." <em>*</em>", $lang->fc_status_desc, $form->generate_yes_no_radio("active", $formcreator->active));
$form_container->end();

Expand Down Expand Up @@ -308,6 +309,8 @@

$legend = "<a href='javascript:insertToEditor(\"{\$formname}\");'>".$lang->fc_form_name."</a><br />";
$legend .= $lang->fc_user_info .": <a href='javascript:insertToEditor(\"{\$username}\");'>".$lang->fc_username."</a> | <a href='javascript:insertToEditor(\"{\$uid}\");'>".$lang->fc_id."</a><br /><br />";
$legend .= $lang->fc_other .": <a href='javascript:insertToEditor(\"{\$ref}\");'>".$lang->fc_reference_number."</a><br /><br />";

foreach ($formcreator->fields as $field) {
$legend .= "(ID:" . $field->fieldid . ") " . $field->name . ": ";
$legend .= "<a href='javascript:insertToEditor(\"{\$fieldname[" . $field->fieldid . "]}\");'>".$lang->fc_fieldname."</a> | <a href='javascript:insertToEditor(\"{\$fieldvalue[" .
Expand Down
16 changes: 14 additions & 2 deletions form.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
if ($formcreator->get_form($mybb->input['formid'])) {

if ($formcreator->check_allowed() && $formcreator->active == 1) {

if(!$formcreator->check_usage_limit_reached()){

add_breadcrumb($formcreator->name, "form.php?formid=" . $formcreator->formid);
$display = true;

Expand Down Expand Up @@ -96,10 +99,12 @@
}
} else {
$display = false;
$ref = $formcreator->get_next_ref();

$subject = $formcreator->parse_subject();
$message = $formcreator->parse_output();

$formcreator->log_usage();
$uid = $mybb->user['uid'];
$username = $mybb->user['username'];

Expand Down Expand Up @@ -320,17 +325,24 @@

$formcontent = '<tr><td class="trow1" colspan="2">'.$lang->fc_no_fields.'</td></tr>';
}

}else{
add_breadcrumb($formcreator->name, "form.php?formid=" . $formcreator->formid);

$formtitle = $lang->fc_limit_reached_title;

$formcontent = '<tr><td class="trow1" colspan="2">'.$lang->fc_limit_reached.'</td></tr>';
}
} elseif ($formcreator->active == 0) {
add_breadcrumb($formcreator->name, "form.php?formid=" . $formcreator->formid);

$formtitle = "Form disabled";
$formtitle = $lang->fc_form_disabled_title;

$formcontent = '<tr><td class="trow1" colspan="2">'.$lang->fc_form_disabled.'</td></tr>';
} else {
add_breadcrumb($formcreator->name, "form.php?formid=" . $formcreator->formid);

$formtitle = "Access Denied";
$formtitle = $lang->fc_access_denied;

$formcontent = '<tr><td class="trow1" colspan="2">'.$lang->fc_form_no_permissions.'</td></tr>';
}
Expand Down
72 changes: 69 additions & 3 deletions inc/class_formcreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class formcreator
public $allowedgidtype;
public $allowedgid;
public $allgroups;
public $limitusage;
public $active;
public $pmusers;
public $pmgroups;
Expand Down Expand Up @@ -48,6 +49,10 @@ class formcreator
"Field" => "allowedgid",
"Type" => "text",
"NULL" => 1),
array(
"Field" => "limitusage",
"Type" => "int(11)",
"NULL" => 1),
array(
"Field" => "active",
"Type" => "tinyint(1)",
Expand Down Expand Up @@ -181,7 +186,26 @@ class formcreator
"Field" => "class",
"Type" => "varchar(50)",
"NULL" => 1),
array("Field" => "html", "Type" => "text")));
array("Field" => "html", "Type" => "text")),

"fc_formusage" => array(
array(
"Field" => "formid",
"Type" => "int(11)",
"NULL" => 0),
array(
"Field" => "uid",
"Type" => "int(11)",
"NULL" => 0),
array(
"Field" => "ref",
"Type" => "int(11)",
"NULL" => 0),
array(
"Field" => "datetime",
"Type" => "timestamp",
"NULL" => 0)
));

public $types = array(
1 => "Textbox (single line)",
Expand Down Expand Up @@ -431,6 +455,7 @@ public function escape_data()
$this->name = $db->escape_string($this->name);
$this->allowedgidtype = intval($this->allowedgidtype);
$this->allowedgid = $db->escape_string($this->allowedgid);
$this->limitusage = intval($this->limitusage);
$this->active = intval($this->active);
$this->pmusers = $db->escape_string($this->pmusers);
$this->pmgroups = $db->escape_string($this->pmgroups);
Expand All @@ -454,6 +479,7 @@ public function load_data($data)
$this->name = $data['name'];
$this->allowedgidtype = $data['allowedgidtype'];
$this->allowedgid = $data['allowedgid'];
$this->limitusage = $data['limitusage'];
$this->active = $data['active'];
$this->pmusers = $data['pmusers'];
$this->pmgroups = $data['pmgroups'];
Expand All @@ -480,6 +506,7 @@ public function get_data()
$data['name'] = $this->name;
$data['allowedgidtype'] = $this->allowedgidtype;
$data['allowedgid'] = $this->allowedgid;
$data['limitusage'] = $this->limitusage;
$data['active'] = $this->active;
$data['pmusers'] = $this->pmusers;
$data['pmgroups'] = $this->pmgroups;
Expand Down Expand Up @@ -574,7 +601,7 @@ public function get_field_values()

public function parse_subject()
{
global $templates, $mybb;
global $templates, $mybb, $ref;
if (empty($this->subjecttemplate)) {
return "Form submission: " . $this->name;
} else {
Expand All @@ -591,7 +618,7 @@ public function parse_subject()

public function parse_output()
{
global $db, $mybb;
global $db, $mybb, $ref;
$output = "";
if (empty($this->messagetemplate)) {
foreach ($this->fields as $field) {
Expand Down Expand Up @@ -620,6 +647,45 @@ public function parse_output()

return $output;
}

public function get_next_ref(){
global $db;

$query = $db->simple_select("fc_formusage","*","formid = '".$this->formid."'",array("order_by" => "ref","order_dir" => "DESC", "LIMIT" => 1));
if($db->num_rows($query) != 0){
$lastrow = $db->fetch_array($query);
}else{
$lastrow = 0;
}

return $lastrow['ref'] + 1;
}

public function log_usage()
{
global $db, $mybb;

$data = array("formid" => $this->formid, "uid" => $mybb->user['uid'], "ref" => $this->get_next_ref());

$db->insert_query("fc_formusage",$data);
}

public function check_usage_limit_reached(){
global $db, $mybb;

if($this->limitusage == 0){
return false;
}

$query = $db->simple_select("fc_formusage","*","formid = '".$this->formid."' AND uid = '".$mybb->user['uid']."'");
$timesused = $db->num_rows($query);

if($timesused >= $this->limitusage){
return true;
}else{
return false;
}
}
}

class formcreator_field
Expand Down
4 changes: 4 additions & 0 deletions inc/languages/english/admin/config_formcreator.lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
$l['fc_allow_unselected_groups'] = "Allow all BUT selected groups";
$l['fc_allowed_groups'] = "Allowed Groups";
$l['fc_allowed_groups_desc'] = "Which groups are allowed to use this form";
$l['fc_limitusage'] = "Usage limit";
$l['fc_limitusage_desc'] = "Enter the maximun number of times a user can fill in the form (0 = unlimited).";
$l['fc_status'] = "Status";
$l['fc_status_desc'] = "Is this form active yes or no?";
$l['fc_active'] = "Active";
Expand Down Expand Up @@ -89,6 +91,8 @@

$l['fc_user_info'] = "User Info";
$l['fc_username'] = "Username";
$l['fc_other'] = "Other";
$l['fc_reference_number'] = "Reference Number";
$l['fc_id'] = "ID";
$l['fc_fieldname'] = "Field Name";
$l['fc_fieldvalue'] = "Field Value";
Expand Down
4 changes: 4 additions & 0 deletions inc/languages/english/formcreator.lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
$l['fc_form_disabled'] = "This form has been disabled for use!";
$l['fc_form_no_permissions'] = "You are not allowed to use this form!";
$l['fc_no_form_found'] = "The form you are looking for doesn't exist!";
$l['fc_access_denied'] = "Access Denied";
$l['fc_form_disabled_title'] = "Form Disabled";
$l['fc_limit_reached_title'] = "Form Limit Reached";
$l['fc_limit_reached'] = "You have reached the maximum form usage";

?>
23 changes: 22 additions & 1 deletion inc/plugins/formcreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,14 @@ function formcreator_install()
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
");
}

if (!$db->table_exists('fc_formusage')) {
$db->write_query("CREATE TABLE IF NOT EXISTS `" . TABLE_PREFIX . "fc_formusage` (
".formcreator_generate_table_fields("fc_formusage")."
PRIMARY KEY (`formid`,`uid`,`ref`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
");
}
}

function formcreator_generate_table_fields($table)
Expand Down Expand Up @@ -326,7 +334,7 @@ function formcreator_check_database()

$errors = 0;

if($db->table_exists('fc_fields') && $db->table_exists('fc_forms')){
if($db->table_exists('fc_fields') && $db->table_exists('fc_forms') && $db->table_exists('fc_formusage')){
$query = $db->query("SHOW COLUMNS FROM ".TABLE_PREFIX."fc_forms");

while($row = $db->fetch_array($query)){
Expand All @@ -352,6 +360,18 @@ function formcreator_check_database()
}
}

$query = $db->query("SHOW COLUMNS FROM ".TABLE_PREFIX."fc_formusage");

while($row = $db->fetch_array($query)){
$cols_db[$row['Field']] = $row;
}

foreach($fields['fc_formusage'] as $field){
if($cols_db[$field['Field']]['Type'] != $field['Type']){
$error++;
}
}

if($error == 0){
return array(true);
}else{
Expand Down Expand Up @@ -391,6 +411,7 @@ function formcreator_uninstall()
if (!isset($mybb->input['no'])) {
$db->drop_table('fc_forms');
$db->drop_table('fc_fields');
$db->drop_table('fc_formusage');

// Delete template groups.
$db->delete_query('templategroups', "prefix='formcreator'");
Expand Down

0 comments on commit a0dc957

Please sign in to comment.