[*] BO #PSFV-94 : Added AdminAttachmentsController

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@10090 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
lLefevre
2011-11-14 10:33:57 +00:00
parent 5119bb7355
commit b78b1a1a4c
7 changed files with 274 additions and 214 deletions
-173
View File
@@ -1,173 +0,0 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 6844 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
include_once(_PS_ADMIN_DIR_.'/../classes/AdminTab.php');
class AdminAttachments extends AdminTab
{
private $_productAttachements = array();
public function __construct()
{
$this->table = 'attachment';
$this->className = 'Attachment';
$this->lang = true;
$this->edit = true;
$this->delete = true;
$this->fieldsDisplay = array(
'id_attachment' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25),
'name' => array('title' => $this->l('Name')),
'file' => array('title' => $this->l('File')));
parent::__construct();
}
public function postProcess()
{
/* PrestaShop demo mode */
if (_PS_MODE_DEMO_)
{
$this->_errors[] = Tools::displayError('This functionnality has been disabled.');
return;
}
/* PrestaShop demo mode*/
if (Tools::isSubmit('submitAdd'.$this->table))
{
if ($id = (int)(Tools::getValue('id_attachment')) AND $a = new Attachment($id))
{
$_POST['file'] = $a->file;
$_POST['mime'] = $a->mime;
}
if (!sizeof($this->_errors))
{
if (isset($_FILES['file']) AND is_uploaded_file($_FILES['file']['tmp_name']))
{
if ($_FILES['file']['size'] > (Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024))
$this->_errors[] = $this->l('File too large, maximum size allowed:').' '.(Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024).' '.$this->l('kb').'. '.$this->l('File size you\'re trying to upload is:').number_format(($_FILES['file']['size']/1024), 2, '.', '').$this->l('kb');
else
{
do $uniqid = sha1(microtime()); while (file_exists(_PS_DOWNLOAD_DIR_.$uniqid));
if (!copy($_FILES['file']['tmp_name'], _PS_DOWNLOAD_DIR_.$uniqid))
$this->_errors[] = $this->l('File copy failed');
$_POST['file_name'] = $_FILES['file']['name'];
@unlink($_FILES['file']['tmp_name']);
$_POST['file'] = $uniqid;
$_POST['mime'] = $_FILES['file']['type'];
}
}
else if (array_key_exists('file', $_FILES) && (int)$_FILES['file']['error'] === 1)
{
$max_upload = (int)(ini_get('upload_max_filesize'));
$max_post = (int)(ini_get('post_max_size'));
$upload_mb = min($max_upload, $max_post);
$this->_errors[] = $this->l('the File').' <b>'.$_FILES['file']['name'].'</b> '.$this->l('exceeds the size allowed by the server. This limit is set to').' <b>'.$upload_mb.$this->l('Mb').'</b>';
}
else if (!empty($_FILES['file']['tmp_name']))
$this->_errors[] = $this->l('No file or your file isn\'t uploadable, check your server configuration about the upload maximum size.');
}
$this->validateRules();
}
return parent::postProcess();
}
public function displayForm($isMainTab = true)
{
parent::displayForm();
if (!($obj = $this->loadObject(true)))
return;
echo '
<form action="'.self::$currentIndex.'&submitAdd'.$this->table.'=1&token='.$this->token.'" method="post" enctype="multipart/form-data">
'.($obj->id ? '<input type="hidden" name="id_'.$this->table.'" value="'.$obj->id.'" />' : '').'
<fieldset><legend><img src="../img/t/AdminAttachments.gif" />'.$this->l('Attachment').'</legend>
<label>'.$this->l('Filename:').' </label>
<div class="margin-form">';
foreach ($this->_languages as $language)
echo ' <div id="cname_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $this->_defaultFormLanguage ? 'block' : 'none').'; float: left;">
<input size="33" type="text" name="name_'.$language['id_lang'].'" value="'.htmlentities($this->getFieldValue($obj, 'name', (int)($language['id_lang'])), ENT_COMPAT, 'UTF-8').'" /><sup> *</sup>
</div>';
$this->displayFlags($this->_languages, $this->_defaultFormLanguage, 'cname¤cdescription', 'cname');
echo ' </div>
<div class="clear">&nbsp;</div>
<label>'.$this->l('Description:').' </label>
<div class="margin-form">';
foreach ($this->_languages as $language)
echo ' <div id="cdescription_'.$language['id_lang'].'" style="display: '.($language['id_lang'] == $this->_defaultFormLanguage ? 'block' : 'none').'; float: left;">
<textarea name="description_'.$language['id_lang'].'">'.htmlentities($this->getFieldValue($obj, 'description', (int)($language['id_lang'])), ENT_COMPAT, 'UTF-8').'</textarea>
</div>';
$this->displayFlags($this->_languages, $this->_defaultFormLanguage, 'cname¤cdescription', 'cdescription');
echo ' </div>
<div class="clear">&nbsp;</div>
<label>'.$this->l('File').'</label>
<div class="margin-form">
<p><input type="file" name="file" /></p>
<p>'.$this->l('Upload file from your computer').'</p>
</div>
<div class="clear">&nbsp;</div>
<div class="margin-form">
<input type="submit" value="'.$this->l(' Save ').'" name="submitAdd'.$this->table.'" class="button" />
</div>
<div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
</fieldset>
</form>';
}
public function getList($id_lang, $orderBy = NULL, $orderWay = NULL, $start = 0, $limit = NULL)
{
parent::getList((int)$id_lang, $orderBy, $orderWay, $start, $limit);
if(sizeof($this->_list))
$this->_productAttachements = Attachment::getProductAttached((int)$id_lang, $this->_list);
}
protected function _displayDeleteLink($token = NULL, $id)
{
$_cacheLang['Delete'] = $this->l('Delete');
$_cacheLang['DeleteItem'] = $this->l('Delete item #', __CLASS__, TRUE, FALSE);
if (isset($this->_productAttachements[$id]))
{
$productList = '';
foreach($this->_productAttachements[$id] as $product)
$productList .= $product.', ';
}
echo '
<script>
function confirmProductAttached(productList)
{
if (confirm(\''.$_cacheLang['DeleteItem'].$id.'\'))
return confirm(\''.$this->l('This attachment is used by the following products:').'\r\n\' + productList);
return false;
}
</script>
<a href="'.self::$currentIndex.'&'.$this->identifier.'='.$id.'&delete'.$this->table.'&token='.($token!=NULL ? $token : $this->token).'"
onclick="'.(isset($this->_productAttachements[$id]) ? 'return confirmProductAttached(\''.$productList.'\')' : 'return confirm(\''.$_cacheLang['DeleteItem'].$id.' ?'.(!is_null($this->specificConfirmDelete) ? '\r'.rtrim($this->specificConfirmDelete, ', ') : '').'\')' ).'">
<img src="../img/admin/delete.gif" alt="'.$_cacheLang['Delete'].'" title="'.$_cacheLang['Delete'].'" /></a>';
}
}
@@ -0,0 +1,38 @@
{*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 9197 $
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*}
<script>
function confirmProductAttached(productList)
{
if (confirm('{$confirm}'))
return confirm('{l s="This attachment is used by the following products:"}' + {$product_list[$id]});
return false;
}
</script>
<a href="{$href}" onclick="{if isset($product_attachements[$id])}return confirmProductAttached('{$product_list[$id]}'){else}return confirm('{$confirm}'){/if}">
<img src="../img/admin/delete.gif" alt="{$action}" title="{$action}" />
</a>
@@ -215,7 +215,7 @@
<div class="translatable">
{foreach $languages as $language}
<div class="lang_{$language.id_lang}" id="{$input.name}_{$language.id_lang}" style="display:{if $language.id_lang == $defaultFormLanguage}block{else}none{/if}; float: left;">
<textarea cols="{$input.cols}" rows="{$input.rows}" name="{$input.name}_{$language.id_lang}" {if $input.autoload_rte}class="autoload_rte"{/if} >{$fields_value[$input.name][$language.id_lang]}</textarea>
<textarea cols="{$input.cols}" rows="{$input.rows}" name="{$input.name}_{$language.id_lang}" {if isset($input.autoload_rte) && $input.autoload_rte}class="autoload_rte"{/if} >{$fields_value[$input.name][$language.id_lang]}</textarea>
</div>
{/foreach}
</div>
+2
View File
@@ -75,6 +75,7 @@ class AdminControllerCore extends Controller
public $tpl_form_vars = array();
public $tpl_list_vars = array();
public $tpl_delete_link_vars = array();
public $tpl_option_vars = array();
public $tpl_view_vars = array();
@@ -1284,6 +1285,7 @@ class AdminControllerCore extends Controller
}
$helper->tpl_vars = $this->tpl_list_vars;
$helper->tpl_delete_link_vars = $this->tpl_delete_link_vars;
$this->setHelperDisplay($helper);
$list = $helper->generateList($this->_list, $this->fieldsDisplay);
$this->toolbar_fix = false;
+51 -37
View File
@@ -27,25 +27,25 @@
class AttachmentCore extends ObjectModel
{
public $file;
public $file_name;
public $name;
public $mime;
public $description;
public $file;
public $file_name;
public $name;
public $mime;
public $description;
/** @var integer position */
public $position;
public $position;
protected $fieldsRequired = array('file', 'mime');
protected $fieldsSize = array('file' => 40, 'mime' => 128, 'file_name' => 128);
protected $fieldsValidate = array('file' => 'isGenericName', 'mime' => 'isCleanHtml', 'file_name' => 'isGenericName');
protected $fieldsRequired = array('file', 'mime');
protected $fieldsSize = array('file' => 40, 'mime' => 128, 'file_name' => 128);
protected $fieldsValidate = array('file' => 'isGenericName', 'mime' => 'isCleanHtml', 'file_name' => 'isGenericName');
protected $fieldsRequiredLang = array('name');
protected $fieldsSizeLang = array('name' => 32);
protected $fieldsValidateLang = array('name' => 'isGenericName', 'description' => 'isCleanHtml');
protected $fieldsRequiredLang = array('name');
protected $fieldsSizeLang = array('name' => 32);
protected $fieldsValidateLang = array('name' => 'isGenericName', 'description' => 'isCleanHtml');
protected $table = 'attachment';
protected $identifier = 'id_attachment';
protected $table = 'attachment';
protected $identifier = 'id_attachment';
public function getFields()
{
@@ -61,66 +61,80 @@ class AttachmentCore extends ObjectModel
$this->validateFieldsLang();
return $this->getTranslationsFields(array('name', 'description'));
}
public function delete()
{
@unlink(_PS_DOWNLOAD_DIR_.$this->file);
Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'product_attachment WHERE id_attachment = '.(int)($this->id));
Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'product_attachment WHERE id_attachment = '.(int)$this->id);
return parent::delete();
}
public function deleteSelection($attachments)
{
$return = 1;
foreach ($attachments AS $id_attachment)
foreach ($attachments as $id_attachment)
{
$attachment = new Attachment((int)($id_attachment));
$attachment = new Attachment((int)$id_attachment);
$return &= $attachment->delete();
}
return $return;
}
public static function getAttachments($id_lang, $id_product, $include = true)
{
return Db::getInstance()->executeS('
SELECT *
FROM '._DB_PREFIX_.'attachment a
LEFT JOIN '._DB_PREFIX_.'attachment_lang al ON (a.id_attachment = al.id_attachment AND al.id_lang = '.(int)($id_lang).')
WHERE a.id_attachment '.($include ? 'IN' : 'NOT IN').' (SELECT pa.id_attachment FROM '._DB_PREFIX_.'product_attachment pa WHERE id_product = '.(int)($id_product).')');
SELECT *
FROM '._DB_PREFIX_.'attachment a
LEFT JOIN '._DB_PREFIX_.'attachment_lang al
ON (a.id_attachment = al.id_attachment AND al.id_lang = '.(int)$id_lang.')
WHERE a.id_attachment '.($include ? 'IN' : 'NOT IN').' (
SELECT pa.id_attachment
FROM '._DB_PREFIX_.'product_attachment pa
WHERE id_product = '.(int)$id_product.'
)'
);
}
public static function attachToProduct($id_product, $array)
{
$result1 = Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'product_attachment WHERE id_product = '.(int)($id_product));
$result1 = Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'product_attachment WHERE id_product = '.(int)$id_product);
if (is_array($array))
{
$ids = array();
foreach ($array as $id_attachment)
$ids[] = '('.(int)($id_product).','.(int)($id_attachment).')';
Db::getInstance()->execute('UPDATE '._DB_PREFIX_.'product SET cache_has_attachments = '.(count($ids) ? '1' : '0').' WHERE id_product = '.(int)($id_product).' LIMIT 1');
return ($result1 && Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'product_attachment (id_product, id_attachment) VALUES '.implode(',',$ids)));
$ids[] = '('.(int)$id_product.','.(int)$id_attachment.')';
Db::getInstance()->execute('
UPDATE '._DB_PREFIX_.'product
SET cache_has_attachments = '.(count($ids) ? '1' : '0').'
WHERE id_product = '.(int)$id_product.'
LIMIT 1
');
return ($result1 && Db::getInstance()->execute('
INSERT INTO '._DB_PREFIX_.'product_attachment (id_product, id_attachment)
VALUES '.implode(',', $ids))
);
}
return $result1;
}
public static function getProductAttached($id_lang, $list)
{
$ids_attachements = array();
if (is_array($list))
{
foreach($list as $attachement)
foreach ($list as $attachement)
$ids_attachements[] = $attachement['id_attachment'];
$sql = 'SELECT * FROM `'._DB_PREFIX_.'product_attachment` pa
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pa.`id_product` = pl.`id_product`'.Context::getContext()->shop->addSqlRestrictionOnLang('pl').')
WHERE `id_attachment` IN ('.implode(',', array_map('intval', $ids_attachements)).')
AND pl.`id_lang` = '.(int)$id_lang;
$tmp = Db::getInstance()->executeS($sql);
$productAttachements = array();
foreach($tmp as $t)
$productAttachements[$t['id_attachment']][] = $t['name'];
return $productAttachements;
}
$product_attachements = array();
foreach ($tmp as $t)
$product_attachements[$t['id_attachment']][] = $t['name'];
return $product_attachements;
}
else
return false;
}
+3 -3
View File
@@ -449,14 +449,14 @@ class HelperListCore extends Helper
if (!array_key_exists('DeleteItem', self::$cache_lang))
self::$cache_lang['DeleteItem'] = $this->l('Delete item #', __CLASS__, true, false);
$tpl->assign(array(
$tpl->assign(array_merge($this->tpl_delete_link_vars, array(
'href' => $this->currentIndex.'&'.$this->identifier.'='.$id.'&delete'.$this->table.'&token='.($token != null ? $token : $this->token),
'confirm' => (!is_null($this->specificConfirmDelete) ? '\r'.$this->specificConfirmDelete : self::$cache_lang['DeleteItem'].$id.' ? '),
'action' => self::$cache_lang['Delete'],
));
'id' => $id,
)));
return $tpl->fetch();
}
/**
@@ -0,0 +1,179 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 6844 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class AdminAttachmentsControllerCore extends AdminController
{
private $product_attachements = array();
public function __construct()
{
$this->table = 'attachment';
$this->className = 'Attachment';
$this->lang = true;
$this->addRowAction('edit');
$this->addRowAction('delete');
$this->fieldsDisplay = array(
'id_attachment' => array(
'title' => $this->l('ID'),
'align' => 'center',
'width' => 25
),
'name' => array(
'title' => $this->l('Name')
),
'file' => array(
'title' => $this->l('File')
)
);
parent::__construct();
}
public function initForm()
{
$this->fields_form = array(
'legend' => array(
'title' => $this->l('Attachment'),
'image' => '../img/t/AdminAttachments.gif'
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('Filename:'),
'name' => 'name',
'size' => 33,
'required' => true,
'lang' => true,
),
array(
'type' => 'textarea',
'label' => $this->l('Description:'),
'name' => 'description',
'cols' => 40,
'rows' => 10,
'lang' => true,
),
array(
'type' => 'file',
'label' => $this->l('File:'),
'name' => 'file',
'p' => $this->l('Upload file from your computer')
),
),
'submit' => array(
'title' => $this->l(' Save '),
'class' => 'button'
)
);
return parent::initForm();
}
public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
{
parent::getList((int)$id_lang, $order_by, $order_way, $start, $limit, $id_lang_shop);
if (count($this->_list))
{
$this->product_attachements = Attachment::getProductAttached((int)$id_lang, $this->_list);
$list_product_list = array();
foreach ($this->_list as $list)
{
$product_list = '';
if (isset($this->product_attachements[$list['id_attachment']]))
{
foreach ($this->product_attachements[$list['id_attachment']] as $product)
$product_list .= $product.', ';
}
$list_product_list[$list['id_attachment']] = $product_list;
}
// Assign array in list_action_delete.tpl
$this->tpl_delete_link_vars = array(
'product_list' => $list_product_list,
'product_attachements' => $this->product_attachements
);
}
}
public function postProcess()
{
/* PrestaShop demo mode */
if (_PS_MODE_DEMO_)
{
$this->_errors[] = Tools::displayError('This functionnality has been disabled.');
return;
}
/* PrestaShop demo mode*/
if (Tools::isSubmit('submitAdd'.$this->table))
{
$id = (int)Tools::getValue('id_attachment');
if ($id && $a = new Attachment($id))
{
$_POST['file'] = $a->file;
$_POST['mime'] = $a->mime;
}
if (!count($this->_errors))
{
if (isset($_FILES['file']) && is_uploaded_file($_FILES['file']['tmp_name']))
{
if ($_FILES['file']['size'] > (Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024 * 1024))
$this->_errors[] = $this->l('File too large, maximum size allowed:').' '.
(Configuration::get('PS_ATTACHMENT_MAXIMUM_SIZE') * 1024).' '.$this->l('kb').'. '.
$this->l('File size you\'re trying to upload is:').number_format(($_FILES['file']['size'] / 1024), 2, '.', '').$this->l('kb');
else
{
do $uniqid = sha1(microtime());
while (file_exists(_PS_DOWNLOAD_DIR_.$uniqid));
if (!copy($_FILES['file']['tmp_name'], _PS_DOWNLOAD_DIR_.$uniqid))
$this->_errors[] = $this->l('File copy failed');
$_POST['file_name'] = $_FILES['file']['name'];
@unlink($_FILES['file']['tmp_name']);
$_POST['file'] = $uniqid;
$_POST['mime'] = $_FILES['file']['type'];
}
}
else if (array_key_exists('file', $_FILES) && (int)$_FILES['file']['error'] === 1)
{
$max_upload = (int)ini_get('upload_max_filesize');
$max_post = (int)ini_get('post_max_size');
$upload_mb = min($max_upload, $max_post);
$this->_errors[] = $this->l('the File').' <b>'.$_FILES['file']['name'].'</b> '.
$this->l('exceeds the size allowed by the server. This limit is set to').' <b>'.$upload_mb.$this->l('Mb').'</b>';
}
else if (!empty($_FILES['file']['tmp_name']))
$this->_errors[] = $this->l('No file or your file isn\'t uploadable, check your server configuration about the upload maximum size.');
}
$this->validateRules();
}
return parent::postProcess();
}
}