diff --git a/code/PostToTwitter.php b/code/PostToTwitter.php index 7c13b10..dc5f158 100755 --- a/code/PostToTwitter.php +++ b/code/PostToTwitter.php @@ -3,79 +3,88 @@ /** * class to send message to Twitter. */ -class PostToTwitter extends Controller { - - private static $twitter_consumer_key; - - private static $twitter_consumer_secret; - - private static $twitter_access_token; - - private static $twitter_access_token_secret; - - public static function set_twitter_consumer_key($key) { - self::$twitter_consumer_key = $key; - } - - protected static function get_twitter_consumer_key() { - return self::$twitter_consumer_key; - } - - public static function set_twitter_consumer_secret($key) { - self::$twitter_consumer_secret = $key; - } - - protected static function get_twitter_consumer_secret() { - return self::$twitter_consumer_secret; - } - - public static function set_twitter_access_token($key) { - self::$twitter_access_token = $key; - } - - protected static function get_twitter_access_token() { - return self::$twitter_access_token; - } - - public static function set_twitter_access_token_secret($key) { - self::$twitter_access_token_secret = $key; - } - - protected static function get_twitter_access_token_secret() { - return self::$twitter_access_token_secret; - } - - public function __construct($twitterAccount = null) { - if($twitterAccount) { - self::set_twitter_consumer_key($twitterAccount->ConsumerKey); - self::set_twitter_consumer_secret($twitterAccount->ConsumerSecret); - self::set_twitter_access_token($twitterAccount->AccessToken); - self::set_twitter_access_token_secret($twitterAccount->TokenSecret); - } +class PostToTwitter extends Controller +{ + + private static $twitter_consumer_key; + + private static $twitter_consumer_secret; + + private static $twitter_access_token; + + private static $twitter_access_token_secret; + + public static function set_twitter_consumer_key($key) + { + self::$twitter_consumer_key = $key; + } + + protected static function get_twitter_consumer_key() + { + return self::$twitter_consumer_key; + } + + public static function set_twitter_consumer_secret($key) + { + self::$twitter_consumer_secret = $key; + } + + protected static function get_twitter_consumer_secret() + { + return self::$twitter_consumer_secret; + } + + public static function set_twitter_access_token($key) + { + self::$twitter_access_token = $key; + } + + protected static function get_twitter_access_token() + { + return self::$twitter_access_token; + } + + public static function set_twitter_access_token_secret($key) + { + self::$twitter_access_token_secret = $key; + } + + protected static function get_twitter_access_token_secret() + { + return self::$twitter_access_token_secret; } - - static function ready_to_tweet() { - return ((self::$twitter_consumer_key && self::$twitter_consumer_secret && self::$twitter_access_token && self::$twitter_access_token_secret) || (TwitterAccount::twitter_accounts_set())); - } - - function postStatus($message = null){ - if(!self::ready_to_tweet() && $message) { - return false; - } + public function __construct($twitterAccount = null) + { + if ($twitterAccount) { + self::set_twitter_consumer_key($twitterAccount->ConsumerKey); + self::set_twitter_consumer_secret($twitterAccount->ConsumerSecret); + self::set_twitter_access_token($twitterAccount->AccessToken); + self::set_twitter_access_token_secret($twitterAccount->TokenSecret); + } + } + + public static function ready_to_tweet() + { + return ((self::$twitter_consumer_key && self::$twitter_consumer_secret && self::$twitter_access_token && self::$twitter_access_token_secret) || (TwitterAccount::twitter_accounts_set())); + } + + public function postStatus($message = null) + { + if (!self::ready_to_tweet() && $message) { + return false; + } - // create instance - $twitter = new Twitter(self::$twitter_consumer_key, self::$twitter_consumer_secret); + // create instance + $twitter = new Twitter(self::$twitter_consumer_key, self::$twitter_consumer_secret); - // set tokens - $twitter->setOAuthToken(self::$twitter_access_token); - $twitter->setOAuthTokenSecret(self::$twitter_access_token_secret); + // set tokens + $twitter->setOAuthToken(self::$twitter_access_token); + $twitter->setOAuthTokenSecret(self::$twitter_access_token_secret); - // make sure message is 140 chars and send message - $message = substr(strip_tags($message),0,140); - $response = $twitter->statusesUpdate($message); - return $response; - } + // make sure message is 140 chars and send message + $message = substr(strip_tags($message), 0, 140); + $response = $twitter->statusesUpdate($message); + return $response; + } } - -?> \ No newline at end of file diff --git a/code/TwitterExtension.php b/code/TwitterExtension.php index a20d824..2dc367b 100644 --- a/code/TwitterExtension.php +++ b/code/TwitterExtension.php @@ -1,79 +1,92 @@ "Varchar(255)", - "LastTweet" => "Varchar(255)" - ); + public static $db = array( + "LastPostedToTwitter" => "Varchar(255)", + "LastTweet" => "Varchar(255)" + ); - static $has_one = array( - 'Account' => 'TwitterAccount' - ); + public static $has_one = array( + 'Account' => 'TwitterAccount' + ); - - function updateCMSFields(FieldList $fields) { - $tabName = 'Root.SocialMedia'; - $fields->addFieldToTab($tabName, new HeaderField('TwitterHeader', 'Twitter', 4)); - // if(TwitterAccount::twitter_accounts_set()) { - // $twitterAccounts = DataObject::get('TwitterAccount','', '"AccountName" ASC'); - // $fields->addFieldToTab($tabName, new DropdownField('twitterAccount', 'Twitter Account', $twitterAccounts->map('ID', 'AccountName'))); - // } - if(!PostToTwitter::ready_to_tweet()) - $fields->addFieldToTab($tabName, new LiteralField('NotGoodToTweet', '
ATTENTION: This will NOT make it to Twitter, you need to set your public and private keys, please see module documentation or http://dev.twitter.com/pages/auth
')); - $fields->addFieldToTab($tabName, DropdownField::create('AccountID', 'Twitter Account', TwitterAccount::get()->map('ID', 'AccountName'))->setEmptyString(' ')); - //'GalleryID', 'Gallery', Gallery::get()->map('ID', 'Title') - $fields->addFieldToTab($tabName, new CheckboxField('PostToTwitter', 'Post to Twitter')); - $fields->addFieldToTab($tabName, new ReadonlyField('LastPostedToTwitter', 'Last Posted To Twitter')); - $fields->addFieldToTab($tabName, new ReadonlyField('LastTweet','Last Tweet Content')); - - } - - public $PostToTwitter = false; - - function setPostToTwitter($value) { $this->PostToTwitter = $value; } - - public static $twitterField = array(); - - function getTwitterField() { - foreach(self::$twitterField as $k => $v) { - if($this->owner instanceof $k) { - if(is_array($v)) { - $message = ''; - foreach($v as $value) { - if($this->owner->$value) $message .= $this->owner->$value . " "; - elseif(method_exists($this->owner,$value)) $message .= $this->owner->$value() . " "; - } - return $message; - } - else return $this->owner->$v; - break; - } - } - } - - static function set_twitter_fields($fields = array()) { - self::$twitterField = $fields; - } - - - function onBeforeWrite(){ - parent::onBeforeWrite(); - - if($this->getTwitterField() && $this->PostToTwitter) { - $this->owner->LastPostedToTwitter = date('d/m/Y g:ia'); - $this->owner->LastTweet = $this->getTwitterField(); - } - } - - function onAfterWrite(){ - parent::onAfterWrite(); - if($this->getTwitterField() && $this->PostToTwitter && $this->owner->AccountID) { - $message = $this->getTwitterField(); - $twitter = new PostToTwitter($this->owner->Account()); - $resp = $twitter->postStatus($message); - } - } -} \ No newline at end of file + + public function updateCMSFields(FieldList $fields) + { + $tabName = 'Root.SocialMedia'; + $fields->addFieldToTab($tabName, new HeaderField('TwitterHeader', 'Twitter', 4)); + // if(TwitterAccount::twitter_accounts_set()) { + // $twitterAccounts = DataObject::get('TwitterAccount','', '"AccountName" ASC'); + // $fields->addFieldToTab($tabName, new DropdownField('twitterAccount', 'Twitter Account', $twitterAccounts->map('ID', 'AccountName'))); + // } + if (!PostToTwitter::ready_to_tweet()) { + $fields->addFieldToTab($tabName, new LiteralField('NotGoodToTweet', 'ATTENTION: This will NOT make it to Twitter, you need to set your public and private keys, please see module documentation or http://dev.twitter.com/pages/auth
')); + } + $fields->addFieldToTab($tabName, DropdownField::create('AccountID', 'Twitter Account', TwitterAccount::get()->map('ID', 'AccountName'))->setEmptyString(' ')); + //'GalleryID', 'Gallery', Gallery::get()->map('ID', 'Title') + $fields->addFieldToTab($tabName, new CheckboxField('PostToTwitter', 'Post to Twitter')); + $fields->addFieldToTab($tabName, new ReadonlyField('LastPostedToTwitter', 'Last Posted To Twitter')); + $fields->addFieldToTab($tabName, new ReadonlyField('LastTweet', 'Last Tweet Content')); + } + + public $PostToTwitter = false; + + public function setPostToTwitter($value) + { + $this->PostToTwitter = $value; + } + + public static $twitterField = array(); + + public function getTwitterField() + { + foreach (self::$twitterField as $k => $v) { + if ($this->owner instanceof $k) { + if (is_array($v)) { + $message = ''; + foreach ($v as $value) { + if ($this->owner->$value) { + $message .= $this->owner->$value . " "; + } elseif (method_exists($this->owner, $value)) { + $message .= $this->owner->$value() . " "; + } + } + return $message; + } else { + return $this->owner->$v; + } + break; + } + } + } + + public static function set_twitter_fields($fields = array()) + { + self::$twitterField = $fields; + } + + + public function onBeforeWrite() + { + parent::onBeforeWrite(); + + if ($this->getTwitterField() && $this->PostToTwitter) { + $this->owner->LastPostedToTwitter = date('d/m/Y g:ia'); + $this->owner->LastTweet = $this->getTwitterField(); + } + } + + public function onAfterWrite() + { + parent::onAfterWrite(); + if ($this->getTwitterField() && $this->PostToTwitter && $this->owner->AccountID) { + $message = $this->getTwitterField(); + $twitter = new PostToTwitter($this->owner->Account()); + $resp = $twitter->postStatus($message); + } + } +} diff --git a/code/admin/TwitterAdmin.php b/code/admin/TwitterAdmin.php index b8cbc7f..69fa2b3 100644 --- a/code/admin/TwitterAdmin.php +++ b/code/admin/TwitterAdmin.php @@ -1,12 +1,13 @@ 'Varchar(255)', - 'ConsumerSecret' => 'Varchar(255)', - 'AccessToken' => 'Varchar(255)', - 'ConsumerKey' => 'Varchar(255)', - 'TokenSecret' => 'Varchar(255)' - ); + public static $db = array( + 'AccountName' => 'Varchar(255)', + 'ConsumerSecret' => 'Varchar(255)', + 'AccessToken' => 'Varchar(255)', + 'ConsumerKey' => 'Varchar(255)', + 'TokenSecret' => 'Varchar(255)' + ); - static function twitter_accounts_set() { - return DataObject::get('TwitterAccount')->Count(); - } + public static function twitter_accounts_set() + { + return DataObject::get('TwitterAccount')->Count(); + } - static $summary_fields = array( - 'AccountName' - ); -} \ No newline at end of file + public static $summary_fields = array( + 'AccountName' + ); +}