Gdn_Format::activityHeadline PHP Method

activityHeadline() public static method

The ProfileHeadline field is to be used on this page (the user profile page). The FullHeadline field is to be used on the main activity page. The replacement definitions are as follows: %1$s = ActivityName %2$s = ActivityName Possessive %3$s = RegardingName %4$s = RegardingName Possessive %5$s = Link to RegardingName's Wall %6$s = his/her %7$s = he/she %8$s = route & routecode %9$s = gender suffix (some languages require this).
public static activityHeadline ( object $Activity, integer $ProfileUserID = '', $ViewingUserID = '' ) : string
$Activity object An object representation of the activity being formatted.
$ProfileUserID integer If looking at a user profile, this is the UserID of the profile we are looking at.
return string
    public static function activityHeadline($Activity, $ProfileUserID = '', $ViewingUserID = '')
    {
        $Activity = (object) $Activity;
        if ($ViewingUserID == '') {
            $Session = Gdn::session();
            $ViewingUserID = $Session->isValid() ? $Session->UserID : -1;
        }
        $GenderSuffixCode = 'First';
        $GenderSuffixGender = $Activity->ActivityGender;
        if ($ViewingUserID == $Activity->ActivityUserID) {
            $ActivityName = $ActivityNameP = T('You');
        } else {
            $ActivityName = $Activity->ActivityName;
            $ActivityNameP = FormatPossessive($ActivityName);
            $GenderSuffixCode = 'Third';
        }
        if ($ProfileUserID != $Activity->ActivityUserID) {
            // If we're not looking at the activity user's profile, link the name
            $ActivityNameD = urlencode($Activity->ActivityName);
            $ActivityName = Anchor($ActivityName, UserUrl($Activity, 'Activity'));
            $ActivityNameP = Anchor($ActivityNameP, UserUrl($Activity, 'Activity'));
            $GenderSuffixCode = 'Third';
        }
        $Gender = t('their');
        //TODO: this isn't preferable but I don't know a better option
        $Gender2 = t('they');
        //TODO: this isn't preferable either
        if ($Activity->ActivityGender == 'm') {
            $Gender = t('his');
            $Gender2 = t('he');
        } elseif ($Activity->ActivityGender == 'f') {
            $Gender = t('her');
            $Gender2 = t('she');
        }
        if ($ViewingUserID == $Activity->RegardingUserID || $Activity->RegardingUserID == '' && $Activity->ActivityUserID == $ViewingUserID) {
            $Gender = $Gender2 = t('your');
        }
        $IsYou = false;
        if ($ViewingUserID == $Activity->RegardingUserID) {
            $IsYou = true;
            $RegardingName = t('you');
            $RegardingNameP = t('your');
            $GenderSuffixGender = $Activity->RegardingGender;
        } else {
            $RegardingName = $Activity->RegardingName == '' ? T('somebody') : $Activity->RegardingName;
            $RegardingNameP = formatPossessive($RegardingName);
            if ($Activity->ActivityUserID != $ViewingUserID) {
                $GenderSuffixCode = 'Third';
            }
        }
        $RegardingWall = '';
        $RegardingWallLink = '';
        if ($Activity->ActivityUserID == $Activity->RegardingUserID) {
            // If the activityuser and regardinguser are the same, use the $Gender Ref as the RegardingName
            $RegardingName = $RegardingProfile = $Gender;
            $RegardingNameP = $RegardingProfileP = $Gender;
        } elseif ($Activity->RegardingUserID > 0 && $ProfileUserID != $Activity->RegardingUserID) {
            // If there is a regarding user and we're not looking at his/her profile, link the name.
            $RegardingNameD = urlencode($Activity->RegardingName);
            if (!$IsYou) {
                $RegardingName = anchor($RegardingName, userUrl($Activity, 'Regarding'));
                $RegardingNameP = anchor($RegardingNameP, userUrl($Activity, 'Regarding'));
                $GenderSuffixCode = 'Third';
                $GenderSuffixGender = $Activity->RegardingGender;
            }
            $RegardingWallActivityPath = userUrl($Activity, 'Regarding');
            $RegardingWallLink = url($RegardingWallActivityPath);
            $RegardingWall = anchor(T('wall'), $RegardingWallActivityPath);
        }
        if ($RegardingWall == '') {
            $RegardingWall = t('wall');
        }
        if ($Activity->Route == '') {
            $ActivityRouteLink = '';
            if ($Activity->RouteCode) {
                $Route = t($Activity->RouteCode);
            } else {
                $Route = '';
            }
        } else {
            $ActivityRouteLink = url($Activity->Route);
            $Route = anchor(T($Activity->RouteCode), $Activity->Route);
        }
        // Translate the gender suffix.
        $GenderSuffixCode = "GenderSuffix.{$GenderSuffixCode}.{$GenderSuffixGender}";
        $GenderSuffix = t($GenderSuffixCode, '');
        if ($GenderSuffix == $GenderSuffixCode) {
            $GenderSuffix = '';
            // in case translate doesn't support empty strings.
        }
        /*
          Debug:
        return $ActivityName
        .'/'.$ActivityNameP
        .'/'.$RegardingName
        .'/'.$RegardingNameP
        .'/'.$RegardingWall
        .'/'.$Gender
        .'/'.$Gender2
        .'/'.$Route
        .'/'.$GenderSuffix.($GenderSuffixCode)
        */
        $FullHeadline = t("Activity.{$Activity->ActivityType}.FullHeadline", t($Activity->FullHeadline));
        $ProfileHeadline = t("Activity.{$Activity->ActivityType}.ProfileHeadline", t($Activity->ProfileHeadline));
        $MessageFormat = $ProfileUserID == $Activity->ActivityUserID || $ProfileUserID == '' || !$ProfileHeadline ? $FullHeadline : $ProfileHeadline;
        return sprintf($MessageFormat, $ActivityName, $ActivityNameP, $RegardingName, $RegardingNameP, $RegardingWall, $Gender, $Gender2, $Route, $GenderSuffix, $RegardingWallLink, $ActivityRouteLink);
    }

