<?php

/*
 * This file is part of Phraseanet
 *
 * (c) 2005-2016 Alchemy
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Model\Entities\User;

class eventsmanager_notify_validate extends eventsmanager_notifyAbstract
{
    public function __construct(Application $app)
    {
        parent::__construct($app);
        $this->group = $this->app->trans('Validation');
    }

    /**
     *
     * @return string
     */
    public function icon_url()
    {
        return '/assets/common/images/icons/push16.png';
    }

    /**
     *
     * @param  string  $datas
     * @param  boolean $unread
     * @return Array
     */
    public function datas(array $data, $unread)
    {
        $from = $data['from'];
        $ssel_id = $data['ssel_id'];
        $isVoteBasket = !empty($data['isVoteBasket']) ? $data['isVoteBasket'] : false;

        if (null === $user = $this->app['repo.users']->find($from)) {
            return [];
        }

        $sender = $user->getDisplayName();

        try {
            $basket = $this->app['converter.basket']->convert($ssel_id);
            $basket_name = trim($basket->getName()) ? : $this->app->trans('Une selection');
        } catch (\Exception $e) {
            $basket_name = $this->app->trans('Une selection');
        }

        $bask_link = '<a href="'
            . $this->app->url('lightbox_validation', ['basket' => $ssel_id])
            . '" target="_blank">'
            . htmlentities($basket_name) . '</a>';

        if ($isVoteBasket) {
            $text = $this->app->trans('%user% vous demande de valider %title%', [
                '%user%' => htmlentities($sender),
                '%title%' => $bask_link,
            ]);

        } else {
            $text = $this->app->trans("notification:: Basket '%title%' shared from %user%", [
                '%user%' => htmlentities($sender),
                '%title%' => $bask_link,
            ]);
        }

        $ret = [
            'text'  => $this->app->trans($text, [
                '%user%' => htmlentities($sender),
                '%title%' => $bask_link,
            ])
            , 'class' => ($unread == 1 ? 'reload_baskets' : '')
        ];

        return $ret;
    }

    /**
     *
     * @return string
     */
    public function get_name()
    {
        return $this->app->trans('Validation');
    }

    /**
     *
     * @return string
     */
    public function get_description()
    {
        return $this->app->trans('Recevoir des notifications lorsqu\'on me demande une validation');
    }

    /**
     * @param integer $usr_id The id of the user to check
     *
     * @return boolean
     */
    public function is_available(User $user)
    {
        return true;
    }
}
