public static function availableAddons($id) { $cacheKey = 'Addons4' . $id; $addons = Yii::$app->cache->get($cacheKey); if ($addons === false) { $addons = Addon::findAll(['addon_category_id' => $id]); Yii::$app->cache->set($cacheKey, $addons, 86400, new TagDependency(['tags' => [ActiveRecordHelper::getCommonTag(Addon::className()), ActiveRecordHelper::getCommonTag(AddonCategory::className())]])); } return $addons; }
public function getBindedAddons() { $object_id = Object::getForClass($this->className())->id; return $this->hasMany(Addon::className(), ['id' => 'addon_id'])->viaTable('{{%addon_bindings}}', ['object_model_id' => 'id'], function ($query) use($object_id) { /** @var \yii\db\Query $query */ $query->andWhere(['appliance_object_id' => $object_id]); $query->orderBy(['sort_order' => SORT_ASC]); return $query; })->innerJoin('{{%addon_bindings}}', 'addon_id=addon.id AND appliance_object_id=:aoid AND object_model_id=:oid', [':aoid' => $object_id, ':oid' => $this->id])->orderBy(['addon_bindings.sort_order' => SORT_ASC]); }
public function up() { $tableOptions = $this->db->driverName === 'mysql' ? 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=MyISAM' : null; $this->insert('{{%object}}', ['name' => 'Addon', 'object_class' => Addon::className(), 'object_table_name' => Yii::$app->db->schema->getRawTableName(Addon::tableName()), 'column_properties_table_name' => Yii::$app->db->schema->getRawTableName('{{%addon_property}}'), 'eav_table_name' => Yii::$app->db->schema->getRawTableName('{{%addon_eav}}'), 'categories_table_name' => Yii::$app->db->schema->getRawTableName('{{%addon_category}}'), 'link_slug_category' => Yii::$app->db->schema->getRawTableName('{{%addon_category_full_slug}}'), 'link_slug_static_value' => Yii::$app->db->schema->getRawTableName('{{%addon_static_value_category}}'), 'object_slug_attribute' => 'slug']); $this->createTable('{{%addon}}', ['id' => $this->primaryKey(), 'name' => $this->text()->notNull(), 'price' => $this->float()->notNull()->defaultValue(0), 'currency_id' => $this->integer()->notNull()->defaultValue(0), 'price_is_multiplier' => $this->boolean()->notNull()->defaultValue(0), 'is_product_id' => $this->integer()->notNull()->defaultValue(0), 'add_to_order' => $this->boolean()->notNull()->defaultValue(0), 'addon_category_id' => $this->integer()->notNull(), 'can_change_quantity' => $this->boolean()->notNull()->defaultValue(0), 'measure_id' => $this->integer()->notNull()->defaultValue(1)], $tableOptions); $this->createIndex('by_category', '{{%addon}}', ['addon_category_id']); if ($this->db->driverName === 'mysql') { // add fulltext index in mysql for name column :-) $this->execute("ALTER TABLE {{%addon}} ADD FULLTEXT(`name`)"); } $this->createTable('{{%addon_category}}', ['id' => $this->primaryKey(), 'name' => $this->text()->notNull()]); // real bindings of addons to objects $this->createTable('{{%addon_bindings}}', ['id' => $this->primaryKey(), 'addon_id' => $this->integer()->notNull(), 'appliance_object_id' => $this->integer()->notNull(), 'object_model_id' => $this->integer()->notNull(), 'sort_order' => $this->integer()->notNull()->defaultValue(0)], $tableOptions); $this->createIndex('addons4object', '{{%addon_bindings}}', ['appliance_object_id', 'object_model_id']); $this->addColumn('{{%order_item}}', 'addon_id', $this->integer()->notNull()->defaultValue(0)); $tblMenu = '{{%backend_menu}}'; /** @var BackendMenu $shopMenuItem */ $shopMenuItem = BackendMenu::findOne(['name' => 'Shop']); $this->batchInsert($tblMenu, ['parent_id', 'name', 'route', 'icon', 'sort_order', 'added_by_ext', 'rbac_check', 'css_class', 'translation_category'], [[$shopMenuItem->id, 'Addons', 'shop/backend-addons/index', 'cart-plus', 0, 'core', 'product manage', '', 'app']]); \yii\caching\TagDependency::invalidate(Yii::$app->cache, [\devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag(\app\backend\models\BackendMenu::className())]); }
/** * @return Addon|null */ public function getAddon() { return $this->hasOne(Addon::className(), ['id' => 'addon_id']); }
public function actionAddAddonBinding($remove = '0') { if (Yii::$app->request->isAjax === false) { throw new BadRequestHttpException(); } Yii::$app->response->format = Response::FORMAT_JSON; $addon_id = Yii::$app->request->post('addon_id', null); $object_id = Yii::$app->request->get('object_id', null); $object_model_id = Yii::$app->request->get('object_model_id', null); if ($addon_id === null || $object_id === null || $object_model_id === null) { throw new BadRequestHttpException(); } $addon = Addon::findById($addon_id); $object = Object::findById($object_id); if ($addon === null || $object === null) { throw new NotFoundHttpException(); } $modelClassName = $object->object_class; $model = $this->loadModel($modelClassName, $object_model_id); // ok, now all's ok, addon and model exist! try { if ($remove === '1') { $model->unlink('bindedAddons', $addon, true); } else { $model->link('bindedAddons', $addon, ['sort_order' => count($model->bindedAddons), 'appliance_object_id' => $object->id]); } } catch (\Exception $e) { if (intval($e->getCode()) == 23000) { return ['data' => Html::tag('div', Yii::t('app', 'Addon is already added'), ['class' => 'alert alert-info']) . AddonsListWidget::widget(['object_id' => $object->id, 'object_model_id' => $model->id, 'bindedAddons' => $model->bindedAddons]), 'error' => false]; } else { return ['data' => Html::tag('div', $e->getMessage(), ['class' => 'alert alert-danger']), 'error' => $e->getMessage()]; } } TagDependency::invalidate(Yii::$app->cache, [Addon::className()]); return ['data' => AddonsListWidget::widget(['object_id' => $object->id, 'object_model_id' => $model->id, 'bindedAddons' => $model->bindedAddons]), 'error' => false]; }
public function rules() { return [['addon_id', 'number', 'integerOnly' => true], ['addon_id', 'exist', 'targetAttribute' => 'id', 'targetClass' => Addon::className()]]; }