Exemplo n.º 1
0
 /**
  * @param Event $event
  * @return mixed the value of the user.
  */
 public function getValue($event)
 {
     if (!$this->value) {
         $filter = new FilterSeoPageName();
         $filter->maxLength = $this->maxLength;
         if ($this->owner->{$this->generatedAttribute}) {
             $seoPageName = $filter->filter($this->owner->{$this->generatedAttribute});
         } else {
             $seoPageName = $filter->filter($this->owner->{$this->fromAttribute});
         }
         //Нужно чтобы поле было уникальным
         if ($this->uniqeue) {
             if (!$this->owner->isNewRecord) {
                 //Значит неуникально
                 if ($founded = $this->owner->find()->where([$this->generatedAttribute => $seoPageName])->andWhere(["!=", "id", $this->owner->id])->one()) {
                     if ($last = $this->owner->find()->orderBy('id DESC')->one()) {
                         $seoPageName = $seoPageName . '-' . $last->id;
                         return $filter->filter($seoPageName);
                     }
                 }
             } else {
                 //Значит неуникально
                 if ($founded = $this->owner->find()->where([$this->generatedAttribute => $seoPageName])->one()) {
                     if ($last = $this->owner->find()->orderBy('id DESC')->one()) {
                         $seoPageName = $seoPageName . '-' . $last->id;
                         return $filter->filter($seoPageName);
                     }
                 }
             }
         }
         return $seoPageName;
     } else {
         return call_user_func($this->value, $event);
     }
 }
Exemplo n.º 2
0
 /**
  * Автоматическая генерация code по названию
  * @return $this
  */
 public function generateCode()
 {
     if ($this->isRoot()) {
         $this->setAttribute("code", null);
     } else {
         $filter = new FilterSeoPageName();
         $filter->maxLength = \Yii::$app->cms->tree_max_code_length;
         $this->code = $filter->filter($this->name);
         $matches = [];
         //Роутинг элементов нужно исключить
         if (preg_match('/(?<id>\\d+)\\-(?<code>\\S+)$/i', $this->code, $matches)) {
             $this->code = "s" . $this->code;
         }
         if (!$this->isValidCode()) {
             $this->code = $filter->filter($this->code . "-" . substr(md5(uniqid() . time()), 0, 4));
             if (!$this->isValidCode()) {
                 $this->generateCode();
             }
         }
     }
     return $this;
 }