<?php

class Notify
{

    private $notificationType;
    private $details;
    private $ci;
    public $is_insertion = false;
    private $response;

    const TYPE_TORRENT_ASSENT      = 0;
    const TYPE_TORRENT_BLOCK       = 2;
    const TYPE_TORRENT_DEL         = 3;
    const TYPE_TORRENT_ADD_NOTE    = 4;
    const TYPE_TORRENT_ADD_COMMENT = 6;
    const TYPE_PROFILE_SANCTION    = 7;
    const TYPE_PROFILE_CITATION    = 8;

    const TYPE_SANCTION_MUTE_SHOUTBOX = 11;
    const TYPE_SANCTION_MUTE_MP       = 12;
    const TYPE_SANCTION_MUTE_COMMENT  = 13;
    const TYPE_SANCTION_NO_UPLOAD     = 14;
    const TYPE_SANCTION_BAN           = 15;

    /**
     * Initialise le constructeur
     * @param array infos générales de la notification
     */
    public function __construct($args)
    {
        $this->ci = &get_instance();
        if (count($args) > 0) {
            $this->notificationType = $args[0];
            $this->details          = $args[1];
            $this->is_insertion     = true;
            $this->controllerNotification();
        }
    }

    private function controllerNotification()
    {
        if ($this->notificationType == self::TYPE_TORRENT_ASSENT || $this->notificationType == self::TYPE_TORRENT_BLOCK || $this->notificationType == self::TYPE_TORRENT_DEL) {
            return $this->validationTorrent_notification();
        } else {
            switch ($this->notificationType) {
                case self::TYPE_TORRENT_ADD_NOTE:
                    $this->addNote_notification();
                    break;
                case self::TYPE_TORRENT_ADD_COMMENT:
                    $this->commentTorrent_notification();
                    break;
                case self::TYPE_PROFILE_SANCTION:
                    break;
                case self::TYPE_PROFILE_CITATION:
                    break;
                case self::TYPE_SANCTION_MUTE_SHOUTBOX:
                    $this->sanctionMuteShout_notification();
                    break;
                case self::TYPE_SANCTION_MUTE_MP:
                    $this->sanctionMuteMP_notification();
                    break;
                case self::TYPE_SANCTION_MUTE_COMMENT:
                    $this->sanctionMuteComment_notification();
                    break;
                case self::TYPE_SANCTION_NO_UPLOAD:
                    $this->sanctionNoUp_notification();
                    break;
                case self::TYPE_SANCTION_BAN:
                    $this->sanctionBan_notification();
                    break;
            }
        }
    }

