Skip to content

Instantly share code, notes, and snippets.

@alectoist
Created January 27, 2016 10:26
Show Gist options
  • Save alectoist/b2b9119fe1fee30bb9a2 to your computer and use it in GitHub Desktop.
Save alectoist/b2b9119fe1fee30bb9a2 to your computer and use it in GitHub Desktop.
Prestashop file uploader usage
<?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('Page content'),
'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')
)
),
),
array(
'type' => 'file',
'label' => $this->l('Attached files'),
'name' => 'attached_files',
'multiple' => true,
'required' => false,
),
),
'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));
}
$this->tpl_form_vars = array(
'active' => $this->object->active,
'PS_ALLOW_ACCENTED_CHARS_URL', (int)Configuration::get('PS_ALLOW_ACCENTED_CHARS_URL')
);
$smarty = Context::getContext()->smarty;
$smarty->assign('list', 'list');
return AdminController::renderForm();
}
public function initFormAttachments()
{
$helper = new HelperUploader();
return $helper->render();
}
public function postProcess()
{
if ($_FILES)
$this->processUploads();
parent::postProcess();
}
private function processUploads()
{
$files_being_uploaded = $_FILES["attached_files"];
$savepath = _PS_ROOT_DIR_.DIRECTORY_SEPARATOR.'upload'.DIRECTORY_SEPARATOR.'cms_uploads/';
$uploader = new Uploader("attached_files");
$uploader->setCheckFileSize(false);
$uploader->setSavePath($savepath);
$file = $uploader->process();
return true;
}
private function processUploadsErrors($params=null)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment