comment::add()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
*
4
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\core;
12
13
use \ernadoo\phpbbdirectory\core\helper;
14
15
class comment extends helper
16
{
17
	/** @var \phpbb\db\driver\driver_interface $db */
18
	protected $db;
19
20
	/** @var \phpbb\language\language */
21
	protected $language;
22
23
	/**
24
	* Constructor
25
	*
26
	* @param \phpbb\db\driver\driver_interface	$db			Database object
27
	* @param \phpbb\language\language			$language	Language object
28
	*/
29
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\language\language $language)
30
	{
31
		$this->db		= $db;
32
		$this->language	= $language;
33
	}
34
35
	/**
36
	* Add a comment
37
	*
38
	* @param	array	$data	Link's data from db
39
	* @return	null
40
	*/
41
	public function add($data)
42
	{
43
		$this->db->sql_transaction('begin');
44
45
		$sql = 'INSERT INTO ' . $this->comments_table . ' ' . $this->db->sql_build_array('INSERT', $data);
46
		$this->db->sql_query($sql);
47
48
		$sql = 'UPDATE ' . $this->links_table . '
49
			SET link_comment = link_comment + 1
50
			WHERE link_id = ' . (int) $data['comment_link_id'];
51
		$this->db->sql_query($sql);
52
53
		$this->db->sql_transaction('commit');
54
	}
55
56
	/**
57
	* Edit a comment
58
	*
59
	* @param	array	$data		Data to edit
60
	* @param	int		$comment_id	The comment ID
61
	* @return	null
62
	*/
63
	public function edit($data, $comment_id)
64
	{
65
		$sql = 'UPDATE ' . $this->comments_table . '
66
			SET ' . $this->db->sql_build_array('UPDATE', $data) . '
67
			WHERE comment_id = ' . (int) $comment_id;
68
		$this->db->sql_query($sql);
69
	}
70
71
	/**
72
	* Delete a comment
73
	*
74
	* @param	string	$link_id	The link ID
75
	* @param	string	$comment_id	The comment ID
76
	* @return	null
77
	*/
78
	public function del($link_id, $comment_id)
79
	{
80
		global $request;
81
82
		$this->db->sql_transaction('begin');
83
84
		$sql = 'DELETE FROM ' . $this->comments_table . ' WHERE comment_id = ' . (int) $comment_id;
85
		$this->db->sql_query($sql);
86
87
		$sql = 'UPDATE ' . $this->links_table . '
88
			SET link_comment = link_comment - 1
89
			WHERE link_id = ' . (int) $link_id;
90
		$this->db->sql_query($sql);
91
92
		$this->db->sql_transaction('commit');
93
94 View Code Duplication
		if ($request->is_ajax())
95
		{
96
			$sql = 'SELECT COUNT(comment_id) AS nb_comments
97
				FROM ' . $this->comments_table . '
98
				WHERE comment_link_id = ' . (int) $link_id;
99
			$result = $this->db->sql_query($sql);
100
			$nb_comments = (int) $this->db->sql_fetchfield('nb_comments');
101
			$this->db->sql_freeresult($result);
102
103
			$json_response = new \phpbb\json_response;
104
			$json_response->send(array(
105
				'success' => true,
106
107
				'MESSAGE_TITLE'		=> $this->language->lang('INFORMATION'),
108
				'MESSAGE_TEXT'		=> $this->language->lang('DIR_COMMENT_DELETE_OK'),
109
				'COMMENT_ID'		=> $comment_id,
110
				'TOTAL_COMMENTS'	=> $this->language->lang('DIR_NB_COMMS', $nb_comments),
111
			));
112
		}
113
	}
114
}
115