Forums

Infinite loop when using some containers

abletech 29 Nov, 2012
Chronoforms Version: v4 RC 3.5 (but I think it's the same in 3.5.1).

In a form I have used 12 containers, created in different steps.

After I finished adding that containers, on frontend the form was displayed correctly, but when accessing Form Wizard in control panel, the script stay running forever, ending with a PHP timeout.

I debugged the script and found that the problem was in function getContainerOutput of file administrator/components/com_chronoforms/views/form_wizard.php.

I was able to solve this problem creating a better recursive function, and an utility function to create a temporary array:

function buildContainersArray($wizcode) {
    $arr = array();
    foreach ($wizcode as $key => $element) {
        $tmp = explode("_",$key);
        $fieldid = intval($tmp[1]);
        $contid = intval($element['container_id']);
        if ($contid!=$fieldid) {
                if (!isset($arr[$contid])) $arr[$contid] = array();
                $arr[$contid][] = $fieldid;
        }
    }
    return $arr;
}
function buildContaintersOutput(&$containers_outputs, &$childs, $id=0) {
    if (isset($childs[$id])) {
        foreach ($childs[$id] as $child) {
            $containers_outputs[$child] = buildContaintersOutput($containers_outputs, $childs, $child);
            $containers_outputs[$id] = str_replace('<!--CONTAINER_ELEMENTS-'.$child.'-->', $containers_outputs[$child], $containers_outputs[$id]);
        }        
    }
    return $containers_outputs[$id];
}


Then, in line 546 of form_wizard.php, I have called this code:

$containers = buildContainersArray($wizardcode);
echo buildContaintersOutput($containers_outputs, $containers);


Instead of:
echo getContainerOutput($containers_outputs[0], $containers_outputs);


It seems to works correctly and it's also more good in performance.

I'm also attaching the complete modified file. Can you include this fix in next release?

Stefano Bagnatica
GreyHead 29 Nov, 2012
Hi abletech,

Thanks very much. I'll go pass a link to this post to Max.

Bob
abletech 01 Dec, 2012
I have found a problem with my first modified source, when entering the wizard view of old forms (created before the introduction of containers feature), because the container "0" seems to have itself as child.

So I have edited my first post and attachment, adding "if ($contid!=$fieldid)" inside my buildContainersArray functions.

Now it works well also with old forms.
GreyHead 01 Dec, 2012
Hi abletech,

Thanks again - passed on to Max. He has confirmed that he'll include your code in the next release.

Bob
GreyHead 14 Dec, 2012
Hi,

Just recording a little bug that is showing up with abletech's code in 3.5.2 A PHP Warning can show in some cases. I found what appears to be a fix:
function buildContaintersOutput(&$containers_outputs, &$childs, $id=0) {
    // Fix: add the next three lines to make sure that $containers_outputs[$id] has a value
    if ( !isset($containers_outputs[$id]) ) {
        $containers_outputs[$id] = '';
    }
    if (isset($childs[$id])) {
        foreach ($childs[$id] as $child) {
            $containers_outputs[$child] = buildContaintersOutput($containers_outputs, $childs, $child);
            $containers_outputs[$id] = str_replace('<!--CONTAINER_ELEMENTS-'.$child.'-->', $containers_outputs[$child], $containers_outputs[$id]);
        }
    }
    return $containers_outputs[$id];
}

Bob
This topic is locked and no more replies can be posted.