php Subclass for representing row from the product table package lib m

  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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
/**
* Subclass for representing a row from the 'product' table.
*
*
*
* @package lib.model.trustdev
*/
class Product extends BaseProduct
{
private $_category_id_old = null;
/**
* Сохраняем старую категорию.
*
* @param int $v
* @return Product
*/
public function setCategoryId ($v)
{
// для уверенности [sergey]
if(is_null($this->_category_id_old)) $this->_category_id_old = $this->getCategoryId();
return parent::setCategoryId($v);
}
public function getCategoryIdOld()
{
return $this->_category_id_old;
}
public function getStatusTitle()
{
if (!$status = $this->getProductStatus())
return '';
return (string) $status;
}
public function getShopTitle()
{
return $this->getShop()->getTitle();
}
public function getCategoryTitle()
{
return $this->getCategory()->getTitle();
}
public function getPriceWithMoney()
{
$shop = $this->getShop();
if (!$shop)
return $this->getPrice();
$currency = $shop->getCurrency();
if (!$currency)
return $this->getPrice();
return $this->getPrice() . $currency->getCode();
}
public function getPriceFull()
{
$priceWithMoney = $this->getPriceWithMoney();
if (!$priceWithMoney)
return $this->getPriceBase();
$currencyDefault = CurrencyPeer::retrieveByPK(sfConfig::get('app_currency_default_id'));
if (!$currencyDefault)
return $this->getPriceBase();
return sprintf('%d%s (%s)', $this->getPriceBase(), $currencyDefault->getCode(), $priceWithMoney);
}
public function save (PropelPDO $con = null)
{
$shopId = $this->getShopId();
$shop = $this->getShop();
// Сохраняем базовую цену (в дефолтной валюте)
$shopCurrencyRate = $shop->getCurrencyRate();
if ($shopCurrencyRate)
$this->setPriceBase($this->getPrice() / $shopCurrencyRate);
ShopCategoryPeer::onAddProductCategoryLink($shopId, $this->getCategoryId());
// Удаляем все категории товара
if (!$this->isNew()) {
$c = new Criteria();
$c->add(ProductCategoryPeer::PRODUCT_ID, $this->getId());
ProductCategoryPeer::doDelete($c);
}
// Существующий товар + изменена категория -> Уменьшить кол-во товаров в дереве старых категорий
if (!$this->isNew() && in_array(ProductPeer::CATEGORY_ID, $this->modifiedColumns)) {
$category = CategoryPeer::retrieveByPK($this->_category_id_old);
if ($category)
do {
$c = new Criteria();
$c->add(ShopCategoryPeer::SHOP_ID, $this->getShopId());
$c->add(ShopCategoryPeer::CATEGORY_ID, $category->getId());
if ($shopCategory = ShopCategoryPeer::doSelectOne($c)) {
if (1 == $shopCategory->getProductsCount()) { // Удаляем категорию магазина без товаров
$shopCategory->delete();
} else {
$shopCategory->setProductsCount($shopCategory->getProductsCount() - 1);
$shopCategory->save();
}
}
} while ($category = $category->getCategoryRelatedByParentId());
}
// Создаем новые категории магазина + обновляем кол-во товаров в категориях
if ($category = $this->getCategory())
do {
$c = new Criteria();
$c->add(ShopCategoryPeer::SHOP_ID, $this->getShopId());
$c->add(ShopCategoryPeer::CATEGORY_ID, $category->getId());
$shopCategory = ShopCategoryPeer::doSelectOne($c);
if (!$shopCategory) {
$shopCategory = new ShopCategory();
$shopCategory->setShopId($this->getShopId());
$shopCategory->setCategoryId($category->getId());
$shopCategory->setProductsCount(1);
$shopCategory->save();
} else {
// Новый товар или изменена категория -> Увеличить кол-во товаров в новой категории
if ($this->isNew() || in_array(ProductPeer::CATEGORY_ID, $this->modifiedColumns)) {
$shopCategory->setProductsCount($shopCategory->getProductsCount() + 1);
$shopCategory->save();
}
}
} while ($category = $category->getCategoryRelatedByParentId());
parent::save($con);
// Создаем категории товара
if ($category = $this->getCategory())
do {
$productCategory = new ProductCategory();
$productCategory->setProductId($this->getId());
$productCategory->setCategoryId($category->getId());
$productCategory->save();
} while ($category = $category->getCategoryRelatedByParentId());
ShopCategoryPeer::onDeleteProductCategoryLink($this->getShopId(), $this->getCategoryIdOld());
ShopPeer::rebuildShopHasApprovedProduct($shopId);
}
public function delete (PropelPDO $con = null)
{
$shopId = $this->getShopId();
// Удаляем все категории товара
$c = new Criteria();
$c->add(ProductCategoryPeer::PRODUCT_ID, $this->getId());
ProductCategoryPeer::doDelete($c);
// Существующий товар + изменена категория -> Уменьшить кол-во товаров в дереве старых категорий
if (!$this->isNew()) {
if ($category = $this->getCategory())
do {
$c = new Criteria();
$c->add(ShopCategoryPeer::SHOP_ID, $this->getShopId());
$c->add(ShopCategoryPeer::CATEGORY_ID, $category->getId());
if ($shopCategory = ShopCategoryPeer::doSelectOne($c)) {
if (1 == $shopCategory->getProductsCount()) { // Удаляем категорию магазина без товаров
$shopCategory->delete();
} else {
$shopCategory->setProductsCount($shopCategory->getProductsCount() - 1);
$shopCategory->save();
}
}
} while ($category = $category->getCategoryRelatedByParentId());
}
parent::delete($con);
ShopCategoryPeer::onDeleteProductCategoryLink($this->getShopId(), $this->getCategoryId());
ShopPeer::rebuildShopHasApprovedProduct($shopId);
}
public function getParamName ($param = 1, $id = 0)
{
$values = array();
if (1 == $param) {
$values = explode(',', $this->getParam1Values());
} elseif (2 == $param) {
$values = explode(',', $this->getParam2Values());
} elseif (3 == $param) {
$values = explode(',', $this->getParam3Values());
}
if (array_key_exists($id, $values)) {
return $values[$id];
}
}
public function getPhoto ()
{
$c = new Criteria();
$c->add(ProductImagePeer::PRODUCT_ID, $this->getId());
return ProductImagePeer::doSelectOne($c);
}
public function getPhotoFilename ()
{
$photo = $this->getPhoto();
if (!$photo)
return '';
$file = $photo->getFile();
return $file ? $file->getFilename() : '';
}
/**
* @param string $tmpImage
*/
public function addImage($tmpImage)
{
// Создаем запись файла, изначально без информации о файле
$file = new File();
$file->setServerId(1);
$file->save();
// Получаем ID файла, на его основе получаем и имя файла
$fileName = $file->getId() . time() . '.jpg';
$file->setFilename($fileName);
$file->save();
// Уменьшаем картину
$thumbnail = new sfThumbnail(300, 300);
$thumbnail->loadFile($tmpImage);
$thumbnail->save(sfConfig::get('sf_upload_dir') . '/products/' . $fileName, 'image/jpeg');
$thumbnail = new sfThumbnail(100, 100);
$thumbnail->loadFile($tmpImage);
$thumbnail->save(sfConfig::get('sf_upload_dir') . '/products/100x100/' . $fileName, 'image/jpeg');
$thumbnail = new sfThumbnail(50, 50);
$thumbnail->loadFile($tmpImage);
$thumbnail->save(sfConfig::get('sf_upload_dir') . '/products/50x50/' . $fileName, 'image/jpeg');
// Создаем связку
$productImage = new ProductImage();
$productImage->setProductId($this->getId());
$productImage->setFileId($file->getId());
$productImage->save();
}
public function cleanImages()
{
$c = new Criteria();
$c->add(ProductImagePeer::PRODUCT_ID, $this->getId());
ProductImagePeer::doDelete($c);
}
public function getTitleFull()
{
return $this->getProducer() . ' ' . $this->getTitle();
}
}
//xfPropelBehavior::register('Product', array('MainIndex'));