/**
  * @param integer       $access_token_id
  * @param string        $type
  * @param integer       $value
  * @param null|string   $date - date formatted in 'Y-m-d' format.
  */
 public static function updateAnalytics($access_token_id, $type, $value, $date = null)
 {
     if (!$date) {
         $now = new DateTime('UTC');
         $date = $now->format('Y-m-d');
     }
     $social_analytics = Social_analytics::getAnalytics($access_token_id, $type, $date);
     if (!$social_analytics->value) {
         $social_analytics->value = 0;
     }
     $social_analytics->value += (int) $value;
     $social_analytics->access_token_id = $access_token_id;
     $social_analytics->type = $type;
     $social_analytics->date = $date;
     $social_analytics->save();
 }
Esempio n. 2
0
 /**
  * Take arguments for running
  *
  * This method is called first, and it lets the action class get
  * all its arguments and validate them. It's also the time
  * to fetch any relevant data from the database.
  *
  * Action classes should run parent::prepare($args) as the first
  * line of this method to make sure the default argument-processing
  * happens.
  *
  * @param array $args $_REQUEST args
  *
  * @return boolean success flag
  */
 function prepare($args)
 {
     parent::prepare($args);
     if (!common_logged_in()) {
         // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
         $this->clientError(_('Not logged in.'));
         return;
     } else {
         if (!common_is_real_login()) {
             // Cookie theft means that automatic logins can't
             // change important settings or see private info, and
             // _all_ our settings are important
             common_set_returnto($this->selfUrl());
             $user = common_current_user();
             if (Event::handle('RedirectToLogin', array($this, $user))) {
                 common_redirect(common_local_url('login'), 303);
             }
         } else {
             $this->user = common_current_user();
             $sdate = !isset($_REQUEST['sdate']) ? new DateTime('first day of this month') : new DateTime($_REQUEST['sdate']);
             $edate = !isset($_REQUEST['edate']) ? new DateTime('last day of this month') : new DateTime($_REQUEST['edate']);
             // Custom date range
             $this->sa = Social_analytics::init($this->user->id, $sdate, $edate);
         }
     }
     return true;
 }
Esempio n. 3
0
 /**
  * Unfollow those who unsubscribed from your account in Twitter.
  *
  * @access public
  * @param $args
  */
 public function unfollowUnsubscribedUsers($args)
 {
     $user_id = (int) $args['user_id'];
     $user = new User($user_id);
     $access_token_id = $args['id'];
     if (!$user->ifUserHasConfigValue('auto_unfollow', $access_token_id)) {
         return;
     }
     $days_before_unfollow = $user->ifUserHasConfigValue('days_before_unfollow', $access_token_id);
     if (!$days_before_unfollow) {
         $days_before_unfollow = 3;
     }
     $date = new DateTime('- ' . $days_before_unfollow . ' days UTC');
     $twitter = $this->inicializeTwitterSocializer($user_id, $args);
     $new_unfollowing_count = 0;
     //        $followersIds = $twitter->get_friends();
     //        foreach($followersIds->ids as $followerId) {
     //            If(!$user->isUserHasTwitterFollower($followerId, $access_token_id)) {
     //                $answer = $twitter->unfollow($followerId);
     //                if ($answer->errors) {
     //                    foreach($answer->errors as $err) {
     //                        log_message('TASK_ERROR', __FUNCTION__ . 'Twitter error: code: '.$err->code.'. Message: ' . $err->message);
     //                    }
     //                } else {
     //                    $new_unfollowing_count++;
     //                }
     //            }
     //        }
     /* @var Twitter_follower[] $followers */
     $followers = $user->twitter_follower->where('still_follow', false)->where('access_token_id', $access_token_id)->where('unfollow_time < ' . $date->getTimestamp())->get();
     foreach ($followers as $follower) {
         $answer = $twitter->unfollow($follower->follower_id);
         if ($answer->errors) {
             foreach ($answer->errors as $err) {
                 log_message('TASK_ERROR', __FUNCTION__ . 'Twitter error: code: ' . $err->code . '. Message: ' . $err->message);
             }
         } else {
             $follower->delete();
             $new_unfollowing_count++;
         }
     }
     Social_analytics::updateAnalytics($access_token_id, Social_analytics::NEW_UNFOLLOWING_ANALYTICS_TYPE, $new_unfollowing_count);
 }