function getTeamSetIdForTeams($teamIds)
 {
     if (!is_array($teamIds)) {
         $teamIds = array($teamIds);
     }
     // if
     $teamSet = new TeamSet();
     $team_set_id = $teamSet->addTeams($teamIds);
     return $team_set_id;
 }
Beispiel #2
0
/**
 * upgradeModulesForTeamsets
 *
 * This method adds the team_set_id values to the module tables that have the new team_set_id column
 * added through the SugarCRM 5.5.x upgrade process.  It also adds the values into the team_sets and
 * team_sets_teams tables.
 *
 * @param filter Array of modules to process; empty by default
 */
function upgradeModulesForTeamsets($filter = array())
{
    require 'include/modules.php';
    foreach ($beanList as $moduleName => $beanName) {
        if (!empty($filter) && array_search($moduleName, $filter) === false) {
            continue;
        }
        if ($moduleName == 'TeamMemberships' || $moduleName == 'ForecastOpportunities') {
            continue;
        }
        $bean = loadBean($moduleName);
        if (empty($bean) || empty($bean->table_name)) {
            continue;
        }
        $FieldArray = $GLOBALS['db']->helper->get_columns($bean->table_name);
        if (!isset($FieldArray['team_id'])) {
            continue;
        }
        upgradeTeamColumn($bean, 'team_id');
    }
    //foreach
    //Upgrade users table
    $bean = loadBean('Users');
    upgradeTeamColumn($bean, 'default_team');
    $result = $GLOBALS['db']->query("SELECT id FROM teams where deleted=0");
    while ($row = $GLOBALS['db']->fetchByAssoc($result)) {
        $teamset = new TeamSet();
        $teamset->addTeams($row['id']);
    }
}
Beispiel #3
0
/**
 * generate_team_set
 * Helper function to recursively create team sets
 * @param $primary The primary team
 * @param $teams The teams to use
 */
function generate_team_set($primary, $teams)
{
    if (!in_array($primary, $teams)) {
        array_push($teams, $primary);
    }
    $teams = array_reverse($teams);
    $team_count = count($teams);
    for ($i = 0; $i < $team_count; $i++) {
        $teamset = new TeamSet();
        $teamset->addTeams($teams);
        array_pop($teams);
    }
}
Beispiel #4
0
 public function run()
 {
     if (!($this->from_flavor == 'ce' && $this->toFlavor('pro'))) {
         return;
     }
     require_once 'ModuleInstall/ModuleInstaller.php';
     $globalteam = new Team();
     $globalteam->retrieve('1');
     if (empty($globalteam->name)) {
         $globalteam->create_team("Global", $this->mod_strings['LBL_GLOBAL_TEAM_DESC'], $globalteam->global_team);
     }
     $this->log("Start Building private teams");
     $result = $this->db->query("SELECT id, user_name, first_name, last_name FROM users where deleted=0");
     while ($row = $this->db->fetchByAssoc($result)) {
         $results2 = $this->db->query("SELECT id FROM teams WHERE name = '({$row['user_name']})'");
         $assoc = '';
         if (!($assoc = $this->db->fetchByAssoc($results2))) {
             //if team does not exist, then lets create the team for this user
             $team = new Team();
             $user = new User();
             $user->retrieve($row['id']);
             $team->new_user_created($user);
             $team_id = $team->id;
         } else {
             $team_id = $assoc['id'];
         }
         //upgrade the team
         $name = is_null($row['first_name']) ? '' : $row['first_name'];
         $name_2 = is_null($row['last_name']) ? '' : $row['last_name'];
         $associated_user_id = $row['id'];
         //Bug 32914
         //Ensure team->name is not empty by using team->name_2 if available
         if (empty($name) && !empty($name_2)) {
             $name = $name_2;
             $name_2 = '';
         }
         $this->db->query("UPDATE teams SET name = '{$name}', name_2 = '{$name_2}', associated_user_id = '{$associated_user_id}' WHERE id = '{$team_id}'");
     }
     //while
     $this->db->query("update users set team_set_id = (select teams.id from teams where teams.associated_user_id = users.id)");
     $this->db->query("update users set default_team = (select teams.id from teams where teams.associated_user_id = users.id)");
     $this->log("Done Building private teams");
     $this->log("Start Building the team_set and team_sets_teams");
     require 'include/modules.php';
     foreach ($beanList as $moduleName => $beanName) {
         if ($moduleName == 'TeamMemberships' || $moduleName == 'ForecastOpportunities') {
             continue;
         }
         $bean = loadBean($moduleName);
         if (empty($bean) || empty($bean->table_name)) {
             continue;
         }
         $FieldArray = $this->db->get_columns($bean->table_name);
         if (!isset($FieldArray['team_id'])) {
             continue;
         }
         $this->upgradeTeamColumn($bean, 'team_id');
     }
     //foreach
     //Upgrade users table
     $bean = BeanFactory::getBean('Users');
     $this->upgradeTeamColumn($bean, 'default_team');
     $result = $this->db->query("SELECT id FROM teams where deleted=0");
     while ($row = $this->db->fetchByAssoc($result)) {
         $teamset = new TeamSet();
         $teamset->addTeams($row['id']);
     }
     $this->log("Finish Building the team_set and team_sets_teams");
     $this->log("Start modules/Administration/upgradeTeams.php");
     ob_start();
     include 'modules/Administration/upgradeTeams.php';
     ob_end_clean();
     $this->log("Finish modules/Administration/upgradeTeams.php");
 }
