/** * Include Content * * @param BaseElementModel $element Element * * @return League\Fractal\Resource\Item Content */ public function includeContent(BaseElementModel $element) { $content = []; if ($this->depth > \Craft\craft()->config->get('contentRecursionLimit', 'httpMessagesRestMiddleware') - 1) { return; } foreach ($element->getFieldLayout()->getFields() as $fieldLayoutField) { $field = $fieldLayoutField->getField(); $value = $element->getFieldValue($field->handle); if (get_class($field->getFieldType()) === 'Craft\\RichTextFieldType') { $value = $value->getRawContent(); } if (is_object($value) && get_class($value) === 'Craft\\ElementCriteriaModel') { $class = get_class($value->getElementType()); $element_type = $this->element_service->getElementTypeByClass($class); $manager = new Manager(); $manager->parseIncludes(array_merge(['content'], explode(',', \Craft\craft()->request->getParam('include')))); $manager->setSerializer(new ArraySerializer()); $transformer = $this->config_service->getTransformer($element_type); $value = $value->find(); $body = new Collection($value, new $transformer($this->depth + 1)); $value = $manager->createData($body)->toArray(); $value = !empty($value['data']) ? $value['data'] : null; } $content[$field->handle] = $value; } if ($content) { return $this->item($content, new ContentTransformer($this->depth), 'content'); } }
public function transform(BaseElementModel $element) { $values = array(); foreach ($element->attributeNames() as $name) { $values[$name] = $this->transformAttribute($element->getAttribute($name)); } return $values; }
/** * Save Element * * @param array $params Parameters * * @return BaseElementModel $model */ public function saveElement(BaseElementModel $element, Request $request) { $element_type = craft()->elements->getElementType($element->getElementType()); $result = $element_type->saveElement($element, null); if (!$result) { $exception = new RestfulApiException(); $exception->setStatus(400)->setMessage('Element could not be stored.'); throw $exception; } craft()->content->saveContent($element); return $element; }
/** * @return array */ protected function defineAttributes() { return array_merge(parent::defineAttributes(), ['id' => AttributeType::Number, 'number' => AttributeType::String, 'couponCode' => AttributeType::String, 'itemTotal' => [AttributeType::Number, 'decimals' => 4, 'default' => 0], 'baseDiscount' => [AttributeType::Number, 'decimals' => 4, 'default' => 0], 'baseShippingCost' => [AttributeType::Number, 'decimals' => 4, 'default' => 0], 'totalPrice' => [AttributeType::Number, 'decimals' => 4, 'default' => 0], 'totalPaid' => [AttributeType::Number, 'decimals' => 4, 'default' => 0], 'email' => AttributeType::String, 'dateOrdered' => AttributeType::DateTime, 'datePaid' => AttributeType::DateTime, 'currency' => AttributeType::String, 'lastIp' => AttributeType::String, 'message' => AttributeType::String, 'returnUrl' => AttributeType::String, 'cancelUrl' => AttributeType::String, 'orderStatusId' => AttributeType::Number, 'billingAddressId' => AttributeType::Number, 'shippingAddressId' => AttributeType::Number, 'shippingMethodId' => AttributeType::Number, 'paymentMethodId' => AttributeType::Number, 'customerId' => AttributeType::Number, 'typeId' => AttributeType::Number, 'shippingAddressData' => AttributeType::Mixed, 'billingAddressData' => AttributeType::Mixed]); }
/** * @param array $map * @param BaseElementModel|MockObject $mockElement */ private function setAttributesMockElementCriteria(array $map, BaseElementModel $mockElement) { $mockElementCriteria = $this->getMockElementCriteria(); if (array_key_exists('parent', $map)) { $mockElementCriteria->expects($this->exactly(1))->method('first')->willReturn('parent'); } if (array_key_exists('ancestors', $map)) { $mockElementCriteria->expects($this->exactly(1))->method('find')->willReturn(array('ancestor1', 'ancestor2')); } $mockElement->expects($this->any())->method('getAncestors')->willReturn($mockElementCriteria); }
protected function defineAttributes() { return array_merge(parent::defineAttributes(), ['id' => [AttributeType::Number], 'productId' => [AttributeType::Number], 'isImplicit' => [AttributeType::Bool], 'sku' => [AttributeType::String, 'required' => true], 'price' => [AttributeType::Number, 'decimals' => 4, 'required' => true], 'width' => [AttributeType::Number, 'decimals' => 4], 'height' => [AttributeType::Number, 'decimals' => 4], 'length' => [AttributeType::Number, 'decimals' => 4], 'weight' => [AttributeType::Number, 'decimals' => 4], 'stock' => [AttributeType::Number], 'unlimitedStock' => [AttributeType::Bool, 'default' => 0], 'minQty' => [AttributeType::Number], 'maxQty' => [AttributeType::Number]]); }
/** * Validate Element * * @param BaseElementModel $element Element * * @return void */ private function validateElement(BaseElementModel $element) { if (!$element->validate()) { $this->addErrors($element->getErrors()); } }
/** * Save a model into DB * * @param BaseElementModel $model * * @return bool * @throws \CDbException * @throws \Exception */ public function save(BaseElementModel $model) { $productTypeId = craft()->db->createCommand()->select('typeId')->from('market_products')->where('id=:id', [':id' => $model->productId])->queryScalar(); $productType = craft()->market_productType->getById($productTypeId); if ($model->id) { $record = Market_VariantRecord::model()->findById($model->id); if (!$record) { throw new HttpException(404); } } else { $record = new Market_VariantRecord(); } /* @var Market_VariantModel $model */ $record->isImplicit = $model->isImplicit; $record->productId = $model->productId; // We dont ask for a sku when dealing with a product with variants if ($model->isImplicit && $productType->hasVariants) { $model->sku = 'implicitSkuOfProductId' . $model->productId; $record->sku = $model->sku; } else { $record->sku = $model->sku; } if (!$productType->titleFormat) { $productType->titleFormat = "{sku}"; } // implicit variant has no custom field data so play it safe and default it to sku. if ($model->isImplicit) { $productType->titleFormat = "{sku}"; } $model->getContent()->title = craft()->templates->renderObjectTemplate($productType->titleFormat, $model); $record->price = $model->price; $record->width = $model->width; $record->height = $model->height; $record->length = $model->length; $record->weight = $model->weight; $record->minQty = $model->minQty; $record->maxQty = $model->maxQty; $record->stock = $model->stock; $record->unlimitedStock = $model->unlimitedStock; if ($model->unlimitedStock && $record->stock == "") { $model->stock = 0; $record->stock = 0; } $record->validate(); $model->addErrors($record->getErrors()); MarketDbHelper::beginStackedTransaction(); try { if (!$model->hasErrors()) { if (craft()->market_purchasable->saveElement($model)) { $record->id = $model->id; $record->save(false); MarketDbHelper::commitStackedTransaction(); return true; } } } catch (\Exception $e) { MarketDbHelper::rollbackStackedTransaction(); throw $e; } MarketDbHelper::rollbackStackedTransaction(); return false; }
/** * @return array */ protected function defineAttributes() { return array_merge(parent::defineAttributes(), ['typeId' => AttributeType::Number, 'authorId' => AttributeType::Number, 'taxCategoryId' => AttributeType::Number, 'promotable' => AttributeType::Bool, 'freeShipping' => AttributeType::Bool, 'availableOn' => AttributeType::DateTime, 'expiresOn' => AttributeType::DateTime]); }