Completed
Push — release-3.0.0 ( 81bda4...4d9055 )
by Daniel
06:58
created

image::display_field()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 2
b 0
f 0
nc 3
nop 4
dl 0
loc 16
rs 9.9666
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\content\services\form\field;
12
13
class image extends base
14
{
15
	/**
16
	 * @inheritdoc
17
	 */
18
	public function get_field_value(array $data)
19
	{
20
		if ($data['field_value'])
21
		{
22
			preg_match('/src="(.*?)"/i', $data['field_value'], $match);
23
			return (!empty($match[1])) ? $match[1] : '';
24
		}
25
26
		return $data['field_props']['default'];
27
	}
28
29
	/**
30
	 * @inheritdoc
31
	 */
32
	public function display_field(array $data, array $topic_data, $display_mode, $view_mode)
33
	{
34
		if ($data['field_value'])
35
		{
36
			$image = '<img src="' . $data['field_value'] . '" class="postimage" alt="' . $data['field_label'] . '" />';
37
			$html = '<figure class="img-ui">' . $image . '</figure>';
38
39
			if ($display_mode !== 'block')
40
			{
41
				$view_props = array_fill_keys(array($view_mode . '_size', $view_mode . '_align'), '');
42
				$image_props = array_filter(array_intersect_key($data['field_props'], $view_props));
43
				$html = '<div class="' . join(' ', $image_props) . '">' . $html . '</div>';
44
			}
45
			return $html;
46
		}
47
		return '';
48
	}
49
50
	/**
51
	 * @inheritdoc
52
	 */
53
	public function get_submitted_value(array $data)
54
	{
55
		$value = $this->request->variable($data['field_name'], $data['field_value']);
56
57
		// we wrap this in image bbcode so that images will still work even if this extension is uninstalled
58
		// this complicates things for us as we have to remove the bbcode when showing the form field
59
		// and match the image source (url) from the image html when displaying the field
60
		return ($value) ? '[img]' . $value . '[/img]' : '';
61
	}
62
63
	/**
64
	 * @inheritdoc
65
	 */
66
	public function show_form_field(array &$data)
67
	{
68
		$data['field_value'] = $this->strip_image_bbcode($data['field_value']);
69
70
		$this->ptemplate->assign_vars($data);
71
72
		return $this->ptemplate->render_view('blitze/content', "fields/image.html", $this->get_name() . '_field');
73
	}
74
75
	/**
76
	 * @inheritdoc
77
	 */
78
	public function get_default_props()
79
	{
80
		return array(
81
			'default'		=> '',
82
			'detail_align'	=> '',
83
			'detail_size'	=> '',
84
			'summary_align'	=> '',
85
			'summary_size'	=> '',
86
		);
87
	}
88
89
	/**
90
	 * @inheritdoc
91
	 */
92
	public function get_name()
93
	{
94
		return 'image';
95
	}
96
97
	/**
98
	 * @param string $bbcode_string
99
	 * @return string
100
	 */
101
	private function strip_image_bbcode($bbcode_string)
102
	{
103
		return str_replace(array('[img]', '[/img]'), '', $bbcode_string);
104
	}
105
}
106