    /**
     * @param NotificationID - Integer
     * @return Array
     */
    public function getNotificationTPL($notifID)
    {
        $notification = $this->getNotification($notifID);
        $details      = json_decode($notification->details);
        switch ($notification->event) {
            case self::TYPE_TORRENT_ASSENT:
                $avatar  = base_url('static/img/avatar_staff.png');
                $head    = '<strong>Torrent approuvé</strong>';
                $content = '<strong style="color:#ececec">' . $details->torrent_name . '</strong> <font style="color:#75ff75; font-weight:bold;">approuvé</font> par la Team Pending';
                $content .= '<br><span style="font-weight: bold;color: #15ccb1;font-size: 11px;text-transform: uppercase;">Il y a ' . $this->time_elapsed_string($notification->date) . '</span>';
                $target_url = base_url('engine/url_builder?type=torrent&id=' . $details->torrent_id);
                break;
            case self::TYPE_TORRENT_BLOCK:
                $avatar  = base_url('static/img/avatar_staff.png');
                $head    = '<strong>Torrent bloqué</strong>';
                $content = '<strong style="color:#ececec">' . $details->torrent_name . '</strong> <font style="color:#ff9a68; font-weight:bold;">bloqué</font> par la Team Pending, <font style="font-weight:bold;">cliquez-ici pour voir les modifications à apporter</font>';
                $content .= '<br><span style="font-weight: bold;color: #15ccb1;font-size: 11px;text-transform: uppercase;">Il y a ' . $this->time_elapsed_string($notification->date) . '</span>';
                $target_url = base_url('engine/url_builder?type=torrent&id=' . $details->torrent_id);
                break;
            case self::TYPE_TORRENT_DEL:
                $avatar  = base_url('static/img/avatar_staff.png');
                $head    = '<strong>Torrent supprimé</strong>';
                $content = '<strong style="color:#ececec">' . $details->torrent_name . '</strong> <font style="color:#df4b4b; font-weight:bold;">supprimé</font> par la Team Pending, <font style="font-weight:bold;">cliquez-ici pour voir les raisons de la suppression</font>';
                $content .= '<br><span style="font-weight: bold;color: #15ccb1;font-size: 11px;text-transform: uppercase;">Il y a ' . $this->time_elapsed_string($notification->date) . '</span>';
                $target_url = base_url('engine/url_builder?type=torrent&id=' . $details->torrent_id);
                break;
            case self::TYPE_TORRENT_ADD_COMMENT:
                $head    = '<strong>Nouvelle</strong>';
                $avatar  = $details->from_uid_avatar == null ? base_url('static/images/avatar.jpg') : base_url('files/avatars/' . $details->from_uid_avatar);
                $content = '<strong>' . $details->from_uid_nickname . '</strong> a ajouté un commentaire  sur votre torrent <strong>' . $details->torrent_name . '</strong>';
                $content .= '<br><span style="font-weight: bold;color: #15ccb1;font-size: 11px;text-transform: uppercase;">Il y a ' . $this->time_elapsed_string($notification->date) . '</span>';
                $target_url = base_url('engine/url_builder?type=focuscomment&id=' . $details->comment_id);
                break;
            case self::TYPE_TORRENT_ADD_NOTE:
                $avatar  = base_url('static/img/avatar_staff.png');
                $head    = '<strong>Nouvelle note</strong>';
                $content = 'Nouvelle note sur votre torrent <strong style="color:#ececec">' . $details->torrent_name . '</strong>';
                $content .= '<br><span style="font-weight: bold;color: #15ccb1;font-size: 11px;text-transform: uppercase;">Il y a ' . $this->time_elapsed_string($notification->date) . '</span>';
                $target_url = base_url('engine/url_builder?type=torrent&id=' . $details->torrent_id);
                break;
            case self::TYPE_SANCTION_MUTE_SHOUTBOX:
                $avatar = base_url('static/img/avatar_staff.png');
                $head   = '<strong style="color: #ff2b2b">Mute Shoutbox</strong>';
                if ($details->duration == 'definitif') {
                    $content = sprintf('Vous avez été <strong>Mute Shoutbox</strong>, <strong>raison</strong> : « <span style="color:#92fff1">%s</span> » <strong>de façon définitive</strong>', $details->reason);
                } else {
                    $content = sprintf('Vous avez été <strong>Mute Shoutbox</strong>, <strong>raison</strong> : « <span style="color:#92fff1">%s</span> » pendant <strong>%s</strong>', $details->reason, $details->duration);
                }
                $content .= '<br><span style="font-weight: bold;color: #15ccb1;font-size: 11px;text-transform: uppercase;">Il y a ' . $this->time_elapsed_string($notification->date) . '</span>';
                $target_url = '#';
                break;
            case self::TYPE_SANCTION_MUTE_MP:
                $avatar = base_url('static/img/avatar_staff.png');
                $head   = '<strong style="color: #ff2b2b">Mute MP</strong>';
                if ($details->duration == 'definitif') {
                    $content = sprintf('Vous avez été <strong>Mute MP</strong>, <strong>raison</strong> : « <span style="color:#92fff1">%s</span> » <strong>de façon définitive</strong>', $details->reason);
                } else {
                    $content = sprintf('Vous avez été <strong>Mute MP</strong>, <strong>raison</strong> : « <span style="color:#92fff1">%s</span> » pendant <strong>%s</strong>', $details->reason, $details->duration);
                }
                $content .= '<br><span style="font-weight: bold;color: #15ccb1;font-size: 11px;text-transform: uppercase;">Il y a ' . $this->time_elapsed_string($notification->date) . '</span>';
                $target_url = '#';
                break;
            case self::TYPE_SANCTION_MUTE_COMMENT:
                $avatar = base_url('static/img/avatar_staff.png');
                $head   = '<strong style="color: #ff2b2b">Mute Commentaire</strong>';
                if ($details->duration == 'definitif') {
                    $content = sprintf('Vous avez été <strong>Mute Commentaire</strong>, <strong>raison</strong> : « <span style="color:#92fff1">%s</span> » <strong>de façon définitive</strong>', $details->reason);
                } else {
                    $content = sprintf('Vous avez été <strong>Mute Commentaire</strong>, <strong>raison</strong> : « <span style="color:#92fff1">%s</span> » pendant <strong>%s</strong>', $details->reason, $details->duration);
                }
                $content .= '<br><span style="font-weight: bold;color: #15ccb1;font-size: 11px;text-transform: uppercase;">Il y a ' . $this->time_elapsed_string($notification->date) . '</span>';
                $target_url = '#';
                break;
            case self::TYPE_SANCTION_NO_UPLOAD:
                $avatar = base_url('static/img/avatar_staff.png');
                $head   = '<strong style="color: #ff2b2b">Interdiction d\'upload</strong>';
                if ($details->duration == 'definitif') {
                    $content = sprintf('Vous avez été <strong>interdit d\'upload</strong>, <strong>raison</strong> : « <span style="color:#92fff1">%s</span> » <strong>de façon définitive</strong>', $details->reason);
                } else {
                    $content = sprintf('Vous avez été <strong>interdit d\'upload</strong>, <strong>raison</strong> : « <span style="color:#92fff1">%s</span> » pendant <strong>%s</strong>', $details->reason, $details->duration);
                }
                $content .= '<br><span style="font-weight: bold;color: #15ccb1;font-size: 11px;text-transform: uppercase;">Il y a ' . $this->time_elapsed_string($notification->date) . '</span>';
                $target_url = '#';
                break;
            case self::TYPE_SANCTION_BAN:
                $avatar = base_url('static/img/avatar_staff.png');
                $head   = '<strong style="color: #ff2b2b">Compte banni</strong>';
                if ($details->duration == 'definitif') {
                    $content = sprintf('Vous avez été <strong>banni</strong>, <strong>raison</strong> : « <span style="color:#92fff1">%s</span> » <strong>de façon définitive</strong>', $details->reason);
                } else {
                    $content = sprintf('Vous avez été <strong>banni</strong>, <strong>raison</strong> : « <span style="color:#92fff1">%s</span >» pendant <strong>%s</strong>', $details->reason, $details->duration);
                }
                $content .= '<br><span style="font-weight: bold;color: #15ccb1;font-size: 11px;text-transform: uppercase;">Il y a ' . $this->time_elapsed_string($notification->date) . '</span>';
                $target_url = '#';
                break;
        }
        return array('title' => $head, 'content' => $content, 'avatar' => $avatar, 'target_url' => $target_url);
    }

