<?php
/*##################################################
* HomeCustomHomePageExtensionPoint.class.php
* -------------------
* begin : August 25, 2012
* copyright : (C) 2012 Kevin MASSY
* email : kevin.massy@phpboost.com
*
*
###################################################
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
###################################################*/
class HomeCustomHomePageExtensionPoint implements HomePageExtensionPoint
{
private $template;
public function get_home_page()
{
$columns_disabled = Environment::get_graphical_environment()->get_columns_disabled();
$columns_disabled->set_disable_left_columns(false); //passer à "true" pour désactiver
$columns_disabled->set_disable_right_columns(false);
$columns_disabled->set_disable_top_central(false);
$columns_disabled->set_disable_bottom_central(false);
return new DefaultHomePage($this->get_title(), $this->get_view());
}
private function get_title()
{
return LangLoader::get_message('title', 'common', 'HomeCustom');
}
private function get_view()
{
$this->template = new FileTemplate('HomeCustom/home.tpl');
$this->build_view();
return $this->template;
}
private function build_view()
{
// ici on définit la date en Français
// for the French date
setlocale(LC_ALL, 'french', 'fr_FR.UTF8', 'fr.UTF8', 'fr_FR.UTF-8', 'fr.UTF-8', 'fr_FR');
$this->build_articles_view($this->articles_results());
$this->build_news_view($this->news_results());
$this->build_events_view($this->events_results());
}
private function articles_results()
{
$now = new Date();
$querier = PersistenceContext::get_querier();
$results = $querier->select('SELECT articles.*, member.*, com.number_comments, notes.average_notes, notes.number_notes, note.note, cat.rewrited_name AS rewrited_name_cat
FROM ' . PREFIX . 'articles articles
LEFT JOIN ' . PREFIX . 'articles_cats cat ON cat.id = articles.id_category
LEFT JOIN ' . DB_TABLE_MEMBER . ' member ON member.user_id = articles.author_user_id
LEFT JOIN ' . DB_TABLE_COMMENTS_TOPIC . ' com ON com.id_in_module = articles.id AND com.module_id = \'articles\'
LEFT JOIN ' . DB_TABLE_AVERAGE_NOTES . ' notes ON notes.id_in_module = articles.id AND notes.module_name = \'articles\'
LEFT JOIN ' . DB_TABLE_NOTE . ' note ON note.id_in_module = articles.id AND note.module_name = \'articles\' AND note.user_id = :user_id
AND (published = 1 OR (published = 2 AND publishing_start_date < :timestamp_now AND (publishing_end_date > :timestamp_now OR publishing_end_date = 0)))
ORDER BY articles.date_created DESC
LIMIT 3',
array(
'user_id' => AppContext::get_current_user()->get_id(),
'timestamp_now' => $now->get_timestamp()
));
/*
SELECT = On selectionne tous les champs liés aux articles
FROM = Dans quelles entités sont issues les champs
LEFT JOIN = On fait les jointure afin de dire que les clefs primaire sont identiques
AND = On fait bien attention a récupérer un article qui a été publié avec autorisation ou différé après heure
ODRDER BY = On classe les articles par la date de création et par ordre décroissant (DESC, le plus récent en premier et le plus ancien en dernier)
LIMIT = On récupère uniquement les 3 derniers articles
*/
return $results;
}
private function news_results()
{
$now = new Date();
$querier = PersistenceContext::get_querier();
$results = $querier->select('SELECT news.*, member.*, cat.rewrited_name AS rewrited_name_cat
FROM ' . PREFIX . 'news news
LEFT JOIN ' . PREFIX . 'news_cats cat ON cat.id = news.id_category
LEFT JOIN ' . DB_TABLE_MEMBER . ' member ON member.user_id = news.author_user_id
AND (approbation_type = 1 OR (approbation_type = 2 AND start_date < :timestamp_now AND (end_date > :timestamp_now OR end_date = 0)))
ORDER BY news.creation_date DESC
LIMIT 3', array(
'timestamp_now' => $now->get_timestamp()
));
/*
SELECT = On selectionne tous les champs liés aux news
FROM = Dans quelles entités sont issues les champs
LEFT JOIN = On fait la jointure afin de dire que les clefs primaire sont identiques
AND = On fait bien attention a récupérer une news qui a été publiée avec autorisation ou différé après heure.
ODRDER BY = On classe les news par la date de création et par ordre décroissant (DESC, le plus récent en premier et le plus ancien en dernier)
LIMIT = On récupère uniquement les 3 dernières news.
*/
return $results;
}
private function events_results()
{
$now = new Date();
$today = strtotime(date("Y-m-d", $now->get_timestamp()));
$querier = PersistenceContext::get_querier();
$results = $querier->select('SELECT *
FROM '. PREFIX . 'calendar_events event
LEFT JOIN ' . PREFIX . 'calendar_events_content event_content ON event_content.id = event.content_id
LEFT JOIN ' . DB_TABLE_MEMBER . ' member ON member.user_id = event_content.author_id
LEFT JOIN '. PREFIX . 'calendar_cats cat ON cat.id = event_content.id_category
WHERE approved = 1
AND start_date >= :timestamp_now
ORDER BY start_date
LIMIT 3
', array(
'timestamp_now' => $today
));
/*
SELECT = On selectionne tous les champs liés aux événements
FROM = Dans quelles entités sont issues les champs
LEFT JOIN = On fait la jointure afin de dire que les clefs primaire sont identiques
WHERE On fait bien attention a récupérer un événement qui a été publié
AND = On récupère uniquement un prochain événement (les événements passés on ne les veut pas)
ODRDER BY = On classe les événements par date de début par ordre chronologique (ASC, inutile de le préciser, on précise uniquement le DESC)
LIMIT = On récupère uniquement les 3 prochains événements
*/
return $results;
}
private function build_articles_view(SelectQueryResult $results)
{
// ici les résultats seront envoyés dans articles.tpl
// results send in articles.tpl
$tpl = new FileTemplate('HomeCustom/articles.tpl');
while($row = $results->fetch())
{
$article = new Article();
$article->set_properties($row);
$tpl->assign_block_vars('articles_items', $article->get_tpl_vars());
// create a new custom TPL variable made with the help of $article
// ici on créeait une nouvelle variable personnalisée avec l'aide de $article
$tpl->put_all(array(
'DATE_DAY' => strftime('%d', $article->get_date_created()->get_timestamp()),
'DATE_MONTH_A' => strftime('%b', $article->get_date_created()->get_timestamp()),
));
}
// ici c'est pour "pousser" articles.tpl via # INCLUDE ARTICLES # dans le home.tpl
// "push" articles.tpl with # INCLUDE ARTICLES # in home.tpl
$this->template->put('ARTICLES', $tpl);
}
private function build_news_view(SelectQueryResult $results)
{
// ici les résultats seront envoyés dans news.tpl
// results send in news.tpl
$tpl = new FileTemplate('HomeCustom/news.tpl');
while ($row = $results->fetch())
{
$news = new News();
$news->set_properties($row);
$tpl->assign_block_vars('news_items', $news->get_array_tpl_vars());
}
// ici c'est pour "pousser" news.tpl via # INCLUDE NEWS # dans le home.tpl
// "push" news.tpl with # INCLUDE NEWS # in home.tpl
$this->template->put('NEWS', $tpl);
}
private function build_events_view(SelectQueryResult $results)
{
// ici les résultats seront envoyés dans events.tpl
// results send in events.tpl
$tpl = new FileTemplate('HomeCustom/events.tpl');
foreach ($results as $row)
{
// si "rewrited_name" est vide c'est que c'est la categorie racine (root)
// if "rewrited_name" is empty it's root category (root)
$row['rewrited_name'] = !empty($row['id_category']) ? $row['rewrited_name_cat'] : 'root';
// je me prend pas la tête j'utilise le builder du module calendar, comme ça je suis sure que le lien généré sera correcte.
// using the "CalendarUrlBuilder" to build link
$link = CalendarUrlBuilder::display_event($row['id_category'], $row['rewrited_name'], $row['id_event'], $row['rewrited_title']);
// ici sa boucle et on fait correspondre le tableau extrait de la BDD avec des variables TPL
// here is the loop to correspond the row in database with the right TPL variable
$tpl->assign_block_vars('events_items', array(
'U_LINK' => $link->absolute(),
'TITLE' => $row['title'],
'CONTENTS' => $row['contents'],
'PSEUDO' => $row['login'],
'LOCATION' => $row['location'],
'LAST_DATE' => strftime('%d/%m/%Y', $row['last_registration_date']),
'MAX_MEMBER' => $row['max_registered_members'],
'START_DATE' => strftime('%d/%m/%Y', $row['start_date']),
'END_DATE' => strftime('%d/%m/%Y', $row['end_date']),
'CAT' => $row['name'],
'START_DATE_FULL' => strftime('%d %B %Y', $row['start_date']),
// date de début jour
// start date of day
'START_DATE_DAY_FULL' => strftime('%A', $row['start_date']),
'START_DATE_DAY_SHORT' => strftime('%a', $row['start_date']),
'START_DATE_DAY_FIGURE' => strftime('%d', $row['start_date']),
// date de début mois
// start date of month
'START_DATE_MONTH_FULL' => strftime('%B', $row['start_date']),
'START_DATE_MONTH_SHORT' => strftime('%b', $row['start_date']),
'START_DATE_MONTH_FIGURE' => strftime('%m', $row['start_date']),
// date de début année
// start date of year
'START_DATE_YEAR_FULL' => gmdate_format('%Y', $row['start_date']),
'START_DATE_YEAR_SHORT' => gmdate_format('%y', $row['start_date']),
));
}
// ici c'est pour "pousser" events.tpl via # INCLUDE EVENTS # dans le home.tpl
// "push" events.tpl with # INCLUDE EVENTS # in home.tpl
$this->template->put('EVENTS', $tpl);
}
}
?>