/php/lib/mail_manager/component/send_mail.class.php
PHP | 373 lines | 307 code | 64 blank | 2 comment | 33 complexity | b66c242f468924f116da40e857c3489d MD5 | raw file
- <?php
- namespace application\survey;
-
- use common\libraries\Translation;
- use common\libraries\Request;
- use common\libraries\Breadcrumb;
- use common\libraries\BreadcrumbTrail;
- use repository\RepositoryDataManager;
- use common\libraries\EqualityCondition;
- use tracking\Tracker;
- use tracking\Event;
- use common\libraries\Theme;
- use user\UserDataManager;
- use common\libraries\path;
- use common\libraries\Mail;
- use common\libraries\DynamicTabsRenderer;
-
- ini_set("memory_limit", "-1");
- ini_set("max_execution_time", "0");
-
- class SurveyMailManagerSendMailComponent extends SurveyMailManager
- {
- private $invitees;
- private $reporting_users;
- private $not_started;
- private $started;
- private $finished;
- private $mail_send = true;
- private $publication_id;
- private $survey_id;
- private $type;
- private $send_date;
-
- function run()
- {
- $this->publication_id = Request :: get(SurveyMailManager :: PARAM_PUBLICATION_ID);
- $this->type = Request :: get(SurveyMailManager :: PARAM_TYPE);
-
- if (! SurveyRights :: is_allowed_in_surveys_subtree(SurveyRights :: RIGHT_INVITE, $this->publication_id, SurveyRights :: TYPE_PUBLICATION, $this->get_user_id()))
- {
- $this->display_header();
- $this->display_error_message(Translation :: get('NotAllowed'));
- $this->display_footer();
- exit();
- }
-
- switch ($this->type)
- {
- case SurveyMailManager :: MAIL_PARTICIPANT_TYPE :
- $this->invitees = SurveyRights :: get_allowed_users(SurveyRights :: RIGHT_PARTICIPATE, $this->publication_id, SurveyRights :: TYPE_PUBLICATION);
- $this->not_started = array();
- $this->started = array();
- $this->finished = array();
-
- $condition = new EqualityCondition(SurveyParticipantTracker :: PROPERTY_SURVEY_PUBLICATION_ID, $this->publication_id);
- $trackers = Tracker :: get_data(SurveyParticipantTracker :: CLASS_NAME, SurveyManager :: APPLICATION_NAME, $condition);
-
- while ($tracker = $trackers->next_result())
- {
- if ($tracker->get_status() == SurveyParticipantTracker :: STATUS_FINISHED)
- {
- $this->finished[] = $tracker->get_user_id();
- }
- else
- if ($tracker->get_status() == SurveyParticipantTracker :: STATUS_STARTED)
- {
- $this->started[] = $tracker->get_user_id();
- }
-
- }
-
- $invitee_count = count(array_unique($this->invitees));
- $started_count = count(array_unique($this->started));
- $finished_count = count(array_unique($this->finished));
- $started_and_finished_users = array_merge($this->started, $this->finished);
- $this->not_started = array_diff($this->invitees, $started_and_finished_users);
-
- $not_started_count = $invitee_count-$started_count-$finished_count;
-
-
- $users = array();
- $users[SurveyRights :: PARTICIPATE_RIGHT_NAME] = $invitee_count;
- $users[SurveyParticipantTracker :: STATUS_STARTED] = $started_count;
- $users[SurveyParticipantTracker :: STATUS_NOTSTARTED] = $not_started_count;
- $users[SurveyParticipantTracker :: STATUS_FINISHED] = $finished_count;
- break;
- case SurveyMailManager :: MAIL_EXPORT_TYPE :
- $this->invitees = SurveyRights :: get_allowed_users(SurveyRights :: RIGHT_EXPORT_RESULT, $this->publication_id, SurveyRights :: TYPE_PUBLICATION);
- $invitee_count = count(array_unique($this->invitees));
- $users = array();
- $users[SurveyRights :: EXPORT_RESULT_RIGHT_NAME] = $invitee_count;
- //
- break;
- case SurveyMailManager :: MAIL_REPORTING_TYPE :
- $this->invitees = SurveyRights :: get_allowed_users(SurveyRights :: RIGHT_REPORTING, $this->publication_id, SurveyRights :: TYPE_PUBLICATION);
- $invitee_count = count(array_unique($this->invitees));
- $users = array();
- $users[SurveyRights :: REPORTING_RIGHT_NAME] = $invitee_count;
- break;
-
- }
-
- $survey_publication = SurveyDataManager :: get_instance()->retrieve_survey_publication($this->publication_id);
- $this->survey_id = $survey_publication->get_content_object_id();
- $survey = RepositoryDataManager :: get_instance()->retrieve_content_object($this->survey_id);
-
- $form = new SurveyPublicationMailerForm($this, $this->get_user(), $users, $this->type, $this->get_url(array(
- self :: PARAM_PUBLICATION_ID => $this->publication_id, SurveyMailManager :: PARAM_TYPE => $this->type)));
-
- if ($form->validate())
- {
- $values = $form->exportValues();
- $user_ids = $form->get_seleted_group_user_ids();
- $this->parse_values($values, $user_ids);
- }
- else
- {
- $this->display_header();
- echo $this->get_survey_html($survey);
- echo $form->toHtml();
- $this->display_footer();
- }
-
- }
-
- function get_survey_html($survey)
- {
- $html = array();
- $html[] = '<div class="content_object" style="background-image: url(' . Theme :: get_image_path('survey') . 'survey_22.png);">';
-
- switch ($this->type)
- {
- case SurveyMailManager :: MAIL_PARTICIPANT_TYPE :
- $html[] = '<div class="title">' . Translation :: get('MailToParticipantsForSurvey') . ' ' . ' </div>';
- break;
- case SurveyMailManager :: MAIL_EXPORT_TYPE :
- $html[] = '<div class="title">' . Translation :: get('MailToExportersForSurvey') . ' ' . ' </div>';
- break;
- case SurveyMailManager :: MAIL_REPORTING_TYPE :
- $html[] = '<div class="title">' . Translation :: get('MailToReportersForSurvey') . ' ' . ' </div>';
- break;
-
- }
- $html[] = $survey->get_title() . '<br/>';
- $html[] = '</div>';
- return implode("\n", $html);
-
- }
-
- function parse_values($values, $user_ids)
- {
-
- $users = array();
- $mail_user_ids = array();
- $dm = UserDataManager :: get_instance();
-
- switch ($this->type)
- {
- case SurveyMailManager :: MAIL_PARTICIPANT_TYPE :
- if (isset($user_ids))
- {
- $this->invitees = array_intersect($this->invitees, $user_ids);
- }
-
- $not_started = $values[SurveyParticipantTracker :: STATUS_NOTSTARTED];
- if ($not_started == 1)
- {
- $mail_user_ids = array_merge($mail_user_ids, $this->not_started);
- }
-
- $started = $values[SurveyParticipantTracker :: STATUS_STARTED];
-
- if ($started == 1)
- {
- $mail_user_ids = array_merge($mail_user_ids, $this->started);
- }
-
- $finished = $values[SurveyParticipantTracker :: STATUS_FINISHED];
-
- if ($finished == 1)
- {
- $mail_user_ids = array_merge($mail_user_ids, $this->finished);
- }
-
- $invitees = $values[SurveyRights :: PARTICIPATE_RIGHT_NAME];
-
- if ($invitees == 1)
- {
-
- $mail_user_ids = array_merge($mail_user_ids, $this->invitees);
- }
- break;
- case SurveyMailManager :: MAIL_EXPORT_TYPE :
- if (isset($user_ids))
- {
- $this->invitees = array_intersect($this->invitees, $user_ids);
- }
-
- $invitees = $values[SurveyRights :: EXPORT_RESULT_RIGHT_NAME];
-
- if ($invitees == 1)
- {
-
- $mail_user_ids = array_merge($mail_user_ids, $this->invitees);
- }
- break;
- case SurveyMailManager :: MAIL_REPORTING_TYPE :
-
- if (isset($user_ids))
- {
- $this->invitees = array_intersect($this->invitees, $user_ids);
- }
-
- $invitees = $values[SurveyRights :: REPORTING_RIGHT_NAME];
-
- if ($invitees == 1)
- {
- $mail_user_ids = array_merge($mail_user_ids, $this->invitees);
- }
- break;
- }
-
- $mail_user_ids = array_unique($mail_user_ids);
-
- if (count($mail_user_ids) == 0)
- {
- $this->redirect(Translation :: get('NoSurveyMailsSend'), false, array(
- self :: PARAM_ACTION => self :: ACTION_BROWSE));
- }
- else
- {
-
- $email_content = $values[SurveyPublicationMailerForm :: EMAIL_CONTENT];
-
- $fullbody = array();
- $fullbody[] = $email_content;
- $fullbody[] = '<br/><br/>';
- $fullbody[] = '<p id="link">';
-
- $parameters = array();
-
- switch ($this->type)
- {
- case SurveyMailManager :: MAIL_PARTICIPANT_TYPE :
- $parameters[SurveyManager :: PARAM_ACTION] = SurveyManager :: ACTION_TAKE;
- $parameters[SurveyManager :: PARAM_SURVEY_ID] = $this->survey_id;
- $parameters[SurveyManager :: PARAM_PUBLICATION_ID] = $this->publication_id;
- $url = Path :: get(WEB_PATH) . $this->get_link($parameters);
- $fullbody[] = '<a href=' . $url . '>' . Translation :: get('ClickToTakeSurvey') . '</a>';
- $selected_tab = SurveyMailManagerBrowserComponent :: TAB_MAILS_TO_PARTICIPANTS;
- break;
- case SurveyMailManager :: MAIL_EXPORT_TYPE :
- $parameters[SurveyManager :: PARAM_ACTION] = SurveyManager :: ACTION_EXPORT;
- $parameters[SurveyManager :: PARAM_PUBLICATION_ID] = $this->publication_id;
- $url = Path :: get(WEB_PATH) . $this->get_link($parameters);
- $fullbody[] = '<a href=' . $url . '>' . Translation :: get('ClickToExportResults') . '</a>';
- $selected_tab = SurveyMailManagerBrowserComponent :: TAB_MAILS_TO_EXPORTERS;
- break;
- case SurveyMailManager :: MAIL_REPORTING_TYPE :
- $parameters[SurveyManager :: PARAM_ACTION] = SurveyManager :: ACTION_REPORTING;
- $parameters[SurveyManager :: PARAM_PUBLICATION_ID] = $this->publication_id;
- $url = Path :: get(WEB_PATH) . $this->get_link($parameters);
- $fullbody[] = '<a href=' . $url . '>' . Translation :: get('ClickToViewReports') . '</a>';
- $selected_tab = SurveyMailManagerBrowserComponent :: TAB_MAILS_TO_REPORTERS;
- break;
- }
-
- $fullbody[] = '<br/><br/>' . Translation :: get('OrCopyAndPasteThisText') . ':';
- $fullbody[] = '<br/><a href=' . $url . '>' . $url . '</a>';
- $fullbody[] = '</p>';
-
- $body = implode("\n", $fullbody);
-
- $email_header = $values[SurveyPublicationMailerForm :: EMAIL_HEADER];
- $email_from_address = $values[SurveyPublicationMailerForm :: FROM_ADDRESS];
- $email_reply_address = $values[SurveyPublicationMailerForm :: REPLY_ADDRESS];
- $email_from_address_name = $values[SurveyPublicationMailerForm :: FROM_ADDRESS_NAME];
- $email_reply_address_name = $values[SurveyPublicationMailerForm :: REPLY_ADDRESS_NAME];
-
- $email = new SurveyPublicationMail();
- $email->set_mail_header($email_header);
- $email->set_mail_content($body);
- $email->set_sender_user_id($this->get_user_id());
- $email->set_from_address($email_from_address);
- $email->set_from_address_name($email_from_address_name);
- $email->set_reply_address($email_reply_address);
- $email->set_reply_address_name($email_reply_address_name);
- $email->set_publication_id($this->publication_id);
- $email->set_send_date(time());
- $email->set_type($this->type);
- $succes = $email->create();
-
- if ($succes)
- {
- foreach ($mail_user_ids as $user_id)
- {
-
- $user = $dm->retrieve_user($user_id);
- $to_email = $user->get_email();
- $this->send_mail($user_id, $to_email, $email);
-
- }
-
- if ($this->mail_send == false)
- {
- $this->redirect(Translation :: get('NotAllMailsSend'), true, array(
- self :: PARAM_ACTION => self :: ACTION_BROWSE,
- DynamicTabsRenderer :: PARAM_SELECTED_TAB => $selected_tab));
- }
- else
- {
- $this->redirect(Translation :: get('AllMailsSend'), false, array(
- self :: PARAM_ACTION => self :: ACTION_BROWSE,
- DynamicTabsRenderer :: PARAM_SELECTED_TAB => $selected_tab));
- }
-
- }
- else
- {
- $this->redirect(Translation :: get('NoMailsSend'), true, array(
- self :: PARAM_ACTION => self :: ACTION_BROWSE,
- DynamicTabsRenderer :: PARAM_SELECTED_TAB => $selected_tab));
- }
-
- }
-
- }
-
- function send_mail($user_id, $to_email, $email)
- {
- $arg = array();
- $args[SurveyPublicationMailTracker :: PROPERTY_USER_ID] = $user_id;
- $args[SurveyPublicationMailTracker :: PROPERTY_SURVEY_PUBLICATION_MAIL_ID] = $email->get_id();
-
- $from = array();
- $from[Mail :: NAME] = $email->get_from_address_name();
- $from[Mail :: EMAIL] = $email->get_from_address();
-
- $mail = Mail :: factory($email->get_mail_header(), $email->get_mail_content(), $to_email, $from);
- $reply = array();
- $reply[Mail :: NAME] = $email->get_reply_address_name();
- $reply[Mail :: EMAIL] = $email->get_reply_address();
- $mail->set_reply($reply);
-
- // Check whether it was sent successfully
- if ($mail->send() === FALSE)
- {
- $this->mail_send = false;
- $args[SurveyPublicationMailTracker :: PROPERTY_STATUS] = SurveyPublicationMailTracker :: STATUS_MAIL_NOT_SEND;
- }
- else
- {
- $args[SurveyPublicationMailTracker :: PROPERTY_STATUS] = SurveyPublicationMailTracker :: STATUS_MAIL_SEND;
- }
-
- $args[SurveyPublicationMailTracker :: PROPERTY_SURVEY_PUBLICATION_ID] = $this->publication_id;
- $tracker = Event :: trigger(SurveyPublicationMailTracker :: REGISTER_PUBLICATION_MAIL_EVENT, SurveyManager :: APPLICATION_NAME, $args);
- }
-
- function add_additional_breadcrumbs(BreadcrumbTrail $breadcrumbtrail)
- {
- $breadcrumbtrail->add(new Breadcrumb($this->get_url(array(self :: PARAM_ACTION => self :: ACTION_BROWSE)), Translation :: get('BrowseSurveys')));
- $breadcrumbtrail->add(new Breadcrumb($this->get_url(array(
- SurveyMailManager :: PARAM_ACTION => SurveyMailManager :: ACTION_BROWSE)), Translation :: get('BrowseSurveyPublicationMails')));
- }
-
- function get_additional_parameters()
- {
- return array(self :: PARAM_PUBLICATION_ID);
- }
-
- }
- ?>