    /**
     * @param Notification - Array
     * @return Array
     */
    public function getNotificationTPLPage($notification)
    {
        $details = json_decode($notification->details);
        switch ($notification->event) {
            case self::TYPE_TORRENT_ASSENT:
                $title      = 'team pending';
                $icon       = 'certificate';
                $content    = '<strong>' . $details->torrent_name . '</strong> <font style="color:#34a720; font-weight:bold;">approuvé</font> par la Team Pending';
                $date       = $this->time_elapsed_string($notification->date);
                $target_url = base_url('engine/url_builder?type=torrent&id=' . $details->torrent_id);
                break;
            case self::TYPE_TORRENT_BLOCK:
                $title      = 'team pending';
                $icon       = 'certificate';
                $content    = '<strong>' . $details->torrent_name . '</strong> <font style="color:#ff9a68; font-weight:bold;">bloqué</font> par la Team Pending, <font style="font-weight:bold;">cliquez-ici pour voir les modifications à apporter</font>';
                $date       = $this->time_elapsed_string($notification->date);
                $target_url = base_url('engine/url_builder?type=torrent&id=' . $details->torrent_id);
                break;
            case self::TYPE_TORRENT_DEL:
                $title      = 'team pending';
                $icon       = 'certificate';
                $content    = '<strong>' . $details->torrent_name . '</strong> <font style="color:#df4b4b; font-weight:bold;">supprimé</font> par la Team Pending, <font style="font-weight:bold;">cliquez-ici pour voir les modifications à apporter</font>';
                $date       = $this->time_elapsed_string($notification->date);
                $target_url = base_url('engine/url_builder?type=torrent&id=' . $details->torrent_id);
                break;
            case self::TYPE_TORRENT_ADD_COMMENT:
                $title      = 'commentaire';
                $icon       = 'comment';
                $content    = '<strong>' . $details->from_uid_nickname . '</strong> a ajouté un commentaire sur votre torrent <strong>' . $details->torrent_name . '</strong>';
                $date       = $this->time_elapsed_string($notification->date);
                $target_url = base_url('engine/url_builder?type=focuscomment&id=' . $details->comment_id);
                break;
            case self::TYPE_TORRENT_ADD_NOTE:
                $title      = 'team pending';
                $icon       = 'certificate';
                $content    = 'Nouvelle note sur votre torrent <strong>' . $details->torrent_name . '</strong>';
                $date       = $this->time_elapsed_string($notification->date);
                $target_url = base_url('engine/url_builder?type=torrent&id=' . $details->torrent_id);
                break;
            case self::TYPE_SANCTION_MUTE_SHOUTBOX:
                $title      = 'modération';
                $icon       = 'close';
                if ($details->duration == 'definitif') {
                    $content = sprintf('Vous avez été <strong>Mute Shoutbox</strong>, <strong>raison</strong> : « <span style="color: #e4392f; font-weight: bold;">%s</span> » <strong>de façon définitive</strong>', $details->reason);
                } else {
                    $content = sprintf('Vous avez été <strong>Mute Shoutbox</strong>, <strong>raison</strong> : « <span style="color: #e4392f; font-weight: bold;">%s</span> » pendant <strong>%s</strong>', $details->reason, $details->duration);
                }
                $date       = $this->time_elapsed_string($notification->date);
                $target_url = '#';
                break;
            case self::TYPE_SANCTION_MUTE_MP:
                $title      = 'modération';
                $icon       = 'close';
                if ($details->duration == 'definitif') {
                    $content = sprintf('Vous avez été <strong>Mute MP</strong>, <strong>raison</strong> : « <span style="color: #e4392f; font-weight: bold;">%s</span> » <strong>de façon définitive</strong>', $details->reason);
                } else {
                    $content = sprintf('Vous avez été <strong>Mute MP</strong>, <strong>raison</strong> : « <span style="color: #e4392f; font-weight: bold;">%s</span> » pendant <strong>%s</strong>', $details->reason, $details->duration);
                }
                $date       = $this->time_elapsed_string($notification->date);
                $target_url = '#';
                break;
            case self::TYPE_SANCTION_MUTE_COMMENT:
                $title      = 'modération';
                $icon       = 'close';
                if ($details->duration == 'definitif') {
                    $content = sprintf('Vous avez été <strong>Mute Commentaire</strong>, <strong>raison</strong> : « <span style="color: #e4392f; font-weight: bold;">%s</span> » <strong>de façon définitive</strong>', $details->reason);
                } else {
                    $content = sprintf('Vous avez été <strong>Mute Commentaire</strong>, <strong>raison</strong> : « <span style="color: #e4392f; font-weight: bold;">%s</span> » pendant <strong>%s</strong>', $details->reason, $details->duration);
                }
                $date       = $this->time_elapsed_string($notification->date);
                $target_url = '#';
                break;
            case self::TYPE_SANCTION_NO_UPLOAD:
                $title      = 'modération';
                $icon       = 'close';
                if ($details->duration == 'definitif') {
                    $content = sprintf('Vous avez été <strong>interdit d\'upload</strong>, <strong>raison</strong> : « <span style="color: #e4392f; font-weight: bold;">%s</span> » <strong>de façon définitive</strong>', $details->reason);
                } else {
                    $content = sprintf('Vous avez été <strong>interdit d\'upload</strong>, <strong>raison</strong> : « <span style="color: #e4392f; font-weight: bold;">%s</span> » pendant <strong>%s</strong>', $details->reason, $details->duration);
                }
                $date       = $this->time_elapsed_string($notification->date);
                $target_url = '#';
                break;
            case self::TYPE_SANCTION_BAN:
                $title      = 'modération';
                $icon       = 'close';
                if ($details->duration == 'definitif') {
                    $content = sprintf('Vous avez été <strong>banni</strong>, <strong>raison</strong> : « <span style="color: #e4392f; font-weight: bold;">%s</span> » <strong>de façon définitive</strong>', $details->reason);
                } else {
                    $content = sprintf('Vous avez été <strong>banni</strong>, <strong>raison</strong> : « <span style="color: #e4392f; font-weight: bold;">%s</span> » pendant <strong>%s</strong>', $details->reason, $details->duration);
                }
                $date       = $this->time_elapsed_string($notification->date);
                $target_url = '#';
        }
        return array('title' => $title, 'icon' => $icon, 'content' => $content, 'date' => $date, 'target_url' => $target_url);
    }

