Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Save successful and failed Tweets. Load latest failed tweet for an it…

…em on subsequent attempts. Delete failed tweets for an item once a tweet is sent for that item successfully. Code cleanup and organization updates.
  • Loading branch information...
commit 1234f8c3ff5a70f14d2a5d1e70153e36ec11ecc0 1 parent 5c3e969
Chad Kieffer authored February 13, 2011
219  3.1/modules/twitter/controllers/twitter.php
@@ -21,65 +21,6 @@
21 21
 class Twitter_Controller extends Controller {
22 22
 
23 23
   /**
24  
-   * Display Twitter status dialog.
25  
-   * @param int       $item_id
26  
-   */
27  
-  public function dialog($item_id) {
28  
-    $item = ORM::factory("item", $item_id);
29  
-    $form = twitter::get_tweet_form($item);
30  
-
31  
-    // Ensure user has permission
32  
-    access::required("view", $item);
33  
-
34  
-    $user_id = identity::active_user()->id;
35  
-    $token_is_set = $this->_is_token_set($user_id);
36  
-
37  
-    $v = new View("twitter_dialog.html");
38  
-    $v->is_registered = twitter::is_registered();
39  
-    $v->user_token_set = $token_is_set;
40  
-
41  
-    if ($token_is_set) {
42  
-      $v->type = $item->type;
43  
-      $v->title = $item->title;
44  
-      $v->description = $item->description;
45  
-      $v->form = $form;
46  
-      $v->character_count = twitter::$character_count;
47  
-    } else {
48  
-      $item_url = urlencode(url::abs_site($item->relative_url_cache));
49  
-      $v->user_id = $user_id;
50  
-      $v->twitter_auth_url = url::site("twitter/redirect?item_url=$item_url");
51  
-    }
52  
-    
53  
-    print $v;
54  
-  }
55  
-
56  
-  /**
57  
-   * Check if current user's Twitter credentials have been stored locally.
58  
-   * @param  int      $user_id
59  
-   * @return boolean
60  
-   */
61  
-  private function _is_token_set($user_id) {
62  
-    $twitter_user = $this->_get_twitter_user($user_id);
63  
-    if (!empty($twitter_user->oauth_token) && !empty($twitter_user->oauth_token_secret)) {
64  
-      return true;
65  
-    }
66  
-    return false;
67  
-  }
68  
-
69  
-  /**
70  
-   * Get Twitter credentials for the current user.
71  
-   * @param int       $user_id
72  
-   * @return mixed    object|false
73  
-   */
74  
-  private function _get_twitter_user($user_id) {
75  
-    $twitter_user = ORM::factory("twitter_user")->where("user_id", "=", $user_id)->find();
76  
-    if ($twitter_user->loaded()) {
77  
-      return $twitter_user;
78  
-    }
79  
-    return false;
80  
-  }
81  
-
82  
-  /**
83 24
    * Verify credentials and redirect based on response from Twitter.
84 25
    */
85 26
   public function callback() {
@@ -94,7 +35,7 @@ public function callback() {
94 35
     // If the oauth_token is old redirect to the connect page
95 36
     if (isset($_REQUEST['oauth_token']) && $oauth_token !== $_REQUEST['oauth_token']) {
96 37
       Session::instance()->set("twitter_oauth_status", "old_token");
97  
-      $this->clear_twitter_session();
  38
+      $this->_clear_session();
98 39
       url::redirect(url::site("twitter/redirect"));
99 40
     }
100 41
 
@@ -114,32 +55,47 @@ public function callback() {
114 55
     // If HTTP response is 200 continue otherwise send to connect page to retry
115 56
     if (200 == $connection->http_code) {
116 57
       // The user has been verified and the access tokens can be saved for future use
117  
-      $this->save_twitter_user($access_token);
  58
+      $this->save_user($access_token);
118 59
       // Redirect to the tweet form
119 60
       $item = ORM::factory("item", $item_id);
120 61
       url::redirect(url::abs_site($item_url));
121 62
     } else {
122 63
       // @todo Log HTTP status for application log and/or error message
123  
-      $this->clear_twitter_session();
  64
+      $this->_clear_session();
124 65
       url::redirect(url::site("twitter/redirect"));
125 66
     }
126 67
   }
127 68
 
128 69
   /**
129  
-   * Save or update the current user's Twitter credentials.
130  
-   * @param array     $access_token
131  
-   * @todo Ensure only one record per twitter_screen_name
  70
+   * Display Twitter status dialog.
  71
+   * @param int       $item_id
132 72
    */
133  
-  function save_twitter_user($access_token) {
134  
-    $twitter_user = ORM::factory("twitter_user");
135  
-    $twitter_user->oauth_token = $access_token["oauth_token"];
136  
-    $twitter_user->oauth_token_secret = $access_token["oauth_token_secret"];
137  
-    $twitter_user->twitter_user_id = $access_token["user_id"];
138  
-    $twitter_user->screen_name = $access_token["screen_name"];
139  
-    $twitter_user->user_id = identity::active_user()->id;
140  
-    $twitter_user->save();
  73
+  public function dialog($item_id) {
  74
+    $item = ORM::factory("item", $item_id);
  75
+    $form = twitter::get_tweet_form($item);
141 76
 
142  
-    message::success(t("Twitter access tokens saved!"));
  77
+    // Ensure user has permission
  78
+    access::required("view", $item);
  79
+
  80
+    $user_id = identity::active_user()->id;
  81
+    $token_is_set = $this->_is_token_set($user_id);
  82
+
  83
+    $v = new View("twitter_dialog.html");
  84
+    $v->is_registered = twitter::is_registered();
  85
+    $v->user_token_set = $token_is_set;
  86
+
  87
+    if ($token_is_set) {
  88
+      $v->type = $item->type;
  89
+      $v->title = $item->title;
  90
+      $v->description = $item->description;
  91
+      $v->form = $form;
  92
+      $v->character_count = twitter::$character_count;
  93
+    } else {
  94
+      $item_url = urlencode(url::abs_site($item->relative_url_cache));
  95
+      $v->user_id = $user_id;
  96
+      $v->twitter_auth_url = url::site("twitter/redirect?item_url=$item_url");
  97
+    }
  98
+    print $v;
143 99
   }
144 100
 
145 101
   /**
@@ -182,24 +138,42 @@ function redirect() {
182 138
   /**
183 139
    * Save tweets sent and those not sent because of Twitter API issues
184 140
    * @param integer  $item_id
185  
-   * @param string   $tweet  The tweet sent, or the tweet that couldn't be sent
186  
-   * @param boolean  $status   1 for success and 0 for not sent
  141
+   * @param object   $tweet     The tweet sent, or the tweet that couldn't be sent
187 142
    */
188  
-  public function save($item_id, $tweet, $status) {
189  
-    if (!empty($item_id) && !empty($tweet) && !empty($status)) {
  143
+  public function save_tweet($tweet) {
  144
+    if (!empty($tweet->item_id) && !empty($tweet->tweet) && !empty($tweet->status)) {
190 145
       $t = ORM::factory("twitter_tweet");
191 146
       $t->created = time();
192  
-      $t->item_id = $item_id;
193  
-      $t->tweet = $tweet;
194  
-      $t->status = $status;
  147
+      $t->item_id = $tweet->item_id;
  148
+      $t->twitter_id = $tweet->twitter_id;
  149
+      $t->tweet = $tweet->tweet;
  150
+      $t->status = $tweet->status;
195 151
       $t->user_id = identity::active_user()->id;
196 152
       $t->save();
197 153
     }
198 154
   }
199 155
 
200 156
   /**
  157
+   * Save or update the current user's Twitter credentials.
  158
+   * @param array     $access_token
  159
+   * @todo Ensure only one record per twitter_screen_name
  160
+   */
  161
+  function save_user($access_token) {
  162
+    $u = ORM::factory("twitter_user");
  163
+    $u->oauth_token = $access_token["oauth_token"];
  164
+    $u->oauth_token_secret = $access_token["oauth_token_secret"];
  165
+    $u->twitter_user_id = $access_token["user_id"];
  166
+    $u->screen_name = $access_token["screen_name"];
  167
+    $u->user_id = identity::active_user()->id;
  168
+    $u->save();
  169
+
  170
+    message::success(t("Twitter access tokens saved!"));
  171
+  }
  172
+
  173
+  /**
201 174
    * Post a status update to Twitter
202 175
    * @param string    $message
  176
+   * @todo Update previously failed tweet, if one exists
203 177
    */
204 178
   public function tweet($item_id) {
205 179
     access::verify_csrf();
@@ -209,7 +183,7 @@ public function tweet($item_id) {
209 183
     
210 184
     if ($form->validate()) {
211 185
       $item_url = url::abs_site($item->relative_url_cache);
212  
-      $twitter_user = $this->_get_twitter_user(identity::active_user()->id);
  186
+      $u = $this->_get_twitter_user(identity::active_user()->id);
213 187
       $consumer_key = module::get_var("twitter", "consumer_key");
214 188
       $consumer_secret = module::get_var("twitter", "consumer_secret");
215 189
 
@@ -218,38 +192,93 @@ public function tweet($item_id) {
218 192
       $connection = new TwitterOAuth(
219 193
               $consumer_key,
220 194
               $consumer_secret,
221  
-              $twitter_user->oauth_token,
222  
-              $twitter_user->oauth_token_secret);
  195
+              $u->oauth_token,
  196
+              $u->oauth_token_secret);
223 197
       
224 198
       $message = $form->twitter_message->tweet->value;
225  
-      $connection->post('statuses/update', array('status' => $message));
  199
+      $response = $connection->post('statuses/update', array('status' => $message));
226 200
 
227 201
       if (200 == $connection->http_code) {
  202
+        $status = 1;
228 203
         message::success(t("Tweet sent!"));
229  
-        //url::redirect(url::abs_site($item->relative_url_cache));
230  
-        //json::reply(array("result" => "success"));
231 204
         json::reply(array("result" => "success", "location" => $item->url()));
232  
-        $status = 1;
233 205
       } else {
234  
-        message::error(t("Unable to send Tweet. Your message has been saved. Please try again later."));
235  
-        json::reply(array("result" => "error", "location" => $item->url()));
236 206
         $status = 0;
237  
-        // @todo Log Twitter error response
  207
+        log::error("content", "Twitter", "Unable to sent tweet, response code: " . $connection->http_code);
  208
+        message::error(t("Unable to send Tweet. Your message has been saved. Please try again later."));
  209
+        json::reply(array("result" => "error", "html" => (string)$form));
238 210
       }
239  
-      $this->save($item_id, $message, $status);
  211
+      $tweet->item_id = $item_id;
  212
+      $tweet->twitter_id = $response->id;
  213
+      $tweet->tweet = $message;
  214
+      $tweet->status = $status;
  215
+
  216
+      $this->save_tweet($tweet);
  217
+      $this->_delete_failed($item_id);
240 218
       
241 219
     } else {
242  
-      echo "validation failed";
243 220
       json::reply(array("result" => "error", "html" => (string)$form));
244 221
     }
245 222
   }
246 223
 
247 224
   /**
  225
+   *
  226
+   * @param <type> $tweet 
  227
+   */
  228
+  function update_tweet($tweet) {
  229
+
  230
+  }
  231
+
  232
+  /**
248 233
    * Clear Twitter module session variables
249 234
    */
250  
-  function clear_twitter_session() {
  235
+  private function _clear_session() {
251 236
     Session::instance()->delete("twitter_oauth_token");
252 237
     Session::instance()->delete("twitter_oauth_token_secret");
253 238
     Session::instance()->delete("twitter_access_token");
254 239
   }
255  
-}
  240
+
  241
+  /**
  242
+   * Delete all failed tweets by the current user for an item
  243
+   * @param integer   $item_id
  244
+   * @todo Not implemented
  245
+   */
  246
+  private function _delete_failed($item_id) {
  247
+    if (is_numeric($item_id)) {
  248
+    $user_id = identity::active_user()->id;
  249
+      $result = db::build()
  250
+              ->delete("twitter_tweets")
  251
+              ->where("user_id", "=", $user_id)
  252
+              ->where("item_id", "=", $item_id)
  253
+              ->where("status", "=", 0)
  254
+              ->execute();
  255
+    }
  256
+  }
  257
+
  258
+  /**
  259
+   * Get Twitter credentials for the current user.
  260
+   * @param int       $user_id
  261
+   * @return mixed    object|false
  262
+   */
  263
+  private function _get_twitter_user($user_id) {
  264
+    $twitter_user = ORM::factory("twitter_user")->where("user_id", "=", $user_id)->find();
  265
+    if ($twitter_user->loaded()) {
  266
+      return $twitter_user;
  267
+    }
  268
+    return false;
  269
+  }
  270
+
  271
+  /**
  272
+   * Check if current user's Twitter credentials have been stored locally.
  273
+   * @param  int      $user_id
  274
+   * @return boolean
  275
+   */
  276
+  private function _is_token_set($user_id) {
  277
+    $twitter_user = $this->_get_twitter_user($user_id);
  278
+    if (!empty($twitter_user->oauth_token) && !empty($twitter_user->oauth_token_secret)) {
  279
+      return true;
  280
+    }
  281
+    return false;
  282
+  }
  283
+
  284
+}
31  3.1/modules/twitter/helpers/twitter.php
@@ -60,11 +60,18 @@ static function get_configure_form() {
60 60
   /**
61 61
    * Get tweet form
62 62
    * @param  object   $item
63  
-   * @return Forge 
  63
+   * @return Forge
  64
+   * @todo Load previously failed tweet for the current user for this item
64 65
    */
65 66
   static function get_tweet_form($item) {
66 67
     $long_url = url::abs_site($item->relative_url_cache);
67  
-    $default_tweet = module::get_var("twitter", "default_tweet");
  68
+    // Check for saved tweets for this user and item
  69
+    $saved_tweet = self::get_failed($item->id);
  70
+    if ($saved_tweet) {
  71
+      $default_tweet = $saved_tweet;
  72
+    } else {
  73
+      $default_tweet = module::get_var("twitter", "default_tweet");
  74
+    }
68 75
     $tweet = preg_replace("/%type/", $item->type, $default_tweet);
69 76
     $tweet = preg_replace("/%title/", $item->title, $tweet);
70 77
     $tweet = preg_replace("/%description/", $item->description, $tweet);
@@ -88,6 +95,26 @@ static function get_tweet_form($item) {
88 95
   }
89 96
 
90 97
   /**
  98
+   * Get the most recent failed tweet for an item
  99
+   * @param  integer  $item_id
  100
+   * @return mixed object|false
  101
+   * @todo Not implemented
  102
+   */
  103
+  function get_failed($item_id) {
  104
+    $user_id = identity::active_user()->id;
  105
+    $t = ORM::factory("twitter_tweet")
  106
+            ->where("item_id", "=", $item_id)
  107
+            ->where("user_id", "=", $user_id)
  108
+            ->where("status", "=", 0)
  109
+            ->find();
  110
+    if ($t->loaded()) {
  111
+      return $t->tweet;
  112
+    } else {
  113
+      return false;
  114
+    }
  115
+  }
  116
+
  117
+  /**
91 118
    * Has this Gallery been registered at dev.twitter.com/app?
92 119
    * @return boolean
93 120
    */

0 notes on commit 1234f8c

Please sign in to comment.
Something went wrong with that request. Please try again.