use_cache == self::SESSION_CACHE && isset($_SESSION['cache_journal'][$key])) { return $_SESSION['cache_journal'][$key]; } else { return false; } } private function setCache($key, $content) { if ($this->use_cache == self::SESSION_CACHE) { $_SESSION['cache_journal'][$key] = $content; } return true; } private function clearCache() { if ($this->use_cache == self::SESSION_CACHE) { $_SESSION['cache_journal'] = array(); } return true; } public function __construct($id, $user_id=null, $use_cache=0) { if ($use_cache == self::SESSION_CACHE) { $this->use_cache = self::SESSION_CACHE; } if (!is_string($id)) throw new lencrierException("Invalid parameter id (non-string)"); $this->id = DB::esc($id); if ($id == $this->getCache('current')) { $this->infos = $this->getCache('infos'); } else { $this->clearCache(); if (is_null($user_id)) $res = DB::aQuery('SELECT * FROM journaux WHERE id="'.$this->id.'";', false, DB::FETCH_ASSOC); else $res = DB::aQuery('SELECT * FROM journaux WHERE id="'.$this->id.'" AND auteur="'.(int)$user_id.'";', false, DB::FETCH_ASSOC); if (!empty($res[0])) { $this->infos = $res[0]; $this->infos['url'] = utils::getJournalURL($this->id); $this->setCache('infos', $this->infos); $this->setCache('current', $this->id); } else { $this->id = false; return false; } } } public function get($key) { if ($key == 'id') return $this->id; elseif (array_key_exists($key, $this->infos)) return $this->infos[$key]; else return null; } public function get_all() { return $this->infos; } public function getAll() { return $this->infos; } public function set($key, $value) { if (is_null($this->get($key))) return false; if ($key == 'id') { throw new InvalidArgumentException('ID ne peut être modifié'); } elseif ($key == 'statut') { if (!is_numeric($value) || (int)$value < 0 || (int)$value > 4) { throw new OutOfRangeException('Statut est invalide'); } $value = (int)$value; } elseif ($key == 'forum' || $key == 'abonnement' || $key == 'contact') { $value = (int)(bool)$value; } elseif ($key == 'visuel') { if (!Skin::checkSkin($value)) { throw new InvalidArgumentException('Ce visuel n\'existe pas'); } } elseif ($key == 'notifications') { $value = (int)$value; if (!(($value == self::NOTIFICATIONS_DISABLED) || (($value & self::NOTIFICATIONS_FORUM) || ($value & self::NOTIFICATIONS_CONTRIBUTION)))) { throw new InvalidArgumentException('Paramètre de notifications invalide'); } } if ($this->infos[$key] == $value) return true; $this->infos[$key] = $value; $this->modified[$key] = $value; return true; } public function setExportDone() { $this->set('print', time()); } public function save() { if (!empty($this->modified)) { $sql = array(); foreach ($this->modified as $key=>$value) { $sql[] = '`'.DB::esc($key).'`="'.DB::esc($value).'"'; } $sql = 'UPDATE journaux SET ' . implode(', ', $sql) . ' WHERE id="'.$this->id.'";'; DB::uQuery($sql); $this->setCache('infos', $this->infos); } return true; } public function getStatistics() { if ($stats = $this->getCache('statistics')) { return $stats; } else { $res = DB::aQuery('SELECT MIN(date) AS first, MAX(date) AS last, COUNT(*) AS total FROM ecrits WHERE journal="'.$this->id.'";'); $duration_years = 0; $duration_months = 0; $duration_days = 0; if ($res[0]['total'] > 1) { $d = $res[0]['last'] - $res[0]['first']; $duration_years = floor($d / 3600 / 24 / 365.25); if($duration_years > 0) { $d = $d - ($duration_years * 3600 * 24 * 365.25); } $duration_months = floor($d / 3600 / 24 / 30.4375); if($duration_months > 0) { $d = $d - ($duration_months * (3600 * 24 * 30.4375)); } $duration_days = floor($d / 3600 / 24); } $res[0]['duration_years'] =& $duration_years; $res[0]['duration_months'] =& $duration_months; $res[0]['duration_days'] =& $duration_days; $this->setCache('statistics', $res[0]); return $res[0]; } } public function listLastArticles($max=10, $withContent = false) { if ($withContent) { return DB::aQuery('SELECT * FROM ecrits AS e INNER JOIN ecrits_html AS t ON e.id = t.id WHERE e.journal="'.$this->id.'" ORDER BY e.date DESC LIMIT 0,'.(int)$max.';', false, DB::FETCH_ASSOC); } else { if ($last = $this->getCache('last'.(int)$max.'articles')) { return $last; } else { $res = DB::aQuery('SELECT * FROM ecrits WHERE journal="'.$this->id.'" ORDER BY date DESC LIMIT 0,'.(int)$max.';', false, DB::FETCH_ASSOC); $this->setCache('last'.(int)$max.'articles', $res); return $res; } } } public function deleteAll($archive='GUESS') { self::deleteJournal($this->get('id'), $archive); if (isset($_SESSION['current_user']['nb_journals'])) { unset($_SESSION['current_user']['nb_journals']); } $this->clearCache(); } public function isPrivate() { return ($this->get('statut') == self::STATUS_PRIVATE) ? true : false; } public function isHidden() { return ($this->get('statut') == self::STATUS_HIDDEN) ? true : false; } public function isSecret() { return ($this->get('statut') == self::STATUS_SECRET) ? true : false; } public function isPublic() { return ($this->get('statut') == self::STATUS_PUBLIC || $this->get('statut') == self::STATUS_OPEN) ? true : false; } public function isOpen() { return ($this->get('statut') == self::STATUS_OPEN) ? true : false; } public function contact($texte, $titre, $nom, $contact, $copie = false) { $to = user::getEmailFromId($this->get('auteur')); notifications::sendContactMessage($to, $this->get('titre'), $nom, $contact, $titre, $texte); } } ?>