-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk2insertitems.php
157 lines (122 loc) · 4.66 KB
/
k2insertitems.php
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* @version 2.2
* @package Example K2 Plugin (K2 plugin)
* @author JoomlaWorks - http://www.joomlaworks.net
* @copyright Copyright (c) 2006 - 2014 JoomlaWorks Ltd. All rights reserved.
* @license GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
*/
// no direct access
defined('_JEXEC') or die ;
/**
* Example K2 Plugin to render YouTube URLs entered in backend K2 forms to video players in the frontend.
*/
// Load the K2 Plugin API
JLoader::register('K2Plugin', JPATH_ADMINISTRATOR.'/components/com_k2/lib/k2plugin.php');
// Initiate class to hold plugin events
class plgK2Example extends K2Plugin
{
// Some params
var $pluginName = 'example';
var $pluginNameHumanReadable = 'K2 Insert Items';
function plgK2Example(&$subject, $params)
{
parent::__construct($subject, $params);
}
/**
* Below we list all available FRONTEND events, to trigger K2 plugins.
* Watch the different prefix "onK2" instead of just "on" as used in Joomla! already.
* Most functions are empty to showcase what is available to trigger and output. A few are used to actually output some code for example reasons.
*/
function onK2PrepareContent(&$item, &$params, $limitstart)
{
$mainframe = JFactory::getApplication();
//$item->text = 'It works! '.$item->text;
}
function onK2AfterDisplay(&$item, &$params, $limitstart)
{
$mainframe = JFactory::getApplication();
return '';
}
function onK2BeforeDisplay(&$item, &$params, $limitstart)
{
$mainframe = JFactory::getApplication();
return '';
}
function onK2AfterDisplayTitle(&$item, &$params, $limitstart)
{
$mainframe = JFactory::getApplication();
return '';
}
function onK2BeforeDisplayContent(&$item, &$params, $limitstart)
{
$mainframe = JFactory::getApplication();
//Find the {K2ID xxx} pattern inside $item->text
$matches = Array();
$count = preg_match_all("/(?<={K2ID:)[^}]*(?=})/", $item->text, $matches);
//Split found patterns to array
if ($count > 0) {
$db = JFactory::getDbo();
// Create a new query object.
$matches = $matches[0];
for ($i=0; $i<$count; $i++){
// Select item text
$query = $db->getQuery(true);
$query->select($db->quoteName(array('introtext','fulltext')));
$query->from($db->quoteName('#__k2_items'));
$query->where($db->quoteName('id') . ' = '. $db->quote($matches[$i]));
$db->setQuery($query);
$row = $db->loadAssoc();
//Replace {K2ID:xxx}
$item->text = str_replace('{K2ID:'.$matches[$i].'}',$row['introtext'].$row['fulltext'],$item->text);
//unset($query);
}
} else {
//Sorry dude no matches
return '';
}
return '';
}
// Event to display (in the frontend) the YouTube URL as entered in the item form
function onK2AfterDisplayContent(&$item, &$params, $limitstart)
{
$mainframe = JFactory::getApplication();
// Get the output of the K2 plugin fields (the data entered by your site maintainers)
$plugins = new K2Parameter($item->plugins, '', $this->pluginName);
$videoURL = $plugins->get('videoURL_item');
// Check if we have a value entered
if (empty($videoURL))
return;
// Output
preg_match('/youtube\.com\/watch\?v=([a-z0-9-_]+)/i', $videoURL, $matches);
$video_id = $matches[1];
$output = '
<p>'.JText::_('Video rendered using the "Example K2 Plugin".').'</p>
<object width="'.$this->params->get('width').'" height="'.$this->params->get('height').'">
<param name="movie" value="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1"></param>
<param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/'.$video_id.'&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'.$this->params->get('width').'" height="'.$this->params->get('height').'"></embed>
</object>
';
return $output;
}
// Event to display (in the frontend) the YouTube URL as entered in the category form
function onK2CategoryDisplay(&$category, &$params, $limitstart)
{
$mainframe = JFactory::getApplication();
// Get the output of the K2 plugin fields (the data entered by your site maintainers)
$plugins = new K2Parameter($category->plugins, '', $this->pluginName);
$output = $plugins->get('videoURL_cat');
return $output;
}
// Event to display (in the frontend) the YouTube URL as entered in the user form
function onK2UserDisplay(&$user, &$params, $limitstart)
{
$mainframe = JFactory::getApplication();
// Get the output of the K2 plugin fields (the data entered by your site maintainers)
$plugins = new K2Parameter($user->plugins, '', $this->pluginName);
$output = $plugins->get('videoURL_user');
return $output;
}
} // END CLASS