/** 
  * eventSendDiscussMessageByEmail
  * This will send an email to all the project participant 
  * Using Radria_Emailer and an email template stored in the emailtemplate table.
  * This method uses the currently open project persistant object. 
  * @param Eventcontroler
  */
 function eventSendDiscussMessageByEmail(EventControler $event_controler)
 {
     $this->setLog("\n eventSendDiscussMessageByEmail: starting (" . date("Y/m/d H:i:s"));
     try {
         $co_workers = $_SESSION["do_project"]->getProjectCoWorkers();
         // print_r($co_workers);exit;
         if ($co_workers !== false) {
             $email_template = new EmailTemplate("ofuz_project_discussion");
             $email_nudge = new EmailTemplate("ofuz_project_discussion_nudge");
             $project_task_url = $GLOBALS['cfg_ofuz_site_http_base'] . 'Task/' . $_SESSION['do_project_task']->idproject_task;
             $project_link = $GLOBALS['cfg_ofuz_site_http_base'] . 'Project/' . $_SESSION['do_project']->idproject;
             if ($event_controler->fields['document'] != '') {
                 // If a file is attached
                 $doc_link = $GLOBALS['cfg_ofuz_site_http_base'] . 'files/' . $event_controler->fields['document'];
                 $doc_text = '<br />' . _('Attachment') . ': <a href="' . $doc_link . '"> ' . $event_controler->fields['document'] . '</a>';
             } else {
                 $doc_text = '';
             }
             $message_html = nl2br(stripslashes($event_controler->fields['discuss'] . $doc_text));
             $message_txt = stripslashes($event_controler->fields['discuss'] . $doc_text);
             $email_data = array('project-name' => $_SESSION['do_project']->name, 'project-link' => $project_link, 'discussion-owner' => $_SESSION['do_User']->getFullName(), 'task_name' => $_SESSION['do_project_task']->task_description, 'task_category' => $_SESSION['do_project_task']->task_category, 'message_txt' => $message_txt, 'message_html' => $message_html, 'project-task-link' => $project_task_url);
             $do_discussion_email_setting = new DiscussionEmailSetting();
             if ($_SESSION['do_project_task']->drop_box_code < 10 || count($co_workers) > 19) {
                 foreach ($co_workers as $co_worker) {
                     $global_email_alert = $_SESSION['UserSettings']->getSettingValue('task_discussion_alert', $co_worker['idcoworker']);
                     $global_email_alert_on = true;
                     if (!$global_email_alert) {
                         $global_email_alert_on = true;
                         // means user has not set on/off in the settings and on by default
                     } else {
                         if (is_array($global_email_alert)) {
                             if ($global_email_alert["setting_value"] == 'Yes') {
                                 $global_email_alert_on = true;
                             } else {
                                 $global_email_alert_on = false;
                             }
                         }
                     }
                     //if($_SESSION['UserSettings']->global_task_discussion_alert  != 'No' && !$do_discussion_email_setting->isDiscussionAlertSet($_SESSION['do_project_task']->idproject,'Project',$co_worker['idcoworker']) ){
                     if ($global_email_alert_on && !$do_discussion_email_setting->isDiscussionAlertSet($_SESSION['do_project_task']->idproject_task, 'Project', $co_worker['idcoworker'])) {
                         $emailer = new Radria_Emailer('UTF-8');
                         $emailer->setEmailTemplate($email_template);
                         //$emailer->cleanup();
                         //$emailer->setFrom('*****@*****.**' , 'Ofuz.net');
                         $email_data['firstname'] = $co_worker['firstname'];
                         $emailer->mergeArray($email_data);
                         $this->setLog("\n Sending email to:" . $co_worker['email']);
                         $emailer->addTo($co_worker['email']);
                         $emailer->send();
                     }
                 }
             } else {
                 $co_workers_nudged = array();
                 $co_workers_nudged_emails = array();
                 foreach ($co_workers as $co_worker) {
                     if (preg_match("/@" . $co_worker['firstname'] . "/i", $event_controler->fields['discuss']) || preg_match("/@" . $co_worker['lastname'] . "/i", $event_controler->fields['discuss'])) {
                         //$co_workers_nudged[] = $co_worker;
                         $co_workers_nudged_emails[] = $co_worker['email'];
                         $this->setLog("\n Is nudged:" . $co_worker['firstname'] . " " . $co_worker['lastname'] . " " . $co_worker['email']);
                     }
                 }
                 $emailer = new Radria_Emailer('UTF-8');
                 $email_template->setSenderName($_SESSION['do_User']->getFullName());
                 $email_template->setSenderEmail($_SESSION['do_User']->email);
                 $emailer->setEmailTemplate($email_template);
                 $emailer->addCc($_SESSION['do_project_task']->getDropBoxEmail());
                 $emailer->addHeader("X-ofuz-emailer", $_SESSION['do_User']->iduser);
                 $emailer->mergeArray($email_data);
                 $this->setLog("\n Sending email to:" . $co_workers[0]['email']);
                 $someone_note_nudged = false;
                 foreach ($co_workers as $to_worker) {
                     /*
                         Check if the Co-Worker is supposed to get the email
                     */
                     $global_email_alert = $_SESSION['UserSettings']->getSettingValue('task_discussion_alert', $to_worker['idcoworker']);
                     $global_email_alert_on = true;
                     if (!$global_email_alert) {
                         $global_email_alert_on = true;
                         //means user has not set on/off in the settings and on by default
                     } else {
                         if (is_array($global_email_alert)) {
                             if ($global_email_alert["setting_value"] == 'Yes') {
                                 $global_email_alert_on = true;
                             } else {
                                 $global_email_alert_on = false;
                             }
                         }
                     }
                     /* if($_SESSION['UserSettings']->global_task_discussion_alert  != 'No' 
                        && !$do_discussion_email_setting->isDiscussionAlertSet($_SESSION['do_project_task']->idproject_task,$to_worker['idcoworker']) 
                        && !in_array($to_worker['email'], $co_workers_nudged_emails)){*/
                     if ($global_email_alert_on && !$do_discussion_email_setting->isDiscussionAlertSet($_SESSION['do_project_task']->idproject, 'Project', $to_worker['idcoworker']) && !in_array($to_worker['email'], $co_workers_nudged_emails)) {
                         $email_to = $to_worker['email'];
                         $emailer->addTo($email_to);
                         $someone_note_nudged = true;
                         break;
                     }
                 }
                 //$emailer->addTo($co_workers[0]['email']);
                 foreach ($co_workers as $cc_worker) {
                     /*
                     Check if the Co-Worker is supposed to get the email
                     */
                     $global_email_alert_on = true;
                     $global_email_alert = $_SESSION['UserSettings']->getSettingValue('task_discussion_alert', $cc_worker['idcoworker']);
                     if (!$global_email_alert) {
                         $global_email_alert_on = true;
                         //means user has not set on/off in the settings and on by default
                     } else {
                         if (is_array($global_email_alert)) {
                             if ($global_email_alert["setting_value"] == 'Yes') {
                                 $global_email_alert_on = true;
                             } else {
                                 $global_email_alert_on = false;
                             }
                         }
                     }
                     if ($email_to != $cc_worker['email']) {
                         /*  if($_SESSION['UserSettings']->global_task_discussion_alert  != 'No' 
                             && !$do_discussion_email_setting->isDiscussionAlertSet($_SESSION['do_project_task']->idproject_task,$cc_worker['idcoworker']) 
                             && !in_array($cc_worker['email'], $co_workers_nudged_emails)){*/
                         if ($global_email_alert_on && !$do_discussion_email_setting->isDiscussionAlertSet($_SESSION['do_project_task']->idproject, 'Project', $cc_worker['idcoworker']) && !in_array($cc_worker['email'], $co_workers_nudged_emails)) {
                             $emailer->addCc($cc_worker['email']);
                             $someone_note_nudged = true;
                         }
                     }
                 }
                 if ($someone_note_nudged) {
                     $emailer->send();
                 }
                 if (count($co_workers_nudged_emails) > 0) {
                     $email_nudge->setSenderName($_SESSION['do_User']->getFullName());
                     $email_nudge->setSenderEmail($_SESSION['do_User']->email);
                     $emailer_nudge = new Radria_Emailer();
                     $emailer_nudge->setEmailTemplate($email_nudge);
                     $emailer_nudge->addCc($_SESSION['do_project_task']->getDropBoxEmail());
                     $emailer_nudge->addHeader("X-ofuz-emailer", $_SESSION['do_User']->iduser);
                     $emailer_nudge->mergeArray($email_data);
                     $emailer_nudge->addTo($co_workers_nudged_emails[0]);
                     $this->setLog("\n Sending Nudge to:" . $co_workers_nudged_emails[0]);
                     for ($i = 1; $i < count($co_workers_nudged_emails); $i++) {
                         $this->setLog("\n Sending Nudge to:" . $co_workers_nudged_emails[$i]);
                         $emailer_nudge->addCc($co_workers_nudged_emails[$i]);
                     }
                     $emailer_nudge->send();
                     $emailer_nudge->cleanup();
                 }
                 $emailer->cleanup();
             }
         }
     } catch (Exception $e) {
         $this->setError('ProjectDiscuss Could not send email: ' . $e->getMessage());
     }
     $this->setLog("\n eventSendDiscussMessageByEmail: ending (" . date("Y/m/d H:i:s"));
 }