Passed
Branch develop (786e4f)
by Daniel
06:31
created

menu_module::main()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 0
dl 0
loc 23
ccs 18
cts 18
cp 1
crap 1
rs 9.0856
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\acp;
11
12
use blitze\sitemaker\services\menus\nestedset;
13
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14
15
/**
16
* @package acp
17
*/
18
class menu_module
19
{
20
	/** @var \phpbb\controller\helper */
21
	protected $controller_helper;
22
23
	/** @var \phpbb\event\dispatcher_interface */
24
	protected $phpbb_dispatcher;
25
26
	/** @var \phpbb\request\request_interface */
27
	protected $request;
28
29
	/** @var \phpbb\template\template */
30
	protected $template;
31
32
	/** @var \blitze\sitemaker\services\icon_picker */
33
	protected $icon;
34
35
	/** @var \phpbb\language\language */
36
	protected $language;
37
38
	/** @var \blitze\sitemaker\model\mapper_factory */
39
	protected $mapper_factory;
40
41
	/** @var \blitze\sitemaker\services\util */
42
	protected $util;
43
44
	/** @var string phpBB root path */
45
	protected $phpbb_root_path;
46
47
	/** @var string phpEx */
48
	protected $php_ext;
49
50
	/** @var string */
51
	public $tpl_name;
52
53
	/** @var string */
54
	public $page_title;
55
56
	/** @var string */
57
	public $u_action;
58
59
	/**
60
	 * menu_module constructor.
61
	 */
62 2
	public function __construct()
63
	{
64 2
		global $phpbb_container, $phpbb_dispatcher, $request, $template, $user, $phpbb_root_path, $phpEx;
65
66 2
		$this->phpbb_dispatcher = $phpbb_dispatcher;
67 2
		$this->request = $request;
68 2
		$this->template = $template;
69 2
		$this->phpbb_root_path = $phpbb_root_path;
70 2
		$this->php_ext = $phpEx;
71
72 2
		$this->controller_helper = $phpbb_container->get('controller.helper');
73 2
		$this->language = $phpbb_container->get('language');
74 2
		$this->mapper_factory = $phpbb_container->get('blitze.sitemaker.mapper.factory');
75 2
		$this->icon = $phpbb_container->get('blitze.sitemaker.icon_picker');
76 2
		$this->util = $phpbb_container->get('blitze.sitemaker.util');
77 2
	}
78
79
	/**
80
	 * @return void
81
	 */
82 2
	public function main()
83
	{
84 2
		$menu_id = $this->request->variable('menu_id', 0);
85
86 2
		nestedset::load_scripts($this->util);
87 2
		$this->util->add_assets(array(
88 2
			'js'	=> array('@blitze_sitemaker/assets/menu/admin.min.js'),
89 2
			'css'	=> array('@blitze_sitemaker/assets/menu/admin.min.css')
90 2
		));
91
92 2
		$this->list_menus($menu_id);
93 2
		$this->build_bulk_options();
94
95 2
		$this->template->assign_vars(array(
96 2
			'S_MENU'		=> true,
97 2
			'MENU_ID'		=> $menu_id,
98 2
			'ICON_PICKER'	=> $this->icon->picker(),
99 2
			'T_PATH'		=> $this->phpbb_root_path,
100 2
			'UA_AJAX_URL'   => $this->controller_helper->route('blitze_sitemaker_menus_admin', array(), true, '') . '/',
101 2
		));
102
103 2
		$this->tpl_name = 'acp_menu';
104 2
		$this->page_title = 'ACP_MENU';
105 2
	}
106
107
	/**
108
	 * @param int $menu_id
109
	 * @return void
110
	 */
111 2
	protected function list_menus(&$menu_id)
112
	{
113 2
		$menu_mapper = $this->mapper_factory->create('menus');
114
115
		// Get all menus
116 2
		$collection = $menu_mapper->find();
117
118 2
		if ($collection->valid())
119 2
		{
120 2
			$menu = (isset($collection[$menu_id])) ? $collection[$menu_id] : $collection->current();
121 2
			$menu_id = $menu->get_menu_id();
0 ignored issues
show
Bug introduced by
The method get_menu_id() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
			/** @scrutinizer ignore-call */ 
122
   $menu_id = $menu->get_menu_id();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
123 2
			foreach ($collection as $entity)
124
			{
125 2
				$id = $entity->get_menu_id();
126 2
				$this->template->assign_block_vars('menu', array(
127 2
					'ID'		=> $id,
128 2
					'NAME'		=> $entity->get_menu_name(),
129 2
					'S_ACTIVE'	=> ($id == $menu_id) ? true : false,
130 2
				));
131 2
			}
132 2
		}
133 2
	}
134
135
	/**
136
	 * @return void
137
	 */
138 2
	protected function build_bulk_options()
139
	{
140 2
		$bulk_options = array();
141 2
		$forumslist = make_forum_select(false, false, true, false, false, false, true);
142
143
		/**
144
		 * Event to add bulk menu options
145
		 *
146
		 * @event blitze.sitemaker.acp_add_bulk_menu_options
147
		 * @var	array	bulk_options	Array of bulk menu options
148
		 * @var	array	forumslist		Array of phpBB forums
149
		 * @since 3.1.0
150
		 */
151 2
		$vars = array('bulk_options', 'forumslist');
152 2
		extract($this->phpbb_dispatcher->trigger_event('blitze.sitemaker.acp_add_bulk_menu_options', compact($vars)));
153
154 2
		$bulk_options['FORUMS']	= $this->get_forums_string($forumslist);
0 ignored issues
show
Bug introduced by
It seems like $forumslist can also be of type string; however, parameter $forumslist of blitze\sitemaker\acp\men...le::get_forums_string() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

154
		$bulk_options['FORUMS']	= $this->get_forums_string(/** @scrutinizer ignore-type */ $forumslist);
Loading history...
155
156 2
		$this->template->assign_var('bulk_options', $bulk_options);
1 ignored issue
show
Bug introduced by
$bulk_options of type string[] is incompatible with the type string expected by parameter $varval of phpbb\template\template::assign_var(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
		$this->template->assign_var('bulk_options', /** @scrutinizer ignore-type */ $bulk_options);
Loading history...
157 2
	}
158
159
	/**
160
	 * @param array $forumslist
161
	 * @return string
162
	 */
163 2
	protected function get_forums_string(array $forumslist)
164
	{
165 2
		$forum_url = $this->controller_helper->route('blitze_sitemaker_forum', array(), true, '', UrlGeneratorInterface::RELATIVE_PATH);
166 2
		$text = $this->language->lang('FORUM') . '|' . $forum_url . "\n";
167 2
		foreach ($forumslist as $forum_id => $row)
168
		{
169 2
			$text .= "\t" . str_replace('&nbsp; &nbsp;', "\t", $row['padding']);
170 2
			$text .= $row['forum_name'] . '|';
171 2
			$text .= "viewforum.{$this->php_ext}?f=$forum_id\n";
172 2
		}
173
174 2
		return trim($text);
175
	}
176
}
177