summaryrefslogtreecommitdiff
path: root/plugins/data.img.php
blob: 32008b5ebbcebcab624d633c4e362e54ca5cb83f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php

namespace Bitweaver\Liberty;

use Bitweaver\KernelTools;

/**
 * @version  $Revision$
 * $Header$
 * @package  liberty
 * @subpackage plugins_data
 */

/**
 * definitions
 */
define( 'PLUGIN_GUID_DATAIMG', 'dataimg' );
global $gLibertySystem;
$pluginParams = [
	'tag'           => 'img',
	'auto_activate' => true,
	'requires_pair' => false,
	'load_function' => '\data_img',
	'title'         => 'Image',
	'help_page'     => 'DataPluginImg',
	'description'   => KernelTools::tra( "Allows you to insert an image into your page with little effort and a multitude of styling options." ),
	'help_function' => '\data_img_help',
	'syntax'        => "{img src=http://www.google.at/logos/olympics06_ski_jump.gif}",
	'plugin_type'   => DATA_PLUGIN,
	'booticon'       => '{booticon iname="icon-picture" iexplain="Web Image"}',
	'taginsert'     => '{img src= width= height= align= description= link=}',
];
$gLibertySystem->registerPlugin( PLUGIN_GUID_DATAIMG, $pluginParams );
$gLibertySystem->registerDataTag( $pluginParams['tag'], PLUGIN_GUID_DATAIMG );

function data_img_help() {
	return
		'<table class="data help">'
			.'<tr>'
				.'<th>'.KernelTools::tra( "Key" ).'</th>'
				.'<th>'.KernelTools::tra( "Type" ).'</th>'
				.'<th>'.KernelTools::tra( "Comments" ).'</th>'
			.'</tr>'
			.'<tr class="odd">'
				.'<td>src</td>'
				.'<td>'.KernelTools::tra( "string").'<br />'.KernelTools::tra("(required)").'</td>'
				.'<td>'.KernelTools::tra( "Specify where the path to the image.").'</td>'
			.'</tr>'
			.'<tr class="even">'
				.'<td>link</td>'
				.'<td>'.KernelTools::tra( "string").'<br />'.KernelTools::tra("(optional)").'</td>'
				.'<td>'.KernelTools::tra( "If you want your image to link to a web address, use link='link/to/page'." ).'</td>'
			.'</tr>'
			.'<tr class="odd">'
				.'<td>'.KernelTools::tra( "styling" ).'</td>'
				.'<td>'.KernelTools::tra( "string").'<br />'.KernelTools::tra("(optional)").'</td>'
				.'<td>'.KernelTools::tra( "Multiple styling options available: padding, margin, background, border, text-align, color, font, font-size, font-weight, font-family, align. Please view CSS guidelines on what values these settings take.").'</td>'
			.'</tr>'
		.'</table>'
		. KernelTools::tra( "Example: ")."{img src=http://www.google.at/logos/olympics06_ski_jump.gif float=right border=\"3px solid blue\"}";
}

function data_img( $pData, $pParams ) {
	$cssStyle = '';
	$cssClass = '';

	foreach( $pParams as $key => $value ) {
		if( !empty( $value ) ) {
			switch( $key ) {
				// rename a couple of parameters
				case 'width':
				case 'height':
					if( preg_match( "/^\d+(em|px|%|pt)$/", trim( $value ) ) ) {
						$cssStyle .= $key.':'.$value.';';
					} elseif( preg_match( "/^\d+$/", $value ) ) {
						$cssStyle .= $key.':'.$value.'px;';
					}
					// remove values from the hash that they don't get used in the div as well
					$pParams[$key] = null;
					break;
				case 'class':
					$cssClass .= $value.' ';
					break;
				case 'style':
					$cssStyle .= ';'.$value;
					break;
			}
		}
	}

	$wrapper = \Bitweaver\Liberty\liberty_plugins_wrapper_style( $pParams );

	// check if we have a source to load an image from
	if( !empty( $pParams['src'] ) ) {
		// set up image first
		$alt = !empty( $wrapper['description'] ) ? $wrapper['description'] : KernelTools::tra( 'Image' );
		$ret = '<img alt="'.$alt.'" title="'.$alt.'" src="'.$pParams['src'].'" style="'.$cssStyle.'" class="img-responsive '.$cssClass.' '.( !empty( $wrapper['class'] ) ? $wrapper['class'] : '').'"/>';

		// if this image is linking to something, wrap the image with the <a>
		if( !empty( $wrapper['link'] ) ) {
			$ret = '<a href="'.trim( $wrapper['link'] ).'">'.$ret.'</a>';
		}

		// finally, wrap the image
		if( !empty( $wrapper['style'] ) || !empty( $class ) || !empty( $wrapper['description'] ) ) {
			$ret = '<'.$wrapper['wrapper'].' class="img-plugin" style="'.$wrapper['style'].'">'.$ret.( !empty( $wrapper['description'] ) ? '<br />'.$wrapper['description'] : '' ).'</'.$wrapper['wrapper'].'>';
		}
	} else {
		$ret = '<span class="warning">'.KernelTools::tra( 'When using <strong>{img}</strong> the <strong>src</strong> parameter is required.' ).'</span>';
	}

	return $ret;
}