/** * Return field class * * @param \XLite\Model\Attribute $attribute Attribute * @param string $value Value * * @return string */ protected function getFieldClass(\XLite\Model\Attribute $attribute, $value) { $class = str_replace(' ', '-', strtolower($attribute->getTypes($attribute->getType()))); if (\XLite\Model\Attribute::TYPE_CHECKBOX == $attribute->getType()) { $class .= ' ' . (static::t('Yes') == $value ? 'checked' : 'no-checked'); } return $class; }
/** * Define additional buttons * * @return array */ protected function defineAdditionalButtons() { foreach (\XLite\Model\Attribute::getTypes() as $type => $name) { $list[] = $this->getWidget(array('label' => $name, 'jsCode' => 'addAttribute(\'' . $type . '\',' . $this->getParam(static::PARAM_LIST_ID) . ')'), 'XLite\\View\\Button\\Regular'); } return $list; }
/** * Get default attributes widgets * * @return array */ protected function getDefaultWidgets() { $list = array(); foreach (\XLite\Model\Attribute::getTypes() as $type => $name) { $list[] = $this->getWidget(array(), \XLite\Model\Attribute::getWidgetClass($type)); } return $list; }
/** * Has global filtered attributes flag * * @return boolean */ protected function hasGlobalFilteredAttributes() { $cnd = new \XLite\Core\CommonCell(); $cnd->productClass = null; $cnd->product = null; $cnd->type = \XLite\Model\Attribute::getFilteredTypes(); return 0 < \XLite\Core\Database::getRepo('\\XLite\\Model\\Attribute')->search($cnd, true); }
/** * Get alll attributes * * @return array */ public function getAllAttributes() { $result = array(); foreach (Attribute::getTypes() as $type => $name) { $class = Attribute::getAttributeValueClass($type); $result = array_merge($result, Database::getRepo($class)->findAllAttributes($this)); } return $result; }
/** * Find filtered product classes * * @param \XLite\Core\CommonCell $cnd Search condition * * @return array */ public function findFilteredProductClasses(\XLite\Core\CommonCell $cnd) { $result = array(); $data = $this->createQueryBuilderCnd($cnd)->innerJoin('p.productClass', 'class')->innerJoin('class.attributes', 'attr')->andWhere('attr.type IN (\'' . implode('\',\'', \XLite\Model\Attribute::getFilteredTypes()) . '\')')->andWhere('p.productClass is not null')->addSelect('COUNT(DISTINCT p.product_id)')->GroupBy('p.productClass')->getSingleResult(); if ($data && $data[1] == $this->search($cnd, true)) { $result[] = $data[0]->getProductClass(); } return $result; }
/** * Assign attribute condition * * @param \XLite\Model\Attribute $attribute Attribute * @param mixed $value Value * * @return void */ public function assignAttributeCondition(\XLite\Model\Attribute $attribute, $value) { $result = null; $alias = 'av' . $attribute->getId(); $getConditionFunc = 'getCondition' . $attribute->getTypes($attribute->getType(), true); $where = $this->{$getConditionFunc}($attribute, $value, $alias); if ($where) { if (is_array($where)) { foreach ($where as $w) { $this->andWhere($w); } } else { $this->andWhere($where); } $attr = 'attribute' . $attribute->getId(); $this->leftJoin('p.attributeValue' . $attribute->getType(), $alias, 'WITH', $alias . '.attribute = :' . $attr); $this->setParameter($attr, $attribute); if ($attribute::TYPE_SELECT == $attribute->getType()) { $this->leftJoin($alias . '.attribute_option', $alias . 'o'); } } }
/** * Return unique field name * * @param \XLite\Model\Attribute $attribute Attribute * * @return string */ protected function getUniqueFieldName(\XLite\Model\Attribute $attribute) { $result = $attribute->getName() . ' (field:'; $cnd = new \XLite\Core\CommonCell(); $cnd->name = $attribute->getName(); if ($attribute->getProduct()) { $result .= 'product'; } elseif ($attribute->getProductClass()) { $result .= 'class'; } else { $result .= 'global'; } if ($attribute->getAttributeGroup()) { $result .= ' >>> ' . $attribute->getAttributeGroup()->getName(); } $result .= ')'; return $result; }
/** * Get product modifier types * * @return array */ protected function getProductModifierTypes() { if (!isset($this->productModifierTypes)) { foreach (\XLite\Model\Attribute::getTypes() as $type => $name) { $class = \XLite\Model\Attribute::getAttributeValueClass($type); if (is_subclass_of($class, 'XLite\\Model\\AttributeValue\\Multiple')) { $modifierTypes = \XLite\Core\Database::getRepo($class)->getModifierTypesByProduct($this->getProduct()); foreach ($modifierTypes as $k => $v) { if (!isset($this->productModifierTypes[$k])) { $this->productModifierTypes[$k] = $v; } else { $this->productModifierTypes[$k] = $this->productModifierTypes[$k] || $v; } } } } } return $this->productModifierTypes; }
/** * Apply attribute values changes * * @param array $oldValues Old values * @param array $newValues New values * * @return void */ protected function applyAttributeValuesChanges(array $oldValues, array $newValues) { $diff = array(); foreach (\XLite\Model\Attribute::getTypes() as $type => $name) { $class = \XLite\Model\Attribute::getAttributeValueClass($type); $diff += $class::getDiff($oldValues[$type], $newValues[$type]); } if ($diff) { $i = 0; do { $processed = 0; foreach (\XLite\Core\Database::getRepo('XLite\\Model\\Product')->findFrame($i, static::CHUNK_LENGTH) as $product) { foreach ($diff as $attributeId => $changes) { $attribute = \XLite\Core\Database::getRepo('XLite\\Model\\Attribute')->find($attributeId); $attribute->applyChanges($product, $changes); } $processed++; } if (0 < $processed) { \XLite\Core\Database::getEM()->flush(); \XLite\Core\Database::getEM()->clear(); } $i += $processed; } while (0 < $processed); } }
/** * Return attribute options by category * * @param \XLite\Model\Attribute $attribute Attribute * * @return array */ public function getAttributeOptionsByAttribute(\XLite\Model\Attribute $attribute) { $config = \XLite\Core\Config::getInstance()->XC->ProductFilter; if ($config->attributes_filter_by_category) { $list = null; $languageCode = $this->getTranslationCode(); if ($config->attributes_filter_cache_mode) { $data = $this->getAttributeOptionsFromCache(); if (empty($data) || !is_array($data)) { $data = array($languageCode => array()); } elseif (!isset($data[$languageCode])) { $data[$languageCode] = array(); } if (isset($data[$languageCode][$attribute->getId()])) { $list = $data[$languageCode][$attribute->getId()]; } } if (null === $list) { $list = array(); $options = \XLite\Core\Database::getRepo('XLite\\Model\\AttributeValue\\AttributeValueSelect')->createQueryBuilder('av')->linkInner('av.product')->linkInner('product.categoryProducts')->andWhere('categoryProducts.category = :category AND av.attribute = :attribute')->setParameter('category', $this)->setParameter('attribute', $attribute)->groupBy('av.attribute_option')->getResult(); foreach ($options as $option) { $list[$option->getAttributeOption()->getId()] = $option->getAttributeOption()->getName(); } if ($config->attributes_filter_cache_mode) { $data[$languageCode][$attribute->getId()] = $list; $this->saveAttributeOptionsInCache($data); } } } else { $list = array(); foreach ($attribute->getAttributeOptions() as $option) { $list[$option->getId()] = $option->getName(); } } return $list; }
/** * Get multiple attributes * * @return array */ public function getMultipleAttributes() { $result = array(); foreach (\XLite\Model\Attribute::getTypes() as $type => $name) { $class = \XLite\Model\Attribute::getAttributeValueClass($type); if (is_subclass_of($class, 'XLite\\Model\\AttributeValue\\Multiple')) { $result = array_merge($result, \XLite\Core\Database::getRepo($class)->findMultipleAttributes($this)); } } if ($result) { foreach ($result as $k => $v) { $result[$k] = $v[0]; } } return $result; }
/** * Prepare attribute element * * @param \XLite\Model\Attribute $attribute Attribute * @param array $filterValues Filter values defined in prepareAttributesList() * * @return array */ protected function prepareAttributeElement(\XLite\Model\Attribute $attribute, array $filterValues) { $result = null; if ($attribute::TYPE_SELECT == $attribute->getType()) { $category = \XLite\Core\Database::getRepo('XLite\\Model\\Category')->find($this->getCategoryId()); if ($category) { $options = $category->getAttributeOptionsByAttribute($attribute); } } if ($attribute::TYPE_SELECT != $attribute->getType() || 0 < count($options)) { $params = array('fieldName' => 'filter[attribute][' . $attribute->getId() . ']', 'label' => $attribute->getName(), 'attribute' => $attribute, 'useColon' => false, 'value' => isset($filterValues[$attribute->getId()]) ? $filterValues[$attribute->getId()] : ''); $class = 'type-' . strtolower($attribute->getType()); if ($attribute::TYPE_CHECKBOX == $attribute->getType() && $params['value']) { $class .= ' checked'; } elseif ($attribute::TYPE_SELECT == $attribute->getType()) { $params['options'] = $options; } $result = array('class' => $class, 'widjet' => $this->getWidget($params, $attribute->getFilterWidgetClass())); } return $result; }
/** * {@inheritDoc} */ public function prepareEntityBeforeCommit($type) { $this->__initializer__ && $this->__initializer__->__invoke($this, 'prepareEntityBeforeCommit', array($type)); return parent::prepareEntityBeforeCommit($type); }
/** * Get attribute value * * @param \XLite\Model\Attribute $attribute Attribute * * @return mixed */ public function getAttributeValue(\XLite\Model\Attribute $attribute) { $result = null; foreach ($this->getValues() as $v) { if ($v->getAttribute()->getId() == $attribute->getId()) { $result = $v; break; } } return $result; }
/** * Attribute is checked flag * * @param \XLite\Model\Attribute $attribute Attribute * * @return boolean */ protected function isChecked(\XLite\Model\Attribute $attribute) { return in_array($attribute->getId(), $this->getVariantsAttributeIds()); }
/** * Import 'group' value * * @param \XLite\Model\Attribute $model Attribute * @param string $value Value * @param array $column Column info * * @return void */ protected function importGroupColumn(\XLite\Model\Attribute $model, $value, array $column) { if ($value) { $group = $this->normalizeGroupValue($value); $this->updateModelTranslations($group, $value); $group->setProductClass($model->getProductClass()); $model->setAttributeGroup($group); } }
/** * Get default options * * @return array */ protected function getDefaultOptions() { return \XLite\Model\Attribute::getTypes(); }
/** * Defines the position of attribute in the current product * * @param \XLite\Model\Attribute $attribute * * @return integer */ protected function getPositionColumnValue(\XLite\Model\Attribute $attribute) { return $attribute->getPosition($this->getProduct()); }