Skip to content

Commit

Permalink
load
Browse files Browse the repository at this point in the history
  • Loading branch information
andrtechno committed Oct 19, 2021
1 parent 3513c56 commit aaae276
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 103 deletions.
2 changes: 1 addition & 1 deletion components/CurrencyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function getById($id)
if (isset($this->_currencies[$id]))
return (object) $this->_currencies[$id];

return (object) $this->_default[$id];
return (object) $this->_default;
}
/**
* @param int $id currency id
Expand Down
2 changes: 2 additions & 0 deletions components/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@ public function countRootAttributeProducts($attribute, $option)
if ($newData)
$model->getFindByEavAttributes2($newData);



return $model->createCommand()->queryScalar();
}

Expand Down
8 changes: 7 additions & 1 deletion components/FilterV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use panix\mod\shop\models\traits\EavQueryTrait;
use yii\base\BaseObject;
use yii\base\Component;
use yii\caching\TagDependency;
use yii\db\ActiveQuery;
use panix\mod\shop\models\query\ProductQuery;
use yii\db\ActiveRecord;
Expand Down Expand Up @@ -665,7 +666,8 @@ public function countRootAttributeProducts($attribute, $option)
if ($newData)
$model->getFindByEavAttributes2($newData);


$model->cache(0,new TagDependency(['tags' => $attribute->name.'-'.$option->id]));
//TagDependency::invalidate(Yii::$app->cache, 'user-123');
return $model->createCommand()->queryScalar();
}

Expand Down Expand Up @@ -739,6 +741,10 @@ public function countAttributeProductsCallback($attribute, $option)
if ($attribute->name == 'tip') {

}

$model->cache(0,new TagDependency(['tags' => $attribute->name.'-'.$option->id]));


//echo $model->createCommand()->rawSql;die;
return $model->createCommand()->queryScalar();
}
Expand Down
69 changes: 57 additions & 12 deletions components/ImageBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace panix\mod\shop\components;

use panix\engine\CMS;
use panix\engine\components\ImageHandler;
use panix\mod\shop\models\ProductImage;
use Yii;
use yii\base\Exception;
use yii\db\ActiveRecord;
use yii\db\Query;
use yii\helpers\BaseFileHelper;
use yii\helpers\FileHelper;
use yii\httpclient\Client;
use panix\engine\CMS;
use panix\engine\components\ImageHandler;
use panix\mod\shop\models\ProductImage;

class ImageBehavior extends \yii\base\Behavior
{
Expand Down Expand Up @@ -58,6 +58,43 @@ public function afterSave()
}
}

/**
* Remove all model images
*/
public function afterDelete()
{
$images = $this->owner->getImages();
if ($images->count() < 1) {
return true;
} else {
foreach ($images->all() as $image) {
$this->removeImage($image);
}

$path = Yii::getAlias($this->savePath) . DIRECTORY_SEPARATOR . $this->owner->primaryKey;
BaseFileHelper::removeDirectory($path);
}
}

/**
* removes concrete model's image
* @param ProductImage $img
* @return bool
* @throws \Exception
*/
public function removeImage(ProductImage $img)
{

$storePath = Yii::getAlias('@uploads/store');

$fileToRemove = Yii::getAlias($this->savePath);
if (preg_match('@\.@', $fileToRemove) and is_file($fileToRemove)) {
unlink($fileToRemove);
}
$img->delete();
return true;
}

protected function updateMainImage()
{
$post = Yii::$app->request->post('AttachmentsMainId');
Expand All @@ -81,8 +118,8 @@ protected function updateMainImage()
if ($currentMainId != $post) {
$customer->is_main = 1;
$customer->update();
$this->owner->main_image = $customer->filename;
$this->owner->save(false);
//$this->owner->main_image = $customer->filename;
// $this->owner->save(false);
}
}
}
Expand Down Expand Up @@ -190,7 +227,8 @@ public function attachImage($file, $is_main = false, $alt = '')
unlink($newAbsolutePath);
throw new \Exception(array_shift($ar));
}
$img = $image->getImage();

$img = $this->owner->getImage();

//If main image not exists
if ($img == null || $is_main) {
Expand Down Expand Up @@ -289,18 +327,25 @@ public function getMainImageObject($main=1)
}


