Save shopping cart.

@param $id
@param $instanceName
@param $content
342404
public function createOrUpdate($id, $instanceName, $content)
    {
        if ($this->exists($id, $instanceName)) {
            $this->update($id, $instanceName, $content);
        } else {
            $this->create($id, $instanceName, $content);
        }
    }
883016
public function renderInstance($instanceId, $instanceConfig)
    {
        $view = new ViewModel(
            [
                'instanceId' => $instanceId,
                'instanceConfig' => $instanceConfig,
                'config' => $this->config,
            ]
        );

        $view->setTemplate($this->template);

        return $view;
    }
876387
public void store(Object entity, String id) {
        storeInternal(entity, null, id, ConcurrencyCheckMode.AUTO);
    }
914006
public function findByIdAndInstanceName($id, $instanceName)
    {
        return $this->getConnection()->table($this->getTableName())
            ->where('id', $id)
            ->where('instance', $instanceName)
            ->first(['id', 'instance', 'content']);
    }
678296
public function find_instance($instanceid) {
        foreach ($this->regions as $region => $notused) {
            $this->ensure_instances_exist($region);
            foreach($this->blockinstances[$region] as $instance) {
                if ($instance->instance->id == $instanceid) {
                    return $instance;
                }
            }
        }
        throw new block_not_on_page_exception($instanceid, $this->page);
    }
519806
public void setInstanceId (final String instanceId)
  {
    if (instanceId == null || instanceId.trim ().length () == 0)
    {
      throw new IllegalArgumentException ("Scheduler instanceId cannot be empty.");
    }

    m_sInstanceId = instanceId;
  }
400608
public function saved(ContentInterface $content)
    {
        if ($content->content_type_id !== null) {
            if (! $content->relationLoaded('type')) {
                $content->load('type');
            }

            $contentType = $content->getRelation('type');

            if ($contentType && $contentType->shouldDynamicTableExist() && ! $contentType->dynamicTableExists()) {
                $contentType->createTable();
            }
        }
    }
443685
public function can_view(field_controller $field, int $instanceid) : bool {
        $visibility = $field->get_configdata_property('visibility');
        if ($visibility == self::NOTVISIBLE) {
            return false;
        } else if ($visibility == self::VISIBLETOTEACHERS) {
            return has_capability('moodle/course:update', $this->get_instance_context($instanceid));
        } else {
            return true;
        }
    }
993536
public function actionSave($id = null, $languageId)
    {
        if (!empty($languageId)) {
            if (!empty($id)) {

                $model = $this->findModel($id);
                if (empty($model)) {
                    throw new NotFoundHttpException;
                }
                $modelTranslation = $this->findModelTranslation($id, $languageId);
                if (empty($modelTranslation)) {
                    $modelTranslation = new ProductAvailabilityTranslation();
                }
            }
            else {
                $model = new ProductAvailability();
                $modelTranslation = new ProductAvailabilityTranslation();
            }
        }
        else throw new BadRequestHttpException();


        if ($modelTranslation->load(Yii::$app->request->post())) {

            $model->save(false);

            $modelTranslation->availability_id = $model->id;
            $modelTranslation->language_id = $languageId;
            if ($modelTranslation->validate()) {

                $modelTranslation->save();
                $this->trigger(self::EVENT_AFTER_CREATE_OR_UPDATE_PRODUCT_AVAILABILITY,
                    new ProductAvailabilityEvent(['availability' => $model]));

                return $this->redirect(['save', 'id' => $model->id, 'languageId' => $languageId]);
            }
        }
        return $this->render('save', [
            'model' => $model,
            'modelTranslation' => $modelTranslation,
            'languages' => Language::find()->all(),
            'selectedLanguage' => Language::findOne($languageId)
        ]);

    }
162568
public function save($content, $fileName) {
        $this->renderer = new Png();
        $this->renderer->setHeight($this->width);
        $this->renderer->setWidth($this->height);
        $writer = new Writer($this->renderer);

        $writer->writeFile($content, $fileName);

        return true;
    }
775195
public function save($id, $data, $lifeTime=0) {
		return $this->driver->save($id, $data, $lifeTime);
	}
827405
public function actionSave($id = null, $languageId)
    {
        if (!empty($languageId)) {
            if (!empty($id)) {
                $model = $this->findModel($id);
            }
            else {
                $model = new OrderStatus();
            }

            $modelTranslation = $this->findOrCreateModelTranslation($model->id, $languageId);

            if ($modelTranslation->load(Yii::$app->request->post()) && $model->load(Yii::$app->request->post())) {
                $model->save();
                $modelTranslation->language_id = $languageId;
                $modelTranslation->order_status_id = $model->id;
                if ($modelTranslation->validate()) {
                    $modelTranslation->save();
                    \Yii::$app->session->setFlash('success', \Yii::t('cart', 'The record was successfully saved.'));
                } else {
                    \Yii::$app->session->setFlash('error', \Yii::t('cart', 'An error occurred when saving the record.'));
                }
                return $this->redirect(['save',
                    'id' => $model->id,
                    'languageId' => $languageId
                ]);
            }

            return $this->render('save',
                [
                    'model' => $model,
                    'modelTranslation' => $modelTranslation,
                    'languages' => Language::find()->all(),
                    'selectedLanguage' => Language::findOne($languageId)
                ]);
        } else throw new Exception(\Yii::t('cart', 'Language id can not be empty'));
    }
