[+] MO : Adding the favoriteproducts module - Part1

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@7978 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
gBrunier
2011-08-09 16:35:11 +00:00
parent d36b369b45
commit 3957de6087
18 changed files with 608 additions and 0 deletions
@@ -0,0 +1,115 @@
<?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$
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class FavoriteProduct extends ObjectModel
{
public $id;
public $id_product;
public $id_customer;
public $id_shop;
public $date_add;
public $date_upd;
protected $fieldRequired = array(
'id_product',
'id_customer',
'id_shop'
);
protected $fieldsValidate = array(
'id_product' => 'isUnsignedInt',
'id_customer' => 'isUnsignedInt',
'id_shop' => 'isUnsignedInt'
);
protected $table = 'favorite_product';
protected $identifier = 'id_favorite_product';
public function getFields()
{
parent::validateFields();
$fields['id_product'] = (int)$this->id_product;
$fields['id_customer'] = (int)$this->id_customer;
$fields['id_shop'] = (int)$this->id_shop;
return $fields;
}
public static function getFavoriteProducts($id_customer, $id_lang, Shop $shop = null)
{
if (!$shop)
$shop = Context::getContext()->shop;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT fp.`id_shop`, p.`id_product`, pl.`description_short`, pl.`link_rewrite`, pl.`name`, i.`id_image`, CONCAT(p.`id_product`, \'-\', i.`id_image`) as image
FROM `'._DB_PREFIX_.'favorite_product` fp
LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product` = fp.`id_product`)
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).$shop->sqlLang('pl').')
LEFT OUTER JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product` AND `default_on` = 1)
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).')
WHERE p.`active` = 1
'.$shop->sqlRestriction(false, 'fp'));
}
public static function getFavoriteProduct($id_customer, $id_product, Shop $shop = null)
{
if (!$shop)
$shop = Context::getContext()->shop;
$id_favorite_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT `id_favorite_product`
FROM `'._DB_PREFIX_.'favorite_product`
WHERE `id_customer` = '.(int)($id_customer).'
AND `id_product` = '.(int)($id_product).'
AND `id_shop` = '.(int)($shop->getID()));
if ($id_favorite_product)
return new FavoriteProduct($id_favorite_product);
return null;
}
public static function isCustomerFavoriteProduct($id_customer, $id_product, Shop $shop = null)
{
if (!$shop)
$shop = Context::getContext()->shop;
return (bool)Db::getInstance()->getValue('
SELECT COUNT(*)
FROM `'._DB_PREFIX_.'favorite_product`
WHERE `id_customer` = '.(int)($id_customer).'
AND `id_product` = '.(int)($id_product).'
AND `id_shop` = '.(int)($shop->getID()));
}
}
+12
View File
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>favoriteproducts</name>
<displayName><![CDATA[Favorite Products]]></displayName>
<version><![CDATA[1]]></version>
<description><![CDATA[Display a page with the favorite products of the customer]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[front_office_features]]></tab>
<is_configurable>0</is_configurable>
<need_instance>0</need_instance>
<limited_countries></limited_countries>
</module>
+4
View File
@@ -0,0 +1,4 @@
<?php
global $_MODULE;
$_MODULE = array();
+10
View File
@@ -0,0 +1,10 @@
<?php
require_once dirname(__FILE__).'/../../config/config.inc.php';
include_once(dirname(__FILE__).'/FavoriteProduct.php');
if(FavoriteProduct::isCustomerFavoriteProduct(2,2))
echo 'yes';
else
echo 'non';
?>
+4
View File
@@ -0,0 +1,4 @@
<?php
global $_MODULE;
$_MODULE = array();
+4
View File
@@ -0,0 +1,4 @@
<?php
global $_MODULE;
$_MODULE = array();
@@ -0,0 +1,48 @@
<?php
/*
* 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$
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/* SSL Management */
$useSSL = true;
require_once(dirname(__FILE__).'/../../config/config.inc.php');
require_once(dirname(__FILE__).'/../../init.php');
require_once(dirname(__FILE__).'/FavoriteProduct.php');
if (!Context::getContext()->cookie->isLogged())
Tools::redirect('authentication.php?back=modules/favoriteproducts/favoriteproducts.php');
include(dirname(__FILE__).'/../../header.php');
if ((int)Context::getContext()->cookie->id_customer)
{
$smarty->assign('favoriteProducts', FavoriteProduct::getFavoriteProducts((int)Context::getContext()->cookie->id_customer, (int)Context::getContext()->cookie->id_lang));
echo Module::display(dirname(__FILE__).'/favoriteproducts.php', 'favoriteproducts-account.tpl');
}
include(dirname(__FILE__).'/../../footer.php');
@@ -0,0 +1,79 @@
{*
* 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$
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*}
<script type="text/javascript">
$('documnet').ready(function(){
$('img[rel^=ajax_id_favoriteproduct_]').click(function(){
var idFavoriteProduct = $(this).attr('rel').replace('ajax_id_favoriteproduct_', '');
var parent = $(this).parent().parent();
$.ajax({
url: "favoriteproducts-ajax.php",
post: "POST",
data: "id_product=" + idFavoriteProduct + "&action=remove",
success: function(result){
if (result == '0')
{
parent.fadeOut("normal", function() {
parent.remove();
});
}
}
});
});
});
</script>
{capture name=path}<a href="{$link->getPageLink('my-account.php', true)}">{l s='My account' mod='favoriteproducts'}</a><span class="navigation-pipe">{$navigationPipe}</span>{l s='My favorite products' mod='favoriteproducts'}{/capture}
{include file="$tpl_dir./breadcrumb.tpl"}
<div id="favoriteproducts_block_account">
<h2>{l s='My favorite products' mod='favoriteproducts'}</h2>
{if $favoriteProducts}
<div>
{foreach from=$favoriteProducts item=favoriteProduct}
<div class="clearfix favoriteproduct">
<a href="{$link->getProductLink($favoriteProduct.id_product, null, null, null, null, $favoriteProduct.id_shop)}" class="fl"><img src="{$link->getImageLink($favoriteProduct.link_rewrite, $favoriteProduct.image, 'medium')}" alt=""/></a>
<div class="fl favoriteproduct_description">
<p><a href="{$link->getProductLink($favoriteProduct.id_product, null, null, null, null, $favoriteProduct.id_shop)}"><strong>{$favoriteProduct.name}</strong></a></p>
<p>{$favoriteProduct.description_short}</p>
</div>
<div class="fr">
<img style="cursor:pointer;" rel="ajax_id_favoriteproduct_{$favoriteProduct.id_product}" src="{$img_dir}icon/delete.gif" alt="" class="icon" />
</div>
</div>
{/foreach}
</div>
{else}
<p class="warning">{l s='No favorite products yet.' mod='favoriteproducts'}</p>
{/if}
<ul class="footer_links">
<li><a href="{$link->getPageLink('my-account.php', true)}"><img src="{$img_dir}icon/my-account.gif" alt="" class="icon" /></a><a href="{$link->getPageLink('my-account.php', true)}">{l s='Back to Your Account' mod='favoriteproducts'}</a></li>
<li><a href="{$base_dir}"><img src="{$img_dir}icon/home.gif" alt="" class="icon" /></a><a href="{$base_dir}">{l s='Home' mod='favoriteproducts'}</a></li>
</ul>
</div>
@@ -0,0 +1,64 @@
<?php
/*
* 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$
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
require_once(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/FavoriteProduct.php');
if (Tools::getValue('action') AND Tools::getValue('id_product') AND Context::getContext()->cookie->id_customer)
{
if (Tools::getValue('action') == 'remove')
{
// check if product exists
$product = new Product((int)Tools::getValue('id_product'));
if (!Validate::isLoadedObject($product))
die('0');
$favoriteProduct = FavoriteProduct::getFavoriteProduct((int)Context::getContext()->cookie->id_customer, (int)$product->id);
if ($favoriteProduct)
if ($favoriteProduct->delete())
die('0');
die('1');
}
elseif (Tools::getValue('action') == 'add')
{
$product = new Product((int)Tools::getValue('id_product'));
// check if product exists
if (!Validate::isLoadedObject($product) || FavoriteProduct::isCustomerFavoriteProduct((int)Context::getContext()->cookie->id_customer, (int)$product->id))
die('1');
$favoriteProduct = new FavoriteProduct();
$favoriteProduct->id_product = $product->id;
$favoriteProduct->id_customer = (int)Context::getContext()->cookie->id_customer;
$favoriteProduct->id_shop = (int)Context::getContext()->shop->getID();
if ($favoriteProduct->add())
die('0');
die('1');
}
else
die('1');
}
else
die('1');
@@ -0,0 +1,51 @@
{*
* 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$
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*}
<script type="text/javascript">
$('document').ready(function(){
$('#add_favorites_btn').click(function(){
$.ajax({
url: "{$module_dir}favoriteproducts-ajax.php",
post: "POST",
data: "id_product={$smarty.get.id_product}&action=add",
success: function(result){
if (result == '0')
{
$('#add_favorites_btn').fadeOut("normal", function() {
$('#add_favorites_btn').remove();
});
}
}
});
});
})
</script>
{if !$isCustomerFavoriteProduct AND $isLogged}
<div id="favoriteproducts_block_extra">
<span id="add_favorites_btn"><img src="{$module_dir}img/add_favorite.gif"/>{l s='Add this product to my favorites' mod='favoriteproducts'}</span>
</div>
{/if}
@@ -0,0 +1,26 @@
#favoriteproducts_block_extra span#add_favorites_btn {
cursor: pointer
}
#favoriteproducts_block_account div.favoriteproduct {
background: none repeat scroll 0 0 #F1F2F4;
border: 1px solid #D0D3D8;
padding: 10px;
margin: 10px 0
}
#favoriteproducts_block_account div.favoriteproduct .fl {
float: left
}
#favoriteproducts_block_account div.favoriteproduct .fr {
float: right
}
#favoriteproducts_block_account div.favoriteproduct img {
margin: 5px
}
#favoriteproducts_block_account div.favoriteproduct_description {
width: 350px
}
@@ -0,0 +1,111 @@
<?php
/*
* 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$
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_CAN_LOAD_FILES_'))
exit;
class FavoriteProducts extends Module
{
public function __construct()
{
$this->name = 'favoriteproducts';
$this->tab = 'front_office_features';
$this->version = 1.0;
$this->author = 'PrestaShop';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Favorite Products');
$this->description = $this->l('Display a page with the favorite products of the customer');
}
public function install()
{
if (!parent::install()
OR !$this->registerHook('myAccountBlock')
OR !$this->registerHook('extraLeft')
OR !$this->registerHook('header'))
return false;
if (!Db::getInstance()->Execute('
CREATE TABLE `'._DB_PREFIX_.'favorite_product` (
`id_favorite_product` int(10) unsigned NOT NULL auto_increment,
`id_product` int(10) unsigned NOT NULL,
`id_customer` int(10) unsigned NOT NULL,
`id_shop` int(10) unsigned NOT NULL,
`date_add` datetime NOT NULL,
`date_upd` datetime NOT NULL,
PRIMARY KEY (`id_favorite_product`))
ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'))
return false;
return true;
}
public function uninstall()
{
if (!parent::uninstall() OR !Db::getInstance()->Execute('DROP TABLE `'._DB_PREFIX_.'favorite_product`'))
return false;
return true;
}
public function hookCustomerAccount($params)
{
include_once(dirname(__FILE__).'/FavoriteProduct.php');
$favoriteProducts = FavoriteProduct::getFavoriteProducts((int)$this->context->cookie->id_customer, (int)$this->context->cookie->id_lang);
$this->context->smarty->assign(array('favorite_products' => $favoriteProducts));
return $this->display(__FILE__, 'my-account.tpl');
}
public function hookMyAccountBlock($params)
{
return $this->hookCustomerAccount($params);
}
public function hookExtraLeft($params)
{
include_once(dirname(__FILE__).'/FavoriteProduct.php');
$this->context->smarty->assign(array(
'isCustomerFavoriteProduct' => (FavoriteProduct::isCustomerFavoriteProduct((int)($this->context->cookie->id_customer), Tools::getValue('id_product')) ? 1 : 0),
'isLogged' => (int)$this->context->cookie->id_customer));
return $this->display(__FILE__, 'favoriteproducts-extra.tpl');
}
public function hookHeader($params)
{
$this->context->controller->addCSS($this->_path.'favoriteproducts.css', 'all');
}
}
+13
View File
@@ -0,0 +1,13 @@
<?php
global $_MODULE;
$_MODULE = array();
$_MODULE['<{favoriteproducts}prestashop>favoriteproducts-account_d95cf4ab2cbf1dfb63f066b50558b07d'] = 'Mon compte';
$_MODULE['<{favoriteproducts}prestashop>favoriteproducts-account_a34e0796b9c15d09300f67d972379722'] = 'Mes produits favoris';
$_MODULE['<{favoriteproducts}prestashop>favoriteproducts-account_e2233f8a7cec20324ed48bc32ec98a5d'] = 'Vous ne possédez pas de produit favoris pour le moment';
$_MODULE['<{favoriteproducts}prestashop>favoriteproducts-account_0b3db27bc15f682e92ff250ebb167d4b'] = 'Retour à votre compte';
$_MODULE['<{favoriteproducts}prestashop>favoriteproducts-account_8cf04a9734132302f96da8e113e80ce5'] = 'Accueil';
$_MODULE['<{favoriteproducts}prestashop>favoriteproducts-extra_77b65985b439ff157c1c13d63c9dc294'] = 'Ajouter ce produit à mes favoris';
$_MODULE['<{favoriteproducts}prestashop>favoriteproducts_c249aeb21294d5e97598462b550e73eb'] = 'Produits Favoris';
$_MODULE['<{favoriteproducts}prestashop>favoriteproducts_0db149d6f1284b51aa48c72dd1b0e7c6'] = 'Affiche une page avec les produits favoris de l\'utilisateur';
$_MODULE['<{favoriteproducts}prestashop>my-account_a34e0796b9c15d09300f67d972379722'] = 'Mes produits favoris';
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

+36
View File
@@ -0,0 +1,36 @@
<?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$
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: ../");
exit;
+4
View File
@@ -0,0 +1,4 @@
<?php
global $_MODULE;
$_MODULE = array();
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

+27
View File
@@ -0,0 +1,27 @@
{*
* 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$
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*}
<li><a href="{$base_dir_ssl}modules/favoriteproducts/favoriteproducts-account.php" title="{l s='My favorite products' mod='favoriteproducts'}">{l s='My favorite products' mod='favoriteproducts'}</a></li>