    /**
     * @return mixed - record id or boolean if it fails
     */
    private function sanctionMuteShout_notification()
    {
        $notif['guid']                  = is_array($this->details) ? $this->details['user_id'] : $this->details;
        $notif['event']                 = $this->notificationType;
        $notif['details']['from_staff'] = $this->ci->session->id;
        $notif['details']['duration']   = $this->details['duration'];
        $notif['details']['reason']     = $this->details['reason'];
        $notif['details']               = json_encode($notif['details']);
        $notif['date']                  = now();
        // print_r($notif);
        $this->incrNotification($notif['guid']);
        $this->response = $this->ci->db->insert('notifications', $notif) ? $this->ci->db->insert_id() : false;
    }

    /**
     * @return mixed - record id or boolean if it fails
     */
    private function sanctionNoUp_notification()
    {
        $notif['guid']                  = is_array($this->details) ? $this->details['user_id'] : $this->details;
        $notif['event']                 = $this->notificationType;
        $notif['details']['from_staff'] = $this->ci->session->id;
        $notif['details']['duration']   = $this->details['duration'];
        $notif['details']['reason']     = $this->details['reason'];
        $notif['details']               = json_encode($notif['details']);
        $notif['date']                  = now();
        // print_r($notif);
        $this->incrNotification($notif['guid']);
        $this->response = $this->ci->db->insert('notifications', $notif) ? $this->ci->db->insert_id() : false;
    }

