Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added missing localised routes and hooked up localisations #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions PollsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function registerCpRoutes()
{
return array(

// list answers in non-primary locale
'polls/(?P<pollHandle>{handle})/questions/(?P<questionId>\d+)/(?P<localeId>\w+)/answers'
=> array('action' => 'polls/answers/answersIndex'),

'polls/(?P<pollHandle>{handle})/questions/(?P<questionId>\d+)/answers'
=> array('action' => 'polls/answers/answersIndex'),

Expand All @@ -68,6 +72,10 @@ public function registerCpRoutes()
'polls/(?P<pollHandle>{handle})/questions/(?P<questionId>\d+)/options/new'
=> array('action' => 'polls/options/editOption'),

// list options in non-primary locale
'polls/(?P<pollHandle>{handle})/questions/(?P<questionId>\d+)/(?P<localeId>\w+)/options'
=> array('action' => 'polls/options/optionsIndex'),

'polls/(?P<pollHandle>{handle})/questions/(?P<questionId>\d+)/options'
=> array('action' => 'polls/options/optionsIndex'),

Expand Down
20 changes: 18 additions & 2 deletions controllers/Polls_OptionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ public function actionOptionsIndex(array $variables = array())
$variables['poll'] = craft()->polls->getPollById($variables['pollId']);
}

// hook up the selected locale
if (!empty($variables['localeId'])) {
$localeId = $variables['localeId'];
} else {
$localeId = craft()->getLanguage();
}

if (!empty($variables['questionId']))
{
$variables['question'] = craft()->polls_questions->getQuestionById($variables['questionId']);
$variables['question'] = craft()->polls_questions->getQuestionById($variables['questionId'], $localeId);

if (!$variables['question'])
{
Expand Down Expand Up @@ -58,9 +65,16 @@ public function actionEditOption(array $variables = array())
$variables['poll'] = craft()->polls->getPollById($variables['pollId']);
}

// hook up the selected locale
if (!empty($variables['localeId'])) {
$localeId = $variables['localeId'];
} else {
$localeId = craft()->getLanguage();
}

if (!empty($variables['questionId']))
{
$variables['question'] = craft()->polls_questions->getQuestionById($variables['questionId']);
$variables['question'] = craft()->polls_questions->getQuestionById($variables['questionId'], $localeId);

if (!$variables['question'])
{
Expand Down Expand Up @@ -266,6 +280,8 @@ public function actionSaveOption()
$option->questionId = craft()->request->getPost('questionId', $option->questionId);
$option->typeId = craft()->request->getPost('typeId', $option->typeId);
$option->kind = craft()->request->getPost('kind', $option->kind);
// hook up the current locale
$option->locale = $localeId;

$option->getContent()->title = craft()->request->getPost('title', $option->title);
$option->setContentFromPost('fields');
Expand Down
6 changes: 4 additions & 2 deletions elementtypes/Polls_QuestionElementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ public function getTableAttributeHtml(BaseElementModel $element, $attribute)
{
case 'options':
{
$question = craft()->polls_questions->getQuestionById($element->id);
// hook up the current locale
$question = craft()->polls_questions->getQuestionById($element->id, $element->locale);
$optionsCount = count($question->options);
$params = array(
'url' => $question->getCpEditUrl().'/options',
Expand Down Expand Up @@ -204,7 +205,8 @@ public function getTableAttributeHtml(BaseElementModel $element, $attribute)

case 'results':
{
$question = craft()->polls_questions->getQuestionById($element->id);
// hook up the current locale
$question = craft()->polls_questions->getQuestionById($element->id, $element->locale);
$params = array(
'url' => $question->getCpEditUrl().'/answers',
'count' => $question->totalAnswers,
Expand Down
5 changes: 3 additions & 2 deletions models/Polls_AnswerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class Polls_AnswerModel extends BaseModel
// Public Methods
// =========================================================================

public function getOption()
public function getOption($localeId = null)
{
return craft()->polls_options->getOptionById($this->optionId);
// hooked up localisation
return craft()->polls_options->getOptionById($this->optionId, $localeId);
}

public function getUser()
Expand Down
3 changes: 2 additions & 1 deletion models/Polls_OptionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public function getPoll()
*/
public function getQuestion()
{
return craft()->polls_questions->getQuestionById($this->questionId);
// hook up the current locale
return craft()->polls_questions->getQuestionById($this->questionId, $this->locale);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion models/Polls_QuestionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function getOptions()
{
if (!isset($this->_options))
{
$this->_options = craft()->polls_options->getOptionsByQuestionId($this->id);
// hooked up localisation
$this->_options = craft()->polls_options->getOptionsByQuestionId($this->id, $this->locale);
}
return $this->_options;
}
Expand Down
8 changes: 7 additions & 1 deletion services/Polls_OptionsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ public function getOptionById($optionId, $localeId = null)
return craft()->elements->getElementById($optionId, Polls_ElementType::Option, $localeId);
}

public function getOptionsByQuestionId($questionId)
public function getOptionsByQuestionId($questionId, $localeId = null)
{
$params = array('questionId' => $questionId);

// hooked up localisation
if ($localeId !== null) {
$params['locale'] = $localeId;
}

$criteria = craft()->elements->getCriteria(Polls_ElementType::Option, $params);
return $criteria->find();
}
Expand Down
5 changes: 4 additions & 1 deletion templates/answers/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@
<td class="thin"></td>
</thead>
<tbody>
{% if localeId is not defined %}
{% set localeId = null %}
{% endif %}
{% for answer in answers %}
<tr data-id="{{ answer.id }}" data-name="Answer {{ answer.id }}">
<th scope="row" data-title="{{ 'ID'|t }}">{{ answer.id }}</th>
<td scope="row" data-title="{{ 'Option'|t }}"><a href="{{ answer.option.getCpEditUrl() }}">{{ answer.option.title }}</a></td>
<td scope="row" data-title="{{ 'Option'|t }}"><a href="{{ answer.option(localeId).getCpEditUrl() }}">{{ answer.option(localeId).title }}</a></td>
<td scope="row" data-title="{{ 'User'|t }}">
{% if answer.user %}
<a href="{{ answer.user.getCpEditUrl() }}">{{ answer.user.username }}</a>
Expand Down
3 changes: 2 additions & 1 deletion templates/options/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
{% block main %}
<form id="option-form" method="post" accept-charset="UTF-8" data-saveshortcut="1" data-saveshortcut-redirect="{{ continueEditingUrl }}">
<input type="hidden" name="action" value="polls/options/saveOption">
<input type="hidden" name="redirect" value="polls/{{ poll.handle }}{{ questionSegment }}/options">
{# make sure redirect works regardless of the locale #}
<input type="hidden" name="redirect" value="polls/{{ poll.handle }}{{ questionSegment }}/{{ option.locale }}/options">
{% if question is defined %}
<input type="hidden" name="questionId" value="{{ question.id }}">
{% endif %}
Expand Down
3 changes: 2 additions & 1 deletion templates/options/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

{% block sidebar %}
<div class="buttons">
<a class="btn submit add icon" href="{{ url('polls/'~poll.handle~'/questions/'~question.id~'/options/new') }}">{{ "New Option"|t }}</a>
{# allow to create new options regardless of the locale #}
<a class="btn submit add icon" href="{{ url('polls/'~poll.handle~'/questions/'~question.id~'/options/new/'~question.locale) }}">{{ "New Option"|t }}</a>
</div>

{{ parent() }}
Expand Down