This Website has been Deprecated. Use http://ddc-web-curriculum.cnm.edu/
For Prework and Written Curriculum

Model, View, Controller

Model

The model is the same as the Tweet class in the Object Oriented example.

Controller

The controller form can be found here.

Controller Form Processor

<?php
require_once(dirname(__DIR__) . "/object-oriented/tweet.php");
require_once(dirname(dirname(__DIR__)) . "/lib/php/encrypted-config.php");
require_once(dirname(dirname(__DIR__)) . "/lib/php/xsrf.php");

try {
	// ensure the fields are filled out
	if(@isset($_POST["profileId"]) === false || @isset($_POST["tweetContent"]) === false) {
		throw(new InvalidArgumentException("form values not complete. Verify the form and try again."));
	}

	// verify the XSRF challenge
	if(session_status() !== PHP_SESSION_ACTIVE) {
		session_start();
	}
	verifyXsrf();

	// create the new tweet and insert into mySQL
	$pdo = connectToEncryptedMySQL("/etc/apache2/data-design/dmcdonald21.ini");
	$tweet = new Tweet(null, $_POST["profileId"], $_POST["tweetContent"]);
	$tweet->insert($pdo);
	$tweetId = $tweet->getTweetId();
	echo <<< EOF
<div class="alert alert-dismissible alert-success" role="alert">
	<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	Tweet (id = $tweetId) posted!
</div>
EOF;

} catch(Exception $exception) {
	$message = $exception->getMessage();
	echo <<< EOF
<div class="alert alert-dismissible alert-danger" role="alert">
	<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	$message
</div>
EOF;
}

View

The view can be viewed here.

<?php
require_once(dirname(__DIR__) . "/object-oriented/tweet.php");
require_once(dirname(dirname(__DIR__)) . "/lib/php/encrypted-config.php");
?>
<!--template inclusion of Bootstrap, jQuery, etc omitted for brevity-->
<h1>View: Tweets</h1>
<table class="table table-bordered table-responsive table-striped">
	<tr><th>tweetId</th><th>profileId</th><th>tweetContent</th><th>tweetDate</th></tr>
	<?php
	try {
		$pdo = connectToEncryptedMySQL("/etc/apache2/data-design/dmcdonald21.ini");
		$tweets = Tweet::getAllTweets($pdo);
		foreach($tweets as $tweet) {
			echo "<tr><td>" . $tweet->getTweetId() ."</td><td>" . $tweet->getProfileId() ."</td><td>" . $tweet->getTweetContent() . "</td><td>" . $tweet->getTweetDate()->format("Y-m-d H:i:s") . "</td></tr>" . PHP_EOL;
		}
	} catch(Exception $exception) {
		$message = $exception->getMessage();
		echo <<< EOF
<div class="alert alert-dismissible alert-danger" role="alert">
	<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	$message
</div>
EOF;
	}
	?>
</table>