    /**
     * @return mixed - record id or boolean if it fails
     */
    private function sanctionBan_notification()
    {
        $notif['guid']                  = is_array($this->details) ? $this->details['user_id'] : $this->details;
        $notif['event']                 = $this->notificationType;
        $notif['details']['from_staff'] = $this->ci->session->id;
        $notif['details']['duration']   = $this->details['duration'];
        $notif['details']['reason']     = $this->details['reason'];
        $notif['details']               = json_encode($notif['details']);
        $notif['date']                  = now();
        // print_r($notif);
        $this->incrNotification($notif['guid']);
        $this->response = $this->ci->db->insert('notifications', $notif) ? $this->ci->db->insert_id() : false;
    }

    /**
     * @return mixed - record id or boolean if it fails
     */
    private function sanctionMuteComment_notification()
    {
        $notif['guid']                  = is_array($this->details) ? $this->details['user_id'] : $this->details;
        $notif['event']                 = $this->notificationType;
        $notif['details']['from_staff'] = $this->ci->session->id;
        $notif['details']['duration']   = $this->details['duration'];
        $notif['details']['reason']     = $this->details['reason'];
        $notif['details']               = json_encode($notif['details']);
        $notif['date']                  = now();
        // print_r($notif);
        $this->incrNotification($notif['guid']);
        $this->response = $this->ci->db->insert('notifications', $notif) ? $this->ci->db->insert_id() : false;
    }

    /**
     * @return mixed - record id or boolean if it fails
     */
    private function sanctionMuteMP_notification()
    {
        $notif['guid']                  = is_array($this->details) ? $this->details['user_id'] : $this->details;
        $notif['event']                 = $this->notificationType;
        $notif['details']['from_staff'] = $this->ci->session->id;
        $notif['details']['duration']   = $this->details['duration'];
        $notif['details']['reason']     = $this->details['reason'];
        $notif['details']               = json_encode($notif['details']);
        $notif['date']                  = now();
        // print_r($notif);
        $this->incrNotification($notif['guid']);
        $this->response = $this->ci->db->insert('notifications', $notif) ? $this->ci->db->insert_id() : false;
    }

    /**
     * @return mixed - record id or boolean if it fails
     */
    private function validationTorrent_notification()
    {
        $torrentid                        = is_array($this->details) ? $this->details['torrent_id'] : $this->details;
        $torrent                          = $this->getTorrent($torrentid);
        $notif['guid']                    = $torrent->uploader;
        $notif['event']                   = $this->notificationType;
        $notif['details']['from_staff']   = $this->ci->session->id;
        $notif['details']['torrent_id']   = $torrent->id;
        $notif['details']['torrent_name'] = $torrent->name;
        if ($this->notificationType != self::TYPE_TORRENT_ASSENT) {
            $notif['details']['note_id']      = $this->details['note_id'];
            $notif['details']['note_content'] = $this->details['note_content'];
        }
        $notif['details'] = json_encode($notif['details']);
        $notif['date']    = now();
        // print_r($notif);
        $this->incrNotification($notif['guid']);
        $this->response = $this->ci->db->insert('notifications', $notif) ? $this->ci->db->insert_id() : false;
    }

