/** * Generate a list of tiers for a wizard in products * @param object * @param string * @return string */ public function generateWizardList($objRecords, $strId) { $strReturn = ' <table class="tl_listing showColumns"> <thead> <td class="tl_folder_tlist">' . Format::dcaLabel('tl_iso_product_price', 'price_tiers') . '</td> <td class="tl_folder_tlist">' . Format::dcaLabel('tl_iso_product_price', 'tax_class') . '</td> <td class="tl_folder_tlist">' . Format::dcaLabel('tl_iso_product_price', 'config_id') . '</td> <td class="tl_folder_tlist">' . Format::dcaLabel('tl_iso_product_price', 'member_group') . '</td> <td class="tl_folder_tlist">' . Format::dcaLabel('tl_iso_product_price', 'start') . '</td> <td class="tl_folder_tlist">' . Format::dcaLabel('tl_iso_product_price', 'stop') . '</td> </thead> <tbody>'; while ($objRecords->next()) { $arrTiers = array(); $objTiers = \Database::getInstance()->execute("SELECT * FROM tl_iso_product_pricetier WHERE pid={$objRecords->id} ORDER BY min"); while ($objTiers->next()) { $arrTiers[] = "{$objTiers->min}={$objTiers->price}"; } $strReturn .= ' <tr> <td class="tl_file_list">' . implode(', ', $arrTiers) . '</td> <td class="tl_file_list">' . (Format::dcaValue('tl_iso_product_price', 'tax_class', $objRecords->tax_class) ?: '-') . '</td> <td class="tl_file_list">' . (Format::dcaValue('tl_iso_product_price', 'config_id', $objRecords->config_id) ?: '-') . '</td> <td class="tl_file_list">' . (Format::dcaValue('tl_iso_product_price', 'member_group', $objRecords->member_group) ?: '-') . '</td> <td class="tl_file_list">' . (Format::dcaValue('tl_iso_product_price', 'member_group', $objRecords->start) ?: '-') . '</td> <td class="tl_file_list">' . (Format::dcaValue('tl_iso_product_price', 'member_group', $objRecords->stop) ?: '-') . '</td> </tr> '; } $strReturn .= ' </tbody> </table>'; return $strReturn; }
/** * Generate a product label and return it as HTML string * @param array * @param string * @param object * @param array * @return string */ public function generate($row, $label, $dc, $args) { foreach ($GLOBALS['TL_DCA'][$dc->table]['list']['label']['fields'] as $i => $field) { if ($field == 'name' && $row['fallback']) { $args[$i] = sprintf('%s <span style="color:#b3b3b3; padding-left:3px;">[%s]</span>', $row['name'], Format::dcaLabel($dc->table, 'fallback')); } else { $args[$i] = Format::dcaValue($dc->table, $field, $row[$field]); } } return $args; }
/** * Generate a product label and return it as HTML string * @param array * @param string * @param object * @param array * @return string */ public function generate($row, $label, $dc, $args) { $objProduct = Product::findByPk($row['id']); foreach ($GLOBALS['TL_DCA'][$dc->table]['list']['label']['fields'] as $i => $field) { switch ($field) { // Add an image case 'images': $arrImages = deserialize($objProduct->images); $args[$i] = ' '; if (is_array($arrImages) && !empty($arrImages)) { foreach ($arrImages as $image) { $strImage = 'isotope/' . strtolower(substr($image['src'], 0, 1)) . '/' . $image['src']; if (!is_file(TL_ROOT . '/' . $strImage)) { continue; } $size = @getimagesize(TL_ROOT . '/' . $strImage); $args[$i] = sprintf('<a href="%s" onclick="Backend.openModalImage({\'width\':%s,\'title\':\'%s\',\'url\':\'%s\'});return false"><img src="%s" alt="%s" align="left"></a>', $strImage, $size[0], str_replace("'", "\\'", $objProduct->name), $strImage, \Image::get($strImage, 50, 50, 'proportional'), $image['alt']); break; } } break; case 'name': $args[$i] = $objProduct->name; /** @var \Isotope\Model\ProductType $objProductType */ if ($row['pid'] == 0 && ($objProductType = ProductType::findByPk($row['type'])) !== null && $objProductType->hasVariants()) { // Add a variants link $args[$i] = sprintf('<a href="%s" title="%s">%s</a>', ampersand(\Environment::get('request')) . '&id=' . $row['id'], specialchars($GLOBALS['TL_LANG'][$dc->table]['showVariants']), $args[$i]); } break; case 'price': $objPrice = ProductPrice::findPrimaryByProductId($row['id']); if (null !== $objPrice) { /** @var \Isotope\Model\TaxClass $objTax */ $objTax = $objPrice->getRelated('tax_class'); $strTax = null === $objTax ? '' : ' (' . $objTax->getName() . ')'; $args[$i] = $objPrice->getValueForTier(1) . $strTax; } break; case 'variantFields': $attributes = array(); foreach ($GLOBALS['TL_DCA'][$dc->table]['list']['label']['variantFields'] as $variantField) { $attributes[] = '<strong>' . Format::dcaLabel($dc->table, $variantField) . ':</strong> ' . Format::dcaValue($dc->table, $variantField, $objProduct->{$variantField}); } $args[$i] = ($args[$i] ? $args[$i] . '<br>' : '') . implode(', ', $attributes); break; } } return $args; }
/** * Auto-format model data based on DCA config * @param \Model * @param callable * @return \ArrayObject */ public static function generate(\Model $objModel = null, $varCallable = null) { if (null === $objModel) { return new \ArrayObject(array(), \ArrayObject::ARRAY_AS_PROPS); } $strTable = $objModel->getTable(); $objDca = new \DcaExtractor($strTable); $arrRelations = $objDca->getRelations(); $arrData = array(); \System::loadLanguageFile($strTable); \Controller::loadDataContainer($strTable); $arrFields =& $GLOBALS['TL_DCA'][$strTable]['fields']; foreach ($objModel->row() as $strField => $varValue) { $arrAdditional = array(); $strLabel = Format::dcaLabel($strTable, $strField); if (isset($arrRelations[$strField])) { $objRelated = $objModel->getRelated($strField); if ($objRelated == null) { $arrData[$strField] = new Plain('', $strLabel, $arrAdditional); } elseif ($objRelated instanceof \Model\Collection) { $arrCollection = array(); foreach ($objRelated as $objRelatedModel) { $arrCollection[] = new Relation($objRelatedModel, '', array(), $varCallable); } $arrData[$strField] = new Collection($arrCollection, $strLabel); } else { $arrData[$strField] = new Relation($objRelated, $strLabel, array(), $varCallable); } continue; } $arrAdditional['formatted'] = Format::dcaValue($strTable, $strField, $varValue); if (in_array($arrFields[$strField]['eval']['rgxp'], array('date', 'datim', 'time'))) { $arrData[$strField] = new Timestamp($varValue, $strLabel, $arrAdditional); } else { $arrData[$strField] = new Plain($varValue, $strLabel, $arrAdditional); } } if (null !== $varCallable) { call_user_func_array($varCallable, array($objModel, &$arrData)); } return new \ArrayObject($arrData, \ArrayObject::ARRAY_AS_PROPS); }
/** * Generate header fields for product or variant * @param array * @param \Contao\DataContainer */ public function headerFields($arrFields, $dc) { $t = Product::getTable(); $arrNew = array(); $objProduct = Product::findByPk($dc->id); if (null === $objProduct) { return $arrFields; } $arrAttributes = array('name', 'alias', 'sku'); if ($objProduct->isVariant()) { $arrAttributes = array_merge($arrAttributes, array_intersect(array_merge($objProduct->getAttributes(), $objProduct->getVariantAttributes()), Attribute::getVariantOptionFields())); } foreach ($arrAttributes as $field) { $v = $objProduct->{$field}; if ($v != '') { $arrNew[Format::dcaLabel($t, $field)] = Format::dcaValue($t, $field, $v); } } // Add fields that have potentially been added through the DCA, but do not allow to override the core fields return array_merge($arrNew, array_diff_key($arrFields, $arrNew)); }
/** * Replace {{dca_label::*}} insert tag * * use case: * * {{dca_label::table::field}} * * @param array $arrTag * * @return string */ private function replaceDcaLabel($arrTag) { $strTable = $arrTag[1]; $strField = $arrTag[2]; return Format::dcaLabel($strTable, $strField); }
/** * Get attributes that can be filtered * * @param DataContainer * @return array */ public function getAttributeNames($dc) { $arrAttributes = array(); foreach ($GLOBALS['TL_DCA']['tl_iso_product']['fields'] as $attribute => $config) { if ($config['attributes']['legend'] != '' && $attribute != 'pages' && $config['inputType'] != 'mediaManager') { $arrAttributes[$attribute] = Format::dcaLabel('tl_iso_product', $attribute); } } asort($arrAttributes); return $arrAttributes; }
/** * Format product configuration using \Haste\Data * * @param array $arrConfig * @param IsotopeProduct|Product $objProduct * * @return array */ public static function formatProductConfiguration(array $arrConfig, IsotopeProduct $objProduct) { Product::setActive($objProduct); $strTable = $objProduct->getTable(); foreach ($arrConfig as $k => $v) { /** @type \Isotope\Model\Attribute $objAttribute */ if (($objAttribute = $GLOBALS['TL_DCA'][$strTable]['attributes'][$k]) !== null && $objAttribute instanceof IsotopeAttributeWithOptions) { /** @type \Widget $strClass */ $strClass = $objAttribute->getFrontendWidget(); $arrField = $strClass::getAttributesFromDca($GLOBALS['TL_DCA'][$strTable]['fields'][$k], $k, $v, $k, $strTable, $objProduct); $arrOptions = array(); $values = $v; if (!empty($arrField['options']) && is_array($arrField['options'])) { if (!is_array($values)) { $values = array($values); } $arrOptions = array_filter($arrField['options'], function (&$option) use(&$values) { if (($pos = array_search($option['value'], $values)) !== false) { $option = $option['label']; unset($values[$pos]); return true; } return false; }); if (!empty($values)) { $arrOptions = array_merge($arrOptions, $values); } } $formatted = implode(', ', $arrOptions); } else { $formatted = Format::dcaValue($strTable, $k, $v); } $arrConfig[$k] = new Plain($v, Format::dcaLabel($strTable, $k), array('formatted' => Haste::getInstance()->call('replaceInsertTags', array($formatted)))); } Product::unsetActive(); return $arrConfig; }
/** * Build palette for the current product type/variant */ public function buildPaletteString() { $this->loadDataContainer(Attribute::getTable()); if (\Input::get('act') == '' && \Input::get('key') == '' || \Input::get('act') == 'select') { return; } $arrTypes = array(); $arrFields =& $GLOBALS['TL_DCA']['tl_iso_product']['fields']; /** @var IsotopeAttribute[] $arrAttributes */ $arrAttributes =& $GLOBALS['TL_DCA']['tl_iso_product']['attributes']; $blnVariants = false; $act = \Input::get('act'); $blnSingleRecord = $act === 'edit' || $act === 'show'; if (\Input::get('id') > 0) { /** @type object $objProduct */ $objProduct = \Database::getInstance()->prepare("SELECT p1.pid, p1.type, p2.type AS parent_type FROM tl_iso_product p1 LEFT JOIN tl_iso_product p2 ON p1.pid=p2.id WHERE p1.id=?")->execute(\Input::get('id')); if ($objProduct->numRows) { $objType = ProductType::findByPk($objProduct->pid > 0 ? $objProduct->parent_type : $objProduct->type); $arrTypes = null === $objType ? array() : array($objType); if ($objProduct->pid > 0 || $act != 'edit' && $act != 'copyFallback' && $act != 'show') { $blnVariants = true; } } } else { $arrTypes = ProductType::findAllUsed() ?: array(); } /** @var \Isotope\Model\ProductType $objType */ foreach ($arrTypes as $objType) { // Enable advanced prices if ($blnSingleRecord && $objType->hasAdvancedPrices()) { $arrFields['prices']['exclude'] = $arrFields['price']['exclude']; $arrFields['prices']['attributes'] = $arrFields['price']['attributes']; $arrFields['price'] = $arrFields['prices']; } else { $GLOBALS['TL_DCA']['tl_iso_product']['config']['onversion_callback'][] = array('Isotope\\Backend\\Product\\Price', 'createVersion'); $GLOBALS['TL_DCA']['tl_iso_product']['config']['onrestore_callback'][] = array('Isotope\\Backend\\Product\\Price', 'restoreVersion'); } $arrInherit = array(); $arrPalette = array(); $arrLegends = array(); $arrLegendOrder = array(); $arrCanInherit = array(); if ($blnVariants) { $arrConfig = $objType->variant_attributes; $arrEnabled = $objType->getVariantAttributes(); $arrCanInherit = $objType->getAttributes(); } else { $arrConfig = $objType->attributes; $arrEnabled = $objType->getAttributes(); } // Go through each enabled field and build palette foreach ($arrFields as $name => $arrField) { if (in_array($name, $arrEnabled)) { if ($arrField['inputType'] == '') { continue; } // Variant fields can only be edited in variant mode if (null !== $arrAttributes[$name] && !$blnVariants && $arrAttributes[$name]->isVariantOption()) { continue; } // Field cannot be edited in variant if ($blnVariants && $arrAttributes[$name]->inherit) { continue; } $arrLegendOrder[$arrConfig[$name]['position']] = $arrConfig[$name]['legend']; $arrPalette[$arrConfig[$name]['legend']][$arrConfig[$name]['position']] = $name; // Apply product type attribute config if ($arrConfig[$name]['tl_class'] != '') { $arrFields[$name]['eval']['tl_class'] = $arrConfig[$name]['tl_class']; } if ($arrConfig[$name]['mandatory'] > 0) { $arrFields[$name]['eval']['mandatory'] = $arrConfig[$name]['mandatory'] == 1 ? false : true; } if ($blnVariants && in_array($name, $arrCanInherit) && null !== $arrAttributes[$name] && !$arrAttributes[$name]->isVariantOption() && !in_array($name, array('price', 'published', 'start', 'stop'))) { $arrInherit[$name] = Format::dcaLabel('tl_iso_product', $name); } } else { // Hide field from "show" option if (!isset($arrField['attributes']) || $arrField['inputType'] != '' && $name != 'inherit') { $arrFields[$name]['eval']['doNotShow'] = true; } } } ksort($arrLegendOrder); $arrLegendOrder = array_unique($arrLegendOrder); // Build foreach ($arrLegendOrder as $legend) { $fields = $arrPalette[$legend]; ksort($fields); $arrLegends[] = '{' . $legend . '},' . implode(',', $fields); } // Set inherit options $arrFields['inherit']['options'] = $arrInherit; // Add palettes $GLOBALS['TL_DCA']['tl_iso_product']['palettes'][$blnVariants ? 'default' : $objType->id] = ($blnVariants ? 'inherit,' : '') . implode(';', $arrLegends); } // Remove non-active fields from multi-selection if ($blnVariants && !$blnSingleRecord) { $arrInclude = empty($arrPalette) ? array() : call_user_func_array('array_merge', $arrPalette); foreach ($arrFields as $name => $config) { if ($arrFields[$name]['attributes']['legend'] != '' && !in_array($name, $arrInclude)) { $arrFields[$name]['exclude'] = true; } } } }
/** * Get header fields * * @return array */ public function getHeaderFields() { $arrHeaderFields = $this->headerFields; if (!is_array($arrHeaderFields) || empty($arrHeaderFields)) { foreach ($this->fields as $field) { if ($field == 'id') { $arrHeaderFields[] = 'ID'; continue; } $arrHeaderFields[] = Format::dcaLabel($this->foreignTable, $field); } } return $arrHeaderFields; }
/** * Generate address details amd return it as string * @param Address * @return string */ protected function generateAddressData(Address $objAddress = null) { if (null === $objAddress) { return '<div class="tl_gerror">No address data available.</div>'; } \System::loadLanguageFile($objAddress->getTable()); $this->loadDataContainer($objAddress->getTable()); $strBuffer = ' <div> <table cellpadding="0" cellspacing="0" class="tl_show" summary="Table lists all details of an entry" style="width:650px"> <tbody>'; $i = 0; foreach ($GLOBALS['TL_DCA'][$objAddress->getTable()]['fields'] as $k => $v) { if (!isset($objAddress->{$k})) { continue; } $v = $objAddress->{$k}; $strClass = ++$i % 2 ? '' : ' class="tl_bg"'; $strBuffer .= ' <tr> <td' . $strClass . ' style="vertical-align:top"><span class="tl_label">' . Format::dcaLabel($objAddress->getTable(), $k) . ': </span></td> <td' . $strClass . '>' . Format::dcaValue($objAddress->getTable(), $k, $v) . '</td> </tr>'; } $strBuffer .= ' </tbody></table> </div>'; return $strBuffer; }
/** * Format options label and value * * @param array $arrData * @param string $strTable * @param bool $blnSkipEmpty * * @return array */ public static function formatOptions(array $arrData, $strTable = 'tl_iso_product', $blnSkipEmpty = true) { $arrOptions = array(); foreach ($arrData as $field => $value) { if ($blnSkipEmpty && ($value == '' || $value == '-')) { continue; } $arrOptions[$field] = array('label' => Format::dcaLabel($strTable, $field), 'value' => Haste::getInstance()->call('replaceInsertTags', Format::dcaValue($strTable, $field, $value))); } return $arrOptions; }
/** * Generate a sorting form */ protected function generateSorting() { $this->Template->hasSorting = false; if (!empty($this->iso_sortingFields)) { $arrOptions = array(); // Cache new request value // @todo should support multiple sorting fields list($sortingField, $sortingDirection) = explode(':', \Input::post('sorting')); if ($this->blnUpdateCache && in_array($sortingField, $this->iso_sortingFields)) { Isotope::getRequestCache()->setSortingForModule($sortingField, $sortingDirection == 'DESC' ? Sort::descending() : Sort::ascending(), $this->id); } elseif (array_diff(array_keys(Isotope::getRequestCache()->getSortingsForModules(array($this->id))), $this->iso_sortingFields)) { // Request cache contains wrong value, delete it! $this->blnUpdateCache = true; Isotope::getRequestCache()->unsetSortingsForModule($this->id); RequestCache::deleteById(\Input::get('isorc')); } elseif (!$this->blnUpdateCache) { // No need to generate options if we reload anyway $first = Isotope::getRequestCache()->getFirstSortingFieldForModule($this->id); foreach ($this->iso_sortingFields as $field) { list($asc, $desc) = $this->getSortingLabels($field); $objSorting = $first == $field ? Isotope::getRequestCache()->getSortingForModule($field, $this->id) : null; $arrOptions[] = array('label' => Format::dcaLabel('tl_iso_product', $field) . ', ' . $asc, 'value' => $field . ':ASC', 'default' => null !== $objSorting && $objSorting->isAscending() ? '1' : ''); $arrOptions[] = array('label' => Format::dcaLabel('tl_iso_product', $field) . ', ' . $desc, 'value' => $field . ':DESC', 'default' => null !== $objSorting && $objSorting->isDescending() ? '1' : ''); } } $this->Template->hasSorting = true; $this->Template->sortingLabel = $GLOBALS['TL_LANG']['MSC']['orderByLabel']; $this->Template->sortingOptions = $arrOptions; } }
/** * Get formatted column labels * @return array */ protected function getColumnLabels() { $arrLabels = array(); $count = 0; foreach ($this->arrListFields as $strField) { // Use a custom label if (count($this->customLabels) > 0) { $label = $this->customLabels[$count++]; } else { // Get the label from DCA list($strTable, $strColumn) = explode('.', $strField); $label = \Haste\Util\Format::dcaLabel($strTable, $strColumn); } $arrLabels[standardize($strField)]['label'] = $label; } \Haste\Generator\RowClass::withKey('rowClass')->addCustom('row')->addCount('row_')->addFirstLast('row_')->addEvenOdd('row_')->applyTo($arrLabels); return $arrLabels; }