public function getModelSubDir($model)
{

$modelName = $this->getShortClass($model);
$modelDir = \yii\helpers\Inflector::pluralize($modelName) . '/' . $model->id;
return $modelDir;
}
/**
* Clear all images cache (and resized copies)
* @return bool
*/
public function clearImagesCache()
{
$cachePath = Yii::$app->getModule('images')->getCachePath();
$subdir = Yii::$app->getModule('images')->getModelSubDir($this->owner);

$dirToRemove = $cachePath . '/' . $subdir;
$subdir = $this->getModelSubDir($this->owner);

$dirToRemove = Yii::getAlias($this->savePath) . '/' . $subdir;

if (preg_match('/' . preg_quote($cachePath, '/') . '/', $dirToRemove)) {
if (preg_match('/' . preg_quote(Yii::getAlias($this->savePath), '/') . '/', $dirToRemove)) {
BaseFileHelper::removeDirectory($dirToRemove);
//exec('rm -rf ' . $dirToRemove);
return true;
Expand Down Expand Up @@ -338,8 +383,8 @@ public function setMainImage($img)
$allImg->setMain(false);
$allImg->save();
}
$this->owner->main_image = $img->filename;
$this->owner->save(false);
//$this->owner->main_image = $img->filename;
//$this->owner->save(false);
$this->clearImagesCache();
}

Expand Down
83 changes: 74 additions & 9 deletions controllers/admin/ProductController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use panix\mod\shop\models\ProductImage;
use panix\mod\shop\models\TypeAttribute;
use Yii;
use yii\caching\TagDependency;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Json;
Expand Down Expand Up @@ -77,8 +78,8 @@ public function actionIndex()
$dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
//$searchModel->behaviors()['eav']['preload']=false;

// $searchModel->attachBehavior('eav',ArrayHelper::merge($searchModel->behaviors()['eav'],['preload'=>false]));
// CMS::dump($searchModel->behaviors['eav']);die;
// $searchModel->attachBehavior('eav',ArrayHelper::merge($searchModel->behaviors()['eav'],['preload'=>false]));
// CMS::dump($searchModel->behaviors['eav']);die;

$this->pageName = Yii::t('shop/admin', 'PRODUCTS');
if (Yii::$app->user->can("/{$this->module->id}/{$this->id}/*") || Yii::$app->user->can("/{$this->module->id}/{$this->id}/create")) {
Expand Down Expand Up @@ -183,6 +184,40 @@ public function actionUpdate($id = false)
}


$attributes = (isset($model->type->shopAttributes)) ? $model->type->shopAttributes : [];
foreach ($attributes as $a) {
if ($a->group_id) {
$result[$a->group->name][] = $a;
} else {
$result['Без группы'][] = $a;
}

}
$eavList = [];
foreach ($result as $group_name => $attributes) {
foreach ($attributes as $a) {
/** @var Attribute|\panix\mod\shop\components\EavBehavior $a */
// Repopulate data from POST if exists
if (isset($_POST['Attribute'][$a->name])) {
$value = $_POST['Attribute'][$a->name];
} else {

$value = $model->getEavAttribute($a->name);
if ($a->select_many && in_array($a->type, [Attribute::TYPE_SELECT_MANY])) {
$value = [$value];
}

}
$model->_old_eav[$a->name] = $value;
$eavList[$group_name][] = [
'attribute' => $a,
'value' => $value
];
}
}
//CMS::dump($eavList);die;


if ($model->load($post) && $model->validate() && $this->validateAttributes($model) && $this->validatePrices($model)) {
$model->setRelatedProducts(Yii::$app->request->post('RelatedProductId', []));
//$model->setKitProducts(Yii::$app->request->post('kitProductId', []));
Expand All @@ -206,7 +241,6 @@ public function actionUpdate($id = false)
}*/



if ($model->save()) {
//$model->processConfigurations(Yii::$app->request->post('ConfigurationsProduct', []));
$mainCategoryId = 1;
Expand Down Expand Up @@ -258,6 +292,7 @@ public function actionUpdate($id = false)

return $this->render('update', [
'model' => $model,
'eavList' => $eavList
]);
}

Expand Down Expand Up @@ -407,26 +442,56 @@ protected function processAttributes(Product $model)
}
}*/


$reAttributes = [];
foreach ($attributes as $key => $val) {

if (in_array($key, [Attribute::TYPE_TEXT, Attribute::TYPE_TEXTAREA, Attribute::TYPE_YESNO])) {
foreach ($val as $k => $v) {
$reAttributes[$k] = '"' . $v . '"';
if (is_string($v) && $v === '') {
foreach ($val as $k => $value) {
$reAttributes[$k] = '"' . $value . '"';
if (is_string($value) && $value === '') {
unset($reAttributes[$k]);
}
}
} else {
foreach ($val as $k => $v) {
$reAttributes[$k] = $v;
if (is_string($v) && $v === '') {
foreach ($val as $k => $value) {
// if(!is_array($v)){
$reAttributes[$k] = $value;
// }

if (is_array($value)) {
$diff = array_diff($model->_old_eav[$k], $reAttributes[$k]);
if ($diff) {
/* @todo need testing */
foreach ($diff as $i){
TagDependency::invalidate(Yii::$app->cache, $k.'-'.$i);
}


}

} else {
if ($model->_old_eav[$k] != $reAttributes[$k]) {
//clear cache
TagDependency::invalidate(Yii::$app->cache, $k.'-'.$model->_old_eav[$k]);
TagDependency::invalidate(Yii::$app->cache, $k.'-'.$reAttributes[$k]);
}
}


if (is_string($value) && $value === '') {
unset($reAttributes[$k]);
}
}
}
}


//CMS::dump($reAttributes);
// CMS::dump($model->_old_eav);
// die;


return $model->setEavAttributes($reAttributes, true);
}

Expand Down
Loading

0 comments on commit aaae276

Please sign in to comment.