name = 'mgallery';
$this->tab = 'Gallery';
$this->version = 0.1;
parent::__construct();
$this->displayName = $this->l('Галерея');
$this->description = $this->l('Управление картинками в галереи')." от devINweb.ru";
}
function install()
{
if (!parent::install()) return false;
Db::getInstance()->Execute('CREATE TABLE `'._DB_PREFIX_.'mgallery` (
`id` int(10) unsigned NOT NULL auto_increment,
`alt` varchar(255) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
');
return true;
}
public function uninstall()
{
Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'mgallery`');
parent::uninstall();
}
public function getContent()
{
if (isset($_POST['btnSubmit']))
$this->_postProcess();
if (isset($_POST['btnUpdate']))
$this->_updProcess();
$this-> _displayForm();
return $this->_html;
}
private function _displayForm()
{
global $cookie;
if (isset($_GET['editpic'])&&(int)($_GET['editpic'])) {
if (isset($_GET['deletepic'])&&(int)($_GET['deletepic'])) $this->delete((int)($_GET['deletepic']));
$pic_id = $_GET['editpic'];
$pic_to_edit = Db::getInstance()->ExecuteS("select * from "._DB_PREFIX_."mgallery where id = $pic_id");
if (count($pic_to_edit) != 0) {
foreach ($pic_to_edit as $pic_edit) {
$this->_html .=
'
';
}
}
else {
$this->_html .=
'
';
}
}
else {
if (isset($_GET['deletepic'])&&(int)($_GET['deletepic'])) $this->delete((int)($_GET['deletepic']));
$this->_html .=
'
';
}
$this->_html .= '';
}
private function delete($delid = 0) {
$delid = (int)$delid;
if (!$delid) return;
if (isset($delid)) {
if (Db::getInstance()->Execute("delete from "._DB_PREFIX_."mgallery where id ='".$delid."'")) {
@unlink($_SERVER['DOCUMENT_ROOT'].'/img/mgallery/'.$delid.'_mini.jpg');
@unlink($_SERVER['DOCUMENT_ROOT'].'/img/mgallery/'.$delid.'.jpg');
}
}
return;
}
private function update($updid = 0) {
$updid = (int)$updid;
if (!$updid) return;
if (isset($updid)) {
@unlink($_SERVER['DOCUMENT_ROOT'].'/img/mgallery/'.$updid.'_mini.jpg');
@unlink($_SERVER['DOCUMENT_ROOT'].'/img/mgallery/'.$updid.'.jpg');
}
}
private function _postProcess() {
if (isset($_POST['alt']) && isset($_FILES['tfile']) && $_FILES['tfile']['size'] > 0) {
if (FALSE !== $source = imagecreatefromjpeg($_FILES['tfile']['tmp_name'])) {
$w = imagesx($source);
$h = imagesy($source);
$x_max_size = 200;
$y_max_size = 150;
if ($w>$x_max_size) {
$pr = $x_max_size/$w;
$w = $w*$pr;
$h = $h*$pr;
}
if ($h>$y_max_size) {
$pr = $y_max_size/$h;
$w = $w*$pr;
$h = $h*$pr;
}
$resource = imagecreatetruecolor($w, $h);
imagecopyresampled($resource, $source, 0, 0, 0, 0, $w, $h, imagesx($source), imagesy($source));
if (Db::getInstance()->Execute("insert into "._DB_PREFIX_."mgallery set
alt = '".addslashes($_POST['alt'])."'")) {
$lid = Db::getInstance()->Insert_ID();
imagejpeg($source, $_SERVER['DOCUMENT_ROOT']."/img/mgallery/".$lid.".jpg");
imagejpeg($resource, $_SERVER['DOCUMENT_ROOT']."/img/mgallery/".$lid."_mini.jpg");
}
header("Location: ".$_SERVER["REQUEST_URI"]);
}
}
}
private function _updProcess() {
$lid = $_GET['editpic'];
Db::getInstance()->Execute("update "._DB_PREFIX_."mgallery set alt = '".addslashes($_POST['alt'])."' where id = '".$_GET['editpic']."'");
if (isset($_FILES['tfile']) && $_FILES['tfile']['size'] > 0) {
$this->update((int)($_GET['editpic']));
if (FALSE !== $source = imagecreatefromjpeg($_FILES['tfile']['tmp_name'])) {
$w = imagesx($source);
$h = imagesy($source);
$x_max_size = 200;
$y_max_size = 150;
if ($w>$x_max_size) {
$pr = $x_max_size/$w;
$w = $w*$pr;
$h = $h*$pr;
}
if ($h>$y_max_size) {
$pr = $y_max_size/$h;
$w = $w*$pr;
$h = $h*$pr;
}
$resource = imagecreatetruecolor($w, $h);
imagecopyresampled($resource, $source, 0, 0, 0, 0, $w, $h, imagesx($source), imagesy($source));
imagejpeg($source, $_SERVER['DOCUMENT_ROOT']."/img/mgallery/".$lid.".jpg");
imagejpeg($resource, $_SERVER['DOCUMENT_ROOT']."/img/mgallery/".$lid."_mini.jpg");
header("Location: ".$_SERVER["REQUEST_URI"]);
}
}
}
}
?>