<?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'] . ' - ' . 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_
*/