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

Fix poll configuration and remove CMSObject #9750

Merged
merged 6 commits into from
Oct 7, 2024
Merged
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
44 changes: 19 additions & 25 deletions src/libraries/kunena/src/Forum/Topic/Poll/KunenaPoll.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Joomla\CMS\Factory;
use Joomla\CMS\Filter\InputFilter;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Object\CMSObject;
use Joomla\CMS\Table\Table;
use Joomla\Database\DatabaseDriver;
use Joomla\Database\DatabaseInterface;
Expand All @@ -40,7 +39,7 @@
* @property string $title
* @since Kunena 6.0
*/
class KunenaPoll extends CMSObject
class KunenaPoll
{
public $id;

Expand Down Expand Up @@ -115,8 +114,6 @@ public function __construct($identifier = 0)
// Always load the topic -- if poll does not exist: fill empty data
$this->_db = Factory::getContainer()->get('DatabaseDriver');
$this->load($identifier);

parent::__construct($identifier);
}

/**
Expand All @@ -142,7 +139,7 @@ public function load($id)
$this->_exists = $table->load($id);

// Assuming all is well at this point lets bind the data
$this->setProperties($table->getProperties());
$this->bind($table->getProperties());

return $this->_exists;
}
Expand Down Expand Up @@ -364,24 +361,18 @@ public function vote(int $option, $change = false, $user = null): bool
{
if (!$this->exists()) {
throw new Exception(Text::_('COM_KUNENA_LIB_POLL_VOTE_ERROR_DOES_NOT_EXIST'));

return false;
}

$options = $this->getOptions();

if (!isset($options[$option])) {
throw new Exception(Text::_('COM_KUNENA_LIB_POLL_VOTE_ERROR_OPTION_DOES_NOT_EXIST'));

return false;
}

$user = KunenaFactory::getUser($user);

if (!$user->exists()) {
throw new Exception(Text::_('COM_KUNENA_LIB_POLL_VOTE_ERROR_USER_NOT_EXIST'));

return false;
}

$lastVoteId = $this->getLastVoteId($user);
Expand Down Expand Up @@ -446,8 +437,6 @@ public function vote(int $option, $change = false, $user = null): bool
KunenaError::displayDatabaseError($e);

throw new Exception(Text::_('COM_KUNENA_LIB_POLL_VOTE_ERROR_USER_INSERT_FAIL'));

return false;
}
} else {
// Already voted
Expand All @@ -473,8 +462,6 @@ public function vote(int $option, $change = false, $user = null): bool
KunenaError::displayDatabaseError($e);

throw new Exception(Text::_('COM_KUNENA_LIB_POLL_VOTE_ERROR_USER_UPDATE_FAIL'));

return false;
}
}

Expand Down Expand Up @@ -588,8 +575,6 @@ protected function changeOptionVotes(int $option, int $delta): bool
KunenaError::displayDatabaseError($e);

throw new Exception(Text::_('COM_KUNENA_LIB_POLL_VOTE_ERROR_OPTION_SAVE_FAIL'));

return false;
}

return true;
Expand All @@ -609,7 +594,9 @@ public function bind(array $data, array $allow = []): void
$data = array_intersect_key($data, array_flip($allow));
}

$this->setProperties($data);
foreach ((array) $data as $property => $value) {
$this->$property = $value;
}
}

/**
Expand All @@ -626,6 +613,8 @@ public function delete(): bool
return true;
}

$success = true;

// Create the table object
$table = $this->getTable();

Expand All @@ -648,10 +637,11 @@ public function delete(): bool
$db->execute();
} catch (ExecutionFailureException $e) {
KunenaError::displayDatabaseError($e);
$success = false;
}

// Delete votes
$query = $db->getQuery();
$query = $db->createQuery();
$query->delete($db->quoteName('#__kunena_polls_users'))
->where($db->quoteName('pollid') . ' = ' . $db->quote($this->id));
$db->setQuery($query);
Expand All @@ -660,6 +650,7 @@ public function delete(): bool
$db->execute();
} catch (ExecutionFailureException $e) {
KunenaError::displayDatabaseError($e);
$success = false;
}

// Remove poll from the topic
Expand Down Expand Up @@ -697,27 +688,30 @@ public function getTimeToLive(): int
/**
* Method to save the \Kunena\Forum\Libraries\Forum\Topic\TopicPoll object to the database.
*
* @param bool $updateOnly Save the object only if not a new poll.
*
* @return boolean True on success.
*
* @throws Exception
* @since Kunena 6.0
*/
public function save($updateOnly = false): bool
public function save(): bool
{
// Are we creating a new poll
$isnew = !$this->_exists;

if ($isnew && empty($this->newOptions)) {
throw new Exception(Text::_('COM_KUNENA_LIB_POLL_SAVE_ERROR_NEW_AND_NO_OPTIONS'));

return false;
}

// Create the topics table object
$table = $this->getTable();
$table->bind($this->getProperties());

$properties = [
'title' => $this->title,
'threadid' => $this->threadid,
'polltimetolive' => $this->polltimetolive
];

$table->bind($properties);
$table->exists($this->_exists);

// Store the topic data in the database
Expand Down
163 changes: 138 additions & 25 deletions src/media/kunena/core/js/plugins/polls/dialogs/polls.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,44 @@ CKEDITOR.dialog.add( 'pollsDialog', function( editor ) {
var options = null;
var nboptionsmax = jQuery('#nb_options_allowed').val();

function createNewOptionField(optionText) {
function createNewOptionField(optionText, optionId, isNew) {
options++;
var paragraph = new CKEDITOR.dom.element( 'p' );
paragraph.setStyle( 'margin-top', '5px' );
var checkbox = new CKEDITOR.dom.element('input');
checkbox.setAttribute('type', 'checkbox');
checkbox.addClass('polloptioncheck');
if (optionId !== undefined) {
checkbox.setAttribute('id', 'polloptioncheck' + optionId);
checkbox.setAttribute('name', 'polloptioncheck[' + optionId + ']');
} else {
checkbox.setAttribute('id', 'polloptioncheck' + options);
checkbox.setAttribute('name', 'polloptioncheck[' + options + ']');
}
paragraph.append(checkbox);
var label = new CKEDITOR.dom.element( 'label' );
label.appendText(Joomla.Text._('COM_KUNENA_POLL_OPTION_NAME')+ ' ' + options + ' ');
label.setAttribute('id', 'labeloption' + options);
if (optionId !== undefined) {
label.setAttribute('id', 'labeloption' + optionId);
} else {
label.setAttribute('id', 'labeloption' + options);
}
paragraph.append( label );
var br = new CKEDITOR.dom.element( 'br' );
paragraph.append( br);
var inputField = new CKEDITOR.dom.element( 'input' );
inputField.addClass( 'kunenackeditorpolloption' );
inputField.addClass( 'cke_dialog_ui_input_text' );
inputField.setAttribute('id', 'field_option' + options);
inputField.setAttribute('name', 'polloptionsID[new' + options + ']' );
if (optionId !== undefined && (isNew === undefined || !isNew)) {
inputField.setAttribute('id', 'field_option' + optionId);
inputField.setAttribute('name', 'polloptionsID[' + optionId + ']');
} else if (optionId && isNew) {
inputField.setAttribute('id', 'field_option' + optionId);
inputField.setAttribute('name', 'polloptionsID[new' + optionId + ']');
} else {
inputField.setAttribute('id', 'field_option' + options);
inputField.setAttribute('name', 'polloptionsID[new' + options + ']');
}
inputField.setAttribute('type', 'text');
inputField.setAttribute('maxLength', 100);
if(optionText!==undefined)
Expand Down Expand Up @@ -69,10 +92,11 @@ CKEDITOR.dialog.add( 'pollsDialog', function( editor ) {
createNewOptionField();
}
else {
// TODO : Hide button add

console.log('max options reach ');
}
if (options >= nboptionsmax) {
this.disable();
}
}
},
{
Expand All @@ -81,14 +105,46 @@ CKEDITOR.dialog.add( 'pollsDialog', function( editor ) {
label: Joomla.Text._('COM_KUNENA_POLL_REMOVE_POLL_OPTION'),
title: Joomla.Text._('COM_KUNENA_POLL_REMOVE_POLL_OPTION'),
onClick: function() {
if (options > 0) {
// this = CKEDITOR.ui.dialog.button
jQuery('#field_option' + options).remove();
jQuery('#labeloption' + options).remove();
options--;
}
jQuery('.polloptioncheck:checked').each(function () {
var optionName = jQuery(this).attr('name');
var optionNew = optionName.match(/\[(new)\d+\]/);
var optionId;
if (optionNew) {
optionId = optionName.match(/\[new(\d+)\]/)[1];
} else {
optionId = optionName.match(/\[(\d+)\]/)[1];
}
jQuery('#field_option' + optionId).closest('p').remove();
if (options > 0) {
options--;
}
});
if (options < nboptionsmax) {
var button = this._.dialog.getContentElement('tab-basic', 'polladdoption');
button.enable();
}
counter = 0;
jQuery('#dynamicContent p').each(function () {
counter++;
var childInput = jQuery(this).children('.kunenackeditorpolloption').first();
var childlabel = jQuery(this).children('label').first();
childlabel.text(Joomla.Text._('COM_KUNENA_POLL_OPTION_NAME') + ' ' + counter + ' ');
childlabel.attr('id', 'labeloption' + counter);
var optionName = childInput.attr('name');
var optionNew = optionName.match(/\[(new)\d+\]/);
if (optionNew) {
var optionId = optionName.match(/\[new(\d+)\]/)[1];
if (optionId != counter) {
var polloptioncheck = jQuery(this).children('.polloptioncheck').first();
polloptioncheck.attr('id', 'polloptioncheck' + counter);
polloptioncheck.attr('name', 'polloptioncheck[' + counter + ']');
childInput.attr('id', 'field_option' + counter);
childInput.attr('name', 'polloptionsID[new' + counter + ']');
}
} else {

// TODO : show button hide if it was hidden
}
});
}
},
{
Expand All @@ -104,7 +160,9 @@ CKEDITOR.dialog.add( 'pollsDialog', function( editor ) {
// Apply the datepicker to the input control
jQuery(theInput.selector).datepicker({
showButtonPanel: true,
format: "yyyy-mm-dd"
format: "yyyy-mm-dd",
todayHighlight: true,
autoclose: true
});
},
},
Expand All @@ -124,6 +182,7 @@ CKEDITOR.dialog.add( 'pollsDialog', function( editor ) {
}
],
onOk: function() {
jQuery( '#poll_options' ).empty();
var inputTitlePoll = new CKEDITOR.dom.element( 'input' );
inputTitlePoll.setAttribute('type', 'hidden');
inputTitlePoll.setAttribute('name', 'poll_title' );
Expand All @@ -136,23 +195,77 @@ CKEDITOR.dialog.add( 'pollsDialog', function( editor ) {
inputPollTTL.setAttribute('value', this.getValueOf( 'tab-basic', 'polllifespan' ) );
CKEDITOR.document.getById( 'poll_options' ).append( inputPollTTL );

jQuery('.kunenackeditorpolloption').each(function(index) {
index++
var inputPollOption = new CKEDITOR.dom.element( 'input' );
inputPollOption.setAttribute('type', 'hidden');
inputPollOption.setAttribute('name', 'polloptionsID['+index+']' );
inputPollOption.setAttribute('value', jQuery('#field_option'+index).val() );
CKEDITOR.document.getById( 'poll_options' ).append( inputPollOption );
});
jQuery('.kunenackeditorpolloption').each(function (index) {
index++
var optionName = jQuery(this).attr('name');
var optionNew = optionName.match(/\[(new)\d+\]/);
if (optionNew) {
var optionId = optionName.match(/\[new(\d+)\]/)[1];
} else {
var optionId = optionName.match(/\[(\d+)\]/)[1];
}
var inputPollOption = new CKEDITOR.dom.element('input');
inputPollOption.setAttribute('type', 'hidden');
if (optionNew) {
inputPollOption.setAttribute('name', 'polloptionsID[' + optionNew[1] + optionId + ']');
} else {
inputPollOption.setAttribute('name', 'polloptionsID[' + optionId + ']');
}
inputPollOption.setAttribute('value', jQuery('#field_option' + optionId).val());
CKEDITOR.document.getById('poll_options').append(inputPollOption);
});
},
onCancel: function () {
jQuery('#dynamicContent').empty();
options = 0;
},
onShow: function() {
if (jQuery('#poll_exist_edit') !== undefined) {
this.setValueOf( 'tab-basic', 'polltitle', jQuery('#ckeditor_dialog_polltitle').val() );
this.setValueOf( 'tab-basic', 'polllifespan', jQuery('#ckeditor_dialog_polltimetolive').val() );

var polloptions = jQuery('#poll_options input[name*="polloptionsID"]');
var polloptionsset = jQuery('#poll_options').children().length > 0;
var polloptionsIDs = [];
var polloptionsNewIDs = [];
if (polloptions) {
polloptions.each(function () {
var optionName = jQuery(this).attr('name');
var optionId = optionName.match(/\[(\d+)\]$/);
if (optionId) {
polloptionsIDs.push(optionId[1]);
}
var optionNewId = optionName.match(/\[new(\d+)\]$/);
if (optionNewId) {
polloptionsNewIDs.push(optionNewId[1]);
}
});
}

jQuery('.ckeditor_dialog_polloption').each(function () {
var optionName = jQuery(this).attr('name');
var optionId = optionName.match(/\d+$/);
if (jQuery('#field_option' + optionId).length === 0) {
if (!polloptionsset
|| (polloptionsset && polloptionsIDs.length > 0 && polloptionsIDs.includes(optionId[0]))
) {
createNewOptionField(jQuery(this).val(), optionId);
}
}
});
jQuery('#poll_options input[name*="polloptionsID[new"]').each(function () {
var optionName = jQuery(this).attr('name');
var optionId = optionName.match(/\[new(\d+)\]$/);

if (optionId && jQuery('#field_option' + optionId[1]).length === 0) {
createNewOptionField(jQuery(this).val(), optionId[1], true);
}
});

jQuery('.ckeditor_dialog_polloption').each(function () {
createNewOptionField(jQuery(this).val());
});
if (options >= nboptionsmax) {
var button = this.getContentElement('tab-basic', 'polladdoption');
button.disable();
}
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/site/src/Controllers/TopicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1249,7 +1249,7 @@ public function edit()
'icon_id' => $this->app->input->getInt('topic_emoticon', $topic->icon_id),
'anonymous' => $this->app->input->getInt('anonymous', 0),
'poll_title' => $this->app->input->getString('poll_title', null),
'poll_options' => $this->app->input->get('polloptionsID', [], 'post', 'array'),
'poll_options' => $this->app->input->get('polloptionsID', [], 'array'),
'poll_time_to_live' => $this->app->input->getString('poll_time_to_live', 0),
'subscribe' => $this->app->input->getInt('subscribeMe', 0),
'private' => (string) $this->input->getRaw('private_message'),
Expand Down
Loading
Loading