Skip to content

Instantly share code, notes, and snippets.

@LukeXF
Created August 29, 2015 23:18
Show Gist options
  • Save LukeXF/a39c7489fda61dd41cc3 to your computer and use it in GitHub Desktop.
Save LukeXF/a39c7489fda61dd41cc3 to your computer and use it in GitHub Desktop.
<?php
// Handles the site functions
class Accounts extends siteFunctions
{
/**
* load all Instagram accounts that belong to the logged in user
*
* @param integer session id
* @return array PDO array of accounts
**/
public function getInstagramAccounts($user_id)
{
// if database connection opened
if ($this->databaseConnection()) {
$sql = $this->db_connection->prepare("SELECT * FROM `instagram_accounts` WHERE `instagram_site_id` = :user_id");
$sql->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$sql->execute();
$widgets = $sql->fetchAll();
if ($sql->rowCount() > 0) {
return $widgets;
} else {
return false;
}
} else {
return false;
}
}
/**
* check that the Instagram account about to be save doesn't already exist
* inside of the instagram_accounts table
*
* @param array Instagram array
* @return array callbackMessage
**/
public function saveInstagramAccount($data) {
return $this->checkForExistingInstagram($data);
}
/**
* check for an existing Instagram account ID already signed into the system
*
* @param array Instagram array
* @return array callbackMessage
**/
private function checkForExistingInstagram($data) {
global $_SESSION;
if ($this->databaseConnection()) {
// load widgets for the user
$sql = $this->db_connection->prepare("SELECT `instagram_site_id` FROM `instagram_accounts` WHERE `instagram_instagram_id` = :instagram_id");
$sql->bindValue(':instagram_id', $data->user->id, PDO::PARAM_INT);
$sql->execute();
$return = $sql->fetchAll();
// if there is an Instagram account linked to Nuuke.com
if ($sql->rowCount() > 0) {
// see if it is linked to current user or another user for callbackMessage
if ($return[0]['instagram_site_id'] == $_SESSION['user_id']) {
return $this->callbackMessage("The Instagram account (" . $data->user->username . ") that you are trying to link is already active on your account. Please open <a href='https://instagram.com/' target='_blank'>Instagram</a> in another tab, sign out and log into the desired account then try again. Click <a href='https://instagram.com/accounts/logout/' target='_blank'>here</a> to log out of your Instagram account on their site.", "danger");
} else {
$user = $this->getUserDatafromID($return[0]['instagram_site_id']);
return $this->callbackMessage("User " . $user . " is already using that Instagram account (" . $data->user->username . ") on this site.", "danger");
}
} else {
$this->insertInstagramAccount($data);
}
} else {
return $this->callbackMessage("Database connection open", "danger");
}
}
/**
* after authenticating with Instagram, save to database
*
* @param array Instagram array
* @return array callbackMessage
**/
private function insertInstagramAccount($data) {
global $_SESSION;
if ($this->databaseConnection()) {
// if the password is invalid
if(empty($data->user->username)){
return $this->callbackMessage("The response from Instagram was invalid due. Please contact support if this problem persists.", "danger");
} else {
$sql = $this->db_connection->prepare('INSERT INTO instagram_accounts (instagram_user, instagram_site_id, instagram_name, instagram_picture, instagram_bio, instagram_website, instagram_instagram_id, instagram_access_token) VALUES (:instagram_user, :instagram_site_id, :instagram_name, :instagram_picture, :instagram_bio, :instagram_website, :instagram_instagram_id, :instagram_access_token)');
$sql->bindValue(':instagram_user', $data->user->username , PDO::PARAM_STR);
$sql->bindValue(':instagram_site_id', $_SESSION['user_id'] , PDO::PARAM_INT);
$sql->bindValue(':instagram_name', $data->user->full_name , PDO::PARAM_STR);
$sql->bindValue(':instagram_bio', $data->user->bio , PDO::PARAM_STR);
$sql->bindValue(':instagram_website', $data->user->website , PDO::PARAM_STR);
$sql->bindValue(':instagram_instagram_id', $data->user->id , PDO::PARAM_INT);
$sql->bindValue(':instagram_picture', $data->user->profile_picture , PDO::PARAM_STR);
$sql->bindValue(':instagram_access_token', $data->access_token , PDO::PARAM_STR);
// execute the Instagram save and check response
if ($sql->execute()) {
return $this->callbackMessage("Congratulations! The Instagram account " . $data->user->username . " has been successful synced to your Nuuke account.", "success");
} else {
return $this->callbackMessage("ERROR: " . $sql->errorCode() . ", please contact support." , "danger");
}
}
} else {
return $this->callbackMessage("Database connection open", "danger");
}
}
/**
* build navbar account switching for Instagram accounts belonging
* to the user that is signed in.
*
**/
public function dropdownListInstagramAccounts(){
global $_SESSION;
$accounts = $this->getInstagramAccounts($_SESSION['user_id']);
for ($i=0; $i < count($accounts); $i++) {
echo "
<li class='account-switch'>
<a href='processing?make_default=" . $accounts[$i]['instagram_id'] . "'>
<img src='" . $accounts[$i]['instagram_picture'] . "'>
" . $accounts[$i]['instagram_user'] . "
</a>
</li>
";
}
}
/**
* on the accounts page build the table of Instagram account belonging
* to the user that is signed in.
*
**/
public function listInstagramAccounts(){
global $_SESSION;
$accounts = $this->getInstagramAccounts($_SESSION['user_id']);
echo '
<table class="table table-striped">
<thead>
<tr>
<th>Username</th>
<th>Successful Bookings</th>
<th>Date Added</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
';
for ($i=0; $i < count($accounts); $i++) {
echo "
<tr>
<td>
<a href='https://instagram.com/" . $accounts[$i]['instagram_user'] . "/' target='_blank'>
<img src='" . $accounts[$i]['instagram_picture'] . "'>
</a>
" . $accounts[$i]['instagram_name'] . "<br>
<i>" . $accounts[$i]['instagram_user'] . "</i>
</td>
<td>0 Bookings ($0)</td>
<td>" . $this->timeAgo($accounts[$i]['instagram_timestamp']) . "</td>
<td>
<div class='btn-group'>
<a href='" . $accounts[$i]['instagram_website'] . "' target='_blank' class='btn btn-default'> <i class='fa fa-globe'></i> </a>
<a href data-toggle='modal' data-target='#myModal' class='btn btn-default'> <i class='fa fa-chain-broken'></i> </a>
<a href='processing?make_default=" . $accounts[$i]['instagram_id'] . "' class='btn btn-default'> <i class='fa fa-star'></i> </a>
</div>
</td>
</tr>
";
}
echo '
</tbody>
</table>
';
}
/**
* remove all active Instagram accounts from the user
**/
private function removeAllDefaults(){
global $_SESSION;
if ($this->databaseConnection()) {
$sql = $this->db_connection->prepare('UPDATE instagram_accounts SET instagram_active = null WHERE instagram_site_id = :user_id');
$sql->bindValue(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$sql->execute();
} else {
return $this->callbackMessage("Database connection issues", "danger");
}
}
/**
* set the selected ID to active
*
* @param integer Instagram ID
* @return array callbackMessage
**/
public function makeDefaultInstagram($instagramID){
global $_SESSION;
$this->removeAllDefaults();
if ($this->databaseConnection()) {
$sql = $this->db_connection->prepare('UPDATE instagram_accounts SET instagram_active = 1 WHERE instagram_id = :instagramID AND instagram_site_id = :user_id');
$sql->bindValue(':instagramID', $instagramID, PDO::PARAM_INT);
$sql->bindValue(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$sql->execute();
// if there is actually some results then continue to echo them out
if ($sql->rowCount() > 0) {
return $this->loadAccountsIntoSession($instagramID);
} else {
return $this->callbackMessage("Unable to change account, this account does not belong to you", "danger");
}
} else {
return $this->callbackMessage("Database connection issues", "danger");
}
}
/**
* load the selected Instagram account into session
*
* @param integer Instagram ID
* @return array callbackMessage
**/
public function loadAccountsIntoSession($instagramID) {
global $_SESSION;
// if database connection opened
if ($this->databaseConnection()) {
// load widgets for the user
$sql = $this->db_connection->prepare("SELECT * FROM `instagram_accounts` WHERE instagram_id = :instagramID AND instagram_site_id = :user_id");
$sql->bindValue(':instagramID', $instagramID, PDO::PARAM_INT);
$sql->bindValue(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$sql->execute();
$widgets = $sql->fetchAll();
if ($sql->rowCount() > 0) {
$this->debug($widgets);
// save the instagram info
unset($_SESSION['instagram']);
session_start();
$_SESSION['instagram'] = $widgets[0];
return $this->callbackMessage($widgets[0]["instagram_user"] . " is now loaded as your current account.", "success");
} else {
return $this->callbackMessage("ERROR: " . $sql->errorCode() . ", please contact support." , "danger");
}
} else {
return false;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment