protected function beforeSave() { if (!empty($this->images)) { $images = array(); $current = $this->images; $this->images = array(); foreach ($current as $image) { if (!empty($image['image'])) { $images[] = array('image' => $image['image']); } } $this->images = $images; } $this->ordering = (int) $this->ordering; if (!empty($this->positions) && !is_array($this->positions)) { $this->positions = trim($this->positions); if (!empty($this->positions)) { $this->positions = array_map(function ($el) { return strtolower($el); }, \Base::instance()->split((string) $this->positions)); } } elseif (empty($this->positions) && !is_array($this->positions)) { $this->positions = array(); } if (!empty($this->{'assignment.routes.list'}) && !is_array($this->{'assignment.routes.list'})) { $this->{'assignment.routes.list'} = trim($this->{'assignment.routes.list'}); if (!empty($this->{'assignment.routes.list'})) { $this->{'assignment.routes.list'} = \Base::instance()->split((string) $this->{'assignment.routes.list'}); } } elseif (empty($this->{'assignment.routes.list'}) && !is_array($this->{'assignment.routes.list'})) { $this->{'assignment.routes.list'} = array(); } if (!empty($this->{'assignment.referers.list'}) && !is_array($this->{'assignment.referers.list'})) { $this->{'assignment.referers.list'} = trim($this->{'assignment.referers.list'}); if (!empty($this->{'assignment.referers.list'})) { $this->{'assignment.referers.list'} = \Base::instance()->split((string) $this->{'assignment.referers.list'}); } } elseif (empty($this->{'assignment.referers.list'}) && !is_array($this->{'assignment.referers.list'})) { $this->{'assignment.referers.list'} = array(); } if (!empty($this->{'assignment.referers.others'}) && !is_array($this->{'assignment.referers.others'})) { $this->{'assignment.referers.others'} = trim($this->{'assignment.referers.others'}); if (!empty($this->{'assignment.referers.others'})) { $this->{'assignment.referers.others'} = \Base::instance()->split((string) $this->{'assignment.referers.others'}); } } elseif (empty($this->{'assignment.referers.others'}) && !is_array($this->{'assignment.referers.others'})) { $this->{'assignment.referers.others'} = array(); } return parent::beforeSave(); }
protected function beforeSave() { if (!empty($this->{'display.stickers'}) && !is_array($this->{'display.stickers'})) { $this->{'display.stickers'} = trim($this->{'display.stickers'}); if (!empty($this->{'display.stickers'})) { $this->{'display.stickers'} = \Base::instance()->split((string) $this->{'display.stickers'}); } } elseif (empty($this->{'display.stickers'}) && !is_array($this->{'display.stickers'})) { $this->{'display.stickers'} = array(); } $this->set('shipping.enabled', (bool) $this->get('shipping.enabled')); $this->set('taxes.enabled', (bool) $this->get('taxes.enabled')); $this->set('prices.default', (double) $this->get('prices.default')); $this->set('prices.list', (double) $this->get('prices.list')); if ($this->get('prices.wholesale')) { $this->set('prices.wholesale', (double) $this->get('prices.wholesale')); } $this->set('policies.track_inventory', (bool) $this->get('policies.track_inventory')); $this->attributes_count = count($this->attributes); $this->variants_count = count($this->variants); $this->inventory_count = 0; array_walk($this->variants, function (&$item, $key) { $item['quantity'] = (int) $item['quantity']; $item['price'] = (double) $item['price']; if (empty($item['quantity']) && !empty($this->{'policies.track_inventory'})) { $item['enabled'] = 0; } if (!empty($item['quantity'])) { $this->inventory_count += (int) $item['quantity']; } }); if (!empty($this->{'prices.special'}) && is_array($this->{'prices.special'})) { // Compress the array to just the values, then sort them by sort order $special_prices = array_filter(array_values($this->{'prices.special'})); usort($special_prices, function ($a, $b) { return $a['ordering'] - $b['ordering']; }); array_walk($special_prices, function (&$item, $key) { if ($item['ordering'] != $key + 1) { $item['ordering'] = $key + 1; } if (!empty($item['start_date'])) { $string = $item['start_date']; if (!empty($item['start_time'])) { $string .= ' ' . $item['start_time']; } $item['start'] = \Dsc\Mongo\Metastamp::getDate(trim($string)); } else { $item['start'] = \Dsc\Mongo\Metastamp::getDate('now'); } if (empty($item['end_date'])) { unset($item['end']); } elseif (!empty($item['end_date'])) { $string = $item['end_date']; if (!empty($item['end_time'])) { $string .= ' ' . $item['end_time']; } $item['end'] = \Dsc\Mongo\Metastamp::getDate(trim($string)); } }); $this->{'prices.special'} = $special_prices; } if (!$this->variantsInStock()) { $this->{'publication.status'} = 'unpublished'; } return parent::beforeSave(); }