예제 #1
0
function dataExtraction($frame = "", $form, $filter, $andor, $scope, $limitStart, $limitSize, $sortField, $sortOrder, $forceQuery, $mainFormOnly, $includeArchived = false, $id_reqsOnly = false, $resultOnly = false, $filterElements = null)
{
    global $xoopsDB;
    $limitStart = intval($limitStart);
    $limitSize = intval($limitSize);
    $sortField = formulize_db_escape($sortField);
    if (isset($_GET['debug'])) {
        $time_start = microtime_float();
    }
    if ($scope == "uid=\"blankscope\"") {
        return array();
    }
    if (is_numeric($frame)) {
        $frid = $frame;
    } elseif ($frame != "") {
        $frameid = go("SELECT frame_id FROM " . DBPRE . "formulize_frameworks WHERE frame_name='{$frame}'");
        $frid = $frameid[0]['frame_id'];
        unset($frameid);
    } else {
        $frid = "";
    }
    if (is_numeric($form)) {
        $fid = $form;
    } elseif ($GLOBALS['formulize_versionFourOrHigher'] == false) {
        $formcheck = go("SELECT ff_form_id FROM " . DBPRE . "formulize_framework_forms WHERE ff_frame_id='{$frid}' AND ff_handle='{$form}'");
        $fid = $formcheck[0]['ff_form_id'];
        unset($formcheck);
    }
    if (!$fid) {
        print "Form Name: " . $form . "<br>";
        print "Form id: " . $fid . "<br>";
        print "Frame Name: " . $frame . "<br>";
        print "Frame id: " . $frid . "<br>";
        exit("selected form does not exist in framework");
    }
    $form_handler = xoops_getmodulehandler('forms', 'formulize');
    $formObject = $form_handler->get($fid);
    if ($frid and !$mainFormOnly) {
        // GET THE LINK INFORMATION FOR THE CURRENT FRAMEWORK BASED ON THE REQUESTED FORM
        $linklist1 = go("SELECT fl_form2_id, fl_key1, fl_key2, fl_relationship, fl_unified_display, fl_common_value FROM " . DBPRE . "formulize_framework_links WHERE fl_frame_id = '{$frid}' AND fl_form1_id = '{$fid}'");
        $linklist2 = go("SELECT fl_form1_id, fl_key1, fl_key2, fl_relationship, fl_unified_display, fl_common_value FROM " . DBPRE . "formulize_framework_links WHERE fl_frame_id = '{$frid}' AND fl_form2_id = '{$fid}'");
    }
    // link list 1 is the list of form2s that the requested form links to
    // link list 2 is the list of form1s that the requested form links to
    // ie: the link list number denotes the position of the requested form in the pair
    //	print "Frame: $frame ($frid)<br>";
    //	print "Form: $form ($fid)<br>";
    // generate the list of key fields in the current form, so we can use the values in these fields to filter the linked forms. -- sept 27 2005
    $linkkeys1 = array();
    $linkisparent1 = array();
    $linkformids1 = array();
    $linktargetids1 = array();
    $linkselfids1 = array();
    $linkcommonvalue1 = array();
    $linkkeys2 = array();
    $linkisparent2 = array();
    $linkformids2 = array();
    $linktargetids2 = array();
    $linkselfids2 = array();
    $linkcommonvalue2 = array();
    if ($frid and !$mainFormOnly) {
        if (count($linklist1) > 0) {
            foreach ($linklist1 as $theselinks) {
                $linkformids1[] = $theselinks['fl_form2_id'];
                if ($theselinks['fl_key1'] != 0) {
                    $handleforlink = formulize_getElementHandleFromID($theselinks['fl_key1']);
                    $linkkeys1[] = $handleforlink;
                    $linktargetids1[] = $theselinks['fl_key2'];
                    $linkselfids1[] = $theselinks['fl_key1'];
                } else {
                    $linkkeys1[] = "";
                    $linktargetids1[] = "";
                }
                if ($theselinks['fl_relationship'] == 2) {
                    // 2 is one to many relationship...this does not appear to be referenced anywhere in the extraction layer!
                    $linkisparent1[] = 1;
                } else {
                    $linkisparent1[] = 0;
                }
                $linkcommonvalue1[] = $theselinks['fl_common_value'];
            }
        }
        if (count($linklist2) > 0) {
            foreach ($linklist2 as $theselinks) {
                $linkformids2[] = $theselinks['fl_form1_id'];
                if ($theselinks['fl_key2'] != 0) {
                    $handleforlink = formulize_getElementHandleFromID($theselinks['fl_key2']);
                    $linkkeys2[] = $handleforlink;
                    $linktargetids2[] = $theselinks['fl_key1'];
                    $linkselfids2[] = $theselinks['fl_key2'];
                } else {
                    $linkkeys2[] = "";
                    $linktargetids2[] = "";
                }
                if ($theselinks['fl_relationship'] == 3) {
                    // 3 is many to one relationship...this does not appear to be referenced anywhere in the extraction layer!
                    $linkisparent2[] = 1;
                } else {
                    $linkisparent2[] = 0;
                }
                $linkcommonvalue2[] = $theselinks['fl_common_value'];
            }
        }
        $linkkeys = array_merge($linkkeys1, $linkkeys2);
        $linkisparent = array_merge($linkisparent1, $linkisparent2);
        $linkformids = array_merge($linkformids1, $linkformids2);
        $linktargetids = array_merge($linktargetids1, $linktargetids2);
        $linkselfids = array_merge($linkselfids1, $linkselfids2);
        $linkcommonvalue = array_merge($linkcommonvalue1, $linkcommonvalue2);
    } else {
        $linkkeys = "";
        $linkisparent = "";
        $linkformids = array();
        $linktargetids = "";
        $linkselfids = "";
        $linkcommonvalue = "";
    }
    //print_r( $linkformids );
    $GLOBALS['formulize_linkformidsForCalcs'] = $linkformids;
    // now that we have the full details from the framework, figure out the full SQL necessary to get the entire dataset
    // This whole approach is predicated on being able to do reliable joins between the key fields of each form
    // Structure of the SQL should be...
    // SELECT main.entry_id, main.creation_uid, main.mod_uid, main.creation_datetime, main.mod_datetime, main.handle1...main.handleN, f2.entry_id, f2.1..f2.n, etc FROM formulize_A AS main [join syntax] WHERE main.handle1 = "whatever" AND/OR f2.handle1 = "whatever"
    // Join syntax:  if there are query terms on the f2 or subsequent forms, then use INNER JOIN formulize_B AS f2 ON main.1 LIKE CONCAT('%,', f2.entry_id, ',%') -- or no %, ,% if only one value is allowed
    // If there are no query terms on the f2 or subsequent forms, then use LEFT JOIN
    // establish the join type and all that
    $joinText = "";
    $linkSelect = "";
    $exportOverrideQueries = array();
    $limitClause = "";
    if ($limitSize) {
        $limitClause = " LIMIT {$limitStart}, {$limitSize} ";
    }
    if (is_array($filter) or substr($filter, 0, 6) != "SELECT" and substr($filter, 0, 6) != "INSERT") {
        // if the filter is not itself a fully formed SQL statement...
        $scopeFilter = "";
        if (is_array($scope)) {
            // assume any arrays are groupid arrays, and so make a valid scope string based on this.  Use the new entry owner table.
            if (count($scope) > 0) {
                $start = true;
                foreach ($scope as $groupid) {
                    // need to loop through the array, and not use implode, so we can sanitize the values
                    if (!$start) {
                        $scopeFilter .= " OR scope.groupid=" . intval($groupid);
                    } else {
                        $start = false;
                        $scopeFilter = " AND EXISTS(SELECT 1 FROM " . DBPRE . "formulize_entry_owner_groups AS scope WHERE (scope.entry_id=main.entry_id AND scope.fid=" . intval($fid) . ") AND (scope.groupid=" . intval($groupid);
                    }
                }
                $scopeFilter .= ")) ";
                // need two closing brackets for the exists statement and its where clause
            } else {
                // no valid entries found, so show no entries
                $scopeFilter = " AND main.entry_id<0 ";
            }
        } elseif ($scope) {
            // need to handle old "uid = X OR..." syntax
            $scopeFilter = " AND (" . str_replace("uid", "main.creation_uid", $scope) . ") ";
        }
        formulize_getElementMetaData("", false, $fid);
        // initialize the element metadata for this form...serious performance gain from this
        list($formFieldFilterMap, $whereClause, $orderByClause, $oneSideFilters, $otherPerGroupFilterJoins, $otherPerGroupFilterWhereClause) = formulize_parseFilter($filter, $andor, $linkformids, $fid, $frid);
        // ***********************
        // NOTE:  the oneSideFilters are divided into two sections, the AND filters and OR filters for a given form
        // These will need to be constructed differently if we are ever to support OR filters that are spread across forms.
        // Right now, oneSideFilters get rendered with all other filters for their form, which is fine if the OR filters all belong to the same form
        // But if there are OR filters on two different forms, then we will need to do some kind of much more complex handling of the OR filters, or else the count query, and the queries for calculations, will be screwed up
        // The proper approach to this would be to have the AND oneSideFilters divided by form, like now, but for the ORs, we would need to loop through all ORs on all forms at once, and concatenate them somehow, ie:
        // foreach($oneSideFilters as $thisOneSideFid=>$oneSideFilterData) {
        //   foreach($oneSideFilterData as $oneSideAndOr=>$thisOneSideFilter) {
        //     if($oneSideAndOr == "and") { // note, it's forced to lowercase in the parseFilter function
        //       // then add this filter to the exists/other construction/whatever for this particular form
        //     } else {
        //       // then add this filter to a more complex exists/other construction/whatever that contains all the ORs, grouped by form, with ORs in between them
        //       // ie, the final output would be like:  AND ( exists(select 1 from table1 where field11=x or field12=y) OR exists(select 1 from table2 where field21 LIKE '%t%') OR (exists(select 1 from table3 where field31 > 23) )
        //       // And this entire construction would be then added to queries, to account properly for all the OR operators that had been requested
        //     }
        //   }
        // }
        // NOTE: Oct 17 2011 -- since we are now splitting multiform queries into may different individual collections of entries, it may be possible to do what's suggested above more easily. However, we still need the full where clause at our disposal in the main query that gets the main form entry ids, or else we'll have an incorrect master list of entry ids to return.  :-(
        // ***********************
        if (isset($oneSideFilters[$fid])) {
            foreach ($oneSideFilters[$fid] as $thisOneSideFilter) {
                $mainFormWhereClause .= " AND ( {$thisOneSideFilter} ) ";
            }
        } else {
            $mainFormWhereClause = "";
        }
        if ($whereClause) {
            $whereClause = "AND {$whereClause}";
        }
        // create the per-group filters, if any, that apply to this user...only available when all XOOPS is invoked, not available when extract.php is being direct included
        global $xoopsDB;
        $perGroupFilter = "";
        $perGroupFiltersPerForms = array();
        // used with exists clauses and other per-form situations
        if ($xoopsDB) {
            $form_handler = xoops_getmodulehandler('forms', 'formulize');
            $perGroupFilter = $form_handler->getPerGroupFilterWhereClause($fid, "main");
            $perGroupFiltersPerForms[$fid] = $perGroupFilter;
            if ($frid) {
                foreach ($linkformids as $id => $thisLinkFid) {
                    $perGroupFiltersPerForms[$thisLinkFid] = $form_handler->getPerGroupFilterWhereClause($thisLinkFid, "f" . $id);
                }
            }
        }
        if ($frid) {
            $joinHandles = formulize_getJoinHandles(array(0 => $linkselfids, 1 => $linktargetids));
            // get the element handles for these elements, since we need those to properly construct the join clauses
            $newJoinText = "";
            // "new" variables initilized in each loop
            $joinTextIndex = array();
            $joinTextTableRef = array();
            $linkSelectIndex = array();
            $newexistsJoinText = "";
            $joinText = "";
            // not "new" variables persist (with .= operator)
            $existsJoinText = "";
            foreach ($linkformids as $id => $linkedFid) {
                // validate that the join conditions are valid...either both must have a value, or neither must have a value (match on user id)...otherwise the join is not possible
                if ($joinHandles[$linkselfids[$id]] and $joinHandles[$linktargetids[$id]] or $linkselfids[$id] == '' and $linktargetids[$id] == '') {
                    formulize_getElementMetaData("", false, $linkedFid);
                    // initialize the element metadata for this form...serious performance gain from this
                    $linkSelectIndex[$linkedFid] = "f{$id}.entry_id AS f" . $id . "_entry_id, f{$id}.creation_uid AS f" . $id . "_creation_uid, f{$id}.mod_uid AS f" . $id . "_mod_uid, f{$id}.creation_datetime AS f" . $id . "_creation_datetime, f{$id}.mod_datetime AS f" . $id . "_mod_datetime, f{$id}.*";
                    $linkSelect .= ", f{$id}.entry_id AS f" . $id . "_entry_id, f{$id}.creation_uid AS f" . $id . "_creation_uid, f{$id}.mod_uid AS f" . $id . "_mod_uid, f{$id}.creation_datetime AS f" . $id . "_creation_datetime, f{$id}.mod_datetime AS f" . $id . "_mod_datetime, f{$id}.*";
                    $joinType = isset($formFieldFilterMap[$linkedFid]) ? "INNER" : "LEFT";
                    $linkedFormObject = $form_handler->get($linkedFid);
                    $joinTextTableRef[$linkedFid] = DBPRE . "formulize_" . $linkedFormObject->getVar('form_handle') . " AS f{$id} ON ";
                    $joinText .= " {$joinType} JOIN " . DBPRE . "formulize_" . $linkedFormObject->getVar('form_handle') . " AS f{$id} ON";
                    // NOTE: we are aliasing the linked form tables to f$id where $id is the key of the position in the linked form metadata arrays where that form's info is stored
                    $newexistsJoinText = $existsJoinText ? " {$andor} " : "";
                    $newexistsJoinText .= " EXISTS(SELECT 1 FROM " . DBPRE . "formulize_" . $linkedFormObject->getVar('form_handle') . " AS f{$id} WHERE ";
                    // set this up also so we have it available for one to many/many to one calculations that require it
                    if ($linkcommonvalue[$id]) {
                        // common value
                        $newJoinText = " main.`" . $joinHandles[$linkselfids[$id]] . "`=f{$id}.`" . $joinHandles[$linktargetids[$id]] . "`";
                    } elseif ($linktargetids[$id]) {
                        // linked selectbox
                        if ($target_ele_value = formulize_isLinkedSelectBox($linktargetids[$id])) {
                            if ($target_ele_value[1]) {
                                // multiple values allowed
                                $newJoinText = " f{$id}.`" . $joinHandles[$linktargetids[$id]] . "` LIKE CONCAT('%,',main.entry_id,',%')";
                            } else {
                                // single value only
                                $newJoinText = " f{$id}.`" . $joinHandles[$linktargetids[$id]] . "` = main.entry_id";
                            }
                        } else {
                            $main_ele_value = formulize_isLinkedSelectBox($linkselfids[$id]);
                            //  we know it's linked because this is a linked selectbox join, we just need the ele_value properties
                            if ($main_ele_value[1]) {
                                // multiple values allowed
                                $newJoinText = " main.`" . $joinHandles[$linkselfids[$id]] . "` LIKE CONCAT('%,',f{$id}.entry_id,',%')";
                            } else {
                                // single value only
                                $newJoinText = " main.`" . $joinHandles[$linkselfids[$id]] . "` = f{$id}.entry_id";
                            }
                        }
                    } else {
                        // join by uid
                        $newJoinText = " main.creation_uid=f{$id}.creation_uid";
                    }
                    if (isset($perGroupFiltersPerForms[$linkedFid])) {
                        $newJoinText .= $perGroupFiltersPerForms[$linkedFid];
                    }
                    $joinTextIndex[$linkedFid] = $newJoinText;
                    $joinText .= $newJoinText;
                    if (count($oneSideFilters[$linkedFid]) > 0) {
                        // only setup the existsJoinText when there is a where clause that applies to this form...otherwise, we don't care, this form is not relevant to the query that the calculations will do (except maybe when the mainform is not the one-side form...but that's another story)
                        $existsJoinText .= $newexistsJoinText . $newJoinText;
                        foreach ($oneSideFilters[$linkedFid] as $thisOneSideFilter) {
                            $thisLinkedFidPerGroupFilter = isset($perGroupFiltersPerForms[$linkedFid]) ? $perGroupFiltersPerForms[$linkedFid] : "";
                            $existsJoinText .= " AND ( {$thisOneSideFilter} {$thisLinkedFidPerGroupFilter}) ";
                        }
                        $existsJoinText .= ") ";
                        // close the exists clause itself
                    }
                }
            }
        }
        // specify the join info for user table (depending whether there's a query on creator_email or not)
        $userJoinType = $formFieldFilterMap['creator_email'] ? "INNER" : "LEFT";
        $userJoinText = " {$userJoinType} JOIN " . DBPRE . "users AS usertable ON main.creation_uid=usertable.uid";
        $sortIsOnMain = true;
        if (!$orderByClause and $sortField) {
            if ($sortField == "creation_uid" or $sortField == "mod_uid" or $sortField == "creation_datetime" or $sortField == "mod_datetime") {
                $elementMetaData['id_form'] = $fid;
            } elseif ($sortField == "uid") {
                $sortField = "creation_uid";
                $elementMetaData['id_form'] = $fid;
            } elseif ($sortField == "proxyid") {
                $sortField = "mod_uid";
                $elementMetaData['id_form'] = $fid;
            } elseif ($sortField == "creation_date") {
                $sortField = "creation_datetime";
                $elementMetaData['id_form'] = $fid;
            } elseif ($sortField == "mod_date") {
                $sortField = "mod_datetime";
                $elementMetaData['id_form'] = $fid;
            } elseif ($sortField == "entry_id") {
                $sortField = "entry_id";
                $elementMetaData['id_form'] = $fid;
            } else {
                $elementMetaData = formulize_getElementMetaData($sortField, true);
                // need to get form that sort field is part of...
            }
            $sortFid = $elementMetaData['id_form'];
            if ($sortFid == $fid) {
                $sortFidAlias = "main";
            } else {
                $sortFidAlias = array_keys($linkformids, $sortFid);
                // position of this form in the linking relationships is important for identifying which form alias to use
                $sortFidAlias = "f" . $sortFidAlias[0];
                $sortIsOnMain = false;
            }
            $sortFieldMetaData = formulize_getElementMetaData($sortField, true);
            if ($sortFieldMetaData['ele_encrypt']) {
                $sortFieldFullValue = "AES_DECRYPT({$sortFidAlias}.`{$sortField}`, '" . getAESPassword() . "')";
                // sorts as text, which will screw up number fields
            } elseif (formulize_isLinkedSelectBox($sortField, true)) {
                $ele_value = unserialize($sortFieldMetaData['ele_value']);
                $boxproperties = explode("#*=:*", $ele_value[2]);
                $target_fid = $boxproperties[0];
                $target_element_handle = $boxproperties[1];
                $form_handler = xoops_getmodulehandler('forms', 'formulize');
                $targetFormObject = $form_handler->get($target_fid);
                // note you cannot sort by multi select boxes!
                $sortFieldFullValue = "(SELECT sourceSortForm.`" . $target_element_handle . "` FROM " . DBPRE . "formulize_" . $targetFormObject->getVar('form_handle') . " as sourceSortForm WHERE sourceSortForm.`entry_id` = " . $sortFidAlias . ".`" . $sortField . "`)";
            } else {
                $sortFieldFullValue = "{$sortFidAlias}.`{$sortField}`";
            }
            $orderByClause = " ORDER BY {$sortFieldFullValue} {$sortOrder} ";
        } elseif (!$orderByClause) {
            $orderByClause = "ORDER BY main.entry_id";
        }
        debug_memory("Before retrieving mainresults");
        //$beforeQueryTime = microtime_float();
        $countMasterResults = "SELECT COUNT(main.entry_id) FROM " . DBPRE . "formulize_" . $formObject->getVar('form_handle') . " AS main ";
        $countMasterResults .= "{$userJoinText} {$otherPerGroupFilterJoins} WHERE main.entry_id>0 {$mainFormWhereClause} {$scopeFilter} {$otherPerGroupFilterWhereClause} ";
        $countMasterResults .= $existsJoinText ? " AND ({$existsJoinText}) " : "";
        $countMasterResults .= isset($perGroupFiltersPerForms[$fid]) ? $perGroupFiltersPerForms[$fid] : "";
        if (isset($GLOBALS['formulize_getCountForPageNumbers'])) {
            // If there's an LOE Limit in place, check that we're not over it first
            global $formulize_LOE_limit;
            if ($countMasterResultsRes = $xoopsDB->query($countMasterResults)) {
                $countMasterResultsRow = $xoopsDB->fetchRow($countMasterResultsRes);
                if ($countMasterResultsRow[0] > $formulize_LOE_limit and $formulize_LOE_limit > 0 and !$forceQuery and !$limitClause) {
                    return $countMasterResultsRow[0];
                } else {
                    // if we're in a getData call from displayEntries, put the count in a special place for use in generating page numbers
                    $GLOBALS['formulize_countMasterResultsForPageNumbers'] = $countMasterResultsRow[0];
                }
            } else {
                exit("Error: could not count master results.<br>" . $xoopsDB->error() . "<br>SQL:{$countMasterResults}<br>");
            }
            unset($GLOBALS['formulize_getCountForPageNumbers']);
        }
        // now, if there's framework in effect, get the entry ids of the entries in the main form that match the criteria, so we can use a specific query for them instead of the order clause in the master query
        $limitByEntryId = "";
        $useAsSortSubQuery = "";
        if ($frid) {
            $limitByEntryId = " AND (";
            $entryIdQuery = str_replace("COUNT(main.entry_id)", "main.entry_id as main_entry_id", $countMasterResults);
            // don't count the entries, select their id numbers
            if (!$sortIsOnMain) {
                $sortFieldMetaData = formulize_getElementMetaData($sortField, true);
                $sortFormObject = $form_handler->get($sortFid);
                if ($sortFieldMetaData['ele_encrypt']) {
                    $useAsSortSubQuery = "(SELECT max(AES_DECRYPT(`{$sortField}`, '" . getAESPassword() . "')) as subsort FROM " . DBPRE . "formulize_" . $sortFormObject->getVar('form_handle') . " as {$sortFidAlias} WHERE " . $joinTextIndex[$sortFid] . " ORDER BY subsort {$sortOrder}) as usethissort";
                } else {
                    $useAsSortSubQuery = "(SELECT max(`{$sortField}`) as subsort FROM " . DBPRE . "formulize_" . $sortFormObject->getVar('form_handle') . " as {$sortFidAlias} WHERE " . $joinTextIndex[$sortFid] . " ORDER BY subsort {$sortOrder}) as usethissort";
                }
                $entryIdQuery = str_replace("SELECT main.entry_id as main_entry_id ", "SELECT {$useAsSortSubQuery}, main.entry_id as main_entry_id ", $entryIdQuery);
                // sorts as text which will screw up number fields
                $thisOrderByClause = " ORDER BY usethissort {$sortOrder} ";
            } else {
                $thisOrderByClause = $orderByClause;
            }
            $entryIdQuery .= " {$thisOrderByClause} {$limitClause}";
            $entryIdResult = $xoopsDB->query($entryIdQuery);
            $start = true;
            while ($entryIdValue = $xoopsDB->fetchArray($entryIdResult)) {
                $limitByEntryId .= !$start ? " OR " : "";
                $limitByEntryId .= "main.entry_id = " . $entryIdValue['main_entry_id'];
                $start = false;
            }
            $limitByEntryId .= ") ";
            if (!$start) {
                $limitClause = "";
                // nullify the existing limitClause since we don't want to use it in the actual query
            } else {
                $limitByEntryId = "";
            }
        }
        $selectClause = "";
        $sqlFilterElements = array();
        if ($filterElements) {
            // THIS IS HIGHLY EXPERIMENTAL...BECAUSE THE PROCESSING OF DATASETS RELIES RIGHT NOW ON METADATA BEING PRESENT AT THE FRONT OF EACH SET OF FIELDS, THERE IS FURTHER WORK REQUIRED TO MAKE THIS FUNCTION WITH THE CODE THAT PROCESSES ENTRIES
            //print_r( $filterElements );
            //print_r( $linkformids );
            foreach ($filterElements as $passedForm => $passedElements) {
                if ($passedForm == $fid) {
                    $formAlias = "main";
                } else {
                    $keys = array_keys($linkformids, $passedForm);
                    //print_r( $keys );
                    $formAlias = "f" . $keys[0];
                }
                foreach ($passedElements as $thisPassedElement) {
                    $sqlFilterElements[] = $formAlias . ".`" . formulize_db_escape($thisPassedElement) . "`";
                }
            }
        }
        if (count($sqlFilterElements) > 0) {
            $selectClause = implode(",", $sqlFilterElements);
        } else {
            $selectClause = "main.entry_id AS main_entry_id, main.creation_uid AS main_creation_uid, main.mod_uid AS main_mod_uid, main.creation_datetime AS main_creation_datetime, main.mod_datetime AS main_mod_datetime, main.* {$linkSelect}";
        }
        // if this is being done for gathering calculations, and the calculation is requested on the one side of a one to many/many to one relationship, then we will need to use different SQL to avoid duplicate values being returned by the database
        // note: when the main form is on the many side of the relationship, then we need to do something rather different...not sure what it is yet...the SQL as prepared is based on the calculation field and the main form being the one side (and so both are called main), but when field is on one side and main form is many side, then the aliases don't match, and scopefilter issues abound.
        // NOTE: Oct 17 2011 - the $oneSideSQL is also used when there are multiple linked subforms, since the exists structure is efficient compared to multiple joins
        $oneSideSQL = " FROM " . DBPRE . "formulize_" . $formObject->getVar('form_handle') . " AS main {$userJoinText} WHERE main.entry_id>0 {$scopeFilter} ";
        // does the mainFormWhereClause need to be used here too?  Needs to be tested. -- further note: Oct 17 2011 -- appears oneSideFilters[fid] is the same as the mainformwhereclause
        $oneSideSQL .= $existsJoinText ? " AND ({$existsJoinText}) " : "";
        if (count($oneSideFilters[$fid]) > 0) {
            foreach ($oneSideFilters[$fid] as $thisOneSideFilter) {
                $oneSideSQL .= " {$andor} ( {$thisOneSideFilter} ) ";
                // properly introduce these filters...need to move $andor to a higher level and put this inside ( ) ?? or maybe this just all gets redone if/when the OR bug is fixed (see big note up where oneSideFilters are first received from parseFilter function)
            }
        }
        $oneSideSQL .= isset($perGroupFiltersPerForms[$fid]) ? $perGroupFiltersPerForms[$fid] : "";
        $restOfTheSQL = " FROM " . DBPRE . "formulize_" . $formObject->getVar('form_handle') . " AS main {$userJoinText} {$joinText} {$otherPerGroupFilterJoins} WHERE main.entry_id>0 {$whereClause} {$scopeFilter} {$perGroupFilter} {$otherPerGroupFilterWhereClause} {$limitByEntryId} {$orderByClause} ";
        $restOfTheSQLForExport = " FROM " . DBPRE . "formulize_" . $formObject->getVar('form_handle') . " AS main {$userJoinText} {$joinText} {$otherPerGroupFilterJoins} WHERE main.entry_id>0 {$whereClause} {$scopeFilter} {$perGroupFilter} {$otherPerGroupFilterWhereClause} {$orderByClause} ";
        // don't use limitByEntryId since exports include all entries
        if (count($linkformids) > 1) {
            // AND $dummy == "never") { // when there is more than 1 joined form, we can get an exponential explosion of records returned, because SQL will give you all combinations of the joins
            if (!$sortIsOnMain) {
                $orderByToUse = " ORDER BY usethissort {$sortOrder} ";
                $useAsSortSubQuery = " @rownum:=@rownum+1, {$useAsSortSubQuery},";
                // need to add a counter as the first field, used as the master sorting key
            } else {
                $orderByToUse = $orderByClause;
                $useAsSortSubQuery = "  @rownum:=@rownum+1, ";
                // need to add a counter as the first field, used as the master sorting key
            }
            $oneSideSQLToUse = str_replace(" AS main {$userJoinText}", " AS main JOIN (SELECT @rownum := 0) as r {$userJoinText}", $oneSideSQL);
            // need to add the initialization of the rownum, which is what we use as the master sorting key
            $masterQuerySQL = "SELECT {$useAsSortSubQuery} main.entry_id {$oneSideSQLToUse} {$limitByEntryId} {$orderByToUse} ";
            $masterQuerySQLForExport = "SELECT {$useAsSortSubQuery} main.entry_id {$oneSideSQLToUse} {$orderByToUse} ";
            // no limit by entry id, since all entries should be included in exports
            if (!$resultOnly) {
                // so let's build a temp table with the unique entry ids in the forms that we care about, and then query each linked form separately for its records, so that we end up processing as few result rows as possible
                $masterQuerySQL = "INSERT INTO " . DBPRE . "formulize_temp_extract_REPLACEWITHTIMESTAMP {$masterQuerySQL} ";
                $masterQuerySQLForExport = "INSERT INTO " . DBPRE . "formulize_temp_extract_REPLACEWITHTIMESTAMP {$masterQuerySQLForExport} ";
            }
        } else {
            $masterQuerySQL = "SELECT {$selectClause}, usertable.email AS main_email, usertable.user_viewemail AS main_user_viewemail {$restOfTheSQL} ";
            $masterQuerySQLForExport = "SELECT {$selectClause}, usertable.email AS main_email, usertable.user_viewemail AS main_user_viewemail {$restOfTheSQLForExport} ";
        }
        $GLOBALS['formulize_queryForCalcs'] = " FROM " . DBPRE . "formulize_" . $formObject->getVar('form_handle') . " AS main {$userJoinText} {$joinText} WHERE main.entry_id>0  {$whereClause} {$scopeFilter} ";
        $GLOBALS['formulize_queryForCalcs'] .= isset($perGroupFiltersPerForms[$fid]) ? $perGroupFiltersPerForms[$fid] : "";
        $GLOBALS['formulize_queryForOneSideCalcs'] = $oneSideSQL;
        if ($GLOBALS['formulize_returnAfterSettingBaseQuery']) {
            return true;
        }
        // if we are only setting up calculations, then return now that the base query is built
        $sortIsOnMainFlag = $sortIsOnMain ? 1 : 0;
        // need to include the query first, so the SELECT or INSERT is the first thing in the string, so we catch it properly when coming back through the export process
        $GLOBALS['formulize_queryForExport'] = $masterQuerySQLForExport . " -- SEPARATOR FOR EXPORT QUERIES -- " . $sortIsOnMainFlag;
        // "$selectClauseToUse FROM " . DBPRE . "formulize_" . $formObject->getVar('form_handle') . " AS main $userJoinText $joinText $otherPerGroupFilterJoins WHERE main.entry_id>0 $whereClause $scopeFilter $perGroupFilter $otherPerGroupFilterWhereClause $limitByEntryId $orderByClause $limitClause";
    } else {
        // end of if the filter has a SELECT in it
        if (strstr($filter, " -- SEPARATOR FOR EXPORT QUERIES -- ")) {
            $exportOverrideQueries = explode(" -- SEPARATOR FOR EXPORT QUERIES -- ", $filter);
            $masterQuerySQL = $exportOverrideQueries[0];
            $sortIsOnMain = $exportOverrideQueries[1];
        } else {
            $masterQuerySQL = $filter;
            // need to split this based on some separator, because export ends up passing in a series of statements
        }
    }
    // after the export query has been generated, then let's put the limit on:
    $masterQuerySQL .= $limitClause;
    /*global $xoopsUser;
      if($xoopsUser->getVar('uid') == 4613) {
           $queryTime = $afterQueryTime - $beforeQueryTime;
           print "Query time: " . $queryTime . "<br>";
      }*/
    debug_memory("After retrieving mainresults");
    // Debug Code
    //global $xoopsUser;
    //if($xoopsUser->getVar('uid') == 1) {
    //     print "<br>Count query: $countMasterResults<br><br>";
    //     print "Master query: $masterQuerySQL<br>";
    //}
    formulize_benchmark("Before query");
    if (count($linkformids) > 1) {
        // AND $dummy=="never") { // when there is more than 1 joined form, we can get an exponential explosion of records returned, because SQL will give you all combinations of the joins, so we create a series of queries that will each handle the main form plus one of the linked forms, then we put all the data together into a single result set below
        if ($resultOnly) {
            $masterQueryRes = $xoopsDB->query($masterQuerySQL);
        } else {
            $timestamp = str_replace(".", "", microtime(true));
            if (!$sortIsOnMain) {
                $creatTableSQL = "CREATE TABLE " . DBPRE . "formulize_temp_extract_{$timestamp} ( `mastersort` BIGINT(11), `throwaway_sort_values` BIGINT(11), `entry_id` BIGINT(11), PRIMARY KEY (`mastersort`), INDEX i_entry_id (`entry_id`) ) ENGINE=MyISAM;";
                // when the sort is not on the main form, then we are including a special field in the select statement that we sort it by, so that the order is correct, and so it has to have a place to get inserted here
            } else {
                $creatTableSQL = "CREATE TABLE " . DBPRE . "formulize_temp_extract_{$timestamp} ( `mastersort` BIGINT(11), `entry_id` BIGINT(11), PRIMARY KEY (`mastersort`), INDEX i_entry_id (`entry_id`) ) ENGINE=MyISAM;";
            }
            $createTableRes = $xoopsDB->queryF($creatTableSQL);
            $gatherIdsRes = $xoopsDB->queryF(str_replace("REPLACEWITHTIMESTAMP", $timestamp, $masterQuerySQL));
            $linkQueryRes = array();
            if (isset($exportOverrideQueries[2])) {
                for ($i = 2; $i < count($exportOverrideQueries); $i++) {
                    $sql = str_replace("REPLACEWITHTIMESTAMP", $timestamp, $exportOverrideQueries[$i]);
                    $linkQueryRes[] = $xoopsDB->query($sql);
                }
            } else {
                // FURTHER OPTIMIZATIONS ARE POSSIBLE HERE...WE COULD NOT INCLUDE THE MAIN FORM AGAIN IN ALL THE SELECTS, THAT WOULD IMPROVE THE PROCESSING TIME A BIT, BUT WE WOULD HAVE TO CAREFULLY REFACTOR MORE OF THE LOOPING CODE BELOW THAT PARSES THE ENTRIES, BECAUSE RIGHT NOW IT'S ASSUMING THE FULL MAIN ENTRY IS PRESENT.  AT LEAST THE MAIN ENTRY ID WOULD NEED TO STILL BE USED, SINCE WE USE THAT TO SYNCH UP ALL THE ENTRIES FROM THE OTHER FORMS.
                foreach ($linkformids as $linkId => $thisLinkFid) {
                    $linkQuery = "SELECT\r\n   main.entry_id AS main_entry_id, main.creation_uid AS main_creation_uid, main.mod_uid AS main_mod_uid, main.creation_datetime AS main_creation_datetime, main.mod_datetime AS main_mod_datetime, main.*, " . $linkSelectIndex[$thisLinkFid] . ", usertable.email AS main_email, usertable.user_viewemail AS main_user_viewemail FROM " . DBPRE . "formulize_" . $formObject->getVar('form_handle') . " AS main\r\n   LEFT JOIN " . DBPRE . "users AS usertable ON main.creation_uid=usertable.uid\r\n   LEFT JOIN " . $joinTextTableRef[$thisLinkFid] . $joinTextIndex[$thisLinkFid] . "\r\n   INNER JOIN " . DBPRE . "formulize_temp_extract_REPLACEWITHTIMESTAMP as sort_and_limit_table ON main.entry_id = sort_and_limit_table.entry_id ";
                    if (isset($oneSideFilters[$thisLinkFid]) and is_array($oneSideFilters[$thisLinkFid])) {
                        $start = true;
                        foreach ($oneSideFilters[$thisLinkFid] as $thisOneSideFilter) {
                            if (!$start) {
                                $linkQuery .= " AND ( {$thisOneSideFilter} ) ";
                            } else {
                                $linkQuery .= " WHERE ( {$thisOneSideFilter} ) ";
                                $start = false;
                            }
                        }
                    }
                    $linkQuery .= " ORDER BY sort_and_limit_table.mastersort";
                    $linkQueryRes[] = $xoopsDB->query(str_replace("REPLACEWITHTIMESTAMP", $timestamp, $linkQuery));
                    $GLOBALS['formulize_queryForExport'] .= " -- SEPARATOR FOR EXPORT QUERIES -- " . $linkQuery;
                }
            }
            $dropRes = $xoopsDB->queryF("DROP TABLE " . DBPRE . "formulize_temp_extract_{$timestamp}");
        }
    } else {
        $masterQueryRes = $xoopsDB->query($masterQuerySQL);
    }
    if ($resultOnly) {
        if ($masterQueryRes) {
            if ($xoopsDB->getRowsNum($masterQueryRes) > 0) {
                return $masterQueryRes;
            } else {
                return false;
            }
        }
    }
    formulize_benchmark("After query");
    // need to calculate the derived value metadata
    // 1. figure out which fields in the included forms have derived values
    // 2. setup the metadata for those fields, according to the order they appear
    // -- metadata should be: formhandle (title or framework formhandle), formula, handle (element handle or framework handle)
    // 3. call the derived value function from inside the main loop
    $linkFormIdsFilter = "";
    if ($frid) {
        $linkFormIdsFilter = (is_array($linkformids) and count($linkformids) > 0) ? " OR t1.id_form IN (" . implode(",", $linkformids) . ") " : "";
    }
    $sql = "SELECT t1.ele_value, t2.desc_form, t1.ele_handle, t2.id_form FROM " . DBPRE . "formulize as t1, " . DBPRE . "formulize_id as t2 WHERE t1.ele_type='derived' AND (t1.id_form='{$fid}' {$linkFormIdsFilter} ) AND t1.id_form=t2.id_form ORDER BY t1.ele_order";
    $derivedFieldMetadata = array();
    if ($res = $xoopsDB->query($sql)) {
        if ($xoopsDB->getRowsNum($res) > 0) {
            $multipleIndexer = array();
            while ($row = $xoopsDB->fetchRow($res)) {
                $ele_value = unserialize($row[0]);
                // derived fields have ele_value as an array with only one element (that was done to future proof the data model, so we could add other things to ele_value if necessary)
                if (!isset($multipleIndexer[$row[1]])) {
                    $multipleIndexer[$row[1]] = 0;
                }
                $derivedFieldMetadata[$row[1]][$multipleIndexer[$row[1]]]['formula'] = $ele_value[0];
                // use row[1] (the form handle) as the key, so we can eliminate some looping later on
                $derivedFieldMetadata[$row[1]][$multipleIndexer[$row[1]]]['handle'] = $row[2];
                $derivedFieldMetadata[$row[1]][$multipleIndexer[$row[1]]]['form_id'] = $row[3];
                $multipleIndexer[$row[1]]++;
            }
        }
    } else {
        print "Error: could not check to see if there were derived value elements in one or more forms.  SQL:<br>{$sql}";
    }
    if (count($linkformids) > 1) {
        // AND $dummy == "never") {
        // this is a refactoring of the original code that is in the else part of this structure.
        // it's virtually the same, except for the part that sets the $masterIndexer, since we will need to reuse masterindex positions when parsing subsequent queries, so we don't just increment the $masterIndexer
        // Also, derived value formulas are processed all at the end, because until we've parsed the last query, we don't have a complete set of data for any record in the masterResults array
        // once this is proven stable, we should refactor this into a function and have a more unified/common way of parsing query results, but for now we'll keep it split out since we know the old way works intact in its current form and this new one is a little bit experimental
        // we need to loop through all the query results that were generated above, and gradually build up the same full results array out of them
        // then we also need to loop through all main entries one more time once we're done building, and set all the derived values
        // this is done to avoid an exponential explosion of results in the SQL, and instead we only have a linear progression of results to parse
        $masterResults = array();
        $masterIndexer = -1;
        $writtenMains = array();
        $masterQueryArrayIndex = array();
        foreach ($linkQueryRes as $thisRes) {
            // loop through the found data and create the dataset array in "getData" format
            $prevFieldNotMeta = true;
            $prevFormAlias = "";
            $prevMainId = "";
            while ($masterQueryArray = $xoopsDB->fetchArray($thisRes)) {
                foreach ($masterQueryArray as $field => $value) {
                    if ($field == "entry_id" or $field == "creation_uid" or $field == "mod_uid" or $field == "creation_datetime" or $field == "mod_datetime" or $field == "main_email" or $field == "main_user_viewemail") {
                        continue;
                    }
                    // ignore those plain fields, since we can only work with the ones that are properly aliased to their respective tables.  More details....Must refer to metadata fields by aliases only!  since * is included in SQL syntax, fetch_assoc will return plain column names from all forms with the values from those columns.....Also want to ignore the email fields, since the fact they're prefixed with "main" can throwoff the calculation of which entry we're currently writing
                    if (strstr($field, "creation_uid") or strstr($field, "creation_datetime") or strstr($field, "mod_uid") or strstr($field, "mod_datetime") or strstr($field, "entry_id")) {
                        // dealing with a new metadata field
                        $fieldNameParts = explode("_", $field);
                        // We account for a mainform entry appearing multiple times in the list, because when there are multiple entries in a subform, and SQL returns one row per subform,  we need to not change the main form and internal record until we pass to a new mainform entry
                        if ($prevFieldNotMeta) {
                            // only do once for each form
                            $curFormId = $fieldNameParts[0] == "main" ? $fid : $linkformids[substr($fieldNameParts[0], 1)];
                            // the table aliases are based on the keys of the linked forms in the linkformids array, so if we get the number out of the table alias, that key will give us the form id of the linked form as stored in the linkformids array
                            $prevFormAlias = $curFormAlias;
                            $curFormAlias = $fieldNameParts[0];
                            if ($prevFormAlias == "main") {
                                // if we just finished up a main form entry, then log that
                                $writtenMains[$prevMainId] = true;
                            }
                            //print "curFormAlias: $curFormAlias<br>prevMainId: $prevMainId<br>current main id: ". $masterQueryArray['main_entry_id'] . "<br><br>";
                            if ($curFormAlias == "main" and $prevMainId != $masterQueryArray['main_entry_id']) {
                                if ($writtenMains[$masterQueryArray['main_entry_id']]) {
                                    $masterIndexer = $masterQueryArrayIndex[$masterQueryArray['main_entry_id']];
                                    // use the master index value for this main entry id if we've already logged it
                                } else {
                                    $masterIndexer = count($masterResults);
                                    // use the next available number for the master indexer
                                    $masterQueryArrayIndex[$masterQueryArray['main_entry_id']] = $masterIndexer;
                                    // log it so we can reuse it for this entry when it comes up in another query
                                }
                                $prevMainId = $masterQueryArray['main_entry_id'];
                                // if the current form is a main, then store it's ID for use later when we're on a new form
                            }
                        }
                        $prevFieldNotMeta = false;
                        // setup handles to use for metadata fields
                        if ($curFormAlias == "main") {
                            if ($field == "main_creation_uid" or $field == "main_mod_uid" or $field == "main_creation_datetime" or $field == "main_mod_datetime" or $field == "main_entry_id") {
                                $elementHandle = $fieldNameParts[1] . "_" . $fieldNameParts[2];
                            }
                        } else {
                            continue;
                            // do not include metadata from the linked forms, or anything else (such as email, etc)
                        }
                    } elseif (!strstr($field, "main_email") and !strstr($field, "main_user_viewemail")) {
                        // dealing with a regular element field
                        $prevFieldNotMeta = true;
                        $elementHandle = $field;
                    } else {
                        // it's some other field...
                        continue;
                    }
                    // Check to see if this is a main entry that has already been catalogued, and if so, then skip it
                    if ($curFormAlias == "main" and isset($writtenMains[$masterQueryArray['main_entry_id']])) {
                        continue;
                    }
                    //print "<br>$curFormAlias - $field: $value<br>"; // debug line
                    formulize_benchmark("preping value...");
                    $valueArray = prepvalues($value, $elementHandle, $masterQueryArray[$curFormAlias . "_entry_id"]);
                    // note...metadata fields must not be in an array for compatibility with the 'display' function...not all values returned will actually be arrays, but if there are multiple values in a cell, then those will be arrays
                    formulize_benchmark("done preping value");
                    $masterResults[$masterIndexer][getFormTitle($curFormId)][$masterQueryArray[$curFormAlias . "_entry_id"]][$elementHandle] = $valueArray;
                    if ($elementHandle == "creation_uid" or $elementHandle == "mod_uid" or $elementHandle == "creation_datetime" or $elementHandle == "mod_datetime" or $elementHandle == "entry_id") {
                        // add in the creator_email when we have done the creation_uid
                        if ($elementHandle == "creation_uid") {
                            if (!isset($is_webmaster)) {
                                global $xoopsUser;
                                if (is_object($xoopsUser)) {
                                    // determine if the user is a webmaster, in order to control whether the e-mail addresses should be shown or not
                                    $is_webmaster = in_array(XOOPS_GROUP_ADMIN, $xoopsUser->getGroups()) ? true : false;
                                    $gperm_handler =& xoops_gethandler('groupperm');
                                    $view_private_fields = $gperm_handler->checkRight("view_private_elements", $fid, $xoopsUser->getGroups(), getFormulizeModId());
                                    $this_userid = $xoopsUser->getVar('uid');
                                } else {
                                    $view_private_fields = false;
                                    $is_webmaster = false;
                                    $this_userid = 0;
                                }
                            }
                            if ($is_webmaster or $view_private_fields or $masterQueryArray['main_user_viewemail'] or $masterQueryArray['main_creation_uid'] == $this_userid) {
                                $masterResults[$masterIndexer][getFormTitle($curFormId)][$masterQueryArray[$curFormAlias . "_entry_id"]]['creator_email'] = $masterQueryArray['main_email'];
                            } else {
                                $masterResults[$masterIndexer][getFormTitle($curFormId)][$masterQueryArray[$curFormAlias . "_entry_id"]]['creator_email'] = "";
                            }
                        }
                        // for backwards compatibility, replicate the old metadata fields
                        switch ($elementHandle) {
                            case "creation_uid":
                                $old_meta = "uid";
                                break;
                            case "mod_uid":
                                $old_meta = "proxyid";
                                break;
                            case "creation_datetime":
                                $old_meta = "creation_date";
                                break;
                            case "mod_datetime":
                                $old_meta = "mod_date";
                                break;
                        }
                        $masterResults[$masterIndexer][getFormTitle($curFormId)][$masterQueryArray[$curFormAlias . "_entry_id"]][$old_meta] = $valueArray;
                    }
                }
                // end of foreach field loop within a record
            }
            // end of main while loop for all records
        }
        // end of foreach linked query result
        if (count($derivedFieldMetadata) > 0 and $masterIndexer > -1) {
            // if there is derived value info for this data set and we have started to create values...need to do this one more time for the last value that we would have gathered data for...
            foreach ($masterResults as $masterIndex => $thisRecord) {
                $masterResults[$masterIndex] = formulize_calcDerivedColumns($thisRecord, $derivedFieldMetadata, $frid, $fid);
            }
        }
    } else {
        // loop through the found data and create the dataset array in "getData" format
        $prevFieldNotMeta = true;
        $masterIndexer = -1;
        $writtenMains = array();
        $prevFormAlias = "";
        $prevMainId = "";
        //formulize_benchmark("About to prepare results.");
        while ($masterQueryArray = $xoopsDB->fetchArray($masterQueryRes)) {
            set_time_limit(120);
            //formulize_benchmark("Starting to process one entry.");
            foreach ($masterQueryArray as $field => $value) {
                //formulize_benchmark("Starting to process one value");
                if ($field == "entry_id" or $field == "creation_uid" or $field == "mod_uid" or $field == "creation_datetime" or $field == "mod_datetime" or $field == "main_email" or $field == "main_user_viewemail") {
                    continue;
                }
                // ignore those plain fields, since we can only work with the ones that are properly aliased to their respective tables.  More details....Must refer to metadata fields by aliases only!  since * is included in SQL syntax, fetch_assoc will return plain column names from all forms with the values from those columns.....Also want to ignore the email fields, since the fact they're prefixed with "main" can throwoff the calculation of which entry we're currently writing
                if (strstr($field, "creation_uid") or strstr($field, "creation_datetime") or strstr($field, "mod_uid") or strstr($field, "mod_datetime") or strstr($field, "entry_id")) {
                    //formulize_benchmark("Starting to process metadata");
                    // dealing with a new metadata field
                    $fieldNameParts = explode("_", $field);
                    // We account for a mainform entry appearing multiple times in the list, because when there are multiple entries in a subform, and SQL returns one row per subform,  we need to not change the main form and internal record until we pass to a new mainform entry
                    if ($prevFieldNotMeta) {
                        // only do once for each form
                        $curFormId = $fieldNameParts[0] == "main" ? $fid : $linkformids[substr($fieldNameParts[0], 1)];
                        // the table aliases are based on the keys of the linked forms in the linkformids array, so if we get the number out of the table alias, that key will give us the form id of the linked form as stored in the linkformids array
                        $prevFormAlias = $curFormAlias;
                        $curFormAlias = $fieldNameParts[0];
                        if ($prevFormAlias == "main") {
                            // if we just finished up a main form entry, then log that
                            $writtenMains[$prevMainId] = true;
                        }
                        //print "curFormAlias: $curFormAlias<br>prevMainId: $prevMainId<br>current main id: ". $masterQueryArray['main_entry_id'] . "<br><br>";
                        if ($curFormAlias == "main" and $prevMainId != $masterQueryArray['main_entry_id']) {
                            //formulize_benchmark("Done entry, ready to do derived values.");
                            // now that the entire entry has been processed, do the derived values for it
                            if (count($derivedFieldMetadata) > 0 and $masterIndexer > -1) {
                                // if there is derived value info for this data set and we have started to create values...
                                //print "fid: $fid<br>";
                                //print "frid: $frid<br>";
                                formulize_benchmark("before doing derived...");
                                $masterResults[$masterIndexer] = formulize_calcDerivedColumns($masterResults[$masterIndexer], $derivedFieldMetadata, $frid, $fid);
                                formulize_benchmark("after doing derived");
                            }
                            $masterIndexer++;
                            // If this is a new main entry, then increment the masterIndexer, since the masterIndexer is used to uniquely identify each main entry
                            $prevMainId = $masterQueryArray['main_entry_id'];
                            // if the current form is a main, then store it's ID for use later when we're on a new form
                        }
                    }
                    $prevFieldNotMeta = false;
                    // setup handles to use for metadata fields
                    if ($curFormAlias == "main") {
                        if ($field == "main_creation_uid" or $field == "main_mod_uid" or $field == "main_creation_datetime" or $field == "main_mod_datetime" or $field == "main_entry_id") {
                            $elementHandle = $fieldNameParts[1] . "_" . $fieldNameParts[2];
                        } else {
                            continue;
                            // do not include main_entry_id as a value in the array...though it should not be in here anyway now that we're checking with strstr for metadata field names above
                        }
                    } else {
                        continue;
                        // do not include metadata from the linked forms, or anything else (such as email, etc)
                    }
                } elseif (!strstr($field, "main_email") and !strstr($field, "main_user_viewemail")) {
                    // dealing with a regular element field
                    $prevFieldNotMeta = true;
                    $elementHandle = $field;
                } else {
                    // it's some other field
                    continue;
                }
                // Check to see if this is a main entry that has already been catalogued, and if so, then skip it
                if ($curFormAlias == "main" and isset($writtenMains[$masterQueryArray['main_entry_id']])) {
                    continue;
                }
                //print "<br>$curFormAlias - $field: $value<br>"; // debug line
                formulize_benchmark("preping value...");
                $valueArray = prepvalues($value, $elementHandle, $masterQueryArray[$curFormAlias . "_entry_id"]);
                // note...metadata fields must not be in an array for compatibility with the 'display' function...not all values returned will actually be arrays, but if there are multiple values in a cell, then those will be arrays
                formulize_benchmark("done preping value");
                $masterResults[$masterIndexer][getFormTitle($curFormId)][$masterQueryArray[$curFormAlias . "_entry_id"]][$elementHandle] = $valueArray;
                if ($elementHandle == "creation_uid" or $elementHandle == "mod_uid" or $elementHandle == "creation_datetime" or $elementHandle == "mod_datetime" or $elementHandle == "entry_id") {
                    // add in the creator_email when we have done the creation_uid
                    if ($elementHandle == "creation_uid") {
                        if (!isset($is_webmaster)) {
                            global $xoopsUser;
                            if (is_object($xoopsUser)) {
                                // determine if the user is a webmaster, in order to control whether the e-mail addresses should be shown or not
                                $is_webmaster = in_array(XOOPS_GROUP_ADMIN, $xoopsUser->getGroups()) ? true : false;
                                $gperm_handler =& xoops_gethandler('groupperm');
                                $view_private_fields = $gperm_handler->checkRight("view_private_elements", $fid, $xoopsUser->getGroups(), getFormulizeModId());
                                $this_userid = $xoopsUser->getVar('uid');
                            } else {
                                $view_private_fields = false;
                                $is_webmaster = false;
                                $this_userid = 0;
                            }
                        }
                        if ($is_webmaster or $view_private_fields or $masterQueryArray['main_user_viewemail'] or $masterQueryArray['main_creation_uid'] == $this_userid) {
                            $masterResults[$masterIndexer][getFormTitle($curFormId)][$masterQueryArray[$curFormAlias . "_entry_id"]]['creator_email'] = $masterQueryArray['main_email'];
                        } else {
                            $masterResults[$masterIndexer][getFormTitle($curFormId)][$masterQueryArray[$curFormAlias . "_entry_id"]]['creator_email'] = "";
                        }
                    }
                    // for backwards compatibility, replicate the old metadata fields
                    switch ($elementHandle) {
                        case "creation_uid":
                            $old_meta = "uid";
                            break;
                        case "mod_uid":
                            $old_meta = "proxyid";
                            break;
                        case "creation_datetime":
                            $old_meta = "creation_date";
                            break;
                        case "mod_datetime":
                            $old_meta = "mod_date";
                            break;
                    }
                    $masterResults[$masterIndexer][getFormTitle($curFormId)][$masterQueryArray[$curFormAlias . "_entry_id"]][$old_meta] = $valueArray;
                }
            }
            // end of foreach field loop within a record
        }
        // end of main while loop for all records
        if (count($derivedFieldMetadata) > 0 and $masterIndexer > -1) {
            // if there is derived value info for this data set and we have started to create values...need to do this one more time for the last value that we would have gathered data for...
            //print "fid: $fid<br>";
            //print "frid: $frid<br>";
            $masterResults[$masterIndexer] = formulize_calcDerivedColumns($masterResults[$masterIndexer], $derivedFieldMetadata, $frid, $fid);
        }
    }
    // end if if there's more the 1 linked fid
    return $masterResults;
}
예제 #2
0
function curl_cache_exec($curl_opt, $proxy, $usecache = true)
{
    if (!empty($curl_opt[CURLOPT_URL])) {
        $curl_opt = $curl_opt + buildCurlOptions($proxy);
        d('Curl', "GET " . $curl_opt[CURLOPT_URL] . " via " . proxyToString($proxy) . " (mem: " . debug_memory() . ")");
        $cacheFile = CACHE_DIR . sha1($curl_opt[CURLOPT_URL]);
        if ($usecache && file_exists($cacheFile)) {
            //$cache_hto =  intval(isset($options['general']['cache_lifetime']) ? $options['general']['cache_lifetime'] : 4);
            $cache_hto = CACHE_LIFETIME;
            //            echo "DEBUG CACHE  Now : ".time()." cacheFile: ". filemtime($cacheFile)." hto : ".($cache_hto*3600)."\n";
            // HIT
            if (time() - filemtime($cacheFile) < $cache_hto * 3600) {
                $cacheData = @file_get_contents($cacheFile);
                if ($cacheData !== FALSE) {
                    // use the cache
                    $response['cache'] = true;
                    $response['data'] = $cacheData;
                    $response['status'] = 200;
                    // we only cache 200, no problem
                    $response['cache_age'] = time() - filemtime($cacheFile);
                    $response['redir'] = null;
                    $response['error'] = null;
                }
                d('Curl', "GOT status=200 cache=HIT age=" . $response['cache_age'] . " (mem: " . debug_memory() . ")");
                return $response;
            }
        }
        $ch = curl_init();
        curl_setopt_array($ch, $curl_opt);
        $data = curl_exec($ch);
        $response = array();
        $response['cache'] = false;
        $response['data'] = $data;
        $response['status'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $response['cache_age'] = 0;
        $response['redir'] = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
        $response['error'] = curl_error($ch);
        curl_close($ch);
        // only cache 200 OK
        if ($usecache && $response['status'] == 200 && strlen($data) > 0) {
            // try to cache the stuff
            if (!file_exists(CACHE_DIR)) {
                @mkdir(CACHE_DIR);
            }
            @file_put_contents($cacheFile, $data);
        }
        d('Curl', "GOT status=" . $response['status'] . " cache=MISS age=0 (mem: " . debug_memory() . ")");
        return $response;
    }
    return null;
}