public function OnPostback()
 {
     # If there's no id, ensure no match object is created. Page will then display a "match not found" message.
     # There's a separate page for adding matches, even for admins.
     if (!$this->editor->GetDataObjectId()) {
         return;
     }
     # Get the submitted match
     $this->match = $this->editor->GetDataObject();
     # Because this is a new request, we need to reverify whether this is the match owner before letting anything happen.
     # Can't trust that info from a postback so MUST go to the database to check it.
     $this->match_manager->ReadByMatchId(array($this->editor->GetDataObjectId()));
     $check_match = $this->match_manager->GetFirst();
     $this->b_user_is_match_owner = ($check_match instanceof Match and AuthenticationManager::GetUser()->GetId() == $check_match->GetAddedBy()->GetId());
     # Don't wan't to edit tournaments on this page, so even as admin make sure we're not trying to save one.
     # Can't change the match type, so find out what that is from the db too.
     $this->b_is_tournament = $check_match->GetMatchType() == MatchType::TOURNAMENT;
     # Check whether cancel was clicked
     if ($this->editor->CancelClicked()) {
         # If so, get the match's short URL and redirect
         $this->match_manager->ExpandMatchUrl($this->match);
         $this->Redirect($this->match->GetNavigateUrl());
     }
     # save data if valid
     if ($this->IsValid() and !$this->b_is_tournament) {
         # Save player of match
         $this->match_manager->SaveHighlights($this->match);
         $this->match_manager->ExpandMatchUrl($this->match);
         # Add comment if provided
         if (trim($this->match->GetNewComment())) {
             require_once 'forums/topic-manager.class.php';
             require_once 'forums/review-item.class.php';
             require_once 'forums/subscription-manager.class.php';
             $topic_manager = new TopicManager($this->GetSettings(), $this->GetDataConnection());
             $item_to_comment_on = new ReviewItem($this->GetSettings());
             $item_to_comment_on->SetType(ContentType::STOOLBALL_MATCH);
             $item_to_comment_on->SetId($this->match->GetId());
             $item_to_comment_on->SetNavigateUrl("https://" . $this->GetSettings()->GetDomain() . $this->match->GetNavigateUrl());
             $message = $topic_manager->SaveComment($item_to_comment_on, $this->match->GetNewComment());
             # send subscription emails
             $subs_manager = new SubscriptionManager($this->GetSettings(), $this->GetDataConnection());
             $subs_manager->SendCommentsSubscriptions($item_to_comment_on, $message);
             unset($subs_manager);
         }
         # Match may have been updated so send an email
         $this->match_manager->NotifyMatchModerator($this->match->GetId());
         # Show user the match, so they can see update was applied
         $this->Redirect($this->match->GetNavigateUrl());
     }
 }
 function OnPageLoad()
 {
     echo '<h1>Subscribed to ' . Html::Encode($this->o_review_item->GetTitle() ? $this->o_review_item->GetTitle() : ' page') . '</h1>';
     # Confirm subscription to user
     $b_title = (bool) $this->o_review_item->GetTitle();
     echo new XhtmlElement('p', 'You have subscribed to ' . Html::Encode($b_title ? "'" . $this->o_review_item->GetTitle() . "'" : 'the page you selected') . '. You will get an email alert every time someone adds a comment.');
     echo new XhtmlElement('p', 'If you want to stop getting these email alerts, sign in to ' . Html::Encode($this->GetSettings()->GetSiteName()) . ' and delete your subscription from the email alerts page.');
     # Links suggesting what to do next
     $o_whatnext = new XhtmlElement('ul');
     if ($this->o_review_item->GetNavigateUrl()) {
         $o_whatnext->AddControl(new XhtmlElement('li', new XhtmlAnchor('Go back to ' . Html::Encode($b_title ? $this->o_review_item->GetTitle() : 'the page you came from'), $this->o_review_item->GetNavigateUrl())));
     }
     $o_whatnext->AddControl(new XhtmlElement('li', new XhtmlAnchor("Email alerts", $this->GetSettings()->GetUrl('EmailAlerts'))));
     echo $o_whatnext;
 }
 function SendCommentsSubscriptions(ReviewItem $review_item, ForumMessage $message)
 {
     # get all subscriptions for this item
     if (AuthenticationManager::GetUser()->IsSignedIn() and $review_item->GetId()) {
         $s_person = $this->GetSettings()->GetTable('User');
         $s_sub = $this->GetSettings()->GetTable('EmailSubscription');
         # join to item's table to get the title, regardless of message title
         $s_sql = '';
         switch ($review_item->GetType()) {
             case ContentType::STOOLBALL_MATCH:
                 $matches = $this->GetSettings()->GetTable('Match');
                 $s_sql = "SELECT {$matches}.match_title AS title, {$s_person}.email\n\t\t\t\t\tFROM ({$s_person} INNER JOIN {$s_sub} ON {$s_person}.user_id = {$s_sub}.user_id AND {$s_sub}.item_type = " . ContentType::STOOLBALL_MATCH . ")\n\t\t\t\t\tINNER JOIN {$matches} ON {$s_sub}.item_id = {$matches}.match_id AND {$s_sub}.item_type = " . ContentType::STOOLBALL_MATCH . "\n\t\t\t\t\tWHERE {$s_sub}.item_id = " . Sql::ProtectNumeric($review_item->GetId()) . " AND {$s_person}.user_id <> " . Sql::ProtectNumeric(AuthenticationManager::GetUser()->GetId());
                 break;
         }
         if ($s_sql) {
             # if there's at least one person, build email
             require_once 'Zend/Mail.php';
             $email = new Zend_Mail('UTF-8');
             if ($this->GetEmailAddresses($s_sql, $email)) {
                 $o_filter = new BadLanguageFilter();
                 $s_title = $o_filter->Filter($this->s_review_item_title);
                 unset($o_filter);
                 $s_title = StringFormatter::PlainText($s_title);
                 # send the email
                 $email->addTo($this->GetSettings()->GetSubscriptionEmailTo());
                 $email->setFrom($this->GetSettings()->GetSubscriptionEmailFrom(), $this->GetSettings()->GetSubscriptionEmailFrom());
                 $email->setSubject("Email alert: '" . $s_title . "'");
                 $email->setBodyText($this->GetHeader() . trim(AuthenticationManager::GetUser()->GetName()) . ' has just commented on a page at ' . $this->GetSettings()->GetSiteName() . ' for which you subscribed to an email alert.' . "\n\n" . "The page is called '" . $s_title . "' - here's an excerpt of the new comments:\n\n" . $message->GetExcerpt() . "\n\n" . 'View the new comments at' . "\n" . $review_item->GetNavigateUrl() . '#message' . $message->GetId() . $this->GetFooter());
                 try {
                     $email->send();
                 } catch (Zend_Mail_Transport_Exception $e) {
                     # Do nothing - email not that important so, if it fails, fail silently rather than raising a fatal error
                 }
             }
         }
     }
 }
 /**
  * Reads the latest few messages posted by a specific user
  * @param int $user_id
  * @return ForumMessage[]
  */
 public function ReadMessagesByUser($user_id)
 {
     $s_sql = 'SELECT nsa_forum_message.id AS message_id, nsa_forum_message.date_added AS message_date, ' . "nsa_match.short_url, nsa_match.match_title " . 'FROM nsa_forum_message INNER JOIN nsa_match ON nsa_forum_message.item_id = nsa_match.match_id AND nsa_forum_message.item_type = ' . ContentType::STOOLBALL_MATCH . ' ' . 'WHERE nsa_forum_message.user_id = ' . Sql::ProtectNumeric($user_id, false) . ' ' . 'ORDER BY nsa_forum_message.date_added DESC ' . 'LIMIT 0,11';
     $result = $this->GetDataConnection()->query($s_sql);
     $messages = array();
     while ($o_row = $result->fetch()) {
         $o_message = new ForumMessage($this->GetSettings(), AuthenticationManager::GetUser());
         $o_message->SetId($o_row->message_id);
         $o_message->SetDate($o_row->message_date);
         $review_item = new ReviewItem($this->GetSettings());
         $review_item->SetTitle($o_row->match_title);
         $review_item->SetNavigateUrl($this->o_settings->GetClientRoot() . $o_row->short_url);
         $o_message->SetReviewItem($review_item);
         $messages[] = $o_message;
     }
     $result->closeCursor();
     return $messages;
 }
 /**
  * Get review items for rendering review items.
  *
  * @return void
  */
 public function getReviewItems()
 {
     global $data;
     global $settings;
     // get target review items
     $this->getTargetReviewItems();
     $array = array();
     foreach ($settings['review_items'] as $code => $review_items) {
         $array[$code] = ReviewItem::whereIn('code', $review_items)->get()->toArray();
         $arraySecond = array();
         foreach ($array[$code] as $key => $row) {
             $arraySecond[$row['code']] = $row;
         }
         $array[$code] = $arraySecond;
     }
     $data['review_items'] = $array;
 }
 function OnLoadPageData()
 {
     /* @var $o_match Match */
     /* @var $o_team Team */
     # new data manager
     $match_manager = new MatchManager($this->GetSettings(), $this->GetDataConnection());
     # create repeater control, and save any posted data
     $this->repeater = new DataEditRepeater($this, 'CreateEditControl');
     $this->repeater->SetCssClass('matchResults');
     $this->repeater->SetPersistedParameters(array('team', 'season', "tournament"));
     $this->repeater->SetButtonText('Save all results');
     $this->repeater->SetShowButtonsAtTop(true);
     if ($this->IsPostback() and !$this->IsRefresh() and $this->IsValid()) {
         require_once 'forums/topic-manager.class.php';
         require_once 'forums/review-item.class.php';
         require_once 'forums/subscription-manager.class.php';
         $topic_manager = new TopicManager($this->GetSettings(), $this->GetDataConnection());
         foreach ($this->repeater->GetDataObjects() as $o_current_match) {
             /* @var $o_current_match Match */
             $match_manager->SaveResult($o_current_match);
             $match_manager->ExpandMatchUrl($o_current_match);
             $match_manager->NotifyMatchModerator($o_current_match->GetId());
             if (trim($o_current_match->GetNewComment())) {
                 $item_to_comment_on = new ReviewItem($this->GetSettings());
                 $item_to_comment_on->SetType(ContentType::STOOLBALL_MATCH);
                 $item_to_comment_on->SetId($o_current_match->GetId());
                 $item_to_comment_on->SetNavigateUrl("https://" . $this->GetSettings()->GetDomain() . $o_current_match->GetNavigateUrl());
                 $message = $topic_manager->SaveComment($item_to_comment_on, $o_current_match->GetNewComment());
                 # send subscription emails - new object each time to reset list of who's already recieved an email
                 $subs_manager = new SubscriptionManager($this->GetSettings(), $this->GetDataConnection());
                 $subs_manager->SendCommentsSubscriptions($item_to_comment_on, $message);
                 unset($subs_manager);
             }
         }
         $this->b_saved = true;
     }
     # get matches
     if (!is_null($this->i_team_id)) {
         $a_season_times = Season::SeasonDates();
         $match_manager->FilterByTeam(array($this->i_team_id));
         $match_manager->FilterByDateStart($a_season_times[0]);
         $match_manager->FilterByDateEnd($a_season_times[1]);
         $match_manager->ReadMatchSummaries();
     } else {
         if (!is_null($this->i_season_id)) {
             $match_manager->ReadBySeasonId(array($this->i_season_id), true);
         } else {
             if (!is_null($this->tournament_id)) {
                 $match_manager->FilterByTournament($this->tournament_id);
                 $this->a_matches = $match_manager->ReadMatchSummaries();
             }
         }
     }
     $this->a_matches = $match_manager->GetItems();
     # Make sure we have some matches
     if (count($this->a_matches)) {
         # If it's matches for a team, get the team
         if (!is_null($this->i_team_id)) {
             # Should only need to look at the first match, because the team must be a
             # part of that match in order for the match to be in this list
             $o_match =& $this->a_matches[0];
             $o_team = $o_match->GetHomeTeam();
             if ($o_team instanceof Team and $o_team->GetId() == $this->i_team_id) {
                 $this->team = $o_team;
             } else {
                 foreach ($o_match->GetAwayTeams() as $o_team) {
                     if ($o_team->GetId() == $this->i_team_id) {
                         $this->team = $o_team;
                         break;
                     }
                 }
             }
             if (!is_object($this->team)) {
                 $this->Redirect();
             }
             # Now that we have short URL data, if data has just been saved for team, redirect back to team
             if ($this->b_saved) {
                 $this->Redirect($this->team->GetNavigateUrl());
             }
         } else {
             if (!is_null($this->i_season_id)) {
                 # get details of the season
                 require_once 'stoolball/competition-manager.class.php';
                 $o_comp_manager = new CompetitionManager($this->GetSettings(), $this->GetDataConnection());
                 $o_comp_manager->ReadById(null, array($this->i_season_id));
                 $o_competition = $o_comp_manager->GetFirst();
                 unset($o_comp_manager);
                 if ($o_competition instanceof Competition) {
                     $this->season = $o_competition->GetWorkingSeason();
                 }
                 if (!$this->season instanceof Season) {
                     $this->Redirect();
                 }
                 # Now that we have short URL data, if data has just been saved for season, redirect back to season
                 if ($this->b_saved) {
                     $this->Redirect($this->season->GetNavigateUrl());
                 }
             } else {
                 if (!is_null($this->tournament_id)) {
                     $match_manager->ReadByMatchId(array($this->tournament_id));
                     $this->tournament = $match_manager->GetFirst();
                     if (!is_null($this->tournament)) {
                         # If the tournament has just been saved, now we have its URL so redirect to the thanks page
                         if ($this->b_saved) {
                             $this->Redirect($this->tournament->GetNavigateUrl());
                         }
                     }
                 }
             }
         }
     } else {
         $this->Redirect();
     }
     unset($match_manager);
 }