-
Notifications
You must be signed in to change notification settings - Fork 0
/
psatolyeproducttocategory.php
executable file
·230 lines (173 loc) · 5.9 KB
/
psatolyeproducttocategory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?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: 7541 $
* @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 PSAtolyeProducttoCategory extends Module {
public function __construct() {
$this->name = 'psatolyeproducttocategory';
$this->tab = 'others';
$this->version = 1.0;
$this->author = 'Bora Yalçın';
$this->need_instance = 0;
$this->is_configurable = 1;
parent::__construct();
$this->displayName = $this->l('Product to Category by PS Atölye');
$this->description = $this->l('Adds and Removes Products to a Category Fast and Easy');
}
public function install() {
if (parent::install() == false)
return false;
return true;
}
function getContent(){
?>
<div class="conf">
<?php
if(Tools::getValue('update_category'))
{
if($this->insert_into_category()):?>
<p><?php echo $this->l('Process Completed');?></p>
<?php endif;
}
?>
</div>
<?php
$categories = Category::getSimpleCategories(Configuration::get('PS_LANG_DEFAULT'));
?>
<style>
form{
font-size:14px;
}
input[type="text"]{
height:30px;
width:400px;
padding:5px;
font-size:14px;
}
.submitbutton,select{
height:30px;
padding:5px;
font-size:14px;
font-weight:bold;
}
</style>
<form action="<?php echo Tools::safeOutput($_SERVER['REQUEST_URI']);?>" method="post">
<p><?php echo $this->l('Product IDs to Add or Remove (Comma Seperated List)');?></p>
<p><input type="text" name="product_ids"/></p>
<p><?php echo $this->l('The category you want to add/remove the products');?></p>
<p>
<select name="category_id">
<?php foreach ($categories as $keycategory => $category):?>
<option value="<?php echo $category['id_category'];?>"><?php echo $category['name'];?> (id: <?php echo $category['id_category'];?>)</option>
<?php endforeach;?>
</select>
</p>
<p><?php echo $this->l('Do you want to add or remove them from the category?');?></p>
<p>
<select name="actiontype">
<option value="add"><?php echo $this->l('Add to Category');?></option>
<option value="remove"><?php echo $this->l('Remove from Category');?></option>
</select>
</p>
<p><?php echo $this->l('Do you want to add or remove them from the parent categories of selected category? (Except "Home" category)');?></p>
<p>
<select name="recursive">
<option value="true"><?php echo $this->l('Yes, add/remove from parents too');?></option>
<option value="false"><?php echo $this->l('No, add/remove from this one only');?></option>
</select>
</p>
<input type="submit" class="submitbutton" value="<?php echo $this->l('Do It!');?>" name="update_category"/>
</form>
<?php
}
public function insert_into_category(){
//check for actiontype
if(Tools::getValue('actiontype') === 'add') {
$action = 'add';
} elseif(Tools::getValue('actiontype') === 'remove') {
$action = 'remove';
} else {
return false;
}
$recursive = false;
if(Tools::getValue('recursive') == true)
$recursive = true;
$id_category = Tools::getValue('category_id');
$id_products = Tools::getValue('product_ids');
//check category exists
if(true != Category::categoryExists($id_category)) {
return false;
}
//current category
$category = new Category($id_category);
if($recursive === true) {
//parents for recursive
$parents = $category->getParentsCategories();
//parents to array
$parent_ids = array();
foreach($parents as $p)
{
//skip the Home category
if($p == 1)continue;
$parent_ids[] = $p['id_category'];
}
}
//products to array
$products = explode(',',trim($id_products));
//add remove
foreach ($products as $keyproduct => $product) {
if( $this->product_id_exists($product) == false ) {
echo '<p>'.$product.' is a wrong product ID!'.'</p>';
continue;
}
$theproduct = new Product( $product );
if($action == 'add') {
$theproduct->addToCategories( array($id_category) );
//add to parent categories too
if($recursive === true && !empty($parent_ids) && $category->level_depth != 0) {
$theproduct->addToCategories($parent_ids);
}
echo '<p>'.$theproduct->name[Configuration::get('PS_LANG_DEFAULT')].' is added</p>';
}
elseif($action == 'remove') {
$theproduct->deleteCategory( $id_category );
//remove from parent categories too
if($recursive === true && !empty($parent_ids) && $category->level_depth != 0) {
foreach ($parent_ids as $keyparent => $parent_id) {
$theproduct->deleteCategory($parent_id);
}
}
echo '<p>'.$theproduct->name[Configuration::get('PS_LANG_DEFAULT')].' is removed</p>';
}
}
return true;
}
function product_id_exists($id_product){
$exists = Db::getInstance()->getValue('SELECT id_product FROM '._DB_PREFIX_.'product WHERE id_product="'.pSQL($id_product).'"');
return $exists;
}
}