Completed
Push — 3.2.x ( eabb04...b8f3d7 )
by Erwan
02:27
created

add_cat_route_field::revert_schema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
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\migrations\v20x;
12
13
use E1379\SpeakingUrl;
14
15
class add_cat_route_field extends \phpbb\db\migration\migration
16
{
17
	static public function depends_on()
18
	{
19
		return array(
20
			'\ernadoo\phpbbdirectory\migrations\v10x\v1_0_0',
21
		);
22
	}
23
24
	/**
25
	* @inheritDoc
26
	*/
27
 	public function update_schema()
28
	{
29
		return array(
30
			'add_columns'	=> array(
31
				$this->table_prefix . 'directory_cats' => array(
32
					'cat_route'	=> array('VCHAR', '', 'after' => 'cat_name'),
33
				)
34
			),
35
		);
36
	}
37
38
	/**
39
	* @inheritDoc
40
	*/
41
	public function update_data()
42
	{
43
		return array(
44
			array('custom', array(array($this, 'rewrite_cat_name'))),
45
		);
46
	}
47
48
	/**
49
	* @inheritDoc
50
	*/
51
	public function revert_schema()
52
	{
53
		return array(
54
			'drop_columns'	=> array(
55
				$this->table_prefix . 'directory_cats' => array(
56
					'cat_route',
57
				),
58
			),
59
		);
60
	}
61
62
	public function rewrite_cat_name()
63
	{
64
		$slug = new \E1379\SpeakingUrl\SpeakingUrl();
65
66
		$sql = 'SELECT cat_id, cat_name FROM ' . $this->table_prefix . 'directory_cats';
67
		$result = $this->db->sql_query($sql);
68
69
		while ($row = $this->db->sql_fetchrow($result))
70
		{
71
			$sql = 'UPDATE ' . $this->table_prefix . 'directory_cats' . '
72
				SET cat_route = "' . (string) $slug->getSlug($row['cat_name'], array('lang' => $this->config['default_lang'], 'symbols' => true)). '"
73
				WHERE cat_id = ' . (int) $row['cat_id'];
74
			$this->db->sql_query($sql);
75
		}
76
	}
77
}
78