Esempio n. 1
0
 public static function setMTTermsforEntity($fieldName, $entityName, $entityID, $multiValueTermArray)
 {
     MtFieldWrapper::getObject($entityName, $fieldName)->DeleteCodesForEntity($entityID);
     foreach ($multiValueTermArray as $term) {
         $term->Save();
     }
 }
Esempio n. 2
0
 public function getMtObject($field, $vocab_number)
 {
     $mtTerm = MtFieldWrapper::getMTObject($field, $this->_table);
     $mtTerm->vocab_number = $vocab_number;
     $mtTerm->record_number = $this->{$this->keyName};
     return $mtTerm;
 }
Esempio n. 3
0
include_once APPROOT . 'inc/lib_files.inc';
include_once APPROOT . 'inc/security/lib_acl.inc';
include_once APPROOT . 'inc/security/handler_acl.inc';
include_once APPROOT . 'inc/lib_form.inc';
include_once APPROOT . 'inc/lib_form_util.inc';
require_once APPROOT . 'data/Browse.php';
require_once APPROOT . 'data/MtField.php';
require_once APPROOT . 'data/MtFieldWrapper.php';
require_once APPROOT . 'data/MtIndex.php';
require_once APPROOT . 'data/MtTerms.php';
$mtIndex = new MtIndex();
$index_terms = $mtIndex->Find('');
$options = array();
$options[''] = '';
foreach ($index_terms as $index_term) {
    $data_array = MtFieldWrapper::getMTList($index_term->no);
    $count = count($data_array);
    $level = 0;
    $levelsarray = array();
    $term_order = 1;
    for ($i = 0; $i < $count;) {
        $element1 = $data_array[$i];
        $element2 = $data_array[++$i];
        $h1 = strlen(rtrim($element1['huri_code'], '0'));
        $h2 = strlen(rtrim($element2['huri_code'], '0'));
        if ($h1 % 2 == 1) {
            $h1++;
        }
        if ($h2 % 2 == 1) {
            $h2++;
        }
Esempio n. 4
0
function get_data_array()
{
    global $dataFile, $dataCols, $fields, $lists, $docsCount, $biosCount;
    if (($handle = fopen($dataFile, "r")) === FALSE) {
        return array();
    }
    $results = array();
    $j = 0;
    while (($cols = fgetcsv($handle, 0, "\t")) !== FALSE) {
        $glData = array();
        $row = array();
        if ($cols && $cols[1]) {
            //var_dump($cols);exit;
            foreach ($cols as $key => $val) {
                $val = trim($val, '"');
                $val = trim($val);
                $row[$key] = $val;
                $entityField = $dataCols[$key];
                if ($entityField) {
                    $ent = $entityField["ent"];
                    $list_code = $entityField['list_code'];
                    $type = $entityField['field_type'];
                    $mlt = trim($entityField['is_repeat']) == 'Y' || trim($entityField['is_repeat']) == 'y' ? true : false;
                    if ($list_code == 39) {
                        if ($val == "M") {
                            $val = "Hombre";
                        } elseif ($val == "F") {
                            $val = "Mujer";
                        }
                    }
                    if ($list_code && !$lists[$list_code]) {
                        $options = array();
                        $data_array = MtFieldWrapper::getMTList($list_code);
                        $size = count($data_array);
                        for ($i = 0; $i < $size; $i++) {
                            $options[$data_array[$i]['vocab_number']] = strtolower($data_array[$i]['label']);
                        }
                        $lists[$list_code] = $options;
                    }
                    if ($list_code) {
                        if ($mlt) {
                            $val2 = explode(";", $val);
                            $val = array();
                            foreach ($val2 as $v) {
                                $v = trim($v);
                                $vocab_number = array_search(strtolower($v), $lists[$list_code]);
                                if ($vocab_number) {
                                    $val[] = $vocab_number;
                                }
                            }
                        } else {
                            $vocab_number = array_search(strtolower($val), $lists[$list_code]);
                            if ($vocab_number) {
                                $val = $vocab_number;
                            } else {
                                $val = null;
                            }
                        }
                    }
                    if ($type == "date") {
                        if ($val) {
                            $d = date_create_from_format("m/d/Y", $val);
                            if ($d) {
                                $val = date_format($d, 'Y-m-d');
                            } else {
                                $val = null;
                            }
                        } else {
                            $val = null;
                        }
                    } elseif ($type == "location") {
                        $val2 = explode(",", $val);
                        //$row[$entityField['field_name'] . "_latitude"] = -floatval($val2[0]);
                        //$row[$entityField['field_name'] . "_longitude"] = -floatval($val2[1]);
                        $glData[$ent][$entityField['field_name'] . "_latitude"] = -floatval($val2[0]);
                        $glData[$ent][$entityField['field_name'] . "_longitude"] = -floatval($val2[1]);
                        $val = null;
                    } elseif ($type == "radio") {
                        if ($val == "NO" || $val == "no") {
                            $val = "n";
                        } elseif ($val == "SÍ" || $val == "sí") {
                            $val = "y";
                        } else {
                            $val = null;
                        }
                    }
                    if (!is_null($val)) {
                        //$row[$entityField['field_name']] = $val;
                        $glData[$ent][$entityField['field_name']] = $val;
                    }
                }
            }
            if (!$glData['act']['ubicacto']) {
                $v = $row[25];
                $vocab_number = array_search(strtolower($v), $lists[71]);
                if ($vocab_number) {
                    $glData['act']['ubicacto'] = $vocab_number;
                }
            }
            if (!$glData['act']['ubicacto']) {
                $v = $row[24];
                $vocab_number = array_search(strtolower($v), $lists[71]);
                if ($vocab_number) {
                    $glData['act']['ubicacto'] = $vocab_number;
                }
            }
            $browse = new Browse();
            $rows = $browse->ExecuteQuery("select event_record_number as id from event where event_title='" . $glData['event']['event_title'] . "'");
            if ($rows && $rows[0]['id']) {
                $event = new Event();
                $event->LoadFromRecordNumber($rows[0]['id']);
            } else {
                $form = event_form('new');
                $event = new Event();
                $event->event_record_number = shn_create_uuid('event');
                form_objects($form, $event, $glData['event']);
                $event->SaveAll();
            }
            $browse = new Browse();
            $person = null;
            if ($glData['person']['person_name'] != "NN") {
                $rows = $browse->ExecuteQuery("select person_record_number as id from person where person_name='" . $glData['person']['person_name'] . "' \n\t\t\t\tand other_names='" . $glData['person']['other_names'] . "' ");
                if ($rows && $rows[0]['id']) {
                    $person = new Person();
                    $person->LoadFromRecordNumber($rows[0]['id']);
                }
            }
            if (!$person) {
                $person_form = person_form('new');
                $person = new Person();
                form_objects($person_form, $person, $glData['person']);
                $person->deceased = $person->deceased == 'on' ? 'y' : 'n';
                if (isset($person->number_of_persons_in_group) && !$person->number_of_persons_in_group) {
                    $person->number_of_persons_in_group = Null;
                }
                if (isset($person->dependants) && !$person->dependants) {
                    $person->dependants = Null;
                }
                $person->SaveAll();
            }
            $victim = $person;
            $act_form = act_form('new');
            $act = new Act();
            $act->act_record_number = shn_create_uuid('act');
            $glData['act']['victim'] = $victim->person_record_number;
            form_objects($act_form, $act, $glData['act']);
            $act->event = $event->event_record_number;
            $act->SaveAll();
            if ($glData['perpetrator']['person_name']) {
                $browse = new Browse();
                $rows = $browse->ExecuteQuery("select person_record_number as id from person where person_name='" . $glData['perpetrator']['person_name'] . "' ");
                if ($rows && $rows[0]['id']) {
                    $person = new Person();
                    $person->LoadFromRecordNumber($rows[0]['id']);
                } else {
                    $person_form = person_form('new');
                    $person = new Person();
                    form_objects($person_form, $person, $glData['perpetrator']);
                    $person->deceased = $person->deceased == 'on' ? 'y' : 'n';
                    if (isset($person->number_of_persons_in_group) && !$person->number_of_persons_in_group) {
                        $person->number_of_persons_in_group = Null;
                    }
                    if (isset($person->dependants) && !$person->dependants) {
                        $person->dependants = Null;
                    }
                    $person->SaveAll();
                }
                $perpetrator = $person;
                $inv = new Involvement();
                $inv->involvement_record_number = shn_create_uuid('inv');
                $inv->degree_of_involvement = "54010101001921";
                //placeholder
                $inv->event = $event->event_record_number;
                $inv->act = $act->act_record_number;
                $inv->perpetrator = $perpetrator->person_record_number;
                $inv->SaveAll();
            }
            $supporting_documents = array();
            if (trim($row[9])) {
                $document_form = document_form('new');
                unset($document_form['doc_id']);
                $supporting_docs = new SupportingDocs();
                $supporting_docs_meta = new SupportingDocsMeta();
                $type = null;
                $doc_uuid = shn_create_uuid('doc');
                $supporting_docs->doc_id = $doc_uuid;
                $supporting_docs_meta->doc_id = $doc_uuid;
                $supporting_docs->uri = '';
                form_objects($document_form, $supporting_docs, array('title' => $row[9]));
                form_objects($document_form, $supporting_docs_meta, array('title' => $row[9]));
                $supporting_docs_meta->format = $type;
                $supporting_docs->Save();
                $supporting_docs_meta->Save();
                $supporting_documents[] = $doc_uuid;
            }
            for ($j = 1; $j <= $docsCount; $j++) {
                if ($glData["supporting_docs_meta" . $j]) {
                    if (!$glData["supporting_docs_meta" . $j]['title']) {
                        continue;
                    }
                    $document_form = document_form('new');
                    unset($document_form['doc_id']);
                    $supporting_docs = new SupportingDocs();
                    $supporting_docs_meta = new SupportingDocsMeta();
                    $type = null;
                    $doc_uuid = shn_create_uuid('doc');
                    $supporting_docs->doc_id = $doc_uuid;
                    $supporting_docs_meta->doc_id = $doc_uuid;
                    $supporting_docs->uri = '';
                    form_objects($document_form, $supporting_docs, $glData["supporting_docs_meta" . $j]);
                    form_objects($document_form, $supporting_docs_meta, $glData["supporting_docs_meta" . $j]);
                    $supporting_docs_meta->format = $type;
                    $supporting_docs->Save();
                    $supporting_docs_meta->SaveAll();
                    $supporting_documents[] = $doc_uuid;
                }
            }
            if ($supporting_documents) {
                $act->supporting_documents = $supporting_documents;
                $act->SaveDocs();
            }
            $bio_details = array();
            for ($j = 1; $j <= $biosCount; $j++) {
                if ($glData["bio_details" . $j] && $glData["bio_details" . $j]["type_of_relationship"]) {
                    $browse = new Browse();
                    $rows = $browse->ExecuteQuery("select person_record_number as id from person where person_name='" . $glData["bio_details" . $j]['person_name'] . "' \n\t\t\t\t\tand other_names='" . $glData['bio_details']['other_names'] . "' ");
                    if ($rows && $rows[0]['id']) {
                        $person = new Person();
                        $person->LoadFromRecordNumber($rows[0]['id']);
                    } else {
                        $person_form = person_form('new');
                        $person = new Person();
                        form_objects($person_form, $person, $glData["bio_details" . $j]);
                        $person->deceased = $person->deceased == 'on' ? 'y' : 'n';
                        if (isset($person->number_of_persons_in_group) && !$person->number_of_persons_in_group) {
                            $person->number_of_persons_in_group = Null;
                        }
                        if (isset($person->dependants) && !$person->dependants) {
                            $person->dependants = Null;
                        }
                        $person->SaveAll();
                    }
                    if ($glData["bio_details" . $j]["phone"] || $glData["bio_details" . $j]["email"]) {
                        $address = new Address();
                        $address_form = address_form('new');
                        form_objects($address_form, $address, $glData["bio_details" . $j]);
                        $address->person = $person->person_record_number;
                        $address->Save();
                    }
                    $biography_form = biographic_form('new');
                    $biography = new BiographicDetail();
                    //$biography->LoadfromRecordNumber();
                    $biography->biographic_details_record_number = shn_create_uuid('biography');
                    $glData["bio_details" . $j]['person_id'] = $person->person_record_number;
                    form_objects($biography_form, $biography, $glData["bio_details" . $j]);
                    $biography->person = $victim->person_record_number;
                    $biography->related_person = $person->person_record_number;
                    if ($biography->related_person == '') {
                        $biography->related_person = null;
                    }
                    $biography->SaveAll();
                }
            }
        }
        $results[] = $row;
        $j++;
    }
    return $results;
}
Esempio n. 5
0
    function act_mt_tree()
    {
        $data_array = MtFieldWrapper::getMTList($_GET['list_code']);
        $count = count($data_array);
        for ($i = 0; $i < $count;) {
            $element1 = $data_array[$i];
            if (is_array($value)) {
                $sel = in_array($element1['vocab_number'], $value) ? 'selected="selected"' : null;
            } else {
                $sel = $element1['vocab_number'] == $value ? 'selected="selected"' : null;
            }
            ?>
            <option value="<?php 
            echo $element1['vocab_number'];
            ?>
" <?php 
            echo $sel;
            ?>
 data-level="<?php 
            echo $element1["term_level"];
            ?>
"><?php 
            echo $element1['label'];
            ?>
</option>

            <?php 
            $i++;
        }
    }
<?php

global $conf;
?>
<div id="browse">
    <div style="display:none">
        <?php 
$fields = shn_form_get_html_fields($fields_form);
foreach ($fields_form as $fieldName => $field) {
    if ($field["type"] == "mt_select" || $field["type"] == "mt_tree") {
        $data_array = MtFieldWrapper::getMTList($field['map']['mt']);
        $size = count($data_array);
        $options = array();
        for ($i = 0; $i < $size; $i++) {
            $options[$data_array[$i]['vocab_number']] = $data_array[$i]['label'];
        }
        ?>
                <select class="mt_select input-large" name=""
                        id="fieldOptionsTemplate_<?php 
        echo $field['field_number'];
        ?>
"
                        multiple="multiple">
                    <?php 
        foreach ($options as $opt_value => $desc) {
            ?>
                        <option value="<?php 
            echo $opt_value;
            ?>
"><?php 
            echo $desc;
Esempio n. 7
0
function get_data_array()
{
    global $dataFile, $dataCols, $fields, $lists, $docsCount, $biosCount;
    if (($handle = fopen($dataFile, "r")) === FALSE) {
        return array();
    }
    $results = array();
    $i = 0;
    while (($cols = fgetcsv($handle, 0, "\t")) !== FALSE) {
        $glData = array();
        $row = array();
        if ($cols && $cols[1]) {
            foreach ($cols as $key => $val) {
                $val = trim($val, '"');
                $val = trim($val);
                $row[$key] = $val;
                $entityField = $dataCols[$key];
                if ($entityField) {
                    $ent = $entityField["ent"];
                    $list_code = $entityField['list_code'];
                    $type = $entityField['field_type'];
                    $mlt = trim($entityField['is_repeat']) == 'Y' || trim($entityField['is_repeat']) == 'y' ? true : false;
                    if ($list_code == 39) {
                        if ($val == "M") {
                            $val == "Hombre";
                        } elseif ($val == "F") {
                            $val == "Mujer";
                        }
                    }
                    if ($list_code && !$lists[$list_code]) {
                        $options = array();
                        $data_array = MtFieldWrapper::getMTList($list_code);
                        $size = count($data_array);
                        for ($i = 0; $i < $size; $i++) {
                            $options[$data_array[$i]['vocab_number']] = strtolower($data_array[$i]['label']);
                        }
                        $lists[$list_code] = $options;
                    }
                    if ($list_code) {
                        if ($mlt) {
                            $val2 = explode(";", $val);
                            $val = array();
                            foreach ($val2 as $v) {
                                $vocab_number = array_search(strtolower($v), $lists[$list_code]);
                                if ($vocab_number) {
                                    $val[] = $vocab_number;
                                }
                            }
                        } else {
                            $vocab_number = array_search(strtolower($val), $lists[$list_code]);
                            if ($vocab_number) {
                                $val = $vocab_number;
                            } else {
                                $val = null;
                            }
                        }
                    }
                    if ($type == "date") {
                        if ($val) {
                            $d = date_create_from_format("m/d/Y", $val);
                            if ($d) {
                                $val = date_format($d, 'Y-m-d');
                            } else {
                                $val = null;
                            }
                        } else {
                            $val = null;
                        }
                    } elseif ($type == "location") {
                        $val2 = explode(",", $val);
                        //$row[$entityField['field_name'] . "_latitude"] = -floatval($val2[0]);
                        //$row[$entityField['field_name'] . "_longitude"] = -floatval($val2[1]);
                        $glData[$ent][$entityField['field_name'] . "_latitude"] = -floatval($val2[0]);
                        $glData[$ent][$entityField['field_name'] . "_longitude"] = -floatval($val2[1]);
                        $val = null;
                    } elseif ($type == "radio") {
                        if ($val == "NO") {
                            $val = "n";
                        } elseif ($val == "SÍ") {
                            $val = "y";
                        } else {
                            $val = null;
                        }
                    }
                    if ($val) {
                        //$row[$entityField['field_name']] = $val;
                        $glData[$ent][$entityField['field_name']] = $val;
                    }
                }
            }
            //var_dump($glData);exit;
            $browse = new Browse();
            $rows = $browse->ExecuteQuery("select event_record_number as id from event where event_title='" . $glData['event']['event_title'] . "'");
            if ($rows && $rows[0]['id']) {
                $event = new Event();
                $event->LoadFromRecordNumber($rows[0]['id']);
                $event->LoadRelationships();
            } else {
                continue;
            }
            if ($event->supporting_documents) {
                $supporting_documents = $event->supporting_documents;
            } else {
                $supporting_documents = array();
            }
            for ($j = 1; $j <= $docsCount; $j++) {
                if ($glData["supporting_docs_meta" . $j]) {
                    if (!$glData["supporting_docs_meta" . $j]['title']) {
                        continue;
                    }
                    $document_form = document_form('new');
                    unset($document_form['doc_id']);
                    $supporting_docs = new SupportingDocs();
                    $supporting_docs_meta = new SupportingDocsMeta();
                    $type = null;
                    $doc_uuid = shn_create_uuid('doc');
                    $supporting_docs->doc_id = $doc_uuid;
                    $supporting_docs_meta->doc_id = $doc_uuid;
                    $supporting_docs->uri = '';
                    form_objects($document_form, $supporting_docs, $glData["supporting_docs_meta" . $j]);
                    form_objects($document_form, $supporting_docs_meta, $glData["supporting_docs_meta" . $j]);
                    $supporting_docs_meta->format = $type;
                    $supporting_docs->Save();
                    $supporting_docs_meta->SaveAll();
                    $supporting_documents[] = $doc_uuid;
                }
            }
            if ($supporting_documents) {
                $event->supporting_documents = $supporting_documents;
                $event->SaveDocs();
            }
        }
        $results[] = $row;
        $i++;
    }
    return $results;
}
 public function act_mt_customization()
 {
     global $conf;
     include_once APPROOT . 'inc/lib_form.inc';
     include_once APPROOT . 'inc/lib_form_util.inc';
     //if the locale is changed need to display extra column in label customization
     $this->locale = $conf['locale'];
     include_once APPROOT . 'inc/i18n/lib_l10n.inc';
     $this->locales = l10n_get_locals();
     $translations = StringTranslations::getMtTranslations(null, $conf['locale']);
     //display the mt select from
     $mtIndex = new MtIndex();
     $index_terms = $mtIndex->Find('');
     $options = array();
     $options[''] = '';
     foreach ($index_terms as $index_term) {
         $label = $index_term->no . ' - ' . $index_term->term;
         if ($translations[$index_term->no][$conf['locale']]) {
             $label = $translations[$index_term->no][$conf['locale']]['value'];
         }
         $options[$index_term->no] = $label;
     }
     $this->mt_select = $_GET['mt_select'];
     $this->sub_act = isset($_REQUEST['sub_act']) ? $_REQUEST['sub_act'] : 'label';
     $_REQUEST['sub_act'] = $this->sub_act;
     //include select entity form
     include_once 'lib_mt_customization.inc';
     $this->customization_form = $customization_form;
     if (isset($this->mt_select)) {
         //handle delete requests
         if ($_POST['bulkaction'] && !$_POST['vocab_number_list']) {
             shnMessageQueue::addError(_t('Please select items to perform action'));
         }
         if (isset($_POST['bulkaction']) && $_POST['bulkaction'] == "deleteselected") {
             $this->has_children = false;
             foreach ($_POST['vocab_number_list'] as $vocab_number => $v) {
                 $this->has_children = $this->has_children || mt_customization_has_children($vocab_number);
             }
             if (!$this->has_children) {
                 mt_customization_delete($this->mt_select, $_POST['vocab_number_list']);
             } else {
                 $this->haschildren = true;
             }
         } else {
             if (isset($_POST['delete_yes'])) {
                 mt_customization_delete($this->mt_select, $_POST['vocab_number_list']);
             } elseif ($this->sub_act == 'order' && isset($_POST['itemsorder'])) {
                 $itemorders = @json_decode(stripslashes($_POST['itemsorder']), true);
                 if (is_array($itemorders)) {
                     mt_customization_update_terms_order($itemorders);
                 }
             } elseif ($this->sub_act == 'label' && (isset($_POST['update']) || $_POST['bulkaction'] == "updateselected")) {
                 mt_customization_add_terms($this->mt_select);
                 mt_customization_update();
             } elseif ($this->sub_act == 'label' && $_POST['bulkaction'] == "visible") {
                 mt_customization_add_terms($this->mt_select);
                 mt_visibility_update($_POST['vocab_number_list'], "y");
             } elseif ($this->sub_act == 'label' && $_POST['bulkaction'] == "disable") {
                 mt_customization_add_terms($this->mt_select);
                 mt_visibility_update($_POST['vocab_number_list'], "n");
             }
         }
         $mtTerms = new MtTerms();
         if ($this->sub_act == 'label') {
             $this->res = Browse::getHuriTermsTranslations($this->mt_select, $this->locales);
         } elseif ($this->sub_act == 'order') {
             $this->res = MtFieldWrapper::getMTList($this->mt_select);
         }
     }
 }
 public function act_facetsearchresults()
 {
     global $global, $conf;
     $searchparams = $_GET['searchparams'];
     $searchparams = json_decode($searchparams);
     $resp = array();
     if ($searchparams) {
         $domaindata = $this->getEntityFields();
         $query = new stdClass();
         $ents = array();
         $additionalFields = array();
         if (count((array) $searchparams->selected_terms)) {
             $fieldCounts = array();
             foreach ($searchparams->selected_terms as $entity => $fields) {
                 foreach ($fields as $field => $terms) {
                     if (!$terms) {
                         continue;
                     }
                     $ents[] = $entity;
                     if ($domaindata->{$entity}->ac_type) {
                         $selEntity2 = $domaindata->{$entity}->ac_type;
                     } else {
                         $selEntity2 = $entity;
                     }
                     $condition = new stdClass();
                     $condition->entity = $entity;
                     $condition->field = $field;
                     $condition->operator = "in";
                     $condition->value = implode(",", $terms);
                     $condition->link = "and";
                     if ($domaindata->{$selEntity2}->fields->{$field}->field_type == "mt_tree") {
                         $condition->operator = "subin";
                     } else {
                         $condition->operator = "in";
                     }
                     $query->conditions[] = $condition;
                 }
             }
         }
         $selEntity = $selEntityOriginal = $searchparams->entities[0];
         //var_dump($selEntity);exit;
         if ($domaindata->{$selEntity}->ac_type) {
             $selEntity = $domaindata->{$selEntity}->ac_type;
         }
         $selectFields = array();
         foreach ($query->conditions as $cond) {
             $sel = new stdClass();
             $sel->entity = $cond->entity;
             $sel->field = $cond->field;
             $selectFields[] = $sel;
         }
         if ($searchparams->facets) {
             foreach ($searchparams->facets as $facet) {
                 $sel = new stdClass();
                 $sel->entity = $facet->entity;
                 $sel->field = $facet->field;
                 $selectFields[] = $sel;
             }
         }
         foreach ($domaindata->{$selEntity}->fields as $field) {
             if ($field->select == "y") {
                 $sel = new stdClass();
                 $sel->entity = $selEntityOriginal;
                 $sel->field = $field->value;
                 $selectFields[] = $sel;
             }
         }
         $selectFields = array_unique($selectFields, SORT_REGULAR);
         foreach ($selectFields as $sfield) {
             if (!in_array($sfield->entity, $ents)) {
                 $ents[] = $sfield->entity;
                 $condition = new stdClass();
                 $entt = $sfield->entity;
                 $selEntt = $entt;
                 if ($domaindata->{$entt}->ac_type) {
                     $selEntt = $domaindata->{$entt}->ac_type;
                 }
                 $fields = (array) $domaindata->{$selEntt};
                 $f = (array) $domaindata->{$selEntt}->fields;
                 $pkey = get_primary_key($selEntt);
                 if ($f[$pkey]) {
                     $f = $f[$pkey];
                 } else {
                     $f = array_shift($f);
                 }
                 $condition->entity = $entt;
                 $condition->field = $f->value;
                 $condition->operator = "contains";
                 $condition->value = "";
                 $query->conditions[] = $condition;
             }
         }
         foreach ($ents as $ent) {
             $recField = get_primary_key($ent);
             $sel = new stdClass();
             $sel->entity = $ent;
             $sel->field = $recField;
             if (!in_array($sel, $selectFields)) {
                 $selectFields[] = $sel;
                 $additionalFields[$sel->entity][] = $recField;
             }
         }
         $selectFields = array_unique($selectFields, SORT_REGULAR);
         $query->select = $selectFields;
         function entitySort($a, $b)
         {
             $enord = array('event', 'act', 'intervention', 'arrest', 'torture', 'killing', 'destruction', 'victim', 'involvement', 'perpetrator', 'information', 'source', 'chain_of_events', 'intervening_party', 'address', 'biographic_details', 'supporting_docs_meta');
             $aorder = array_search($a->entity, $enord);
             $border = array_search($b->entity, $enord);
             return $border - $aorder;
         }
         usort($query->select, "entitySort");
         usort($query->conditions, "entitySort");
         $from = (int) $searchparams->paging->from;
         $size = (int) $searchparams->paging->size;
         if ($from < 0) {
             $from = 0;
         }
         if ($size < 1) {
             $size = 20;
         }
         $records = array();
         include_once 'searchSql.php';
         $start = $from;
         $limit = $size;
         $sidx = $_GET['sidx'];
         if (!$sidx) {
             $sidx = 1;
         }
         $sord = $_GET['sord'];
         $fields_array = array();
         $entities = analysis_get_search_entities();
         $fieldTitles = array();
         //if the query is a search put select fields to the array
         foreach ($query->select as $field) {
             if (in_array($field->field, $additionalFields[$field->entity])) {
                 continue;
             }
             $entity = isset($entities[$field->entity]['ac_type']) ? $entities[$field->entity]['ac_type'] : $field->entity;
             $mt = is_mt_field($entity, $field->field);
             array_push($fields_array, array('name' => $field->entity . '_' . $field->field, 'mt' => $mt));
             $fieldname = $field->field;
             if (isset($domaindata->{$entity}->fields->{$fieldname}->label)) {
                 $fieldTitles[] = $domaindata->{$entity}->fields->{$fieldname}->label;
             } else {
                 $fieldTitles[] = $field->field;
             }
         }
         $records[0] = $fieldTitles;
         $searchSql = new SearchResultGenerator();
         $sqlArray = $searchSql->sqlForJsonQuery(json_encode($query));
         $count_query = "SELECT COUNT(*) FROM ({$sqlArray['result']}) as results";
         try {
             $res_count = $global['db']->Execute($count_query);
         } catch (Exception $e) {
             $response->error = "error";
             //$e->getMessage();
             $res_count = null;
         }
         if ($res_count != null) {
             while (!$res_count->EOF) {
                 $count = $res_count->fields[0];
                 $res_count->MoveNext();
             }
         }
         $sql = $sqlArray['result'];
         if ($limit != -1) {
             $sql .= " LIMIT {$start} , {$limit}";
         }
         try {
             $res = $global['db']->Execute($sql);
         } catch (Exception $e) {
             $response->error = "error";
             //$e->getMessage();
         }
         $resp = array("response" => array("start" => $from, "found" => $count));
         $number_of_fields = count($fields_array);
         foreach ($res as $key => $val) {
             //$response->aaData[$i]['id'] = $val[$fields_array[0]];
             $array_values = array();
             for ($count = 0; $number_of_fields > $count; $count++) {
                 $field_name = $fields_array[$count]['name'];
                 $record_number_field = substr($field_name, strlen($field_name) - 13);
                 $confidentiality_field = substr($field_name, strlen($field_name) - 15);
                 $deceased_field = substr($field_name, strlen($field_name) - 8);
                 $doc_field = substr($field_name, strlen($field_name) - 6);
                 if ($confidentiality_field == 'confidentiality') {
                     if ($val[$field_name] == 'y') {
                         $val[$field_name] = _t('YES');
                     } elseif ($val[$field_name] == 'n') {
                         $val[$field_name] = _t('NO');
                     }
                 }
                 if ($deceased_field == 'deceased') {
                     if ($val[$field_name] == 'y') {
                         $val[$field_name] = _t('YES');
                     } else {
                         $val[$field_name] = _t('NO');
                     }
                 }
                 $string = null;
                 if ($fields_array[$count]['mt']) {
                     $list = explode(',', $val[$field_name]);
                     //var_dump($val);exit;
                     foreach ($list as $term) {
                         $string = $string . ", " . get_mt_term(trim($term));
                     }
                     $array_values[$field_name] = ltrim($string, ',');
                 } else {
                     if ($record_number_field == 'record_number' || $doc_field == 'doc_id') {
                         if (preg_match('/event/', $field_name)) {
                             $link_entity = 'event';
                         } else {
                             if (preg_match('/act/', $field_name)) {
                                 $link_entity = 'act';
                             } else {
                                 if (preg_match('/source/', $field_name)) {
                                     $link_entity = 'source';
                                 } else {
                                     if (preg_match('/perpetrator/', $field_name)) {
                                         $link_entity = 'perpetrator';
                                     } else {
                                         if (preg_match('/victim/', $field_name)) {
                                             $link_entity = 'victim';
                                         } else {
                                             if (preg_match('/involvement/', $field_name)) {
                                                 $link_entity = 'involvement';
                                             } else {
                                                 if (preg_match('/information/', $field_name)) {
                                                     $link_entity = 'information';
                                                 } else {
                                                     if (preg_match('/intervention/', $field_name)) {
                                                         $link_entity = 'intervention';
                                                     } else {
                                                         if (preg_match('/intervening_party/', $field_name)) {
                                                             $link_entity = 'intervening_party';
                                                         } else {
                                                             if (preg_match('/person/', $field_name)) {
                                                                 $link_entity = 'person';
                                                             } else {
                                                                 if (preg_match('/biographic_details/', $field_name)) {
                                                                     $link_entity = 'biographic_details';
                                                                 } else {
                                                                     if (preg_match('/supporting_docs_meta/', $field_name)) {
                                                                         $link_entity = 'supporting_docs_meta';
                                                                     } else {
                                                                         if (preg_match('/address/', $field_name)) {
                                                                             $link_entity = 'address';
                                                                         }
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                         if ($link_entity != 'address') {
                             $url = get_record_url($val[$field_name], $link_entity);
                             $array_values[$field_name] = "<a href='{$url}' target='_blank'>" . $val[$field_name] . "</a>";
                         } else {
                             if ($val[$field_name]) {
                                 $array_values[$field_name] = $val[$field_name];
                             } else {
                                 $array_values[$field_name] = "";
                             }
                         }
                     } else {
                         if ($val[$field_name]) {
                             $array_values[$field_name] = $val[$field_name];
                         } else {
                             $array_values[$field_name] = "";
                         }
                     }
                 }
             }
             $records[] = $array_values;
             $i++;
         }
         $resp["response"]["records"] = $records;
         $recField = get_primary_key($selEntity);
         // . "_record_number";
         $charts = array();
         $conditions = array();
         if (count((array) $query->conditions)) {
             foreach ($query->conditions as $condition) {
                 $condition2 = $condition;
                 $conditions[$condition->entity][$condition->field][] = $condition2;
             }
         }
         //var_dump($conditions, $sqlArray['result']);
         if ($searchparams->facets) {
             foreach ($searchparams->facets as $facet) {
                 $entity = $facet->entity;
                 $field = $facet->field;
                 if ($domaindata->{$entity}->ac_type) {
                     $en = $domaindata->{$entity}->ac_type;
                     $fields = $domaindata->{$en}->fields;
                 } else {
                     $fields = $domaindata->{$entity}->fields;
                 }
                 $fieldType = $fields->{$field}->field_type;
                 $listCode = $fields->{$field}->list_code;
                 //charts
                 $entityForm = $searchSql->getEntityArray($entity);
                 $fieldArray = $entityForm[$field];
                 $recField = get_primary_key($selEntityOriginal);
                 $selEntt = $entity;
                 if ($domaindata->{$selEntt}->ac_type) {
                     $selEntt = $domaindata->{$selEntt}->ac_type;
                 }
                 $recFieldEnt = get_primary_key($searchSql->tableOfEntity($fieldArray['map']['entity']));
                 if ($fieldArray['map']['mlt'] && $fieldArray['type'] != "user_select") {
                     $mltTable = 'mlt_' . $searchSql->tableOfEntity($fieldArray['map']['entity']) . '_' . $fieldArray['map']['field'];
                     $sqlchart = "SELECT IFNULL(l.msgstr , english) as val, COUNT(t.record_number) AS count,m.vocab_number as vocab_number\n                            FROM ({$sqlArray['result']}) d LEFT JOIN {$mltTable} t  on  t.record_number=d.{$entity}_{$recFieldEnt} left join\n                            mt_vocab m on m.vocab_number=t.vocab_number\n                            LEFT JOIN mt_vocab_l10n l ON ( l.msgid = m.vocab_number AND l.locale = '{$conf['locale']}' )  GROUP BY t.vocab_number\n                            ";
                 } elseif (is_management_field($fieldArray)) {
                     $f = $entity . "_" . $fieldArray['map']['field'];
                     $sqlchart = "SELECT IFNULL(l.msgstr , english) as val, COUNT(d.{$selEntityOriginal}_{$recField}) AS count,m.vocab_number as vocab_number\n                            FROM ({$sqlArray['result']}) d LEFT JOIN management t  on t.entity_id=d.{$entity}_{$recFieldEnt} and t.entity_type='{$entity}'\n                            left join  mt_vocab m on m.vocab_number=t.{$fieldArray['map']['field']}\n                            LEFT JOIN mt_vocab_l10n l ON ( l.msgid = m.vocab_number AND l.locale = '{$conf['locale']}' )  GROUP BY {$f}\n                            ";
                 } else {
                     $f = $entity . "_" . $fieldArray['map']['field'];
                     if ($fieldType == "mt_select" || $fieldType == "mt_tree") {
                         $sqlchart = "SELECT IFNULL(l.msgstr , english)  as val, COUNT({$selEntityOriginal}_{$recField}) AS count,m.vocab_number as vocab_number\n                            FROM ({$sqlArray['result']}) d left join  mt_vocab m on m.vocab_number=d.{$f}\n                            LEFT JOIN mt_vocab_l10n l ON ( l.msgid = m.vocab_number AND l.locale = '{$conf['locale']}' )  GROUP BY {$f}";
                     } else {
                         $sqlchart = "SELECT d.{$f} as val, COUNT({$selEntityOriginal}_{$recField}) AS count,d.{$f} as vocab_number\n                            FROM ({$sqlArray['result']}) d   GROUP BY {$f}";
                     }
                 }
                 $facetcounts = array();
                 if ($sqlchart) {
                     try {
                         $res = $global['db']->Execute($sqlchart);
                         $chart = array();
                         $chart["type"] = "editchart";
                         $chart["editcharttype"] = "BarChart";
                         $chart["title"] = $fieldArray["label"];
                         $chart2 = $chart;
                         $chart2["editcharttype"] = "PieChart";
                         foreach ($res as $val) {
                             $vall = _t("Undefined");
                             if ($val[0]) {
                                 if ($val[0] == "y") {
                                     $vall = _t('YES');
                                 } elseif ($val[0] == "n") {
                                     $vall = _t('NO');
                                 } else {
                                     $vall = $val[0];
                                 }
                             } elseif (!(int) $val[1]) {
                                 continue;
                             }
                             $chart["data"][0][0] = $chart["title"];
                             $chart["data"][1][0] = "";
                             $chart["data"][0][] = $vall;
                             $chart["data"][1][] = (int) $val[1];
                             $chart2["data"][0] = array($chart["title"], _t("COUNT"));
                             $chart2["data"][] = array($vall, (int) $val[1]);
                             $facetcounts[$val[2]] = (int) $val[1];
                         }
                         //$resp["charts"][] = array($chart, $chart2);
                         $resp["charts"][] = array($chart);
                         $resp["charts"][] = array($chart2);
                     } catch (Exception $e) {
                         $response->error = "error";
                         //$e->getMessage();
                     }
                 }
                 $terms = array();
                 switch ($fieldType) {
                     case 'radio':
                         $label = _t('YES');
                         if (isset($facetcounts["y"])) {
                             $label .= " (" . $facetcounts["y"] . ")";
                         }
                         $resp["facets"][$field]["terms"][] = array('term' => 'y', 'label' => $label);
                         $label = _t('NO');
                         if (isset($facetcounts["n"])) {
                             $label .= " (" . $facetcounts["n"] . ")";
                         }
                         $resp["facets"][$field]["terms"][] = array('term' => 'n', 'label' => $label);
                         $resp["facets"][$field]["entity"] = $entity;
                         break;
                     case 'mt_select':
                         $data_array = MtFieldWrapper::getMTList($listCode);
                         $size = count($data_array);
                         $options[''] = ' ';
                         for ($i = 0; $i < $size; $i++) {
                             $label = $data_array[$i]['label'];
                             if (isset($facetcounts[$data_array[$i]['vocab_number']])) {
                                 $label .= " (" . $facetcounts[$data_array[$i]['vocab_number']] . ")";
                             }
                             $terms[] = array('term' => $data_array[$i]['vocab_number'], 'label' => $label);
                         }
                         $resp["facets"][$field] = array("terms" => $terms, "entity" => $entity);
                         break;
                     case 'mt_tree':
                         $data_array = MtFieldWrapper::getMTList($listCode);
                         $count = count($data_array);
                         for ($i = 0; $i < $count;) {
                             $element1 = $data_array[$i];
                             $label = $element1['label'];
                             if (isset($facetcounts[$element1['vocab_number']])) {
                                 $label .= " (" . $facetcounts[$element1['vocab_number']] . ")";
                             }
                             $terms[] = array('term' => $element1['vocab_number'], 'label' => $label, 'level' => (int) $element1["term_level"]);
                             $i++;
                         }
                         $resp["facets"][$field] = array("terms" => $terms, "field_type" => "mt_tree", "entity" => $entity);
                         break;
                 }
             }
         }
         $locationFields = Browse::getEntityLocationFields($selEntity);
         $field_names = array();
         $fieldsArray = array();
         $markers = array();
         if ($locationFields) {
             $selectFields = array();
             $sel = new stdClass();
             $sel->entity = $selEntityOriginal;
             $sel->field = get_primary_key($selEntity);
             // . "_record_number";
             $selectFields[] = $sel;
             foreach ($locationFields as $locationField) {
                 $field_names[] = $locationField["field_name"];
                 /* $fieldsArray[] = $locationField["field_name"] . "_latitude";
                    $fieldsArray[] = $locationField["field_name"] . "_longitude"; */
                 $sel = new stdClass();
                 $sel->entity = $selEntityOriginal;
                 $sel->field = $locationField["field_name"] . "_latitude";
                 $selectFields[] = $sel;
                 $sel = new stdClass();
                 $sel->entity = $selEntityOriginal;
                 $sel->field = $locationField["field_name"] . "_longitude";
                 $selectFields[] = $sel;
             }
             $query->select = $selectFields;
             $searchSql = new SearchResultGenerator();
             $sqlArray = $searchSql->sqlForJsonQuery(json_encode($query));
             $sql = $sqlArray['result'];
             if ($limit != -1) {
                 // $sql .= " LIMIT $start , $limit";
             }
             //$sql = "select " . $selEntity . "_record_number," . implode(",", $fieldsArray) . " from " . $selEntity;
             try {
                 $res = $global['db']->GetAll($sql);
             } catch (Exception $e) {
                 $response->error = "error";
                 //$e->getMessage();
             }
             foreach ($res as $val) {
                 $url = get_record_url($val[0], $selEntity);
                 $i = 1;
                 foreach ($field_names as $field_name) {
                     if ($val[$i] && $val[$i + 1]) {
                         $markers[] = array("latitude" => $val[$i], "longitude" => $val[$i + 1], "title" => $val[0], "content" => "<a href='" . $url . "' target='_blank'>" . $val[0] . "</a>");
                     }
                     $i = $i + 2;
                 }
             }
         }
         $resp["markers"] = $markers;
     }
     echo json_encode($resp);
     exit;
 }