Files
le-matelot/local/modules/Contest/Contest.php
2020-01-27 08:56:08 +01:00

119 lines
3.5 KiB
PHP

<?php
/**
* This class has been generated by TheliaStudio
* For more information, see https://github.com/thelia-modules/TheliaStudio
*/
namespace Contest;
use Contest\Model\AnswerQuery;
use Contest\Model\Base\GameQuery;
use Contest\Model\ParticipateQuery;
use Contest\Model\QuestionQuery;
use Thelia\Core\Translation\Translator;
use Thelia\Model\Lang;
use Thelia\Model\LangQuery;
use Thelia\Model\Message;
use Thelia\Model\MessageQuery;
use Thelia\Module\BaseModule;
use Propel\Runtime\Connection\ConnectionInterface;
use Thelia\Install\Database;
/**
* Class Contest
* @package Contest
*/
class Contest extends BaseModule
{
const MESSAGE_DOMAIN = "contest";
const ROUTER = "router.contest";
//Mail
const MESSAGE_WIN = "contest_confirm_win";
const MESSAGE_FRIEND = "contest_send_friend";
//Options
const WIN_OPTION = "contest_show_win_page";
const CONNECT_OPTION = "contest_must_connected";
const FRIEND_OPTION = "contest_can_invite_friend";
const FRIEND_MAX_OPTION = "contest_max_invitation";
const MAX_PARTICIPATE_OPTION = "contest_max_participate";
/** @var Translator $translator */
protected $translator;
protected function trans($id, $locale, $parameters = [])
{
if ($this->translator === null) {
$this->translator = Translator::getInstance();
}
return $this->translator->trans($id, $parameters, self::MESSAGE_DOMAIN, $locale);
}
public function postActivation(ConnectionInterface $con = null)
{
try {
GameQuery::create()->findOne();
QuestionQuery::create()->findOne();
AnswerQuery::create()->findOne();
ParticipateQuery::create()->findOne();
} catch (\Exception $e) {
$database = new Database($con);
$database->insertSql(null, [__DIR__ . "/Config/create.sql", __DIR__ . "/Config/insert.sql"]);
}
//MAIL DECLARATION
$this->declareMessage(self::MESSAGE_WIN, 'You win', 'You win');
$this->declareMessage(self::MESSAGE_FRIEND, 'You get invited to a game', 'You get invited to a game');
//OPTION INIT
$this->initOption(self::WIN_OPTION,true);
$this->initOption(self::CONNECT_OPTION,false);
$this->initOption(self::FRIEND_OPTION,false);
$this->initOption(self::FRIEND_MAX_OPTION,5);
$this->initOption(self::MAX_PARTICIPATE_OPTION,1);
}
protected function declareMessage($name, $subject, $title)
{
$languages = LangQuery::create()->find();
if (null === MessageQuery::create()->findOneByName($name)) {
$message = new Message();
$message
->setName($name)
->setHtmlLayoutFileName('')
->setHtmlTemplateFileName($name . '.html')
->setTextLayoutFileName('')
->setTextTemplateFileName($name . '.txt');
foreach ($languages as $language) {
/** @var Lang $language */
$locale = $language->getLocale();
$message->setLocale($locale);
$message->setTitle(
$this->trans($title, $locale)
);
$message->setSubject(
$this->trans($subject, $locale)
);
}
$message->save();
}
}
protected function initOption($name, $value)
{
if (!$this->getConfigValue($name)) {
$this->setConfigValue($name, $value);
}
}
}