name = 'azgallery'; $this->tab = 'front_office_features'; $this->version = '1.5.1.2'; $this->author = 'Tho '; $this->need_instance = 0; $this->secure_key = Tools::encrypt($this->name); parent::__construct(); $this->displayName = $this->l('Images gallery'); $this->description = $this->l('Images gallery module for your site.'); } /** * @see Module::install() */ public function install() { if (parent::install() == false || $this->registerHook('leftColumn') == false || $this->registerHook('header') == false || Configuration::updateValue('AZGALLERY_DISPLAY', 0) == false ) return false; if( !$this->createTable() ) return false; return true; } /** * @see Module::uninstall() */ public function uninstall() { /* Deletes Module */ if (parent::uninstall()) { /* Deletes tables */ $res = $this->dropTable(); /* Unsets configuration */ $res &= Configuration::deleteByName('AZGALLERY_DISPLAY'); return $res; } return false; } /** * Create table */ protected function createTable() { $sql1 = 'CREATE TABLE `'._DB_PREFIX_.'azgallery` ( `id_image` int(10) unsigned NOT NULL auto_increment, `id_album` int(2) NOT NULL, `caption` varchar(256) NULL, PRIMARY KEY (`id_image`)) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'; $sql2 = 'CREATE TABLE `'._DB_PREFIX_.'azgallery_album` ( `id_album` int(10) unsigned NOT NULL auto_increment, `title` varchar(256) NULL, `short_description` text NULL, `description` text NULL, PRIMARY KEY (`id_album`)) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'; if( !Db::getInstance()->Execute($sql1) ) return false; if( !Db::getInstance()->Execute($sql2) ) return false; return true; } /** * Drop table */ protected function dropTable() { if( !$this->_deleteAllImages() ) return false; if( !Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'azgallery`') ) return false; if( !Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'azgallery_album`') ) return false; return true; } private function _deleteAllImages() { $this->_deleteFiles( dirname(__FILE__).'/images/c/' ); $this->_deleteFiles( dirname(__FILE__).'/images/' ); return true; } private function _deleteFiles( $path ) { $files = glob( $path.'{,.}*', GLOB_BRACE ); foreach($files as $file) { if(is_file($file)) unlink($file); } } public function getContent() { $errors = array(); $this->_html .= '

'.$this->displayName.' V.'.$this->version.'.

'; if( $_GET['deleteAll'] == 1 ) { $this->_deleteAllImages(); } /* Add */ if ( Tools::isSubmit('submitNewAlbum') ) { if( $_POST['name'] == '' ) $errors[] = $this->l('Album name cannot blank'); if( empty($_FILES['album_image']['tmp_name']) ) $errors[] = $this->l('Album image cannot blank'); else { /* Insert to database */ if( Db::getInstance()->execute(' INSERT INTO '._DB_PREFIX_.'azgallery_album (`id_album`, `title`, `short_description`, `description`) VALUES (NULL, \''.$_POST['name'].'\', \''.$_POST['short_description'].'\', \''.$_POST['description'].'\')') ) { $id_albumn = Db::getInstance()->Insert_ID(); /* Upload image */ if( !count($errors) && !empty($_FILES['album_image']['tmp_name']) ) { $image = new Image($id_albumn); $method = 'auto'; if (!$new_path = dirname(__FILE__).'/images/c/') $errors[] = $this->l('An error occurred during new folder creation'); if (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['album_image']['tmp_name'], $tmpName)) $errors[] = $this->l('An error occurred during the image upload'); elseif (!ImageManager::resize($tmpName, $new_path.$id_albumn.'.'.$image->image_format)) $errors[] = $this->l('An error occurred while copying image.'); elseif ($method == 'auto') { $imagesTypes = ImageType::getImagesTypes('products'); foreach ($imagesTypes as $k => $image_type) { if (!ImageManager::resize($tmpName, $new_path.$id_albumn.'-'.stripslashes($image_type['name']).'.'.$image->image_format, $image_type['width'], $image_type['height'], $image->image_format)) $errors[] = $this->l('An error occurred while copying image:').' '.stripslashes($image_type['name']); } } @unlink($tmpName); $this->_html .= $this->displayConfirmation('Created new album successfully'); } } else $errors[] = $this->l('Cannot create new album'); } } /* Update */ if ( Tools::isSubmit('submitMultiImage') ) { if( empty($_FILES['userfile']['tmp_name']) ) $errors[] = $this->l('Image cannot blank'); else { /* Insert to database */ $countfile = count($_FILES['userfile']['name']); for ($i = 0; $i < $countfile; $i++) { if( Db::getInstance()->execute(' INSERT INTO '._DB_PREFIX_.'azgallery (`id_image`, `id_album`, `caption`) VALUES (NULL, '.$_POST['album'].', NULL)') ) { $id_image = Db::getInstance()->Insert_ID(); /* Upload image */ if( !count($errors) && !empty($_FILES['userfile']['name']) ) { $image = new Image($id_image); $method = 'auto'; if (!$new_path = dirname(__FILE__).'/images/') $errors[] = $this->l('An error occurred during new folder creation'); if (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $tmpName)) $errors[] = $this->l('An error occurred during the image upload'); elseif (!ImageManager::resize($tmpName, $new_path.$id_image.'.'.$image->image_format)) $errors[] = $this->l('An error occurred while copying image.'); elseif ($method == 'auto') { $imagesTypes = ImageType::getImagesTypes('products'); foreach ($imagesTypes as $k => $image_type) { if (!ImageManager::resize($tmpName, $new_path.$id_image.'-'.stripslashes($image_type['name']).'.'.$image->image_format, $image_type['width'], $image_type['height'], $image->image_format)) $errors[] = $this->l('An error occurred while copying image:').' '.stripslashes($image_type['name']); } } @unlink($tmpName); } } else $errors[] = $this->l('Cannot upload new image'); } $this->_html .= $this->displayConfirmation($countfile.' картинок было успешно добавлено.'); } } if ( Tools::isSubmit('submitNewImage') ) { if( empty($_FILES['_image']['tmp_name']) ) $errors[] = $this->l('Image cannot blank'); else { /* Insert to database */ if( Db::getInstance()->execute(' INSERT INTO '._DB_PREFIX_.'azgallery (`id_image`, `id_album`, `caption`) VALUES (NULL, '.$_POST['album'].', \''.$_POST['caption'].'\')') ) { $id_image = Db::getInstance()->Insert_ID(); /* Upload image */ if( !count($errors) && !empty($_FILES['_image']['name']) ) { $image = new Image($id_image); $method = 'auto'; if (!$new_path = dirname(__FILE__).'/images/') $errors[] = $this->l('An error occurred during new folder creation'); if (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['_image']['tmp_name'], $tmpName)) $errors[] = $this->l('An error occurred during the image upload'); elseif (!ImageManager::resize($tmpName, $new_path.$id_image.'.'.$image->image_format)) $errors[] = $this->l('An error occurred while copying image.'); elseif ($method == 'auto') { $imagesTypes = ImageType::getImagesTypes('products'); foreach ($imagesTypes as $k => $image_type) { if (!ImageManager::resize($tmpName, $new_path.$id_image.'-'.stripslashes($image_type['name']).'.'.$image->image_format, $image_type['width'], $image_type['height'], $image->image_format)) $errors[] = $this->l('An error occurred while copying image:').' '.stripslashes($image_type['name']); } } @unlink($tmpName); $this->_html .= $this->displayConfirmation('Uploaded image successfully'); } } else $errors[] = $this->l('Cannot upload new image'); } } if( isset($_GET['deleteAlbum']) && !Tools::isSubmit('submitNewAlbum') && !Tools::isSubmit('submitNewImage') ) { if( !$this->_deleteAlbum( $_GET['deleteAlbum'] ) ) $errors[] = $this->l('Cannot delete this album.'); else $this->_html .= $this->displayConfirmation('Deleted album successfully'); } if( isset($_GET['deleteImage']) && !Tools::isSubmit('submitNewAlbum') && !Tools::isSubmit('submitNewImage') ) { if( !$this->_deleteImagesByID( $_GET['deleteImage'] ) ) $errors[] = $this->l('Cannot delete this image.'); else $this->_html .= $this->displayConfirmation('Deleted image successfully'); } if ( Tools::isSubmit('submitEditAlbum') ) { $edit_album = $_GET['editAlbum']; if( Db::getInstance()->execute(' UPDATE '._DB_PREFIX_.'azgallery_album SET `title` = \''.$_POST['name'].'\', `short_description` = \''.$_POST['short_description'].'\', `description` = \''.$_POST['description'].'\' WHERE `id_album` = '.$edit_album.'') ) { /* Upload image */ if( !count($errors) && !empty($_FILES['album_image']['tmp_name']) ) { $imagesTypes = ImageType::getImagesTypes('products'); foreach ($imagesTypes as $k => $image_type) { @unlink( dirname(__FILE__).'/images/c/'.$edit_album.'-'.$image_type['name'].'.jpg' ); } @unlink( dirname(__FILE__).'/images/c/'.$edit_album.'.jpg' ); $image = new Image($edit_album); $method = 'auto'; if (!$new_path = dirname(__FILE__).'/images/c/') $errors[] = $this->l('An error occurred during new folder creation'); if (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['album_image']['tmp_name'], $tmpName)) $errors[] = $this->l('An error occurred during the image upload'); elseif (!ImageManager::resize($tmpName, $new_path.$edit_album.'.'.$image->image_format)) $errors[] = $this->l('An error occurred while copying image.'); elseif ($method == 'auto') { $imagesTypes = ImageType::getImagesTypes('products'); foreach ($imagesTypes as $k => $image_type) { if (!ImageManager::resize($tmpName, $new_path.$edit_album.'-'.stripslashes($image_type['name']).'.'.$image->image_format, $image_type['width'], $image_type['height'], $image->image_format)) $errors[] = $this->l('An error occurred while copying image:').' '.stripslashes($image_type['name']); } } @unlink($tmpName); $this->_html .= $this->displayConfirmation('Картинка альбома обновлена'); } } } if ( Tools::isSubmit('submitEditImage') ) { $edit_image = $_GET['editImage']; if( Db::getInstance()->execute(' UPDATE '._DB_PREFIX_.'azgallery SET `caption` = \''.$_POST['caption'].'\', `id_album` = \''.$_POST['album'].'\' WHERE `id_image` = '.$edit_image.'') ) { /* Upload image */ if( !count($errors) && !empty($_FILES['_image']['tmp_name']) ) { $imagesTypes = ImageType::getImagesTypes('products'); foreach ($imagesTypes as $k => $image_type) { @unlink( dirname(__FILE__).'/images/'.$edit_image.'-'.$image_type['name'].'.jpg' ); } @unlink( dirname(__FILE__).'/images/'.$edit_image.'.jpg' ); $image = new Image($edit_image); $method = 'auto'; if (!$new_path = dirname(__FILE__).'/images/') $errors[] = $this->l('An error occurred during new folder creation'); if (!($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) || !move_uploaded_file($_FILES['_image']['tmp_name'], $tmpName)) $errors[] = $this->l('An error occurred during the image upload'); elseif (!ImageManager::resize($tmpName, $new_path.$edit_image.'.'.$image->image_format)) $errors[] = $this->l('An error occurred while copying image.'); elseif ($method == 'auto') { $imagesTypes = ImageType::getImagesTypes('products'); foreach ($imagesTypes as $k => $image_type) { if (!ImageManager::resize($tmpName, $new_path.$edit_image.'-'.stripslashes($image_type['name']).'.'.$image->image_format, $image_type['width'], $image_type['height'], $image->image_format)) $errors[] = $this->l('An error occurred while copying image:').' '.stripslashes($image_type['name']); } } @unlink($tmpName); $this->_html .= $this->displayConfirmation('Картинка обновлена'); } } } /* Display errors */ if (count($errors)) $this->_html .= $this->displayError(implode('
', $errors)); /* Display form */ $this->_adminHeader(); $this->_displayForm(); return $this->_html; } private function _displayForm() { if( !isset($_GET['id_album']) && !isset($_GET['editAlbum']) ) $this->newAlbumForm(); if( isset($_GET['editAlbum']) ) $this->editAlbumForm(); if( isset($_GET['editImage']) ) $this->editImageForm(); if( !isset($_GET['editAlbum']) && !isset($_GET['editImage']) ) $this->addImageForm(); if( !isset($_GET['id_album']) && !isset($_GET['editAlbum']) ) $this->albumsList(); if( !isset($_GET['editAlbum']) && !isset($_GET['editImage']) ) $this->imagesList(); } private function _adminHeader() { $this->context->controller->addCSS(($this->_path).'css/admin.css', 'all'); } /* Edit form */ private function editAlbumForm() { $this_url = '?controller=adminmodules&configure='.$this->name.'&token='.Tools::getValue('token').'&tab_module='.$this->tab.'&module_name='.$this->name; $this->_html .= ' '.$this->l('Back to home').'

'; /* Add banner */ $this->_html .= '
'.$this->l('Редактирование альбома').''; /* Begin form */ $this->_html .= '
'; if( !isset( $_GET['edit'] ) || Tools::isSubmit('submitNewImage') ) { $album_edit = $this->getEditAlbum(); foreach($album_edit AS $album_to_edit ) { $title = $album_to_edit['title']; $short_description = $album_to_edit['short_description']; $description = $album_to_edit['description']; } /* name */ $this->_html .= '
*
'; /* Short description */ $this->_html .= '
'; /* Description */ $this->_html .= '
'; /* Image field */ $this->_html .= '
'; /* Add */ $this->_html .= '
'; } /* End form */ $this->_html .= '
'; /* End fieldset */ $this->_html .= '
'; $this->_html .= '

'; } private function editImageForm() { $this_url = '?controller=adminmodules&configure='.$this->name.'&token='.Tools::getValue('token').'&tab_module='.$this->tab.'&module_name='.$this->name; $this->_html .= ' '.$this->l('Back to home').'

'; /* Edit image */ $edit_image = $_GET['editImage']; $this->_html .= '
'.$this->l('Редактирование картинки').''; /* Begin form */ $this->_html .= '
'; if( !isset( $_GET['edit'] ) || Tools::isSubmit('submitNewAlbum') ) { $pic_edit = $this->getEditPic(); foreach($pic_edit AS $pic_to_edit ) { $pic_id_album = $pic_to_edit['id_album']; $pic_caption = $pic_to_edit['caption']; } /* Image field */ $this->_html .= '
'.$this->l('Картинка:').'

'; /* Album */ $this->_html .= ' '.$this->l('Альбом:').'

'; /* Caption */ $this->_html .= ' '.$this->l('Описание:').'

'; /* Add */ $this->_html .= '
'; } /* End form */ $this->_html .= '
'; /* End fieldset */ $this->_html .= '
'; $this->_html .= '

'; } /* Add form */ private function newAlbumForm() { /* Add banner */ $this->_html .= '
'.$this->l('Новый альбом').''; /* Begin form */ $this->_html .= '
'; if( !isset( $_GET['edit'] ) || Tools::isSubmit('submitNewImage') ) { /* name */ $this->_html .= '
*
'; /* Short description */ $this->_html .= '
'; /* Description */ $this->_html .= '
'; /* Image field */ $this->_html .= '
*
'; /* Add */ $this->_html .= '
'; } /* End form */ $this->_html .= '
'; /* End fieldset */ $this->_html .= '
'; $this->_html .= '

'; } /* Add form */ private function addImageForm() { /* Back to home */ if( isset($_GET['id_album']) ) { $this_url = '?controller=adminmodules&configure='.$this->name.'&token='.Tools::getValue('token').'&tab_module='.$this->tab.'&module_name='.$this->name; $this->_html .= ' '.$this->l('Back to home').'

'; } /* Add multi-image */ $this->_html .= '
'.$this->l('Мультизагрузка картинкок').''; /* Begin form */ $this->_html .= '
'; if( !isset( $_GET['edit'] ) || Tools::isSubmit('submitNewAlbum') ) { /* Image field */ $this->_html .= '
*
'; /* Album */ if( !isset($_GET['id_album']) ) { $this->_html .= '
'; } else { $this->_html .= ''; } /* Add */ $this->_html .= '
'; } /* End form */ $this->_html .= '
'; /* End fieldset */ $this->_html .= '
'; $this->_html .= '

'; /* Add image */ $this->_html .= '
'.$this->l('Новая картинка').''; /* Begin form */ $this->_html .= '
'; if( !isset( $_GET['edit'] ) || Tools::isSubmit('submitNewAlbum') ) { /* Image field */ $this->_html .= '
*
'; /* Album */ if( !isset($_GET['id_album']) ) { $this->_html .= '
'; } else { $this->_html .= ''; } /* Caption */ $this->_html .= '
'; /* Add */ $this->_html .= '
'; } /* End form */ $this->_html .= '
'; /* End fieldset */ $this->_html .= '
'; $this->_html .= '

'; } /* Albums list */ private function albumsList() { /* Add banner */ $this->_html .= '
'.$this->l('Альбомы').'
'; $this->_html .= '

'; } /* Images list */ private function imagesList() { /* Add banner */ $this->_html .= '
'.$this->l('Картинки').'
'; $this->_html .= '

'; } /* Get album image URL */ private function _getAlbumImage($id, $type) { return _PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name.'/images/c/'.$id.'-'.$type.'.jpg'; } /* Get all album */ public function getAlbums() { return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'azgallery_album`'); } private function getEditAlbum() { $edit_album = $_GET['editAlbum']; return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'azgallery_album` WHERE `id_album` = '.$edit_album); } private function getEditPic() { $edit_album = $_GET['id_album']; $edit_image = $_GET['editImage']; return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'azgallery` WHERE `id_image` = '.$edit_image.''); } /* Get all images in selected album */ public function getImages( $id_album = 0 ) { return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'azgallery` WHERE `id_album`='.$id_album.' ORDER BY `id_image` DESC'); } /* Get image URL */ private function _getImageLink($id, $type) { return _PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name.'/images/'.$id.'-'.$type.'.jpg'; } /* Delete album */ private function _deleteAlbum( $id_album ) { $images = $this->getImages( $id_album ); foreach( $images AS $image ) { $this->_deleteImagesByID( $image['id_image'] ); } $imagesTypes = ImageType::getImagesTypes('products'); foreach ($imagesTypes as $k => $image_type) { @unlink( dirname(__FILE__).'/images/c/'.$id_album.'-'.$image_type['name'].'.jpg' ); } @unlink( dirname(__FILE__).'/images/c/'.$id_album.'.jpg' ); return Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'azgallery_album` WHERE `id_album`='.$id_album); } /* Delete image */ private function _deleteImagesByID( $id_image ) { $imagesTypes = ImageType::getImagesTypes('products'); foreach ($imagesTypes as $k => $image_type) { @unlink( dirname(__FILE__).'/images/'.$id_image.'-'.$image_type['name'].'.jpg' ); } @unlink( dirname(__FILE__).'/images/'.$id_image.'.jpg' ); @Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'azgallery` WHERE `id_image`='.$id_image); return true; } /* Get last image in selected album */ private function _getLastImage() { return Db::getInstance()->executeS('SELECT * FROM `'._DB_PREFIX_.'azgallery` ORDER BY `id_image` DESC LIMIT 0,1'); } public function hookLeftColumn($params) { $this->smarty->assign(array( 'gallery_path' => _PS_BASE_URL_.__PS_BASE_URI__.'modules/'.$this->name, 'gallery_dir' => _PS_BASE_URL_.__PS_BASE_URI__.'index.php?fc=module&module='.$this->name.'&controller=gallery', 'images' => $this->_getLastImage() )); return $this->display(__FILE__, 'azgallery.tpl'); } public function hookRightColumn($params) { return $this->hookLeftColumn($params); } public function hookHeader() { $this->context->controller->addJS(($this->_path).'js/slimbox2.js'); $this->context->controller->addCSS(($this->_path).'css/slimbox2.css', 'all'); $this->context->controller->addCSS(($this->_path).'css/azgallery.css', 'all'); } }