Completed
Push — master ( c8f12a...0c069d )
by Curtis
11:16 queued 05:15
created

GedcomController::store()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Gedcom;
4
5
use Illuminate\Http\Request;
6
use App\Http\Controllers\Controller;
7
use App\Individual;
8
use App\Family;
9
use App\Event;
10
use App\Note;
11
12
class GedcomController extends Controller
13
{
14
15
  /*
16
  * Api end-point for Gedcom api/gedcom/store
17
  * Saving uploaded file to storage and starting to read
18
  */
19
20
  public function store(Request $request)
21
  {
22
    if($request->hasFile('file')){
23
      if ($request->file('file')->isValid()) {
24
        $request->file->storeAs('gedcom', 'file.ged');
25
        $this->readData();
26
        return ['File uploaded'];
27
      }
28
      return ['File corrupted'];
29
    }
30
    return ['Not uploaded'];
31
  }
32
33
  /*
34
  * Read ged file
35
  */
36
37
  protected $persons_id = array();
38
  protected $list = array();
39
  protected $familylist = array();
40
41
  private function readData()
42
  {
43
    $parser = new \PhpGedcom\Parser();
44
    $gedcom = $parser->parse(storage_path('app/gedcom/file.ged'));
45
46
    $individuals    = $gedcom->getIndi();
47
    $families       = $gedcom->getFam();
48
    foreach ($individuals as $individual) {
49
        $this->get_Person($individual);
50
    }
51
    foreach ($families as $family) {
52
        $this->get_Family($family);
53
    }
54
  }
55
56
  private function get_Person($individual){
57
          $g_id   = $individual->getId();
0 ignored issues
show
Unused Code introduced by
The assignment to $g_id is dead and can be removed.
Loading history...
58
          $surn   = current($individual->getName())->getSurn();
59
          $givn   = current($individual->getName())->getGivn();
60
          $name   = current($individual->getName())->getName();
61
          $sex    = $individual->getSex();
62
          $attr   = $individual->getAttr();
63
          $events = $individual->getEven();
64
          $media  = $individual->getObje();
65
66
          if($givn == "") {
67
              $givn = $name;
0 ignored issues
show
Unused Code introduced by
The assignment to $givn is dead and can be removed.
Loading history...
68
          }
69
70
          $sex = $sex === 'F' ? 'female' : 'male';
71
          $surn = isset($surn) ? $surn : 'No Surname';
72
73
          foreach($events as $event){
74
              $date = $this->get_date($event->getDate());
75
              $place = $this->get_place($event->getPlac());
76
              if(isset($date) || isset($place)){
0 ignored issues
show
Bug introduced by
The function  isset was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
              if(isset($date) ||/** @scrutinizer ignore-call */  isset($place)){
Loading history...
77
                $place = isset($place) ? $place : 'No place';
78
                Event::create([
79
                  'event_type' => 'App\Individual',
80
                  'event_date' => $date,
81
                  'event_id'   => 1,
82
                  'name' => mb_convert_encoding($place, 'UTF-8', 'UTF-8'),
83
                  'description' => mb_convert_encoding($place, 'UTF-8', 'UTF-8'),
84
                  'event_type_id' => 1
85
                ]);
86
              }
87
          };
88
89
          foreach($attr as $event){
90
              $date = $this->get_date($event->getDate());
91
              $place = $this->get_place($event->getPlac());
92
              if(count($event->getNote())>0){
93
                  $note = current($event->getNote())->getNote();
94
              }
95
              else{
96
                  $note = '';
97
              }
98
              if(isset($date) || isset($place)){
99
                $place = isset($place) ? $place : 'No place';
100
                Note::create([
101
                  'name' => mb_convert_encoding($place, 'UTF-8', 'UTF-8'),
102
                  'date' => mb_convert_encoding($date, 'UTF-8', 'UTF-8'),
103
                  'description' => mb_convert_encoding($note, 'UTF-8', 'UTF-8'),
104
                ]);
105
              }
106
          };
107
          foreach ($media as $mediafile){
108
              $title = $mediafile->getTitl();
109
              $file  = $mediafile->getFile();
110
              if(isset($title) || isset($file)){
111
                Note::create([
112
                  'name' => mb_convert_encoding($title, 'UTF-8', 'UTF-8'),
113
                  'description' => mb_convert_encoding($file, 'UTF-8', 'UTF-8')
114
                ]);
115
              }
116
          };
117
          Individual::create([
118
            'first_name' => $name,
119
            'last_name'  => $surn,
120
            'gender'     => $sex,
121
            'is_active'  => false
122
          ]);
123
  }
124
125
  private function get_Family($family){
126
      $g_id = $family->getId() ;
127
      $husb = $family->getHusb();
128
      $wife = $family->getWife();
129
      $children = $family->getChil();
130
      $events =$family->getEven();
131
      $husband_id = (isset($this->persons_id[$husb])) ? $this->persons_id[$husb]: 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $husband_id is dead and can be removed.
Loading history...
132
      $wife_id    = (isset($this->persons_id[$wife])) ? $this->persons_id[$wife]: 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $wife_id is dead and can be removed.
Loading history...
133
134
      Family::create([
135
        'father_id' => rand(1,100),
136
        'mother_id' => rand(1,100),
137
        'description' => $g_id . ' family',
138
        'type_id' => 1
139
      ]); //father and mother id should be gedcom(string) type, not integer
140
141
      foreach($children as $child){
142
          if (isset($this->persons_id[$child])){
143
              $individual = Individual::find($this->persons_id[$child]);
144
              $individual->children()->save($individual);
145
          }
146
      }
147
      foreach ($events as $event){
148
          $date = $this->get_date($event->getDate());
149
          $place = $this->get_place($event->getPlac());
150
          if(isset($date) || isset($place)){
0 ignored issues
show
Bug introduced by
The function  isset was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
          if(isset($date) ||/** @scrutinizer ignore-call */  isset($place)){
Loading history...
151
            $place = isset($place) ? $place : 'No place';
152
            Event::create([
153
              'event_type' => 'App\Family',
154
              'event_date' => mb_convert_encoding($date, 'UTF-8', 'UTF-8'),
155
              'event_id'   => 1,
156
              'name' => mb_convert_encoding($place, 'UTF-8', 'UTF-8'),
157
              'description' => mb_convert_encoding($place, 'UTF-8', 'UTF-8'),
158
              'event_type_id' => 1
159
            ]);
160
          }
161
      };
162
    }
163
164
    private function get_date($input_date){
165
        return "$input_date";
166
    }
167
168
    private function get_place($place){
169
        if(is_object($place)){
170
            $place = $place->getPlac();
171
        }
172
        return $place;
173
    }
174
}
175