335141
public function savePropertyId($propertyId, $id = false)
    {
        $em = $this->getEntityManager();
        $config = $id ? $this->find($id) : $this->findFirst();
        $config->setPropertyId($propertyId);
        $em->persist($config);
        $em->flush();
    }
1204847
public static function export_gradingform_instance_data(\context $context, int $instanceid, array $subcontext) {
        static::_export_gradingform_instance_data($context, $instanceid, $subcontext);
    }
191077
public static function init($instance_name, $save_on_demand){

        $request = app('request');
        $session_id = $request->session()->getId();
        $user_id = config('cart.user_id');
        $app = Application::getInstance();
        $carts = $app->offsetGet("cart_instances");

        if ($user_id instanceof \Closure)
            $user_id = $user_id();

        //if user logged in
        if( $user_id ){
            $user_cart = static::active()->user()->where('name', $instance_name)->first();

            $session_cart_id = $request->session()->get('cart_'.$instance_name);
            $session_cart = is_null($session_cart_id)? null: static::active()->session($session_cart_id)->where('name', $instance_name)->first();

            switch (true) {

                case is_null($user_cart) && is_null($session_cart): //no user cart or session cart
                    $attributes = array(
                        'user_id' => $user_id,
                        'name' => $instance_name,
                        'status' => STATUS_ACTIVE
                    );
                    if($save_on_demand)
                        $cart = new static($attributes);
                    else
                        $cart = static::create($attributes);

                    break;

                case !is_null($user_cart) && is_null($session_cart): //only user cart
                    $cart = $user_cart;
                    break;

                case is_null($user_cart) && !is_null($session_cart): //only session cart
                    $cart = $session_cart;
                    $cart->user_id = $user_id;
                    $cart->session = null;
                    $cart->save();
                    break;

                case !is_null($user_cart) && !is_null($session_cart): //both user cart and session cart exists
                    $session_cart->moveItemsTo($user_cart); //move items from session cart to user cart
                    $session_cart->delete(); //delete session cart
                    $cart = $user_cart;
                    break;
            }

            $request->session()->forget('cart_'.$instance_name); //no longer need it.
            $carts[$instance_name] = $cart;
        }
        //guest user, create cart with session id
        else{
            $attributes = array(
                'session' => $session_id,
                'name' => $instance_name,
                'status' => STATUS_ACTIVE
            );
            $cart = static::firstOrNew($attributes);

            if(!$save_on_demand)
                $cart->save();

            //save current session id, since upon login session id will be regenerated
            //we will use this id to get back the cart before login
            $request->session()->put('cart_'.$instance_name, $session_id);
            $carts[$instance_name] = $cart;
        }
        $app->offsetSet("cart_instances", $carts);
        return $carts[$instance_name];
    }
518368
public static function set($className, $instance)
    {
        $key = self::getStorageKey($className);

        if (is_null($instance)) {
            unset(self::$instances[$key]);

            return;
        }

        self::$instances[$key] = $instance;

        return;
    }
310375
public function actionSave($id = null, $languageId = null)
    {
        if (empty($languageId)) {
            $languageId = Language::getCurrent()->id;
        }

        if (!empty($id)) {
            $model = PaymentMethod::findOne($id);
            if (empty($model)) throw new NotFoundHttpException();
            $modelTranslation = PaymentMethodTranslation::find()
                ->where(['payment_method_id' => $model->id, 'language_id' => $languageId])->one();
            if (empty($modelTranslation)) {
                $modelTranslation = new PaymentMethodTranslation();
            }
        }
        else {
            $model = new PaymentMethod();
            $modelTranslation = new PaymentMethodTranslation();
        }
        if (\Yii::$app->request->isPost) {

            if ($model->load(\Yii::$app->request->post())) {
                $imageModel = new PaymentImageModel();
                $imageModel->imageFile = UploadedFile::getInstance($model, 'image');

                if (!empty($imageModel->imageFile)) {
                    $uploadedImageName = $imageModel->upload();
                    $model->image = $uploadedImageName;
                }
            }

            if ($modelTranslation->load(Yii::$app->request->post())) {
                if ($modelTranslation->validate()) {
                    $model->save();
                    $modelTranslation->payment_method_id = $model->id;
                    $modelTranslation->language_id = $languageId;

                    $modelTranslation->save();
                    return $this->redirect(['save', 'id' => $model->id, 'languageId' => $languageId]);
                }
            }

        }
        return $this->render('save', [
            'model' => $model,
            'modelTranslation' => $modelTranslation,
            'selectedLanguage' => Language::findOne($languageId)
        ]);
    }