<?php
/**
* Table Definition for articles
*/
require_once 'DB/DataObject.php';
class DataObject_Articles extends DB_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
public $__table = 'articles'; // table name
public $id; // int(11) not_null primary_key unsigned auto_increment
public $title; // string(100) not_null
public $content; // blob(65535) blob
public $is_active; // int(4) not_null unsigned
public $is_expired; // int(4) not_null unsigned
public $published_on; // date(10) not_null binary
public $created_on; // datetime(19) binary
public $updated_on; // datetime(19) binary
/* Static get */
function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('DataObject_Articles',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
// Some vars
public $articleList;
public $singleArticle;
public $searchTerm;
public $searchProduct;
public $searchPeriod;
public $searchCategory;
public $archivedYears = array();
// This function retrieves articles from the database.
// If the optional input parameter $archives is false, retrieve only the last number
// of months defined in the constant LATEST_MAX_AGE (config.php)
function getArticleList($year = FALSE)
{
// Select ID and title only, to improve memory usage
$this->selectAdd();
$this->selectAdd('id');
$this->selectAdd('title');
$this->selectAdd('is_expired');
$dateLimit = date("Y-m-d", mktime(0,0,0, date("m") + 1 - LATEST_MAX_AGE, 0, date("Y")));
// Are we seeing the latest articles, or the archive?
if (!$year)
$this->whereAdd("published_on > '".$dateLimit."'");
else {
// Show only archived for this year
if ($year == date("Y")) {
$dateInit = date("Y-m-d", mktime(0,0,0, 1, 0, $year));
$this->whereAdd("published_on > '".$dateInit."'");
$this->whereAdd("published_on <= '".$dateLimit."'");
}
// Or show complete years
else {
$dateInit = date("Y-m-d", mktime(0,0,0, 1, 0, $year));
$dateEnd = date("Y-m-d", mktime(0,0,0, 1, 1, $year+1));
$this->whereAdd("published_on > '".$dateInit."'");
$this->whereAdd("published_on < '".$dateEnd."'");
}
}
// Do not show inactive articles
$this->whereAdd("is_active = 1");
$this->selectAdd('DATE_FORMAT(published_on, \'%d/%m/%Y\') as published_on_formatted');
$this->selectAdd('DATE_FORMAT(published_on, \'%M\') as published_on_month');
$this->selectAdd('DATE_FORMAT(published_on, \'%Y\') as published_on_year');
$this->orderBy('published_on DESC');
$this->orderBy('id DESC');
$this->find();
// Fetch all data and return it as an assoc array
$this->articleList = generateArray(&$this);
if (count($this->articleList) > 0)
return(TRUE);
else
return(FALSE);
}
// This function fetches a single article from the database
// Returns false if none was found
function getArticle($id)
{
$this->id = $id;
$this->selectAdd('DATE_FORMAT(published_on, \'%D %M %Y\') as published_on_formatted');
$this->find();
$this->singleArticle = generateArray(&$this);
}
// This function retrieves articles from the database matching a search pattern.
function searchArticles()
{
// Select ID and title only, to improve memory usage
$this->selectAdd();
$this->selectAdd('id');
$this->selectAdd('title');
$this->selectAdd('is_expired');
// How old are the articles we are searching?
switch ($this->searchPeriod) {
case 'lastThreeMonths':
$dateLimit = date("Y-m-d", mktime(0,0,0, date("m")-2, 1, date("Y")));
$this->whereAdd("published_on >= '".$dateLimit."'");
break;
// The rest are gonna be numeric (years, to be precise), so an additional check is required
default:
if (is_numeric($this->searchPeriod)) {
$dateLimit = date("Y-m-d", mktime(0,0,0, 1, 1, $this->searchPeriod));
$dateUpperLimit = date("Y-m-d", mktime(0,0,0, 1, 1, $this->searchPeriod+1));
$this->whereAdd("published_on >= '".$dateLimit."'");
$this->whereAdd("published_on < '".$dateUpperLimit."'");
}
break;
}
// Search terms...
// It is possible to have several words
foreach (explode (' ', $this->searchTerm) as $value) {
$whereAdd = "(LOWER(title) LIKE '%".strtolower($value)."%'";
$whereAdd .= " OR ";
$whereAdd .= "LOWER(content) LIKE '%".strtolower($value)."%')";
$this->whereAdd($whereAdd);
}
// Products
if (!empty($this->searchProduct)) {
$whereAdd = "(LOWER(title) LIKE '%".strtolower($this->searchProduct)."%'";
$whereAdd .= " OR ";
$whereAdd .= "LOWER(content) LIKE '%".strtolower($this->searchProduct)."%')";
$this->whereAdd($whereAdd);
}
// Categories
if (!empty($this->searchCategory)) {
$catArticles = DB_DataObject::factory('articles_categories');
$this->joinAdd($catArticles);
$this->whereAdd("articles_categories.category_id = {$this->searchCategory}");
}
// Date formating options
$this->selectAdd('DATE_FORMAT(published_on, \'%d/%m/%Y\') as published_on_formatted');
$this->selectAdd('DATE_FORMAT(published_on, \'%M %Y\') as published_on_month_year');
$this->orderBy('published_on DESC');
$this->orderBy('id DESC');
$this->find();
// Fetch all data and return it as an assoc array
$this->articleList = generateArray(&$this);
if (count($this->articleList) > 0)
return(TRUE);
else
return(FALSE);
}
}
// This function generates an associative array given a DataObject object
// Not a bad idea to actually pass the object by reference
// please note implications of doing this when using PHP5
function generateArray($object)
{
// This array will contain all assoc data we will return
$array = array();
// This will fetch all rows, one by one, and inserting in array assoc format in the array created previously
while ($row = $object->fetch())
$array[] = $object->toArray();
// Let's return properly, we might need to return false
if (count($array) == 1)
return($array[0]);
elseif (count($array) > 1)
return($array);
else
return(FALSE);
}
?>