2017-07-28 13:06:15 +02:00

244 lines
6.1 KiB
PHP

<?php
class AdminCmsController extends AdminCmsControllerCore
{
public function renderForm()
{
if (!$this->loadObject(true)) {
return;
}
if (Validate::isLoadedObject($this->object)) {
$this->display = 'edit';
} else {
$this->display = 'add';
}
$this->initToolbar();
$this->initPageHeaderToolbar();
$categories = CMSCategory::getCategories($this->context->language->id, false);
$html_categories = CMSCategory::recurseCMSCategory($categories, $categories[0][1], 1, $this->getFieldValue($this->object, 'id_cms_category'), 1);
$this->fields_form = array(
'tinymce' => true,
'legend' => array(
'title' => $this->l('CMS Page'),
'icon' => 'icon-folder-close'
),
'input' => array(
// custom template
array(
'type' => 'select_category',
'label' => $this->l('CMS Category'),
'name' => 'id_cms_category',
'options' => array(
'html' => $html_categories,
),
),
array(
'type' => 'text',
'label' => $this->l('Meta title'),
'name' => 'meta_title',
'id' => 'name', // for copyMeta2friendlyURL compatibility
'lang' => true,
'required' => true,
'class' => 'copyMeta2friendlyURL',
'hint' => $this->l('Invalid characters:').' &lt;&gt;;=#{}'
),
array(
'type' => 'text',
'label' => $this->l('Meta description'),
'name' => 'meta_description',
'lang' => true,
'hint' => $this->l('Invalid characters:').' &lt;&gt;;=#{}'
),
array(
'type' => 'tags',
'label' => $this->l('Meta keywords'),
'name' => 'meta_keywords',
'lang' => true,
'hint' => array(
$this->l('To add "tags" click in the field, write something, and then press "Enter."'),
$this->l('Invalid characters:').' &lt;&gt;;=#{}'
)
),
array(
'type' => 'text',
'label' => $this->l('Friendly URL'),
'name' => 'link_rewrite',
'required' => true,
'lang' => true,
'hint' => $this->l('Only letters and the hyphen (-) character are allowed.')
),
array(
'type' => 'textarea',
'label' => $this->l('Intro'),
'name' => 'content',
'autoload_rte' => true,
'lang' => true,
'rows' => 5,
'cols' => 40,
'hint' => $this->l('Invalid characters:').' <>;=#{}'
),
array(
'type' => 'switch',
'label' => $this->l('Indexation by search engines'),
'name' => 'indexation',
'required' => false,
'class' => 't',
'is_bool' => true,
'values' => array(
array(
'id' => 'indexation_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'indexation_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
array(
'type' => 'switch',
'label' => $this->l('Displayed'),
'name' => 'active',
'required' => false,
'is_bool' => true,
'values' => array(
array(
'id' => 'active_on',
'value' => 1,
'label' => $this->l('Enabled')
),
array(
'id' => 'active_off',
'value' => 0,
'label' => $this->l('Disabled')
)
),
),
),
'submit' => array(
'title' => $this->l('Save'),
),
'buttons' => array(
'save_and_preview' => array(
'name' => 'viewcms',
'type' => 'submit',
'title' => $this->l('Save and preview'),
'class' => 'btn btn-default pull-right',
'icon' => 'process-icon-preview'
)
)
);
if (Shop::isFeatureActive()) {
$this->fields_form['input'][] = array(
'type' => 'shop',
'label' => $this->l('Shop association'),
'name' => 'checkBoxShopAsso',
);
}
if (Validate::isLoadedObject($this->object)) {
$this->context->smarty->assign('url_prev', $this->getPreviewUrl($this->object));
}
$languages = Language::getLanguages();
// Gestion des matrices
$matrices = !empty($this->object->id) && !empty($this->object->matrices) ? array_filter(unserialize(base64_decode($this->object->matrices))) : false;
$this->context->smarty->assign(array(
'base_dir' => _PS_BASE_URL_.__PS_BASE_URI__,
'matrices' => $matrices,
'languages' => $languages,
'optionsClass' => self::getOptionsClass()
));
return AdminController::renderForm();
}
public static function getOptionsClass()
{
return array(
'classStd' => 'Standard',
'classImgLeft' => 'Image à gauche',
'classImgRight' => 'Image à droite',
'classExergue' => 'Exergue',
);
}
public function copyFromPost(&$object, $table)
{
parent::copyFromPost($object, $table);
$matrices = Tools::getValue('matrices', false);
if ($matrices && !empty(array_filter($matrices))) {
$matrices = array_values($matrices); // On réinitialise les index dans le cas où une matrice ait été supprimée
// Upload des images par matrice, par langue
foreach($matrices as $key => $matrice)
{
foreach($matrice['image'] as $id_lang => $img)
{
$matrices[$key]['image'][$id_lang] = (!empty($_FILES['matrices']['tmp_name'][$key]['image'][$id_lang])) ? $this->uploadMatriceImg($key, $id_lang) : $img['name'];
}
}
// Serialization
$object->matrices = base64_encode(serialize($matrices));
}
else {
$object->matrices = null;
}
}
public static function generateMatrice($idx)
{
$tpl = Context::getContext()->smarty->createTemplate(_PS_ROOT_DIR_.'/override/controllers/admin/templates/cms/helpers/form/matrice.tpl');
$tpl->assign(array(
'idx' => $idx,
'matrices' => false,
'languages' => Language::getLanguages(false),
'optionsClass' => self::getOptionsClass(),
'last' => true,
));
return $tpl->fetch();
}
public function uploadMatriceImg($key, $id_lang)
{
$folder = 'cms/matrices';
$fileTemp = $_FILES['matrices']['tmp_name'][$key]['image'][$id_lang];
$fileParts = pathinfo($_FILES['matrices']['name'][$key]['image'][$id_lang]);
$name = $fileParts['filename'] . '-' . time();
if($fileParts['extension'] == 'jpg' || $fileParts['extension'] == 'png')
{
if(!is_dir(_PS_IMG_DIR_.$folder))
{
mkdir(_PS_IMG_DIR_.$folder, 0775);
}
return move_uploaded_file($fileTemp, _PS_IMG_DIR_.$folder.'/'.$name.'.jpg') ? $name : false;
}
else
{
return false;
}
}
}