Usage Example

示例#1
0
 /**
  * Queue a notification for sending.
  *
  * @since 2.0.17
  * @access public
  * @param int $ActivityID
  * @param string $Story
  * @param string $Position
  * @param bool $Force
  */
 public function queueNotification($ActivityID, $Story = '', $Position = 'last', $Force = false)
 {
     $Activity = $this->getID($ActivityID);
     if (!is_object($Activity)) {
         return;
     }
     $Story = Gdn_Format::text($Story == '' ? $Activity->Story : $Story, false);
     // If this is a comment on another activity, fudge the activity a bit so that everything appears properly.
     if (is_null($Activity->RegardingUserID) && $Activity->CommentActivityID > 0) {
         $CommentActivity = $this->getID($Activity->CommentActivityID);
         $Activity->RegardingUserID = $CommentActivity->RegardingUserID;
         $Activity->Route = '/activity/item/' . $Activity->CommentActivityID;
     }
     $User = Gdn::userModel()->getID($Activity->RegardingUserID, DATASET_TYPE_OBJECT);
     //$this->SQL->select('UserID, Name, Email, Preferences')->from('User')->where('UserID', $Activity->RegardingUserID)->get()->firstRow();
     if ($User) {
         if ($Force) {
             $Preference = $Force;
         } else {
             //            $Preferences = Gdn_Format::Unserialize($User->Preferences);
             $ConfigPreference = c('Preferences.Email.' . $Activity->ActivityType, '0');
             if ($ConfigPreference !== false) {
                 $Preference = val('Email.' . $Activity->ActivityType, $User->Preferences, $ConfigPreference);
             } else {
                 $Preference = false;
             }
         }
         if ($Preference) {
             $ActivityHeadline = Gdn_Format::text(Gdn_Format::activityHeadline($Activity, $Activity->ActivityUserID, $Activity->RegardingUserID), false);
             $Email = new Gdn_Email();
             $Email->subject(sprintf(t('[%1$s] %2$s'), Gdn::config('Garden.Title'), $ActivityHeadline));
             $Email->to($User);
             $url = externalUrl(val('Route', $Activity) == '' ? '/' : val('Route', $Activity));
             $emailTemplate = $Email->getEmailTemplate()->setButton($url, t('Check it out'))->setTitle(Gdn_Format::plainText(val('Headline', $Activity)));
             if ($message = val('Story', $Activity)) {
                 $emailTemplate->setMessage($message, true);
             }
             $Email->setEmailTemplate($emailTemplate);
             if (!array_key_exists($User->UserID, $this->_NotificationQueue)) {
                 $this->_NotificationQueue[$User->UserID] = array();
             }
             $Notification = array('ActivityID' => $ActivityID, 'User' => $User, 'Email' => $Email, 'Route' => $Activity->Route, 'Story' => $Story, 'Headline' => $ActivityHeadline, 'Activity' => $Activity);
             if ($Position == 'first') {
                 $this->_NotificationQueue[$User->UserID] = array_merge(array($Notification), $this->_NotificationQueue[$User->UserID]);
             } else {
                 $this->_NotificationQueue[$User->UserID][] = $Notification;
             }
         }
     }
 }