Exemple #1
0
/** Function to get the emailids for the given ids form the request parameters
 *  It returns an array which contains the mailids and the parentidlists
 */
function get_to_emailids($module)
{
    global $adb, $current_user, $log;
    require_once 'include/Webservices/Query.php';
    //$idlists1 = "";
    $mailds = '';
    if (empty($_REQUEST['field_lists'])) {
        switch ($module) {
            case 'Accounts':
                $_REQUEST["field_lists"] = 9;
                break;
            case 'Contacts':
                $_REQUEST["field_lists"] = 80;
                break;
            case 'Vendors':
                $_REQUEST["field_lists"] = 292;
                break;
        }
    }
    $fieldids = explode(":", vtlib_purify($_REQUEST['field_lists']));
    if ($_REQUEST['idlist'] == 'all' || $_REQUEST['idlist'] == 'relatedListSelectAll') {
        $idlist = getSelectedRecords($_REQUEST, vtlib_purify($_REQUEST['pmodule']), vtlib_purify($_REQUEST['idlist']), vtlib_purify($_REQUEST['excludedRecords']));
    } else {
        $idlist = explode(":", str_replace("undefined", "", vtlib_purify($_REQUEST['idlist'])));
    }
    $entityids = array();
    foreach ($idlist as $key => $id) {
        $entityids[] = vtws_getWebserviceEntityId($module, $id);
    }
    $vtwsObject = VtigerWebserviceObject::fromName($adb, $module);
    $vtwsCRMObjectMeta = new VtigerCRMObjectMeta($vtwsObject, $current_user);
    $emailFields = $vtwsCRMObjectMeta->getEmailFields();
    foreach ($emailFields as $key => $fieldname) {
        $fieldid = $vtwsCRMObjectMeta->getFieldIdFromFieldName($fieldname);
        if (!in_array($fieldid, $fieldids)) {
            unset($emailFields[$key]);
        }
    }
    if (empty($emailFields)) {
        return false;
    }
    if ($module == 'Leads') {
        $query = 'SELECT firstname,lastname,' . implode(",", $emailFields) . ',vtiger_leaddetails.leadid as id
				  FROM vtiger_leaddetails
				  INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_leaddetails.leadid
				  LEFT JOIN vtiger_leadscf ON vtiger_leaddetails.leadid = vtiger_leadscf.leadid
				  WHERE vtiger_crmentity.deleted=0 AND vtiger_leaddetails.leadid IN (' . generateQuestionMarks($idlist) . ')';
    } else {
        if ($module == 'Contacts') {
            $query = 'SELECT firstname,lastname,' . implode(",", $emailFields) . ',vtiger_contactdetails.contactid as id
				  FROM vtiger_contactdetails
				  INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_contactdetails.contactid
				  LEFT JOIN vtiger_contactscf ON vtiger_contactdetails.contactid = vtiger_contactscf.contactid
				  WHERE vtiger_crmentity.deleted=0 AND vtiger_contactdetails.contactid IN (' . generateQuestionMarks($idlist) . ') AND vtiger_contactdetails.emailoptout=0';
        } else {
            if ($module == 'Accounts') {
                $query = 'SELECT vtiger_account.accountname, ' . implode(",", $emailFields) . ',vtiger_account.accountid as id FROM vtiger_account
				   INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_account.accountid
				   LEFT JOIN vtiger_accountscf ON vtiger_accountscf.accountid= vtiger_account.accountid
				   WHERE vtiger_crmentity.deleted=0 AND vtiger_account.accountid IN (' . generateQuestionMarks($idlist) . ') AND vtiger_account.emailoptout=0';
            } else {
                if ($module == 'Project') {
                    $query = 'SELECT projectname,' . implode(",", $emailFields) . ',vtiger_project.projectid as id
				  FROM vtiger_project
				  INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_project.projectid
				  LEFT JOIN vtiger_projectcf ON vtiger_projectcf.projectid = vtiger_project.projectid
				  WHERE vtiger_crmentity.deleted=0 AND vtiger_project.projectid IN (' . generateQuestionMarks($idlist) . ')';
                } else {
                    if ($module == 'ProjectTask') {
                        $query = 'SELECT projecttaskname,' . implode(",", $emailFields) . ',vtiger_projecttask.projecttaskid as id
				  FROM vtiger_projecttask
				  INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_projecttask.projecttaskid
				  LEFT JOIN vtiger_projecttaskcf ON vtiger_projecttaskcf.projecttaskid = vtiger_projecttask.projecttaskid
				  WHERE vtiger_crmentity.deleted=0 AND vtiger_projecttask.projecttaskid IN (' . generateQuestionMarks($idlist) . ')';
                    } else {
                        if ($module == 'Potentials') {
                            $query = 'SELECT potentialname,' . implode(",", $emailFields) . ',vtiger_potential.potentialid as id
				  FROM vtiger_potential
				  INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_potential.potentialid
				  LEFT JOIN vtiger_potentialscf ON vtiger_potentialscf.potentialid = vtiger_potential.potentialid
				  WHERE vtiger_crmentity.deleted=0 AND vtiger_potential.potentialid IN (' . generateQuestionMarks($idlist) . ')';
                        } else {
                            if ($module == 'HelpDesk') {
                                $query = 'SELECT title,' . implode(",", $emailFields) . ',vtiger_troubletickets.ticketid as id
				  FROM vtiger_troubletickets
				  INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_troubletickets.ticketid
				  LEFT JOIN vtiger_ticketcf ON vtiger_ticketcf.ticketid = vtiger_troubletickets.ticketid
				  WHERE vtiger_crmentity.deleted=0 AND vtiger_troubletickets.ticketid IN (' . generateQuestionMarks($idlist) . ')';
                            } else {
                                // vendors
                                $query = 'SELECT vtiger_vendor.vendorname, ' . implode(",", $emailFields) . ',vtiger_vendor.vendorid as id FROM vtiger_vendor
				   INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_vendor.vendorid
				   LEFT JOIN vtiger_vendorcf ON vtiger_vendorcf.vendorid= vtiger_vendor.vendorid
				   WHERE vtiger_crmentity.deleted=0 AND vtiger_vendor.vendorid IN (' . generateQuestionMarks($idlist) . ')';
                            }
                        }
                    }
                }
            }
        }
    }
    $result = $adb->pquery($query, $idlist);
    if ($adb->num_rows($result) > 0) {
        while ($entityvalue = $adb->fetchByAssoc($result)) {
            $vtwsid = $entityvalue['id'];
            foreach ($emailFields as $i => $emailFieldName) {
                if ($entityvalue[$emailFieldName] != NULL || $entityvalue[$emailFieldName] != '') {
                    $idlists .= $vtwsid . '@' . $vtwsCRMObjectMeta->getFieldIdFromFieldName($emailFieldName) . '|';
                    if ($module == 'Leads' || $module == 'Contacts') {
                        $mailids .= $entityvalue['lastname'] . " " . $entityvalue['firstname'] . "<" . $entityvalue[$emailFieldName] . ">,";
                    } else {
                        if ($module == "Project") {
                            $mailids .= $entityvalue['projectname'] . "<" . $entityvalue[$emailFieldName] . ">,";
                        } else {
                            if ($module == "ProjectTask") {
                                $mailids .= $entityvalue['projecttaskname'] . "<" . $entityvalue[$emailFieldName] . ">,";
                            } else {
                                if ($module == "Potentials") {
                                    $mailids .= $entityvalue['potentialname'] . "<" . $entityvalue[$emailFieldName] . ">,";
                                } else {
                                    if ($module == "HelpDesk") {
                                        $mailids .= $entityvalue['title'] . "<" . $entityvalue[$emailFieldName] . ">,";
                                    } else {
                                        $mailids .= $entityvalue['accountname'] . "<" . $entityvalue[$emailFieldName] . ">,";
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    $return_data = array('idlists' => $idlists, 'mailds' => $mailids);
    return $return_data;
}