Skip to content

Commit

Permalink
Creating custom element class for textboxes (finally!) (#529)
Browse files Browse the repository at this point in the history
* Creating custom element class for textboxes (finally!)

* Touchups to make things work
  • Loading branch information
jegelstaff authored Aug 12, 2024
1 parent c53be71 commit 1dbaa68
Show file tree
Hide file tree
Showing 31 changed files with 692 additions and 1,388 deletions.
1 change: 1 addition & 0 deletions include/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,7 @@ function &icms_getModuleHandler($name = null, $module_dir = null, $module_basena
}
}
if (!isset($handlers[$module_dir][$name]) && !$optional) {
debug_print_backtrace();
trigger_error(sprintf(_CORE_MODULEHANDLER_NOTAVAILABLE, $module_dir, $name), E_USER_ERROR);
}
if (isset($handlers[$module_dir][$name])) {return $handlers[$module_dir][$name];}
Expand Down
16 changes: 2 additions & 14 deletions modules/formulize/admin/element.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,6 @@
$ele_use_default_when_blank = 0;
global $xoopsModuleConfig;
switch($ele_type) {
case("text"):
$ele_value[0] = $xoopsModuleConfig['t_width'];
$ele_value[1] = $xoopsModuleConfig['t_max'];
$ele_value[3] = 0;
$ele_value[5] = isset($formulizeConfig['number_decimals']) ? $formulizeConfig['number_decimals'] : 0;
$ele_value[6] = isset($formulizeConfig['number_prefix']) ? $formulizeConfig['number_prefix'] : '';
$ele_value[7] = isset($formulizeConfig['number_decimalsep']) ? $formulizeConfig['number_decimalsep'] : '.';
$ele_value[8] = isset($formulizeConfig['number_sep']) ? $formulizeConfig['number_sep'] : ',';
break;
case("textarea"):
$ele_value[1] = $xoopsModuleConfig['ta_rows'];
$ele_value[2] = $xoopsModuleConfig['ta_cols'];
Expand Down Expand Up @@ -289,10 +280,7 @@
$options['typetemplate'] = "db:admin/element_type_".$ele_type.".html";

// setup various special things per element, including ele_value
if ($ele_type=='text') {
$formlink = createFieldList($ele_value[4], true);
$options['formlink'] = $formlink->render();
} else if ($ele_type=='textarea') {
if ($ele_type=='textarea') {
$formlink = createFieldList($ele_value[3], true);
$options['formlink'] = $formlink->render();
} else if ($ele_type=='derived') {
Expand Down Expand Up @@ -620,7 +608,7 @@ function createDataTypeUI($ele_type, $element,$id_form,$ele_encrypt) {
$customTypeNeedsUI = $customTypeObject->needsDataType;
}

if (($ele_type == "text" OR $ele_type == "textarea" OR $ele_type == "select" OR $ele_type == "radio" OR $ele_type == "derived" OR $customTypeNeedsUI) AND !$ele_encrypt) {
if (($ele_type == "textarea" OR $ele_type == "select" OR $ele_type == "radio" OR $ele_type == "derived" OR $customTypeNeedsUI) AND !$ele_encrypt) {
if ($element) {
$defaultTypeInformation = $element->getDataTypeInformation();
$defaultType = $defaultTypeInformation['dataType'];
Expand Down
2 changes: 1 addition & 1 deletion modules/formulize/admin/save/element_options_save.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function formulize_extractUIText($values) {
return;
}

if(($ele_type == "text" OR $ele_type == "textarea") AND $_POST['formlink'] != "none") {
if($ele_type == "textarea" AND $_POST['formlink'] != "none") {
$ele_value_key = $ele_type == "text" ? 4 : 3; // two types keep this info in diff key
$processedValues['elements']['ele_value'][$ele_value_key] = $_POST['formlink'];
}
Expand Down
42 changes: 21 additions & 21 deletions modules/formulize/class/anonPasscodeElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,49 @@
require_once XOOPS_ROOT_PATH . "/modules/formulize/class/elements.php"; // you need to make sure the base element class has been read in first!

class formulizeAnonPasscodeElement extends formulizeformulize {

function __construct() {
$this->name = "Anonymous User Passcode";
$this->hasData = true; // set to false if this is a non-data element, like the subform or the grid
$this->needsDataType = false; // set to false if you're going force a specific datatype for this element using the overrideDataType
$this->overrideDataType = "varchar(255)"; // use this to set a datatype for the database if you need the element to always have one (like 'date'). set needsDataType to false if you use this.
$this->adminCanMakeRequired = false; // set to true if the webmaster should be able to toggle this element as required/not required
$this->alwaysValidateInputs = false; // set to true if you want your custom validation function to always be run. This will override any required setting that the webmaster might have set, so the recommendation is to set adminCanMakeRequired to false when this is set to true.
$this->isSystemElement = true;
$this->isSystemElement = true;
parent::__construct();
}

}

class formulizeAnonPasscodeElementHandler extends formulizeElementsHandler {

var $db;
var $clickable; // used in formatDataForList
var $striphtml; // used in formatDataForList
var $length; // used in formatDataForList

function __construct($db) {
$this->db =& $db;
}

function create() {
return new formulizeAnonPasscodeElement();
}

// this method would gather any data that we need to pass to the template, besides the ele_value and other properties that are already part of the basic element class
// it receives the element object and returns an array of data that will go to the admin UI template
// when dealing with new elements, $element might be FALSE
function adminPrepare($element) {
}

// this method would read back any data from the user after they click save in the admin UI, and save the data to the database, if it were something beyond what is handled in the basic element class
// this is called as part of saving the options tab. It receives a copy of the element object immediately prior to it being saved, so the element object will have all its properties set as they would be based on the user's changes in the names & settings tab, and in the options tab (the tabs are saved in order from left to right).
// the exception is the special ele_value array, which is passed separately from the object (this will contain the values the user set in the Options tab)
// You can modify the element object in this function and since it is an object, and passed by reference by default, then your changes will be saved when the element is saved.
// You should return a flag to indicate if any changes were made, so that the page can be reloaded for the user, and they can see the changes you've made here.
function adminSave($element, $ele_value) {
}

// this method reads the current state of an element based on the user's input, and the admin options, and sets ele_value to what it needs to be so we can render the element correctly
// it must return $ele_value, with the correct value set in it, so that it will render as expected in the render method
// $value is the value that was retrieved from the database for this element in the active entry. It is a raw value, no processing has been applied, it is exactly what is in the database (as prepared in the prepareDataForSaving method and then written to the DB)
Expand All @@ -84,7 +84,7 @@ function adminSave($element, $ele_value) {
function loadValue($value, $ele_value, $element) {
return $value;
}

// this method renders the element for display in a form
// the caption has been pre-prepared and passed in separately from the element object
// if the element is disabled, then the method must take that into account and return a non-interactable label with some version of the element's value in it
Expand All @@ -107,13 +107,13 @@ function render($ele_value, $caption, $markupName, $isDisabled, $element, $entry
//return new xoopsFormLabel($caption, $ele_value);
}
}

// this method returns any custom validation code (javascript) that should figure out how to validate this element
// 'myform' is a name enforced by convention that refers to the form where this element resides
// use the adminCanMakeRequired property and alwaysValidateInputs property to control when/if this validation code is respected
function generateValidationCode($caption, $markupName, $element, $entry_id) {
}

// this method will read what the user submitted, and package it up however we want for insertion into the form's datatable
// You can return {WRITEASNULL} to cause a null value to be saved in the database
// $value is what the user submitted
Expand All @@ -125,7 +125,7 @@ function prepareDataForSaving($value, $element, $entry_id=null, $subformBlankCou
return isset($_SESSION['formulize_passCode_'.$value]) ? $_SESSION['formulize_passCode_'.$value] : "{WRITEASNULL}";
}
}

// this method will handle any final actions that have to happen after data has been saved
// this is typically required for modifications to new entries, after the entry ID has been assigned, because before now, the entry ID will have been "new"
// value is the value that was just saved
Expand All @@ -134,7 +134,7 @@ function prepareDataForSaving($value, $element, $entry_id=null, $subformBlankCou
// ALSO, $GLOBALS['formulize_afterSavingLogicRequired']['elementId'] = type , must be declared in the prepareDataForSaving step if further action is required now -- see fileUploadElement.php for an example
function afterSavingLogic($value, $element_id, $entry_id) {
}

// this method will prepare a raw data value from the database, to be included in a dataset when formulize generates a list of entries or the getData API call is made
// in the standard elements, this particular step is where multivalue elements, like checkboxes, get converted from a string that comes out of the database, into an array, for example
// $value is the raw value that has been found in the database
Expand All @@ -143,26 +143,26 @@ function afterSavingLogic($value, $element_id, $entry_id) {
function prepareDataForDataset($value, $handle, $entry_id) {
return $value; // we're not making any modifications for this element type
}

// this method will take a text value that the user has specified at some point, and convert it to a value that will work for comparing with values in the database. This is used primarily for preparing user submitted text values for saving in the database, or for comparing to values in the database, such as when users search for things. The typical user submitted values would be coming from a condition form (ie: fieldX = [term the user typed in]) or other situation where the user types in a value that needs to interact with the database.
// it is only necessary to do special logic here if the values stored in the database do not match what users would be typing, ie: you're using coded numbers in the database, but displaying text on screen to users
// this would be where a Yes value would be converted to a 1, for example, in the case of a yes/no element, since 1 is how yes is represented in the database for that element type
// $partialMatch is used to indicate if we should search the values for partial string matches, like On matching Ontario. This happens in the getData function when processing filter terms (ie: searches typed by users in a list of entries)
// if $partialMatch is true, then an array may be returned, since there may be more than one matching value, otherwise a single value should be returned.
// if literal text that users type can be used as is to interact with the database, simply return the $value
// if literal text that users type can be used as is to interact with the database, simply return the $value
function prepareLiteralTextForDB($value, $element, $partialMatch=false) {
return $value;
}

// this method will format a dataset value for display on screen when a list of entries is prepared
// for standard elements, this step is where linked selectboxes potentially become clickable or not, among other things
// Set certain properties in this function, to control whether the output will be sent through a "make clickable" function afterwards, sent through an HTML character filter (a security precaution), and trimmed to a certain length with ... appended.
function formatDataForList($value, $handle="", $entry_id=0) {
function formatDataForList($value, $handle="", $entry_id=0, $textWidth=100) {
$this->clickable = true; // make urls clickable
$this->striphtml = true; // remove html tags as a security precaution
$this->length = 255; // truncate to a maximum of 100 characters, and append ... on the end

return parent::formatDataForList($value); // always return the result of formatDataForList through the parent class (where the properties you set here are enforced)
}
}

}
7 changes: 1 addition & 6 deletions modules/formulize/class/checkboxElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,6 @@ function render($ele_value, $caption, $markupName, $isDisabled, $element, $entry
trans($renderedElement),
$markupName
);
$ele_desc = $element->getVar('ele_desc', "f"); // the f causes no stupid reformatting by the ICMS core to take place
$elementRenderer = new formulizeElementRenderer($element);
$helpText = $elementRenderer->formulize_replaceCurlyBracketVariables(html_entity_decode($ele_desc,ENT_QUOTES), $entry_id, $element->getVar('id_form'), $markupName);
$form_ele->setDescription($helpText);

return $form_ele;
}

Expand Down Expand Up @@ -599,7 +594,7 @@ function prepareLiteralTextForDB($value, $element, $partialMatch=false) {
// this method will format a dataset value for display on screen when a list of entries is prepared
// for standard elements, this step is where linked selectboxes potentially become clickable or not, among other things
// Set certain properties in this function, to control whether the output will be sent through a "make clickable" function afterwards, sent through an HTML character filter (a security precaution), and trimmed to a certain length with ... appended.
function formatDataForList($value, $handle="", $entry_id=0) {
function formatDataForList($value, $handle="", $entry_id=0, $textWidth=100) {
$this->clickable = true; // make urls clickable
$this->striphtml = true; // remove html tags as a security precaution
$this->length = 1000; // truncate to a maximum of 100 characters, and append ... on the end
Expand Down
2 changes: 1 addition & 1 deletion modules/formulize/class/dummyElement-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ function prepareLiteralTextForDB($value, $element, $partialMatch=false) {
// this method will format a dataset value for display on screen when a list of entries is prepared
// for standard elements, this step is where linked selectboxes potentially become clickable or not, among other things
// Set certain properties in this function, to control whether the output will be sent through a "make clickable" function afterwards, sent through an HTML character filter (a security precaution), and trimmed to a certain length with ... appended.
function formatDataForList($value, $handle="", $entry_id=0) {
function formatDataForList($value, $handle="", $entry_id=0, $textWidth=100) {
$this->clickable = true; // make urls clickable
$this->striphtml = true; // remove html tags as a security precaution
$this->length = 100; // truncate to a maximum of 100 characters, and append ... on the end
Expand Down
Loading

0 comments on commit 1dbaa68

Please sign in to comment.