Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to PSR-2 #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 79 additions & 70 deletions code/PostToTwitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

?>
159 changes: 86 additions & 73 deletions code/TwitterExtension.php
Original file line number Diff line number Diff line change
@@ -1,79 +1,92 @@
<?php

class TwitterExtension extends DataExtension {

class TwitterExtension extends DataExtension
{


static $db = array(
"LastPostedToTwitter" => "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', '<p>ATTENTION: This will NOT make it to Twitter, you need to set your public and private keys, please see module documentation or <a href="http://dev.twitter.com/pages/auth" target="_blank">http://dev.twitter.com/pages/auth</a></p>'));
$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);
}
}
}

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', '<p>ATTENTION: This will NOT make it to Twitter, you need to set your public and private keys, please see module documentation or <a href="http://dev.twitter.com/pages/auth" target="_blank">http://dev.twitter.com/pages/auth</a></p>'));
}
$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);
}
}
}
15 changes: 8 additions & 7 deletions code/admin/TwitterAdmin.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

class TwitterAdmin extends ModelAdmin {
class TwitterAdmin extends ModelAdmin
{

static $url_segment = 'social-media';
public static $url_segment = 'social-media';

static $menu_title = 'Social Media';
public static $menu_title = 'Social Media';

static $managed_models = array(
'TwitterAccount'
);
}
public static $managed_models = array(
'TwitterAccount'
);
}
32 changes: 17 additions & 15 deletions code/model/TwitterAccount.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
<?php

class TwitterAccount extends DataObject {
class TwitterAccount extends DataObject
{

static $db = array(
'AccountName' => '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'
);
}
public static $summary_fields = array(
'AccountName'
);
}