    /**
     * @return mixed - record id or boolean if it fails
     */
    private function addNote_notification()
    {
        $torrentid                        = is_array($this->details) ? $this->details['torrent_id'] : $this->details;
        $torrent                          = $this->getTorrent($torrentid);
        $notif['guid']                    = $torrent->uploader;
        $notif['event']                   = $this->notificationType;
        $notif['details']['from_staff']   = $this->ci->session->id;
        $notif['details']['torrent_id']   = $torrent->id;
        $notif['details']['torrent_name'] = $torrent->name;
        $notif['details']['note_id']      = $this->details['note_id'];
        $notif['details']['note_content'] = $this->details['note_content'];

        $notif['details'] = json_encode($notif['details']);
        $notif['date']    = now();
        // print_r($notif);
        $this->incrNotification($notif['guid']);
        $this->response = $this->ci->db->insert('notifications', $notif) ? $this->ci->db->insert_id() : false;
    }

    /**
     * @return mixed - record id or boolean if it fails
     */
    private function commentTorrent_notification()
    {
        $torrentid                        = is_array($this->details) ? $this->details['torrent_id'] : $this->details;
        $torrent                          = $this->getTorrent($torrentid);
        $notif['event']                   = $this->notificationType;
        $notif['guid']                    = $torrent->uploader;
        $notif['details']                 = array('from_uid' => $this->ci->session->id, 'from_uid_nickname' => $this->getNickname($this->ci->session->id), 'from_uid_avatar' => $this->ci->session->avatar);
        $notif['details']['torrent_id']   = $torrent->id;
        $notif['details']['torrent_name'] = $torrent->name;
        $notif['details']['comment_id']   = $this->details['comment_id'];
        $notif['details']                 = json_encode($notif['details']);
        $notif['date']                    = now();
        $this->incrNotification($notif['guid']);
        $this->response = $this->ci->db->insert('notifications', $notif) ? $this->ci->db->insert_id() : false;
    }

    public function getResponse()
    {
        return $this->response;
    }

    private function incrNotification($userID)
    {
        return $this->ci->db->where('id', $userID)->set('notifications', 'notifications+1', false)->update('users');
    }

    private function getNickname($userID)
    {
        return $this->ci->db->select('nickname')->where('id', $userID)->from('users')->get()->row()->nickname;
    }

    private function getTorrent($torrentID)
    {
        return $this->ci->db->select('id,name,uploader')->where('id', $torrentID)->from('torrents')->get()->row();
    }

    private function getNotification($notifID)
    {
        return $this->ci->db->where('id', $notifID)->get('notifications')->row();
    }

    private function time_elapsed_string($time, $full = false)
    {
        $today    = time();
        $datediff = abs($today - $time);
        $difftext = "";
        $years    = floor($datediff / (365 * 60 * 60 * 24));
        $months   = floor(($datediff - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
        $days     = floor(($datediff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
        $hours    = floor($datediff / 3600);
        $minutes  = floor($datediff / 60);
        $seconds  = floor($datediff);
        //year checker
        if ($difftext == "") {
            if ($years > 1) {
                $difftext = "" . $years . " ans";
            } elseif ($years == 1) {
                $difftext = "" . $years . "";
            }

        }
        //month checker
        if ($difftext == "") {
            if ($months > 1) {
                $difftext = "" . $months . " mois";
            } elseif ($months == 1) {
                $difftext = "" . $months . " mois";
            }

        }
        //month checker
        if ($difftext == "") {
            if ($days > 1) {
                $difftext = "" . $days . " jours";
            } elseif ($days == 1) {
                $difftext = "" . $days . " jour";
            }

        }
        //hour checker
        if ($difftext == "") {
            if ($hours > 1) {
                $difftext = "" . $hours . " heures";
            } elseif ($hours == 1) {
                $difftext = "" . $hours . " heure";
            }

        }
        //minutes checker
        if ($difftext == "") {
            if ($minutes > 1) {
                $difftext = "" . $minutes . " minutes";
            } elseif ($minutes == 1) {
                $difftext = "" . $minutes . " minute";
            }

        }
        //seconds checker
        if ($difftext == "") {
            if ($seconds > 1) {
                $difftext = "" . $seconds . " secondes";
            } elseif ($seconds == 1) {
                $difftext = "" . $seconds . " seconde";
            } elseif ($seconds == 0) {
                $difftext = "à l'instant";
            }

        }
        return $difftext;
    }

}
