Skip to content

Commit

Permalink
Code files use handles instead of ids (#554)
Browse files Browse the repository at this point in the history
* Adding patch to update ids to handles for codefiles

* Initial creation of the screen handle

* Ensuring ids are treated as numbers when retrieving screens

* Renaming element class, moving handle sanitizing method to a common parent class

* Reorg some code, correct some references

* Finish roughing in the screen handle UI and functionality

* Initialize screen handles with DB patch

* Remove custom button code files with old name when button handle changes

* Updating screenHandle to screen_handle for consistency with forms and elements

* Updating DB patch to be based on screen_handle

* Updating patch to use handles in original file creation

* Instantiating original button data properly when saving

* Rename custom button code files when screen handle changes

* Update form procedure code files when form handle changes

* Update element code files when element handle changes

* Updating references to filenames of code files

* Fixing dumb code flow

* Further updating files names to write to

* Updating class names to avoid conflict

* Make initial screen handles lowercase

* Typo in SQL

* Renaming existing files in patch process needed to be smarter

* Fixing references to custom button code file names

* Fixing UI for screen handles
  • Loading branch information
jegelstaff authored Oct 20, 2024
1 parent 167f0be commit 824ea79
Show file tree
Hide file tree
Showing 32 changed files with 380 additions and 151 deletions.
49 changes: 21 additions & 28 deletions integration_api.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,37 +567,10 @@ static function getExternalResourceID($resource_type, $xoops_id) {
}
}

/**
* Formulize objects extend this class so that their
* instance fields can be manipulated.
*/
class FormulizeObject {

/**
* Get the value of a field in this FormulizeUser
* @param key String The name of the field to be retrieved
* @return [AnyType] The requested property
*/
function get($key) {
if(!isset($this->{$key}) && $this->{$key} != null) throw new Exception('FormulizeObject - Attempted to get an invalid object field: ' . $key);
return $this->{$key};
}

/**
* Set the value of a field in this FormulizeUser
* @param key String The name of the field to be retrieved
* @param key Object The value to be stored in this field
*/
function set($key, $value) {
if(!isset($this->{$key})) throw new Exception('FormulizeObject - Attempted to set an invalid object field: ' . $key);
$this->{$key} = $value;
}
}

