Skip to content
Snippets Groups Projects
Commit 3dafecb2 authored by Brett Bieber's avatar Brett Bieber
Browse files

Add wellness resources project files.

parent 5abf80f1
No related branches found
No related tags found
No related merge requests found
<?php
class Article
{
protected $result;
function __construct($result)
{
$this->result = $result;
}
function __get($var)
{
return $this->result[$var];
}
function isInCategory($category)
{
$result = WellnessResources::getDB()->query('SELECT count(*) FROM article_has_category WHERE article_id = '.$this->id.' AND category_id = '.$category->id);
return $result->num_rows;
}
}
?>
\ No newline at end of file
<?php
require_once 'Article.php';
class Articles implements Iterator, Countable
{
protected $result;
protected $current = true;
function __construct(Category $category)
{
$this->result = WellnessResources::getDB()->query('SELECT article_id FROM article_has_category WHERE category_id='.(int)$category->id);
}
function current()
{
$result = WellnessResources::getDB()->query('SELECT * FROM articles WHERE id='.(int)$this->current['article_id']);
$row = $result->fetch_assoc();
return new Article($row);
}
function next()
{
$this->current = $this->result->fetch_assoc();
}
function key()
{
$current = $this->current();
return $current['article_id'];
}
function count()
{
return $this->result->num_rows;
}
function valid()
{
if ($this->current) {
return true;
}
return false;
}
function rewind()
{
$this->result->data_seek(0);
}
}
?>
\ No newline at end of file
<?php
class Categories implements Iterator, Countable
{
protected $result;
protected $current = true;
function __construct($result)
{
include_once 'Category.php';
$this->result = $result;
}
function current()
{
if ($this->current === true) {
$this->next();
}
return new Category($this->current['id'], $this->current['title']);
}
function next()
{
$this->current = $this->result->fetch_assoc();
}
function key()
{
$current = $this->current();
return $current['id'];
}
function count()
{
return $this->result->num_rows;
}
function valid()
{
if ($this->current) {
return true;
}
return false;
}
function rewind()
{
$this->result->data_seek(0);
}
}
?>
\ No newline at end of file
<?php
class Category
{
public $id;
public $title;
function __construct($id, $title)
{
$this->id = $id;
$this->title = $title;
}
function getArticles()
{
include_once 'Articles.php';
return new Articles($this);
}
}
?>
\ No newline at end of file
<?php
class WellnessResources
{
protected static $mysqli;
function __construct($user, $pass, $db)
{
self::$mysqli = new mysqli('localhost', $user, $pass, $db);
}
function getCategory($id)
{
$result = self::$mysqli->query('SELECT * FROM categories WHERE id='.(int)$id);
$row = $result->fetch_assoc();
return new Category($row['id'], $row['title']);
}
function getCategories()
{
return new Categories(self::$mysqli->query('SELECT * FROM categories ORDER BY title'));
}
function getArticles()
{
return new Articles(self::$mysqli->query('SELECT * FROM articles ORDER BY title'));
}
public static function getDB()
{
return self::$mysqli;
}
}
?>
\ No newline at end of file
<?php
$db = 'wellness';
$user = 'wellness';
$pw = 'wellness';
?>
\ No newline at end of file
This diff is collapsed.
<?php
/**
* This script is used to generate the SQL to import the wellness
* articles into a database.
*
* php create_sql.php > import.sql
*
* then import the sql file
*/
$csv = file('all_wellness_documents.csv');
$columns = explode(',', $csv[0]);
array_shift($columns); // Take off URL
array_shift($columns); // Take off Title
array_shift($csv);
foreach ($columns as $id=>$col) {
$category_id = $id+1;
$col = trim($col);
echo 'INSERT INTO categories (id, title) VALUES ('.$category_id.',"'.$col.'");'.PHP_EOL;
}
foreach ($csv as $id=>$row) {
$article_id = $id+1;
$art_cat = explode(',', $row);
$url = str_replace('"', '', trim(array_shift($art_cat)));
$title = str_replace('"', '', trim(array_shift($art_cat)));
echo 'INSERT INTO articles (id, title, url) VALUES ('.$article_id.',"'.$title.'","'.$url.'");'.PHP_EOL;
foreach ($art_cat as $cat_id=>$column) {
if (strtolower(trim($column)) == 'x') {
$cat_id = $cat_id+1;
echo 'INSERT INTO article_has_category (article_id, category_id) VALUES ('.$article_id.','.$cat_id.');'.PHP_EOL;
}
}
}
This diff is collapsed.
<?php
require_once 'UNL/Autoload.php';
require_once 'config.inc.php';
require_once 'WellnessResources.php';
require_once 'Categories.php';
require_once 'Category.php';
ini_set('display_errors',true);
error_reporting(E_ALL);
// Specify domains from which requests are allowed
header('Access-Control-Allow-Origin: *');
// Specify which request methods are allowed
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
// Additional headers which may be sent along with the CORS request
// The X-Requested-With header allows jQuery requests to go through
header('Access-Control-Allow-Headers: X-Requested-With');
// Exit early so the page isn't fully loaded for options requests
if (strtolower($_SERVER['REQUEST_METHOD']) == 'options') {
exit();
}
$resources = new WellnessResources($user, $pw, $db);
UNL_Templates::$options['version'] = 3;
$page = UNL_Templates::factory('Fixed');
$page->maincontentarea = '';
if (isset($_GET['category'])) {
$category = $resources->getCategory($_GET['category']);
$page->maincontentarea .= '<h1>'.$category->title.'</h1>';
$articles = $category->getArticles();
$page->maincontentarea .= '<ul>';
foreach ($articles as $article) {
$page->maincontentarea .= '<li><a href="'.$article->url.'">'.$article->title.'</a></li>';
}
$page->maincontentarea .= '</ul>';
} else {
// $page->maincontentarea .= '<ul class="wdn_tabs">';
// foreach (range('a', 'z') as $letter) {
// $page->maincontentarea .= '<li><a href="#'.$letter.'">'.$letter.'</a></li>';
// }
// $page->maincontentarea .= '</ul>';
$categories = $resources->getCategories();
$page->maincontentarea .= '<ul>';
foreach ($categories as $category) {
$page->maincontentarea .= '<li class="category" id="#'.$category->id.'"><a href="?category='.$category->id.'">'.$category->title.'</a>';
$articles = $category->getArticles();
$page->maincontentarea .= '<ul>';
foreach ($articles as $article) {
$page->maincontentarea .= '<li><a href="'.$article->url.'">'.$article->title.'</a></li>';
}
$page->maincontentarea .= '</ul></li>';
}
$page->maincontentarea .= '</ul>';
}
if (isset($_GET['format'])) {
echo $page->maincontentarea;
} else {
echo $page;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment