public function execute(HTTPRequestCustom $request)
 {
     $id = $request->get_int('id', 0);
     $id_stream = $request->get_int('id_stream', 0);
     $db_querier = PersistenceContext::get_querier();
     if ($this->subscriber_exist($id) || $id_stream !== 0 && $id !== 0) {
         if (!NewsletterAuthorizationsService::id_stream($id_stream)->moderation_subscribers()) {
             NewsletterAuthorizationsService::get_errors()->moderation_subscribers();
         }
         $condition = "WHERE subscriber_id = :id AND stream_id = :id_stream";
         $parameters = array('id' => $id, 'id_stream' => $id_stream);
         $db_querier->delete(NewsletterSetup::$newsletter_table_subscriptions, $condition, $parameters);
         $condition = "WHERE subscriber_id = :id";
         $parameters = array('id' => $id);
         $is_last = PersistenceContext::get_querier()->count(NewsletterSetup::$newsletter_table_subscriptions, $condition, $parameters) == 0;
         if ($is_last) {
             $condition = "WHERE id = :id";
             $parameters = array('id' => $id);
             $db_querier->delete(NewsletterSetup::$newsletter_table_subscribers, $condition, $parameters);
         }
         NewsletterStreamsCache::invalidate();
         $controller = new UserErrorController(LangLoader::get_message('success', 'status-messages-common'), LangLoader::get_message('process.success', 'status-messages-common'), UserErrorController::SUCCESS);
         DispatchManager::redirect($controller);
     } else {
         $controller = new UserErrorController(LangLoader::get_message('error', 'status-messages-common'), LangLoader::get_message('error-subscriber-not-existed', 'common', 'newsletter'));
         DispatchManager::redirect($controller);
     }
 }
 private function build_form($request)
 {
     $id = $request->get_int('id', 0);
     $archive_exist = PersistenceContext::get_querier()->count(NewsletterSetup::$newsletter_table_archives, "WHERE id = '" . $id . "'") > 0;
     if (!$archive_exist) {
         $controller = new UserErrorController(LangLoader::get_message('error', 'status-messages-common'), $this->lang['error-archive-not-existed']);
         DispatchManager::redirect($controller);
     }
     $id_stream = PersistenceContext::get_querier()->get_column_value(NewsletterSetup::$newsletter_table_archives, 'stream_id', "WHERE id = '" . $id . "'");
     if (!NewsletterAuthorizationsService::id_stream($id_stream)->read_archives()) {
         NewsletterAuthorizationsService::get_errors()->read_archives();
     }
     $this->contents = NewsletterService::display_newsletter($id);
 }
 public function execute(HTTPRequestCustom $request)
 {
     $id = $request->get_int('id', 0);
     $id_stream = $request->get_int('id_stream', 0);
     if ($this->archive_exist($id) || $id_stream !== 0 && $id !== 0) {
         if (!NewsletterAuthorizationsService::id_stream($id_stream)->moderation_subscribers()) {
             NewsletterAuthorizationsService::get_errors()->moderation_archives();
         }
         NewsletterService::delete_archive($id);
         AppContext::get_response()->redirect($request->get_url_referrer() ? $request->get_url_referrer() : NewsletterUrlBuilder::archives($id_stream), LangLoader::get_message('newsletter.message.success.delete', 'common', 'newsletter'));
     } else {
         $controller = new UserErrorController(LangLoader::get_message('error', 'status-messages-common'), $this->lang['error-archive-not-existed']);
         DispatchManager::redirect($controller);
     }
 }
 private function build_form($request)
 {
     $field = $request->get_value('field', NewsletterUrlBuilder::DEFAULT_SORT_FIELD);
     $sort = $request->get_value('sort', NewsletterUrlBuilder::DEFAULT_SORT_MODE);
     $current_page = $request->get_int('page', 1);
     $mode = $sort == 'top' ? 'ASC' : 'DESC';
     if (!NewsletterAuthorizationsService::id_stream($this->stream->get_id())->read_archives()) {
         NewsletterAuthorizationsService::get_errors()->read_archives();
     }
     if (!NewsletterStreamsCache::load()->stream_exists($this->stream->get_id())) {
         $controller = new UserErrorController(LangLoader::get_message('error', 'status-messages-common'), LangLoader::get_message('admin.stream-not-existed', 'common', 'newsletter'));
         DispatchManager::redirect($controller);
     }
     switch ($field) {
         case 'stream':
             $field_bdd = 'stream_id';
             break;
         case 'subject':
             $field_bdd = 'subject';
             break;
         case 'date':
             $field_bdd = 'timestamp';
             break;
         case 'subscribers':
             $field_bdd = 'nbr_subscribers';
             break;
         default:
             $field_bdd = 'timestamp';
     }
     $stream_id = $this->stream->get_id();
     $stream_condition = $stream_id ? "WHERE stream_id = '" . $stream_id . "'" : "";
     $nbr_archives = PersistenceContext::get_querier()->count(NewsletterSetup::$newsletter_table_archives, $stream_condition);
     $pagination = $this->get_pagination($current_page, $nbr_archives, $field, $sort);
     $moderation_auth = NewsletterAuthorizationsService::id_stream($this->stream->get_id())->moderation_archives();
     $this->view->put_all(array('C_MODERATE' => $moderation_auth, 'C_ARCHIVES' => (double) $nbr_archives, 'C_SPECIFIC_STREAM' => $stream_id, 'C_PAGINATION' => $pagination->has_several_pages(), 'NUMBER_COLUMN' => 3 + (int) (empty($stream_id) && !empty($nbr_archives)) + $moderation_auth, 'SORT_STREAM_TOP' => NewsletterUrlBuilder::archives($this->stream->get_id(), $this->stream->get_rewrited_name(), 'stream', 'top', $current_page)->rel(), 'SORT_STREAM_BOTTOM' => NewsletterUrlBuilder::archives($this->stream->get_id(), $this->stream->get_rewrited_name(), 'stream', 'bottom', $current_page)->rel(), 'SORT_SUBJECT_TOP' => NewsletterUrlBuilder::archives($this->stream->get_id(), $this->stream->get_rewrited_name(), 'subject', 'top', $current_page)->rel(), 'SORT_SUBJECT_BOTTOM' => NewsletterUrlBuilder::archives($this->stream->get_id(), $this->stream->get_rewrited_name(), 'subject', 'bottom', $current_page)->rel(), 'SORT_DATE_TOP' => NewsletterUrlBuilder::archives($this->stream->get_id(), $this->stream->get_rewrited_name(), 'date', 'top', $current_page)->rel(), 'SORT_DATE_BOTTOM' => NewsletterUrlBuilder::archives($this->stream->get_id(), $this->stream->get_rewrited_name(), 'date', 'bottom', $current_page)->rel(), 'SORT_SUBSCRIBERS_TOP' => NewsletterUrlBuilder::archives($this->stream->get_id(), $this->stream->get_rewrited_name(), 'subscribers', 'top', $current_page)->rel(), 'SORT_SUBSCRIBERS_BOTTOM' => NewsletterUrlBuilder::archives($this->stream->get_id(), $this->stream->get_rewrited_name(), 'subscribers', 'bottom', $current_page)->rel(), 'PAGINATION' => $pagination->display()));
     $result = PersistenceContext::get_querier()->select("SELECT *\n\t\tFROM " . NewsletterSetup::$newsletter_table_archives . "\n\t\t" . $stream_condition . "\n\t\tORDER BY " . $field_bdd . " " . $mode . "\n\t\tLIMIT :number_items_per_page OFFSET :display_from", array('number_items_per_page' => $pagination->get_number_items_per_page(), 'display_from' => $pagination->get_display_from()));
     while ($row = $result->fetch()) {
         $stream = NewsletterStreamsCache::load()->get_stream($row['stream_id']);
         $this->view->assign_block_vars('archives_list', array('STREAM_NAME' => $stream->get_name(), 'SUBJECT' => $row['subject'], 'DATE' => Date::to_format($row['timestamp'], Date::FORMAT_DAY_MONTH_YEAR), 'NBR_SUBSCRIBERS' => $row['nbr_subscribers'], 'U_VIEW_STREAM' => NewsletterUrlBuilder::archives($stream->get_id(), $this->stream->get_rewrited_name())->rel(), 'U_VIEW_ARCHIVE' => NewsletterUrlBuilder::archive($row['id'])->rel(), 'U_DELETE_ARCHIVE' => NewsletterUrlBuilder::delete_archive($row['id'], $stream->get_id())->rel()));
     }
     $result->dispose();
 }
    private function build_form($request)
    {
        $pagination = $this->get_pagination();
        $result = PersistenceContext::get_querier()->select('SELECT @id_stream:= id, newsletter_streams.*,
			(SELECT COUNT(*)
			FROM ' . NewsletterSetup::$newsletter_table_subscriptions . ' subscriptions
			LEFT JOIN ' . NewsletterSetup::$newsletter_table_subscribers . ' subscribers ON subscriptions.subscriber_id = subscribers.id
			LEFT JOIN ' . DB_TABLE_MEMBER . ' member ON subscribers.user_id = member.user_id
			WHERE (subscribers.mail <> \'\' OR member.email <> \'\') AND subscriptions.stream_id = @id_stream
			) AS subscribers_number
		FROM ' . NewsletterSetup::$newsletter_table_streams . ' newsletter_streams
		LIMIT :number_items_per_page OFFSET :display_from', array('number_items_per_page' => $pagination->get_number_items_per_page(), 'display_from' => $pagination->get_display_from()));
        while ($row = $result->fetch()) {
            if (NewsletterAuthorizationsService::id_stream($row['id'])->read()) {
                $this->view->assign_block_vars('streams_list', array('C_VIEW_ARCHIVES' => NewsletterAuthorizationsService::id_stream($row['id'])->read_archives(), 'C_VIEW_SUBSCRIBERS' => NewsletterAuthorizationsService::id_stream($row['id'])->read_subscribers(), 'IMAGE' => Url::to_rel($row['image']), 'NAME' => $row['name'], 'DESCRIPTION' => $row['description'], 'SUBSCRIBERS_NUMBER' => $row['subscribers_number'], 'U_VIEW_ARCHIVES' => NewsletterUrlBuilder::archives($row['id'], $row['rewrited_name'])->absolute(), 'U_VIEW_SUBSCRIBERS' => NewsletterUrlBuilder::subscribers($row['id'], $row['rewrited_name'])->absolute()));
            }
        }
        $this->view->put_all(array('C_SUSCRIBE' => NewsletterAuthorizationsService::check_authorizations()->subscribe(), 'C_STREAMS' => $result->get_rows_count() != 0, 'C_PAGINATION' => $pagination->has_several_pages(), 'PAGINATION' => $pagination->display()));
        $result->dispose();
    }
 private function build_form($request)
 {
     $field = $request->get_value('field', 'pseudo');
     $sort = $request->get_value('sort', 'top');
     $current_page = $request->get_int('page', 1);
     if (!NewsletterAuthorizationsService::id_stream($this->stream->get_id())->read_subscribers()) {
         NewsletterAuthorizationsService::get_errors()->read_subscribers();
     }
     $mode = $sort == 'top' ? 'ASC' : 'DESC';
     switch ($field) {
         case 'pseudo':
             $field_bdd = 'display_name';
             break;
         default:
             $field_bdd = 'display_name';
     }
     $subscribers_list = NewsletterService::list_subscribers_by_stream($this->stream->get_id());
     $nbr_subscribers = count($subscribers_list);
     $pagination = new ModulePagination($current_page, $nbr_subscribers, $this->nbr_subscribers_per_page);
     $pagination->set_url(NewsletterUrlBuilder::subscribers($this->stream->get_id(), $this->stream->get_rewrited_name(), $field, $sort, '%d'));
     if ($pagination->current_page_is_empty() && $current_page > 1) {
         $error_controller = PHPBoostErrors::unexisting_page();
         DispatchManager::redirect($error_controller);
     }
     $this->view->put_all(array('C_SUBSCRIBERS' => (int) $nbr_subscribers, 'C_SUBSCRIPTION' => NewsletterUrlBuilder::subscribe()->rel(), 'C_PAGINATION' => $pagination->has_several_pages(), 'SORT_PSEUDO_TOP' => NewsletterUrlBuilder::subscribers($this->stream->get_id(), $this->stream->get_rewrited_name(), 'pseudo', 'top', $current_page)->rel(), 'SORT_PSEUDO_BOTTOM' => NewsletterUrlBuilder::subscribers($this->stream->get_id(), $this->stream->get_rewrited_name(), 'pseudo', 'bottom', $current_page)->rel(), 'PAGINATION' => $pagination->display()));
     if (!empty($nbr_subscribers)) {
         $result = PersistenceContext::get_querier()->select("SELECT subscribers.id, subscribers.user_id, subscribers.mail, member.display_name, member.email\n\t\t\tFROM " . NewsletterSetup::$newsletter_table_subscribers . " subscribers\n\t\t\tLEFT JOIN " . DB_TABLE_MEMBER . " member ON subscribers.user_id = member.user_id\n\t\t\tWHERE subscribers.id IN :ids_list\n\t\t\tORDER BY " . $field_bdd . " " . $mode . "\n\t\t\tLIMIT :number_items_per_page OFFSET :display_from", array('ids_list' => array_keys($subscribers_list), 'number_items_per_page' => $pagination->get_number_items_per_page(), 'display_from' => $pagination->get_display_from()));
         while ($row = $result->fetch()) {
             $pseudo = $row['user_id'] > 0 ? '<a href="' . UserUrlBuilder::profile($row['user_id'])->rel() . '">' . $row['display_name'] . '</a>' : LangLoader::get_message('visitor', 'user-common');
             $mail = $row['user_id'] > 0 ? $row['email'] : $row['mail'];
             if (!empty($mail)) {
                 $this->view->assign_block_vars('subscribers_list', array('C_AUTH_MODO' => NewsletterAuthorizationsService::id_stream($this->stream->get_id())->moderation_subscribers(), 'C_EDIT' => $row['user_id'] == User::VISITOR_LEVEL, 'U_EDIT' => $row['user_id'] == User::VISITOR_LEVEL ? NewsletterUrlBuilder::edit_subscriber($row['id'])->rel() : '', 'U_DELETE' => NewsletterUrlBuilder::delete_subscriber($row['id'], $this->stream->get_id())->rel(), 'PSEUDO' => $pseudo, 'MAIL' => $mail));
             }
         }
         $result->dispose();
     }
 }
 private function get_streams()
 {
     $streams = array();
     $newsletter_streams = NewsletterStreamsCache::load()->get_streams();
     foreach ($newsletter_streams as $id => $stream) {
         if ($id != Category::ROOT_CATEGORY && NewsletterAuthorizationsService::id_stream($id)->subscribe()) {
             $streams[] = new FormFieldMultipleCheckboxOption($id, $stream->get_name());
         }
     }
     return $streams;
 }