Skip to content

Commit

Permalink
Validation updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kinerity committed Mar 4, 2019
1 parent d82a6ee commit 6545eb7
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 52 deletions.
2 changes: 1 addition & 1 deletion config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ kinerity_bestanswer_controller:
path: /answer/{action}
defaults: { _controller: kinerity.bestanswer.controller:change_post_status }
requirements:
action: mark_answer|unmark_answer
action: 'mark_answer|unmark_answer'
1 change: 1 addition & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ services:
arguments:
- '@auth'
- '@dbal.conn'
- '@language'
- '@log'
- '@request'
- '@user'
Expand Down
17 changes: 11 additions & 6 deletions controller/main_controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class main_controller
/* @var \phpbb\db\driver\driver_interface */
protected $db;

/* @var \phpbb\language\language */
protected $lang;

/* @var \phpbb\log\log */
protected $log;

Expand All @@ -36,11 +39,12 @@ class main_controller
/* @var string */
protected $php_ext;

/**
* Constructor
/**
*
* @param \phpbb\auth\auth $auth
* @param \phpbb\db\driver\driver_interface $db
* @param \phpbb\language\language $lang
* @param \phpbb\log\log $log
* @param \phpbb\request\request $request
* @param \phpbb\user $user
Expand All @@ -51,6 +55,7 @@ public function __construct(\phpbb\auth\auth $auth, \phpbb\db\driver\driver_inte
{
$this->auth = $auth;
$this->db = $db;
$this->lang = $lang;
$this->log = $log;
$this->request = $request;
$this->user = $user;
Expand Down Expand Up @@ -87,22 +92,22 @@ public function change_post_status($action)
// Error checking
if (!$topic_data['enable_answer'])
{
throw new \phpbb\exception\http_exception(403, $this->user->lang('NOT_AUTHORISED'));
throw new \phpbb\exception\http_exception(403, $this->lang->lang('NOT_AUTHORISED'));
}

if (!$this->auth->acl_get('m_mark_answer', (int) $topic_data['forum_id']) && (!$this->auth->acl_get('f_mark_answer', (int) $topic_data['forum_id']) && $topic_data['topic_poster'] != (int) $this->user->data['user_id']))
{
throw new \phpbb\exception\http_exception(403, $this->user->lang('NOT_AUTHORISED'));
throw new \phpbb\exception\http_exception(403, $this->lang->lang('NOT_AUTHORISED'));
}

if ((int) $topic_data['topic_first_post_id'] == (int) $post_id)
{
throw new \phpbb\exception\http_exception(404, $this->user->lang('NOT_AUTHORISED'));
throw new \phpbb\exception\http_exception(404, $this->lang->lang('NOT_AUTHORISED'));
}

if ((int) $topic_data['topic_status'] == ITEM_LOCKED && !$this->auth->acl_get('m_mark_answer', (int) $topic_data['forum_id']))
{
throw new \phpbb\exception\http_exception(403, $this->user->lang('NOT_AUTHORISED'));
throw new \phpbb\exception\http_exception(403, $this->lang->lang('NOT_AUTHORISED'));
}

$log_var = $this->auth->acl_get('m_mark_answer', (int) $topic_data['forum_id']) ? 'mod' : 'user';
Expand Down Expand Up @@ -171,7 +176,7 @@ public function change_post_status($action)
}
else
{
confirm_box(false, $this->user->lang(strtoupper($action) . '_CONFIRM'));
confirm_box(false, $this->lang->lang(strtoupper($action) . '_CONFIRM'));
}

// Redirect back to the post
Expand Down
58 changes: 18 additions & 40 deletions event/main_listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ static public function getSubscribedEvents()
'core.permissions' => 'permissions',

'core.search_modify_tpl_ary' => 'modify_topicrow_tpl_ary',
'core.set_post_visibility_after' => 'set_post_visibility_after',
'core.set_topic_visibility_after' => 'set_topic_visibility_after',

'core.ucp_pm_view_message' => 'ucp_pm_view_message',
Expand All @@ -108,32 +109,20 @@ static public function getSubscribedEvents()

public function acp_manage_forums_display_form($event)
{
$template_data = array_merge($event['template_data'], array(
'S_ENABLE_ANSWER' => $event['forum_data']['enable_answer'],
));

$event['template_data'] = $template_data;
$event->update_subarray('template_data', 'S_ENABLE_ANSWER', $event['forum_data']['enable_answer']);
}

public function acp_manage_forums_initialise_data($event)
{
if ($event['action'] == 'add')
{
$forum_data = array_merge($event['forum_data'], array(
'enable_answer' => false,
));

$event['forum_data'] = $forum_data;
$event->update_subarray('forum_data', 'enable_answer', false);
}
}

public function acp_manage_forums_request_data($event)
{
$forum_data = array_merge($event['forum_data'], array(
'enable_answer' => $this->request->variable('enable_answer', 0),
));

$event['forum_data'] = $forum_data;
$event->update_subarray('forum_data', 'enable_answer', $this->request->variable('enable_answer', 0));
}

public function delete_posts_in_transaction_before($event)
Expand Down Expand Up @@ -163,18 +152,15 @@ public function delete_posts_in_transaction_before($event)
'answer_user_id' => 0,
);

if (sizeof($answer_post_ids))
if ($answer_post_ids)
{
$sql = 'UPDATE ' . TOPICS_TABLE . ' SET ' . $this->db->sql_build_array('UPDATE', $data) . ' WHERE ' . $this->db->sql_in_set('answer_post_id', $answer_post_ids);
$this->db->sql_query($sql);

foreach ($answer_user_ids as $answer_user_id)
{
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_answers = user_answers - 1
WHERE user_id = ' . (int) $answer_user_id;
$this->db->sql_query($sql);
}
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_answers = user_answers - 1
WHERE ' . $this->db->sql_in_set('user_id', $answer_user_ids, false, true);
$this->db->sql_query($sql);
}
}

Expand All @@ -193,13 +179,10 @@ public function delete_topics_before_query($event)
}
$this->db->sql_freeresult($result);

foreach ($answer_user_ids as $answer_user_id)
{
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_answers = user_answers - 1
WHERE user_id = ' . (int) $answer_user_id;
$this->db->sql_query($sql);
}
$sql = 'UPDATE ' . USERS_TABLE . '
SET user_answers = user_answers - 1
WHERE ' . $this->db->sql_in_set('user_id', $answer_user_ids, false, true);
$this->db->sql_query($sql);
}

public function display_forums_modify_forum_rows($event)
Expand Down Expand Up @@ -330,7 +313,7 @@ public function mcp_topic_review_modify_row($event)
$topic_info = $event['topic_info'];

// Does the topic have a best answer and is the post the first post in a topic
if (sizeof($this->answer) && ($topic_info['topic_first_post_id'] == $row['post_id']))
if ($this->answer && ($topic_info['topic_first_post_id'] == $row['post_id']))
{
$post_row = array_merge($post_row, array(
'U_ANSWER' => append_sid("{$this->root_path}viewtopic.{$this->php_ext}", 'p=' . (int) $topic_info['answer_post_id'] . '#p' . (int) $topic_info['answer_post_id']),
Expand Down Expand Up @@ -397,14 +380,10 @@ public function set_post_visibility_after($event)
FROM ' . TOPICS_TABLE . '
WHERE topic_id = ' . (int) $topic_id;
$result = $this->db->sql_query($sql);
while ($row = $this->db->sql_fetchrow($result))
{
$answer_post_id = $row['answer_post_id'];
$answer_user_id = $row['answer_user_id'];
}
$row = $this->db->sql_fetchrow($result);
$this->db->sql_freeresult($result);

if ((is_array($post_id) && in_array($answer_post_id, $post_id)) || $answer_post_id = $post_id)
if ((is_array($post_id) && in_array((int) $row['answer_post_id'], $post_id)) || (int) $row['answer_post_id'] = $post_id)
{
$data = array(
'answer_post_id' => 0,
Expand All @@ -416,7 +395,7 @@ public function set_post_visibility_after($event)

$sql = 'UPDATE ' . USERS_TABLE . '
SET user_answers = user_answers - 1
WHERE user_id = ' . (int) $answer_user_id;
WHERE user_id = ' . (int) $row['answer_user_id'];
$this->db->sql_query($sql);
}
}
Expand Down Expand Up @@ -520,7 +499,6 @@ public function viewtopic_get_post_data($event)

public function viewtopic_modify_post_row($event)
{
$poster_id = $event['poster_id'];
$row = $event['row'];
$user_poster_data = $event['user_poster_data'];
$post_row = $event['post_row'];
Expand All @@ -540,7 +518,7 @@ public function viewtopic_modify_post_row($event)
));

// Only add to post_row array if an answer_post_id is supplied and the post_id is the first post in a topic
if (sizeof($this->answer) && ($topic_data['topic_first_post_id'] == $row['post_id']))
if ($this->answer && ($topic_data['topic_first_post_id'] == $row['post_id']))
{
$post_row = array_merge($post_row, array(
'ANSWER_POST_TEXT' => $this->answer['POST_TEXT'],
Expand Down
5 changes: 5 additions & 0 deletions migrations/v10x/release_0_0_1.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

class release_0_0_1 extends \phpbb\db\migration\container_aware_migration
{
public function effectively_installed()
{
return $this->db_tools->sql_column_exists($this->table_prefix . 'forums', 'enable_answer');
}

static public function depends_on()
{
return array('\phpbb\db\migration\data\v320\v320');
Expand Down
2 changes: 0 additions & 2 deletions styles/all/template/jxtools.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @author Javier Lopez (javiexin)
*/

var jxtools = {};

(function($) { // Avoid conflicts with other libraries

'use strict';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{% if searchresults.S_ANSWERED %}
{% INCLUDEJS '@kinerity_bestanswer/jxtools.js' %}

{% if not $INCLUDED_JXTOOLSJS %}
{% INCLUDEJS '@kinerity_bestanswer/jxtools.js' %}
{% DEFINE $INCLUDED_JXTOOLSJS = true %}
{% endif %}
<div id="list_inner_hidden_container_1" jxdom-closest="dl.postprofile" jxdom-find="dd:nth-of-type(3)" jxdom-action="replace" >
<dd>{L_TOPIC}{L_COLON} <i class="icon fa-check fa-fw icon-green" aria-hidden="true"></i> <a href="{searchresults.U_VIEW_TOPIC}">{searchresults.TOPIC_TITLE}</a></dd>
<dd>{{ lang('TOPIC') }}{{ lang('COLON') }} <i class="icon fa-check fa-fw icon-green" aria-hidden="true"></i> <a href="{{ searchresults.U_VIEW_TOPIC }}">{{ searchresults.TOPIC_TITLE }}</a></dd>
</div>
{% endif %}

0 comments on commit 6545eb7

Please sign in to comment.