/**
  * createQuickSearchCode
  * This function creates the $sqs_objects array that will be used by the quicksearch Javascript
  * code.  The $sqs_objects array is wrapped in a $json->encode call.
  *
  * @param $def The vardefs.php definitions
  * @param $defs2 The Meta-Data file definitions
  *
  */
 function createQuickSearchCode($defs, $defs2, $view = '', $module = '')
 {
     $sqs_objects = array();
     require_once 'include/QuickSearchDefaults.php';
     $qsd = new QuickSearchDefaults();
     $qsd->setFormName($view);
     if (preg_match('/^SearchForm_.+/', $view)) {
         if (strpos($view, 'popup_query_form')) {
             $qsd->setFormName('popup_query_form');
             $parsedView = 'advanced';
         } else {
             $qsd->setFormName('search_form');
             $parsedView = preg_replace("/^SearchForm_/", "", $view);
         }
         //Loop through the Meta-Data fields to see which ones need quick search support
         foreach ($defs as $f) {
             $field = $f;
             $name = $qsd->form_name . '_' . $field['name'];
             if ($field['type'] == 'relate' && isset($field['module']) && preg_match('/_name$|_c$/si', $name)) {
                 if (preg_match('/^(Campaigns|Teams|Users|Contacts|Accounts)$/si', $field['module'], $matches)) {
                     if ($matches[0] == 'Campaigns') {
                         $sqs_objects[$name . '_' . $parsedView] = $qsd->loadQSObject('Campaigns', 'Campaign', $field['name'], $field['id_name'], $field['id_name']);
                     } else {
                         if ($matches[0] == 'Users') {
                             if (!empty($f['name']) && !empty($f['id_name'])) {
                                 $sqs_objects[$name . '_' . $parsedView] = $qsd->getQSUser($f['name'], $f['id_name']);
                             } else {
                                 $sqs_objects[$name . '_' . $parsedView] = $qsd->getQSUser();
                             }
                         } else {
                             if ($matches[0] == 'Campaigns') {
                                 $sqs_objects[$name . '_' . $parsedView] = $qsd->loadQSObject('Campaigns', 'Campaign', $field['name'], $field['id_name'], $field['id_name']);
                             } else {
                                 if ($matches[0] == 'Accounts') {
                                     $nameKey = $name;
                                     $idKey = isset($field['id_name']) ? $field['id_name'] : 'account_id';
                                     //There are billingKey, shippingKey and additionalFields entries you can define in editviewdefs.php
                                     //entry to allow quick search to autocomplete fields with a suffix value of the
                                     //billing/shippingKey value (i.e. 'billingKey' => 'primary' in Contacts will populate
                                     //primary_XXX fields with the Account's billing address values).
                                     //addtionalFields are key/value pair of fields to fill from Accounts(key) to Contacts(value)
                                     $billingKey = isset($f['displayParams']['billingKey']) ? $f['displayParams']['billingKey'] : null;
                                     $shippingKey = isset($f['displayParams']['shippingKey']) ? $f['displayParams']['shippingKey'] : null;
                                     $additionalFields = isset($f['displayParams']['additionalFields']) ? $f['displayParams']['additionalFields'] : null;
                                     $sqs_objects[$name . '_' . $parsedView] = $qsd->getQSAccount($nameKey, $idKey, $billingKey, $shippingKey, $additionalFields);
                                 } else {
                                     if ($matches[0] == 'Contacts') {
                                         $sqs_objects[$name . '_' . $parsedView] = $qsd->getQSContact($field['name'], $field['id_name']);
                                     }
                                 }
                             }
                         }
                     }
                 } else {
                     $sqs_objects[$name . '_' . $parsedView] = $qsd->getQSParent($field['module']);
                     if (!isset($field['field_list']) && !isset($field['populate_list'])) {
                         $sqs_objects[$name . '_' . $parsedView]['populate_list'] = array($field['name'], $field['id_name']);
                         $sqs_objects[$name . '_' . $parsedView]['field_list'] = array('name', 'id');
                     } else {
                         $sqs_objects[$name . '_' . $parsedView]['populate_list'] = $field['field_list'];
                         $sqs_objects[$name . '_' . $parsedView]['field_list'] = $field['populate_list'];
                     }
                 }
             } else {
                 if ($field['type'] == 'parent') {
                     $sqs_objects[$name . '_' . $parsedView] = $qsd->getQSParent();
                 }
             }
             //if-else
         }
         //foreach
         foreach ($sqs_objects as $name => $field) {
             foreach ($field['populate_list'] as $key => $fieldname) {
                 $sqs_objects[$name]['populate_list'][$key] = $sqs_objects[$name]['populate_list'][$key] . '_' . $parsedView;
             }
         }
     } else {
         //Loop through the Meta-Data fields to see which ones need quick search support
         foreach ($defs2 as $f) {
             if (!isset($defs[$f['name']])) {
                 continue;
             }
             $field = $defs[$f['name']];
             if ($view == "ConvertLead") {
                 $field['name'] = $module . $field['name'];
                 if (!empty($field['id_name'])) {
                     $field['id_name'] = $field['name'] . "_" . $field['id_name'];
                 }
             }
             $name = $qsd->form_name . '_' . $field['name'];
             if ($field['type'] == 'relate' && isset($field['module']) && (preg_match('/_name$|_c$/si', $name) || !empty($field['quicksearch']))) {
                 if (!preg_match('/_c$/si', $name) && preg_match('/^(Campaigns|Teams|Users|Contacts|Accounts)$/si', $field['module'], $matches)) {
                     if ($matches[0] == 'Campaigns') {
                         $sqs_objects[$name] = $qsd->loadQSObject('Campaigns', 'Campaign', $field['name'], $field['id_name'], $field['id_name']);
                     } else {
                         if ($matches[0] == 'Users') {
                             if ($field['name'] == 'reports_to_name') {
                                 $sqs_objects[$name] = $qsd->getQSUser('reports_to_name', 'reports_to_id');
                             } else {
                                 if ($view == "ConvertLead") {
                                     $sqs_objects[$name] = $qsd->getQSUser($field['name'], $field['id_name']);
                                 } else {
                                     $sqs_objects[$name] = $qsd->getQSUser();
                                 }
                             }
                         } else {
                             if ($matches[0] == 'Campaigns') {
                                 $sqs_objects[$name] = $qsd->loadQSObject('Campaigns', 'Campaign', $field['name'], $field['id_name'], $field['id_name']);
                             } else {
                                 if ($matches[0] == 'Accounts') {
                                     $nameKey = $name;
                                     $idKey = isset($field['id_name']) ? $field['id_name'] : 'account_id';
                                     //There are billingKey, shippingKey and additionalFields entries you can define in editviewdefs.php
                                     //entry to allow quick search to autocomplete fields with a suffix value of the
                                     //billing/shippingKey value (i.e. 'billingKey' => 'primary' in Contacts will populate
                                     //primary_XXX fields with the Account's billing address values).
                                     //addtionalFields are key/value pair of fields to fill from Accounts(key) to Contacts(value)
                                     $billingKey = SugarArray::staticGet($f, 'displayParams.billingKey');
                                     $shippingKey = SugarArray::staticGet($f, 'displayParams.shippingKey');
                                     $additionalFields = SugarArray::staticGet($f, 'displayParams.additionalFields');
                                     $sqs_objects[$name] = $qsd->getQSAccount($nameKey, $idKey, $billingKey, $shippingKey, $additionalFields);
                                 } else {
                                     if ($matches[0] == 'Contacts') {
                                         $sqs_objects[$name] = $qsd->getQSContact($field['name'], $field['id_name']);
                                         if (preg_match('/_c$/si', $name) || !empty($field['quicksearch'])) {
                                             $sqs_objects[$name]['field_list'] = array('salutation', 'first_name', 'last_name', 'id');
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 } else {
                     $sqs_objects[$name] = $qsd->getQSParent($field['module']);
                     if (!isset($field['field_list']) && !isset($field['populate_list'])) {
                         $sqs_objects[$name]['populate_list'] = array($field['name'], $field['id_name']);
                         // now handle quicksearches where the column to match is not 'name' but rather specified in 'rname'
                         if (!isset($field['rname'])) {
                             $sqs_objects[$name]['field_list'] = array('name', 'id');
                         } else {
                             $sqs_objects[$name]['field_list'] = array($field['rname'], 'id');
                             $sqs_objects[$name]['order'] = $field['rname'];
                             $sqs_objects[$name]['conditions'] = array(array('name' => $field['rname'], 'op' => 'like_custom', 'end' => '%', 'value' => ''));
                         }
                     } else {
                         $sqs_objects[$name]['populate_list'] = $field['field_list'];
                         $sqs_objects[$name]['field_list'] = $field['populate_list'];
                     }
                 }
             } else {
                 if ($field['type'] == 'parent') {
                     $sqs_objects[$name] = $qsd->getQSParent();
                 }
             }
             //if-else
         }
         //foreach
     }
     //Implement QuickSearch for the field
     if (!empty($sqs_objects) && count($sqs_objects) > 0) {
         $quicksearch_js = '<script language="javascript">';
         $quicksearch_js .= 'if(typeof sqs_objects == \'undefined\'){var sqs_objects = new Array;}';
         $json = getJSONobj();
         foreach ($sqs_objects as $sqsfield => $sqsfieldArray) {
             $quicksearch_js .= "sqs_objects['{$sqsfield}']={$json->encode($sqsfieldArray)};";
         }
         return $quicksearch_js . '</script>';
     }
     return '';
 }