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 beforeValidate() { $result = parent::beforeValidate(); // add username for blog posts so we dont have to look up their usernames all the time if (!$this->get('author')) { $this->{'author.id'} = $this->{'metadata.creator.id'}; $this->{'author.name'} = $this->{'metadata.creator.name'}; $user = (new \Users\Models\Users())->populateState()->setState('filter.id', $this->{'metadata.creator.id'})->getItem(); if (empty($user)) { $act_user = \Dsc\System::instance()->get('auth')->getIdentity(); $this->{'author.username'} = $act_user->username; $this->{'author.name'} = $act_user->fullName(); $this->{'author.id'} = $act_user->id; } else { $this->{'author.username'} = $user->{'username'}; } } else { if ($this->get('author.id')) { $user = (new \Users\Models\Users())->populateState()->setState('filter.id', $this->get('author.id'))->getItem(); if (!empty($user->id)) { $this->{'author.name'} = $user->fullName(); $this->{'author.username'} = $user->username; } } else { $this->setError("Author is required"); } } if (!empty($this->category_ids)) { $category_ids = $this->category_ids; unset($this->category_ids); $categories = array(); if ($list = (new \Blog\Models\Categories())->setState('select.fields', array('title', 'slug'))->setState('filter.ids', $category_ids)->getList()) { foreach ($list as $list_item) { $cat = array('id' => $list_item->id, 'title' => $list_item->title, 'slug' => $list_item->slug); $categories[] = $cat; } } $this->categories = $categories; } unset($this->parent); unset($this->new_category_title); return $this->checkErrors(); }
protected function beforeValidate() { if (!empty($this->category_ids)) { $category_ids = $this->category_ids; unset($this->category_ids); $categories = array(); if ($list = (new \Pages\Models\Categories())->setState('select.fields', array('title', 'slug'))->setState('filter.ids', $category_ids)->getList()) { foreach ($list as $list_item) { $cat = array('id' => $list_item->id, 'title' => $list_item->title, 'slug' => $list_item->slug); $categories[] = $cat; } } $this->categories = $categories; } 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; } unset($this->parent); unset($this->new_category_title); $this->links = array_values(array_filter($this->links)); if ($this->links) { foreach ($this->links as $key => $link) { if (empty($link['title']) && empty($link['url'])) { unset($this->links[$key]); } } $this->links = array_values(array_filter($this->links)); } return parent::beforeValidate(); }
protected function beforeValidate() { 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; } if (!empty($this->{'manufacturer.id'})) { $manufacturer = array(); if ($item = (new \Shop\Models\Manufacturers())->setState('filter.id', $this->{'manufacturer.id'})->getItem()) { $manufacturer = array('id' => $item->id, 'title' => $item->title, 'slug' => $item->slug); } $this->manufacturer = $manufacturer; } else { $this->manufacturer = array(); } if (!empty($this->category_ids)) { $category_ids = array_filter($this->category_ids); unset($this->category_ids); $categories = array(); if (empty($category_ids)) { $this->categories = $categories; } elseif ($list = (new \Shop\Models\Categories())->setState('select.fields', array('title', 'slug'))->setState('filter.ids', $category_ids)->getList()) { foreach ($list as $list_item) { $cat = array('id' => $list_item->id, 'title' => $list_item->title, 'slug' => $list_item->slug); $categories[] = $cat; } } $this->categories = $categories; } if (!empty($this->attributes) && is_array($this->attributes)) { // Compress the attributes array to just the values, then sort them by sort order $this->attributes = array_filter(array_values($this->attributes)); usort($this->attributes, function ($a, $b) { return $a['ordering'] - $b['ordering']; }); array_walk($this->attributes, function (&$item, $key) { if (empty($item['id'])) { $item['id'] = (string) new \MongoId(); } if ($item['ordering'] != $key + 1) { $item['ordering'] = $key + 1; } // then Loop through each attribute and do the same for each attribute's options $item['options'] = array_values($item['options']); usort($item['options'], function ($a, $b) { return $a['ordering'] - $b['ordering']; }); array_walk($item['options'], function (&$item, $key) { if (empty($item['id'])) { $item['id'] = (string) new \MongoId(); } if ($item['ordering'] != $key + 1) { $item['ordering'] = $key + 1; } }); }); } // related_products could be a CSV of MongoIds if (empty($this->related_products)) { if (!is_array($this->related_products)) { $this->related_products = array(); } } else { if (!is_array($this->related_products)) { $this->related_products = trim($this->related_products); if (!empty($this->related_products)) { $this->related_products = \Base::instance()->split((string) $this->related_products); } else { $this->related_products = array(); } } } foreach ($this->related_products as $key => $product_id) { // don't allow self-relations. it will make you go blind. :-) if ((string) $product_id == (string) $this->id) { unset($this->related_products[$key]); } else { $this->related_products[$key] = new \MongoId((string) $product_id); } } $this->related_products = array_values($this->related_products); sort($this->related_products); // whether related_products is empty or not, we have to compare it to its previous state // and make updates if they aren't the same $old_products = array(); if (!empty($this->id)) { $old_product = (new static())->load(array('_id' => new \MongoId((string) $this->id))); if (!empty($old_product->related_products) && is_array($old_product->related_products)) { sort($old_product->related_products); } else { $old_product->related_products = array(); } $old_products = $old_product->related_products; } $this->__old_products = $old_products; unset($this->parent); unset($this->new_category_title); return parent::beforeValidate(); }