/**
  * Run the migrations.
  *
  * @return void
  */
 public function up()
 {
     if (!Schema::hasTable('tags')) {
         Schema::create('tags', function (Blueprint $table) {
             $table->increments('id')->unsigned();
             $table->bigInteger('parent_id')->default(0);
             $table->string('name', 255)->unique();
             $table->string('bgcolor', 50)->nullable();
             $table->boolean('group');
             $table->timestamps();
         });
         $groups = ['status', 'type', 'resolution'];
         foreach ($groups as $group) {
             $model = new Model\Tag();
             $model->name = $group;
             $model->group = true;
             $model->save();
         }
         $tags = [['name' => 'open', 'parent_id' => 'status', 'bgcolor' => '#c43c35'], ['name' => 'testing', 'parent_id' => 'status', 'bgcolor' => '#6c8307'], ['name' => 'closed', 'parent_id' => 'status', 'bgcolor' => '#46a546'], ['name' => 'feature', 'parent_id' => 'type', 'bgcolor' => '#62cffc'], ['name' => 'bug', 'parent_id' => 'type', 'bgcolor' => '#f89406'], ['name' => 'won\'t fix', 'parent_id' => 'resolution', 'bgcolor' => '#812323'], ['name' => 'fixed', 'parent_id' => 'resolution', 'bgcolor' => '#048383']];
         foreach ($tags as $tag) {
             $model = new Model\Tag();
             $model->name = $tag['name'];
             $model->bgcolor = $tag['bgcolor'];
             $model->parent_id = Model\Tag::where('name', '=', $tag['parent_id'])->first()->id;
             $model->group = false;
             $model->save();
         }
     }
     if (!Schema::hasTable('projects_issues_tags')) {
         Schema::create('projects_issues_tags', function (Blueprint $table) {
             $table->bigInteger('issue_id');
             $table->bigInteger('tag_id');
             $table->primary(['issue_id', 'tag_id']);
         });
     }
     $openIssues = Model\Project\Issue::where('status', '=', Model\Project\Issue::STATUS_OPEN)->lists('id');
     if (count($openIssues) > 0) {
         $openTag = Model\Tag::where('parent_id', '=', Model\Tag::where('name', '=', 'status')->first()->id)->where('name', '=', 'open')->first();
         $openTag->issues()->attach($openIssues);
     }
     $closedIssues = Model\Project\Issue::where('status', '=', Model\Project\Issue::STATUS_CLOSED)->lists('id');
     if (count($closedIssues) > 0) {
         $closedTag = Model\Tag::where('parent_id', '=', Model\Tag::where('name', '=', 'status')->first()->id)->where('name', '=', 'closed')->first();
         $closedTag->issues()->attach($closedIssues);
     }
     // Create activity type for tag update
     $activity = new Model\Activity();
     $activity->description = 'Updated issue tags';
     $activity->activity = 'update-issue-tags';
     $activity->save();
 }
Ejemplo n.º 2
0
 /**
  * Create a new tag if valid or return existing one
  *
  * @param string   $name
  * @param null|Tag $parent
  *
  * @return bool|$this
  */
 public function validOrCreate($name, Tag $parent = null)
 {
     $group = $parent === null ? true : false;
     $tag = $this->where('name', '=', $name)->first();
     if ($tag && $tag->group != $group) {
         return false;
     }
     if (!$tag) {
         $tag = new Tag();
         $tag->name = $name;
         $tag->group = $group;
         if (!is_null($parent)) {
             $tag->parent_id = $parent->id;
             $tag->setRelation('parent', $parent);
         }
         $tag->save();
     }
     return $tag;
 }