Esempio n. 1
0
 public function saveGroup()
 {
     $cleanUrl = preg_replace("/[^a-zA-Z0-9\\/_|+ -]/", '', $this->name);
     $cleanUrl = strtolower(trim($cleanUrl, '-'));
     $cleanUrl = preg_replace("/[\\/_|+ -]+/", '-', $cleanUrl);
     $this->url = $cleanUrl;
     $urlExists = Group::findOne(['url' => $cleanUrl]);
     if ($urlExists) {
         if ($this->isNewRecord) {
             $lastGroup = Group::find()->orderBy(['id' => SORT_DESC])->one();
             $this->url = $lastGroup->id++ . '-' . $this->url;
         } else {
             if ($urlExists->id !== $this->id) {
                 $this->url = $this->id . '-' . $this->url;
             }
         }
     }
     if (!$this->save()) {
         return false;
     }
     $tags = explode(',', str_replace(['[', ']', '\''], ['', '', ''], $this->tag));
     foreach ($tags as $tag) {
         $newTag = Tag::findOne(['name' => strtolower($tag)]);
         if (!$newTag) {
             $newTag = new Tag();
             $newTag->name = strtolower($tag);
             if ($newTag->save()) {
                 $tagToGroup = new TagToGroup();
                 $tagToGroup->tag_id = $newTag->id;
                 $tagToGroup->group_id = $this->id;
                 $tagToGroup->save();
             }
         } else {
             $tagToGroup = new TagToGroup();
             $tagToGroup->tag_id = $newTag->id;
             $tagToGroup->group_id = $this->id;
             $tagToGroup->save();
         }
     }
     $userToGroup = new UserToGroup();
     $userToGroup->user_id = Yii::$app->user->getIdentity()->id;
     $userToGroup->group_id = $this->id;
     $userToGroup->can_edit = 1;
     $userToGroup->group_admin = 1;
     if ($userToGroup->save()) {
         return true;
     }
     return false;
 }