Files
newwalls.pl/modules/higallery/controllers/admin/AdminGalleryController.php
2024-12-17 13:43:22 +01:00

421 lines
21 KiB
PHP

<?php
/**
* 2012 - 2020 HiPresta
*
* MODULE Gallery
*
* @author HiPresta <support@hipresta.com>
* @copyright HiPresta 2020
* @license Addons PrestaShop license limitation
* @link https://hipresta.com
*
* NOTICE OF LICENSE
*
* Don't use this module on several shops. The license provided by PrestaShop Addons
* for all its modules is valid only once for a single shop.
*/
class AdminGalleryController extends ModuleAdminController
{
public $errors = array();
public $error = false;
public function __construct()
{
$this->secure_key = Tools::getValue('secure_key');
parent::__construct();
}
public function init()
{
parent::init();
if ($this->ajax) {
if ($this->secure_key == $this->module->secure_key) {
switch (Tools::getValue('action')) {
/*Gallery item*/
case 'update_status':
if (Tools::getValue('status') == '0') {
$active = 1;
} else {
$active = 0;
}
$gallery = new GalleryItem(Tools::getValue('id'));
$gallery->active = $active;
$gallery->update();
die(Tools::jsonEncode(array(
'content' => $this->module->renderGalleriesList(),
)));
case 'update_helper_list':
die(Tools::jsonEncode(array(
'content' => $this->module->renderGalleriesList(),
)));
case 'show_gallery_add_form':
die(Tools::jsonEncode(array(
'error' => false,
'content' => $this->module->renderAddGalleryForm('add', null),
)));
case 'show_gallery_update_form':
die(Tools::jsonEncode(array(
'error' => false,
'content' => $this->module->renderAddGalleryForm('update', Tools::getValue('id')),
)));
case 'save_gallery':
$this->valideatFields();
if (!empty($this->errors)) {
die(Tools::jsonEncode(array(
'error' => true,
'errors' => $this->errors,
)));
} else {
$this->module->saveGallery(Tools::getValue('action_type'), Tools::getValue('row_id'));
die(Tools::jsonEncode(array(
'error' => false,
)));
}
break;
case 'delete_gallery_list_item':
$gallery = new GalleryItem(Tools::getValue('id'));
$gallery->delete();
die();
/*Gallery item image*/
case 'sortable_change_position':
$gallery_item = Tools::getValue('gallery_item');
if (!empty($gallery_item)) {
$i = 1;
foreach ($gallery_item as $item) {
$this->updatePosition($item['id_item'], Tools::getValue('id_parent'), $i);
$i++;
}
}
die();
case 'gallery_manage_image':
die(Tools::jsonEncode(array(
'content' => $this->module->renderGalleryImageList(Tools::getValue('id_gallery')).$this->module->renderAddGalleryImageForm(Tools::getValue('id_gallery'), 'add', null),
)));
case 'add_gallery_image':
if (Tools::getValue('action_type') == 'add') {
if ($_FILES['image_file']['name'] == '') {
$this->errors['error'] = $this->l('Please select image');
}
}
if (!empty($this->errors)) {
die(Tools::jsonEncode(array(
'error' => true,
'errors' => $this->errors,
)));
} else {
if($this->module->saveGalleryImage(Tools::getValue('action_type'), Tools::getValue('row_id'), Tools::getValue('id_gallery'))) {
die(Tools::jsonEncode(array(
'content' => $this->module->renderGalleryImageList(Tools::getValue('id_gallery')).$this->module->renderAddGalleryImageForm(Tools::getValue('id_gallery'), 'add', null),
)));
} else {
die(Tools::jsonEncode(array(
'error' => true,
'errors' => array('error' => $this->l('Something went wrong, please try again'))
)));
}
}
break;
case 'upload_multiple_images':
if (!isset($_FILES['files']) || $_FILES['files']['name'] == '') {
die(Tools::jsonEncode(array(
'error' => $this->l('Please select image')
)));
} else {
if($this->module->saveMultipleImages(Tools::getValue('id_gallery'))) {
die(Tools::jsonEncode(array(
'content' => $this->module->renderGalleryImageList(Tools::getValue('id_gallery')).$this->module->renderAddGalleryImageForm(Tools::getValue('id_gallery'), 'add', null),
)));
} else {
die(Tools::jsonEncode(array(
'error' => $this->l('Some images were not uploaded correctly, please check the file size and format')
)));
}
}
break;
case 'show_update_gallery_image':
die(Tools::jsonEncode(array(
'content' => $this->module->renderAddGalleryImageForm(Tools::getValue('id_gallery'), 'update', Tools::getValue('id_image')),
)));
case 'delete_gallery_image':
$gallery_image = new GalleryImage(Tools::getValue('id_image'));
if (!empty($gallery_image)) {
if ($gallery_image->image) {
if (file_exists(_PS_MODULE_DIR_.$this->module->name.'/views/img/upload/original/'.$gallery_image->image)) {
unlink(_PS_MODULE_DIR_.$this->module->name.'/views/img/upload/original/'.$gallery_image->image);
}
if (file_exists(_PS_MODULE_DIR_.$this->module->name.'/views/img/upload/small/'.$gallery_image->image)) {
unlink(_PS_MODULE_DIR_.$this->module->name.'/views/img/upload/small/'.$gallery_image->image);
}
if (file_exists(_PS_MODULE_DIR_.$this->module->name.'/views/img/upload/thumbnail/'.$gallery_image->image)) {
unlink(_PS_MODULE_DIR_.$this->module->name.'/views/img/upload/thumbnail/'.$gallery_image->image);
}
}
}
if($gallery_image->delete()){
die(Tools::jsonEncode(array(
'error' => false,
'content' => $this->module->renderGalleryImageList(Tools::getValue('id_gallery'))
)));
} else {
die(Tools::jsonEncode(array(
'error' => $this->l('Something went wrong, please refresh the page and try again')
)));
}
case 'displaySocialAccountForm':
die(Tools::jsonEncode(array(
'error' => false,
'content' => $this->module->adminForms->renderSocialNetworksForm()
)));
case 'displaySelectedSocialForm':
$function = Tools::getValue('social_network');
die(Tools::jsonEncode(array(
'content' => $this->module->adminForms->$function()
)));
case 'saveSocialAccount':
$this->valideatSocialAccount();
if (!$this->error) {
$this->module->saveSocialAccount();
die(Tools::jsonEncode(array(
'error' => false,
'message' => $this->l('Social account successfully added'),
'content' => $this->module->adminForms->renderSocialAccountsList()
)));
} else {
die(Tools::jsonEncode(array(
'error' => $this->error
)));
}
case 'displaySocialAccountEditForm':
die(Tools::jsonEncode(array(
'error' => false,
'content' => $this->module->adminForms->renderSocialNetworkEditForm((int)Tools::getValue('id_social_account'))
)));
case 'deleteSocialAccount':
$social_account = new GallerySocialAccount((int)Tools::getValue('id_social_account'));
if (!$social_account->delete()) {
die(Tools::jsonEncode(array(
'error' => $this->module->l('Error: couldn\'t delete the social account. Please refresh the page and try again.')
)));
} else {
die(Tools::jsonEncode(array(
'error' => false,
'content' => $this->module->adminForms->renderSocialAccountsList(),
'message' => $this->l('Social Account successfully deleted')
)));
}
case 'selectSocialAccount':
die(Tools::jsonEncode(array(
'error' => false,
'content' => $this->module->renderSelectSocialAccount(),
'message' => $this->l('Social Accounts successfully loaded.')
)));
case 'loadSocialImages':
$social_account = new GallerySocialAccount((int)Tools::getValue('id_social_account'));
if (!Validate::isLoadedObject($social_account)) {
die(Tools::jsonEncode(array(
'error' => $this->module->l('The social account is not found.')
)));
}
if ($social_account->social_network == 'instagram') {
$media = Tools::file_get_contents('https://graph.instagram.com/me/media?fields=id,caption,media_url,permalink&access_token='.$social_account->access_token);
$media = Tools::jsonDecode($media);
if(isset($media->error) && isset($media->error->message)) {
die(Tools::jsonEncode(array(
'error' => $this->l('We\'re unable to connect Instagarm. Error message: ').$media->error->message.': '.$this->l('Please try to generate Access Token')
)));
}
$ig_images = array();
if (isset($media->data) && $media->data) {
foreach ($media->data as $data) {
if (strpos($data->media_url, 'video') !== false) {
continue;
}
$ig_images[] = $data->media_url;
}
}
$ig_next_page = false;
if (isset($media->paging) && $media->paging && isset($media->paging->next) && $media->paging->next) {
$ig_next_page = $media->paging->next;
}
die(Tools::jsonEncode(array(
'error' => false,
'content' => $this->module->loadSocialAccountImages($ig_images, $ig_next_page),
'message' => $this->l('Social Account images successfully loaded.')
)));
} else {
die(Tools::jsonEncode(array(
'error' => $this->module->l('Sorry, currently you can import images only from Instagram account.')
)));
}
case 'loadMoreImagesIG':
$media = Tools::file_get_contents(Tools::getValue('next'));
$media = Tools::jsonDecode($media);
if(isset($media->error) && isset($media->error->message)) {
die(Tools::jsonEncode(array(
'error' => $this->l('We\'re unable to connect Instagarm. Error message: ').$media->error->message.': '.$this->l('Please try to generate Access Token')
)));
}
$ig_images = array();
if (isset($media->data) && $media->data) {
foreach ($media->data as $data) {
if (strpos($data->media_url, 'video') !== false) {
continue;
}
$ig_images[] = $data->media_url;
}
}
$ig_next_page = false;
if (isset($media->paging) && $media->paging && isset($media->paging->next) && $media->paging->next) {
$ig_next_page = $media->paging->next;
}
die(Tools::jsonEncode(array(
'error' => false,
'content' => $this->module->loadMoreSocialAccountImages($ig_images),
'ig_next_page' => $ig_next_page,
'message' => $this->l('Images successfully loaded.')
)));
case 'importSocialImages':
$images = Tools::getValue('images');
if (!is_array($images) || !$images) {
die(Tools::jsonEncode(array(
'error' => $this->module->l('Please select images to import')
)));
}
$languages = Language::getLanguages(false);
$id_gallery = (int)Tools::getValue('id_gallery');
foreach ($images as $image) {
$gallery_image = new GalleryImage();
$gallery_image->id_gallery = $id_gallery;
$gallery_image->image = $this->module->copyImageFromURL($image);
if(!$gallery_image->image) {
continue;
}
$gallery_image->position = GalleryImage::getPositionMaxValue();
$gallery_image->add();
}
die(Tools::jsonEncode(array(
'error' => false,
'content' => $this->module->renderGalleryImageList(Tools::getValue('id_gallery')),
'message' => $this->module->l('Images successfully imported.')
)));
case 'sortGalleries':
$sorted_galleries = Tools::getValue('sortedGalleries');
if (is_array($sorted_galleries) && $sorted_galleries) {
$i = 1;
foreach ($sorted_galleries as $id_gallery) {
Db::getInstance()->Execute('
UPDATE '._DB_PREFIX_.'higallery
SET sort='.(int)$i.'
WHERE id_gallery ='.(int)$id_gallery.'
');
$i++;
}
die(Tools::jsonEncode(array(
'error' => '',
'message' => $this->module->l('Successfully updated')
)));
}
die(Tools::jsonEncode(array(
'error' => $this->module->l('Something went wrong, please refresh the page and try again.')
)));
}
} else {
die();
}
} else {
Tools::redirectAdmin($this->module->HiPrestaClass->getModuleUrl('&'.$this->module->name.'=hisocialblock'));
}
}
private function valideatSocialAccount()
{
if (!trim(Tools::getValue('social_title'))) {
$this->error = $this->l('Title is required');
return;
}
$access_token = trim(Tools::getValue('social_access_token'));
$media = Tools::file_get_contents('https://graph.instagram.com/me/media?fields=id,caption,media_url,permalink&access_token='.$access_token);
$media = Tools::jsonDecode($media);
if(isset($media->error) && isset($media->error->message)) {
$this->error = $this->l('We\'re unable to connect Instagarm. Error message: ').$media->error->message.': '.$this->l('Please try to generate Access Token');
return;
}
}
public function valideatFields()
{
$languages = Language::getLanguages(false);
if (Tools::getValue('position') == '') {
$this->errors['error'] = $this->l('Please select position.');
return;
}
if (trim(Tools::getValue('name_'.Configuration::get('PS_LANG_DEFAULT'))) == '') {
$this->errors['error'] = $this->l('Gallery name is required');
return;
}
if (Tools::getValue('position') != 'gallery_page' && Tools::getValue('position') != 'single_gallery_page') {
return;
}
if (trim(Tools::getValue('link_rewrite_'.Configuration::get('PS_LANG_DEFAULT'))) == '') {
$this->errors['error'] = $this->l('friendly URL is required');
return;
}
foreach ($languages as $lang) {
$link_rewrite = trim(Tools::getValue('link_rewrite_'.$lang['id_lang']));
if ($link_rewrite != '' && !Validate::isLinkRewrite($link_rewrite)) {
$this->errors['error'] = $this->l('Invalid value for friendly URL') . ' (' . $lang['iso_code'] . ')';
return;
}
$meta_title = Tools::getValue('meta_title_'.$lang['id_lang']);
if (!Validate::isGenericName($meta_title) || Tools::strlen($meta_title) > 255) {
$this->errors['error'] = $this->l('Meta title is not valid, max 255 characters') . ' (' . $lang['iso_code'] . ')';
return;
}
$meta_description = Tools::getValue('meta_description_'.$lang['id_lang']);
if (!Validate::isGenericName($meta_description) || Tools::strlen($meta_description) > 255) {
$this->errors['error'] = $this->l('Meta description is not valid, max 255 characters.') . ' (' . $lang['iso_code'] . ')';
return;
}
$meta_keywords = Tools::getValue('meta_keywords_'.$lang['id_lang']);
if (!Validate::isGenericName($meta_keywords) || Tools::strlen($meta_keywords) > 255) {
$this->errors['error'] = $this->l('Meta keywords is not valid, max 255 characters') . ' (' . $lang['iso_code'] . ')';
return;
}
}
}
protected function updatePosition($id_image, $id_gallery, $position)
{
Db::getInstance()->Execute('
UPDATE '._DB_PREFIX_.'higallery_image
SET position='.(int)$position.'
WHERE id_gallery ='.(int)($id_gallery).' AND id_image ='.(int)($id_image).'
');
}
}