Completed
Push — master ( a821fd...7659ff )
by Khoa
07:38 queued 30s
created

bbook_word::count_words()   A

Complexity

Conditions 2
Paths 2

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 13
Code Lines 9

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
/**
3
* This file is part of the VinaBB.vn package.
4
*
5
* @copyright (c) VinaBB <vinabb.vn>
6
* @license GNU General Public License, version 2 (GPL-2.0)
7
*/
8
9
namespace vinabb\web\operators;
10
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
13
/**
14
* Operator for a set of words
15
*/
16
class bbook_word implements bbook_word_interface
17
{
18
	/** @var ContainerInterface $container */
19
	protected $container;
20
21
	/** @var \phpbb\db\driver\driver_interface $db */
22
	protected $db;
23
24
	/** @var string $table_name */
25
	protected $table_name;
26
27
	/**
28
	* Constructor
29
	*
30
	* @param ContainerInterface					$container	Container object
31
	* @param \phpbb\db\driver\driver_interface	$db			Database object
32
	* @param string								$table_name	Table name
33
	*/
34
	public function __construct(ContainerInterface $container, \phpbb\db\driver\driver_interface $db, $table_name)
35
	{
36
		$this->container = $container;
37
		$this->db = $db;
38
		$this->table_name = $table_name;
39
	}
40
41
	/**
42
	* Get number of words
43
	*
44
	* @param string $lang 2-letter language ISO code
45
	* @return int
46
	*/
47
	public function count_words($lang = '')
48
	{
49
		$sql_where = ($lang != '') ? "WHERE word_lang = '" . $this->db->sql_escape($lang) . "'" : '';
50
51
		$sql = 'SELECT COUNT(word_id) AS counter
52
			FROM ' . $this->table_name . "
53
			$sql_where";
54
		$result = $this->db->sql_query($sql);
55
		$counter = (int) $this->db->sql_fetchfield('counter');
56
		$this->db->sql_freeresult($result);
57
58
		return $counter;
59
	}
60
61
	/**
62
	* Get all words
63
	*
64
	* @param string $lang 2-letter language ISO code
65
	* @return array
66
	*/
67
	public function get_words($lang = '')
68
	{
69
		$entities = [];
70
		$sql_where = ($lang != '') ? "WHERE word_lang = '" . $this->db->sql_escape($lang) . "'" : '';
71
72
		$sql = 'SELECT *
73
			FROM ' . $this->table_name . "
74
			$sql_where
75
			ORDER BY word";
76
		$result = $this->db->sql_query($sql);
77
78
		while ($row = $this->db->sql_fetchrow($result))
79
		{
80
			$entities[] = $this->container->get('vinabb.web.entities.bbook_word')->import($row);
81
		}
82
		$this->db->sql_freeresult($result);
83
84
		return $entities;
85
	}
86
87
	/**
88
	* Add a word
89
	*
90
	* @param \vinabb\web\entities\bbook_word_interface $entity Word entity
91
	* @return \vinabb\web\entities\bbook_word_interface
92
	*/
93
	public function add_word(\vinabb\web\entities\bbook_word_interface $entity)
94
	{
95
		// Insert the entity to the database
96
		$entity->insert();
97
98
		// Get the newly inserted entity ID
99
		$id = $entity->get_id();
100
101
		// Reload the data to return a fresh entity
102
		return $entity->load($id);
103
	}
104
105
	/**
106
	* Delete a word
107
	*
108
	* @param int $id Word ID
109
	* @return bool True if row was deleted, false otherwise
110
	*/
111
	public function delete_word($id)
112
	{
113
		$sql = 'DELETE FROM ' . $this->table_name . '
114
			WHERE word_id = ' . (int) $id;
115
		$this->db->sql_query($sql);
116
117
		// Return true/false if the entity was deleted
118
		return (bool) $this->db->sql_affectedrows();
119
	}
120
}
121