1: <?php
2:
3:
4: namespace app\controllers;
5: use \mako\http\routing\Controller;
6: use \mako\syringe\ContainerAwareTrait;
7: use \mako\auth\Gatekeeper;
8:
9: //require_once ('/var/www/avisproject/SearchAvisData.php');
10: //require_once '/var/www/hbrutvikling/hbr/app/controllers/SearchAvisData.php';
11: //require_once ('SearchAvisData.php');
12: //include 'SearchAvisData.php';
13:
14:
15: /**
16: * person controllerold.
17: *
18: * @author Svetlana Boudko
19: */
20:
21: class PersonControllerold extends Controller
22: {
23: use ContainerAwareTrait;
24: const DAURL = "http://digitalarkivet.arkivverket.no/api/person/";
25: const json = "/json";
26: protected $fields = array( "id"=>"pfid", "avispersonid"=>"pfid","pfid"=>"pfid", "fornavn"=>"fornavn", "forenamn"=>"fornavn", "etternavn"=>"etternavn", "etternamn"=>"etternavn", "slektsnavn"=>"etternavn", "patronymikon"=>"patronymikon", "kjonn"=>"kjonn",
27: "fodselsaar"=>"fodselsaar", "year_birth"=>"fodselsaar", "fodselsdato"=>"fodselsdato", "dodsdato"=>"dodsdato", "fodested"=>"fodested", "dodssted"=>"dodssted");
28:
29: protected $tableFields = array( "rolle"=>"rolle", "navn"=>"navn", "bosted"=>"bosted", "bustad"=>"bosted" , "familiestilling"=>"familiestilling", "sivilstand"=>"sivilstand", "yrke"=>"yrke",
30: "stilling_stand"=>"yrke", "kildeid"=> "kildeid", "far"=>"", "mor"=>"", "barn"=>"", "partner"=>""
31: //, "Far/brudgom"=>"", "Mor/brud"=>"", "Partner"=>"", "Barn/andre"=>"""id"=>"pfid",
32: );
33:
34: protected $rollerMapping = array(
35: "far"=> array("barn"=>"far", "konfirmant"=>"far", "brudgom"=>"brudgommens far", "brur"=>"bruras far","trulovar"=> "brudgom"),
36: "mor"=> array("barn"=>"mor", "konfirmant"=>"mor", "trulovar"=> "brur"),
37: "barn"=> array("mor"=>"barn", "far"=>"barn", "mor"=>"konfirmant", "far"=>"konfirmant", "fadder"=>"barn", "bruras far"=> "brur", "brur far"=> "brur", "brudgommens far"=>"brudgom"),
38: "partner"=> array("mor"=>"far", "far"=>"mor", "brur"=>"brudgom", "brudgom"=>"brur")
39: );
40:
41: protected $familyMapping = array(
42: "far"=> array("barn"=>"far", "konfirmant"=>"far", "brudgom"=>"brudgommens far", "brur"=>"bruras far"),
43: "mor"=> array("barn"=>"mor", "konfirmant"=>"mor"),
44: "barn"=> array("mor"=>"barn", "far"=>"barn", "mor"=>"konfirmant", "far"=>"konfirmant", "bruras far"=> "brur", "brur far"=> "brur", "brudgommens far"=>"brudgom"),
45: "partner"=> array("mor"=>"far", "far"=>"mor", "brur"=>"brudgom", "brudgom"=>"brur")
46: );
47:
48: public $types = array('da'=>'daid', 'hbr'=>'hbrid','avis'=>'avisid');
49:
50: protected $projecturl = "/hbrutvikling/hbr/index.php/";
51:
52: private $testperson;
53:
54: private $facade;
55: /*
56: public function getDAPerson($id)
57: {
58: $person = NULL;
59: $url = self::DAURL.$id.self::json;
60: $person = file_get_contents($url);
61: return $person;
62: }
63:
64: private function initPersonData()
65: {
66: $person = array();
67: foreach ($this->fields as $field=>$value)
68: {
69: if(!array_key_exists($value, $person))
70: $person[$value] = "";
71: }
72: return $person;
73: }
74:
75:
76:
77: public function getPersonfromFK($entry, $type)
78: {
79: $person = $this->initPersonData();
80: if ($type == 'daid')
81: {
82: foreach ($this->fields as $field=>$value)
83: {
84: if (property_exists($entry->individual, $field) && !empty($entry->individual->{$field}))
85: {
86: if ($field == 'fodselsdato')
87: {
88: $person[$value] = $this->processDate($entry->individual->{$field});
89: }
90: else
91: {
92: $person[$value] = $entry->individual->{$field};
93: }
94: }
95: }
96: }
97: else if ($type == 'avisid')
98: {
99: foreach ($this->fields as $field=>$value)
100: {
101: if (property_exists($entry, $field) && !empty($entry->{$field}))
102: {
103: $person[$value] = $entry->{$field};
104: }
105: }
106:
107: }
108: else
109: {
110: }
111: return $person;
112: }
113:
114: private function packMember($pfid, $entry)
115: {
116: //
117: $res = array();
118: //$url = $this->projecturl.'person/daid/'.$pfid;
119: foreach ($entry["other"] as $key=>$member)
120: {
121: if ($member["pfid"] == $pfid)
122: {
123: $name = $member["navn"];
124: }
125: }
126: $res['pfid'] = $pfid;
127: $res['name'] = $name;
128: $res['type'] ='daid';
129: return $res;
130:
131: }
132:
133: private function getFamilyforAvis($entries)
134: {
135: $families = array();
136: $dbfamily = array();
137: $pfid = '';
138: $avisSearch = new SearchAvisData();
139: if ($entries != NULL && property_exists($entries, 'autoid'))
140: {
141: $dbfamily = $avisSearch->getFamilyById($entries->autoid);
142: if ($dbfamily != NULL)
143: {
144: $dbfamily = $this->processAvisEntry($dbfamily);
145: if ($dbfamily != NULL)
146: {
147: $families['Partner og barn'] = array();
148: $families['Partner og barn'][] = array();
149: $pfid = $dbfamily->pfid;
150: $families['Partner og barn'][0][] = array('partner', array("pfid"=>$dbfamily->pfid, "name"=>$dbfamily->navn, "type"=>"avisid"));
151: }
152: }
153: }
154:
155: return array('families'=>$families, 'other'=>$dbfamily, 'pfid'=>$pfid);
156: }
157:
158: private function getFamilyforFK($persondata, $oneentry)
159: {
160: $families = array();
161: $partnerogbarn = array("barn", "partner");
162: $foreldre = array("mor", "far");
163:
164: foreach ($foreldre as $member)
165: {
166: $m = $this->processField($member, $persondata, $this->familyMapping);
167: if (!empty($m))
168: {
169: if (!array_key_exists('Foreldre og søsken', $families))
170: {
171: $families['Foreldre og søsken'] = array();
172: $families['Foreldre og søsken'][] = array();
173: }
174: $families['Foreldre og søsken'][0][] = array($member, $this->packMember($m, $oneentry));
175: }
176: }
177: foreach ($partnerogbarn as $member)
178: {
179: $m = $this->processField($member, $persondata, $this->familyMapping);
180: if (!empty($m))
181: {
182: if (!array_key_exists('Partner og barn', $families))
183: {
184: $families['Partner og barn'] = array();
185: $families['Partner og barn'][] = array();
186: }
187: $families['Partner og barn'][0][] = array($member, $this->packMember($m, $oneentry));
188: }
189: }
190:
191: return $families;
192: }
193:
194: private function processField($field, $persondata, $mapping)
195: {
196: //
197: $map = $mapping[$field];
198: $rolle = $persondata->individual->rolle;
199: if(array_key_exists($rolle, $map))
200: {
201: $search = $map[$rolle];
202: if (property_exists($persondata, "individuals"))
203: {
204: foreach($persondata->individuals as $individ)
205: {
206: if ($individ->rolle == $search)
207: return $individ->id;
208: }
209: }
210: }
211: return '';
212: }
213:
214: private function processDate($date)
215: {
216: //
217: $res = trim($date);
218: if (strlen($res) == 5)
219: {
220: $parts = explode ("-" , $res);
221: if (count($parts) == 2)
222: {
223: $res = $parts[1].".".$parts[0];
224: return $res;
225: }
226: }
227: return $res;
228: }
229:
230: private function getEntry($individual, $persondata) //$persondata->individual
231: {
232: $entry = array();
233: $entry["type"] = "daid";
234: foreach ($this->fields as $field=>$value)
235: {
236: if (property_exists($individual, $field) && !empty($individual->{$field}))
237: {
238: if ($field == 'fodselsdato')
239: {
240: $entry[$value] = $this->processDate($individual->{$field});
241: }
242: else
243: {
244: $entry[$value] = $individual->{$field};
245: }
246: }
247: }
248: foreach ($this->tableFields as $field=>$value)
249: {
250: if (property_exists($individual, $field) && !empty($individual->{$field}))
251: $entry[$value] = $individual->{$field};
252: else
253: {
254: if ($field == "navn")
255: {
256: $entry["navn"] = '';
257: if (array_key_exists("fornavn", $entry))
258: $entry["navn"] .= $entry["fornavn"];
259: if (array_key_exists("patronymikon", $entry))
260: $entry["navn"] .= ' '.$entry["patronymikon"];
261: if (array_key_exists("etternavn", $entry))
262: $entry["navn"] .= ' '.$entry["etternavn"];
263: }
264: else if ($persondata != NULL && ($field == "mor" || $field == "far" || $field == "barn" || $field == "partner"))
265: {
266: $entry[$field] = $this->processField($field, $persondata, $this->rollerMapping);
267: }
268: }
269: }
270: return $entry;
271: }
272:
273: private function processDAEntry($persondata)
274: {
275: //
276: $entry = array();
277: $id = '';
278: if (property_exists($persondata, "individual"))
279: {
280: $entry["individual"] = $this->getEntry($persondata->individual, $persondata);
281:
282: $id = $persondata->individual->autoid;
283: }
284: if (property_exists($persondata, "individuals"))
285: {
286: $entry["other"] = array();
287: foreach($persondata->individuals as $individ)
288: {
289: if ( property_exists($individ, "autoid") && $individ->autoid != $id)
290: {
291: if (property_exists($individ, "rolle") && !empty($individ->rolle))
292: {
293: //
294: $entry["other"][$individ->rolle] = $this->getEntry($individ, NULL);
295: }
296: else
297: {
298: //
299: $entry["other"]["undef"] = $this->getEntry($individ, NULL);
300: }
301: }
302: }
303: }
304: return $entry;
305: }
306:
307: private function updateField($type, $id, $field, $value)
308: {
309: //
310: $newid = false;
311: $person = new Person();
312: switch($type)
313: {
314: case 'daid': //create new person entry
315: $entries = json_decode($this->getDAPerson($id));
316: $persondata = $this->getPersonfromFK($entries, $type);
317: $person->setFieldsFromAPI($persondata);
318: $this->testperson = $value;
319: //$this->testperson = $field;
320: for ($ii = 0; $ii < count($field); $ii++)
321: {
322: $person->set($field[$ii], $value[$ii]);
323:
324: }
325: $person->addEntry(array("id"=>$entries->individual->autoid , "pfid"=>$entries->individual->id, "type"=>$type)); // $persondata["pfid"]));
326: $newid = $person->save();
327: //$this->testperson = $person;
328: break;
329: case 'avisid':
330: $avisSearch = new SearchAvisData();
331: $entries = $avisSearch->getPersonByPFid($id);
332: $entries = $this->processAvisEntry($entries);
333: $persondata = $this->getPersonfromFK($entries, $type);
334: $person->setFieldsFromAPI($persondata);
335: for ($ii = 0; $ii < count($field); $ii++)
336: {
337: $person->set($field[$ii], $value[$ii]);
338:
339: }
340: $person->addEntry(array("id"=>$entries->autoid , "pfid"=>$entries->pfid, "type"=>$type));
341: $newid = $person->save();
342: break;
343: case 'hbrid':
344: $person->update($field, $value, $id);
345: break;
346: }
347: return $newid;
348: }
349: */
350: private function isAuthenticated()
351: {
352: return $this->gatekeeper->isLoggedIn();
353: }
354: /*
355: public function personExists($pfid)
356: {
357: $pson = new Person();
358: $ids = $pson->getPersonForPFID($pfid);
359: if ($ids != NULL)
360: return $ids["pid"];
361: else
362: return NULL;
363: }
364:
365: private function processFamilies($families)
366: {
367: $processed = array();
368: $processed['Partner og barn'] = array();
369: $processed['Foreldre og søsken'] = array();
370: foreach($families as $f)
371: {
372: if (array_key_exists('Foreldre og søsken', $f))
373: {
374: foreach ($f['Foreldre og søsken'] as $family)
375: $processed['Foreldre og søsken'][] = $family;
376: }
377: if (array_key_exists('Partner og barn', $f))
378: {
379: foreach ($f['Partner og barn'] as $family)
380: $processed['Partner og barn'][] = $family;
381: }
382: }
383: return $processed;
384: }
385:
386: private function wrappDate($date)
387: {
388: if (strlen ($date) == 1)
389: return '0'.$date;
390: else
391: return $date;
392: }
393:
394: public function processAvisEntry($entries)
395: {
396: if ($entries != NULL)
397: {
398: $entries->type = 'avisid';
399: if (property_exists($entries,"navn" ))
400: {
401: $entries->fornavn = $entries->navn;
402: unset($entries->navn);
403: }
404: if (property_exists($entries,"avispersonid" ))
405: {
406: $entries->pfid = $entries->avispersonid;
407: unset($entries->avispersonid);
408: }
409: $entries->fodselsdato = '';
410: $entries->dodsdato = '';
411: if (property_exists($entries,"day_birth" ) && (!empty($entries->day_birth)))
412: {
413: $entries->fodselsdato = $this->wrappDate($entries->day_birth).'.';
414: }
415: if (property_exists($entries,"month_birth" ) && (!empty($entries->month_birth)))
416: {
417: $entries->fodselsdato .= $this->wrappDate($entries->month_birth).'.';
418: }
419: if (property_exists($entries,"year_birth" ) && (!empty($entries->year_birth)))
420: {
421: $entries->fodselsdato .= $entries->year_birth;
422: }
423: if (property_exists($entries,"avispersonid" ))
424: {
425: $entries->pfid = $entries->avispersonid;
426: unset($entries->avispersonid);
427: }
428: if (property_exists($entries,"day_death" ) && (!empty($entries->day_death)))
429: {
430: $entries->dodsdato = $this->wrappDate($entries->day_death).'.';
431: }
432: if (property_exists($entries,"month_death" ) && (!empty($entries->month_death)))
433: {
434: $entries->dodsdato .= $this->wrappDate($entries->month_death).'.';
435: }
436: if (property_exists($entries,"year_death" ) && (!empty($entries->year_death)))
437: {
438: $entries->dodsdato .= $entries->year_death;
439: }
440: $entries->fodselsdato = trim($entries->fodselsdato);
441: $entries->fodselsdato = rtrim($entries->fodselsdato, '.');
442: $entries->dodsdato = trim($entries->dodsdato);
443: $entries->dodsdato = rtrim($entries->dodsdato, '.');
444: $entries->navn = $entries->fornavn.' '.$entries->etternavn;
445: $entries->navn = trim($entries->navn);
446: }
447: return $entries;
448: }
449: */
450: public function show($type, $id)
451: {
452: $this->facade = new PersonFacade($type, $id);
453: $this->session->put('lasturl', $this->urlBuilder->current());
454: $personcart = new PersonCartController();
455: $personcart->setPersonController($this);
456: $cart = NULL;
457: $opencart = false;
458: $test = '';
459: $edit = false;
460: $person = array();
461: $useautoid = false;
462: $currenttype = $type;
463: $currentid = $id;
464: $exist = NULL;
465: $data = NULL;
466:
467: //session_start();
468: //check if there exists person for requested pfid
469: /*
470: if ($type == 'daid' || $type == 'avisid')
471: {
472: $exist = $this->personExists($id);
473: if ($exist != NULL)
474: {
475: $currenttype = 'hbrid';
476: $currentid = $exist;
477: }
478: } */
479: if ($_REQUEST != NULL)
480: {
481: if (array_key_exists('editBio', $_REQUEST) )
482: {
483: $field = 'biocontent';
484: $bio = array($this->request->data($field));
485: //save changes
486: $fields = array('remarks');
487: //$updateid=
488: $this->facade->updateField($fields, $bio);//($currenttype, $currentid, $fields, $bio);
489: /*if ($updateid !== false)
490: {
491: $currenttype = 'hbrid';
492: $useautoid = true;
493: $currentid = $updateid;
494: }
495: else
496: {
497: //$currenttype = $type;
498: }*/
499: }
500: else if (array_key_exists('editRef', $_REQUEST))
501: {
502: $field = 'refcontent';
503: $ref = $this->request->data($field);
504: //save changes
505: //$opp = $this->savePersonRef($ref, $type, $id);
506: }
507: else if (array_key_exists('openeditpersondetails', $_REQUEST))
508: {
509: $edit = true;
510: }
511: else if (array_key_exists('savepersondata', $_REQUEST))
512: {
513: $pm = new PersonMappings();
514: $fields = array();
515: $values = array();
516: foreach ($pm->webDbMapping as $map=>$value)
517: {
518: //
519: if (array_key_exists($map, $_REQUEST))
520: {
521: $fields[] = $value;
522: $values[] = $_REQUEST[$map];
523: }
524: }
525: //$updateid=
526: $this->facade->updateField($fields, $values);//($currenttype, $currentid, $fields, $values);
527: /*if ($updateid !== false)
528: {
529: $currenttype = 'hbrid';
530: $useautoid = true;
531: $currentid = $updateid;
532: }
533: else
534: {}*/
535: }
536: else if (array_key_exists('addmerge', $_REQUEST))
537: {
538: //
539: $cart = $personcart->addToCart($type, $id);
540: }
541: else if (array_key_exists('open', $_REQUEST))
542: {
543: //
544: $opencart = true;
545: }
546: else if (array_key_exists('close', $_REQUEST))
547: {
548: //
549: $opencart = false; //explicit
550: }
551: else if (array_key_exists('remove', $_REQUEST))
552: {
553: //
554: $personcart->removeFromCart($_REQUEST);
555: }
556: else if (array_key_exists('merge', $_REQUEST))
557: {
558: //
559: $newperson = $personcart->merge($_REQUEST);
560:
561: }
562: else
563: {
564: }
565: }
566: $person = $this->facade->getPerson();
567: /*
568: switch($currenttype)
569: {
570: case 'daid':
571: $person = $this->daQueries->getPersonData($currentid);
572: break;
573: case 'avisid': //todo restructure
574: $person = $this->avisQueries->getPersonData($currentid);*/
575: /*
576: $avisSearch = new SearchAvisData();
577: $entries = $avisSearch->getPersonByPFid($currentid);
578: $entries = $this->processAvisEntry($entries);
579: $persondata = $this->getPersonfromFK($entries, $currenttype);
580: $avisfamily = $this->getFamilyforAvis($entries);
581: $entries->partner = $avisfamily['pfid'];
582: $person['family'] = $avisfamily['families'] ;
583: $person['persondata'] = $persondata;
584: $allentries = array();
585: $oneentry = array();
586: $oneentry["individual"] = (array) $entries;
587: if ($avisfamily['other'] != NULL)
588: {
589: $oneentry["other"] = array();
590: $oneentry["other"]["partner"] = (array)$avisfamily['other'];
591: }
592: $allentries[] = $oneentry;
593: $person['entries'] = $allentries;*/
594: /* break;
595: case 'hbrid':
596: $pson = new Person();
597: $family = array();
598: if ($useautoid === true)
599: {
600: $data = $pson->getPersonByID($currentid);
601: }
602: else
603: {
604: $data = $pson->getPersonByPFID($currentid);
605: }
606: $allentries = array();
607: //$person['family'] = $this->getFamilyForPerson($data);
608: //get family from FKs
609: if (!empty($data) && !empty($data['pfids']) && !empty($data['types']))
610: {
611: //restructure
612: $i = 0;
613: foreach($data['pfids'] as $pfid)
614: {
615: if ($data['types'][$i] == 'avisid')
616: {
617: $avisSearch = new SearchAvisData();
618: $entries = $avisSearch->getPersonByPFid($pfid); //$currentid);
619: $entries = $this->processAvisEntry($entries);
620: $persondata = $this->getPersonfromFK($entries, $currenttype);
621: $avisfamily = array();
622: $avisfamily = $this->getFamilyforAvis($entries);
623: $entries->partner = $avisfamily['pfid'];
624: $person['persondata'] = $persondata;
625: $allentries = array();
626: $oneentry = array();
627: $oneentry["individual"] = (array) $entries;
628: if ($avisfamily['other'] != NULL)
629: {
630: $oneentry["other"]= array();
631: $oneentry["other"]["partner"] = (array)$avisfamily['other'];
632: }
633: $family[] = $avisfamily['families'];
634: $allentries[] = $oneentry;
635: }
636: else
637: {
638: $entries = json_decode($this->getDAPerson($pfid));
639: if (!empty($entries))
640: {
641: $persondata = $this->getPersonfromFK($entries, $currenttype);
642: $oneentry = $this->processDAEntry($entries);
643: $family[] = $this->getFamilyforFK($entries, $oneentry);
644: $allentries[] = $oneentry;
645: }
646: }
647: $i = $i +1;
648: }
649: }
650: $this->testperson= $family;
651: $person['family'] = $this->processFamilies($family);
652: $person['entries'] = $allentries;
653: $person['persondata'] = $data['person'];
654: break;
655: } */
656: $cart = $personcart->getCart();
657: $person["authenticated"] = $this->isAuthenticated();
658: $person ['edit'] = $edit;
659: return $this->view->render('personview.showperson', array('test'=>$_REQUEST, 'type'=>$type, 'id'=>$id, 'person'=>$person, 'cart'=>array('data'=>$cart, 'open'=>$opencart))); //json_encode($person), 'persondata'=>$persondata));
660: }
661: }