/**
  * Send multiple emails using this simple tool
  *
  * @param void
  * @return null
  */
 function tool_mass_mailer()
 {
     $tool = AdministrationTools::getByName('mass_mailer');
     if (!$tool instanceof AdministrationTool) {
         flash_error(lang('administration tool dnx', 'test_mail_settings'));
         $this->redirectTo('administration', 'tools');
     }
     // if
     $massmailer_data = array_var($_POST, 'massmailer');
     tpl_assign('tool', $tool);
     tpl_assign('grouped_users', Users::getGroupedByCompany());
     tpl_assign('massmailer_data', $massmailer_data);
     if (is_array($massmailer_data)) {
         try {
             $subject = trim(array_var($massmailer_data, 'subject'));
             $message = trim(array_var($massmailer_data, 'message'));
             $errors = array();
             if ($subject == '') {
                 $errors[] = lang('massmailer subject required');
             }
             // if
             if ($message == '') {
                 $errors[] = lang('massmailer message required');
             }
             // if
             $users = Users::getAll();
             $recipients = array();
             if (is_array($users)) {
                 foreach ($users as $user) {
                     if (array_var($massmailer_data, 'user_' . $user->getId()) == 'checked') {
                         $recipients[] = Notifier::prepareEmailAddress($user->getEmail(), $user->getDisplayName());
                     }
                     // if
                 }
                 // foreach
             }
             // if
             if (!count($recipients)) {
                 $errors[] = lang('massmailer select recipients');
             }
             // if
             if (count($errors)) {
                 throw new FormSubmissionErrors($errors);
             }
             // if
             if (Notifier::sendEmail($recipients, Notifier::prepareEmailAddress(logged_user()->getEmail(), logged_user()->getDisplayName()), $subject, $message)) {
                 flash_success(lang('success massmail'));
             } else {
                 flash_error(lang('error massmail'));
             }
             // if
             $this->redirectToUrl($tool->getToolUrl());
         } catch (Exception $e) {
             tpl_assign('error', $e);
         }
         // try
     }
     // if
 }
 function send_reports()
 {
     $company = Companies::findById(1);
     $lTime = time() + 60 * 60 * $company->getTimezone();
     $dayOfWeek = date("l", $lTime);
     $footer = '<a href="' . externalUrl(ROOT_URL) . '">' . externalUrl(ROOT_URL) . "</a>";
     $people = Reminders::findAll(array('conditions' => 'reports_enabled = 1 and report_day = "' . $dayOfWeek . '"'));
     if (is_array($people) && count($people)) {
         foreach ($people as $person) {
             tpl_assign('settings', $person);
             $user = Users::findById($person->getUserId());
             tpl_assign('user', $user);
             $offset = 60 * 60 * $user->getTimezone();
             tpl_assign('offset', $offset);
             $allProjects = $user->getProjects();
             $emailBody = '';
             $recipient = Notifier::prepareEmailAddress($user->getEmail(), $user->getDisplayName());
             foreach ($allProjects as $project) {
                 if ($project->isActive() || $project->getCompletedOn()->getLeftInDays() > -7) {
                     tpl_assign('project', $project);
                     $condition = 'project_id = ' . $project->getId();
                     $condition .= " and is_private = 0 and is_silent = 0";
                     if (!$person->getReportsIncludeEveryone()) {
                         $condition .= ' and taken_by_id = ' . $user->getId();
                     }
                     $logs = array();
                     if ($person->getReportsIncludeActivity()) {
                         $condition .= " and created_on > Interval -7 day + now()";
                         $logs = ApplicationLogs::findAll(array('conditions' => $condition));
                     }
                     tpl_assign('logs', $logs);
                     $taskLists = $project->getAllTaskLists();
                     $emailTaskLists = array();
                     if (is_array($taskLists) && count($taskLists)) {
                         foreach ($taskLists as $taskList) {
                             $condition = 'task_list_id = ' . $taskList->getId();
                             if (!$person->getReportsIncludeEveryone()) {
                                 $condition .= " and assigned_to_user_id = " . $user->getId();
                             }
                             $condition .= " and completed_on > Interval -7 day + now()";
                             $tasks = ProjectTasks::findAll(array('conditions' => $condition));
                             if (is_array($tasks) && count($tasks)) {
                                 $emailTaskLists[] = $taskList;
                             }
                         }
                     }
                     if (count($emailTaskLists) || count($logs)) {
                         tpl_assign('taskLists', $emailTaskLists);
                         $emailBody .= tpl_fetch(get_template_path('report_per_project', 'reminders'));
                         if ($person->getReportsSummarizedBy()) {
                             try {
                                 Notifier::sendEmail($recipient, $recipient, "[ProjectPier] - Project Report - " . $project->getObjectName(), $emailBody . $footer, 'text/html');
                                 // send
                                 $emailBody = '';
                             } catch (Exception $e) {
                                 echo $e;
                             }
                         }
                     }
                 }
             }
             if (strlen($emailBody) && !$person->getReportsSummarizedBy()) {
                 $time = time() + 60 * 60 * $user->getTimezone();
                 try {
                     Notifier::sendEmail($recipient, $recipient, "[ProjectPier] - Activity Report - " . gmdate('Y/m/d', $time), $emailBody . $footer, 'text/html');
                     // send
                     $emailBody = '';
                 } catch (Exception $e) {
                     echo $e;
                 }
             }
         }
     }
 }