php Plugin Name JS Slideshow Description JavaScript based slide show A

  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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
/*
Plugin Name: JS Slideshow
Description: JavaScript based slide show
Author: fantomas
Version: 0.2.1
*/
//$wpdb->show_errors();
//forbidden direct access to plugin
if (eregi(basename(__FILE__),$_SERVER['PHP_SELF'])) {
if (!headers_sent()) {
header('HTTP/1.1 403 Forbidden');
exit;
}}
//runs on activate
function jsslsh_install () {
global $wpdb;
$table_name = $wpdb->prefix . "slideshow";
//create database
if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
$sql="CREATE TABLE `$table_name` (
`id` SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`category` SMALLINT NOT NULL ,
`imgorder` SMALLINT NOT NULL ,
`image` VARCHAR( 55 ) NOT NULL
)";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
$uploaddirname='jsslsh';
//create options
update_option("jsslsh_uploadpath", $uploaddirname);
update_option("jsslsh_imgwidth", 400);
update_option("jsslsh_imgheight", 200);
update_option("jsslsh_bannerwidth", 150);
//create upload dir
$uploaddirname=ABSPATH . "wp-content/uploads/".$uploaddirname;
if (!file_exists($uploaddirname) && !mkdir ($uploaddirname, 0777))
echo "Не могу создать $uploaddirname"; //[fix] error output
} //end jsslsh_install()
function jsslsh_options()
{ global $wpdb;
if( isset($_POST['jsslsh_update']))
{
if(! is_numeric($_POST[ 'jsslsh_imgwidth' ]) ||
! is_numeric($_POST[ 'jsslsh_imgheight' ]) ||
! is_numeric($_POST[ 'jsslsh_bannerwidth' ])
)
{
echo '<div class="updated"><p><strong style="color:red">Ошибка ввода параметров</strong></p></div>';
}
else
{
$_POST[ 'jsslsh_uploadpath' ]=$wpdb->escape($_POST[ 'jsslsh_uploadpath' ]);
$uploaddirname=ABSPATH . "wp-content/uploads/".$_POST[ 'jsslsh_uploadpath' ];
if ($_POST[ 'jsslsh_uploadpath' ]!=get_option('jsslsh_uploadpath') && !file_exists($uploaddirname) && !mkdir ($uploaddirname, 0777))
echo "Не могу создать $uploaddirname"; //[fix] error output
update_option('jsslsh_uploadpath',$_POST[ 'jsslsh_uploadpath' ]);
update_option('jsslsh_imgwidth',$_POST[ 'jsslsh_imgwidth' ]);
update_option('jsslsh_imgheight',$_POST[ 'jsslsh_imgheight' ]);
update_option('jsslsh_bannerwidth',$_POST[ 'jsslsh_bannerwidth' ]);
//update_option(,$_POST[ ]);
echo '<div class="updated"><p><strong>Настройки сохранены</strong></p></div>';
}
}
//get options from database
$uploadpath=get_option('jsslsh_uploadpath');
$imgwidth=get_option('jsslsh_imgwidth');
$imgheight=get_option('jsslsh_imgheight');
$bannerwidth=get_option('jsslsh_bannerwidth');
//$=get_option('');
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br /></div><h2>JS Slideshow</h2><h3>Настройка параметров</h3>
<form name="jsslsh_options" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
<h4>
<label for="jsslsh_uploadpath">Папка загрузки <i><?php echo ABSPATH ?>wp-content/uploads/</i></label>
<input type="text" name="jsslsh_uploadpath" id="jsslsh_uploadpath" value="<?php echo $uploadpath ?>">
</h4>
<h4>
<label for="jsslsh_imgwidth">Ширина загруженных изображений </label>
<input type="text" name="jsslsh_imgwidth" id="jsslsh_imgwidth" value="<?php echo $imgwidth ?>">
</h4>
<h4>
<label for="jsslsh_imgheight">Высота загруженных изображений </label>
<input type="text" name="jsslsh_imgheight" id="jsslsh_imgheight" value="<?php echo $imgheight ?>">
</h4>
<h4>
<label for="jsslsh_bannerwidth">Ширина баннеров </label>
<input type="text" name="jsslsh_bannerwidth" id="jsslsh_bannerwidth" value="<?php echo $bannerwidth ?>">
</h4>
<p class="submit">
<input type="submit" name="jsslsh_update" class="button-primary" value="Сохранить изменения" />
</p>
</div>
<?php
}//end jsslsh_options()
//manage slideshow
function jsslsh_manager($category)
{
global $wpdb;
$table_name = $wpdb->prefix . "slideshow";
//get options from database
$uploaddir=get_option('jsslsh_uploadpath');
$uploadurl=get_option("siteurl")."/wp-content/uploads/".$uploaddir;
$imagesurl=get_option("siteurl")."/wp-content/plugins/js-slideshow/";
$uploadpath=ABSPATH . "wp-content/uploads/".$uploaddir;
$imgwidth=get_option('jsslsh_imgwidth');
$imgheight=get_option('jsslsh_imgheight');
$bannerwidth=get_option('jsslsh_bannerwidth');
//adding new image
if( isset($_POST['jsslsh_add'])/* && isset($_FILES['image'])*/)
{
require('class.upload.php');
function showError ($handle)// one error occured
{
echo '<fieldset><legend>Файл не загружен в указанную дирректорию</legend>Ошибка: ' . $handle->error . '</fieldset>';
}
// we create an instance of the class, giving as argument the PHP object
// corresponding to the file field from the form
// All the uploads are accessible from the PHP object $_FILES
$handle = new Upload($_FILES['image'],'ru_RU');
echo '<div class="updated">';
// then we check if the file has been uploaded properly
// in its *temporary* location in the server (often, it is /tmp)
if ($handle->uploaded) {
// yes, the file is on the server
$handle->image_resize = true;
if ($category>0)
{
$handle->image_x = $bannerwidth;
$handle->image_ratio_y = true;
}
else
{
$handle->image_x = $imgwidth;
$handle->image_y = $imgheight;
}
// now, we start the upload 'process'. That is, to copy the uploaded file
// from its temporary location to the wanted location
// It could be something like $handle->Process('/home/www/my_uploads/');
$handle->Process($uploadpath);
// we check if everything went OK
if ($handle->processed) {
// everything was fine !
//update db
$sql="SELECT MAX(`imgorder`) + 1 as imgord FROM `$table_name`";
$result = $wpdb->get_results($sql, ARRAY_A);
$order=$result[0]['imgord'];
if (empty($order)) $order=1;
$sql="INSERT INTO `$table_name` VALUES (NULL, $category,$order, '".$wpdb->escape($handle->file_dst_name)."');";
$wpdb->query($sql);
echo '<fieldset><legend>Файл успешно загружен</legend>';
$info = getimagesize($handle->file_dst_pathname);
echo ' <p>' . $info['mime'] . ' &nbsp;-&nbsp;' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>';
echo '</fieldset>';
}
else // one error occured
showError ($handle);
$handle-> Clean(); // we delete the temporary files
}
else {
// if we're here, the upload file failed for some reasons
// i.e. the server didn't receive the file
showError ($handle);
}
echo '</div>' ; //end updated div
}
//reorder
if( isset($_POST['first']) && isset($_POST['second']))
{
$sql="SELECT `imgorder` FROM `$table_name` WHERE `id` ={$_POST['first']} OR `id` ={$_POST['second']}";
$orders = $wpdb->get_results($sql, ARRAY_A);
$sql="UPDATE `$table_name` SET `imgorder` = '{$orders[1]['imgorder']}' WHERE `id` ={$_POST['first']}";
$wpdb->query($sql);
$sql="UPDATE `$table_name` SET `imgorder` = '{$orders[0]['imgorder']}' WHERE `id` ={$_POST['second']}";
$wpdb->query($sql);
}
//delete image
if( isset($_POST['imgdelete']))
{
$_POST['imgdelete']=$wpdb->escape($_POST['imgdelete']);
//get filename and delete it
$sql="SELECT `image` FROM `$table_name` WHERE `id`=".$_POST['imgdelete'];
$result = $wpdb->get_results($sql, ARRAY_A);
$file=$uploadpath.'/'.$result[0]['image'];
unlink($file);
//delete from db
$sql="DELETE FROM `$table_name` WHERE `id`=".$_POST['imgdelete'];
$wpdb->query($sql);
}
//output
$formaction=str_replace( '%7E', '~', $_SERVER['REQUEST_URI']);
?>
<div id="icon-options-general" class="icon32"><br /></div><h2>Управление слайдами</h2><h3>Список слайдов</h3>
<div class="wrap"><table style="width:100%;border:1px solid grey">
<?php
$sql="SELECT id,image FROM `$table_name` WHERE `category`=$category ORDER BY `imgorder`";
$images = $wpdb->get_results($sql, ARRAY_A);
function drawElement($img,$id,$order,$imgwidth,$imgheight,$uploadurl,$imagesurl)
{
echo "<tr><td><img width='$imgwidth' height='$imgheight' src='{$uploadurl}/$img'></td><td>$img</td><td>$order</td><td><a href='#' onclick='deleteItem($id)' title='Удалить'><img src='{$imagesurl}delete.png' border=0 /></a></td></tr>";
}
function getOrderHtml($up,$down,$imagesurl)
{ $o='';
if (count($up))
$o=$o."<a href='#' onclick='swapItems({$up[0]},{$up[1]})' title='Вверх'><img src='{$imagesurl}up.png' border=0 /></a>";
$o.='</td><td>' ;
if (count($down))
$o=$o." <a href='#' onclick='swapItems({$down[0]},{$down[1]})' title='Вниз'><img src='{$imagesurl}down.png' border=0 /></a>";
return $o;
}
$imgwidth=min($imgwidth,150);
$imgheight=min($imgheight,100);
$c=count($images);
if ($c>0)
{
//draw first
$o=array();
if($c>1)
$o=array($images[0]['id'],$images[1]['id']);
drawElement($images[0]['image'],$images[0]['id'],getOrderHtml(array(),$o,$imagesurl),$imgwidth,$imgheight,$uploadurl,$imagesurl);
//draw all
for ($i=1;$i<$c-1;$i++)
drawElement($images[$i]['image'],$images[$i]['id'],getOrderHtml(array($images[$i]['id'],$images[$i-1]['id']),array($images[$i]['id'],$images[$i+1]['id']),$imagesurl),$imgwidth,$imgheight,$uploadurl,$imagesurl);
//draw last
if($c>1)
drawElement($images[$c-1]['image'],$images[$c-1]['id'],getOrderHtml(array($images[$c-1]['id'],$images[$c-2]['id']),array(),$imagesurl),$imgwidth,$imgheight,$uploadurl,$imagesurl);
}
else echo 'нет слайдов';
?>
</table></div>
<script type='text/javascript'>
function deleteItem(itemID)
{
document.getElementById("imgdelete").value=itemID;
document.getElementById("jsslsh_delete").submit();
}
function swapItems(first,second)
{
document.getElementById("swapfirst").value=first;
document.getElementById("swapsecond").value=second;
document.getElementById("jsslsh_order").submit();
}
</script>
<h3>Добавить новый</h3>
<form method="post" action="<?php echo $formaction?>" name="jsslsh_add" enctype="multipart/form-data">
<p class="submit" name="image">
<input type="file" name="image">
<input type="submit" name="jsslsh_add" class="button-primary" value="Сохранить изменения" />
</p>
</form>
<form method="post" action="<?php echo $formaction?>" name="jsslsh_order" id="jsslsh_order">
<input type="hidden" name="first" value="0" id="swapfirst">
<input type="hidden" name="second" value="0" id="swapsecond">
</form>
<form method="post" action="<?php echo $formaction?>" name="jsslsh_delete" id="jsslsh_delete">
<input type="hidden" name="imgdelete" value="99" id="imgdelete">
</form>
<?php
} //end jsslsh_manager()
function jsslsh_top_banner()
{
jsslsh_manager(0);
}
function jsslsh_left_banner()
{
jsslsh_manager(1);
}
function jsslsh_right_banner()
{
jsslsh_manager(2);
}
//adds a new submenu for options
function jsslsh_optionshookmenu() {
// add_menu_page('Слайды','Слайды', 8, __FILE__, 'jsslsh_manager');
add_menu_page('Слайды','Слайды', 8, __FILE__, 'jsslsh_top_banner');
add_submenu_page(__FILE__, 'Баннер слева', 'Баннер слева', 8, 'jsslsh_sub-page1', 'jsslsh_left_banner');
add_submenu_page(__FILE__, 'Баннер справа', 'Баннер справа', 8, 'jsslsh_sub-page2', 'jsslsh_right_banner');
add_submenu_page(__FILE__, 'Настройка', 'Настройка', 8, 'jsslsh_sub-page3', 'jsslsh_options');
}
function jsslsh_drawBlock($blockID)
{global $wpdb;
//get options from database
$uploaddir=get_option('jsslsh_uploadpath');
$uploadurl=get_option("siteurl")."/wp-content/uploads/".$uploaddir;
//$imgwidth=get_option('jsslsh_imgwidth');
//$imgheight=get_option('jsslsh_imgheight');
$sql="SELECT image FROM `{$wpdb->prefix}slideshow` WHERE `category`=$blockID ORDER BY `imgorder`";
$result = $wpdb->get_results($sql, ARRAY_A);
if (count($result)) foreach($result as $images)
{
// echo "<img src='$uploadurl/{$images['image']}' width='$imgwidth' height='$imgheight'/>";
echo "<img src='$uploadurl/{$images['image']}'/>";
}
}
//register hooks
register_activation_hook(__FILE__,'jsslsh_install');
add_action('admin_menu', 'jsslsh_optionshookmenu');
/*
jsslsh_
*/