Skip to content

Commit 8244ca8

Browse files
committed
2.1.0; closes #7, closes #8
1 parent 92531db commit 8244ca8

File tree

14 files changed

+663
-424
lines changed

14 files changed

+663
-424
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.svn*
1+
.DS_Store
2+
.idea

add.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@
2525
);
2626

2727
$page_vars["head_js"] =<<< EOF
28+
var check_field_identifier = function () {
29+
var field = $("#field_identifier");
30+
if (/[^a-zA-Z0-9_]/g.test(field.val())) {
31+
return [[field, "{$L["validation_invalid_field_identifier"]}"]];
32+
}
33+
return true;
34+
}
35+
2836
var rules = [];
2937
rules.push("required,template_hook,{$L["validation_no_template_hook"]}");
3038
rules.push("required,field_label,{$L["validation_no_field_label"]}");
3139
rules.push("required,field_type,{$L["validation_no_field_type"]}");
40+
rules.push("function,check_field_identifier");
3241
$(function() {
3342
ecf_ns.add_field_option(null, null);
3443
ecf_ns.add_field_option(null, null);

code/Fields.class.php

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@ public static function addField($info, $L)
2929

3030
$db->query("
3131
INSERT INTO {PREFIX}module_extended_client_fields (template_hook, admin_only, field_label, field_type,
32-
option_source, option_list_id, field_orientation, default_value, is_required, error_string, field_order)
33-
VALUES (:template_hook, :admin_only, :field_label, :field_type, :option_source, :option_list_id, :field_orientation,
34-
:default_value, :is_required, :error_string, :list_order)
32+
field_identifier, option_source, option_list_id, field_orientation, default_value, is_required,
33+
error_string, field_order)
34+
VALUES (:template_hook, :admin_only, :field_label, :field_type, :field_identifier, :option_source,
35+
:option_list_id, :field_orientation, :default_value, :is_required, :error_string, :list_order)
3536
");
3637
$db->bindAll(array(
3738
"template_hook" => $info["template_hook"],
3839
"admin_only" => $info["admin_only"],
3940
"field_label" => $info["field_label"],
4041
"field_type" => $info["field_type"],
42+
"field_identifier" => $info["field_identifier"],
4143
"option_source" => $info["option_source"],
4244
"option_list_id" => $option_list_id,
4345
"field_orientation" => $info["field_orientation"],
@@ -95,6 +97,7 @@ public static function updateField($client_field_id, $info, $L)
9597
admin_only = :admin_only,
9698
field_label = :field_label,
9799
field_type = :field_type,
100+
field_identifier = :field_identifier,
98101
option_source = :option_source,
99102
option_list_id = :option_list_id,
100103
field_orientation = :field_orientation,
@@ -109,6 +112,7 @@ public static function updateField($client_field_id, $info, $L)
109112
"admin_only" => $info["admin_only"],
110113
"field_label" => $info["field_label"],
111114
"field_type" => $info["field_type"],
115+
"field_identifier" => $info["field_identifier"],
112116
"option_source" => $option_source,
113117
"option_list_id" => $option_list_id,
114118
"field_orientation" => $info["field_orientation"],
@@ -156,13 +160,13 @@ public static function updateField($client_field_id, $info, $L)
156160
}
157161

158162

159-
/**
160-
* Returns a page (or all) client fields.
161-
*
162-
* @param integer $page_num
163-
* @param array $search a hash whose keys correspond to database column names
164-
* @return array
165-
*/
163+
/**
164+
* Returns a page (or all) client fields.
165+
* @param int $page_num
166+
* @param int $num_per_page
167+
* @param array $search
168+
* @return array
169+
*/
166170
public static function getClientFields($page_num = 1, $num_per_page = 10, $search = array())
167171
{
168172
$db = Core::$db;
@@ -590,10 +594,8 @@ public static function updateFieldOrder($info, $L)
590594
* This function is attached to the admin_edit_view_client_map_filter_dropdown hook. It populates the
591595
* Edit View -> Client Map Filters -> client fields dropdown with any additional fields defined in this module.
592596
*/
593-
public static function displayExtendedFieldOptions($location, $template_vars)
597+
public static function displayExtendedFieldOptions($template_vars, $L)
594598
{
595-
$LANG = Core::$L;
596-
597599
Modules::getModuleInstance("extended_client_fields");
598600

599601
$client_fields = self::getClientFields(1, "all");
@@ -605,7 +607,7 @@ public static function displayExtendedFieldOptions($location, $template_vars)
605607
$client_map_filters = $template_vars["client_map_filters"];
606608
$selected_value = $client_map_filters[$current_row-1]["filter_values"];
607609

608-
echo "<optgroup label=\"{$LANG["extended_client_fields"]["module_name"]}\">\n";
610+
echo "<optgroup label=\"{$L["module_name"]}\">\n";
609611
foreach ($client_fields["results"] as $field_info) {
610612
$field_label = htmlspecialchars($field_info["field_label"]);
611613
$selected = ($selected_value == "ecf_" . $field_info["client_field_id"]) ? "selected" : "";
@@ -649,27 +651,43 @@ public static function reorderFields()
649651

650652

651653
/**
652-
* This is called by the "start" hook in the ft_get_view_filter_sql function. It
653-
* adds the extended client field placeholder variable with the e
654+
* This is called by the "start" hook in the ft_get_view_filter_sql function. It adds the extended client field
655+
* placeholder variable with the extended client field settings.
656+
*
657+
* Note: $info["placeholders"] already exists
654658
*/
655659
public static function updateViewFilterSqlPlaceholders($info)
656660
{
657-
$is_client_account = Core::$user->getAccountType() == "client";
658-
659-
if ($is_client_account) {
660-
$settings = Sessions::get("account.settings");
661-
if (is_array($settings)) {
662-
foreach ($settings as $key => $value) {
663-
if (preg_match("/^ecf_(\d)+$/", $key, $matches)) {
664-
$info["placeholders"][$key] = $value;
665-
}
666-
}
667-
}
668-
}
661+
$client_placeholders = self::getClientPlaceholders();
662+
663+
if (!empty($client_placeholders)) {
664+
$info["placeholders"] = array_merge($info["placeholders"], $client_placeholders);
665+
}
669666

670667
return $info;
671668
}
672669

670+
// TODO be nice to refactor this now we added field_identifier in 2.1.0. Using "ecf_X" as an identifier is a bit kludgy
671+
// but it means making the field_identifier column required for all extended client fields. For for now, internally
672+
// we use ecf_X to target the field, whereas end users can use the field_identifier value within their placeholders
673+
// e.g. {$CLIENT.identifier_here}
674+
public static function getClientPlaceholders()
675+
{
676+
$is_client_account = Core::$user->getAccountType() == "client";
677+
$placeholders = array();
678+
if ($is_client_account) {
679+
$settings = Sessions::get("account.settings");
680+
if (is_array($settings)) {
681+
foreach ($settings as $key => $value) {
682+
if (preg_match("/^ecf_(\d)+$/", $key, $matches)) {
683+
$placeholders[$key] = $value;
684+
}
685+
}
686+
}
687+
}
688+
689+
return $placeholders;
690+
}
673691

674692
private static function getNextFieldOrder ()
675693
{

code/Module.class.php

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@ class Module extends FormToolsModule
1919
protected $author = "Ben Keen";
2020
protected $authorEmail = "ben.keen@gmail.com";
2121
protected $authorLink = "https://formtools.org";
22-
protected $version = "2.0.4";
23-
protected $date = "2018-05-07";
22+
protected $version = "2.1.0";
23+
protected $date = "2019-01-10";
2424
protected $originLanguage = "en_us";
2525
protected $jsFiles = array(
2626
"{MODULEROOT}/scripts/field_options.js"
2727
);
28+
protected $cssFiles = array(
29+
"{MODULEROOT}/css/styles.css"
30+
);
2831

2932
protected $nav = array(
3033
"module_name" => array("index.php", false),
3134
"phrase_add_field" => array("add.php", true),
3235
"phrase_section_titles" => array("titles.php", false),
36+
"word_placeholders" => array("placeholders.php", false),
3337
"word_help" => array("help.php", false)
3438
);
3539

@@ -44,6 +48,7 @@ public function install($module_id)
4448
admin_only enum('yes','no') default NULL,
4549
field_label varchar(255) NOT NULL,
4650
field_type enum('textbox','textarea','password','radios','checkboxes','select','multi-select') NOT NULL,
51+
field_identifier varchar(255) NOT NULL,
4752
option_source enum('option_list', 'custom_list') NOT NULL DEFAULT 'option_list',
4853
option_list_id MEDIUMINT NULL,
4954
field_orientation enum('horizontal','vertical','na') NOT NULL default 'na',
@@ -119,7 +124,9 @@ public function uninstall($module_id)
119124
public function upgrade($module_id, $old_module_version)
120125
{
121126
$this->resetHooks();
122-
}
127+
128+
self::addFieldIdentifierFieldIfNotExists();
129+
}
123130

124131

125132
public function resetHooks()
@@ -146,6 +153,7 @@ public function resetHooks()
146153

147154
// general code hooks
148155
Hooks::registerHook("code", "extended_client_fields", "start", "FormTools\\ViewFilters::getViewFilterSql", "updateViewFilterSqlPlaceholders");
156+
Hooks::registerHook("code", "extended_client_fields", "main", "FormTools\\User->getAccountPlaceholders", "getExtendedClientFieldPlaceholders");
149157
}
150158

151159

@@ -205,14 +213,31 @@ public function insertHeadJs($location, $info)
205213
}
206214

207215

216+
// 2.1.0 added a new field_identifier column to allow the extended client fields to be used elsewhere in Form Tools,
217+
// namely as placeholders in the "Default Values for New Submissions" view setting:
218+
// https://github.com/formtools/core/issues/472
219+
// This adds the column to the database. Note: existing users will need to add in the identifiers manually to be
220+
// able to the user the feature
221+
public static function addFieldIdentifierFieldIfNotExists() {
222+
$db = Core::$db;
223+
224+
if (!General::checkDbTableFieldExists("module_extended_client_fields", "field_identifier")) {
225+
$db->query("
226+
ALTER TABLE {PREFIX}module_extended_client_fields
227+
ADD field_identifier VARCHAR(255) NULL AFTER field_type
228+
");
229+
$db->execute();
230+
}
231+
}
232+
208233
// ---------------- wrappers for hook calls ------------------
209234

210235
public function displayFields($location, $template_vars) {
211236
Fields::displayFields($location, $template_vars);
212237
}
213238

214239
public function displayExtendedFieldOptions($location, $template_vars) {
215-
Fields::displayExtendedFieldOptions($location, $template_vars);
240+
Fields::displayExtendedFieldOptions($template_vars, $this->getLangStrings());
216241
}
217242

218243
public function adminSaveExtendedClientFields($postdata) {
@@ -226,4 +251,56 @@ public function clientSaveExtendedClientFields($postdata) {
226251
public function updateViewFilterSqlPlaceholders($info) {
227252
return Fields::updateViewFilterSqlPlaceholders($info);
228253
}
254+
255+
// appends all ECF placeholders for use in the Default values for Submission fields (per View).
256+
public function getExtendedClientFieldPlaceholders($info) {
257+
258+
// we expect the account_id to be passed containing the client/admin account ID
259+
if (!isset($info["account_id"]) || empty($info["account_id"])) {
260+
return;
261+
}
262+
263+
// in { ecf_1 => "value", "ecf_2" => "value" } format
264+
$placeholders = Fields::getClientPlaceholders();
265+
if (empty($placeholders)) {
266+
return;
267+
}
268+
269+
$keys = array_keys($placeholders);
270+
271+
$fields = Fields::getClientFields(1, "all");
272+
$found = array();
273+
foreach ($fields["results"] as $field_info) {
274+
$current_key = "ecf_{$field_info["client_field_id"]}";
275+
if (in_array($current_key, $keys) && !empty($field_info["field_identifier"])) {
276+
$found[$field_info["field_identifier"]] = $placeholders[$current_key];
277+
}
278+
}
279+
$info["placeholders"]["CLIENT"] = $found;
280+
281+
return $info;
282+
}
229283
}
284+
285+
286+
287+
288+
289+
290+
291+
292+
293+
294+
295+
296+
297+
298+
299+
300+
301+
302+
303+
304+
305+
306+

css/styles.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.grey {
2+
color: #999999;
3+
}

index.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
$results = $info["results"];
3333
$num_results = $info["num_results"];
3434

35-
3635
$page_vars = array(
3736
"results" => $results,
3837
"head_title" => $module->getModuleName(),

lang/en_us.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
$L["word_bottom"] = "Bottom";
2222
$L["word_orientation"] = "Orientation";
2323
$L["word_settings"] = "Settings";
24+
$L["word_placeholder"] = "Placeholder";
25+
$L["word_placeholders"] = "Placeholders";
2426
$L["word_help"] = "Help";
2527

2628
$L["phrase_add_field"] = "Add Field";
2729
$L["phrase_add_new_field"] = "Add New Field";
2830
$L["phrase_edit_field"] = "Edit Field";
2931
$L["phrase_field_label"] = "Field Label";
3032
$L["phrase_field_type"] = "Field Type";
33+
$L["phrase_field_identifier"] = "Field Identifier";
3134
$L["phrase_default_value"] = "Default Value";
3235
$L["phrase_page_and_location"] = "Page and Location";
3336
$L["phrase_main_account_page"] = "Main Account Page";
@@ -46,7 +49,7 @@
4649

4750
$L["confirm_delete_field"] = "Are you sure you want to delete this field? This cannot be undone.";
4851

49-
$L["notify_no_fields"] = "There are no additional fields added to the module yet. Click the button below to add a new field.";
52+
$L["notify_no_fields"] = "There are no fields created yet. Click the button below to add a new field.";
5053
$L["notify_problem_installing"] = "There following error occurred when trying to create the database tables for this module: <b>{\$error}</b>";
5154
$L["notify_field_added"] = "The field has been added. You may add another field below, <a href=\"edit.php?id={\$client_field_id}\">edit the last field</a>, or <a href=\"index.php\">return to the main field list page</a>.";
5255
$L["notify_admin_only_field_explanation"] = "This setting determines lets you hide the field from the client; it only appears for the the administrator.";
@@ -59,6 +62,7 @@
5962
$L["validation_no_template_hook"] = "Please choose the page and location where this field should appear.";
6063
$L["validation_no_field_label"] = "Please enter the field label.";
6164
$L["validation_no_field_type"] = "Please choose the field type.";
65+
$L["validation_invalid_field_identifier"] = "Please only enter alphanumeric (a-Z and 0-9) and the underscore character.";
6266

63-
$L["text_help"] = "Please see our <a href=\"https://docs.formtools.org/modules/extended_client_fields/\" target=\"_blank\">online help documentation</a> for information on how to use this module.";
67+
$L["text_help"] = "Please see the <a href=\"https://docs.formtools.org/modules/extended_client_fields/\" target=\"_blank\">help documentation</a> for information about this module.";
6468
$L["text_client_field_titles_intro"] = "Your client fields can be inserted in different locations on the Edit Client pages, visible by the administrator and client. This page gives you the option of providing a title for each of the fields. These titles are omitted by default.";

placeholders.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
require("../../global/library.php");
4+
5+
use FormTools\Modules;
6+
use FormTools\Modules\ExtendedClientFields\Fields;
7+
8+
$module = Modules::initModulePage("admin");
9+
10+
$info = Fields::getClientFields(1, "all");
11+
12+
$results = array();
13+
foreach ($info["results"] as $row) {
14+
if (empty($row["field_identifier"])) {
15+
continue;
16+
}
17+
$results[] = $row;
18+
}
19+
20+
$page_info = array(
21+
"results" => $results
22+
);
23+
24+
$module->displayPage("templates/placeholders.tpl", $page_info);

0 commit comments

Comments
 (0)