/**
  * Send out insight email digest for a given time period.
  * @param Owner $owner Owner to send for
  * @param str $start When to start insight lookup
  * @param str $template Email view template to use
  * @param array $options Plugin options
  * @param bool $weekly Is this a weekly email?
  * return bool Whether email was sent
  */
 private function sendDigestSinceWithTemplate($owner, $start, $template, &$options, $weekly)
 {
     $insights_dao = DAOFactory::GetDAO('InsightDAO');
     $start_time = date('Y-m-d H:i:s', strtotime($start, $this->current_timestamp));
     $insights = $insights_dao->getAllOwnerInstanceInsightsSince($owner->id, $start_time);
     $num_insights = count($insights);
     if ($num_insights == 0) {
         return false;
     }
     $config = Config::getInstance();
     $view = new ViewManager();
     $view->caching = false;
     $logger = Logger::getInstance();
     // If we've got a Mandrill key and template, send HTML
     if ($config->getValue('mandrill_api_key') != null && !empty($options['mandrill_template'])) {
         $logger->logUserInfo("Mandrill API key and template set; sending HTML", __METHOD__ . ',' . __LINE__);
         $view->assign('insights', $insights);
         $view->assign('application_url', Utils::getApplicationURL());
         $view->assign('header_text', $this->getEmailMessageHeaderText());
         if (Utils::isThinkUpLLC()) {
             $logger->logUserInfo("Email via ThinkUpLLC, process welcome / free trial messaging", __METHOD__ . ',' . __LINE__);
             $thinkupllc_endpoint = $config->getValue('thinkupllc_endpoint');
             $view->assign('thinkupllc_endpoint', $thinkupllc_endpoint);
             $view->assign('unsub_url', $thinkupllc_endpoint . 'settings.php');
             $view->assign('install_folder', $config->getValue('install_folder'));
             if (!isset($options['last_daily_email'])) {
                 $logger->logUserInfo("No daily email ever sent before, include welcome message", __METHOD__ . ',' . __LINE__);
                 $view->assign('show_welcome_message', true);
             } else {
                 if ($owner->is_free_trial) {
                     $logger->logUserInfo("Owner is in free trial", __METHOD__ . ',' . __LINE__);
                     $creation_date = new DateTime($owner->joined);
                     $now = new DateTime();
                     $end_of_trial = $creation_date->add(new DateInterval('P15D'));
                     if ($end_of_trial >= $now) {
                         $interval = $now->diff($end_of_trial);
                         $days_left = $interval->format('%a');
                         if ($days_left > 2) {
                             $view->assign('pay_prompt_headline', $days_left . ' days left in your free trial!');
                         } elseif ($days_left == 0) {
                             //Last day
                             $view->assign('pay_prompt_headline', 'Last chance!');
                         } else {
                             //Show hours if it's 24 or 48 hours
                             $view->assign('pay_prompt_headline', 'Only ' . $days_left * 24 . ' hours left!');
                         }
                         $explainer_copy_options = array("Your free trial expires today. Don't lose any of your insights!", "It's time to become a member. We'd love to have you.", "It's just " . ($owner->membership_level == 'Member' ? '16' : '32') . " cents a day to get smarter about the time you spend online.", "Isn't this better than boring \"analytics\"?", "Just wait 'til you see ThinkUp next week.", "We never sell your data and we don't show you ads.", "Get our exclusive book on the future of social media for free.", "ThinkUp gives you social network superpowers.", "The longer you use ThinkUp, the smarter it gets.", "ThinkUp helps you be more thoughtful about your time online.", "ThinkUp works in email, on the web, and on all your devices.", "ThinkUp members can cancel at any time—with no hassles.", 'Wait until you see what ThinkUp has in store tomorrow.', "Your morning ThinkUp email will make your day.");
                         $view->assign('pay_prompt_explainer', $explainer_copy_options[$days_left]);
                         if ($owner->membership_level == 'Member') {
                             $view->assign('pay_prompt_button_label', 'Just $5/month');
                         } elseif ($owner->membership_level == 'Pro') {
                             $view->assign('pay_prompt_button_label', 'Just $10/month');
                         }
                     }
                 } else {
                     //Check subscription status and show a message if Payment failed or due
                     $logger->logUserInfo("User is not in free trial; check subscription status", __METHOD__ . ',' . __LINE__);
                     $thinkupllc_api_accessor = new ThinkUpLLCAPIAccessor();
                     $membership_details = $thinkupllc_api_accessor->getSubscriptionStatus($owner->email);
                     $logger->logUserInfo("Subscription status is " . Utils::varDumpToString($membership_details), __METHOD__ . ',' . __LINE__);
                     if (isset($membership_details->subscription_status) && ($membership_details->subscription_status == 'Payment failed' || $membership_details->subscription_status == 'Payment due')) {
                         $logger->logUserInfo("Owner has payment failure; include alert in email", __METHOD__ . ',' . __LINE__);
                         $payment_failed_copy = array();
                         if ($membership_details->subscription_status == 'Payment failed') {
                             $payment_failed_copy[] = array('headline' => 'Oops! Your account needs attention', 'explainer' => "We had a problem processing your membership payment. " . "But it's easy to fix.");
                             $payment_failed_copy[] = array('headline' => 'Uh oh, problem with your subscription...', 'explainer' => "There was a problem processing your membership payment. " . "To fix it, update your payment info.");
                             $payment_failed_copy[] = array('headline' => 'Your ThinkUp subscription is out of date...', 'explainer' => "We tried to charge your Amazon account for your ThinkUp membership," . " and there was an error. But it's easy to fix.");
                             $payment_failed_copy[] = array('headline' => 'Action required to keep your ThinkUp account active', 'explainer' => "We weren't able to process your last membership payment—maybe your " . "info is out of date? Fixing it just takes a moment.");
                             $payment_failed_copy[] = array('headline' => "Urgent! Keep your ThinkUp account active", 'explainer' => "We tried to process your ThinkUp subscription, but " . "the payment was not successful. Please update your payment information " . "now to make sure your ThinkUp membership stays in good standing.");
                             $payment_failed_button_label = "Update your payment info";
                         } else {
                             //Payment due
                             $payment_failed_copy[] = array('headline' => 'Oops! Your ThinkUp membership needs attention', 'explainer' => "Looks like you've got a membership payment due. " . "But it's easy to make it.");
                             $payment_failed_copy[] = array('headline' => "Uh-oh, don't let your subscription expire", 'explainer' => "Looks like your ThinkUp payment is due. " . "Make it now so your account doesn't expire.");
                             $payment_failed_copy[] = array('headline' => 'Hey, your ThinkUp subscription payment is due', 'explainer' => "Your membership payment isn't up-to-date. " . " But it's easy to fix.");
                             $payment_failed_copy[] = array('headline' => 'Action required: Keep your ThinkUp account active', 'explainer' => "Looks like you've got a membership payment due. " . "Making it just takes a moment.");
                             $payment_failed_copy[] = array('headline' => "Urgent! Keep your ThinkUp account active", 'explainer' => "Your subscription payment is due. " . "Please update your information now to keep your ThinkUp membership " . "in good standing.");
                             $payment_failed_button_label = "Make your payment";
                         }
                         $copy_index = TimeHelper::getDayOfYear() % count($payment_failed_copy);
                         $payment_failed_headline = $payment_failed_copy[$copy_index]['headline'];
                         $payment_failed_explainer = $payment_failed_copy[$copy_index]['explainer'];
                         $view->assign('payment_failed_headline', $payment_failed_headline);
                         $view->assign('payment_failed_explainer', $payment_failed_explainer);
                         $view->assign('payment_failed_button_label', $payment_failed_button_label);
                     }
                 }
             }
             $thinkupllc_email_tout = $config->getValue('thinkupllc_email_tout');
             if (isset($thinkupllc_email_tout)) {
                 $view->assign('thinkupllc_email_tout', $thinkupllc_email_tout);
             }
         } else {
             $logger->logUserInfo("Email is NOT via ThinkUpLLC", __METHOD__ . ',' . __LINE__);
             $view->assign('unsub_url', Utils::getApplicationURL() . 'account/index.php?m=manage#instances');
         }
         // It's a weekly digest if we're going back more than a day or two.
         $daily_or_weekly = $weekly ? 'Weekly' : 'Daily';
         $view->assign('weekly_or_daily', $daily_or_weekly);
         $view->assign('pay_prompt_url', $config->getValue('thinkupllc_endpoint') . 'membership.php');
         if ($config->getValue('image_proxy_enabled') == true) {
             $view->assign('image_proxy_sig', $config->getValue('image_proxy_sig'));
         }
         $insights_markup = $view->fetch(Utils::getPluginViewDirectory($this->folder_name) . '_email.insights_html.tpl');
         $parameters = array();
         $parameters['insights'] = $insights_markup;
         $parameters['app_title'] = $config->getValue('app_title_prefix') . "ThinkUp";
         $parameters['application_url'] = Utils::getApplicationURL();
         $parameters['weekly_or_daily'] = $daily_or_weekly;
         try {
             if (!isset($options['last_daily_email'])) {
                 $subject_line = "Welcome to ThinkUp! Here are your insights.";
             } else {
                 $subject_line = $this->getEmailMessageSubjectLine($daily_or_weekly, $insights);
             }
             $logger->logUserInfo("About to call Mailer::mailHTMLViaMandrillTemplate", __METHOD__ . ',' . __LINE__);
             Mailer::mailHTMLViaMandrillTemplate($owner->email, $subject_line, $options['mandrill_template']->option_value, $parameters);
             return true;
         } catch (Mandrill_Unknown_Template $e) {
             // In this case, we'll fall back to plain text sending and warn the user in the log
             $logger->logUserError("Invalid mandrill template configured:" . $options['mandrill_template']->option_value . ".", __METHOD__ . ',' . __LINE__);
             unset($options['mandrill_template']);
         }
     } else {
         if ($config->getValue('mandrill_api_key') == null) {
             $logger->logUserInfo("Mandrill API key is null", __METHOD__ . ',' . __LINE__);
         }
         if (empty($options['mandrill_template'])) {
             $logger->logUserInfo("Mandrill template is not set", __METHOD__ . ',' . __LINE__);
         }
     }
     $view->assign('app_title', $config->getValue('app_title_prefix') . "ThinkUp");
     $view->assign('application_url', Utils::getApplicationURL());
     $view->assign('insights', $insights);
     $message = $view->fetch(Utils::getPluginViewDirectory($this->folder_name) . $template);
     list($subject, $message) = explode("\n", $message, 2);
     $logger->logUserInfo("About to call Mailer::mail", __METHOD__ . ',' . __LINE__);
     Mailer::mail($owner->email, $subject, $message);
     return true;
 }
