public function onBeforeUpdate()
 {
     /** @var $model \Mindy\Orm\TreeModel */
     $model = $this->getModel();
     // Случай когда обнулен slug, например из админки
     if (empty($model->{$this->name})) {
         $model->{$this->name} = Meta::cleanString($model->{$this->source});
     }
     // if remove parent (parent is null)
     if (!$model->parent) {
         if (strpos($model->{$this->name}, '/') === false) {
             $url = Meta::cleanString($model->{$this->name});
         } else {
             if ($model->{$this->name}) {
                 $slugs = explode('/', $model->{$this->name});
                 $url = end($slugs);
             } else {
                 $url = Meta::cleanString($model->{$this->source});
             }
         }
     } else {
         $parentUrl = $model->parent->{$this->name};
         $slugs = explode('/', $model->{$this->name});
         $url = $parentUrl . '/' . end($slugs);
     }
     $url = ltrim($url, '/');
     if ($this->unique) {
         $url = $this->uniqueUrl($url, 0, $model->pk);
     }
     $model->setAttribute($this->name, $url);
     $schema = ConnectionManager::getDb()->getSchema();
     $model->tree()->filter(['lft__gt' => $model->getOldAttribute('lft'), 'rgt__lt' => $model->getOldAttribute('rgt'), 'root' => $model->getOldAttribute('root')])->update([$this->name => new Expression("REPLACE(" . $schema->quoteColumnName($this->name) . ", :from, :to)", [':from' => $model->getOldAttribute($this->name), ':to' => $url])]);
 }
Example #2
0
 /**
  * @param $owner
  * @void
  */
 public function afterOwnerSave($owner)
 {
     if (method_exists($owner, 'getAbsoluteUrl')) {
         $metaConfig = $owner->metaConfig;
         $attributes = $this->getAttributes();
         if (empty($attributes['is_custom'])) {
             $this->setAttributes(['title' => $owner->{$metaConfig['title']}, 'keywords' => MetaGenerator::generateKeywords($owner->{$metaConfig['keywords']}), 'description' => MetaGenerator::generateDescription($owner->{$metaConfig['description']}), 'url' => $owner->getAbsoluteUrl()]);
         } else {
             $this->setAttributes(['url' => $owner->getAbsoluteUrl()]);
         }
     }
 }
Example #3
0
 public function onBeforeUpdate()
 {
     /** @var $model \Mindy\Orm\TreeModel */
     $model = $this->getModel();
     // Случай когда обнулен slug, например из админки
     if (empty($model->{$this->name})) {
         $this->value = Meta::cleanString($model->{$this->source});
     }
     if ($this->unique) {
         $this->value = $this->uniqueUrl($this->value, 0, $model->pk);
     }
     $model->setAttribute($this->name, $this->value);
 }
Example #4
0
 /**
  * @param \Mindy\Form\BaseForm|ModelForm $owner
  * @param array $attributes
  * @throws Exception
  * @return array
  */
 public function beforeSetAttributes($owner, array $attributes)
 {
     $model = $owner->getModel();
     $ownerModel = $owner->getParentForm()->getInstance();
     if (!isset($ownerModel->metaConfig)) {
         throw new Exception("metaConfig doesn't exists in model: " . $model->className());
     }
     $meta = $ownerModel->metaConfig;
     if (empty($attributes['is_custom'])) {
         $attributes = array_merge($attributes, ['title' => $ownerModel->{$meta['title']}, 'keywords' => MetaGenerator::generateKeywords($ownerModel->{$meta['keywords']}), 'description' => MetaGenerator::generateDescription($ownerModel->{$meta['description']})]);
     }
     return parent::beforeSetAttributes($owner, $attributes);
 }