function getDetailSectionForSmarty($parentName, $dtKey, $dtValue, $ind)
{
    global $dtStructs, $rtStructs, $first_record, $recursion_depth, $mode;
    $dtNames = $dtStructs['names'];
    $rtNames = $rtStructs['names'];
    $dty_fi = $dtStructs['typedefs']['fieldNamesToIndex'];
    $rst_fi = $rtStructs['typedefs']['dtFieldNamesToIndex'];
    $ind++;
    if ($dtNames[$dtKey]) {
        $dtname_wo_parent = getVariableNameForSmarty($dtNames[$dtKey]);
        $dtname = $parentName . '.' . $dtname_wo_parent;
        //name of variable
        $dtDef = $dtStructs['typedefs'][$dtKey]['commonFields'];
        if ($dtDef) {
            $tree = array();
            $tree_children = array();
            $vars = array();
            $detailtypes = array();
            $arr_text = array();
            $detailType = $dtDef[$dty_fi['dty_Type']];
            //HARDCODED!!!!
            $dt_label = $dtValue[$rst_fi['rst_DisplayName']];
            $dt_tooltip = $dtValue[$rst_fi['rst_DisplayHelpText']];
            //help text
            switch ($detailType) {
                /* @TODO
                			case 'file':
                			break;
                			case 'geo':
                			break;
                			case 'calculated':
                			break;
                			case 'fieldsetmarker':
                			break;
                			case 'relationtype':
                			*/
                case 'separator':
                    return null;
                case 'enum':
                    //@todo!!!!
                    /*
                    				$tree_children = array_merge($tree_children, array($dtname_wo_parent=>$dtname));
                    				$vars = array_merge($vars, array($dtname=>$dt_label));
                    				$detailtypes = array_merge($detailtypes, array($dtname=>$detailType));
                    				array_push($arr_text, '{$'.$dtname.'}'); //lbl="'.$dt_label.'"
                    */
                    //
                    $enumtree = array("id" => $dtname . ".id", "code" => $dtname . ".code", "label" => $dtname . ".label", "conceptid" => $dtname . ".conceptid");
                    $tree_children = array_merge($tree_children, array($dtname => $enumtree));
                    $vars = array_merge($vars, array($dtname . ".id" => "id"));
                    $vars = array_merge($vars, array($dtname . ".code" => "code"));
                    $vars = array_merge($vars, array($dtname . ".label" => "label"));
                    $vars = array_merge($vars, array($dtname . ".conceptid" => "conceptid"));
                    array_push($arr_text, '{$' . $dtname . '.id}', '{$' . $dtname . '.code}', '{$' . $dtname . '.label}', '{$' . $dtname . '.conceptid}');
                    $detailtypes = array_merge($detailtypes, array($dtname => $detailType));
                    break;
                case 'resource':
                    // link to another record type
                // link to another record type
                case 'relmarker':
                    $pointerRecTypeId = $dtValue[$rst_fi['rst_PtrFilteredIDs']];
                    //@TODO!!!!! it may be comma separated string
                    //load this record
                    if ($recursion_depth < 2) {
                        // && ($mode=='varsonly' || array_key_exists($pointerRecTypeId, $rtNames))) {
                        $recursion_depth++;
                        /*if($mode=='varsonly' || !array_key_exists($pointerRecTypeId, $rtNames)){
                        			$recordTypeName = $dt_label;
                        		}else{
                        			$recordTypeName = $rtNames[$pointerRecTypeId];
                        		}*/
                        $recordTypeName = $dt_label;
                        $recordTypeName = getVariableNameForSmarty($recordTypeName, false);
                        if ($pointerRecTypeId == "") {
                            //unconstrainded
                            $dt_label = $dt_label . " ptr";
                            $vars = array_merge($vars, array($recordTypeName => $dt_label));
                        }
                        $_fe = array($dt_label, '{foreach $' . $parentName . '.' . $recordTypeName . 's as $' . $recordTypeName . '}');
                        array_push($arr_text, _maketext($_fe, $ind));
                        //creates headers
                        $res2 = getRecordHeaderSectionForSmarty($first_record, $recordTypeName, $ind, false);
                        //was 15-Aug-2012 ($detailType=='relmarker')); //from
                        array_push($arr_text, $res2['text']);
                        $vars = array_merge($vars, $res2['vars']);
                        $tree = $res2['tree'];
                        if ($recursion_depth > 1) {
                            //$tree_children = array_merge($tree_children, $res2['tree']);
                        } else {
                        }
                        //add specific for this record type details
                        if (array_key_exists($pointerRecTypeId, $rtNames)) {
                            //$tree[$recordTypeName] = array_merge($tree[$recordTypeName], $res2['tree']);
                            //get the list of details from this record
                            $details = $rtStructs['typedefs'][$pointerRecTypeId]['dtFields'];
                            // TODO: delete this? TEMP - to avoid self recursion
                            // no need $text  = $text.'\n<tr><td colspan="13">';
                            foreach ($details as $dtKey2 => $dtValue2) {
                                $dt = getDetailSectionForSmarty($recordTypeName, $dtKey2, $dtValue2, $ind);
                                if ($dt) {
                                    array_push($arr_text, $dt['text']);
                                    $vars = array_merge($vars, $dt['vars']);
                                    $detailtypes = array_merge($detailtypes, $dt['detailtypes']);
                                    if (false && $recursion_depth > 1) {
                                        //$res2[$recordTypeName] = array_merge($res2[$recordTypeName], $dt['tree_children']);
                                        //$res2 = array_merge($res2, $dt['tree']);
                                    } else {
                                        $tree[$recordTypeName] = array_merge($tree[$recordTypeName], $dt['tree_children']);
                                        //detail fields
                                        if ($recursion_depth > 1) {
                                            $tree[$recordTypeName] = array_merge($tree[$recordTypeName], $dt['tree']);
                                        } else {
                                            $tree = array_merge($tree, $dt['tree']);
                                            //assign first level records to root
                                        }
                                    }
                                }
                            }
                            //for
                            ///no need $text = $text.'</td></tr>\n';
                            /*
                            					if($recursion_depth>1){
                            /*****DEBUG****/
                            //error_log(">>>>            ");
                            /*****DEBUG****/
                            //error_log(">>>>            ".print_r($res2['tree'], true));
                            //						$tree = array_merge($tree, $res2['tree']);
                            //					}*/
                        }
                        //array_key_exists($pointerRecTypeId, $rtNames))
                        array_push($arr_text, '{/foreach}');
                        array_push($arr_text, '');
                    }
                    /*
                    				//@todo - parsing will depend on depth level
                    				// if there are not mentions about this record type in template (based on obtained array of variables)
                    				// we will create href link to this record
                    				// otherwise - we have to obtain this record (by ID) and add subarray
                    
                    				$res = array();
                    				$rectypeID = null;
                    
                    				foreach ($dtValue as $key => $value){
                    /*****DEBUG****/
                    //error_log(">>>>>>>>>>".array_key_exists('id',$value));
                    /*					if (array_key_exists('id',$value))
                    					{
                    						//this is record ID
                    						$recordID = $value['id'];
                    						//get full record info
                    						$record = loadRecord($recordID); //from search/getSearchResults.php
                    						$res0 = getRecordForSmarty($record); //@todo - need to
                    
                    						if($res0){
                    							array_push($res, $res0);
                    							if($rectypeID==null){
                    								$rectypeID = $res0['recRecTypeID'];
                    							}
                    						}
                    					}
                    				}//for each
                    
                    				if( count($res)>0 && array_key_exists($rectypeID, $rtNames))
                    				{
                    					$recordTypeName = $rtNames[$rectypeID];
                    					$recordTypeName = getVariableNameForSmarty($recordTypeName, false);
                    /*****DEBUG****/
                    //error_log(">>>>>>>>>>".$rectypeID."=".$rtNames[$rectypeID]."=".$recordTypeName);
                    /*					$res = array( $recordTypeName."s" =>$res, $recordTypeName =>$res[0] );
                    				}else{
                    					$res = null;
                    				}
                    */
                    break;
                default:
                    //:&nbsp;
                    //array_push($vars, $dtname);
                    $tree_children = array_merge($tree_children, array($dtname_wo_parent => $dtname));
                    $vars = array_merge($vars, array($dtname => $dt_label));
                    $detailtypes = array_merge($detailtypes, array($dtname => $detailType));
                    array_push($arr_text, '{$' . $dtname . '}');
                    //lbl="'.$dt_label.'"
            }
            //end switch
            $text = _maketext($arr_text, $ind);
            return array("text" => $text, "vars" => $vars, 'detailtypes' => $detailtypes, 'tree_children' => $tree_children, 'tree' => $tree);
        } else {
            return null;
            //detail type not found in detailTypes.typedefs
        }
    } else {
        //name is not defined
        return null;
    }
}
Beispiel #2
0
function getDetailForSmarty($dtKey, $dtValue, $recursion_depth, $recTypeID, $recID)
{
    global $dtStructs, $dtTerms, $rtStructs;
    $dtNames = $dtStructs['names'];
    $rtNames = $rtStructs['names'];
    $dty_fi = $dtStructs['typedefs']['fieldNamesToIndex'];
    /*****DEBUG****/
    //	if($dtKey==9){
    /*****DEBUG****/
    //error_log("KEY=".$dtKey."   NAME=".$dtNames[$dtKey]);
    /*****DEBUG****/
    //error_log("dtValue=".print_r($dtValue, true));
    /*****DEBUG****/
    //	}
    if ($dtKey < 1 || $dtNames[$dtKey]) {
        if ($dtKey < 1) {
            $dt_label = "Relationship";
            /*****DEBUG****/
            //error_log("111>>>>>".print_r($dtValue, true));
            /*****DEBUG****/
            //error_log("222>>>>>".$recTypeID);
        } else {
            $rt_structure = $rtStructs['typedefs'][$recTypeID]['dtFields'];
            $dtlabel_index = $rtStructs['typedefs']['dtFieldNamesToIndex']['rst_DisplayName'];
            if (array_key_exists($dtKey, $rt_structure)) {
                $dt_label = $rt_structure[$dtKey][$dtlabel_index];
                //$dtname = getVariableNameForSmarty($dt_label);
            }
            $dtname = getVariableNameForSmarty($dtNames[$dtKey]);
        }
        if (is_array($dtValue)) {
            //complex type - need more analize
            $res = null;
            $dtDef = $dtKey < 1 ? "dummy" : $dtStructs['typedefs'][$dtKey]['commonFields'];
            if ($dtDef) {
                $detailType = $dtKey < 1 ? "relmarker" : $dtDef[$dty_fi['dty_Type']];
                switch ($detailType) {
                    case 'enum':
                        $fi = $dtTerms['fieldNamesToIndex'];
                        $res_id = "";
                        $res_cid = "";
                        $res_code = "";
                        $res_label = "";
                        foreach ($dtValue as $key => $value) {
                            if (array_key_exists($value, $dtTerms['termsByDomainLookup']['enum'])) {
                                $term = $dtTerms['termsByDomainLookup']['enum'][$value];
                                $res_id = _add_term_val($res_id, $value);
                                $res_cid = _add_term_val($res_cid, $term[$fi['trm_ConceptID']]);
                                $res_code = _add_term_val($res_code, $term[$fi['trm_Code']]);
                                $res_label = _add_term_val($res_label, $term[$fi['trm_Label']]);
                            }
                        }
                        $res = array("id" => $res_id, "code" => $res_code, "label" => $res_label, "conceptid" => $res_cid);
                        $res = array($dtname => $res);
                        /*if(strlen($res)==0){ //no valid terms
                        			$res = null;
                        		}else{
                        			$res = array( $dtname=>$res );
                        		}*/
                        break;
                    case 'date':
                        $res = "";
                        $origvalues = array();
                        foreach ($dtValue as $key => $value) {
                            if (strlen($res) > 0) {
                                $res = $res . ", ";
                            }
                            $res = $res . temporalToHumanReadableString($value);
                            array_push($origvalues, temporalToHumanReadableString($value));
                        }
                        if (strlen($res) == 0) {
                            //no valid terms
                            $res = null;
                        } else {
                            $res = array($dtname => $res, $dtname . "_originalvalue" => $origvalues);
                        }
                        break;
                    case 'file':
                        //get url for file download
                        //if image - special case
                        $res = "";
                        $origvalues = array();
                        foreach ($dtValue as $key => $value) {
                            if (strlen($res) > 0) {
                                $res = $res . ", ";
                            }
                            $res = $res . $value['file']['URL'];
                            //original value keeps the whole 'file' array
                            array_push($origvalues, $value['file']);
                            //$dtname2 = $dtname."_originalvalue";
                            //$arres = array_merge($arres, array($dtname2=>$value['file']));
                            /*****DEBUG****/
                            //error_log(">>>>>".$dtname2."= ".print_r($value['file'],true));
                        }
                        if (strlen($res) == 0) {
                            $res = null;
                        } else {
                            $res = array($dtname => $res, $dtname . "_originalvalue" => $origvalues);
                            //array_merge($arres, array($dtname=>$res));
                        }
                        break;
                        /* NOT USED ANYMORE
                        			case 'urlinclude':
                        				//
                        				$res = "";
                        				$arres = array();
                        
                        				foreach ($dtValue as $key => $value){
                        					if(strlen($res)>0) $res = $res.", ";
                        
                        					$arr = explode('|', $value);
                        					$res = $res.$arr[0];
                        
                        					//original value keeps source and type
                        					$dtname2 = $dtname."_originalvalue";
                        					$arres = array_merge($arres, array($dtname2=>$value));
                        				}
                        				if(strlen($res)==0){
                        					$res = null;
                        				}else{
                        					$res = array_merge($arres, array($dtname=>$res));
                        				}
                        
                        			break;
                        */
                    /* NOT USED ANYMORE
                    			case 'urlinclude':
                    				//
                    				$res = "";
                    				$arres = array();
                    
                    				foreach ($dtValue as $key => $value){
                    					if(strlen($res)>0) $res = $res.", ";
                    
                    					$arr = explode('|', $value);
                    					$res = $res.$arr[0];
                    
                    					//original value keeps source and type
                    					$dtname2 = $dtname."_originalvalue";
                    					$arres = array_merge($arres, array($dtname2=>$value));
                    				}
                    				if(strlen($res)==0){
                    					$res = null;
                    				}else{
                    					$res = array_merge($arres, array($dtname=>$res));
                    				}
                    
                    			break;
                    */
                    case 'geo':
                        $res = "";
                        $arres = array();
                        foreach ($dtValue as $key => $value) {
                            /*****DEBUG****/
                            //error_log("GEO=>>>>".print_r($value, true));
                            //original value keeps whole geo array
                            $dtname2 = $dtname . "_originalvalue";
                            $value['geo']['recid'] = $recID;
                            $arres = array_merge($arres, array($dtname2 => $value['geo']));
                            $res = $value['geo']['wkt'];
                            break;
                            //only one geo location at the moment
                            /*
                            						$geom = geoPHP::load($value['geo']['wkt'],'wkt');
                            						if(!$geom->isEmpty()){
                            							$point = $geom->centroid();
                            							$res = "http://maps.google.com/maps?z=18&q=".$point->y().",".$point->x();
                            							break;
                            						}*/
                        }
                        if (strlen($res) == 0) {
                            $res = null;
                        } else {
                            $res = array_merge($arres, array($dtname => $res));
                            //$res = array( $dtname=>$res );
                        }
                        break;
                    case 'separator':
                        break;
                    case 'calculated':
                        break;
                    case 'fieldsetmarker':
                        break;
                    case 'relmarker':
                    case 'resource':
                        // link to another record type
                        //@todo - parsing will depend on depth level
                        // if there are not mentions about this record type in template (based on obtained array of variables)
                        // we will create href link to this record
                        // otherwise - we have to obtain this record (by ID) and add subarray
                        $res = array();
                        $rectypeID = null;
                        $prevID = null;
                        /*****DEBUG****/
                        //error_log("dtValue>>>>>".print_r($dtValue,true));
                        $order = 0;
                        foreach ($dtValue as $key => $value) {
                            if ($recursion_depth < 2 && (array_key_exists('id', $value) || array_key_exists('RelatedRecID', $value))) {
                                //this is record ID
                                if (array_key_exists('RelatedRecID', $value)) {
                                    $recordID = $value['RelatedRecID']['rec_ID'];
                                    /*****DEBUG****/
                                    //error_log(">>>>value=".print_r($value, true));
                                } else {
                                    $recordID = $value['id'];
                                }
                                //get full record info
                                $record = loadRecord($recordID);
                                //from search/getSearchResults.php
                                $res0 = null;
                                if (true) {
                                    //64719  45171   48855    57247
                                    $res0 = getRecordForSmarty($record, $recursion_depth + 2, $order);
                                    //@todo - need to
                                    $order++;
                                }
                                if ($res0) {
                                    if ($rectypeID == null && @$res0['recRecTypeID']) {
                                        $rectypeID = $res0['recRecTypeID'];
                                        /* TEMP DEBUG
                                        								if(array_key_exists($rectypeID, $rtNames))
                                        								{
                                        									$pointerIDs = ($dtKey<1) ?"" :$dtDef[ $dty_fi['dty_PtrTargetRectypeIDs'] ];
                                        									$isunconstrained = ($pointerIDs=="");
                                        if($isunconstrained){
                                        /*****DEBUG****/
                                        //error_log($dt_label.">>>>>>>");
                                        /*}
                                        		}            */
                                    }
                                    //add relationship specific variables
                                    if (array_key_exists('RelatedRecID', $value) && array_key_exists('RelTerm', $value)) {
                                        $res0["recRelationType"] = $value['RelTerm'];
                                        /*if(array_key_exists('interpRecID', $value)){
                                        			$record = loadRecord($value['interpRecID']);
                                        			$res0["recRelationInterpretation"] = getRecordForSmarty($record, $recursion_depth+2, $order);
                                        		}*/
                                        if (array_key_exists('Notes', $value)) {
                                            $res0["recRelationNotes"] = $value['Notes'];
                                        }
                                        if (array_key_exists('StartDate', $value)) {
                                            $res0["recRelationStartDate"] = temporalToHumanReadableString($value['StartDate']);
                                        }
                                        if (array_key_exists('EndDate', $value)) {
                                            $res0["recRelationEndDate"] = temporalToHumanReadableString($value['EndDate']);
                                        }
                                        /*****DEBUG****/
                                        //error_log(">>>>RECID=".$recordID." >>>>>".print_r($res0, true));
                                    }
                                    array_push($res, $res0);
                                }
                            }
                        }
                        //for each repeated value
                        if (count($res) > 0 && array_key_exists($rectypeID, $rtNames)) {
                            $pointerIDs = $dtKey < 1 ? "" : $dtDef[$dty_fi['dty_PtrTargetRectypeIDs']];
                            /*if($pointerIDs==""){ //unconstrainted pointer - we will use as name of variable display name for current record type
                            			$recordTypeName = $dt_label;
                            		}else{
                            			$recordTypeName = $rtNames[$rectypeID];
                            		}*/
                            if (@$dt_label) {
                                $recordTypeName = $dt_label;
                                $recordTypeName = getVariableNameForSmarty($recordTypeName, false);
                                $res = array($recordTypeName . "s" => $res, $recordTypeName => $res[0]);
                            }
                        } else {
                            $res = null;
                        }
                        break;
                    default:
                        // repeated basic detail types
                        $res = "";
                        $origvalues = array();
                        foreach ($dtValue as $key => $value) {
                            if (strlen($res) > 0) {
                                $res = $res . ", ";
                            }
                            $res = $res . $value;
                            array_push($origvalues, $value);
                        }
                        if (strlen($res) == 0) {
                            //no valid terms
                            $res = null;
                        } else {
                            $res = array($dtname => $res, $dtname . "_originalvalue" => $origvalues);
                        }
                }
                //it depends on detail type - need specific behaviour for each type
                //foreach ($value as $dtKey => $dtValue){}
            }
            //end switch
            return $res;
        } else {
            return array($dtname => $dtValue);
        }
    } else {
        //name is not defined
        return null;
    }
}