Ejemplo n.º 1
0
 * @var \yii\db\ActiveRecord $model
 * @var array $addAddonTree
 * @var array $bindedAddons
 */
/** @var \app\modules\shop\models\AddonCategory $addonCategories */
/** @var app\backend\components\ActiveForm $form */
/** @var app\modules\shop\models\AddAddonModel $addAddonModel */
?>

<?php 
BackendWidget::begin(['title' => Yii::t('app', 'Addons'), 'icon' => 'cart-plus', 'footer' => $this->blocks['submit']]);
echo \app\backend\widgets\Select2Ajax::widget(['initialData' => [], 'form' => $form, 'model' => $addAddonModel, 'modelAttribute' => 'addon_id', 'multiple' => false, 'searchUrl' => \yii\helpers\Url::to(['/shop/backend-addons/ajax-search-addons']), 'additional' => ['placeholder' => Yii::t('app', 'Search addons...')]]);
?>
<div class="addons-widget">
<?php 
echo AddonsListWidget::widget(['object_id' => $object->id, 'object_model_id' => $model->id, 'bindedAddons' => $bindedAddons]);
?>
</div>
<?php 
BackendWidget::end();
$url = \yii\helpers\Json::encode(Url::to(['/shop/backend-addons/add-addon-binding', 'object_id' => $object->id, 'object_model_id' => $model->id]));
$reorderUrl = \yii\helpers\Json::encode(Url::to(['/shop/backend-addons/reorder', 'object_id' => $object->id, 'object_model_id' => $model->id]));
$js = <<<JS
    \$("#addaddonmodel-addon_id").on('change', function(){
        var val = \$(this).val();
        if (val === '' || val === '0') {
            return;
        }
        // here comes the dragon! \$(this).val() is the addon_id to be added
        \$.ajax({
            url: {$url},
Ejemplo n.º 2
0
 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];
 }