PopUp Relations

If you use this modification you read and accept the following introduction:
https://support.stefanwarnat.de/en:modifications

Explanation of modification

This modification could help you, if you create a custom module and want to link this module with default modules.

For example you create a new Module “Buildings” which has a field “Organization” to link a Organizations.
Now you had a related field in Contacts to choose a record within the new “Buildings” module.

In basic Vtiger you will see every available record in “Buildings” if you click on the search button, regardless if you select a Organization in the Contact, which could limit the available records.
This modification will integrate this in a flexible way.
You could create a configuration in the database to only get Buildings from the Organization of the Contacts as selection. (See below)
But you are not limited to this records. If you use the search option in the popup you could nevertheless select every record.

Read first

This modification was only tested in vtigerCRM 6.1.

This modification could decrease the frontend performance a little, because it needs to be integrated into every EditView. Regardless if you use this feature in the module.
In modern Browsers you won't recognize this.
It execute 2 database Queries on the EditView/CreateView and one in the PopUp.

At first please install the latest version of VtigerCRM Tools

Create DB Table

CREATE TABLE IF NOT EXISTS `vtiger_popup_relation` (
`id` INT(11) NOT NULL,
  `src_module` VARCHAR(30) NOT NULL,
  `src_field` VARCHAR(50) NOT NULL,
  `module` VARCHAR(30) NOT NULL,
  `search_key` VARCHAR(50) NOT NULL,
  `search_value` VARCHAR(50) NOT NULL
) ENGINE=InnoDB ;
 
ALTER TABLE `vtiger_popup_relation`
 ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `src_module` (`src_module`,`src_field`,`module`);
 
ALTER TABLE `vtiger_popup_relation`
MODIFY `id` INT(11) NOT NULL AUTO_INCREMENT;

Open: /modules/Vtiger/views/Popup.php

Search: [~Line 106]

if(empty($cvId)) {

Insert Before:

/**
 * START swarnat Modification
 * https://support.stefanwarnat.de/en:modifications:popup_relations
 */
$swpopup_search= $request->get('swpopup_search');
if(empty($searchKey) === true && empty($swpopup_search) === false) {
    $searchValue = Vtiger_Util_Helper::getRecordName(intval($request->get('swpopup_value')));
    $searchKey = $swpopup_search;
}
/**
 * END swarnat Modification
 * https://support.stefanwarnat.de/en:modifications:popup_relations
 */

Open: /module/Vtiger/views/Edit.php

Search: [~Line 106]

$viewer->view('EditView.tpl', $moduleName);

Insert After:

/**
 * START swarnat Modification
 * https://support.stefanwarnat.de/en:modifications:popup_relations
 */
$adb = \PearDatabase::getInstance();
 
$result = $adb->pquery(
    'SELECT src_field, search_key, search_value FROM vtiger_popup_relation WHERE src_module = ?',
    array($moduleName)
);
if($adb->num_rows($result) > 0) {
    $additionalParameters = array();
    while($row = $adb->fetchByAssoc($result)) {
        $additionalParameters[$row['src_field']] = array($row['search_key'], $row['search_value']);
    }
    echo '<script type="text/javascript">
        var $additionalSearchParameters = '.json_encode($additionalParameters).';
        jQuery(function() {
            if(typeof SWVtigerTools !== "undefined") {
                SWVtigerTools.registerFilter("PopUpParams", function(params) {
                    if(typeof $additionalSearchParameters[params.src_field] !== "undefined") {
                        var value = $additionalSearchParameters[params.src_field];
                        var fieldValue = jQuery(\'[name="\' + value[1] + \'"]\').val();
                        if(fieldValue == "") return params;
                        params["swpopup_search"] = value[0];
                        params["swpopup_value"] = fieldValue
                    }
                    return params;
                });
            }
        });
    </script>';
}
/**
 * END swarnat Modification
 * https://support.stefanwarnat.de/en:modifications:popup_relations
 */

Open: /layouts/vlayout/modules/Vtiger/resources/Edit.js

Search: [~Line 109]

if(isMultiple) {
    params.multi_select = true ;
}

Insert After:

/**
 * START swarnat Modification
 * https://support.stefanwarnat.de/en:modifications:popup_relations
 */
if(typeof SWVtigerTools === 'object' && typeof SWVtigerTools.filter === 'function') {
    params = SWVtigerTools.filter('PopUpParams', params);
}
/**
 * END swarnat Modification
 * https://support.stefanwarnat.de/en:modifications:popup_relations
 */

Create a Relation

You now could use this feature.
You need to insert a new line in the just created table vtiger_popup_relation.

You get the values if you open the PopUp you want to modify.

Column Value
src_module You get from the PopUP URL
src_field You get from the PopUP URL
module You get from the PopUP URL
search_key Which field in the related module should be used to search
search_value Which fieldvalue of the record you modify should be used

Example

You have a new Field “Contact B” (Fieldname: contactb_id) in the Organization module, which should only choosable from Contacts within the Organization in “Member Of” (Fieldname: account_id)
(Organization fieldname in Contacts module is “account_id”, too)

You need to insert:

src_module Accounts
src_field contactb_id
module Contacts
search_key account_id (The field within Contacts module)
search_value account_id (The field within Organization module)