Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Database Config and Methods
- class Database {
- var $config = array(
- 'old' => array(
- 'username' => '********',
- 'password' => '********',
- 'hostname' => '********',
- 'database' => '********'
- ),
- 'default' => array(
- 'username' => '********',
- 'password' => '********',
- 'hostname' => '********',
- 'database' => '********'
- ),
- 'dev' => array(
- 'username' => '********',
- 'password' => '********',
- 'hostname' => '********',
- 'database' => '********'
- )
- );
- var $conn = null; // Placeholder for the SQL Connection object
- function __construct($config = 'dev') {
- $this->connect($config); // Connect to the default database
- }
- /**
- * Connect to the specified database config, or default
- */
- function connect($config = 'dev') {
- if (is_null($this->conn)) {
- $db = $this->config[$config];
- if (empty($db)) {
- die('Invalid database configuration');
- }
- $this->conn = mysql_connect($db['hostname'], $db['username'], $db['password']);
- if(!$this->conn) {
- die("Cannot connect to database server");
- }
- if(!mysql_select_db($db['database'])) {
- die("Cannot select database");
- }
- }
- return $this->conn;
- }
- }
- ?>
- <?php
- /**
- * Contains all of the Model Classes
- *
- * @author Trae Robrock
- * @version $Id$
- * @copyright Trae Robrock, 12 August, 2009
- * @package ocsa.libs.models
- **/
- /**
- * Define DocBlock
- **/
- require_once('database.php');
- /**
- * The Model for handling any calls to or from the Meetings table
- */
- class Meetings {
- var $config = array(
- 'table' => 'meetings',
- 'default_time' => '6:00 PM',
- );
- function __construct() {
- $db = new Database();
- $conn = $db->connect();
- $table = $this->config['table'];
- $sql = "DESCRIBE `$table`";
- $query = mysql_query($sql);
- if (!$query) {
- die('Could not connect to the database table!');
- }
- }
- /**
- *
- * Get all the meetings in the database
- * $sort either ASC or DESC
- * Returns an associative array
- *
- */
- function getAll($sort = 'ASC') {
- $db = new Database();
- $conn = $db->connect();
- if ((!$sort == 'DESC') && (!$sort == 'ASC')) {
- $sort = 'ASC';
- // Might want to log a possible malicious user here
- }
- $table = $this->config['table'];
- $sql = "SELECT * FROM `$table` ORDER BY `date` $sort";
- $result = mysql_query($sql);
- if (!$result) {
- die('Error with your query. ' . mysql_error());
- }
- $return = array();
- while ($row = mysql_fetch_assoc($result)) {
- $return['Meetings'][] = $row;
- }
- return $return;
- }
- /**
- *
- * Get all the meetings for a specific date
- * $month integer 1 through 12
- * $sort works like getAll()
- * Returns an associative array
- *
- */
- function getByMonth($month = '', $sort = 'ASC') {
- $db = new Database();
- $conn = $db->connect();
- if (empty($month)) {
- return false;
- }
- $current_year = date('Y', time());
- $next_month = (($month + 1) > 12) ? 1 : ($month + 1);
- $start_time = mktime(0,0,0,$month,1,$current_year);
- if ($next_month == 1) { $current_year++; }
- $end_time = mktime(0,0,0,$next_month,1,$current_year);
- $sql = "SELECT * FROM `{$this->config['table']}` WHERE `date` >= FROM_UNIXTIME($start_time) AND `date` < FROM_UNIXTIME($end_time)";
- $result = mysql_query($sql);
- if (!$result) {
- die('Error with your query. ' . mysql_error());
- }
- $return = array();
- while ($row = mysql_fetch_assoc($result)) {
- $return['Meetings'][] = $row;
- }
- return $return;
- }
- /**
- *
- * Saves a new meeting
- * $date String compatible with strtotime()
- * $location String
- *
- */
- function saveMeeting($date, $location) {
- $db = new Database();
- $conn = $db->connect();
- $date = strtotime($date);
- if (!$date) {
- die('Invalid Date Format!');
- }
- if (date('H:i', $date) == '00:00') {
- $defTime = strtotime($this->config['default_time']);
- $date = mktime(date('H', $defTime), date('i', $defTime), 0, date('n', $date), date('j', $date), date('Y', $date));
- }
- $location = mysql_escape_string(trim($location));
- $sql = "INSERT INTO `{$this->config['table']}` (`date`, `location`) VALUES (FROM_UNIXTIME($date), '$location')";
- $result = mysql_query($sql);
- if (!$result) {
- return false;
- }
- else {
- $sql = "SELECT * FROM `{$this->config['table']}` WHERE `id` = LAST_INSERT_ID()";
- $result = mysql_query($sql);
- $return = mysql_fetch_row($result);
- return $return;
- }
- }
- /**
- *
- * Build the HTML for the Month list with links
- *
- */
- function buildMonthList() {
- echo "<div id='meetings-list-months'>";
- echo "<a href='index.php'>All</a>\n";
- for ($i = 1; $i <= 12; $i++) {
- $month = date('M', mktime(0,0,0,$i,1,date('Y', time())));
- $data = $this->getByMonth($i);
- if (empty($data)) {
- echo "$month\n";
- }
- else {
- echo "<a href='index.php?m=$i'>$month</a>\n";
- }
- }
- echo "</div>";
- }
- /**
- *
- * Deletes a meeting by its ID
- *
- */
- function deleteById($id = null) {
- $db = new Database();
- $conn = $db->connect();
- if (!empty($id) && is_numeric($id)) {
- $sql = "DELETE FROM `{$this->config['table']}` WHERE `id` = $id";
- if (!mysql_query($sql)) {
- die('Couldnt delete the meeting');
- }
- else {
- return true;
- }
- }
- }
- /**
- *
- * Build HTML for Meeting list
- *
- */
- function buildMeetingList($month = null) {
- echo '<table id="meetings-list-table"><tr><th>Date</th><th>Location</th></tr>';
- if ((!empty($month)) && (is_numeric($month)) && ($month > 0) && ($month <= 12)) {
- $month = trim($_GET['m']);
- $data = $this->getByMonth($month);
- }
- else {
- $data = $this->getAll();
- }
- foreach($data['Meetings'] as $meeting) {
- $timestamp = strtotime($meeting['date']);
- $meeting['date'] = date('M d, Y h:ia', $timestamp);
- echo '<tr class=\'meeting-entry\' id=\'' . $meeting['id'] . '\'>';
- echo "<td>{$meeting['date']}</td>";
- echo "<td>{$meeting['location']}</td>";
- echo '</tr>';
- }
- echo '</table>';
- }
- }
- /**
- * The Model for handling any calls to or from the Officers table
- */
- class Officers {
- var $config = array(
- 'table' => 'officers'
- );
- function __construct() {
- $db = new Database();
- $conn = $db->connect();
- $sql = "DESCRIBE `{$this->config['table']}`";
- $query = mysql_query($sql);
- if (!$query) {
- die('Could not connect to the database table!');
- }
- }
- function viewAll()
- {
- $db = new Database();
- $conn = $db->connect();
- $sql = "SELECT * FROM `{$this->config['table']}`";
- $result = mysql_query($sql);
- if (!$result) {
- die('Error with your query. ' . mysql_error());
- }
- $return = array();
- while ($row = mysql_fetch_assoc($result)) {
- $return['Officers'][] = $row;
- }
- return $return;
- }
- function eraseAll()
- {
- $db = new Database();
- $conn = $db->connect();
- $sql = "DELETE FROM `{$this->config['table']}`";
- var_dump($sql);
- $result = mysql_query($sql);
- var_dump($result);
- if (!$result) {
- die('Error with your query. ' . mysql_error());
- }
- else {
- return true;
- }
- }
- function saveOfficer($first_name = null, $last_name = null, $email = null, $position = null)
- {
- $db = new Database();
- $conn = $db->connect();
- if (!empty($first_name) && !empty($last_name) && !empty($email) && !empty($position)) {
- $sql = "INSERT INTO {$this->config['table']} (`id`, `first_name`, `last_name`, `email`, `position`) VALUES (NULL, '$first_name', '$last_name', '$email', '$position')";
- $result = mysql_query($sql);
- if (!$result) {
- return false;
- }
- else {
- return true;
- }
- }
- else {
- return false;
- }
- }
- }
- /**
- * The model for handling any calls for the News table
- *
- * @package ocsa.libs.models.News
- * @author Trae Robrock
- **/
- class News
- {
- /**
- * The configuration var, holds any config specific to this model
- *
- * @var array
- **/
- var $config = array(
- 'table' => 'news',
- 'legacy' => array(
- 'active' => true,
- 'table' => 'spotlight',
- 'fields' => array(
- 'content' => 'text',
- ),
- ),
- );
- /**
- * Constructor
- *
- * Tries connecting to the database, dies on failure
- *
- * @return void
- * @author Trae Robrock
- **/
- function __construct()
- {
- /**
- * Adds a little functionality to support the legacy system
- * This can be switched off once the site is refactored to use
- * The new database
- */
- if ($this->config['legacy']['active'] === true) {
- return;
- }
- $db = new Database();
- $conn = $db->connect();
- $sql = "DESCRIBE `{$this->config['table']}`";
- $query = mysql_query($sql);
- if (!$query) {
- die('Could not connect to the database table!');
- }
- }
- /**
- * Gets all entries from the table and returns them as an array
- *
- * @return array Contains all the table entries
- * @author Trae Robrock
- **/
- function viewAll()
- {
- /**
- * Adds a little functionality to support the legacy system
- * This can be switched off once the site is refactored to use
- * The new database
- */
- if ($this->config['legacy']['active'] === true) {
- return $this->legacyViewAll();
- }
- $db = new Database();
- $conn = $db->connect();
- $sql = "SELECT * FROM `{$this->config['table']}` ORDER BY `id` DESC";
- $result = mysql_query($sql);
- if (!$result) {
- die('Error with your query. ' . mysql_error());
- }
- $return = array();
- while ($row = mysql_fetch_assoc($result)) {
- $return['News'][] = $row;
- }
- return $return;
- }
- /**
- * Saves a new entry to the table
- *
- * @return boolean True on success, false on failure
- * @param string $title the title of the new story to add
- * @param string $content any content for the new story to add
- * @author Trae Robrock
- **/
- function saveNew($title = null, $content = null)
- {
- /**
- * Adds a little functionality to support the legacy system
- * This can be switched off once the site is refactored to use
- * The new database
- */
- if ($this->config['legacy']['active'] === true) {
- return $this->legacySaveNew($title, $content);
- }
- $db = new Database();
- $conn = $db->connect();
- $time = time();
- if (!empty($title) && !empty($content)) {
- $sql = "INSERT INTO {$this->config['table']} (`id`, `date`, `title`, `content`) VALUES (NULL, FROM_UNIXTIME($time), '$title', '$content')";
- $result = mysql_query($sql);
- if (!$result) {
- return false;
- }
- else {
- return true;
- }
- }
- else {
- return false;
- }
- }
- /**
- * This does the same as viewAll, but supports the legacy system's format
- *
- * @return array Contains all the table entries
- * @author Trae Robrock
- **/
- function legacyViewAll()
- {
- $db = new Database('old');
- $conn = $db->connect('old');
- $sql = "SELECT * FROM `{$this->config['legacy']['table']}` ORDER BY `id` DESC";
- $result = mysql_query($sql);
- if (!$result) {
- die('Error with your query. ' . mysql_error());
- }
- $return = array();
- while ($row = mysql_fetch_assoc($result)) {
- $return['News'][] = $row;
- }
- return $this->legacyConvertFields($return);
- }
- /**
- * This does the same as saveNew, but supports the legacy system's format
- *
- * @return boolean True on success, False on failure
- * @param string $title the title of the new story to add
- * @param string $content any content for the new story to add
- * @author Trae Robrock
- **/
- function legacySaveNew($title = null, $content = null)
- {
- $db = new Database('old');
- $conn = $db->connect('old');
- $time = time();
- $sql = "UPDATE {$this->config['legacy']['table']} SET `active` = 0 WHERE 1=1";
- $result = mysql_query($sql);
- if (!$result) {
- return false;
- }
- if (!empty($title) && !empty($content)) {
- $sql = "INSERT INTO {$this->config['legacy']['table']} (`id`, `date`, `title`, `text`, `active`) VALUES (NULL, '$time', '$title', '$content', '1')";
- $result = mysql_query($sql);
- if (!$result) {
- return false;
- }
- else {
- return true;
- }
- }
- else {
- return false;
- }
- }
- /**
- * This will take in a standard $data container filled with rows of table data
- * and convert the field names to be readable by the newer system files
- *
- * @return array Containing the original $data array with usable field names
- * @param array $data Standard data array with table data
- * @author Trae Robrock
- **/
- function legacyConvertFields($data = array())
- {
- $keys = array_keys($data);
- foreach ($keys as $key) {
- foreach ($data[$key] as $rowNumber => $rowData) {
- foreach ($rowData as $fieldName => $fieldData) {
- if ($newFieldName = array_search($fieldName, $this->config['legacy']['fields'])) {
- unset($data[$key][$rowNumber][$fieldName]);
- $data[$key][$rowNumber][$newFieldName] = $fieldData;
- }
- if ($fieldName == 'date') {
- $data[$key][$rowNumber][$fieldName] = date('m.d.Y', $fieldData);
- }
- }
- }
- }
- return $data;
- }
- } // END class News
- ?>
Add Comment
Please, Sign In to add comment