function add_team_to_team_set($team_set_id, $user_id){
    if(!isset($GLOBALS['user_team_checked'][$user_id][$team_set_id])){
        $result = $GLOBALS['db']->query("SELECT default_team FROM users WHERE id=$user_id");
        while($row = $GLOBALS['db']->fetchByAssoc($result)){
            $teamset = new TeamSet();
            $teams = $teamset->getTeamIds($team_set_id);
            $teams[] = $row['default_team'];
            $team_set_id = $teamset->addTeams($teams);
            $GLOBALS['user_team_checked'][$user_id][$team_set_id] = true;
        }
    }
    return $team_set_id;
}    
Beispiel #6
0
 /**
  * Sends Email for Email 2.0
  */
 function email2Send($request)
 {
     global $current_user;
     global $timedate;
     $saveAsDraft = !empty($request['saveDraft']);
     if (!$saveAsDraft && !empty($request["MAIL_RECORD_STATUS"]) && $request["MAIL_RECORD_STATUS"] == 'archived') {
         $archived = true;
         $this->type = 'archived';
     } else {
         $archived = false;
         if (!empty($request['MAIL_RECORD_STATUS']) && $request['MAIL_RECORD_STATUS'] === 'ready') {
             $this->type = 'out';
         }
     }
     /**********************************************************************
      * Sugar Email PREP
      */
     /* preset GUID */
     $orignialId = "";
     if (!empty($this->id)) {
         $orignialId = $this->id;
     }
     // if
     if (empty($this->id)) {
         $this->id = create_guid();
         $this->new_with_id = true;
     }
     /* satisfy basic HTML email requirements */
     $this->name = $request['sendSubject'];
     if (isset($_REQUEST['setEditor']) && $_REQUEST['setEditor'] == 1) {
         $_REQUEST['description_html'] = $_REQUEST['sendDescription'];
         $this->description_html = $_REQUEST['description_html'];
     } else {
         $this->description_html = '';
         $this->description = $_REQUEST['sendDescription'];
     }
     if ($this->isDraftEmail($request)) {
         if ($this->type != 'draft' && $this->status != 'draft') {
             $this->id = create_guid();
             $this->new_with_id = true;
             $this->date_entered = "";
         }
         // if
         $q1 = "update emails_email_addr_rel set deleted = 1 WHERE email_id = '{$this->id}'";
         $this->db->query($q1);
     }
     // if
     if ($saveAsDraft) {
         $this->type = 'draft';
         $this->status = 'draft';
     } else {
         if ($archived) {
             $this->type = 'archived';
             $this->status = 'archived';
         }
         /* Apply Email Templates */
         // do not parse email templates if the email is being saved as draft....
         $toAddresses = $this->email2ParseAddresses($_REQUEST['sendTo']);
         $sea = BeanFactory::getBean('EmailAddresses');
         $object_arr = array();
         if (!empty($_REQUEST['parent_type']) && !empty($_REQUEST['parent_id']) && ($_REQUEST['parent_type'] == 'Accounts' || $_REQUEST['parent_type'] == 'Contacts' || $_REQUEST['parent_type'] == 'Leads' || $_REQUEST['parent_type'] == 'Users' || $_REQUEST['parent_type'] == 'Prospects')) {
             $bean = BeanFactory::getBean($_REQUEST['parent_type'], $_REQUEST['parent_id']);
             if (!empty($bean->id)) {
                 $object_arr[$bean->module_dir] = $bean->id;
             }
         }
         foreach ($toAddresses as $addrMeta) {
             $addr = $addrMeta['email'];
             $beans = $sea->getBeansByEmailAddress($addr);
             foreach ($beans as $bean) {
                 if (!isset($object_arr[$bean->module_dir])) {
                     $object_arr[$bean->module_dir] = $bean->id;
                 }
             }
         }
         /* template parsing */
         if (empty($object_arr)) {
             $object_arr = array('Contacts' => '123');
         }
         $object_arr['Users'] = $current_user->id;
         $this->description_html = EmailTemplate::parse_template($this->description_html, $object_arr);
         $this->name = EmailTemplate::parse_template($this->name, $object_arr);
         $this->description = EmailTemplate::parse_template($this->description, $object_arr);
         $this->description = html_entity_decode($this->description, ENT_COMPAT, 'UTF-8');
         if ($this->type != 'draft' && $this->status != 'draft' && $this->type != 'archived' && $this->status != 'archived') {
             $this->id = create_guid();
             $this->date_entered = "";
             $this->new_with_id = true;
             $this->type = 'out';
             $this->status = 'sent';
         }
     }
     if (isset($_REQUEST['parent_type']) && empty($_REQUEST['parent_type']) && isset($_REQUEST['parent_id']) && empty($_REQUEST['parent_id'])) {
         $this->parent_id = "";
         $this->parent_type = "";
     }
     // if
     $forceSave = false;
     $subject = $this->name;
     $textBody = from_html($this->description);
     $htmlBody = from_html($this->description_html);
     //------------------- HANDLEBODY() ---------------------------------------------
     if (isset($_REQUEST['setEditor']) && $_REQUEST['setEditor'] == 1 && trim($_REQUEST['description_html']) != '' || trim($this->description_html) != '' && $current_user->getPreference('email_editor_option', 'global') !== 'plain') {
         $textBody = strip_tags(br2nl($htmlBody));
     } else {
         // plain-text only
         $textBody = str_replace("&nbsp;", " ", $textBody);
         $textBody = str_replace("</p>", "</p><br />", $textBody);
         $textBody = strip_tags(br2nl($textBody));
         $textBody = html_entity_decode($textBody, ENT_QUOTES, 'UTF-8');
         $this->description_html = "";
         // make sure it's blank to avoid any mishaps
         $htmlBody = $this->description_html;
     }
     $textBody = $this->decodeDuringSend($textBody);
     $htmlBody = $this->decodeDuringSend($htmlBody);
     $this->description = $textBody;
     $this->description_html = $htmlBody;
     $mailConfig = null;
     try {
         if (isset($request["fromAccount"]) && !empty($request["fromAccount"])) {
             $mailConfig = OutboundEmailConfigurationPeer::getMailConfigurationFromId($current_user, $request["fromAccount"]);
         } else {
             $mailConfig = OutboundEmailConfigurationPeer::getSystemMailConfiguration($current_user);
         }
     } catch (Exception $e) {
         if (!$saveAsDraft && !$archived) {
             throw $e;
         }
     }
     if (!$saveAsDraft && !$archived && is_null($mailConfig)) {
         throw new MailerException("No Valid Mail Configurations Found", MailerException::InvalidConfiguration);
     }
     try {
         $mailer = null;
         if (!$saveAsDraft && !$archived) {
             $mailerFactoryClass = $this->MockMailerFactoryClass;
             $mailer = $mailerFactoryClass::getMailer($mailConfig);
             $mailer->setSubject($subject);
             $mailer->setHtmlBody($htmlBody);
             $mailer->setTextBody($textBody);
             $replyTo = $mailConfig->getReplyTo();
             if (!empty($replyTo)) {
                 $replyToEmail = $replyTo->getEmail();
                 if (!empty($replyToEmail)) {
                     $mailer->setHeader(EmailHeaders::ReplyTo, new EmailIdentity($replyToEmail, $replyTo->getName()));
                 }
             }
         }
         if (!is_null($mailer)) {
             // Any individual Email Address that is not valid will be logged and skipped
             // If all email addresses in the request are skipped, an error "No Recipients" is reported for the request
             foreach ($this->email2ParseAddresses($request['sendTo']) as $addr_arr) {
                 try {
                     $mailer->addRecipientsTo(new EmailIdentity($addr_arr['email'], $addr_arr['display']));
                 } catch (MailerException $me) {
                     // Invalid Email Address - Log it and Skip
                     $GLOBALS["log"]->warning($me->getLogMessage());
                 }
             }
             foreach ($this->email2ParseAddresses($request['sendCc']) as $addr_arr) {
                 try {
                     $mailer->addRecipientsCc(new EmailIdentity($addr_arr['email'], $addr_arr['display']));
                 } catch (MailerException $me) {
                     // Invalid Email Address - Log it and Skip
                     $GLOBALS["log"]->warning($me->getLogMessage());
                 }
             }
             foreach ($this->email2ParseAddresses($request['sendBcc']) as $addr_arr) {
                 try {
                     $mailer->addRecipientsBcc(new EmailIdentity($addr_arr['email'], $addr_arr['display']));
                 } catch (MailerException $me) {
                     // Invalid Email Address - Log it and Skip
                     $GLOBALS["log"]->warning($me->getLogMessage());
                 }
             }
         }
         /* handle attachments */
         if (!empty($request['attachments'])) {
             $exAttachments = explode("::", $request['attachments']);
             foreach ($exAttachments as $file) {
                 $file = trim(from_html($file));
                 $file = str_replace("\\", "", $file);
                 if (!empty($file)) {
                     $fileGUID = preg_replace('/[^a-z0-9\\-]/', "", substr($file, 0, 36));
                     $fileLocation = $this->et->userCacheDir . "/{$fileGUID}";
                     $filename = substr($file, 36, strlen($file));
                     // strip GUID	for PHPMailer class to name outbound file
                     // only save attachments if we're archiving or drafting
                     if ($this->type == 'draft' && !empty($this->id) || isset($request['saveToSugar']) && $request['saveToSugar'] == 1) {
                         $note = new Note();
                         $note->id = create_guid();
                         $note->new_with_id = true;
                         // duplicating the note with files
                         $note->parent_id = $this->id;
                         $note->parent_type = $this->module_dir;
                         $note->name = $filename;
                         $note->filename = $filename;
                         $note->file_mime_type = $this->email2GetMime($fileLocation);
                         $note->team_id = isset($_REQUEST['primaryteam']) ? $_REQUEST['primaryteam'] : $current_user->getPrivateTeamID();
                         $noteTeamSet = new TeamSet();
                         $noteteamIdsArray = isset($_REQUEST['teamIds']) ? explode(",", $_REQUEST['teamIds']) : array($current_user->getPrivateTeamID());
                         $note->team_set_id = $noteTeamSet->addTeams($noteteamIdsArray);
                         $dest = "upload://{$note->id}";
                         if (!file_exists($fileLocation) || !copy($fileLocation, $dest)) {
                             $GLOBALS['log']->debug("EMAIL 2.0: could not copy attachment file to {$fileLocation} => {$dest}");
                         } else {
                             $note->save();
                             $validNote = true;
                         }
                     } else {
                         $note = new Note();
                         $validNote = (bool) $note->retrieve($fileGUID);
                     }
                     if (isset($validNote) && $validNote === true) {
                         $attachment = AttachmentPeer::attachmentFromSugarBean($note);
                         if (!is_null($mailer)) {
                             $mailer->addAttachment($attachment);
                         }
                     }
                 }
             }
         }
         /* handle sugar documents */
         if (!empty($request['documents'])) {
             $exDocs = explode("::", $request['documents']);
             foreach ($exDocs as $docId) {
                 $docId = trim($docId);
                 if (!empty($docId)) {
                     $doc = new Document();
                     $doc->retrieve($docId);
                     if (empty($doc->id) || $doc->id != $docId) {
                         throw new Exception("Document Not Found: Id='" . $request['documents'] . "'");
                     }
                     $documentRevision = new DocumentRevision();
                     $documentRevision->retrieve($doc->document_revision_id);
                     //$documentRevision->x_file_name   = $documentRevision->filename;
                     //$documentRevision->x_file_path   = "upload/{$documentRevision->id}";
                     //$documentRevision->x_file_exists = (bool) file_exists($documentRevision->x_file_path);
                     //$documentRevision->x_mime_type   = $documentRevision->file_mime_type;
                     $filename = $documentRevision->filename;
                     $docGUID = preg_replace('/[^a-z0-9\\-]/', "", $documentRevision->id);
                     $fileLocation = "upload://{$docGUID}";
                     if (empty($documentRevision->id) || !file_exists($fileLocation)) {
                         throw new Exception("Document Revision Id Not Found");
                     }
                     // only save attachments if we're archiving or drafting
                     if ($this->type == 'draft' && !empty($this->id) || isset($request['saveToSugar']) && $request['saveToSugar'] == 1) {
                         $note = new Note();
                         $note->id = create_guid();
                         $note->new_with_id = true;
                         // duplicating the note with files
                         $note->parent_id = $this->id;
                         $note->parent_type = $this->module_dir;
                         $note->name = $filename;
                         $note->filename = $filename;
                         $note->file_mime_type = $documentRevision->file_mime_type;
                         $note->team_id = $this->team_id;
                         $note->team_set_id = $this->team_set_id;
                         $dest = "upload://{$note->id}";
                         if (!file_exists($fileLocation) || !copy($fileLocation, $dest)) {
                             $GLOBALS['log']->debug("EMAIL 2.0: could not copy SugarDocument revision file {$fileLocation} => {$dest}");
                         }
                         $note->save();
                     }
                     $attachment = AttachmentPeer::attachmentFromSugarBean($documentRevision);
                     //print_r($attachment);
                     if (!is_null($mailer)) {
                         $mailer->addAttachment($attachment);
                     }
                 }
             }
         }
         /* handle template attachments */
         if (!empty($request['templateAttachments'])) {
             $exNotes = explode("::", $request['templateAttachments']);
             foreach ($exNotes as $noteId) {
                 $noteId = trim($noteId);
                 if (!empty($noteId)) {
                     $note = new Note();
                     $note->retrieve($noteId);
                     if (!empty($note->id)) {
                         $filename = $note->filename;
                         $noteGUID = preg_replace('/[^a-z0-9\\-]/', "", $note->id);
                         $fileLocation = "upload://{$noteGUID}";
                         $mime_type = $note->file_mime_type;
                         if (!$note->embed_flag) {
                             $attachment = AttachmentPeer::attachmentFromSugarBean($note);
                             //print_r($attachment);
                             if (!is_null($mailer)) {
                                 $mailer->addAttachment($attachment);
                             }
                             // only save attachments if we're archiving or drafting
                             if ($this->type == 'draft' && !empty($this->id) || isset($request['saveToSugar']) && $request['saveToSugar'] == 1) {
                                 if ($note->parent_id != $this->id) {
                                     $this->saveTempNoteAttachments($filename, $fileLocation, $mime_type);
                                 }
                             }
                             // if
                         }
                         // if
                     } else {
                         $fileGUID = preg_replace('/[^a-z0-9\\-]/', "", substr($noteId, 0, 36));
                         $fileLocation = $this->et->userCacheDir . "/{$fileGUID}";
                         $filename = substr($noteId, 36, strlen($noteId));
                         // strip GUID	for PHPMailer class to name outbound file
                         $mimeType = $this->email2GetMime($fileLocation);
                         $note = $this->saveTempNoteAttachments($filename, $fileLocation, $mimeType);
                         $attachment = AttachmentPeer::attachmentFromSugarBean($note);
                         //print_r($attachment);
                         if (!is_null($mailer)) {
                             $mailer->addAttachment($attachment);
                         }
                     }
                 }
             }
         }
         /**********************************************************************
          * Final Touches
          */
         if ($this->type == 'draft' && !$saveAsDraft) {
             // sending a draft email
             $this->type = 'out';
             $this->status = 'sent';
             $forceSave = true;
         } elseif ($saveAsDraft) {
             $this->type = 'draft';
             $this->status = 'draft';
             $forceSave = true;
         }
         if (!is_null($mailer)) {
             $mailer->send();
         }
     } catch (MailerException $me) {
         $GLOBALS["log"]->error($me->getLogMessage());
         throw $me;
     } catch (Exception $e) {
         // eat the phpmailerException but use it's message to provide context for the failure
         $me = new MailerException("Email2Send Failed: " . $e->getMessage(), MailerException::FailedToSend);
         $GLOBALS["log"]->error($me->getLogMessage());
         $GLOBALS["log"]->info($me->getTraceMessage());
         if (!empty($mailConfig)) {
             $GLOBALS["log"]->info($mailConfig->toArray(), true);
         }
         throw $me;
     }
     if (!(empty($orignialId) || $saveAsDraft || $this->type == 'draft' && $this->status == 'draft') && ($_REQUEST['composeType'] == 'reply' || $_REQUEST['composeType'] == 'replyAll' || $_REQUEST['composeType'] == 'replyCase') && $orignialId != $this->id) {
         $originalEmail = BeanFactory::getBean('Emails', $orignialId);
         $originalEmail->reply_to_status = 1;
         $originalEmail->save();
         $this->reply_to_status = 0;
     }
     // if
     if (isset($_REQUEST['composeType']) && ($_REQUEST['composeType'] == 'reply' || $_REQUEST['composeType'] == 'replyCase')) {
         if (isset($_REQUEST['ieId']) && isset($_REQUEST['mbox'])) {
             $emailFromIe = BeanFactory::getBean('InboundEmail', $_REQUEST['ieId'], array('disable_row_level_security' => true));
             $emailFromIe->mailbox = $_REQUEST['mbox'];
             if (isset($emailFromIe->id) && $emailFromIe->is_personal) {
                 if ($emailFromIe->isPop3Protocol()) {
                     $emailFromIe->mark_answered($this->uid, 'pop3');
                 } elseif ($emailFromIe->connectMailserver() == 'true') {
                     $emailFromIe->markEmails($this->uid, 'answered');
                     $emailFromIe->mark_answered($this->uid);
                 }
             }
         }
     }
     if ($forceSave || $this->type == 'draft' || $this->type == 'archived' || isset($request['saveToSugar']) && $request['saveToSugar'] == 1) {
         // Set Up From Name and Address Information
         if ($this->type == 'archived') {
             $this->from_addr = empty($request['archive_from_address']) ? '' : $request['archive_from_address'];
         } elseif (!empty($mailConfig)) {
             $sender = $mailConfig->getFrom();
             $decodedFromName = mb_decode_mimeheader($sender->getName());
             $this->from_addr = "{$decodedFromName} <" . $sender->getEmail() . ">";
         } else {
             $ret = $current_user->getUsersNameAndEmail();
             if (empty($ret['email'])) {
                 $systemReturn = $current_user->getSystemDefaultNameAndEmail();
                 $ret['email'] = $systemReturn['email'];
                 $ret['name'] = $systemReturn['name'];
             }
             $decodedFromName = mb_decode_mimeheader($ret['name']);
             $this->from_addr = "{$decodedFromName} <" . $ret['email'] . ">";
         }
         $this->from_addr_name = $this->from_addr;
         $this->to_addrs = $_REQUEST['sendTo'];
         $this->to_addrs_names = $_REQUEST['sendTo'];
         $this->cc_addrs = $_REQUEST['sendCc'];
         $this->cc_addrs_names = $_REQUEST['sendCc'];
         $this->bcc_addrs = $_REQUEST['sendBcc'];
         $this->bcc_addrs_names = $_REQUEST['sendBcc'];
         $this->team_id = isset($_REQUEST['primaryteam']) ? $_REQUEST['primaryteam'] : $current_user->getPrivateTeamID();
         $teamSet = BeanFactory::getBean('TeamSets');
         $teamIdsArray = isset($_REQUEST['teamIds']) ? explode(",", $_REQUEST['teamIds']) : array($current_user->getPrivateTeamID());
         $this->team_set_id = $teamSet->addTeams($teamIdsArray);
         $this->assigned_user_id = $current_user->id;
         $this->date_sent = $timedate->now();
         ///////////////////////////////////////////////////////////////////
         ////	LINK EMAIL TO SUGARBEANS BASED ON EMAIL ADDY
         if (!empty($_REQUEST['parent_type']) && !empty($_REQUEST['parent_id'])) {
             $this->parent_id = $this->db->quote($_REQUEST['parent_id']);
             $this->parent_type = $this->db->quote($_REQUEST['parent_type']);
             $a = $this->db->fetchOne("SELECT count(*) c FROM emails_beans WHERE  email_id = '{$this->id}' AND bean_id = '{$this->parent_id}' AND bean_module = '{$this->parent_type}'");
             if ($a['c'] == 0) {
                 $bean = BeanFactory::getBean($_REQUEST['parent_type'], $_REQUEST['parent_id']);
                 if (!empty($bean)) {
                     if (!empty($bean->field_defs['emails']['type']) && $bean->field_defs['emails']['type'] == 'link') {
                         $email_link = "emails";
                     } else {
                         $email_link = $this->findEmailsLink($bean);
                     }
                     if ($email_link && $bean->load_relationship($email_link)) {
                         $bean->{$email_link}->add($this);
                     }
                 }
             }
             // if
         } else {
             $c = BeanFactory::getBean('Cases');
             if ($caseId = InboundEmail::getCaseIdFromCaseNumber($subject, $c)) {
                 $c->retrieve($caseId);
                 $c->load_relationship('emails');
                 $c->emails->add($this->id);
                 $this->parent_type = "Cases";
                 $this->parent_id = $caseId;
             }
             // if
         }
         // else
         ////	LINK EMAIL TO SUGARBEANS BASED ON EMAIL ADDY
         ///////////////////////////////////////////////////////////////////
         $this->save();
     }
     /**** --------------------------------- ?????????
     		if(!empty($request['fromAccount'])) {
                 $ie = new InboundEmail();
                 $ie->retrieve($request['fromAccount']);
     			if (isset($ie->id) && !$ie->isPop3Protocol() && $mail->oe->mail_smtptype != 'gmail') {
     				$sentFolder = $ie->get_stored_options("sentFolder");
     				if (!empty($sentFolder)) {
     					$data = $mail->CreateHeader() . "\r\n" . $mail->CreateBody() . "\r\n";
     					$ie->mailbox = $sentFolder;
     					if ($ie->connectMailserver() == 'true') {
     						$connectString = $ie->getConnectString($ie->getServiceString(), $ie->mailbox);
     						$returnData = imap_append($ie->conn,$connectString, $data, "\\Seen");
     						if (!$returnData) {
     							$GLOBALS['log']->debug("could not copy email to {$ie->mailbox} for {$ie->name}");
     						} // if
     					} else {
     						$GLOBALS['log']->debug("could not connect to mail serve for folder {$ie->mailbox} for {$ie->name}");
     					} // else
     				} else {
     					$GLOBALS['log']->debug("could not copy email to {$ie->mailbox} sent folder as its empty");
     				} // else
     			} // if
     		} // if
             ------------------------------------- ****/
     return true;
 }