Completed
Push — develop ( 843010...da46c8 )
by Daniel
07:37
created

recent::build_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 15
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 12
nc 1
nop 1
crap 2
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\content\blocks;
11
12
class recent extends \blitze\sitemaker\services\blocks\driver\block
13
{
14
	/** @var \phpbb\config\db */
15
	protected $config;
16
17
	/** @var\phpbb\language\language */
18
	protected $language;
19
20
	/** @var \blitze\content\services\types */
21
	protected $content_types;
22
23
	/* @var \blitze\content\services\fields */
24
	protected $fields;
25
26
	/** @var \blitze\sitemaker\services\date_range */
27
	protected $date_range;
28
29
	/** @var \blitze\sitemaker\services\forum\data */
30
	protected $forum;
31
32
	/** @var  array */
33
	protected $settings;
34
35
	/** @var array */
36
	protected $sort_options = array();
37
38
	/** @var */
39
	const SORT_TOPIC_TIME = 0;
40
41
	/** @var */
42
	const SORT_TOPIC_VIEWS = 1;
43
44
	/** @var */
45
	const SORT_TOPIC_READ = 2;
46
47
	/**
48
	 * Constructor
49
	 *
50
	 * @param \phpbb\config\db							$config				Config object
51
	 * @param \phpbb\language\language					$language			Language Object
52
	 * @param \blitze\content\services\types			$content_types		Content types object
53
	 * @param \blitze\content\services\fields			$fields				Content fields object
54
	 * @param \blitze\sitemaker\services\date_range		$date_range			Date Range Object
55
	 * @param \blitze\sitemaker\services\forum\data		$forum				Forum Data object
56
	 */
57
	public function __construct(\phpbb\config\db $config, \phpbb\language\language $language, \blitze\content\services\types $content_types, \blitze\content\services\fields $fields, \blitze\sitemaker\services\date_range $date_range, \blitze\sitemaker\services\forum\data $forum)
58
	{
59
		$this->config = $config;
60
		$this->language = $language;
61
		$this->content_types = $content_types;
62
		$this->fields = $fields;
63
		$this->date_range = $date_range;
64
		$this->forum = $forum;
65
66
		$this->sort_options = array(
67
			self::SORT_TOPIC_TIME	=> 'TOPIC_TIME',
68
			self::SORT_TOPIC_VIEWS	=> 'TOPIC_VIEWS',
69
			self::SORT_TOPIC_READ	=> 'LAST_READ_TIME',
70
		);
71
	}
72
73
	/**
74
	 * Block config
75
	 * @param array $settings
76
	 * @return array
77
	 */
78
	public function get_config(array $settings)
79
	{
80
		$content_type_options = $field_options = array();
81
		$default_type = $this->get_content_type_options($content_type_options, $field_options);
82
83
		return array(
84
			'legend1'			=> 'DISPLAY',
85
			'content_type'		=> array('lang' => 'CONTENT_TYPE', 'validate' => 'string', 'type' => 'select:1:toggable', 'object' => $this, 'method' => 'select_content_type', 'options' => $content_type_options, 'default' => $default_type, 'explain' => false),
86
			'max_chars'			=> array('lang' => 'FIELD_MAX_CHARS', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 3, 'explain' => false, 'default' => 125),
87
			'fields'			=> array('lang' => 'SELECT_FIELDS', 'validate' => 'string', 'type' => 'checkbox', 'options' => $field_options, 'default' => array(), 'explain' => true),
88
			'block_tpl'			=> array('lang' => 'TEMPLATE', 'validate' => 'string', 'type' => 'textarea:5:50', 'maxlength' => 255, 'explain' => false, 'default' => ''),
89
90
			'legend2'			=> 'SETTINGS',
91
			'topic_type'		=> array('lang' => 'TOPIC_TYPE', 'validate' => 'string', 'type' => 'select', 'options' => $this->get_topic_type_options(), 'default' => POST_NORMAL, 'explain' => false),
92
			'max_topics'		=> array('lang' => 'MAX_TOPICS', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 5),
93
			'offset_start'		=> array('lang' => 'OFFSET_START', 'validate' => 'int:0:20', 'type' => 'number:0:20', 'maxlength' => 2, 'explain' => false, 'default' => 0),
94
			'topic_title_limit'	=> array('lang' => 'TOPIC_TITLE_LIMIT', 'validate' => 'int:0:255', 'type' => 'number:0:255', 'maxlength' => 3, 'explain' => false, 'default' => 25),
95
			'date_range'		=> array('lang' => 'LIMIT_POST_TIME', 'validate' => 'string', 'type' => 'select', 'options' => $this->get_range_options(), 'default' => '', 'explain' => false),
96
			'sort_key'			=> array('lang' => 'SORT_BY', 'validate' => 'string', 'type' => 'select', 'options' => $this->sort_options, 'default' => self::SORT_TOPIC_TIME, 'explain' => false),
97
			'enable_tracking'	=> array('lang' => 'ENABLE_TOPIC_TRACKING', 'validate' => 'bool', 'type' => 'radio:yes_no', 'explain' => false, 'default' => 1),
98
			'last_modified'		=> array('type' => 'hidden', 'default' => time()),
99
		);
100
	}
101
102
	/**
103
	 * @param array $bdata
104
	 * @param bool $edit_mode
105
	 * @return array
106
	 */
107
	public function display(array $bdata, $edit_mode = false)
108
	{
109
		$this->settings = $bdata['settings'];
110
		$type = $this->settings['content_type'];
111
112
		if (empty($this->settings['content_type']) || false === $entity = $this->content_types->get_type($type, false))
113
		{
114
			return array(
115
				'title'		=> '',
116
				'content'	=> ($edit_mode) ? $this->language->lang('NO_CONTENT_TYPE') : '',
117
			);
118
		}
119
120
		$forum_id = $entity->get_forum_id();
121
		$this->build_query($forum_id);
122
123
		return $this->show_topics($edit_mode, $bdata['bid'], $forum_id, $type, $entity);
124
	}
125
126
	/**
127
	 * @param int $forum_id
128
	 * @return void
129
	 */
130
	protected function build_query($forum_id)
131
	{
132
		$sort_keys = array(
133
			self::SORT_TOPIC_TIME	=> 't.topic_time',
134
			self::SORT_TOPIC_VIEWS	=> 't.topic_views',
135
			self::SORT_TOPIC_READ	=> 't.topic_last_view_time'
136
		);
137
138
		$range_info = $this->date_range->get($this->settings['date_range']);
139
140
		$this->forum->query($this->settings['enable_tracking'])
141
			->fetch_forum($forum_id)
142
			->fetch_topic_type(array($this->settings['topic_type']))
143
			->fetch_date_range($range_info['start'], $range_info['stop'])
144
			->set_sorting($sort_keys[$this->settings['sort_key']])
145
			->build(true, true, false);
146
	}
147
148
	/**
149
	 * @param bool $edit_mode
150
	 * @param int $block_id
151
	 * @param int $forum_id
152
	 * @param string $type
153
	 * @param \blitze\content\model\entity\type $entity
154
	 * @return array
155
	 */
156
	protected function show_topics($edit_mode, $block_id, $forum_id, $type, \blitze\content\model\entity\type $entity)
157
	{
158
		$topics_data = $this->forum->get_topic_data($this->settings['max_topics'], $this->settings['offset_start']);
159
		$posts_data = $this->forum->get_post_data('first');
160
161
		$title = $content = '';
162
		if (sizeof($posts_data) || $edit_mode !== false)
163
		{
164
			$users_cache = $this->forum->get_posters_info();
165
			$attachments = $this->forum->get_attachments($forum_id);
166
			$topic_tracking_info = $this->forum->get_topic_tracking_info($forum_id);
167
			$block_fields = $this->get_block_fields($entity->get_field_types());
168
169
			$this->fields->prepare_to_show($entity, array_keys($topics_data), $block_fields, $this->settings['block_tpl'], 'block');
170
171
			$update_count = array();
172
			foreach ($posts_data as $topic_id => $posts)
173
			{
174
				$post_data	= array_shift($posts);
175
				$topic_data	= $topics_data[$topic_id];
176
				$this->ptemplate->assign_block_vars('topicrow', $this->fields->show($type, $topic_data, $post_data, $users_cache, $attachments, $update_count, $topic_tracking_info));
177
			}
178
			unset($topics_data, $posts_data, $users_cache, $attachments, $topic_tracking_info);
179
180
			$title = $this->get_block_title($entity->get_content_langname());
181
			$content = $this->ptemplate->render_view('blitze/content', 'blocks/recent_content.html', 'recent_content_block');
182
		}
183
184
		return array(
185
			'title'		=> $title,
186
			'content'	=> $content,
187
		);
188
	}
189
190
	/**
191
	 * @param string $content_langname
192
	 * @return string
193
	 */
194
	protected function get_block_title($content_langname)
195
	{
196
		$topic_types = array(
197
			POST_GLOBAL		=> 'CONTENT_GLOBAL_ANNOUNCEMENTS',
198
			POST_ANNOUNCE	=> 'CONTENT_ANNOUNCEMENTS',
199
			POST_STICKY		=> 'CONTENT_STICKY_POSTS',
200
		);
201
202
		return (isset($topic_types[$this->settings['topic_type']])) ? $topic_types[$this->settings['topic_types']] :  $this->language->lang('CONTENT_' . $this->sort_options[$this->settings['sort_key']], $content_langname);
203
	}
204
205
	/**
206
	 * @param array $field_types
207
	 * @return array
208
	 */
209
	protected function get_block_fields(array $field_types)
210
	{
211
		$block_fields = (!empty($this->settings['fields'])) ? $this->settings['fields'] : array();
212
		return array_intersect_key($field_types, array_flip($block_fields));
213
	}
214
215
	/**
216
	 * @param array $fields
217
	 * @param array $fields_data
218
	 * @return array
219
	 */
220
	protected function get_block_fields_data(array $fields, array $fields_data)
221
	{
222
		$textarea_fields = array_keys($fields, 'textarea');
223
224
		foreach ($textarea_fields as $field)
225
		{
226
			$fields_data[$field]['field_props']['max_chars'] = $this->settings['max_chars'];
227
		}
228
229
		return $fields_data;
230
	}
231
232
	/**
233
	 * @param array $type_options
234
	 * @param array $field_options
235
	 * @return array
236
	 */
237
	protected function get_content_type_options(array &$type_options, array &$field_options)
238
	{
239
		$content_types = $this->content_types->get_all_types();
240
241
		$type_options = $field_options = array();
242
		foreach ($content_types as $type => $entity)
243
		{
244
			/** @var \blitze\content\model\entity\type $entity */
245
			$type_options[$type] = $entity->get_content_langname();
246
247
			$content_fields = $entity->get_content_fields();
248
			foreach ($content_fields as $field => $fdata)
249
			{
250
				$field_options[$type][$field] = $fdata['field_label'];
251
			}
252
		}
253
		reset($content_types);
254
255
		return key($content_types);
256
	}
257
258
	/**
259
	 * @param array $content_types
260
	 * @param string $type
261
	 * @return string
262
	 */
263
	public function select_content_type(array $content_types, $type)
264
	{
265
		$html = '';
266
		foreach ($content_types as $value => $title)
267
		{
268
			$selected = ($type == $value) ? ' selected="selected"' : '';
269
			$html .= '<option value="' . $value . '"' . $selected . ' data-toggle-setting="#fields-col-' . $value . '">' . $title . '</option>';
270
		}
271
272
		return $html;
273
	}
274
275
	/**
276
	 * @return array
277
	 */
278
	protected function get_topic_type_options()
279
	{
280
		return array(
281
			POST_NORMAL		=> 'POST_NORMAL',
282
			POST_STICKY		=> 'POST_STICKY',
283
			POST_ANNOUNCE	=> 'POST_ANNOUNCEMENT',
284
			POST_GLOBAL		=> 'POST_GLOBAL',
285
		);
286
	}
287
288
	/**
289
	 * @return array
290
	 */
291
	protected function get_range_options()
292
	{
293
		return array(
294
			''		=> 'ALL_TIME',
295
			'today'	=> 'TODAY',
296
			'week'	=> 'THIS_WEEK',
297
			'month'	=> 'THIS_MONTH',
298
			'year'	=> 'THIS_YEAR',
299
		);
300
	}
301
}
302