<?php
/**
 * 2019-2020 Team Ever
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 *  @author    Team Ever <https://www.team-ever.com/>
 *  @copyright 2019-2020 Team Ever
 *  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

require_once(dirname(__FILE__).'/../../config/config.inc.php');
require_once(dirname(__FILE__).'/../../init.php');

$smileys = array(
    '😀',
    '😁',
    '😃',
    '😄',
    '😇',
    '😉',
    '😊',
    '😋',
    '😌',
    '😎',
    '😏',
    '😗',
    '😘',
    '😙',
    '😚',
    '😛',
    '😜',
    '😝',
    '😬',
    '😶',
    '🙂',
    '🙃',
    '🙄',
    '🤐',
    '🤫',
    '🧐'
);
$randSmiley = array_rand($smileys);
if (!Tools::getIsset('token')
    || Tools::substr(Tools::encrypt('everpsblog/cron'), 0, 10) != Tools::getValue('token')
    || !Module::isInstalled('everpsblog')
) {
    die($smileys[$randSmiley].' Holy crap ! Invalid token... '.$smileys[$randSmiley]);
}

$everpsblog = Module::getInstanceByName('everpsblog');

if (!$everpsblog->active) {
    die($smileys[$randSmiley].' Sorry, the module EverPsBlog is not active '.$smileys[$randSmiley]);
}
/* Check if the requested shop exists */
$shops = Db::getInstance()->ExecuteS('SELECT id_shop FROM `'._DB_PREFIX_.'shop`');

$list_id_shop = array();
foreach ($shops as $shop) {
    $list_id_shop[] = (int)$shop['id_shop'];
}

$id_shop = (Tools::getIsset('id_shop') && in_array(Tools::getValue('id_shop'), $list_id_shop))
    ? (int)Tools::getValue('id_shop') : (int)Configuration::get('PS_SHOP_DEFAULT');

$everpsblog->cron = true;
if ($everpsblog->sendPendingNotification((int)$id_shop)) {
    die($smileys[$randSmiley].' All emails for pending posts have been sent '.$smileys[$randSmiley]);
}
