public function loadTweets() { $conn = $this->connection; $sqlQuery = "SELECT user_id, tweets.id, text FROM tweets INNER JOIN users ON tweets.user_id = users.id ORDER BY tweets.created_at DESC"; $result = $conn->query($sqlQuery); if ($result->num_rows > 0) { while (list($userId, $tweetId, $text) = $result->fetch_array(MYSQLI_NUM)) { $tweet = new Tweet($conn); $tweet->setUserId($userId); $tweet->setText($text); $tweet->setId($tweetId); $this->arrayWithTweets[] = $tweet; } return true; } else { return false; } }
<?php session_start(); require_once '../resources/require.php'; // Dodaje nowy wpis do bazy, uzupełnia komunikat o dodaniu bądź błędach. if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (trim($_POST['text']) != '') { $tweet = new Tweet($mysqli); $tweet->setUserId($_SESSION['user_id']); $tweet->setText($_POST['text']); if (!$tweet->create()) { $info = 'Błąd przy dodawaniu wpisu'; } else { $info = 'Dodano nowy wpis'; } } else { $info = 'Uzupełnij treść wpisu!'; } } ?> <!DOCTYPE html> <html lang="pl-PL"> <title>Twitter | Strona główna</title> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> </head> <body>
<?php include_once "../configRoot.php"; require_once ROOT_NAME . "/classes/Database.php"; require_once ROOT_NAME . "/classes/Tweet.php"; require_once ROOT_NAME . "/includes/checkProfile.php"; if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["tweet"])) { $text = $_POST["tweet"]; $text = htmlspecialchars($text); $db = Database::getInstance(); $conn = $db->getConnection(); $tweet = new Tweet($conn); $tweet->setUserId($id); $tweet->setText($text); $tweet->create(); header("location: ../index.php"); } else { header("location: ../index.php"); }
<?php session_start(); require_once '../resources/require.php'; // Umożliwia wyśletanie odświeżonej treści strony, po dodaniu komentarza if ($_SERVER['REQUEST_METHOD'] === 'POST') { $_GET['tweet_text'] = $_POST['tweet_text']; } // Tworzy obiekt tweet potrzebny do wyświetlania treści wpisu, który przekierował na tę stronę i dodawania do niego komentarzy $tweet = new Tweet($mysqli); $tweet->setText($_GET['tweet_text']); $tweet->loadFromDB(); // Tworzy nowy komentarz i uzupełnia treść informacji o sukcesie/błędach if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (trim($_POST['comment_text']) != '') { $comment = new Comment($mysqli); $comment->setTweetId($tweet->getId()); $comment->setUserId($_SESSION['user_id']); $comment->setText($_POST['comment_text']); $comment->setCreationDate(date("Y-m-d H:i:s")); if (!$comment->create()) { $info = 'Błąd przy dodawaniu komentarza.'; } else { $info = 'Dodano komentarz.'; } } else { $info = 'Uzupełnij treść komentarza.'; } } ?> <!DOCTYPE html>
protected function execute($arguments = array(), $options = array()) { $databaseManager = new sfDatabaseManager($this->configuration); $connection = $databaseManager->getDatabase($options['connection'])->getConnection(); $web = new sfWebBrowser(); $this->logSection($this->namespace, 'Getting latest tweets for @' . sfConfig::get('app_twitter_username')); $atom = $web->get('http://search.twitter.com/search.atom?q=from:' . sfConfig::get('app_twitter_username') . '&rpp=5'); try { if (!$atom->responseIsError()) { $feed = new SimpleXMLElement($atom->getResponseText()); foreach ($feed->entry as $rss) { $id = preg_replace('/[^0-9]+/', '', $rss->link[0]['href']); $tweet = Doctrine::getTable('Tweet')->find($id); $namespaces = $rss->getNameSpaces(true); if ($tweet instanceof Tweet) { if (strtotime($rss->updated) <= strtotime($tweet->getUpdatedAt())) { continue; } else { $this->updated++; } } else { $tweet = new Tweet(); $this->new++; } $file = $web->get('http://api.twitter.com/1/statuses/show/' . $id . '.json'); try { if (!$file->responseIsError()) { $json = json_decode($file->getResponseText()); $tweet->setId($id); $tweet->setText($rss->title); $tweet->setHTML(html_entity_decode($rss->content)); $tweet->setUri($rss->link[0]['href']); if (isset($json->in_reply_to_status_id)) { $tweet->setReplyId($json->in_reply_to_status_id); } if (isset($json->in_reply_to_user_id)) { $tweet->setReplyUserId($json->in_reply_to_user_id); $tweet->setReplyUsername($json->in_reply_to_screen_name); } if (isset($json->geo, $json->geo->coordinates)) { $tweet->setLatitude($json->geo->coordinates[0]); $tweet->setLongitude($json->geo->coordinates[1]); } $tweet->setLanguage($rss->children($namespaces['twitter'])->lang); $tweet->setSource(html_entity_decode($rss->children($namespaces['twitter'])->source)); $tweet->setCreatedAt($rss->published); $tweet->setUpdatedAt($rss->updated); $tweet->save(); echo '.'; } else { // Error response (eg. 404, 500, etc) } } catch (Exception $e) { // Adapter error (eg. Host not found) } } } else { // Error response (eg. 404, 500, etc) } } catch (Exception $e) { // Adapter error (eg. Host not found) } echo "\n"; $this->logSection($this->namespace, 'Done: ' . $this->new . ' new, ' . $this->updated . ' updated.'); }
public function loadAllTweets() { $conn = $this->connection; $arrayWithTweets = []; $sqlQuery = "SELECT id, text FROM tweets WHERE user_id = {$this->id} ORDER BY tweets.created_at DESC"; $result = $conn->query($sqlQuery); if ($result->num_rows > 0) { while (list($id, $text) = $result->fetch_array(MYSQLI_NUM)) { $tweet = new Tweet($conn); $tweet->setId($id); $tweet->setText($text); $arrayWithTweets[] = $tweet; } return $arrayWithTweets; } }