/**
* Formulize User Object
*/
class FormulizeUser extends FormulizeObject {
class FormulizeUser {

protected $uid = -1;
protected $uname = '';
Expand Down Expand Up @@ -632,6 +605,26 @@ function __construct($user_data) {
}
}

/**
* Get the value of a field in this FormulizeUser
* @param key String The name of the field to be retrieved
* @return [AnyType] The requested property
*/
function get($key) {
if(!isset($this->{$key}) && $this->{$key} != null) throw new Exception('FormulizeUser - Attempted to get an invalid object field: ' . $key);
return $this->{$key};
}

/**
* Set the value of a field in this FormulizeUser
* @param key String The name of the field to be retrieved
* @param key Object The value to be stored in this field
*/
function set($key, $value) {
if(!isset($this->{$key})) throw new Exception('FormulizeUser - Attempted to set an invalid object field: ' . $key);
$this->{$key} = $value;
}

function insertAndMapUser($groups) {

global $icmsConfigUser;
Expand Down
141 changes: 114 additions & 27 deletions modules/formulize/admin/formindex.php

Large diffs are not rendered by default.

14 changes: 11 additions & 3 deletions modules/formulize/admin/save/element_names_save.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ function figureOutOrder($orderChoice, $oldOrder, $fid) {
$isNew = $_POST['formulize_admin_key'] == "new" ? true : false;
foreach ($processedValues['elements'] as $property => $element_handle_name) {
if ($property == "ele_handle") {
$element_handle_name = formulizeForm::sanitize_handle_name($element_handle_name);
$element_handle_name = formulizeElement::sanitize_handle_name($element_handle_name);
if (strlen($element_handle_name)) {
$firstUniqueCheck = true;
while (!$uniqueCheck = $form_handler->isHandleUnique($element_handle_name, $ele_id)) {
while (!$uniqueCheck = $form_handler->isElementHandleUnique($element_handle_name, $ele_id)) {
if ($firstUniqueCheck) {
$element_handle_name = $element_handle_name . "_".$fid;
$firstUniqueCheck = false;
Expand Down Expand Up @@ -166,7 +166,15 @@ function figureOutOrder($orderChoice, $oldOrder, $fid) {
$xoopsDB->query($updateSQL);
}
}

// update element code file names
$elementTypes = array('ib', 'areamodif', 'text', 'textarea', 'derived');
foreach($elementTypes as $type) {
$oldFileName = XOOPS_ROOT_PATH.'/modules/formulize/code/'.$type.'_'.$original_handle.'.php';
$newFileName = XOOPS_ROOT_PATH.'/modules/formulize/code/'.$type.'_'.$ele_handle.'.php';
if(file_exists($oldFileName)) {
rename($oldFileName, $newFileName);
}
}
}
}

Expand Down
9 changes: 9 additions & 0 deletions modules/formulize/admin/save/form_settings_save.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@
if(!$renameResult = $form_handler->renameDataTable($old_form_handle, $formObject->getVar( "form_handle" ), $formObject)) {
exit("Error: could not rename the data table in the database.");
}
// update code files with this form handle
$events = array('on_before_save', 'on_after_save', 'on_delete', 'custom_edit_check');
foreach($events as $event) {
$oldFileName = XOOPS_ROOT_PATH.'/modules/formulize/code/'.$event.'_'.$old_form_handle.'.php';
$newFileName = XOOPS_ROOT_PATH.'/modules/formulize/code/'.$event.'_'.$formObject->getVar( "form_handle" ).'.php';
if(file_exists($oldFileName)) {
rename($oldFileName, $newFileName);
}
}
}

$selectedAppIds = array();
Expand Down
20 changes: 14 additions & 6 deletions modules/formulize/admin/save/screen_list_custom_save.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@
// watch out for effects hat are supposed to be deleted...don't save them
$deleteButton = $_POST['deletebutton'];
$removeEffect = $_POST['removeeffect'] ? explode("_", $_POST['removeeffect']) : array("","");

$currentCustomActionData = $screen->getVar('customactions');

// read all the button info that was submitted, pack it up and assign it to the screen object
foreach($_POST as $k=>$v) {
if(substr($k, 0, 7)=="handle_") {
// found a button, grab all its info
$buttonId = substr($k, 7);
if((string)$buttonId === (string)$deleteButton) { continue; }
$buttonData[$buttonId]['handle'] = $v;
$buttonData[$buttonId]['handle'] = formulizeScreen::sanitize_handle_name($v);
$buttonData[$buttonId]['buttontext'] = $_POST['buttontext_'.$buttonId];
$buttonData[$buttonId]['messagetext'] = $_POST['messagetext_'.$buttonId];
$buttonData[$buttonId]['popuptext'] = $_POST['popuptext_'.$buttonId];
Expand All @@ -90,16 +90,24 @@
foreach($_POST['code_'.$buttonId] as $effectId=>$code) {
if((string)$effectId === (string)$removeEffect[1] AND (string)$buttonId === (string)$removeEffect[0]) { continue; }
$buttonData[$buttonId][$effectId]['code'] = "";
$filename = "custom_code_".$effectId."_".$buttonId."_".$sid.".php";
formulize_writeCodeToFile($filename, $code);
$filename = "custom_code_".$effectId."_".$buttonData[$buttonId]['handle']."_".$screen->getVar('screen_handle').".php";
if(formulize_writeCodeToFile($filename, $code)) {
if(isset($currentCustomActionData[$buttonId]['handle']) AND $buttonData[$buttonId]['handle'] != $currentCustomActionData[$buttonId]['handle']) {
unlink(XOOPS_ROOT_PATH."/modules/formulize/code/custom_code_".$effectId."_".$currentCustomActionData[$buttonId]['handle']."_".$screen->getVar('screen_handle').".php");
}
}
}
}
if(isset($_POST['html_'.$buttonId])) {
foreach($_POST['html_'.$buttonId] as $effectId=>$html) {
if((string)$effectId === (string)$removeEffect[1] AND (string)$buttonId === (string)$removeEffect[0]) { continue; }
$buttonData[$buttonId][$effectId]['html'] = "";
$filename = "custom_html_".$effectId."_".$buttonId."_".$sid.".php";
formulize_writeCodeToFile($filename, $html);
$filename = "custom_html_".$effectId."_".$buttonData[$buttonId]['handle']."_".$screen->getVar('screen_handle').".php";
if(formulize_writeCodeToFile($filename, $html)) {
if(isset($currentCustomActionData[$buttonId]['handle']) AND $buttonData[$buttonId]['handle'] != $currentCustomActionData[$buttonId]['handle']) {
unlink(XOOPS_ROOT_PATH."/modules/formulize/code/custom_html_".$effectId."_".$currentCustomActionData[$buttonId]['handle']."_".$screen->getVar('screen_handle').".php");
}
}
}
}
if(isset($_POST['element_'.$buttonId])) {
Expand Down
37 changes: 34 additions & 3 deletions modules/formulize/admin/save/screen_settings_save.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
return;
}


$aid = intval($_POST['aid']);
$sid = $_POST['formulize_admin_key'];
$fid = intval($_POST['formulize_admin_fid']);
Expand All @@ -49,7 +48,6 @@
return;
}


$screens = $processedValues['screens'];

$isNew = ($sid=='new');
Expand Down Expand Up @@ -97,18 +95,51 @@
$screen = $screen_handler->get($sid);
}

if (!strlen($screens['screen_handle']) AND $sid) {
$screens['screen_handle'] = $sid;
}
$screens['screen_handle'] = $screen_handler->makeHandleUnique($screens['screen_handle'], ($sid ? $sid : ""));
if ($screens['screen_handle'] != $processedValues['screens']['screen_handle']) {
$_POST['reload_names_page'] = 1;
}

$originalScreenHandle = $screen->getVar('screen_handle');

$screen->setVar('title',$screens['title']);
$screen->setVar('screen_handle',$screens['screen_handle']);
$screen->setVar('fid',$fid);
$screen->setVar('type',$screens['type']);
$screen->setVar('useToken',$screens['useToken']);
$screen->setVar('anonNeedsPasscode',$screens['anonNeedsPasscode']);
$screen->setVar('rewriteruleAddress',formulizeForm::sanitize_handle_name(str_replace(" ", "_", $screens['rewriteruleAddress'])));
$screen->setVar('rewriteruleAddress',formulizeScreen::sanitize_handle_name($screens['rewriteruleAddress']));
$screen->setVar('rewriteruleElement', intval($screens['rewriteruleElement']));

if(!$sid = $screen_handler->insert($screen)) {
print "Error: could not save the screen properly: ".$xoopsDB->error();
}

// replace blank handle with screen id, must be done after insert in case we're dealing with new screen
if($screens['screen_handle'] == '') {
$screen->setVar('screen_handle', $screen_handler->makeHandleUnique($sid, $sid));
if(!$sid = $screen_handler->insert($screen)) {
print "Error: could not update new screen with screen id: ".$xoopsDB->error();
}
}

if($originalScreenHandle !== $screen->getVar('screen_handle')) {
$currentCustomActionData = $screen->getVar('customactions');
foreach($currentCustomActionData as $buttonId=>$buttonData) {
foreach($buttonData as $key=>$value) {
if(is_numeric($key) AND is_array($value) AND (isset($value['code']) OR isset($value['html']))) {
$effectId = $key;
$buttonHandle = $buttonData['handle'];
$codeType = isset($value['code']) ? 'custom_code' : 'custom_html';
rename(XOOPS_ROOT_PATH."/modules/formulize/code/".$codeType."_".$effectId."_".$buttonHandle."_".$originalScreenHandle.".php", XOOPS_ROOT_PATH."/modules/formulize/code/".$codeType."_".$effectId."_".$buttonHandle."_".$screen->getVar('screen_handle').".php");
}
}
}
}

$reloadNow = false;
$passcode_handler = xoops_getmodulehandler('passcode', 'formulize');
if($_POST['delete_passcode']) {
Expand Down
9 changes: 6 additions & 3 deletions modules/formulize/admin/screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
$settings['useToken'] = $formulizeConfig['useToken'];
$settings['anonNeedsPasscode'] = 1;
$screenName = "New screen";
$screen_handle = "";
} else {
$screen_handler = xoops_getmodulehandler('screen', 'formulize');
$screen = $screen_handler->get($screen_id);
Expand Down Expand Up @@ -96,6 +97,7 @@
$screen = $screen_handler->get($screen_id);

$screenName = $screen->getVar('title');
$screen_handle = $screen->getVar('screen_handle');
$form_id = $screen->form_id();

$adminPage["template"] = "ABC, mellonfarmers!";
Expand Down Expand Up @@ -283,15 +285,15 @@
foreach($buttonData as $key=>$value) {
if (is_numeric($key)) { // effects have numeric keys
if ($buttonData['applyto'] == 'custom_code') {
$filename = "custom_code_".$key."_".$buttonId."_".$screen_id.".php";
$filename = "custom_code_".$key."_".$buttonData['handle']."_".$screen_handle.".php";
$custom['custombuttons'][$buttonId]['content'][$key]['code'] = file_get_contents(XOOPS_ROOT_PATH.'/modules/formulize/code/'.$filename);
$custom['custombuttons'][$buttonId]['content'][$key]['description'] = _AM_FORMULIZE_SCREEN_LOE_CUSTOMBUTTON_EFFECT_CUSTOM_CODE_DESC;
} elseif($buttonData['applyto'] == 'custom_code_once') {
$filename = "custom_code_".$key."_".$buttonId."_".$screen_id.".php";
$filename = "custom_code_".$key."_".$buttonData['handle']."_".$screen_handle.".php";
$custom['custombuttons'][$buttonId]['content'][$key]['code'] = file_get_contents(XOOPS_ROOT_PATH.'/modules/formulize/code/'.$filename);
$custom['custombuttons'][$buttonId]['content'][$key]['description'] = _AM_FORMULIZE_SCREEN_LOE_CUSTOMBUTTON_EFFECT_CUSTOM_CODE_ONCE_DESC;
} elseif ($buttonData['applyto'] == 'custom_html') {
$filename = "custom_html_".$key."_".$buttonId."_".$screen_id.".php";
$filename = "custom_html_".$key."_".$buttonData['handle']."_".$screen_handle.".php";
$custom['custombuttons'][$buttonId]['content'][$key]['html'] = file_get_contents(XOOPS_ROOT_PATH.'/modules/formulize/code/'.$filename);
$custom['custombuttons'][$buttonId]['content'][$key]['description'] = _AM_FORMULIZE_SCREEN_LOE_CUSTOMBUTTON_EFFECT_CUSTOM_HTML_DESC;
} else {
Expand Down Expand Up @@ -526,6 +528,7 @@
$common['fid'] = $form_id;
$common['aid'] = $aid;
$common['uid'] = $xoopsUser->getVar('uid');
$common['screen_handle'] = $screen_handle;

// generate a group list for use with the custom buttons
$sql = "SELECT name, groupid FROM ".$xoopsDB->prefix("groups")." ORDER BY groupid";
Expand Down
2 changes: 1 addition & 1 deletion modules/formulize/class/anonPasscodeElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

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 {
class formulizeAnonPasscodeElement extends formulizeElement {

function __construct() {
$this->name = "Anonymous User Passcode";
Expand Down
6 changes: 3 additions & 3 deletions modules/formulize/class/checkboxElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
require_once XOOPS_ROOT_PATH . "/modules/formulize/class/elements.php"; // you need to make sure the base element class has been read in first!
require_once XOOPS_ROOT_PATH . "/modules/formulize/include/functions.php";

class formulizeCheckboxElement extends formulizeformulize {
class formulizeCheckboxElement extends formulizeElement {

function __construct() {
$this->name = "Checkboxes";
Expand Down Expand Up @@ -81,7 +81,7 @@ function create() {
// when dealing with new elements, $element might be FALSE
function adminPrepare($element) {
$dataToSendToTemplate = array();
if(is_object($element) AND is_subclass_of($element, 'formulizeformulize')) {
if(is_object($element) AND is_subclass_of($element, 'formulizeElement')) {
$ele_value = $this->backwardsCompatibility($element->getVar('ele_value'));
if(is_array($ele_value[2])) { // an array will be a set of hard coded options
$ele_value[2] = formulize_mergeUIText($ele_value[2], $element->getVar('ele_uitext'));
Expand Down Expand Up @@ -164,7 +164,7 @@ function adminPrepare($element) {
function adminSave($element, $ele_value) {
$changed = false;

if(is_object($element) AND is_subclass_of($element, 'formulizeformulize')) {
if(is_object($element) AND is_subclass_of($element, 'formulizeElement')) {

$ele_value = array(
EV_MULTIPLE_LIST_COLUMNS=>$ele_value[EV_MULTIPLE_LIST_COLUMNS],
Expand Down
6 changes: 3 additions & 3 deletions modules/formulize/class/dummyElement-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

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 formulizeDummyElement extends formulizeformulize {
class formulizeDummyElement extends formulizeElement {

function __construct() {
$this->name = "The Dummy Element";
Expand Down Expand Up @@ -68,7 +68,7 @@ function create() {
// when dealing with new elements, $element might be FALSE
function adminPrepare($element) {
$dataToSendToTemplate = array();
if(is_object($element) AND is_subclass_of($element, 'formulizeformulize')) {
if(is_object($element) AND is_subclass_of($element, 'formulizeElement')) {
$ele_value = $element->getVar('ele_value');
if($ele_value[0] == "foo") {
$dataToSendToTemplate['first'] = "First value is 'foo'!"; // key is referenced in the template
Expand All @@ -88,7 +88,7 @@ function adminPrepare($element) {
// $ele_value will be only the values parsed out of the Options tab on the element's admin page, which follow the naming convention elements-ele_value -- other values that should be in ele_value will need to be parsed here from $_POST or elsewhere
function adminSave($element, $ele_value) {
$changed = false;
if(is_object($element) AND is_subclass_of($element, 'formulizeformulize')) {
if(is_object($element) AND is_subclass_of($element, 'formulizeElement')) {
$ele_desc = $element->getVar('ele_desc');
$new_desc = "The default value for this element will be: " . $ele_value[0] . $ele_value[1];
if($ele_desc != $new_desc) {
Expand Down
Loading

0 comments on commit 824ea79

Please sign in to comment.