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); } ?>