Пример #2
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating profile change insight", __METHOD__ . ',' . __LINE__);
     //Bio changes
     if (($instance->network == 'twitter' || $instance->network == 'instagram') && $this->shouldGenerateInsight($this->slug_bio, $instance)) {
         $this->logger->logInfo("Should generate bio change tracker", __METHOD__ . ',' . __LINE__);
         $user_versions_dao = DAOFactory::getDAO('UserVersionsDAO');
         $versions = $user_versions_dao->getRecentFriendsVersions($user, 7, array('description'));
         //$this->logger->logInfo(Utils::varDumpToString($versions), __METHOD__.','.__LINE__);
         $changes = array();
         $examined_users = array();
         foreach ($versions as $change) {
             $user_key = intval($change['user_key']);
             if (!in_array($user_key, $examined_users)) {
                 $examined_users[] = $user_key;
                 $last_description = $user_versions_dao->getVersionBeforeDay($user_key, date('Y-m-d'), 'description');
                 if ($last_description) {
                     $user_dao = DAOFactory::getDAO('UserDAO');
                     $user = $user_dao->getDetailsByUserKey($user_key);
                     if ($user && Utils::stripURLsOutOfText($user->description) !== Utils::stripURLsOutOfText($last_description['field_value'])) {
                         $changes[] = array('user' => $user, 'field_name' => 'description', 'field_description' => 'bio', 'before' => $last_description['field_value'], 'after' => $user->description);
                     }
                 }
             }
         }
         $this->logger->logInfo("Got " . count($changes) . " bio changes", __METHOD__ . ',' . __LINE__);
         if (count($changes) > 0) {
             $changes = array_slice($changes, 0, 10);
             $insight = new Insight();
             $insight->instance_id = $instance->id;
             $insight->slug = $this->slug_bio;
             $insight->date = $this->insight_date;
             $insight->filename = basename(__FILE__, ".php");
             $insight->emphasis = Insight::EMPHASIS_MED;
             $insight->related_data = array('changes' => $changes);
             $insight->text = $this->getTextBioChange($changes, $instance);
             $insight->headline = $this->getHeadlineBioChange($changes, $instance);
             if (count($changes) == 1) {
                 $insight->header_image = $changes[0]["user"]->avatar;
             }
             $this->insight_dao->insertInsight($insight);
         }
     }
     //Avatar changes
     if (($instance->network == 'twitter' || $instance->network == 'instagram') && $this->shouldGenerateInsight($this->slug_avatar, $instance)) {
         $this->logger->logInfo("Should generate avatar change tracker", __METHOD__ . ',' . __LINE__);
         $user_versions_dao = DAOFactory::getDAO('UserVersionsDAO');
         $versions = $user_versions_dao->getRecentFriendsVersions($user, 7, array('avatar'));
         //$this->logger->logInfo(Utils::varDumpToString($versions), __METHOD__.','.__LINE__);
         $changes = array();
         $examined_users = array();
         $user_dao = DAOFactory::getDAO('UserDAO');
         foreach ($versions as $change) {
             $user_key = intval($change['user_key']);
             if (!in_array($user_key, $examined_users)) {
                 $examined_users[] = $user_key;
                 $last_version = $user_versions_dao->getVersionBeforeDay($user_key, date('Y-m-d'), 'avatar');
                 if ($last_version) {
                     $user = $user_dao->getDetailsByUserKey($user_key);
                     if ($user && $user->avatar !== $last_version['field_value']) {
                         $do_show_change = true;
                         //Extra check for ThinkUp LLC users
                         if (Utils::isThinkUpLLC()) {
                             $api_accessor = new ThinkUpLLCAPIAccessor();
                             $avatar_url1_https = preg_replace('/^http:(.+)$/', "https:\$1", $user->avatar);
                             $avatar_url2_https = preg_replace('/^http:(.+)$/', "https:\$1", $last_version['field_value']);
                             if ($instance->network == 'twitter') {
                                 //Get the original version of the avatar
                                 //https://dev.twitter.com/overview/general/user-profile-images-and-banners
                                 $avatar_url1_https = str_replace('_normal', '', $avatar_url1_https);
                                 $avatar_url2_https = str_replace('_normal', '', $avatar_url2_https);
                             }
                             $do_show_change = $api_accessor->didAvatarsChange($avatar_url1_https, $avatar_url2_https);
                             if (!$do_show_change) {
                                 $this->logger->logInfo("Skipping change for " . $avatar_url1_https . " and " . $avatar_url2_https, __METHOD__ . ',' . __LINE__);
                             }
                         }
                         if ($do_show_change) {
                             $changes[] = array('user' => $user, 'field_name' => 'avatar', 'field_description' => 'avatar', 'before' => $last_version['field_value'], 'after' => $user->avatar);
                         }
                     }
                 }
             }
         }
         $this->logger->logInfo("Got " . count($changes) . " avatar changes", __METHOD__ . ',' . __LINE__);
         if (count($changes) > 0) {
             $changes = array_slice($changes, 0, 10);
             $insight = new Insight();
             $insight->instance_id = $instance->id;
             $insight->slug = $this->slug_avatar;
             $insight->date = $this->insight_date;
             $insight->filename = basename(__FILE__, ".php");
             $insight->emphasis = Insight::EMPHASIS_MED;
             $insight->related_data = array('changes' => $changes);
             $insight->text = $this->getTextAvatarChange($changes, $instance);
             $insight->headline = $this->getHeadlineAvatarChange($changes, $instance);
             $this->insight_dao->insertInsight($insight);
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }