Files
newwalls.pl/modules/higallery/classes/adminForms.php
2024-12-17 13:43:22 +01:00

518 lines
20 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 HiGalleryAdminForms
{
public function __construct($module)
{
$this->module = $module;
$this->name = $module->name;
$this->context = Context::getContext();
}
public function renderSocialAccountsList()
{
$fields_list = array(
'id_higallerysocialnetwork' => array(
'title' => $this->l('ID'),
'width' => 60,
'type' => 'text',
'search' => false,
),
'title' => array(
'title' => $this->l('Title'),
'width' => 140,
'type' => 'text',
'search' => false,
),
'social_network' => array(
'title' => $this->l('Social Network'),
'width' => 140,
'type' => 'text',
'search' => false,
),
'status' => array(
'title' => $this->l('Status'),
'width' => 140,
'type' => 'text',
'search' => false,
),
);
$helper = new HelperList();
$helper->shopLinkType = '';
$helper->simple_header = false;
$helper->no_link = true;
$helper->actions = array('edit', 'delete');
$helper->identifier = 'id_higallerysocialnetwork';
$helper->show_toolbar = false;
$helper->title = $this->l('Social Accounts');
$helper->table = 'higallerysocialnetwork';
$helper->module = $this;
$helper->toolbar_btn['new'] = array(
'href' => '#',
'desc' => $this->l('Add New Account')
);
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name.'&'.$this->name.'=social_accounts';
$accounts = GallerySocialAccount::getAccounts();
$helper->listTotal = count($accounts);
$page = ($page = Tools::getValue('submitFilter'.$helper->table)) ? $page : 1;
$pagination = ($pagination = Tools::getValue($helper->table.'_pagination')) ? $pagination : 50;
$accounts = $this->module->HiPrestaClass->pagination($accounts, $page, $pagination);
return $helper->generateList($accounts, $fields_list);
}
public function renderSocialNetworksForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->module->l('Select Social Network'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'select',
'label' => $this->module->l('Social Network'),
'name' => 'social_network',
'options' => array(
'query' => array(
array(
'id' => 'renderInstagramForm',
'name' => $this->module->l('Instagram')
),
// array(
// 'id' => 'renderPinterestForm',
// 'name' => $this->module->l('Pinterest')
// )
),
'id' => 'id',
'name' => 'name'
)
)
),
'submit' => array(
'title' => $this->module->l('Next'),
'name' => 'submit_social_network_form',
),
),
);
$helper = new HelperForm();
$languages = Language::getLanguages(false);
foreach ($languages as $key => $language) {
$languages[$key]['is_default'] = (int)($language['id_lang'] == Configuration::get('PS_LANG_DEFAULT'));
}
$helper->languages = $languages;
$helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->allow_employee_form_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->show_toolbar = false;
$helper->submit_action = 'submitBlockSettings';
$helper->currentIndex = '';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->module = $this;
$helper->tpl_vars = array(
'fields_value' => array(
'social_network' => 'renderInstagramForm'
)
);
return $helper->generateForm(array($fields_form));
}
public function renderSocialNetworkEditForm($id_social_account)
{
$social_account = new GallerySocialAccount($id_social_account);
switch ($social_account->social_network) {
case 'instagram':
return $this->renderInstagramForm($id_social_account);
case 'pinterest':
return $this->renderPinterestForm($id_social_account);
default:
return $this->module->l('The Edit form not found');
}
}
public function renderInstagramForm($id_social_account = 0)
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->module->l('Instagram Account'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'hidden',
'name' => 'id_social_account',
),
array(
'type' => 'hidden',
'name' => 'social_network',
),
array(
'type' => 'switch',
'label' => $this->module->l('Active'),
'name' => 'social_active',
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'social_active_on',
'value' => 1,
'label' => $this->module->l('Enabled')
),
array(
'id' => 'social_active_off',
'value' => 0,
'label' => $this->module->l('Disabled')
)
),
),
array(
'type' => 'text',
'label' => $this->module->l('Title'),
'name' => 'social_title'
),
array(
'type' => 'text',
'label' => $this->module->l('Access Token'),
'name' => 'social_access_token'
)
),
'submit' => array(
'title' => $id_social_account ? $this->module->l('Update') : $this->module->l('Add'),
'name' => 'submit_instagram_save',
),
'buttons' => array(
array(
'title' => $this->module->l('Cancel'),
'name' => 'submit_cancel_social_account',
'type' => 'submit',
'icon' => 'process-icon-cancel',
'class' => 'btn btn-default pull-left',
)
)
),
);
$helper = new HelperForm();
$languages = Language::getLanguages(false);
foreach ($languages as $key => $language) {
$languages[$key]['is_default'] = (int)($language['id_lang'] == Configuration::get('PS_LANG_DEFAULT'));
}
$helper->languages = $languages;
$helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->allow_employee_form_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->show_toolbar = false;
$helper->submit_action = 'submitInstagram';
$helper->currentIndex = '';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->module = $this->module;
if ($id_social_account) {
$social_account = new GallerySocialAccount($id_social_account);
$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)) {
$error_message = $this->l('We\'re unable to connect Instagarm. Error message: ').$media->error->message.': '.$this->l('Please try to generate Access Token');
} else {
$error_message = '';
}
} else {
$error_message = '';
}
$helper->tpl_vars = array(
'psv' => $this->module->psv,
'error_message' => $error_message,
'fields_value' => $this->getInstagramAddFieldsValues($id_social_account)
);
return $helper->generateForm(array($fields_form));
}
public function getInstagramAddFieldsValues($id_social_account)
{
if ($id_social_account) {
$social_account = new GallerySocialAccount($id_social_account);
return array(
'id_social_account' => $id_social_account,
'social_network' => 'instagram',
'social_active' => $social_account->active,
'social_title' => $social_account->title,
'social_access_token' => $social_account->access_token
);
} else {
return array(
'id_social_account' => 0,
'social_network' => 'instagram',
'social_active' => true,
'social_title' => '',
'social_access_token' => ''
);
}
}
public function renderPinterestForm($id_social_account = 0)
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->module->l('Instagram Account'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'hidden',
'name' => 'id_social_account',
),
array(
'type' => 'hidden',
'name' => 'social_network',
),
array(
'type' => 'switch',
'label' => $this->module->l('Active'),
'name' => 'social_active',
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'social_active_on',
'value' => 1,
'label' => $this->module->l('Enabled')
),
array(
'id' => 'social_active_off',
'value' => 0,
'label' => $this->module->l('Disabled')
)
),
),
array(
'type' => 'text',
'label' => $this->module->l('Title'),
'name' => 'social_title'
),
array(
'type' => 'text',
'label' => $this->module->l('Instagram App ID'),
'name' => 'social_app_id',
),
array(
'type' => 'text',
'label' => $this->module->l('Instagram App Secret'),
'name' => 'social_app_secret'
),
array(
'type' => 'text',
'label' => $this->module->l('Access Token'),
'name' => 'social_access_token'
)
),
'submit' => array(
'title' => $id_social_account ? $this->module->l('Update') : $this->module->l('Add'),
'name' => 'submit_instagram_save',
),
'buttons' => array(
array(
'title' => $this->module->l('Cancel'),
'name' => 'submit_cancel_social_account',
'type' => 'submit',
'icon' => 'process-icon-cancel',
'class' => 'btn btn-default pull-left',
)
)
)
);
$helper = new HelperForm();
$languages = Language::getLanguages(false);
foreach ($languages as $key => $language) {
$languages[$key]['is_default'] = (int)($language['id_lang'] == Configuration::get('PS_LANG_DEFAULT'));
}
$helper->languages = $languages;
$helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->allow_employee_form_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->show_toolbar = false;
$helper->submit_action = 'submitPinterest';
$helper->currentIndex = '';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->module = $this->module;
$helper->tpl_vars = array(
'psv' => $this->module->psv,
'fields_value' => $this->getPinterestFieldsValues($id_social_account)
);
return $helper->generateForm(array($fields_form));
}
public function getPinterestFieldsValues($id_social_account)
{
if ($id_social_account) {
$social_account = new GallerySocialAccount($id_social_account);
return array(
'id_social_account' => $id_social_account,
'social_network' => 'pinterest',
'social_active' => $social_account->active,
'social_title' => $social_account->title,
'social_access_token' => $social_account->access_token
);
} else {
return array(
'id_social_account' => 0,
'social_network' => 'pinterest',
'social_active' => true,
'social_title' => '',
'social_access_token' => ''
);
}
}
public function renderSlideForm($id_slide = 0)
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->module->l('Slide'),
'icon' => 'icon-cogs'
),
'input' => array(
array(
'type' => 'hidden',
'name' => 'id_slide',
),
array(
'type' => 'switch',
'label' => $this->module->l('Active'),
'name' => 'status',
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'status_on',
'value' => 1,
'label' => $this->module->l('Enabled')
),
array(
'id' => 'status_off',
'value' => 0,
'label' => $this->module->l('Disabled')
)
),
),
array(
'type' => 'text',
'label' => $this->module->l('Title'),
'name' => 'title',
'placeholder' => $this->module->l('Title')
),
array(
'type' => 'file_lang',
'label' => $this->module->l('Image'),
'name' => 'image',
'class' => 'type_slide',
),
array(
'type' => 'text',
'label' => $this->module->l('Caption'),
'name' => 'caption',
'lang' => true
),
array(
'type' => 'text',
'label' => $this->module->l('Link'),
'name' => 'link',
'lang' => true
)
),
'submit' => array(
'title' => $id_slide ? $this->module->l('Update') : $this->module->l('Add'),
'name' => 'submit_slide',
),
'buttons' => array(
array(
'title' => $this->module->l('Cancel'),
'name' => 'submit_cancel_block',
'type' => 'submit',
'icon' => 'process-icon-cancel',
'class' => 'btn btn-default pull-left',
)
)
),
);
$helper = new HelperForm();
$languages = Language::getLanguages(false);
foreach ($languages as $key => $language) {
$languages[$key]['is_default'] = (int)($language['id_lang'] == Configuration::get('PS_LANG_DEFAULT'));
}
$helper->languages = $languages;
$helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->allow_employee_form_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$helper->show_toolbar = false;
$helper->submit_action = 'submitSlide';
$helper->currentIndex = '';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->module = $this->module;
$helper->tpl_vars = array(
'psv' => $this->module->psv,
'image_dir' => __PS_BASE_URI__.'modules/'.$this->module->name.'/views/img/upload/',
'fields_value' => $this->getSlideFieldsValues($id_slide)
);
return $helper->generateForm(array($fields_form));
}
public function getSlideFieldsValues($id_slide)
{
if ($id_slide) {
$slide = new BlogSlide($id_slide);
return array(
'id_slide' => $id_slide,
'status' => $slide->active,
'title' => $slide->title,
'image' => $slide->image,
'caption' => $slide->caption,
'link' => $slide->link,
);
} else {
$empty_array = array();
foreach (Language::getLanguages(false) as $lang) {
$empty_array[$lang['id_lang']] = '';
}
return array(
'id_slide' => 0,
'status' => true,
'title' => '',
'image' => $empty_array,
'caption' => $empty_array,
'link' => $empty_array
);
}
}
public function l($string)
{
return $this->module->l($string);
}
}