public function validate() { if (empty($this->copy)) { $this->setError('Body copy is required'); } return parent::validate(); }
protected function beforeValidate() { if (empty($this->event_id)) { throw new \Exception('Event Id is required'); } // TODO Put this in beforeSave, to ensure that the slug is clean //$this->slug = \Web::instance()->slug( $this->slug ); return parent::beforeValidate(); }
protected function beforeUpdate() { // IMPORTANT: Variant IDs need to be preserved, SO, // if the attributes array is diff from before, Variants are no longer valid and can be recreated (IDs included) // but if the attributes array is the same, match the Variant with its pre-existing ID $prev_product = (new static())->setState('filter.id', $this->id)->getItem(); // get the attribute ids for the prev_product and $this, sorted $prev_attributes = \Dsc\ArrayHelper::getColumn($prev_product->attributes, 'id'); sort($prev_attributes); $this_attributes = \Dsc\ArrayHelper::getColumn($this->attributes, 'id'); sort($this_attributes); if ($prev_attributes != $this_attributes) { // a complete variant rebuild is fine $this->createVariants(); } elseif (count($this->attributes) == 0) { // there's only one variant, the default product, so // preserve variant IDs since the attribute set hasn't changed $edited = array(); if (!empty($this->variants) && is_array($this->variants)) { $edited = array_values($this->variants); $edited = $edited[0]; } $this->createVariants(); $variant = array_merge($prev_product->variants[0], $this->variants[0], $edited); $variant['id'] = $prev_product->variants[0]['id']; $variant['key'] = $variant['id']; if (!empty($variant['attributes']) && !is_array($variant['attributes'])) { $variant['attributes'] = json_decode($variant['attributes']); } elseif (empty($variant['attributes'])) { $variant['attributes'] = array(); } if (!empty($variant['tags']) && !is_array($variant['tags'])) { $variant['tags'] = trim($variant['tags']); if (!empty($variant['tags'])) { $variant['tags'] = array_map(function ($el) { return strtolower($el); }, \Base::instance()->split((string) $variant['tags'])); } } elseif (empty($variant['tags']) && !is_array($variant['tags'])) { $variant['tags'] = array(); } $this->variants[0] = $variant; //$this->variants[0]['id'] = $prev_product->variants[0]['id']; //$this->variants[0]['key'] = $prev_product->variants[0]['key']; } else { // preserve variant IDs since the attribute set hasn't changed $this->createVariants(); array_walk($this->variants, function (&$item, $key) use($prev_product) { // if a variant with this attribute set existed, then preserve its ID and extended properties if ($prev_variant = $prev_product->variantByKey($item['key'])) { $variant = array_merge($prev_variant, $item); $variant['id'] = $prev_variant['id']; $item = $variant; //$item['id'] = $prev_variant['id']; } if (!empty($item['attributes']) && !is_array($item['attributes'])) { $item['attributes'] = json_decode($item['attributes']); } if (!empty($item['tags']) && !is_array($item['tags'])) { $item['tags'] = trim($item['tags']); if (!empty($item['tags'])) { $item['tags'] = array_map(function ($el) { return strtolower($el); }, \Base::instance()->split((string) $item['tags'])); } } elseif (empty($item['tags']) && !is_array($item['tags'])) { $item['tags'] = array(); } }); } return parent::beforeUpdate(); }
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(); }
/** * enables trash */ protected function beforeDelete() { if (!empty($this->__enable_trash)) { \Dsc\Mongo\Collections\Trash::trash($this); } parent::beforeDelete(); }